StatisticsService.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. <?php
  2. namespace App\Service;
  3. use App\Model\EmployeeIndex;
  4. use App\Model\FreightFee;
  5. use App\Model\GiveOut;
  6. use App\Model\RevenueCost;
  7. use App\Model\RevenueCostTotal;
  8. use App\Model\SalaryEmployee;
  9. use Illuminate\Support\Facades\DB;
  10. class StatisticsService extends Service
  11. {
  12. public function statisticsRevenueCostCommon($data,$user, $field = []){
  13. if(empty($field)) $field = RevenueCostTotal::$field;
  14. $model = RevenueCostTotal::Clear($user,$data);
  15. $model = $model->where('del_time',0)
  16. ->select($field)
  17. ->orderby('id', 'desc');
  18. if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
  19. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
  20. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  21. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  22. $model->where('order_time', '>=', $start_time)
  23. ->where('order_time', '<', $end_time);
  24. }
  25. if(! empty($data['order_type'])) $model->where('order_type',$data['order_type']);
  26. return [true, $model];
  27. }
  28. public function statisticsRevenueCost($data,$user){
  29. list($status, $model) = $this->statisticsRevenueCostCommon($data, $user);
  30. if(! $status) return [false, $model];
  31. $list = $this->limit($model,'',$data);
  32. $list = $this->statisticsRevenueCostFillData($list);
  33. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  34. return [true, $list];
  35. }
  36. public function statisticsRevenueCostFillData($data){
  37. if(empty($data['data'])) return $data;
  38. foreach ($data['data'] as $key => $value){
  39. $time = date("Y-m", $value['order_time']);
  40. $data['data'][$key]['order_time'] = $time;
  41. $data['data'][$key]['order_type_title'] = RevenueCost::$order_type[$value['order_type']] ?? "";
  42. $data['data'][$key]['profit_rate'] = bcmul($value['profit_rate'], 100,2);
  43. }
  44. return $data;
  45. }
  46. public function statisticsRevenueCostOneAndTwoCommon($data,$user, $field = []){
  47. if(empty($data['order_type']) || ! isset(RevenueCost::$order_type[$data['order_type']])) return [false, '单据类型不存在或错误'];
  48. if(empty($field)) {
  49. if($data['order_type'] == 1){
  50. $field = RevenueCost::$field_xhd;
  51. }else{
  52. $field = RevenueCost::$field_xsfp;
  53. }
  54. }
  55. $model = RevenueCost::Clear($user,$data);
  56. $model = $model->where('del_time',0)
  57. ->where('order_type',$data['order_type'])
  58. ->select($field)
  59. ->orderby('id', 'desc');
  60. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
  61. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  62. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  63. $model->where('order_time', '>=', $start_time)
  64. ->where('order_time', '<=', $end_time);
  65. }
  66. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  67. if(! empty($data['customer_title'])) $model->where('customer_title', 'LIKE', '%'.$data['customer_title'].'%');
  68. if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
  69. return [true, $model];
  70. }
  71. public function statisticsRevenueCostOneAndTwo($data,$user){
  72. list($status, $model) = $this->statisticsRevenueCostOneAndTwoCommon($data, $user);
  73. if(! $status) return [false, $model];
  74. $list = $this->limit($model,'',$data);
  75. $list = $this->statisticsRevenueCostOneAndTwoFillData($list);
  76. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  77. return [true, $list];
  78. }
  79. public function statisticsRevenueCostOneAndTwoFillData($data){
  80. if(empty($data['data'])) return $data;
  81. foreach ($data['data'] as $key => $value){
  82. $data['data'][$key]['profit_rate'] = bcmul($value['profit_rate'], 100,2);
  83. $data['data'][$key]['order_time'] = $value['order_time'] ? date("Y-m-d", $value['order_time']) : "";
  84. }
  85. return $data;
  86. }
  87. public function statisticsRevenueCostThreeCommon($data,$user, $field = []){
  88. if(empty($field)) {
  89. $field = RevenueCost::$field_hkd_salary_main;
  90. $field[] = DB::raw('sum(payment_amount) as payment_amount');
  91. $field[] = DB::raw('sum(price_4_total) as price_4_total');
  92. $field[] = DB::raw('sum(profit) as profit');
  93. $field[] = DB::raw('max(employee_id_2) as employee_id_2');
  94. $field[] = DB::raw("MAX(COALESCE(employee_id_2_title, '')) as employee_id_2_title");
  95. }
  96. $model = RevenueCost::Clear($user,$data);
  97. $model = $model->where('del_time',0)
  98. ->where('order_type',RevenueCost::ORDER_THREE)
  99. ->select($field)
  100. ->groupby('order_id')
  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['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
  110. if(! empty($data['employee_id_1'])) $model->where('employee_id_1', 'LIKE', $data['employee_id_1']);
  111. if(! empty($data['employee_id_2'])) $model->where('employee_id_2', 'LIKE', $data['employee_id_2']);
  112. if(! empty($data['employee_id_2_title'])) $model->where('employee_id_2_title', 'LIKE', '%'.$data['employee_id_2_title'].'%');
  113. if(! empty($data['channel_finance_array'])) {
  114. $array = explode("\n", str_replace(["\r\n", "\r"], "\n", $data['channel_finance_array']));
  115. $model->whereIn('channel_finance', $array);
  116. }
  117. return [true, $model];
  118. }
  119. public function statisticsRevenueCostThree($data,$user){
  120. list($status, $model) = $this->statisticsRevenueCostThreeCommon($data, $user);
  121. if(! $status) return [false, $model];
  122. $list = $this->limit($model,'',$data);
  123. $list = $this->statisticsRevenueCostThreeFillData($list);
  124. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  125. return [true, $list];
  126. }
  127. public function statisticsRevenueCostThreeFillData($data){
  128. if(empty($data['data'])) return $data;
  129. foreach ($data['data'] as $key => $value){
  130. $profit_rate = $value['payment_amount'] > 0 ? bcdiv($value['profit'], $value['payment_amount'],2) : 0;
  131. $data['data'][$key]['profit_rate'] = bcmul($profit_rate, 100,2);
  132. $data['data'][$key]['order_time'] = $value['order_time'] ? date("Y-m-d", $value['order_time']) : "";
  133. }
  134. return $data;
  135. }
  136. public function statisticsRevenueCostThreeDetail($data,$user){
  137. if(empty($data['order_number'])) return [false, '回款单号不能为空'];
  138. $result = RevenueCost::where('del_time',0)
  139. ->where('order_type',RevenueCost::ORDER_THREE)
  140. ->where('order_number',$data['order_number'])
  141. ->select(RevenueCost::$field_hkd_detail)
  142. ->get()->toArray();
  143. if(empty($result)) return [false, "回款单不存在或已被删除"];
  144. $detail = [];
  145. foreach ($result as $value){
  146. $detail[] = [
  147. 'order_number' => $value['order_number_upstream'],
  148. 'customer_code' => $value['customer_code'],
  149. 'customer_title' => $value['customer_title'],
  150. 'channel_finance' => $value['channel_finance'],
  151. 'product_code' => $value['product_code'],
  152. 'product_title' => $value['product_title'],
  153. 'product_size' => $value['product_size'],
  154. 'unit' => $value['unit'],
  155. 'quantity' => $value['quantity'],
  156. 'payment_amount' => $value['payment_amount'],
  157. 'price_1' => $value['price_1'],
  158. 'price_1_total' => $value['price_1_total'],
  159. 'price_4' => $value['price_4'],
  160. 'price_4_total' => $value['price_4_total'],
  161. 'profit' => $value['profit'],
  162. 'profit_rate' => bcmul($value['profit_rate'],100,2) . '%',
  163. 'customer_profit_rate' => bcmul($value['customer_profit_rate'],100,2) . '%',
  164. 'voucher_type_title' => RevenueCost::$voucher_type[$value['voucher_type']] ?? "",
  165. ];
  166. }
  167. $footer = $this->countTotal($result, $user['header_default']);
  168. $footer['profit_rate'] = $footer['profit_rate'] . "%";
  169. $footer = array_merge( [
  170. 'order_number' => '',
  171. 'customer_code' => '',
  172. 'customer_title' => '',
  173. 'channel_finance' => '',
  174. 'product_code' => '',
  175. 'product_title' => '',
  176. 'product_size' => '',
  177. 'unit' => '',
  178. 'quantity' => '',
  179. 'payment_amount' => '',
  180. 'price_1' => '',
  181. 'price_1_total' => '',
  182. 'price_4' => '',
  183. 'price_4_total' => '',
  184. 'profit' => '',
  185. 'profit_rate' => '',
  186. 'customer_profit_rate'=> '',
  187. 'voucher_type_title' => '',
  188. ], $footer);
  189. $detail[] = $footer;
  190. $first = $result[0];
  191. $order = [
  192. 'order_number' => $data['order_number'],
  193. 'order_time' => date("Y-m-d",$first['order_time']),
  194. 'employee_id_1_title' => $first['employee_id_1_title'],
  195. 'detail' => $detail
  196. ];
  197. return [true, $order];
  198. }
  199. public function statisticsProfitCommon($data,$user, $field = []){
  200. if(empty($field)) {
  201. $field = RevenueCost::$field_hkd_profit_main;
  202. $field[] = DB::raw('COALESCE(SUM(rc.payment_amount), 0) AS payment_amount');
  203. }
  204. $type = RevenueCost::ORDER_THREE;
  205. $model = EmployeeIndex::Clear($user, $data)
  206. ->where('employee_index.del_time', 0)
  207. ->where('employee_index.type', EmployeeIndex::TYPE_THREE)
  208. ->leftJoin(DB::raw('revenue_cost as rc'), function ($join) use ($type) {
  209. $join->on('rc.employee_id_2', '=', 'employee_index.employee_id')
  210. ->where('rc.del_time', 0)
  211. ->where('rc.order_type', $type)
  212. ->whereNotNull('rc.employee_id_2')
  213. ->where('rc.employee_id_2', '<>', '')
  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. // $type = RevenueCost::ORDER_THREE;
  221. // $model = EmployeeIndex::where('employee_index.del_time',0)
  222. // ->where('employee_index.type',EmployeeIndex::TYPE_THREE)
  223. // ->leftJoin(DB::raw('revenue_cost as rc'), function ($join) use ($type) {
  224. // $join->on('rc.employee_id_2', '=', 'employee_index.employee_id')
  225. // ->where('rc.del_time', 0)
  226. // ->where('rc.order_type', $type)
  227. // ->whereRaw('rc.order_time >= employee_index.start_time')
  228. // ->whereRaw('rc.order_time <= employee_index.end_time');
  229. // })
  230. // ->select($field)
  231. // ->groupBy('employee_index.employee_id', 'employee_index.start_time', 'employee_index.end_time')
  232. // ->orderBy('employee_index.end_time','desc');
  233. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){;
  234. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  235. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  236. $model->where('employee_index.start_time', '<=', $end_time)
  237. ->where('employee_index.end_time', '>=', $start_time);
  238. }
  239. if(! empty($data['employee_id_2_title'])) $model->where('rc.employee_id_2_title', 'LIKE', '%'.$data['employee_id_2_title'].'%');
  240. return [true, $model];
  241. }
  242. public function statisticsProfit($data,$user){
  243. list($status, $model) = $this->statisticsProfitCommon($data, $user);
  244. if(! $status) return [false, $model];
  245. $list = $this->limit($model,'',$data);
  246. $list = $this->statisticsProfitFillData($list);
  247. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  248. return [true, $list];
  249. }
  250. public function statisticsProfitFillData($data){
  251. if(empty($data['data'])) return $data;
  252. //获取应发放金额需要的指标
  253. $indexes = $this->getEmployeeIndex($data['data']);
  254. //已发
  255. $indexes_give = $this->getEmployeeGiveOut($data['data']);
  256. foreach ($data['data'] as $key => $value){
  257. $start = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  258. $end = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
  259. $string = "";
  260. if(! empty($start) && ! empty($end)) $string = $start . "-" . $end;
  261. $value['check_time'] = $string;
  262. $value['index_' . EmployeeIndex::TYPE_ONE] = 0;
  263. $value['index_' . EmployeeIndex::TYPE_FIVE] = 0;
  264. //指标
  265. $this->makeIndex($indexes, $value, $data['data'][$key]);
  266. //应付
  267. $this->makeShouldPay($value,$data['data'][$key]);
  268. //已付
  269. $this->makePayed($indexes_give, $value,$data['data'][$key]);
  270. //未付
  271. $not_pay = bcsub($data['data'][$key]['should_pay'], $data['data'][$key]['payed'],2);
  272. $data['data'][$key]['not_pay'] = $not_pay;
  273. }
  274. return $data;
  275. }
  276. private function getEmployeeIndex($existingData)
  277. {
  278. if (empty($existingData)) {
  279. return collect();
  280. }
  281. // 取出所有涉及的 employee_id 和时间区间
  282. $employeeIds = array_column($existingData, 'employee_id_2');
  283. $start_time = array_column($existingData, 'start_time');
  284. $minStart = ! empty($start_time) ? min($start_time) : 0;
  285. $end_time = array_column($existingData, 'end_time');
  286. $maxEnd = ! empty($end_time) ? max($end_time) : 0;
  287. // 一次性查出这些员工在最大区间范围内的所有指标
  288. $results = EmployeeIndex::where('del_time', 0)
  289. ->whereIn('type', [EmployeeIndex::TYPE_ONE, EmployeeIndex::TYPE_FIVE])
  290. ->whereIn('employee_id', $employeeIds)
  291. ->where('start_time', '<=', $maxEnd)
  292. ->where('end_time', '>=', $minStart)
  293. ->select('start_time','end_time','employee_id','index','type')
  294. ->get();
  295. return $results;
  296. }
  297. private function getEmployeeGiveOut($existingData)
  298. {
  299. if (empty($existingData)) {
  300. return collect();
  301. }
  302. // 取出所有涉及的 employee_id 和时间区间
  303. $employeeIds = array_column($existingData, 'employee_id_2');
  304. $start_time = array_column($existingData, 'start_time');
  305. $minStart = ! empty($start_time) ? min($start_time) : 0;
  306. $end_time = array_column($existingData, 'end_time');
  307. $maxEnd = ! empty($end_time) ? max($end_time) : 0;
  308. // 一次性查出这些员工在最大区间范围内的所有指标
  309. $results = GiveOut::where('del_time', 0)
  310. ->whereIn('employee_id_1', $employeeIds)
  311. ->where('start_time', '<=', $maxEnd)
  312. ->where('end_time', '>=', $minStart)
  313. ->select('start_time','end_time','employee_id_1 as employee_id','give_out_amount')
  314. ->get();
  315. return $results;
  316. }
  317. private function makeIndex($indexes, $value, &$data){
  318. // 找到所有符合条件的 index(可能多个 type)
  319. $matchedIndexes = $indexes->filter(function ($item) use ($value) {
  320. return $item['employee_id'] == $value['employee_id_2']
  321. && $item['start_time'] <= $value['end_time']
  322. && $item['end_time'] >= $value['start_time'];
  323. });
  324. // 按 type 去重,只保留第一次出现的
  325. $uniqueByType = [];
  326. foreach ($matchedIndexes as $item) {
  327. $index = "index_" . $item['type'];
  328. if (! isset($uniqueByType[$index])) {
  329. $uniqueByType[$index] = $item['index'];
  330. }
  331. }
  332. $data = array_merge($value, $uniqueByType);
  333. }
  334. private function makeShouldPay($value, &$data){
  335. $index_1 = bcdiv($data['index_1'],100,2);
  336. $index_5 = bcdiv($data['index_5'],100,2);
  337. // 分红比例/2
  338. $a = bcdiv($index_5,2,2);
  339. // 季度预支分红比例/2
  340. $b = bcdiv($index_1,2,2);
  341. //(分红比例/2-季度预支分红比例/2)
  342. $c = bcsub($a, $b,2);
  343. $data['part_left'] = $c;
  344. if($value['payment_amount'] < $value['index']){
  345. //回款金额*(分红比例/2-季度预支分红比例/2);
  346. $shouldPay = bcmul($value['payment_amount'], $c,2);
  347. }else{
  348. //回款金额-季度指标金额
  349. $d = bcsub($value['payment_amount'], $value['index'],2);
  350. //(回款金额-季度指标金额)*15%
  351. $e = bcmul($d, 0.15,2);
  352. //(回款金额-季度指标金额)*15%/2
  353. $e = bcdiv($e,2,2);
  354. //(分红比例/2-季度预支分红比例/2+(回款金额-季度指标金额)*15%/2)
  355. $rate = bcadd($c, $e,2);
  356. // $data['data'][$key]['rate'] = $rate;
  357. //回款金额*(分红比例/2-回款金额*季度预支分红比例/2+(回款金额-季度指标金额)*15%/2)
  358. $shouldPay = bcmul($value['payment_amount'], $rate,2);
  359. }
  360. $data['should_pay'] = $shouldPay;
  361. }
  362. private function makePayed($indexes, $value, &$data){
  363. $matchedIndexes_give = $indexes->filter(function ($item) use ($value) {
  364. return $item['employee_id'] == $value['employee_id_2']
  365. && $item['start_time'] <= $value['end_time']
  366. && $item['end_time'] >= $value['start_time'];
  367. });
  368. $give_out = 0;
  369. foreach ($matchedIndexes_give as $item) {
  370. $give_out = bcadd($item['give_out_amount'], $give_out,2);
  371. }
  372. $data['payed'] = $give_out;
  373. }
  374. public function statisticsEmployeeSalaryCommon($data,$user, $field = []){
  375. if(empty($field)) {
  376. $field = SalaryEmployee::$field;
  377. }
  378. $model = SalaryEmployee::Clear($user,$data);
  379. $model = $model->where('del_time',0)
  380. ->where('order_type',RevenueCost::ORDER_THREE)
  381. ->select($field)
  382. ->orderby('order_time', 'desc');
  383. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
  384. $start_time = strtotime($data['order_time'][0] . "-01");
  385. $end_time = strtotime($data['order_time'][1] . "-01");
  386. $model->where('order_time', '>=', $start_time)
  387. ->where('order_time', '<=', $end_time);
  388. }
  389. if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
  390. return [true, $model];
  391. }
  392. public function statisticsEmployeeSalary($data,$user){
  393. list($status, $model) = $this->statisticsEmployeeSalaryCommon($data, $user);
  394. if(! $status) return [false, $model];
  395. $list = $this->limit($model,'',$data);
  396. $list = $this->statisticsEmployeeSalaryFillData($list);
  397. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  398. return [true, $list];
  399. }
  400. public function statisticsEmployeeSalaryFillData($data){
  401. if(empty($data['data'])) return $data;
  402. foreach ($data['data'] as $key => $value){
  403. $order_time = $value['order_time'] ? date('Y-m',$value['order_time']) : '';
  404. $data['data'][$key]['order_time'] = $order_time;
  405. }
  406. return $data;
  407. }
  408. public function statisticsFreightFeeCommon($data,$user, $field = []){
  409. if(empty($field)) {
  410. $field = FreightFee::$field;
  411. }
  412. $model = FreightFee::Clear($user,$data);
  413. $model = $model->where('del_time',0)
  414. ->select($field)
  415. ->groupby('order_time')
  416. ->orderby('order_time', 'desc');
  417. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
  418. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  419. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  420. $model->where('order_time', '>=', $start_time)
  421. ->where('order_time', '<=', $end_time);
  422. }
  423. if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
  424. return [true, $model];
  425. }
  426. public function statisticsFreightFee($data,$user){
  427. list($status, $model) = $this->statisticsFreightFeeCommon($data, $user);
  428. if(! $status) return [false, $model];
  429. $list = $this->limit($model,'',$data);
  430. $list = $this->statisticsFreightFeeFillData($list, $user,$data);
  431. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  432. return [true, $list];
  433. }
  434. public function statisticsFreightFeeFillData($data, $user, $ergs){
  435. if(empty($data['data'])) return $data;
  436. $order_time = array_column($data['data'], 'order_time');
  437. $model = FreightFee::Clear($user,$ergs);
  438. $allFees = $model->where('del_time', 0)
  439. ->whereIn('order_time', $order_time)
  440. ->get()
  441. ->toArray();
  442. $feesByDay = [];
  443. foreach ($allFees as $f) {
  444. $feesByDay[$f['order_time']][] = $f;
  445. }
  446. foreach ($data['data'] as $key => $value) {
  447. $fees = $feesByDay[$value['order_time']] ?? [];
  448. if (empty($fees)) {
  449. $data['data'][$key]['total_amount'] = 0;
  450. continue;
  451. }
  452. // 准备 time_amount(和详情逻辑一致)
  453. $time_amount = [];
  454. foreach ($fees as $f) {
  455. if ($f['business_type_id'] == FreightFee::businessTypeNormal) {
  456. $k = $f['order_time'] . $f['area_hs'];
  457. $time_amount[$k]['amount'] = isset($time_amount[$k]['amount'])
  458. ? bcadd($f['freight_amount'], $time_amount[$k]['amount'], 2)
  459. : $f['freight_amount'];
  460. $time_amount[$k]['is_use'] = $time_amount[$k]['is_use'] ?? 0;
  461. }
  462. }
  463. // 逐条算金额
  464. $valueTotal = 0;
  465. foreach ($fees as $f) {
  466. $f = $this->calculateFreightFeeRow($f, $time_amount);
  467. $valueTotal = bcadd($valueTotal, $f['total_amount'], 2);
  468. }
  469. $data['data'][$key]['total_amount'] = $valueTotal;
  470. $order_time = $value['order_time'] ? date('Y-m-d',$value['order_time']) : '';
  471. $data['data'][$key]['order_time_show'] = $order_time;
  472. $data['data'][$key]['id'] = $value['order_time'];
  473. }
  474. return $data;
  475. }
  476. // public function statisticsFreightFeeDetail($data, $user){
  477. // if(empty($data['order_time'])) return [false, '单据日期不能为空'];
  478. //
  479. // $fee = FreightFee::where('del_time',0)
  480. // ->where('order_time', $data['order_time'])
  481. // ->get()->toArray();
  482. // if(empty($fee)) return [false, '单据日期下暂无数据'];
  483. //
  484. // //同一天 同一个地区 非退货的配送费总金额
  485. // $time_amount = [];
  486. // foreach ($fee as $value){
  487. // if($value['business_type_id'] == FreightFee::businessTypeNormal){
  488. // $key = $value['order_time'] . $value['area_hs'];
  489. // if(isset($time_amount[$key])){
  490. // $freight_amount = bcadd($value['freight_amount'], $time_amount[$key]['amount'],2);
  491. // $time_amount[$key]['amount'] = $freight_amount;
  492. // }else{
  493. // $time_amount[$key] = [
  494. // 'is_use' => 0,
  495. // 'amount' => $value['freight_amount'],
  496. // ];
  497. // }
  498. // }
  499. // }
  500. //
  501. // $return = [];
  502. // foreach ($fee as $value){
  503. // //结算金额
  504. // $js_amount = 0;
  505. // $key = $value['order_time'] . $value['area_hs'];
  506. // if($value['business_type_id'] == FreightFee::businessTypeNormal){
  507. // $tmp_total_arr = $time_amount[$key];
  508. // if($tmp_total_arr['amount'] < $value['min_freight_amount']){
  509. // if(! $time_amount[$key]['is_use']){
  510. // $js_amount = $value['min_freight_amount'];
  511. // $time_amount[$key]['is_use'] = 1;
  512. // }
  513. // }else{
  514. // $js_amount = $value['js_single_amount'];
  515. // }
  516. // $value['js_amount'] = $js_amount;
  517. // //加急费
  518. // $jj_fee = 0;
  519. // if($value['delivery_mode'] == FreightFee::deliveryModeRightNow) $jj_fee = bcmul($js_amount, 0.5,2);
  520. // $value['jj_fee'] = $jj_fee;
  521. // //总金额
  522. // $total_amount = bcadd($js_amount,$value['customer_store_zx_fee'],2);
  523. // $total_amount = bcadd($total_amount,$value['sl_fee'],2);
  524. // $total_amount = bcadd($total_amount,$jj_fee,2);
  525. // $total_amount = bcadd($total_amount,$value['dh_fee'],2);
  526. // $value['total_amount'] = $total_amount;
  527. // $return[FreightFee::businessTypeNormal][$key][] = $value;
  528. // }else{
  529. // $js_amount = $value['js_single_amount'];
  530. // $value['js_amount'] = $js_amount;
  531. // $jj_fee = 0;
  532. // if($value['delivery_mode'] == FreightFee::deliveryModeRightNow) $jj_fee = bcmul($js_amount, 0.5,2);
  533. // //加急费
  534. // $value['jj_fee'] = $jj_fee;
  535. // //总金额
  536. // $total_amount = bcadd($js_amount,$value['customer_store_zx_fee'],2);
  537. // $total_amount = bcadd($total_amount,$value['sl_fee'],2);
  538. // $total_amount = bcadd($total_amount,$jj_fee,2);
  539. // $total_amount = bcadd($total_amount,$value['dh_fee'],2);
  540. // $value['total_amount'] = $total_amount;
  541. // $return[FreightFee::businessTypeReturn][$key][] = $value;
  542. // }
  543. // }
  544. //
  545. // $result = [];
  546. // foreach ($return as $value){
  547. // foreach (array_values($value) as $val){
  548. // $result[] = $val;
  549. // }
  550. // }
  551. // dd($result);
  552. // }
  553. public function statisticsFreightFeeDetail($data, $user)
  554. {
  555. if (empty($data['order_time'])) {
  556. return [false, '单据日期不能为空'];
  557. }
  558. $model = FreightFee::Clear($user,$data);
  559. $fee = $model->where('del_time', 0)
  560. ->where('order_time', $data['order_time'])
  561. ->get()
  562. ->toArray();
  563. if (empty($fee)) {
  564. return [false, '单据日期下暂无数据'];
  565. }
  566. // 按时间+地区聚合同一天、同地区的非退货配送费
  567. $time_amount = [];
  568. foreach ($fee as $value) {
  569. if ($value['business_type_id'] == FreightFee::businessTypeNormal) {
  570. $key = $value['order_time'] . $value['area_hs'];
  571. $time_amount[$key]['amount'] = isset($time_amount[$key]['amount'])
  572. ? bcadd($value['freight_amount'], $time_amount[$key]['amount'], 2)
  573. : $value['freight_amount'];
  574. $time_amount[$key]['is_use'] = $time_amount[$key]['is_use'] ?? 0;
  575. }
  576. }
  577. $return = [];
  578. foreach ($fee as $value) {
  579. $value = $this->calculateFreightFeeRow($value, $time_amount);
  580. $key = $value['order_time'] . '|' . $value['area_hs'];
  581. $return[$value['business_type_id']][$key][] = $value;
  582. }
  583. $result = [];
  584. // 需要合计的字段
  585. $sumFields = ['xs', 'weight', 'freight_amount', 'js_amount', 'customer_store_zx_fee', 'jj_fee', 'dh_fee', 'total_amount'];
  586. // 业务类型 日期 地区 算一张表算运费
  587. foreach ($return as $g => $group) {
  588. $g_title = FreightFee::$business[$g] ?? "";
  589. foreach ($group as $key => $val) {
  590. $tmp_key = explode('|', $key);
  591. $title = $tmp_key[1] . "(" . $g_title . ")";
  592. // 计算合计
  593. $sumRow = [];
  594. foreach ($val as $key => $row) {
  595. $order_time = $row['order_time'] ? date('Y-m-d',$row['order_time']) : '';
  596. $val[$key]['order_time_show'] = $order_time;
  597. $val[$key]['is_present_show'] = $row['is_present'] ? '是' : '否';
  598. $delivery_mode_show = FreightFee::$delivery[$row['delivery_mode']] ?? "";
  599. $val[$key]['delivery_mode_show'] = $delivery_mode_show;
  600. $val[$key]['area_range_show'] = $row['area_range'] == 1 ? '1~5吨' : '5吨以上';
  601. foreach ($sumFields as $field) {
  602. $sumRow[$field] = isset($sumRow[$field])
  603. ? bcadd($sumRow[$field], $row[$field], 2)
  604. : $row[$field];
  605. }
  606. }
  607. // 末行合计行(非合计字段设为空字符串)
  608. $footer = [];
  609. foreach ($val[0] as $field => $v) {
  610. $footer[$field] = in_array($field, $sumFields) ? $sumRow[$field] : '';
  611. }
  612. $val[] = $footer;
  613. $result[] = [
  614. 'key' => $title,
  615. 'list' => $val
  616. ];
  617. }
  618. }
  619. return [true, $result];
  620. }
  621. protected function calculateFreightFeeRow(array $value, array &$time_amount)
  622. {
  623. $key = $value['order_time'] . $value['area_hs'];
  624. $js_amount = 0;
  625. if ($value['business_type_id'] == FreightFee::businessTypeNormal) {
  626. $tmp_total_arr = $time_amount[$key];
  627. // 判断是否使用最低运费
  628. if ($tmp_total_arr['amount'] < $value['min_freight_amount']) {
  629. if (!$time_amount[$key]['is_use']) {
  630. $js_amount = $value['min_freight_amount'];
  631. $time_amount[$key]['is_use'] = 1;
  632. }
  633. } else {
  634. $js_amount = $value['js_single_amount'];
  635. }
  636. } else {
  637. $js_amount = $value['js_single_amount'];
  638. }
  639. $value['js_amount'] = $js_amount;
  640. // 加急费
  641. $jj_fee = 0;
  642. if ($value['delivery_mode'] == FreightFee::deliveryModeRightNow) {
  643. $jj_fee = bcmul($js_amount, 0.5, 2);
  644. }
  645. $value['jj_fee'] = $jj_fee;
  646. // 总金额
  647. $total_amount = bcadd($js_amount, $value['customer_store_zx_fee'], 2);
  648. $total_amount = bcadd($total_amount, $value['sl_fee'], 2);
  649. $total_amount = bcadd($total_amount, $jj_fee, 2);
  650. $total_amount = bcadd($total_amount, $value['dh_fee'], 2);
  651. $value['total_amount'] = $total_amount;
  652. return $value;
  653. }
  654. }