StatisticsService.php 19 KB

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