Files
liche/admin/controllers/biz/Settle.php
T
2022-04-06 17:34:13 +08:00

445 lines
18 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once COMMPATH.'third_party/PHPExcel.php';
class Settle extends HD_Controller
{
private $biz_type = [
1 => ['liche' => 0.25 , 'partner' => 0.75],
2 => ['liche' => 0.35 , 'partner' => 0.65],
];
public function __construct()
{
parent::__construct();
$this->load->model('auto/auto_brand_model');
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_attr_model');
$this->load->model('auto/auto_business_model');
$this->load->model('items/items_model');
$this->load->model('biz/biz_model');
$this->load->model('biz/biz_settle_model');
$this->load->model('biz/biz_settle_static_model');
$this->load->model('biz/biz_info_model');
}
public function index()
{
$this->lists();
}
public function lists()
{
$params = $this->input->get();
$where = array();
$t1 = 'lc_biz_settle';
$t2 = 'lc_receiver_orders_v2';
if($params['biz_id']){
$where["$t1.biz_id"] = $params['biz_id'];
} else{
$params['biz_id'] = '';
}
if($params['brand_id']){
$where["$t2.brand_id"] = $params['brand_id'];
} else{
$params['brand_id'] = '';
}
if($params['s_id']){
$where["$t2.s_id"] = $params['s_id'];
} else{
$params['s_id'] = '';
}
if($params['v_id']){
$where["$t2.v_id"] = $params['v_id'];
} else{
$params['v_id'] = '';
}
if($params['year']){
$where['year'] = $params['year'];
}elseif(!isset($params['year'])){
$where['year'] = $params['year'] = date('Y');
}
if($params['month']){
$where['month'] = $params['month'];
}elseif(!isset($params['month'])){
$where['month'] = $params['month'] = intval(date('m'));
}
$page = $params['page'];
$page = !$page ? 1 : $page;
$size = $params['size'];
$size = !$size ? 20 : $size;
//获取品牌map
$where_brand = array('status > -1' => null);
$map_brand = $this->auto_brand_model->map('id', 'name', $where_brand, 'id desc', 0 , 0, 'id, name');
$total = $this->biz_settle_model->selectOrders($where, '', '', '',1);
$lists = array();
if($total){
$orderby = "$t1.id desc";
$select = "$t1.*,$t2.sid,$t2.name,$t2.mobile,$t2.s_id,$t2.v_id,$t2.brand_id,$t2.cor_id,$t2.item_id";
$rows = $this->biz_settle_model->selectOrders($where, $orderby, $page, $size,0, $select);
if($rows){
$s_ids = array_unique(array_filter(array_column($rows,'s_id')));
$cor_ids = array_unique(array_filter(array_column($rows,'cor_id')));
$v_ids = array_unique(array_filter(array_column($rows,'v_id')));
$attr_ids = array_merge($cor_ids,$v_ids);
$item_ids = array_unique(array_filter(array_column($rows,'item_id')));
//获取车系列表
$map_sery = array();
if($s_ids){
$str_ids = implode(',', $s_ids);
$where_sery = array("id in ({$str_ids})" => null);
$map_sery = $this->auto_series_model->map('id', 'name', $where_sery, 'id desc', 0, 0, 'id,name');
}
$map_attr = array();
if($attr_ids){
$str_ids = implode(',', $attr_ids);
$where_attr = array("id in ({$str_ids})" => null);
$map_attr = $this->auto_attr_model->map('id', 'title', $where_attr, 'id desc', 0 , 0, 'id, title, type');
}
$biz_ids = array_unique(array_column($rows,'biz_id'));
$map_biz = [];
if($biz_ids){
$biz_ids = implode(',',array_filter($biz_ids));
$where_biz = ["id in ({$biz_ids})" => null];
$map_biz = $this->biz_model->map('id', 'biz_name', $where_biz, '', 0, 0, 'id,biz_name');
}
$map_item = [];
if($item_ids){
$str_item_ids = implode(',',$item_ids);
$where_item = ["id in ({$str_item_ids})" => null];
$map_item = $this->items_model->map('id', 'vin', $where_item, '', 0, 0, 'id,vin');
}
foreach($rows as $v){
$title = "{$map_brand[$v['brand_id']]} {$map_sery[$v['s_id']]} {$map_attr[$v['v_id']]} {$map_attr[$v['cor_id']]}";
$month = date('Y-m',strtotime("{$v['year']}-{$v['month']}"));
$lists[] = array(
'id' => $v['id'],
'o_id' => $v['o_id'],
'title' => $title,
'biz_name' => $map_biz[$v['biz_id']],
'profix_car' => $v['profix_car'],
'profix_insure' => $v['profix_insure'],
'profix_carno' => $v['profix_carno'],
'profix_loan' => $v['profix_loan'],
'price_trucking' => $v['price_trucking'],
'name' => $v['name'],
'mobile' => $v['mobile'],
'sid' => $v['sid'],
'month' => $month,
'vin' => $map_item[$v['item_id']]
);
}
}
}
$this->data['params'] = $params;
$this->data['lists'] = $lists;
$this->data['brandAry'] = $map_brand;
$this->data['yearAry'] = $this->auto_business_model->year();
$this->data['monthAry'] = $this->auto_business_model->month();
$this->data['pager'] = array('count' => ceil($total / $size), 'curr' => $page,'totle'=>$total);
$this->data['_title'] = '商家结算管理';
$this->show_view('biz/settle/lists',true);
}
public function get(){
}
public function add(){
}
public function edit()
{
}
public function del()
{
}
public function batch()
{
}
public function export()
{
$biz_id = $this->input->get('id');
$month = $this->input->get('month');
$year = date('Y');
!$month && $month = intval(date('m'));
$where = [
'biz_id' => $biz_id,
'year' => $year,
'month' => $month
];
$type_arr = $this->biz_settle_static_model->other_price_type();
$biz = $this->biz_model->get(['id'=>$biz_id],'biz_name');
$row = $this->biz_settle_static_model->get($where);
$sheet_data = [];
if($row){
$sheet_data[] = [
'门店','单车总毛利','税后单车总毛利','水平业务总毛利',
'店面租金','水电费','店长工资','店员工资',
'抽成','拖车费','其它费用','总利润',
'狸车分润','合伙人分润','时间'];
$row['month_str'] = date('Y-m',strtotime("{$row['year']}-{$row['month']}"));
$row['total_need'] = $row['rent'] + $row['wat_ele'] + $row['manager_wage'] + $row['employee_wage'] + $row['commission'] + $row['price_trucking'];
$row['price_ml'] = $row['price_total'] - $row['total_need'];
$other_price = '';
$other_json = json_decode($row['price_other'],true);
if($other_json){
foreach($other_json as $key=>$val){
$type_cn = $type_arr[$val['type']];
$other_price .= $val['name'].$val['money']."({$type_cn})\n";
}
}
$sheet_data[] = [
$biz['biz_name'],
$row['profix_car'],
$row['profix_car_after'],
$row['price_all'],
$row['rent'],
$row['wat_ele'],
$row['manager_wage'],
$row['employee_wage'],
$row['commission'],
$row['price_trucking'],
$other_price,
$row['price_ml'],
$row['profix_liche'],
$row['profix_partner']+$row['profix_partner_oflow'],
$row['month_str']
];
}
$t1 = 'lc_biz_settle';
$t2 = 'lc_receiver_orders_v2';
$where = [
"{$t1}.biz_id" => $biz_id,
"{$t1}.year" => $year,
"{$t1}.month" => $month
];
$orderby = "$t1.id desc";
$select = "$t1.*,$t2.sid,$t2.name,$t2.mobile,$t2.s_id,$t2.v_id,$t2.brand_id,$t2.cor_id,$t2.item_id";
$all_rows = $this->biz_settle_model->selectOrders($where, $orderby, 0, 0,0, $select);
$sheet_data2 = [];
if($all_rows){
$sheet_data2[] = ['门店','订单信息','品牌车型','车架号','单车利润','保险利润','贷款利润','挂牌利润','拖车费','时间'];
$s_ids = array_unique(array_filter(array_column($all_rows,'s_id')));
$cor_ids = array_unique(array_filter(array_column($all_rows,'cor_id')));
$v_ids = array_unique(array_filter(array_column($all_rows,'v_id')));
$attr_ids = array_merge($cor_ids,$v_ids);
$item_ids = array_unique(array_filter(array_column($all_rows,'item_id')));
//获取车系列表
$map_sery = array();
if($s_ids){
$str_ids = implode(',', $s_ids);
$where_sery = array("id in ({$str_ids})" => null);
$map_sery = $this->auto_series_model->map('id', 'name', $where_sery, 'id desc', 0, 0, 'id,name');
}
$map_attr = array();
if($attr_ids){
$str_ids = implode(',', $attr_ids);
$where_attr = array("id in ({$str_ids})" => null);
$map_attr = $this->auto_attr_model->map('id', 'title', $where_attr, 'id desc', 0 , 0, 'id, title, type');
}
$map_item = [];
if($item_ids){
$str_item_ids = implode(',',$item_ids);
$where_item = ["id in ({$str_item_ids})" => null];
$map_item = $this->items_model->map('id', 'vin', $where_item, '', 0, 0, 'id,vin');
}
foreach($all_rows as $v){
$title = "{$map_brand[$v['brand_id']]} {$map_sery[$v['s_id']]} {$map_attr[$v['v_id']]} {$map_attr[$v['cor_id']]}";
$vin = $map_item[$v['item_id']];
$month_str = date('Y-m',strtotime("{$v['year']}-{$v['month']}"));
$sheet_data2[] = array(
$biz['biz_name'],
"{$v['name']} {$v['mobile']}",
$title,
$vin,
$v['profix_car'],
$v['profix_insure'],
$v['profix_loan'],
$v['profix_carno'],
$v['price_trucking'],
$month_str,
);
}
}
$cells = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$obpe = new PHPExcel();
if($sheet_data){
$obpe->setactivesheetindex(0);
foreach($sheet_data as $key=>$val){
foreach($val as $k2=>$v2){
$cell = $cells[$k2];
$k = $key+1;
$obpe->getactivesheet()->setcellvalue("{$cell}{$k}", $v2);
}
}
}
if($sheet_data2){
//创建一个新的工作空间(sheet)
$obpe->createSheet();
$obpe->setactivesheetindex(1);
foreach($sheet_data2 as $key=>$val){
foreach($val as $k2=>$v2){
$cell = $cells[$k2];
$k = $key+1;
$obpe->getactivesheet()->setcellvalue("{$cell}{$k}", $v2);
}
}
}
$filename = $biz['biz_name']."_{$year}{$month}月_结算表";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename='. $filename .'.xlsx');
header("Content-Transfer-Encoding:binary");
$writer = PHPExcel_IOFactory::createWriter($obpe, 'Excel2007');
$writer->save('php://output');
}
//统计合计
public function lists_static(){
$params = $this->input->get();
$where = [];
if($params['year']){
$where['year'] = $params['year'];
}elseif(!isset($params['year'])){
$where['year'] = $params['year'] = date('Y');
}
if($params['month']){
$where['month'] = $params['month'];
}elseif(!isset($params['month'])){
$where['month'] = $params['month'] = intval(date('m'));
}
if($params['id']){
$where['biz_id'] = $params['id'];
}
$page = $params['page'];
$page = !$page ? 1 : $page;
$size = $params['size'];
$size = !$size ? 20 : $size;
$total = $this->biz_settle_static_model->count($where);
$lists = array();
if($total){
$orderby = 'id desc';
$select = '*';
$rows = $this->biz_settle_static_model->select($where, $orderby, $page, $size, $select);
if($rows){
$biz_ids = array_unique(array_column($rows,'biz_id'));
$map_biz = [];
if($biz_ids){
$biz_ids = implode(',',$biz_ids);
$where_biz = ["id in ({$biz_ids})" => null];
$map_biz = $this->biz_model->map('id', 'biz_name', $where_biz, '', 0, 0, 'id,biz_name');
}
//属性按'车型-车身颜色-内饰颜色'排序
foreach($rows as $v){
$temp = $v;
$temp['biz_name'] = $map_biz[$v['biz_id']];
$temp['month_str'] = date('Y-m',strtotime("{$v['year']}-{$v['month']}"));
$temp['price_other'] = $v['price_other'] ? json_decode($v['price_other']) : [];
$temp['total_need'] = $v['rent'] + $v['wat_ele'] + $v['manager_wage'] + $v['employee_wage'] + $v['commission'] + $v['price_trucking'];
$temp['price_ml'] = $v['price_total'] - $temp['total_need'];
$temp['profix_partner'] = $v['profix_partner'] + $v['profix_partner_oflow'];
$lists[] = $temp;
}
}
}
$this->data['params'] = $params;
$this->data['lists'] = $lists;
$this->data['yearAry'] = $this->auto_business_model->year();
$this->data['monthAry'] = $this->auto_business_model->month();
$this->data['type_arr'] = $this->biz_settle_static_model->other_price_type();
$this->data['pager'] = array('count' => ceil($total / $size), 'curr' => $page,'totle'=>$total);
$this->data['_title'] = '商家结算管理';
$this->show_view('biz/settle/lists_static',true);
}
//重新统计
public function edit_static(){
$id = $this->input->post('id');
$row = $this->biz_settle_static_model->get(['id'=>$id]);
if(!$row){
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
$all_other = 0; //共同承担其它费用
$liche_other = 0; //狸车承担其它费用
$partner_other = 0; //合伙人承担其它费用
$price_other = json_decode($row['price_other'],true);
if($price_other){
foreach($price_other as $item){
if($item['type']==2){
$partner_other += $item['money'];
}elseif($item['type']==1){
$liche_other += $item['money'];
}else{
$all_other += $item['money'];
}
}
}
$biz_info = $this->biz_info_model->get(['biz_id'=>$row['biz_id']]);
$type_config = $this->biz_type[$biz_info['type']];
$price_need = $row['rent'] + $row['wat_ele'] + $row['employee_wage'] + $row['manager_wage'] + $row['commission'] + $row['price_trucking'];
$price_ml = $row['price_total'] - $price_need - $all_other;
$update['profix_liche'] = $price_ml*$type_config['liche']-$liche_other;
$update['profix_partner'] = $price_ml*$type_config['partner']-$partner_other;
$this->biz_settle_static_model->update($update,['id'=>$row['id']]);
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
public function add_price(){
$id = $this->input->get_post('id');
$row = $this->biz_settle_static_model->get(['id'=>$id]);
if(!$row){
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
if($this->input->method()=='post'){
$lists = $this->input->post('lists');
if(!is_array($lists)){
return $this->show_json(SYS_CODE_FAIL, '参数错误!');
}
$price_other = [];
foreach($lists as $key=>$val){
if($val['money']){
$price_other[] = [
'name' => $val['name'],
'money' => $val['money'],
'type' => $val['type'] ? $val['type'] : 0
];
}
}
$up_data['price_other'] = json_encode($price_other,JSON_UNESCAPED_UNICODE);
$this->biz_settle_static_model->update($up_data,['id'=>$id]);
return $this->show_json(SYS_CODE_SUCCESS, '操作成功');
}
$lists = $row['price_other'] ? json_decode($row['price_other']) : [];
$this->data['id'] = $id;
$this->data['lists'] = $lists;
$this->data['type_arr'] = $this->biz_settle_static_model->other_price_type();
$this->show_view('biz/settle/add');
}
}