Files
2022-09-15 15:48:56 +08:00

161 lines
7.4 KiB
PHP

<?php
class Settle_entity{
private $ci;
public function __construct(){
$this->ci = & get_instance();
$this->ci->load->model('biz/biz_settle_model');
$this->ci->load->model('biz/biz_settle_static_model');
$this->ci->load->model('biz/biz_settle_price_model');
$this->ci->load->model('biz/biz_info_model');
$this->ci->load->model('biz/biz_trucking_model');
$this->ci->load->model('receiver/order/receiver_orders_v2_model');
}
//品牌更新统计
public function brand_static($static_id,$biz_id,$year,$month){
if($static_id){
$static_row = $this->ci->biz_settle_static_model->get(['id'=>$static_id]);
}else{
$where = [
'biz_id' => $biz_id,
'year' => $year,
'month' => $month
];
$static_row = $this->ci->biz_settle_static_model->get($where);
}
if(!$static_row){
return ['code'=>0,'msg'=>'统计数据不存在'];
}
$all_other = 0; //共同承担其它费用
$liche_other = 0; //狸车承担其它费用
$partner_other = 0; //品牌承担其它费用
$price_other = json_decode($static_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_price = 0;//门店成本
$biz_price_list = [];
$p_where = [
'biz_id' => $static_row['biz_id'],
'status' => 1,
'year' => $static_row['year'],
'month' => $static_row['month'],
];
$price_row = $this->ci->biz_settle_price_model->get($p_where);
if($price_row){
$biz_price = $this->ci->biz_settle_price_model->sum_jsondata($price_row['money_json']);
$biz_price_list = $this->ci->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);
$sum_profix_car = $this->ci->biz_settle_model->sum('profix_car',['stic_id'=>$static_row['id']]);
$sum_profix_insure = $this->ci->biz_settle_model->sum('profix_insure',['stic_id'=>$static_row['id']]);
$sum_profix_loan = $this->ci->biz_settle_model->sum('profix_loan',['stic_id'=>$static_row['id']]);
$sum_profix_carno = $this->ci->biz_settle_model->sum('profix_carno',['stic_id'=>$static_row['id']]);
$sum_profix_fine = $this->ci->biz_settle_model->sum('profix_fine',['stic_id'=>$static_row['id']]);
$data['profix_car'] = $sum_profix_car['profix_car'];
$data['price_all'] = $sum_profix_insure['profix_insure']+$sum_profix_loan['profix_loan']+$sum_profix_carno['profix_carno']+$sum_profix_fine['profix_fine'];
$data['profix_car_after'] = sprintf("%.2f",$data['profix_car']/1.13);
$data['price_total'] = sprintf("%.2f",$data['price_all']/1.13)+$data['profix_car_after'];
//计算分润
$biz_info = $this->ci->biz_info_model->get(['biz_id'=>$static_row['biz_id']]);
$price_need = $data['price_total'] - $biz_price - $all_other;
$partner_rate = $biz_info['rate']/100;
$liche_rate = (100-$biz_info['rate'])/100;
$data['profix_liche'] = $price_need*$liche_rate-$liche_other;
$data['profix_partner'] = $price_need*$partner_rate-$partner_other;
$res = $this->ci->biz_settle_static_model->update($data,['id'=>$static_row['id']]);
if($res){
return ['code'=>1,'msg'=>'更新成功'];
}else{
return ['code'=>0,'msg'=>'更新失败'];
}
}
//合伙人更新统计
public function re_static($static_id,$biz_id,$year,$month){
if($static_id){
$row = $this->ci->biz_settle_static_model->get(['id'=>$static_id]);
}else{
$where = [
'biz_id' => $biz_id,
'year' => $year,
'month' => $month
];
$row = $this->ci->biz_settle_static_model->get($where);
}
if(!$row){
return ['code'=>0,'msg'=>'统计数据不存在'];
}
$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'];
}
}
}
$car_total = $this->ci->biz_settle_model->count(['stic_id'=>$row['id']]);
$biz_info = $this->ci->biz_info_model->get(['biz_id'=>$row['biz_id']]);
$partner_rate = $biz_info['rate']/100;
$liche_rate = (100-$biz_info['rate'])/100;
$update = [
'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;
$update['employee_wage'] = $employee_wage*$biz_info['employee_wage'];
$settle_rows = $this->ci->biz_settle_model->select(['stic_id'=>$row['id']],'id asc',0,0,'id,o_id');
if(is_array($settle_rows)){
$o_id_str = implode(',',array_column($settle_rows,'o_id'));
$order_rows = [];
if($o_id_str){
$where = [
"id in ($o_id_str)" => null
];
$order_rows = $this->ci->receiver_orders_v2_model->map('id','brand_id',$where,0,0,'id,brand_id');
}
foreach($settle_rows as $val){
$auto_b_id = $order_rows[$val['o_id']] ? $order_rows[$val['o_id']] : 0;
$truck_row = $this->ci->biz_trucking_model->get(['biz_id'=>$row['biz_id'],'auto_b_id'=>$auto_b_id,'status'=>1]);
$price = $truck_row ? $truck_row['money'] : 0;
$this->ci->biz_settle_model->update(['price_trucking'=>$price],['id'=>$val['id']]);
}
}
$truck_sum = $this->ci->biz_settle_model->sum('price_trucking',['stic_id' => $row['id']]);
$update['price_trucking'] = $truck_sum['price_trucking'];
$price_need = $update['rent'] + $update['wat_ele'] + $update['employee_wage'] + $update['manager_wage'] + $update['commission'] + $update['price_trucking'];
$price_ml = $row['price_total'] - $price_need - $all_other;
$update['profix_liche'] = $price_ml*$liche_rate-$liche_other;
$update['profix_partner'] = $price_ml*$partner_rate-$partner_other;
$res = $this->ci->biz_settle_static_model->update($update,['id'=>$row['id']]);
if($res){
return ['code'=>1,'msg'=>'更新成功'];
}else{
return ['code'=>0,'msg'=>'更新失败'];
}
}
}