diff --git a/admin/controllers/items/Cost.php b/admin/controllers/items/Cost.php index 3371c68a..0051884b 100644 --- a/admin/controllers/items/Cost.php +++ b/admin/controllers/items/Cost.php @@ -22,12 +22,109 @@ class Cost extends HD_Controller{ $this->lists(); } + private function get_where(&$params){ + $where = []; + + # ref \libraries\Ordersv2List.php private function orders($params = array()), 非lc_items_cost相关组装为in查询 + //lc_items + //品牌 车系 车型 + $where_items = $where_items_default = '1 = 1'; + if ($params['brand_id']) {//品牌 + //$where["brand_id"] = $params['brand_id']; + $where_items = $where_items." and brand_id = ".$params['brand_id']; + } else { + if (is_product()) { + //$where["brand_id<>"] = 3;//狸车品牌不显示 + $where_items = $where_items." and brand_id <> 3"; + } + $params['brand_id'] = ''; + } + if ($params['s_id']) {//车系 + //$where["s_id"] = $params['s_id']; + $where_items = $where_items." and s_id = ".$params['s_id']; + } else { + $params['s_id'] = ''; + } + if ($params['v_id']) {//车型 + $where["v_id"] = $params['v_id']; + $where_items = $where_items." and v_id = ".$params['v_id']; + } else { + $params['v_id'] = ''; + } + + if ($params['vin']) { + //$where["item_id in(select id from lc_items where vin like '%{$params['vin']}%')"] = null; + $where_items = $where_items." and vin like '%{$params['vin']}%'"; + } + if ($where_items != $where_items_default){ + $where["item_id in (select id from lc_items where $where_items)"] = null; + } + + # lc_receiver_orders_v2 + //销售员筛选 + $where_orders = $where_orders_default = '1 = 1'; + if ($params['admin_id']) { + //$where["sale_id"] = $params['admin_id']; + $where_orders = $where_orders.' and sale_id = '.$params['admin_id']; + } + else { + if ($params['biz_id_admin']) {//指定店铺所有销售员 + //$where["biz_id"] = $params['biz_id_admin']; + $where_orders = $where_orders.' and biz_id = '.$params['biz_id_admin']; + } + else { + //指定城市的所有门店 + $where_biz = array(); + if ($params['county_id_admin']) { + $where_biz['county_id'] = $params['county_id_admin']; + } + else if ($params['city_id_admin']) { + $where_biz['city_id'] = $params['city_id_admin']; + } + if ($where_biz) { + $where_biz['status>-1'] = null; + $rows_biz = $this->biz_model->select($where_biz, 'id desc', 0, 0, 'id'); + if ($rows_biz) { + $str_ids = implode(',', array_column($rows_biz, 'id')); + //$where["biz_id in({$str_ids})"] = null; + $where_orders = $where_orders." and biz_id in ({$str_ids})"; + } + else { + //$where["biz_id"] = -1; + $where_orders = $where_orders.' and biz_id = -1'; + } + } + } + !$params['city_id_admin'] && $params['city_id_admin'] = ''; + !$params['county_id_admin'] && $params['county_id_admin'] = ''; + !$params['biz_id_admin'] && $params['biz_id_admin'] = ''; + $params['admin_id'] = ''; + } + if ($where_orders != $where_orders_default){ + $where["o_id in (select id from lc_receiver_orders_v2 where $where_orders)"] = null; + } + + //开票时间 + if ($params['bill_time']) { + $bill_time = explode(' ~ ', $params['bill_time']); + if ($bill_time[0]) { + $where["bill_time >="] = $bill_time[0] . ' 00:00:00'; + } + if ($bill_time[1]) { + $where["bill_time <="] = $bill_time[1] . ' 23:59:59'; + } + } + + return $where; + } + public function lists(){ $params = $this->input->get(); $page = $params['page'] = $params['page'] ? intval($params['page']) : 1; $size = $params['size'] = $params['size'] ? intval($params['size']) : 20; $autoList = array(); - $where = []; + $where = $this->get_where($params); + $count = $this->items_cost_model->count($where); $lists = []; if($count){ @@ -41,6 +138,19 @@ class Cost extends HD_Controller{ $map_item[$item['id']] = $item; } } + + //订单(ref export) + $o_ids = implode(',',array_column($rows,'o_id')); + $map_order = []; + if($o_ids){ + $where = ["id in ($o_ids)" => null]; + $fields = "id, (select biz_name from lc_biz where id = lc_receiver_orders_v2.biz_id) as biz_name"; + $order_rows = $this->receiver_orders_v2_model->select($where,'','','',$fields); + foreach ($order_rows as $item) { + $map_order[$item['id']] = $item; + } + } + //品牌车型 $brand_arr = array_unique(array_column($item_rows, 'brand_id')); $brands = $this->auto_brand_model->get_map_by_ids($brand_arr, 'id,name'); @@ -60,8 +170,11 @@ class Cost extends HD_Controller{ $brand_name = isset($brands[$item['brand_id']]) ? $brands[$item['brand_id']][0]['name'] : ''; $series_name = isset($series[$item['s_id']]) ? $series[$item['s_id']][0]['name'] : ''; $v_name = isset($attr[$item['v_id']]) ? $attr[$item['v_id']][0]['title'] : ''; + $srv_json = json_decode($val['srv_json'],true); + $order = $map_order[$val['o_id']]; $lists[] = [ 'id' => $val['id'], + 'biz_name' => $order['biz_name'], 'title' => "{$brand_name} {$series_name} {$v_name}", 'vin' => $item['vin'], 'sale_price' => $val['sale_price'], @@ -74,6 +187,10 @@ class Cost extends HD_Controller{ 'loan_price' => $val['loan_price'], 'srv_price' => $val['srv_price'], 'car_price' => $val['car_price'], + 'dl_car_price' => $val['dl_car_price'], + 'brand_car_price' => $val['brand_car_price'], + 'if_pay' => $srv_json['if_pay'] ? intval($srv_json['if_pay']) : 0, + 'if_pay_name' => $srv_json['if_pay'] == 1 ? '是' : '', ]; } } @@ -124,7 +241,8 @@ class Cost extends HD_Controller{ 'ori_price' => $item_row['ori_price'], 'buy_price' => $item_row['buy_price'], 'company_name' => $company['short'], - 'name' => $order_row['name']."({$order_row['mobile']})", + 'name' => $order_row['owner_name']."({$order_row['owner_mobile']})", // 'name' => $order_row['name']."({$order_row['mobile']})", + 'payway' => $order_row['payway'], 'biz_type' => $this->biz_model->type_ary($biz['type']), 'biz_name' => $biz['biz_name'], 'admin_name' => $admin['uname'], @@ -160,6 +278,14 @@ class Cost extends HD_Controller{ if(!$row){ return $this->show_json(SYS_CODE_FAIL,'参数错误'); } + + $item_row = $this->items_model->get(['id'=>$row['item_id']]); + $buy_price = $post_data['buy_price'] ? $post_data['buy_price'] : 0; + $res_item = 0; + if ($item_row && $item_row['buy_price'] != $buy_price){ + $res_item = $this->items_model->update(array('buy_price'=>$buy_price),['id'=>$row['item_id']]); + } + $data = [ 'price' => $post_data['price'] ? $post_data['price'] : 0, 'cost_json' => json_encode($post_data['cost_json'],JSON_UNESCAPED_UNICODE), @@ -167,7 +293,7 @@ class Cost extends HD_Controller{ 'srv_json' => json_encode($post_data['srv_json'],JSON_UNESCAPED_UNICODE), ]; $res = $this->items_cost_model->update($data,['id'=>$row['id']]); - if(is_numeric($res)){ + if(is_numeric($res_item) || is_numeric($res)){ $this->items_cost_model->update_total($row['id']); return $this->show_json(SYS_CODE_SUCCESS,'保存成功'); }else{ @@ -208,22 +334,42 @@ class Cost extends HD_Controller{ $params['size'] = 10000; $data = $indexs = array(); $fileName = '整车成本'; - $where = []; + $where = $this->get_where($params); $count = $this->items_cost_model->count($where); if($count){ $rows = $this->items_cost_model->select($where,'id desc',$params['page'],$params['size']); + + //车辆 $item_ids = implode(',',array_column($rows,'item_id')); $map_item = $item_rows = []; if($item_ids){ $where = ["id in ($item_ids)" => null]; - $item_rows = $this->items_model->select($where,'','','','id,brand_id,s_id,v_id,vin'); + $item_rows = $this->items_model->select($where,'','','','id,brand_id,s_id,v_id,vin,ori_price,buy_price,(select short from lc_sys_company where id = lc_items.company_id) as cpmpany_name'); foreach ($item_rows as $item) { $map_item[$item['id']] = $item; } } + + //订单 + $o_ids = implode(',',array_column($rows,'o_id')); + $map_order = []; + if($o_ids){ + $where = ["id in ($o_ids)" => null]; + $fields = "id, name, mobile, owner_name, owner_mobile, c_time, biz_id, admin_id, payway, + if(payway=1,'贷款','全款') as payway_name, + (select type from lc_biz where id = lc_receiver_orders_v2.biz_id) as biz_type, + (select biz_name from lc_biz where id = lc_receiver_orders_v2.biz_id) as biz_name, + (select uname from lc_app_licheb_users where id = lc_receiver_orders_v2.admin_id) as admin_name"; + $order_rows = $this->receiver_orders_v2_model->select($where,'','','',$fields); + foreach ($order_rows as $item) { + $map_order[$item['id']] = $item; + } + } + //品牌车型 $brand_arr = array_unique(array_column($item_rows, 'brand_id')); $brands = $this->auto_brand_model->get_map_by_ids($brand_arr, 'id,name'); + //车系车型 $series_arr = array_unique(array_column($item_rows, 's_id')); $series = $this->auto_series_model->get_map_by_ids($series_arr, 'id,name'); @@ -240,42 +386,216 @@ class Cost extends HD_Controller{ $brand_name = isset($brands[$item['brand_id']]) ? $brands[$item['brand_id']][0]['name'] : ''; $series_name = isset($series[$item['s_id']]) ? $series[$item['s_id']][0]['name'] : ''; $v_name = isset($attr[$item['v_id']]) ? $attr[$item['v_id']][0]['title'] : ''; + + $order = $map_order[$val['o_id']]; + + // json字段防止为空 + $promotion_json = json_decode($val['promotion_json'],true); + !$promotion_json['sale_price'] && $promotion_json['sale_price'] = 0; + !$promotion_json['other'] && $promotion_json['other'] = 0; + !$promotion_json['factory_price'] && $promotion_json['factory_price'] = 0; + $cost_json = json_decode($val['cost_json'],true); + !$cost_json['select_price'] && $cost_json['select_price'] = 0; + !$cost_json['labor_price'] && $cost_json['labor_price'] = 0; + $srv_json = json_decode($val['srv_json'],true); + !$srv_json['commission_price'] && $srv_json['commission_price'] = 0; + !$srv_json['back_price'] && $srv_json['back_price'] = 0; + !$srv_json['loan_offset_price'] && $srv_json['loan_offset_price'] = 0; + !$srv_json['loan_subsidy_price'] && $srv_json['loan_subsidy_price'] = 0; + !$srv_json['loan_in_price'] && $srv_json['loan_in_price'] = 0; + !$srv_json['loan_out_price'] && $srv_json['loan_out_price'] = 0; + !$srv_json['other_out_price'] && $srv_json['other_out_price'] = 0; + !$srv_json['other_in_price'] && $srv_json['other_in_price'] = 0; + !$srv_json['ori_price'] && $srv_json['ori_price'] = 0; + + $sum = $this->app_liche_orders_model->sum('total_price', ['o_id'=>$val['o_id'],'status'=>1,'pay_price <>'=>0]); + $pay_price = $sum['total_price'] ? $sum['total_price'] : 0; + $need_price = $this->orders_v2_entity->recevable_price($val['o_id']); + $data[] = [ 'id' => $val['id'], - 'title' => "{$brand_name} {$series_name} {$v_name}", + 'bill_name' => $val['bill_name'], + 'biz_type' => $order['biz_type'], + 'biz_type_name' => $this->biz_model->type_ary($order['biz_type']), 'vin' => $item['vin'], - 'sale_price' => $val['sale_price'], - 'buy_price' => $val['buy_price'], + 'brand_name' => $brand_name, + 'series_name' => "{$series_name} {$v_name}", + 'ori_price' => $srv_json['ori_price'], + + 'owner_name' => $order['owner_name']."({$order['owner_mobile']})", + 'admin_name' => $order['admin_name'], + 'biz_name' => $order['biz_name'], + 'bill_time' => date('Y-m-d',strtotime($val['bill_time'])), + 'bill_price' => $val['bill_price'], + 'bill_price_diff' => $val['price'] - $val['bill_price'], + 'price' => $val['price'], + 'price_fine_select' => $srv_json['price_fine_select'], + 'sale_price' => $val['sale_price'], // $val['price'] + $srv_json['price_fine_select'] + 'order_time' => date('Y-m-d',$order['c_time']), + + 'price_floor' => $srv_json['price_floor'], + 'profix_car_1' => $order['biz_type'] == 1 ? $srv_json['profix_car'] : 0, + 'profix_car_1_not' => $order['biz_type'] != 1 ? $srv_json['profix_car'] : 0, + + 'company_name' => $item['cpmpany_name'], + 'buy_price' => $item['buy_price'], + 'select_price' => $cost_json['select_price'], + 'labor_price' => $cost_json['labor_price'], + 'buy_price_total' => $val['buy_price'], + + 'promotion_sale_price' => $promotion_json['sale_price'], + 'promotion_other' => $promotion_json['other'], 'promotion_price' => $val['promotion_price'], + + 'factory_price' => $promotion_json['factory_price'], 'car_buy_price' => $val['car_buy_price'], 'car_profit_price' => $val['car_profit_price'], - 'insurance_price' => $val['insurance_price'], - 'fee_carno_price' => $val['fee_carno_price'], - 'loan_price' => $val['loan_price'], + + "business_product" => $srv_json['business_product'], + "business_price" => $srv_json['business_price'], + "business_fd" => $srv_json['business_fd'], + "business_ins_price" => $srv_json['business_ins_price'], + "insurance_price" => $srv_json['insurance_price'], + "insurance_fd" => $srv_json['insurance_fd'], + "insurance_ins_price" => $srv_json['insurance_ins_price'], + "back_price" => $srv_json['back_price'], + "insurance_price_pure" => $val['insurance_price'], + + 'fee_carno' => $srv_json['fee_carno'], + 'cb_fee_carno' => $srv_json['cb_fee_carno'], + "fee_carno_price" => $val['fee_carno_price'], + + 'payway_name' => $order['payway_name'], + + 'loan_product' => $srv_json['loan_product'], + 'loan_price' => $srv_json['loan_price'], + 'loan_num' => $srv_json['loan_num'], + 'loan_srv_price' => $srv_json['loan_srv_price'], + 'loan_offset_price' => $srv_json['loan_offset_price'], + 'loan_subsidy_price' => $srv_json['loan_subsidy_price'], + 'commission_price' => $srv_json['commission_price'], + 'loan_in_price' => $srv_json['loan_in_price'], + 'loan_out_price' => $srv_json['loan_out_price'], + 'loan_price_pure' => $val['loan_price'], + + 'other_in_price' => $srv_json['other_in_price'], + 'other_out_price' => $srv_json['other_out_price'], + 'other_price' => $srv_json['other_in_price'] - $srv_json['other_out_price'], + 'srv_price' => $val['srv_price'], 'car_price' => $val['car_price'], + 'dl_car_price' => $val['dl_car_price'], + 'brand_car_price' => $val['brand_car_price'], + + 'need_price' => $need_price, + 'pay_price' => $pay_price, + 'if_pay' => $srv_json['if_pay'] == 1 ? '是' : '', + ]; } } + $headers = [ + 'owner_name' => array('title'=>'销售信息', 'column_num'=>10), // 从该indexs.key开始合并第一行的单元格数及显示标题 + 'price_floor' => array('title'=>'销售政策', 'column_num'=>3), + 'company_name' => array('title'=>'采购成本', 'column_num'=>5), + 'promotion_sale_price' => array('title'=>'促销成本', 'column_num'=>3), + 'business_product' => array('title'=>'保险收入', 'column_num'=>9), + 'fee_carno' => array('title'=>'挂牌收入', 'column_num'=>3), + 'loan_product' => array('title'=>'分期付款业务', 'column_num'=>10), + 'other_in_price' => array('title'=>'其他收入及成本', 'column_num'=>3), + 'need_price' => array('title'=>'收款核对', 'column_num'=>3), + ]; $indexs = [ - 'title' => '车辆', + 'id' => '序号', + 'bill_name' => '开票方', + //'biz_type' => 'biz_type', + 'biz_type_name' => '门店类型', 'vin' => '车架号', + 'brand_name' => '品牌', + 'series_name' => '车型', + 'ori_price' => '指导价', + + 'owner_name' => '车主', // 客户名称 + 'admin_name' => '销售顾问', + 'biz_name' => '门店名称', // 销售归属地 + 'bill_time' => '开票日期', + 'bill_price' => '开票价', // 开票金额 + 'bill_price_diff' => '实际售价与开票差额', + 'price' => '实际售价', + 'price_fine_select' => '选装实收', 'sale_price' => '销售收入合计', - 'buy_price' => '采购及加装成本合计', - "promotion_price" => "促销成本合计", + 'order_time' => '订单日期', + + 'price_floor' => '商务政策低价', // 商务政策销售价 + 'profix_car_1' => '直营店分润', + 'profix_car_1_not' => '渠道分润', + + 'company_name' => '车辆归属', // 进货地(车辆归属) + 'buy_price' => '采购成本', + 'select_price' => '选装成本', // 选装单金额 + 'labor_price' => '选装工时费', + 'buy_price_total' => '采购及加装成本合计', + + 'promotion_sale_price' => '销售佣金', + 'promotion_other' => '其他', + 'promotion_price' => '促销成本合计', + + 'factory_price' => '厂家补贴', "car_buy_price" => "整车采购成本", "car_profit_price" => "整车采购毛利", - "insurance_price" => "保险收入合计", + + "business_product" => "保险公司", + "business_price" => "商业险金额", + "business_fd" => "商业险返点", + "business_ins_price" => "商业险佣金", + "insurance_price" => "交强险金额", + "insurance_fd" => "交强险返点", + "insurance_ins_price" => "交强险佣金", + "back_price" => "客户退点金额", // 退客户保险退点 + "insurance_price_pure" => "保险收入合计", // 保险净收入 + + + 'fee_carno' => '挂牌收入', + 'cb_fee_carno' => '挂牌成本', "fee_carno_price" => "挂牌收入合计", - "loan_price" => "按揭收入合计", - "srv_price" => "水平业务总毛利 ", - "car_price" => "单车总毛利", + + 'payway_name' => '购车方式', + + 'loan_product' => '按揭银行', + 'loan_price' => '按揭金额', + 'loan_num' => '期数', + 'loan_srv_price' => '按揭服务费', + 'loan_offset_price' => '解抵服务费', + 'loan_subsidy_price' => '利息补贴收入', + 'commission_price' => '按揭手续费收入', + 'loan_in_price' => '其他收入金额', + 'loan_out_price' => '其他支出金额', + 'loan_price_pure' => '按揭收入合计', + + 'other_in_price' => '其他收入', + 'other_out_price' => '其他支出', + 'other_price' => '其他合计', + + 'srv_price' => '水平业务总毛利', + 'car_price' => '单车总毛利', + 'dl_car_price' => '门店单车毛利', + 'brand_car_price' => '品牌单车总毛利', + + 'need_price' => '应收款金额', + 'pay_price' => '已收款金额', + 'if_pay' => '是否齐款', + ]; array_unshift($data, $indexs); $this->load->library('excel'); - $this->excel->out_csv($data, $indexs, $fileName . "_" . date('YmdHis')); + #$this->excel->out_csv($data, $indexs, $fileName . "_" . date('YmdHis')); + + $sheet_title = $fileName; + $filename = $fileName.'_' . date('YmdHis'); + $this->excel->excel2($data, $indexs, $headers, $sheet_title, $filename); } + // export old public function export_(){ $params = $this->input->get(); $params['page'] = 1; diff --git a/admin/views/items/cost/get.php b/admin/views/items/cost/get.php index 40583d2b..37dcd5fb 100644 --- a/admin/views/items/cost/get.php +++ b/admin/views/items/cost/get.php @@ -70,7 +70,7 @@
| 门店 | 车辆 | 销售收入合计 | 采购及加装成本合计 | @@ -78,11 +126,15 @@按揭收入合计 | 水平业务总毛利 | 单车总毛利 | +门店单车毛利 | +品牌单车总毛利 | +齐款 |
|---|---|---|---|---|---|---|---|---|---|
| {{v.biz_name}} |
{{v.vin}} {{v.title}} @@ -98,6 +150,12 @@ | {{v.loan_price}} | {{v.srv_price}} | {{v.car_price}} | +{{v.dl_car_price}} | +{{v.brand_car_price}} | ++ + + |