834 lines
40 KiB
PHP
834 lines
40 KiB
PHP
<?php
|
||
defined('BASEPATH') OR exit('No direct script access allowed');
|
||
|
||
class Cost extends HD_Controller{
|
||
public function __construct(){
|
||
parent::__construct();
|
||
$this->load->model('items/items_cost_model');
|
||
$this->load->model('auto/auto_brand_model', 'mdAutoBrand');
|
||
$this->load->model('receiver/order/receiver_orders_v2_model');
|
||
$this->load->model('receiver/order/receiver_order_agents_model', 'order_agents_model');
|
||
$this->load->model('items/items_model');
|
||
$this->load->model("biz/biz_model");
|
||
$this->load->model('app/licheb/app_licheb_users_model');
|
||
$this->load->model('app/liche/app_liche_orders_model');
|
||
$this->load->model('sys/sys_company_model');
|
||
$this->load->model('auto/auto_series_model');
|
||
$this->load->model('auto/auto_brand_model');
|
||
$this->load->model('auto/auto_attr_model');
|
||
$this->load->library('receiver/orders_v2_entity');
|
||
}
|
||
public function index(){
|
||
$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';
|
||
$where["item_id in (select id from lc_items where brand_id in (select id from lc_auto_brand where status > -1))"] = null;
|
||
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';
|
||
//门店类型
|
||
!$params['biz_type'] && $params['biz_type'] = '';
|
||
if ($params['biz_type']) {
|
||
//$where["biz_id in (select id from lc_biz where type = " . $params['biz_type'] . ')'] = null;
|
||
$where_orders = $where_orders." and biz_id in (select id from lc_biz where type = {$params['biz_type']})";
|
||
}
|
||
//销售员筛选
|
||
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 (strlen($params['if_usedcar'])) {
|
||
$where_orders = $where_orders." and if_usedcar = {$params['if_usedcar']}";
|
||
}
|
||
else {
|
||
$params['if_usedcar'] = '';
|
||
}
|
||
|
||
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';
|
||
}
|
||
}
|
||
else{
|
||
if (!$params['bt_init']){
|
||
$params['bt_init'] = 1;
|
||
$time1 = date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
|
||
$time2 = date("Y-m-d",mktime(23,59,59,date("m"),date("t"),date("Y")));
|
||
$params['bill_time'] = $time1 . ' ~ ' . $time2;
|
||
$where["bill_time >="] = $time1 . ' 00:00:00';
|
||
$where["bill_time <="] = $time2 . ' 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 = $this->get_where($params);
|
||
|
||
$count = $this->items_cost_model->count($where);
|
||
$lists = [];
|
||
if($count){
|
||
$rows = $this->items_cost_model->select($where,'id desc',$page,$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');
|
||
foreach ($item_rows as $item) {
|
||
$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, if_usedcar, (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');
|
||
//车系车型
|
||
$series_arr = array_unique(array_column($item_rows, 's_id'));
|
||
$series = $this->auto_series_model->get_map_by_ids($series_arr, 'id,name');
|
||
|
||
//获取属性
|
||
$v_arr = array_unique(array_column($item_rows, 'v_id'));
|
||
$cor_arr = array_unique(array_column($item_rows, 'cor_id'));
|
||
$incor_arr = array_unique(array_column($item_rows, 'incor_id'));
|
||
$attr_arr = array_merge($v_arr, $cor_arr, $incor_arr);
|
||
$attr = $this->auto_attr_model->get_map_by_ids($attr_arr, 'id,title');
|
||
|
||
foreach ($rows as $key => $val) {
|
||
$item = $map_item[$val['item_id']];
|
||
$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']];
|
||
$if_usedcar_name = $order['if_usedcar'] ? ' (二手车)':'';
|
||
$lists[] = [
|
||
'id' => $val['id'],
|
||
'biz_name' => $order['biz_name'],
|
||
'title' => "{$brand_name} {$series_name} {$v_name}{$if_usedcar_name}",
|
||
'vin' => $item['vin'],
|
||
'sale_price' => $val['sale_price'],
|
||
'buy_price' => $val['buy_price'],
|
||
'promotion_price' => $val['promotion_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'],
|
||
'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 ? '是' : '',
|
||
];
|
||
}
|
||
}
|
||
$show_info['bizTypeAry'] = $this->biz_model->type_ary();//门店类型
|
||
$show_info['usedcarAry'] = array(1=>'是', 0=>'否');
|
||
$this->data['show_info'] = $show_info;
|
||
#$autoList[1] = $this->mdAutoBrand->select(array('status' => 1), 'id desc', 0, 0, 'id,name');
|
||
$autoList[1] = $this->mdAutoBrand->map_brand(array('status > -1' => null), false);
|
||
$this->data['lists'] = $lists;
|
||
$this->data['pager'] = array('count' => ceil($count / $size), 'curr' => $page, 'totle' => $count);
|
||
$this->data['autoList'] = $autoList;
|
||
$this->data['params'] = $params;
|
||
$this->data['_title'] = '整车成本';
|
||
return $this->show_view('items/cost/lists', true);
|
||
}
|
||
|
||
public function get(){
|
||
$id = $this->input->get('id');
|
||
$row = $this->items_cost_model->get(['id'=>$id]);
|
||
if(!$row){
|
||
return $this->show_json(SYS_CODE_FAIL,'参数错误');
|
||
}
|
||
$order_row = $this->receiver_orders_v2_model->get(['id'=>$row['o_id']]);
|
||
$info_json = json_decode($order_row['info_json'],true);
|
||
$item_row = $this->items_model->get(['id'=>$row['item_id']]);
|
||
$biz = $this->biz_model->get(['id'=>$order_row['biz_id']]);
|
||
$admin = $this->app_licheb_users_model->get(['id'=>$order_row['admin_id']]);
|
||
$company = $this->sys_company_model->get(['id'=>$item_row['company_id']]);
|
||
$promotion_json = json_decode($row['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($row['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($row['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;
|
||
$info = [
|
||
'bill_name' => $row['bill_name'],
|
||
'bill_time' => $row['bill_time'] !='0000-00-00 00:00:00' ? date('Y-m-d',strtotime($row['bill_time'])) : '',
|
||
'price' => $row['price'],
|
||
'bill_price' => $row['bill_price'],
|
||
'if_usedcar_name' => $order_row['if_usedcar'] ? '是':'否',
|
||
'vin' => $item_row['vin'],
|
||
'ori_price' => $item_row['ori_price'],
|
||
'buy_price' => $item_row['buy_price'],
|
||
'company_name' => $company['short'],
|
||
'name' => $order_row['owner_name']."({$order_row['owner_mobile']})", // 'name' => $order_row['name']."({$order_row['mobile']})",
|
||
'payway' => $order_row['payway'],
|
||
'biz_type' => $biz['type'],
|
||
'biz_type_name' => $this->biz_model->type_ary($biz['type']),
|
||
'biz_name' => $biz['biz_name'],
|
||
'admin_name' => $admin['uname'],
|
||
'order_time' => date('Y-m-d',$order_row['c_time']),
|
||
'promotion_json' => $promotion_json,
|
||
'cost_json' => $cost_json,
|
||
'srv_json' => $srv_json,
|
||
'row' => $row,
|
||
'business_id' => $info_json['business_id'] ? $info_json['business_id'] : 0
|
||
];
|
||
$sum = $this->app_liche_orders_model->sum('total_price', ['o_id'=>$row['o_id'],'status'=>1,'pay_price <>'=>0]);
|
||
$info['pay_price'] = $sum['total_price'] ? $sum['total_price'] : 0;
|
||
$info['need_price'] = $this->orders_v2_entity->recevable_price($row['o_id']);
|
||
//获取车信息
|
||
$brand = $this->auto_brand_model->get(['id' => $item_row['brand_id']], 'name');
|
||
$serie = $this->auto_series_model->get(['id' => $item_row['s_id']], 'name');
|
||
$attr = $this->auto_attr_model->get(['id'=>$item_row['v_id']], 'title');
|
||
$info['brand_name'] = $brand['name'];
|
||
$info['s_name'] = $serie['name'];
|
||
$info['v_name'] = $attr['title'];
|
||
$this->data['info'] = $info;
|
||
$this->data['_title'] = '编辑整车成本';
|
||
return $this->show_view('items/cost/get', true);
|
||
}
|
||
|
||
public function add(){
|
||
|
||
}
|
||
|
||
public function edit(){
|
||
$post_data = $this->input->post();
|
||
$row = $this->items_cost_model->get(['id'=>$post_data['id']]);
|
||
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),
|
||
'promotion_json' => json_encode($post_data['promotion_json'],JSON_UNESCAPED_UNICODE),
|
||
'srv_json' => json_encode($post_data['srv_json'],JSON_UNESCAPED_UNICODE),
|
||
];
|
||
|
||
/*
|
||
水平
|
||
品牌店按现有逻辑 (1)
|
||
合伙店取商务政策的固定值 (2)
|
||
代理店没有水平业务 (3)
|
||
*/
|
||
$order_row = $this->receiver_orders_v2_model->get(['id'=>$row['o_id']], 'id, (select type from lc_biz where id = lc_receiver_orders_v2.biz_id) as biz_type');
|
||
$biz_type = $order_row['biz_type'];
|
||
if ($biz_type == 2){
|
||
$insurance_price = $post_data['insurance_price'] ? $post_data['insurance_price'] : 0;
|
||
$insurance_price != $row['insurance_price'] && $data['insurance_price'] = $insurance_price;
|
||
|
||
$fee_carno_price = $post_data['fee_carno_price'] ? $post_data['fee_carno_price'] : 0;
|
||
$fee_carno_price != $row['fee_carno_price'] && $data['fee_carno_price'] = $fee_carno_price;
|
||
|
||
$loan_price = $post_data['loan_price'] ? $post_data['loan_price'] : 0;
|
||
$loan_price != $row['loan_price'] && $data['loan_price'] = $loan_price;
|
||
}
|
||
|
||
$res = $this->items_cost_model->update($data,['id'=>$row['id']]);
|
||
if(is_numeric($res_item) || is_numeric($res)){
|
||
$this->items_cost_model->update_total($row['id']);
|
||
return $this->show_json(SYS_CODE_SUCCESS,'保存成功');
|
||
}else{
|
||
return $this->show_json(SYS_CODE_FAIL,'保存失败');
|
||
}
|
||
}
|
||
|
||
public function edit_pay(){
|
||
$post_data = $this->input->post();
|
||
$row = $this->items_cost_model->get(['id'=>$post_data['id']]);
|
||
if(!$row){
|
||
return $this->show_json(SYS_CODE_FAIL,'参数错误');
|
||
}
|
||
$srv_json = json_decode($row['srv_json'],true);
|
||
$srv_json['if_pay'] = $post_data['status'] == 1 ? 1 : 0;
|
||
$data = [
|
||
'srv_json' => json_encode($srv_json,JSON_UNESCAPED_UNICODE),
|
||
];
|
||
$res = $this->items_cost_model->update($data,['id'=>$row['id']]);
|
||
if(is_numeric($res)){
|
||
return $this->show_json(SYS_CODE_SUCCESS,'保存成功');
|
||
}else{
|
||
return $this->show_json(SYS_CODE_FAIL,'保存失败');
|
||
}
|
||
}
|
||
|
||
public function del(){
|
||
|
||
}
|
||
|
||
public function batch(){
|
||
|
||
}
|
||
|
||
public function add_excel()
|
||
{
|
||
require_once COMMPATH . '/third_party/PHPExcel/IOFactory.php';
|
||
$res = $this->upload();
|
||
if (!$res['code']) {
|
||
return $this->show_json(0, $res['message']);
|
||
}
|
||
$file = $res['path'];
|
||
if ($res['file_ext'] == '.xls') {
|
||
$reader = \PHPExcel_IOFactory::createReader('Excel5'); // 读取 excel 文档
|
||
} elseif ($res['file_ext'] == '.xlsx') {
|
||
$reader = \PHPExcel_IOFactory::createReader('Excel2007'); // 读取 excel 文档
|
||
}
|
||
$PHPExcel = $reader->load($file); // 文档名称
|
||
$objWorksheet = $PHPExcel->getActiveSheet();
|
||
$rowCnt = $objWorksheet->getHighestRow(); //获取总行数
|
||
if ($rowCnt > 10000) {
|
||
@unlink($file);
|
||
return $this->show_json(0, '数据大于一万请拆分多个表格导入');
|
||
}
|
||
$s = $f = $ignore_vin = $ignore_item = $ignore_cost = 0;
|
||
for ($_row = 2; $_row <= $rowCnt; $_row++) { //读取内容
|
||
$vin = $objWorksheet->getCell('A' . $_row)->getValue(); //vin码
|
||
if (!$vin){
|
||
$ignore_vin++;
|
||
continue;
|
||
}
|
||
$item = $this->items_model->get(array('vin' => $vin));
|
||
if (!$item){
|
||
$ignore_item++;
|
||
continue;
|
||
}
|
||
$cost = $this->items_cost_model->get(array('item_id' => $item['id']));
|
||
if (!$cost){
|
||
$ignore_cost++;
|
||
continue;
|
||
}
|
||
|
||
$id = $cost['id'];
|
||
$promotion_json = json_decode($cost['promotion_json'],true);
|
||
$cost_json = json_decode($cost['cost_json'],true);
|
||
$srv_json = json_decode($cost['srv_json'],true);
|
||
# 读取需要更新的列,生成需更新的数据
|
||
# 参考 edit* 更新
|
||
$buy_price = floatval($objWorksheet->getCell('B' . $_row)->getValue()); //采购成本
|
||
$select_price = floatval($objWorksheet->getCell('C' . $_row)->getValue()); //选装成本
|
||
$labor_price = floatval($objWorksheet->getCell('D' . $_row)->getValue()); //选装工时费
|
||
$sale_price = floatval($objWorksheet->getCell('E' . $_row)->getValue()); //销售佣金
|
||
$other = floatval($objWorksheet->getCell('F' . $_row)->getValue()); //其他促销成本
|
||
$factory_price = floatval($objWorksheet->getCell('G' . $_row)->getValue()); //厂家补贴
|
||
$other_in_price = floatval($objWorksheet->getCell('H' . $_row)->getValue()); //其他收入
|
||
$other_out_price = floatval($objWorksheet->getCell('I' . $_row)->getValue()); //其他支出
|
||
$res_item = 0;
|
||
if ($item['buy_price'] != $buy_price){
|
||
$res_item = $this->items_model->update(array('buy_price'=>$buy_price),['id'=>$item['id']]);
|
||
}
|
||
$cost_json['select_price'] = $select_price ? $select_price : 0;
|
||
$cost_json['labor_price'] = $labor_price ? $labor_price : 0;
|
||
|
||
$promotion_json['sale_price'] = $sale_price ? $sale_price : 0;
|
||
$promotion_json['other'] = $other ? $other : 0;
|
||
$promotion_json['factory_price'] = $factory_price ? $factory_price : 0;
|
||
|
||
$srv_json['other_in_price'] = $other_in_price ? $other_in_price : 0;
|
||
$srv_json['other_out_price'] = $other_out_price ? $other_out_price : 0;
|
||
|
||
$data = [
|
||
'cost_json' => json_encode($cost_json,JSON_UNESCAPED_UNICODE),
|
||
'promotion_json' => json_encode($promotion_json,JSON_UNESCAPED_UNICODE),
|
||
'srv_json' => json_encode($srv_json,JSON_UNESCAPED_UNICODE),
|
||
];
|
||
$res = $this->items_cost_model->update($data,['id'=>$cost['id']]);
|
||
if(is_numeric($res_item) || is_numeric($res)){
|
||
$this->items_cost_model->update_total($cost['id']);
|
||
}else{
|
||
$f++;
|
||
}
|
||
$s++;
|
||
}
|
||
@unlink($file);
|
||
$ignore = $ignore_vin + $ignore_item + $ignore_cost;
|
||
$msg = "导入完成,导入总条数为:" . $s;
|
||
$f && $msg = $msg . '; 失败条数:'. $f;
|
||
$ignore && $msg = $msg . '; 忽略条数:'. $ignore;
|
||
return $this->show_json(SYS_CODE_SUCCESS, $msg);
|
||
}
|
||
|
||
public function export(){
|
||
$params = $this->input->get();
|
||
$params['page'] = 1;
|
||
$params['size'] = 10000;
|
||
$data = $indexs = array();
|
||
$fileName = '整车成本';
|
||
$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,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,
|
||
if_usedcar, if(if_usedcar=1,'是','') as if_usedcar_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');
|
||
|
||
//获取属性
|
||
$v_arr = array_unique(array_column($item_rows, 'v_id'));
|
||
$cor_arr = array_unique(array_column($item_rows, 'cor_id'));
|
||
$incor_arr = array_unique(array_column($item_rows, 'incor_id'));
|
||
$attr_arr = array_merge($v_arr, $cor_arr, $incor_arr);
|
||
$attr = $this->auto_attr_model->get_map_by_ids($attr_arr, 'id,title');
|
||
|
||
foreach ($rows as $key => $val) {
|
||
$item = $map_item[$val['item_id']];
|
||
$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'],
|
||
'bill_name' => $val['bill_name'],
|
||
'biz_type' => $order['biz_type'],
|
||
'biz_type_name' => $this->biz_model->type_ary($order['biz_type']),
|
||
'vin' => $item['vin'],
|
||
'brand_name' => $brand_name,
|
||
'series_name' => "{$series_name} {$v_name}",
|
||
'if_usedcar_name' => $order['if_usedcar_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'],
|
||
|
||
"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 = [
|
||
'id' => '序号',
|
||
'bill_name' => '开票方',
|
||
//'biz_type' => 'biz_type',
|
||
'biz_type_name' => '门店类型',
|
||
'vin' => '车架号',
|
||
'brand_name' => '品牌',
|
||
'series_name' => '车型',
|
||
'if_usedcar_name' => '二手车',
|
||
'ori_price' => '指导价',
|
||
|
||
'owner_name' => '车主', // 客户名称
|
||
'admin_name' => '销售顾问',
|
||
'biz_name' => '门店名称', // 销售归属地
|
||
'bill_time' => '开票日期',
|
||
'bill_price' => '开票价', // 开票金额
|
||
'bill_price_diff' => '实际售价与开票差额',
|
||
'price' => '实际售价',
|
||
'price_fine_select' => '选装实收',
|
||
'sale_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" => "整车采购毛利",
|
||
|
||
"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" => "挂牌收入合计",
|
||
|
||
'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'));
|
||
|
||
$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;
|
||
$params['size'] = 10000;
|
||
$data = $indexs = array();
|
||
$fileName = '整车成本';
|
||
$where = [];
|
||
$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');
|
||
foreach ($item_rows as $item) {
|
||
$map_item[$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');
|
||
|
||
//获取属性
|
||
$v_arr = array_unique(array_column($item_rows, 'v_id'));
|
||
$cor_arr = array_unique(array_column($item_rows, 'cor_id'));
|
||
$incor_arr = array_unique(array_column($item_rows, 'incor_id'));
|
||
$attr_arr = array_merge($v_arr, $cor_arr, $incor_arr);
|
||
$attr = $this->auto_attr_model->get_map_by_ids($attr_arr, 'id,title');
|
||
|
||
foreach ($rows as $key => $val) {
|
||
$item = $map_item[$val['item_id']];
|
||
$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'] : '';
|
||
$data[] = [
|
||
'id' => $val['id'],
|
||
'title' => "{$brand_name} {$series_name} {$v_name}",
|
||
'vin' => $item['vin'],
|
||
'sale_price' => $val['sale_price'],
|
||
'buy_price' => $val['buy_price'],
|
||
'promotion_price' => $val['promotion_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'],
|
||
'srv_price' => $val['srv_price'],
|
||
'car_price' => $val['car_price'],
|
||
];
|
||
}
|
||
}
|
||
$indexs = [
|
||
'title' => '车辆',
|
||
'vin' => '车架号',
|
||
'sale_price' => '销售收入合计',
|
||
'buy_price' => '采购及加装成本合计',
|
||
"promotion_price" => "促销成本合计",
|
||
"car_buy_price" => "整车采购成本",
|
||
"car_profit_price" => "整车采购毛利",
|
||
"insurance_price" => "保险收入合计",
|
||
"fee_carno_price" => "挂牌收入合计",
|
||
"loan_price" => "按揭收入合计",
|
||
"srv_price" => "水平业务总毛利 ",
|
||
"car_price" => "单车总毛利",
|
||
];
|
||
array_unshift($data, $indexs);
|
||
$this->load->library('excel');
|
||
$this->excel->out_csv($data, $indexs, $fileName . "_" . date('YmdHis'));
|
||
}
|
||
|
||
/**
|
||
* Notes:上传excel
|
||
* Created on: 2021/7/14 15:06
|
||
* Created by: dengbw
|
||
* @return array
|
||
*/
|
||
private function upload()
|
||
{
|
||
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/temp/';
|
||
$config['allowed_types'] = '*';//xls|xlsx
|
||
$config['max_size'] = 5 * 1024;
|
||
$config['file_name'] = 'items_' . time() . rand(1, 99999);
|
||
$this->load->library('upload', $config);
|
||
if (!$this->upload->do_upload('file')) {
|
||
return array('code' => SYS_CODE_FAIL, 'message' => $this->upload->display_errors('', ''));
|
||
} else {
|
||
$data = $this->upload->data();
|
||
return array('code' => SYS_CODE_SUCCESS, 'path' => $data['full_path'], 'file_ext' => $data['file_ext']);
|
||
}
|
||
}
|
||
} |