Files
2022-05-05 14:06:14 +08:00

181 lines
7.8 KiB
PHP

<?php
class Biz extends HD_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('receiver/order/receiver_orders_v2_model','orders_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_trucking_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'] - $data['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';
}
}
}
}