| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 | 
							- <?php
 
- namespace App\Service;
 
- use App\Model\EmployeeIndex;
 
- use App\Model\GiveOut;
 
- use App\Model\RevenueCost;
 
- use App\Model\RevenueCostTotal;
 
- use App\Model\SalaryEmployee;
 
- use Illuminate\Support\Facades\DB;
 
- class StatisticsService extends Service
 
- {
 
-     public function statisticsRevenueCostCommon($data,$user, $field = []){
 
-         if(empty($field)) $field = RevenueCostTotal::$field;
 
-         $model = RevenueCostTotal::where('del_time',0)
 
-             ->select($field)
 
-             ->orderby('id', 'desc');
 
-         if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
 
-             list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
 
-             if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
 
-             $model->where('order_time', '>=', $start_time)
 
-                 ->where('order_time', '<', $end_time);
 
-         }
 
-         if(! empty($data['order_type'])) $model->where('order_type',$data['order_type']);
 
-         return [true, $model];
 
-     }
 
-     public function statisticsRevenueCost($data,$user){
 
-         list($status, $model) = $this->statisticsRevenueCostCommon($data, $user);
 
-         if(! $status) return [false, $model];
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->statisticsRevenueCostFillData($list);
 
-         return [true, $list];
 
-     }
 
-     public function statisticsRevenueCostFillData($data){
 
-         if(empty($data['data'])) return $data;
 
-         foreach ($data['data'] as $key => $value){
 
-             $time = date("Y-m", $value['order_time']);
 
-             $data['data'][$key]['order_time'] = $time;
 
-             $data['data'][$key]['order_type_title'] = RevenueCost::$order_type[$value['order_type']] ?? "";
 
-             $data['data'][$key]['profit_rate'] = bcmul($value['profit_rate'], 100,2);
 
-         }
 
-         return $data;
 
-     }
 
-     //无用
 
-     public function statisticsRevenueCostFillData1($data){
 
-         if(empty($data)) return $data;
 
-         $return = [];
 
-         foreach ($data as $key => $value){
 
-             $time = date("Y-m", $value['order_time']);
 
-             if($value['order_type'] == RevenueCost::ORDER_ONE){
 
-                 $income = $value['price_3_total'];
 
-             }elseif ($value['order_type'] == RevenueCost::ORDER_TWO){
 
-                 $income = $value['price_1_total'];
 
-             }else{
 
-                 $income = $value['payment_amount'];
 
-             }
 
-             $adjust = $income < 0 ? $income : 0;
 
-             $business = $value['price_4_total'];
 
-             if(isset($return[$time][$value['order_type']])){
 
-                 $income_total = bcadd($return[$time][$value['order_type']]['income'], $income,2);
 
-                 $adjust_total = bcadd($return[$time][$value['order_type']]['adjust'], $adjust,2);
 
-                 $business_total = bcadd($return[$time][$value['order_type']]['business'], $business,2);
 
-                 $return[$time][$value['order_type']]['income'] = $income_total;
 
-                 $return[$time][$value['order_type']]['adjust'] = $adjust_total;
 
-                 $return[$time][$value['order_type']]['business'] = $business_total;
 
-             }else{
 
-                 $return[$time][$value['order_type']] = [
 
-                     'income' => $income,
 
-                     'adjust' => $adjust,
 
-                     'business' => $business,
 
-                     'time' => $time,
 
-                 ];
 
-             }
 
-         }
 
-         $final = [];
 
-         foreach ($return as $value){
 
-             foreach ($value as $key => $val){
 
-                 $title = RevenueCost::$order_type[$key] ?? "";
 
-                 $val['title'] = $title;
 
-                 $profit = bcsub($val['income'], $val['business'],2);
 
-                 $profit_rate = $val['income'] > 0 ? bcdiv($profit, $val['income'],2) : 0;
 
-                 $profit_rate = bcmul($profit_rate, 100,2);
 
-                 $val['profit'] = $profit;
 
-                 $val['profit_rate'] = $profit_rate;
 
-                 $val['order_type'] = $key;
 
-                 $final[] = $val;
 
-             }
 
-         }
 
-         return $final;
 
-     }
 
