StatisticsService.php 18 KB

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