load->model('receiver/order/receiver_orders_v2_model','orders_model'); $this->load->model('receiver/order/receiver_order_agents_model', 'order_agents_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('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 ]; $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; $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(){ $size = $this->input->get('size'); !$size && $size = 5; $t1 = 'lc_receiver_orders_v2'; $t2 = 'lc_biz_settle'; $t3 = 'lc_biz'; $fields = "$t1.*"; $where = [ "$t1.status" => 1, "$t1.id>=" => Orders_v2_entity::V2_START_ID, "$t2.id is null" => null, "$t3.type" => 1, ]; $this->db->from("$t1"); $this->db->join("$t2", "$t2.o_id=$t1.id",'left'); $this->db->join("$t3", "$t3.id=$t1.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); $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; //挂牌利润 = 实收 - 成本 $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',$val['c_time']), 's_effect_time<=' => date('Y-m-t',$val['c_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; } //保险利润 = 金额(是否含税) * 返点百分比(保险类型):保险生效日 $agent_row = $this->order_agents_model->get(['o_id'=>$val['id']]); $agent_json = json_decode($agent_row['jsondata'],true); //强险 $where = [ 'biz_id' => $val['biz_id'], 'type' => 2, "json_extract(jsondata, '$.type') = '1'" => 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($agent_json['ins_risk']) * $qx_fd; //商业险 $where = [ 'biz_id' => $val['biz_id'], 'type' => 2, "json_extract(jsondata, '$.type') = '2'" => 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($money_json['business_risk'])*$bis_fd; $profix_insuer = $profix_insuer_ins + $profix_insuer_bis; //贷款利润 = 手续费 + 贷款金额 * 返点(对应产品) $profix_loan = floatval($money_json['price_finance']); 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, "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; } //实收 - 成本(赠送的也要算) $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; //成本 $sh = 0; //实收 foreach ($fines as $f_val) { $sh += floatval($f_val['price']); } $profix_fine = $sh-$cb; } $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' => $profix_insuer, 'profix_loan' => $profix_loan, 'profix_carno' => $profix_carno, 'price_trucking' => $price_trucking, 'profix_fine' => $profix_fine, 'year' => date('Y'), 'month' => intval(date('m')), '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'; } } }