-     public function statisticsRevenueCostOneAndTwoCommon($data,$user, $field = []){
 
-         if(empty($data['order_type']) || ! isset(RevenueCost::$order_type[$data['order_type']])) return [false, '单据类型不存在或错误'];
 
-         if(empty($field)) {
 
-             if($data['order_type'] == 1){
 
-                 $field = RevenueCost::$field_xhd;
 
-             }else{
 
-                 $field = RevenueCost::$field_xsfp;
 
-             }
 
-         }
 
-         $model = RevenueCost::where('del_time',0)
 
-             ->where('order_type',$data['order_type'])
 
-             ->select($field)
 
-             ->orderby('id', 'desc');
 
-         if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
 
-             list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
 
-             if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
 
-             $model->where('order_time', '>=', $start_time)
 
-                 ->where('order_time', '<=', $end_time);
 
-         }
 
-         if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
 
-         if(! empty($data['customer_title'])) $model->where('customer_title', 'LIKE', '%'.$data['customer_title'].'%');
 
-         if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
 
-         return [true, $model];
 
-     }
 
-     public function statisticsRevenueCostOneAndTwo($data,$user){
 
-         list($status, $model) = $this->statisticsRevenueCostOneAndTwoCommon($data, $user);
 
-         if(! $status) return [false, $model];
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->statisticsRevenueCostOneAndTwoFillData($list);
 
-         return [true, $list];
 
-     }
 
-     public function statisticsRevenueCostOneAndTwoFillData($data){
 
-         if(empty($data['data'])) return $data;
 
-         foreach ($data['data'] as $key => $value){
 
-             $data['data'][$key]['profit_rate'] = bcmul($value['profit_rate'], 100,2);
 
-             $data['data'][$key]['order_time'] = $value['order_time'] ? date("Y-m-d", $value['order_time']) : "";
 
-         }
 
-         return $data;
 
-     }
 
-     public function statisticsRevenueCostThreeCommon($data,$user, $field = []){
 
-         if(empty($field)) {
 
-             $field = RevenueCost::$field_hkd_salary_main;
 
-             $field[] = DB::raw('sum(payment_amount) as payment_amount');
 
-             $field[] = DB::raw('sum(price_4_total) as price_4_total');
 
-             $field[] = DB::raw('sum(profit) as profit');
 
-         }
 
-         $model = RevenueCost::where('del_time',0)
 
-             ->where('order_type',RevenueCost::ORDER_THREE)
 
-             ->select($field)
 
-             ->groupby('order_id')
 
-             ->orderby('id', 'desc');
 
-         if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
 
-             list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
 
-             if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
 
-             $model->where('order_time', '>=', $start_time)
 
-                 ->where('order_time', '<=', $end_time);
 
-         }
 
-         if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
 
-         if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
 
-         return [true, $model];
 
-     }
 
-     public function statisticsRevenueCostThree($data,$user){
 
-         list($status, $model) = $this->statisticsRevenueCostThreeCommon($data, $user);
 
-         if(! $status) return [false, $model];
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->statisticsRevenueCostThreeFillData($list);
 
-         return [true, $list];
 
-     }
 
-     public function statisticsRevenueCostThreeFillData($data){
 
-         if(empty($data['data'])) return $data;
 
-         foreach ($data['data'] as $key => $value){
 
-             $profit_rate = $value['payment_amount'] > 0 ? bcdiv($value['profit'], $value['payment_amount'],2) : 0;
 
-             $data['data'][$key]['profit_rate'] = bcmul($profit_rate, 100,2);
 
-             $data['data'][$key]['order_time'] = $value['order_time'] ? date("Y-m-d", $value['order_time']) : "";
 
-         }
 
-         return $data;
 
-     }
 
