load->model('receiver/order/receiver_orders_v2_model','orders_model'); $this->load->model('receiver/order/receiver_order_loans_model','order_loans_model'); $this->load->model('receiver/order/receiver_order_agents_model', 'order_agents_model'); $this->load->model('receiver/order/receiver_order_datas_model'); $this->load->model('receiver/receiver_fine_model'); $this->load->model('auto/auto_business_model'); $this->load->model('biz/biz_model'); $this->load->model('biz/biz_info_model'); $this->load->model('biz/biz_settle_model'); $this->load->model('biz/biz_settle_static_model'); $this->load->model('biz/biz_settle_srv_model'); $this->load->model('biz/biz_trucking_model'); $this->load->model('biz/biz_settle_price_model'); $this->load->model('sys/sys_finance_model'); $this->load->library('receiver/orders_v2_entity'); } public function settle(){ $size = $this->input->get('size'); !$size && $size = 5; $t1 = 'lc_receiver_order_status'; $t2 = 'lc_receiver_orders_v2'; $t3 = 'lc_biz_settle'; $t4 = 'lc_biz'; $fields = "$t2.*,$t4.city_id"; $where = [ "$t1.pid_status" => 3, "$t1.status" => 1, "$t2.status in (0,1)" => null, "$t2.id>=" => Orders_v2_entity::V2_START_ID, "$t2.bill_time !='0000-00-00 00:00:00'" => null, "$t3.id is null" => null, "$t4.type" => 2, ]; $this->db->from("$t1"); $this->db->join("$t2", "$t2.id=$t1.o_id",'left'); $this->db->join("$t3", "$t3.o_id=$t1.o_id",'left'); $this->db->join("$t4", "$t4.id=$t2.biz_id",'left'); $this->db->select($fields); $this->db->where($where); $this->db->order_by("$t2.id ASC"); $this->db->limit($size); $rows = $this->db->get()->result_array(); if($rows){ foreach($rows as $key=>$val){ $info_json = json_decode($val['info_json'],true); $money_json = json_decode($val['money_json'],true); //获取创建订单时商务政策 $where = [ 'id' => $info_json['business_id'], ]; $b_row_one = $this->auto_business_model->get($where); //获取开票日期商务政策 $b_where = [ 'city_id' => $val['city_id'], 'brand_id' => $val['brand_id'], 's_id' => $val['s_id'], 'v_id' => $val['v_id'], 'if_effect' => 1, ]; $b_row_two = $this->auto_business_model->select($b_where,'id desc',1,1); $b_row_two = $b_row_two[0]; $truck_row = $this->biz_trucking_model->get(['auto_b_id'=>$val['brand_id'],'biz_id'=>$val['biz_id'],'status'=>1]); $price_trucking = $truck_row['money'] ? $truck_row['money'] : 0; $add_data = [ 'biz_id' => $val['biz_id'], 'o_id' => $val['id'], 'profix_car' => $b_row_one['profix_car'] ? $b_row_one['profix_car'] : 0, 'profix_insure' => $b_row_two['profix_insure'] ? $b_row_two['profix_insure'] : 0, 'profix_loan' => $b_row_two['profix_loan'] ? $b_row_two['profix_loan'] : 0, 'profix_carno' => $b_row_two['profix_carno'] ? $b_row_two['profix_carno'] : 0, 'price_trucking' => $price_trucking, 'year' => date('Y',strtotime($val['bill_time'])), 'month' => intval(date('m',strtotime($val['bill_time']))), 'c_time' => time() ]; if($money_json['price_car']>$b_row_one['price_floor']){ $add_data['oflow_profix_car'] = $money_json['price_car'] - $b_row_one['price_floor']; } $this->biz_settle_model->add($add_data); } }else{ echo 'finish'; } } //统计前一个月利润 public function merge(){ $size = $this->input->get('size'); $page = 1; !$size && $size = 5; $where = [ "year" => date('Y',strtotime("last month")), "month" => intval(date('m',strtotime('last day of - 1 months'))), "stic_id" => 0, "type" => 0 ]; $rows = $this->biz_settle_model->select($where,'id asc',$page,$size); if($rows){ foreach($rows as $key=>$val){ $where = [ 'biz_id' => $val['biz_id'], 'year' => $val['year'], 'month' => $val['month'] ]; $static_row = $this->biz_settle_static_model->get($where); $price_all = $val['profix_insure']+$val['profix_loan']+$val['profix_carno']; if(!$static_row){ $data = [ 'biz_id' => $val['biz_id'], 'profix_car' => $val['profix_car'], 'price_all' => $price_all, 'price_trucking' => $val['price_trucking'], 'year' => $val['year'], 'month' => $val['month'], 'c_time' => time() ]; $stic_id = $this->biz_settle_static_model->add($data); if($stic_id){ $this->biz_settle_model->update(['stic_id'=>$stic_id],['id'=>$val['id']]); } }else{ $data = [ 'profix_car' => $static_row['profix_car'] + $val['profix_car'], 'price_all' => $static_row['price_all'] + $price_all, 'price_trucking' => $static_row['price_trucking'] + $val['price_trucking'], ]; $res = $this->biz_settle_static_model->update($data,['id'=>$static_row['id']]); if($res){ $this->biz_settle_model->update(['stic_id'=>$static_row['id']],['id'=>$val['id']]); } } } }else{ //计算分润 $where = [ 'profix_car_after' => 0.00 ]; $rows = $this->biz_settle_static_model->select($where,'id asc',$page,$size); if($rows){ foreach($rows as $val){ $biz_info = $this->biz_info_model->get(['biz_id'=>$val['biz_id']]); $car_total = $this->biz_settle_model->count(['stic_id'=>$val['id']]); $data = [ 'profix_car_after' => sprintf("%.2f",$val['profix_car']/1.13), 'rent' => $biz_info['rent'], 'wat_ele' => $biz_info['wat_ele'], 'manager_wage' => $biz_info['manager_wage'], 'commission' => $biz_info['commission']*$car_total, ]; $employee_wage = $biz_info['num'] ? ceil($car_total/$biz_info['num']) : 0; $data['employee_wage'] = $employee_wage * $biz_info['employee_wage']; $data['price_total'] = $val['price_all']+$data['profix_car_after']; $price_need = $data['price_total'] - $data['rent'] - $data['wat_ele'] - $data['employee_wage'] - $data['manager_wage'] - $data['commission'] - $val['price_trucking']; $partner_rate = $biz_info['rate']/100; $liche_rate = (100-$biz_info['rate'])/100; $data['profix_liche'] = $price_need*$liche_rate; $data['profix_partner'] = $price_need*$partner_rate; //超出裸车价部分 $overflow = $this->biz_settle_model->sum('oflow_profix_car',['stic_id'=>$val['id']]); if($overflow['oflow_profix_car']){ $data['profix_partner_oflow'] = sprintf("%.2f",$overflow['oflow_profix_car']/1.13); } $this->biz_settle_static_model->update($data,['id'=>$val['id']]); } }else{ echo 'finish'; } } } //品牌店统计 public function settle_brand(){ $biz_id = $this->input->get('biz_id'); $id = $this->input->get('id'); $size = $this->input->get('size'); !$size && $size = 5; $t1 = 'lc_receiver_order_status'; $t2 = 'lc_receiver_orders_v2'; $t3 = 'lc_biz_settle'; $t4 = 'lc_biz'; $fields = "$t2.*,$t4.city_id"; $where = [ "$t1.pid_status" => 3, "$t1.status" => 1, "$t2.status in (0,1)" => null, "$t2.id>=" => Orders_v2_entity::V2_START_ID, "$t2.bill_time !='0000-00-00 00:00:00'" => null, "$t2.bill_time>=" => "2022-07-01 00:00:00", "$t3.id is null" => null, "$t4.type" => 1, ]; $id && $where["$t2.id"] = $id; $biz_id && $where["$t4.id"] = $biz_id; $this->db->from("$t1"); $this->db->join("$t2", "$t2.id=$t1.o_id",'left'); $this->db->join("$t3", "$t3.o_id=$t1.o_id",'left'); $this->db->join("$t4", "$t4.id=$t2.biz_id",'left'); $this->db->select($fields); $this->db->where($where); $this->db->order_by("$t2.id ASC"); $this->db->limit($size); $rows = $this->db->get()->result_array(); if($rows){ foreach($rows as $key=>$val){ $info_json = json_decode($val['info_json'],true); $money_json = json_decode($val['money_json'],true); $bill_time_month = date('Y-m-d',strtotime($val['bill_time'])); $settle_money_json = []; $settle_money_json['in'] = [ //收入 'profix_car' => 0, //整车 'srv_price' => $this->orders_v2_entity->order_srv_money($val['id']), //服务费 'commission' => 0, //佣金 ]; $settle_money_json['out'] = [ //成本 // 'biz_price' => 0', //门店成本 'apply_price' => 0, //申请成本 'fine_price' => 0, //赠送精品成本 ]; //获取创建订单时商务政策 $where = [ 'id' => $info_json['business_id'], ]; $b_row_one = $this->auto_business_model->get($where); $settle_money_json['in']['profix_car'] = $b_row_one['profix_car'] ? $b_row_one['profix_car']-$money_json['price_discount'] : 0; //挂牌利润 = 实收 - 成本 $profix_carno = 0; if($money_json['fee_carno']>0){ $where = [ 'biz_id' => $val['biz_id'], 'type' => 1, 'status' => 1, 's_effect_time>=' => date('Y-m-01',strtotime($val['bill_time'])), 's_effect_time<=' => date('Y-m-t',strtotime($val['bill_time'])), ]; $free_row = $this->biz_settle_srv_model->get($where); if(!$free_row){ $free_row = $this->biz_settle_srv_model->get(['biz_id'=>$val['biz_id'],'is_def'=>1,'status'=>1,'type'=>1]); } $free_jsondata = json_decode($free_row['jsondata'],true); $cb_fee_carno = $free_jsondata['price'] ? $free_jsondata['price'] : 0; $profix_carno = $money_json['fee_carno'] - $cb_fee_carno; } //保险利润 = 金额(是否含税) * 返点百分比(保险类型):保险生效日 $data_row = $this->receiver_order_datas_model->get(['o_id'=>$val['id']]); $insurance_img = json_decode($data_row['insurance_img'],true); $business_img = json_decode($data_row['business_img'],true); $profix_insuer_ins = 0; if($insurance_img){ //强险 $time = strtotime($insurance_img['date']); $where = [ 'biz_id' => $val['biz_id'], 'title' => $insurance_img['product'], 'type' => 2, "json_extract(jsondata, '$.type') = '2'" => null, "json_extract(jsondata, '$.s_time') >= '{$time}'" => null, "json_extract(jsondata, '$.e_time') <= '{$time}'" => null ]; $fd_row = $this->biz_settle_srv_model->get($where); if(!$fd_row){ $where = [ 'biz_id' => $val['biz_id'], 'title' => $insurance_img['product'], 'type' => 2, 'is_def' => 1, "json_extract(jsondata, '$.type') = '2'" => null, ]; $fd_row = $this->biz_settle_srv_model->get($where); } $fd_jsondata = json_decode($fd_row['jsondata'],true); $qx_fd = $fd_jsondata['rebate']/100; $profix_insuer_ins = floatval($insurance_img['price']) * $qx_fd; } $profix_insuer_bis = 0; if($business_img){ //商业险 $time = strtotime($business_img['date']); $where = [ 'biz_id' => $val['biz_id'], 'title' => $business_img['product'], 'type' => 2, "json_extract(jsondata, '$.type') = '1'" => null, "json_extract(jsondata, '$.s_time') >= '{$time}'" => null, "json_extract(jsondata, '$.e_time') <= '{$time}'" => null ]; $bis_fd_row = $this->biz_settle_srv_model->get($where); if(!$bis_fd_row){ $where = [ 'biz_id' => $val['biz_id'], 'title' => $business_img['product'], 'type' => 2, 'is_def' => 1, "json_extract(jsondata, '$.type') = '1'" => null, ]; $bis_fd_row = $this->biz_settle_srv_model->get($where); } $bis_fd_jsondata = json_decode($bis_fd_row['jsondata'],true); $bis_fd = $bis_fd_jsondata['rebate']/100; $profix_insuer_bis = floatval($business_img['price'])*$bis_fd; } $profix_insuer = $profix_insuer_ins + $profix_insuer_bis; //贷款利润 = 手续费 + 贷款金额 * 返点(对应产品) // $profix_loan = floatval($money_json['price_finance']); //贷款佣金 = 贷款金额 * 返点(对应产品) $profix_loan = 0; if($money_json['price_loan']>=0){ $loan_row = $this->order_loans_model->get(['o_id'=>$val['id']]); $finance_id = $val['finance_id']; $finance_num = $loan_row['num'] ? $loan_row['num'] : 0; $where = [ 'biz_id' => $val['biz_id'], 'type' => 3, 's_effect_time' => $bill_time_month, "json_extract(jsondata, '$.finance_id') = '{$finance_id}'" => null, "json_extract(jsondata, '$.finance_num') = '{$finance_num}'" => null, ]; $fd_row = $this->biz_settle_srv_model->get($where); if(!$fd_row){ $fd_row = $this->biz_settle_srv_model->get(['biz_id'=>$val['biz_id'],'is_def'=>1,'status'=>1,'type'=>3]); } $fd_jsondata = json_decode($fd_row['jsondata'],true); $loan_fd = $fd_jsondata['rebate'] ? $fd_jsondata['rebate']/100:0; $profix_loan = $money_json['price_loan']*$loan_fd; //获取贷款手续费 $srv_loan = 0; $srv_json = json_decode($val['srv_ids'],true); if($srv_json){ foreach ($srv_json as $v2) { if($v2['key']=='price_finance'){ $srv_loan = $v2['price']; break; } } } $profix_loan += $srv_loan; } $settle_money_json['profix_insuer_ins'] = $profix_insuer_ins; $settle_money_json['profix_insuer_bis'] = $profix_insuer_bis; $settle_money_json['in']['commission'] = $profix_loan+$profix_insuer; //精品成本(赠送的也要算) $profix_fine = 0; $fines = json_decode($val['fines'],true); $fines_ids = array_column($fines,'id'); if($fines){ $fines_ids = implode(',',$fines_ids); $where = [ "id in ({$fines_ids})" => null ]; $fines_ids && $cb_sum = $this->receiver_fine_model->sum('price',$where); $cb = $cb_sum['price'] ? $cb_sum['price'] : 0; //成本 $profix_fine = $cb; } $settle_money_json['out']['biz_price'] = $profix_fine; //拖车费 $truck_row = $this->biz_trucking_model->get(['auto_b_id'=>$val['brand_id'],'biz_id'=>$val['biz_id'],'status'=>1]); $price_trucking = $truck_row['money'] ? $truck_row['money'] : 0; $add_data = [ 'biz_id' => $val['biz_id'], 'o_id' => $val['id'], 'profix_car' => $settle_money_json['in']['profix_car'], 'profix_insure' => $profix_insuer, 'profix_loan' => $profix_loan, 'profix_carno' => $profix_carno, 'price_trucking' => $price_trucking, 'profix_fine' => $money_json['price_fine_select'] - $profix_fine, 'money_json' => json_encode($settle_money_json,JSON_UNESCAPED_UNICODE), 'year' => date('Y',strtotime($val['bill_time'])), 'month' => intval(date('m',strtotime($val['bill_time']))), 'type' => 1, 'c_time' => time() ]; if($money_json['price_car']>$b_row_one['price_floor']){ $add_data['oflow_profix_car'] = $money_json['price_car'] - $b_row_one['price_floor']; } $this->biz_settle_model->add($add_data); } }else{ echo 'finish'; } } //统计前一个月利润 public function merge_brand(){ $size = $this->input->get('size'); $page = 1; !$size && $size = 5; $where = [ "year" => date('Y',strtotime("last month")), "month" => intval(date('m',strtotime('last day of - 1 months'))), "stic_id" => 0, "type" => 1 ]; $rows = $this->biz_settle_model->select($where,'id asc',$page,$size); if($rows){ foreach($rows as $key=>$val){ $where = [ 'biz_id' => $val['biz_id'], 'year' => $val['year'], 'month' => $val['month'] ]; $static_row = $this->biz_settle_static_model->get($where); $price_all = $val['profix_insure']+$val['profix_loan']+$val['profix_carno']+$val['profix_fine']; if(!$static_row){ $data = [ 'biz_id' => $val['biz_id'], 'profix_car' => $val['profix_car'], 'price_all' => $price_all, 'price_trucking' => $val['price_trucking'], 'year' => $val['year'], 'month' => $val['month'], 'c_time' => time() ]; $biz_price = 0;//门店成本 $biz_price_list = []; $p_where = [ 'biz_id' => $val['biz_id'], 'status' => 1, 'year' => $val['year'], 'month' => $val['month'], ]; $price_row = $this->biz_settle_price_model->get($p_where); if($price_row){ $biz_price = $this->biz_settle_price_model->sum_jsondata($price_row['money_json']); $biz_price_list = $this->biz_settle_price_model->get_jsondata($price_row['money_json']); } $add_money_json = [ //成本 'biz_price' => $biz_price, //门店成本 'biz_price_list' => $biz_price_list ]; $data['money_json'] = json_encode($add_money_json,JSON_UNESCAPED_UNICODE); $stic_id = $this->biz_settle_static_model->add($data); if($stic_id){ $this->biz_settle_model->update(['stic_id'=>$stic_id],['id'=>$val['id']]); } }else{ $data = [ 'profix_car' => $static_row['profix_car'] + $val['profix_car'], 'price_all' => $static_row['price_all'] + $price_all, 'price_trucking' => $static_row['price_trucking'] + $val['price_trucking'], ]; $res = $this->biz_settle_static_model->update($data,['id'=>$static_row['id']]); if($res){ $this->biz_settle_model->update(['stic_id'=>$static_row['id']],['id'=>$val['id']]); } } } }else{ //计算分润 $where = [ 'price_total' => 0.00 ]; $rows = $this->biz_settle_static_model->select($where,'id asc',$page,$size); if($rows){ foreach($rows as $val){ $money_json = json_decode($val['money_json'],true); $data = [ 'profix_car_after' => sprintf("%.2f",$val['profix_car']/1.13), ]; $data['price_total'] = sprintf("%.2f",$val['price_all']/1.13)+$data['profix_car_after']; $biz_info = $this->biz_info_model->get(['biz_id'=>$val['biz_id']]); $price_need = $data['price_total'] - $money_json['biz_price']; $partner_rate = $biz_info['rate']/100; $liche_rate = (100-$biz_info['rate'])/100; $data['profix_liche'] = $price_need*$liche_rate; $data['profix_partner'] = $price_need*$partner_rate; $this->biz_settle_static_model->update($data,['id'=>$val['id']]); } }else{ echo 'finish'; } } } }