StatisticsService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. namespace App\Service;
  3. use App\Model\EmployeeIndex;
  4. use App\Model\GiveOut;
  5. use App\Model\RevenueCost;
  6. use App\Model\RevenueCostTotal;
  7. use App\Model\SalaryEmployee;
  8. use Illuminate\Support\Facades\DB;
  9. class StatisticsService extends Service
  10. {
  11. public function statisticsRevenueCostCommon($data,$user, $field = []){
  12. if(empty($field)) $field = RevenueCostTotal::$field;
  13. $model = RevenueCostTotal::where('del_time',0)
  14. ->select($field)
  15. ->orderby('id', 'desc');
  16. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
  17. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  18. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  19. $model->where('order_time', '>=', $start_time)
  20. ->where('order_time', '<', $end_time);
  21. }
  22. if(! empty($data['order_type'])) $model->where('order_type',$data['order_type']);
  23. return [true, $model];
  24. }
  25. public function statisticsRevenueCost($data,$user){
  26. list($status, $model) = $this->statisticsRevenueCostCommon($data, $user);
  27. if(! $status) return [false, $model];
  28. $list = $this->limit($model,'',$data);
  29. $list = $this->statisticsRevenueCostFillData($list);
  30. return [true, $list];
  31. }
  32. public function statisticsRevenueCostFillData($data){
  33. if(empty($data['data'])) return $data;
  34. foreach ($data['data'] as $key => $value){
  35. $time = date("Y-m", $value['order_time']);
  36. $data['data'][$key]['order_time'] = $time;
  37. $data['data'][$key]['order_type_title'] = RevenueCost::$order_type[$value['order_type']] ?? "";
  38. $data['data'][$key]['profit_rate'] = bcmul($value['profit_rate'], 100,2);
  39. }
  40. return $data;
  41. }
  42. //无用
  43. public function statisticsRevenueCostFillData1($data){
  44. if(empty($data)) return $data;
  45. $return = [];
  46. foreach ($data as $key => $value){
  47. $time = date("Y-m", $value['order_time']);
  48. if($value['order_type'] == RevenueCost::ORDER_ONE){
  49. $income = $value['price_3_total'];
  50. }elseif ($value['order_type'] == RevenueCost::ORDER_TWO){
  51. $income = $value['price_1_total'];
  52. }else{
  53. $income = $value['payment_amount'];
  54. }
  55. $adjust = $income < 0 ? $income : 0;
  56. $business = $value['price_4_total'];
  57. if(isset($return[$time][$value['order_type']])){
  58. $income_total = bcadd($return[$time][$value['order_type']]['income'], $income,2);
  59. $adjust_total = bcadd($return[$time][$value['order_type']]['adjust'], $adjust,2);
  60. $business_total = bcadd($return[$time][$value['order_type']]['business'], $business,2);
  61. $return[$time][$value['order_type']]['income'] = $income_total;
  62. $return[$time][$value['order_type']]['adjust'] = $adjust_total;
  63. $return[$time][$value['order_type']]['business'] = $business_total;
  64. }else{
  65. $return[$time][$value['order_type']] = [
  66. 'income' => $income,
  67. 'adjust' => $adjust,
  68. 'business' => $business,
  69. 'time' => $time,
  70. ];
  71. }
  72. }
  73. $final = [];
  74. foreach ($return as $value){
  75. foreach ($value as $key => $val){
  76. $title = RevenueCost::$order_type[$key] ?? "";
  77. $val['title'] = $title;
  78. $profit = bcsub($val['income'], $val['business'],2);
  79. $profit_rate = $val['income'] > 0 ? bcdiv($profit, $val['income'],2) : 0;
  80. $profit_rate = bcmul($profit_rate, 100,2);
  81. $val['profit'] = $profit;
  82. $val['profit_rate'] = $profit_rate;
  83. $val['order_type'] = $key;
  84. $final[] = $val;
  85. }
  86. }
  87. return $final;
  88. }
  89. public function statisticsRevenueCostOneAndTwoCommon($data,$user, $field = []){
  90. if(empty($data['order_type']) || ! isset(RevenueCost::$order_type[$data['order_type']])) return [false, '单据类型不存在或错误'];
  91. if(empty($field)) {
  92. if($data['order_type'] == 1){
  93. $field = RevenueCost::$field_xhd;
  94. }else{
  95. $field = RevenueCost::$field_xsfp;
  96. }
  97. }
  98. $model = RevenueCost::where('del_time',0)
  99. ->where('order_type',$data['order_type'])
  100. ->select($field)
  101. ->orderby('id', 'desc');
  102. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
  103. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  104. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  105. $model->where('order_time', '>=', $start_time)
  106. ->where('order_time', '<=', $end_time);
  107. }
  108. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  109. if(! empty($data['customer_title'])) $model->where('customer_title', 'LIKE', '%'.$data['customer_title'].'%');
  110. if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
  111. return [true, $model];
  112. }
  113. public function statisticsRevenueCostOneAndTwo($data,$user){
  114. list($status, $model) = $this->statisticsRevenueCostOneAndTwoCommon($data, $user);
  115. if(! $status) return [false, $model];
  116. $list = $this->limit($model,'',$data);
  117. $list = $this->statisticsRevenueCostOneAndTwoFillData($list);
  118. return [true, $list];
  119. }
  120. public function statisticsRevenueCostOneAndTwoFillData($data){
  121. if(empty($data['data'])) return $data;
  122. foreach ($data['data'] as $key => $value){
  123. $data['data'][$key]['profit_rate'] = bcmul($value['profit_rate'], 100,2);
  124. $data['data'][$key]['order_time'] = $value['order_time'] ? date("Y-m-d", $value['order_time']) : "";
  125. }
  126. return $data;
  127. }
  128. public function statisticsRevenueCostThreeCommon($data,$user, $field = []){
  129. if(empty($field)) {
  130. $field = RevenueCost::$field_hkd_salary_main;
  131. $field[] = DB::raw('sum(payment_amount) as payment_amount');
  132. $field[] = DB::raw('sum(price_4_total) as price_4_total');
  133. $field[] = DB::raw('sum(profit) as profit');
  134. }
  135. $model = RevenueCost::where('del_time',0)
  136. ->where('order_type',RevenueCost::ORDER_THREE)
  137. ->select($field)
  138. ->groupby('order_id')
  139. ->orderby('id', 'desc');
  140. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
  141. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  142. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  143. $model->where('order_time', '>=', $start_time)
  144. ->where('order_time', '<=', $end_time);
  145. }
  146. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  147. if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
  148. return [true, $model];
  149. }
  150. public function statisticsRevenueCostThree($data,$user){
  151. list($status, $model) = $this->statisticsRevenueCostThreeCommon($data, $user);
  152. if(! $status) return [false, $model];
  153. $list = $this->limit($model,'',$data);
  154. $list = $this->statisticsRevenueCostThreeFillData($list);
  155. return [true, $list];
  156. }
  157. public function statisticsRevenueCostThreeFillData($data){
  158. if(empty($data['data'])) return $data;
  159. foreach ($data['data'] as $key => $value){
  160. $profit_rate = $value['payment_amount'] > 0 ? bcdiv($value['profit'], $value['payment_amount'],2) : 0;
  161. $data['data'][$key]['profit_rate'] = bcmul($profit_rate, 100,2);
  162. $data['data'][$key]['order_time'] = $value['order_time'] ? date("Y-m-d", $value['order_time']) : "";
  163. }
  164. return $data;
  165. }
  166. public function statisticsRevenueCostThreeDetail($data,$user){
  167. if(empty($data['order_number'])) return [false, '回款单号不能为空'];
  168. $result = RevenueCost::where('del_time',0)
  169. ->where('order_type',RevenueCost::ORDER_THREE)
  170. ->where('order_number',$data['order_number'])
  171. ->select(RevenueCost::$field_hkd_detail)
  172. ->get()->toArray();
  173. if(empty($result)) return [false, "回款单不存在或已被删除"];
  174. $detail = [];
  175. foreach ($result as $value){
  176. $detail[] = [
  177. 'order_number' => $value['order_number_upstream'],
  178. 'customer_code' => $value['customer_code'],
  179. 'customer_title' => $value['customer_title'],
  180. 'product_code' => $value['product_code'],
  181. 'product_title' => $value['product_title'],
  182. 'product_size' => $value['product_size'],
  183. 'unit' => $value['unit'],
  184. 'quantity' => $value['quantity'],
  185. 'payment_amount' => $value['payment_amount'],
  186. 'price_1' => $value['price_1'],
  187. 'price_1_total' => $value['price_1_total'],
  188. 'price_4' => $value['price_4'],
  189. 'price_4_total' => $value['price_4_total'],
  190. 'profit' => $value['profit'],
  191. 'profit_rate' => bcmul($value['profit_rate'],100,2),
  192. ];
  193. }
  194. $first = $result[0];
  195. $order = [
  196. 'order_number' => $data['order_number'],
  197. 'order_time' => date("Y-m-d",$first['order_time']),
  198. 'employee_id_1_title' => $first['employee_id_1_title'],
  199. 'detail' => $detail
  200. ];
  201. return [true, $order];
  202. }
  203. public function statisticsProfitCommon($data,$user, $field = []){
  204. if(empty($field)) {
  205. $field = RevenueCost::$field_hkd_profit_main;
  206. $field[] = DB::raw('COALESCE(SUM(rc.payment_amount), 0) AS payment_amount');
  207. }
  208. $type = RevenueCost::ORDER_THREE;
  209. $model = EmployeeIndex::where('employee_index.del_time',0)
  210. ->where('employee_index.type',EmployeeIndex::TYPE_THREE)
  211. ->leftJoin(DB::raw('revenue_cost as rc'), function ($join) use ($type) {
  212. $join->on('rc.employee_id_2', '=', 'employee_index.employee_id')
  213. ->where('rc.del_time', 0)
  214. ->where('rc.order_type', $type)
  215. ->whereRaw('rc.order_time >= employee_index.start_time')
  216. ->whereRaw('rc.order_time <= employee_index.end_time');
  217. })
  218. ->select($field)
  219. ->groupBy('employee_index.employee_id', 'employee_index.start_time', 'employee_index.end_time')
  220. ->orderBy('employee_index.end_time','desc');
  221. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){;
  222. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  223. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  224. $model->where('employee_index.start_time', '<=', $end_time)
  225. ->where('employee_index.end_time', '>=', $start_time);
  226. }
  227. if(! empty($data['employee_id_2_title'])) $model->where('rc.employee_id_2_title', 'LIKE', '%'.$data['employee_id_2_title'].'%');
  228. return [true, $model];
  229. }
  230. public function statisticsProfit($data,$user){
  231. list($status, $model) = $this->statisticsProfitCommon($data, $user);
  232. if(! $status) return [false, $model];
  233. $list = $this->limit($model,'',$data);
  234. $list = $this->statisticsProfitFillData($list);
  235. return [true, $list];
  236. }
  237. public function statisticsProfitFillData($data){
  238. if(empty($data['data'])) return $data;
  239. //获取应发放金额需要的指标
  240. $indexes = $this->getEmployeeIndex($data['data']);
  241. //已发
  242. $indexes_give = $this->getEmployeeGiveOut($data['data']);
  243. foreach ($data['data'] as $key => $value){
  244. $start = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  245. $end = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
  246. $string = "";
  247. if(! empty($start) && ! empty($end)) $string = $start . "-" . $end;
  248. $value['check_time'] = $string;
  249. $value['index_' . EmployeeIndex::TYPE_ONE] = 0;
  250. $value['index_' . EmployeeIndex::TYPE_FIVE] = 0;
  251. //指标
  252. $this->makeIndex($indexes, $value, $data['data'][$key]);
  253. //应付
  254. $this->makeShouldPay($value,$data['data'][$key]);
  255. //已付
  256. $this->makePayed($indexes_give, $value,$data['data'][$key]);
  257. //未付
  258. $not_pay = bcsub($data['data'][$key]['should_pay'], $data['data'][$key]['payed'],2);
  259. $data['data'][$key]['not_pay'] = $not_pay;
  260. }
  261. return $data;
  262. }
  263. private function getEmployeeIndex($existingData)
  264. {
  265. if (empty($existingData)) {
  266. return collect();
  267. }
  268. // 取出所有涉及的 employee_id 和时间区间
  269. $employeeIds = array_column($existingData, 'employee_id_2');
  270. $start_time = array_column($existingData, 'start_time');
  271. $minStart = ! empty($start_time) ? min($start_time) : 0;
  272. $end_time = array_column($existingData, 'end_time');
  273. $maxEnd = ! empty($end_time) ? max($end_time) : 0;
  274. // 一次性查出这些员工在最大区间范围内的所有指标
  275. $results = EmployeeIndex::where('del_time', 0)
  276. ->whereIn('type', [EmployeeIndex::TYPE_ONE, EmployeeIndex::TYPE_FIVE])
  277. ->whereIn('employee_id', $employeeIds)
  278. ->where('start_time', '<=', $maxEnd)
  279. ->where('end_time', '>=', $minStart)
  280. ->select('start_time','end_time','employee_id','index','type')
  281. ->get();
  282. return $results;
  283. }
  284. private function getEmployeeGiveOut($existingData)
  285. {
  286. if (empty($existingData)) {
  287. return collect();
  288. }
  289. // 取出所有涉及的 employee_id 和时间区间
  290. $employeeIds = array_column($existingData, 'employee_id_2');
  291. $start_time = array_column($existingData, 'start_time');
  292. $minStart = ! empty($start_time) ? min($start_time) : 0;
  293. $end_time = array_column($existingData, 'end_time');
  294. $maxEnd = ! empty($end_time) ? max($end_time) : 0;
  295. // 一次性查出这些员工在最大区间范围内的所有指标
  296. $results = GiveOut::where('del_time', 0)
  297. ->whereIn('employee_id_1', $employeeIds)
  298. ->where('start_time', '<=', $maxEnd)
  299. ->where('end_time', '>=', $minStart)
  300. ->select('start_time','end_time','employee_id_1 as employee_id','give_out_amount')
  301. ->get();
  302. return $results;
  303. }
  304. private function makeIndex($indexes, $value, &$data){
  305. // 找到所有符合条件的 index(可能多个 type)
  306. $matchedIndexes = $indexes->filter(function ($item) use ($value) {
  307. return $item['employee_id'] == $value['employee_id_2']
  308. && $item['start_time'] <= $value['end_time']
  309. && $item['end_time'] >= $value['start_time'];
  310. });
  311. // 按 type 去重,只保留第一次出现的
  312. $uniqueByType = [];
  313. foreach ($matchedIndexes as $item) {
  314. $index = "index_" . $item['type'];
  315. if (! isset($uniqueByType[$index])) {
  316. $uniqueByType[$index] = $item['index'];
  317. }
  318. }
  319. $data = array_merge($value, $uniqueByType);
  320. }
  321. private function makeShouldPay($value, &$data){
  322. $index_1 = bcdiv($data['index_1'],100,2);
  323. $index_5 = bcdiv($data['index_5'],100,2);
  324. // 分红比例/2
  325. $a = bcdiv($index_5,2,2);
  326. // 回款金额*季度预支分红比例/2
  327. $b = bcdiv(bcmul($value['payment_amount'], $index_1),2,2);
  328. //(分红比例/2-回款金额*季度预支分红比例/2)
  329. $c = bcsub($a, $b,2);
  330. $data['part_left'] = $c;
  331. if($value['payment_amount'] < $value['index']){
  332. //回款金额*(分红比例/2-回款金额*季度预支分红比例/2);
  333. $shouldPay = bcmul($value['payment_amount'], $c,2);
  334. }else{
  335. //回款金额-季度指标金额
  336. $d = bcsub($value['payment_amount'], $value['index'],2);
  337. //(回款金额-季度指标金额)*15%
  338. $e = bcmul($d, 0.15,2);
  339. //(回款金额-季度指标金额)*15%/2
  340. $e = bcdiv($e,2,2);
  341. //(分红比例/2-回款金额*季度预支分红比例/2+(回款金额-季度指标金额)*15%/2)
  342. $rate = bcadd($c, $e,2);
  343. // $data['data'][$key]['rate'] = $rate;
  344. //回款金额*(分红比例/2-回款金额*季度预支分红比例/2+(回款金额-季度指标金额)*15%/2)
  345. $shouldPay = bcmul($value['payment_amount'], $rate,2);
  346. }
  347. $data['should_pay'] = $shouldPay;
  348. }
  349. private function makePayed($indexes, $value, &$data){
  350. $matchedIndexes_give = $indexes->filter(function ($item) use ($value) {
  351. return $item['employee_id'] == $value['employee_id_2']
  352. && $item['start_time'] <= $value['end_time']
  353. && $item['end_time'] >= $value['start_time'];
  354. });
  355. $give_out = 0;
  356. foreach ($matchedIndexes_give as $item) {
  357. $give_out = bcadd($item['give_out_amount'], $give_out,2);
  358. }
  359. $data['payed'] = $give_out;
  360. }
  361. public function statisticsEmployeeSalaryCommon($data,$user, $field = []){
  362. if(empty($field)) {
  363. $field = SalaryEmployee::$field;
  364. }
  365. $model = SalaryEmployee::where('del_time',0)
  366. ->where('order_type',RevenueCost::ORDER_THREE)
  367. ->select($field)
  368. ->orderby('order_time', 'desc');
  369. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
  370. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  371. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  372. $start_time = strtotime(date("Y-m-01",$start_time));
  373. $end_time = strtotime(date("Y-m-01",$end_time));
  374. $model->where('order_time', '>=', $start_time)
  375. ->where('order_time', '<=', $end_time);
  376. }
  377. if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
  378. return [true, $model];
  379. }
  380. public function statisticsEmployeeSalary($data,$user){
  381. list($status, $model) = $this->statisticsEmployeeSalaryCommon($data, $user);
  382. if(! $status) return [false, $model];
  383. $list = $this->limit($model,'',$data);
  384. $list = $this->statisticsEmployeeSalaryFillData($list);
  385. return [true, $list];
  386. }
  387. public function statisticsEmployeeSalaryFillData($data){
  388. if(empty($data['data'])) return $data;
  389. foreach ($data['data'] as $key => $value){
  390. $order_time = $value['order_time'] ? date('Y-m',$value['order_time']) : '';
  391. $data['data'][$key]['order_time'] = $order_time;
  392. }
  393. return $data;
  394. }
  395. }