-     public function statisticsRevenueCostThreeDetail($data,$user){
 
-         if(empty($data['order_number'])) return [false, '回款单号不能为空'];
 
-         $result = RevenueCost::where('del_time',0)
 
-             ->where('order_type',RevenueCost::ORDER_THREE)
 
-             ->where('order_number',$data['order_number'])
 
-             ->select(RevenueCost::$field_hkd_detail)
 
-             ->get()->toArray();
 
-         if(empty($result)) return [false, "回款单不存在或已被删除"];
 
-         $detail = [];
 
-         foreach ($result as $value){
 
-             $detail[] = [
 
-                 'order_number' => $value['order_number_upstream'],
 
-                 'customer_code' => $value['customer_code'],
 
-                 'customer_title' => $value['customer_title'],
 
-                 'product_code' => $value['product_code'],
 
-                 'product_title' => $value['product_title'],
 
-                 'product_size' => $value['product_size'],
 
-                 'unit' => $value['unit'],
 
-                 'quantity' => $value['quantity'],
 
-                 'payment_amount' => $value['payment_amount'],
 
-                 'price_1' => $value['price_1'],
 
-                 'price_1_total' => $value['price_1_total'],
 
-                 'price_4' => $value['price_4'],
 
-                 'price_4_total' => $value['price_4_total'],
 
-                 'profit' => $value['profit'],
 
-                 'profit_rate' => bcmul($value['profit_rate'],100,2),
 
-             ];
 
-         }
 
-         $first = $result[0];
 
-         $order = [
 
-             'order_number' => $data['order_number'],
 
-             'order_time' => date("Y-m-d",$first['order_time']),
 
-             'employee_id_1_title' => $first['employee_id_1_title'],
 
-             'detail' => $detail
 
-         ];
 
-         return [true, $order];
 
-     }
 
-     public function statisticsProfitCommon($data,$user, $field = []){
 
-         if(empty($field)) {
 
-             $field = RevenueCost::$field_hkd_profit_main;
 
-             $field[] = DB::raw('COALESCE(SUM(rc.payment_amount), 0) AS payment_amount');
 
-         }
 
-         $type = RevenueCost::ORDER_THREE;
 
-         $model = EmployeeIndex::where('employee_index.del_time',0)
 
-             ->where('employee_index.type',EmployeeIndex::TYPE_THREE)
 
-             ->leftJoin(DB::raw('revenue_cost as rc'), function ($join) use ($type) {
 
-                 $join->on('rc.employee_id_2', '=', 'employee_index.employee_id')
 
-                     ->where('rc.del_time', 0)
 
-                     ->where('rc.order_type', $type)
 
-                     ->whereRaw('rc.order_time >= employee_index.start_time')
 
-                     ->whereRaw('rc.order_time <= employee_index.end_time');
 
-             })
 
-             ->select($field)
 
-             ->groupBy('employee_index.employee_id', 'employee_index.start_time', 'employee_index.end_time')
 
-             ->orderBy('employee_index.end_time','desc');
 
-         if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){;
 
-             list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
 
-             if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
 
-             $model->where('employee_index.start_time', '<=', $end_time)
 
-                 ->where('employee_index.end_time', '>=', $start_time);
 
-         }
 
-         if(! empty($data['employee_id_2_title'])) $model->where('rc.employee_id_2_title', 'LIKE', '%'.$data['employee_id_2_title'].'%');
 
-         return [true, $model];
 
-     }
 
-     public function statisticsProfit($data,$user){
 
-         list($status, $model) = $this->statisticsProfitCommon($data, $user);
 
-         if(! $status) return [false, $model];
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->statisticsProfitFillData($list);
 
-         return [true, $list];
 
-     }
 
