|
@@ -2,6 +2,8 @@
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
+use App\Model\EmployeeIndex;
|
|
|
+use App\Model\GiveOut;
|
|
|
use App\Model\RevenueCost;
|
|
|
use App\Model\RevenueCostTotal;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
@@ -229,4 +231,179 @@ class StatisticsService extends Service
|
|
|
|
|
|
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');
|
|
|
+ $minStart = min(array_column($existingData, 'start_time'));
|
|
|
+ $maxEnd = max(array_column($existingData, 'end_time'));
|
|
|
+
|
|
|
+ // 一次性查出这些员工在最大区间范围内的所有指标
|
|
|
+ $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');
|
|
|
+ $minStart = min(array_column($existingData, 'start_time'));
|
|
|
+ $maxEnd = max(array_column($existingData, 'end_time'));
|
|
|
+
|
|
|
+ // 一次性查出这些员工在最大区间范围内的所有指标
|
|
|
+ $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($value['index_1'],100,2);
|
|
|
+ $index_5 = bcdiv($value['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;
|
|
|
+ }
|
|
|
}
|