-     public function statisticsProfitFillData($data){
 
-         if(empty($data['data'])) return $data;
 
-         //获取应发放金额需要的指标
 
-         $indexes = $this->getEmployeeIndex($data['data']);
 
-         //已发
 
-         $indexes_give = $this->getEmployeeGiveOut($data['data']);
 
-         foreach ($data['data'] as $key => $value){
 
-             $start = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
 
-             $end = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
 
-             $string = "";
 
-             if(! empty($start) && ! empty($end)) $string = $start . "-" . $end;
 
-             $value['check_time'] = $string;
 
-             $value['index_' . EmployeeIndex::TYPE_ONE] = 0;
 
-             $value['index_' . EmployeeIndex::TYPE_FIVE] = 0;
 
-             //指标
 
-             $this->makeIndex($indexes, $value, $data['data'][$key]);
 
-             //应付
 
-             $this->makeShouldPay($value,$data['data'][$key]);
 
-             //已付
 
-             $this->makePayed($indexes_give, $value,$data['data'][$key]);
 
-             //未付
 
-             $not_pay = bcsub($data['data'][$key]['should_pay'], $data['data'][$key]['payed'],2);
 
-             $data['data'][$key]['not_pay'] = $not_pay;
 
-         }
 
-         return $data;
 
-     }
 
-     private function getEmployeeIndex($existingData)
 
-     {
 
-         if (empty($existingData)) {
 
-             return collect();
 
-         }
 
-         // 取出所有涉及的 employee_id 和时间区间
 
-         $employeeIds = array_column($existingData, 'employee_id_2');
 
-         $start_time = array_column($existingData, 'start_time');
 
-         $minStart = ! empty($start_time) ? min($start_time) : 0;
 
-         $end_time = array_column($existingData, 'end_time');
 
-         $maxEnd   = ! empty($end_time) ? max($end_time) : 0;
 
-         // 一次性查出这些员工在最大区间范围内的所有指标
 
-         $results = EmployeeIndex::where('del_time', 0)
 
-             ->whereIn('type', [EmployeeIndex::TYPE_ONE, EmployeeIndex::TYPE_FIVE])
 
-             ->whereIn('employee_id', $employeeIds)
 
-             ->where('start_time', '<=', $maxEnd)
 
-             ->where('end_time', '>=', $minStart)
 
-             ->select('start_time','end_time','employee_id','index','type')
 
-             ->get();
 
-         return $results;
 
-     }
 
-     private function getEmployeeGiveOut($existingData)
 
-     {
 
-         if (empty($existingData)) {
 
-             return collect();
 
-         }
 
-         // 取出所有涉及的 employee_id 和时间区间
 
-         $employeeIds = array_column($existingData, 'employee_id_2');
 
-         $start_time = array_column($existingData, 'start_time');
 
-         $minStart = ! empty($start_time) ? min($start_time) : 0;
 
-         $end_time = array_column($existingData, 'end_time');
 
-         $maxEnd   = ! empty($end_time) ? max($end_time) : 0;
 
-         // 一次性查出这些员工在最大区间范围内的所有指标
 
-         $results = GiveOut::where('del_time', 0)
 
-             ->whereIn('employee_id_1', $employeeIds)
 
-             ->where('start_time', '<=', $maxEnd)
 
-             ->where('end_time', '>=', $minStart)
 
-             ->select('start_time','end_time','employee_id_1 as employee_id','give_out_amount')
 
-             ->get();
 
-         return $results;
 
-     }
 
-     private function makeIndex($indexes, $value, &$data){
 
-         // 找到所有符合条件的 index(可能多个 type)
 
-         $matchedIndexes = $indexes->filter(function ($item) use ($value) {
 
-             return $item['employee_id'] == $value['employee_id_2']
 
-                 && $item['start_time'] <= $value['end_time']
 
-                 && $item['end_time'] >= $value['start_time'];
 
-         });
 
-         // 按 type 去重,只保留第一次出现的
 
-         $uniqueByType = [];
 
-         foreach ($matchedIndexes as $item) {
 
-             $index = "index_" . $item['type'];
 
-             if (! isset($uniqueByType[$index])) {
 
-                 $uniqueByType[$index] = $item['index'];
 
-             }
 
-         }
 
-         $data = array_merge($value, $uniqueByType);
 
-     }
 
-     private function makeShouldPay($value, &$data){
 
-         $index_1 = bcdiv($data['index_1'],100,2);
 
-         $index_5 = bcdiv($data['index_5'],100,2);
 
-         // 分红比例/2
 
-         $a = bcdiv($index_5,2,2);
 
-         // 回款金额*季度预支分红比例/2
 
-         $b = bcdiv(bcmul($value['payment_amount'], $index_1),2,2);
 
-         //(分红比例/2-回款金额*季度预支分红比例/2)
 
-         $c = bcsub($a, $b,2);
 
-         $data['part_left'] = $c;
 
-         if($value['payment_amount'] < $value['index']){
 
-             //回款金额*(分红比例/2-回款金额*季度预支分红比例/2);
 
-             $shouldPay = bcmul($value['payment_amount'], $c,2);
 
-         }else{
 
-             //回款金额-季度指标金额
 
-             $d = bcsub($value['payment_amount'], $value['index'],2);
 
-             //(回款金额-季度指标金额)*15%
 
-             $e = bcmul($d, 0.15,2);
 
-             //(回款金额-季度指标金额)*15%/2
 
-             $e = bcdiv($e,2,2);
 
-             //(分红比例/2-回款金额*季度预支分红比例/2+(回款金额-季度指标金额)*15%/2)
 
-             $rate = bcadd($c, $e,2);
 
- //                $data['data'][$key]['rate'] = $rate;
 
-             //回款金额*(分红比例/2-回款金额*季度预支分红比例/2+(回款金额-季度指标金额)*15%/2)
 
-             $shouldPay = bcmul($value['payment_amount'], $rate,2);
 
-         }
 
-         $data['should_pay'] = $shouldPay;
 
-     }
 
-     private function makePayed($indexes, $value, &$data){
 
-         $matchedIndexes_give = $indexes->filter(function ($item) use ($value) {
 
-             return $item['employee_id'] == $value['employee_id_2']
 
-                 && $item['start_time'] <= $value['end_time']
 
-                 && $item['end_time'] >= $value['start_time'];
 
-         });
 
-         $give_out = 0;
 
-         foreach ($matchedIndexes_give as $item) {
 
-             $give_out = bcadd($item['give_out_amount'], $give_out,2);
 
-         }
 
-         $data['payed'] = $give_out;
 
-     }
 
-     public function statisticsEmployeeSalaryCommon($data,$user, $field = []){
 
-         if(empty($field)) {
 
-             $field = SalaryEmployee::$field;
 
-         }
 
-         $model = SalaryEmployee::where('del_time',0)
 
-             ->where('order_type',RevenueCost::ORDER_THREE)
 
-             ->select($field)
 
-             ->orderby('order_time', 'desc');
 
-         if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
 
-             list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
 
-             if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
 
-             $start_time = strtotime(date("Y-m-01",$start_time));
 
-             $end_time = strtotime(date("Y-m-01",$end_time));
 
-             $model->where('order_time', '>=', $start_time)
 
-                 ->where('order_time', '<=', $end_time);
 
-         }
 
-         if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
 
-         return [true, $model];
 
-     }
 
-     public function statisticsEmployeeSalary($data,$user){
 
-         list($status, $model) = $this->statisticsEmployeeSalaryCommon($data, $user);
 
-         if(! $status) return [false, $model];
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->statisticsEmployeeSalaryFillData($list);
 
-         return [true, $list];
 
-     }
 
-     public function statisticsEmployeeSalaryFillData($data){
 
-         if(empty($data['data'])) return $data;
 
-         foreach ($data['data'] as $key => $value){
 
-             $order_time = $value['order_time'] ? date('Y-m',$value['order_time']) : '';
 
-             $data['data'][$key]['order_time'] = $order_time;
 
-         }
 
-         return $data;
 
-     }
 
- }
 
 
  |