StatisticsService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. return [true, $model];
  111. }
  112. public function statisticsRevenueCostThree($data,$user){
  113. list($status, $model) = $this->statisticsRevenueCostThreeCommon($data, $user);
  114. if(! $status) return [false, $model];
  115. $list = $this->limit($model,'',$data);
  116. $list = $this->statisticsRevenueCostThreeFillData($list);
  117. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  118. return [true, $list];
  119. }
  120. public function statisticsRevenueCostThreeFillData($data){
  121. if(empty($data['data'])) return $data;
  122. foreach ($data['data'] as $key => $value){
  123. $profit_rate = $value['payment_amount'] > 0 ? bcdiv($value['profit'], $value['payment_amount'],2) : 0;
  124. $data['data'][$key]['profit_rate'] = bcmul($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 statisticsRevenueCostThreeDetail($data,$user){
  130. if(empty($data['order_number'])) return [false, '回款单号不能为空'];
  131. $result = RevenueCost::where('del_time',0)
  132. ->where('order_type',RevenueCost::ORDER_THREE)
  133. ->where('order_number',$data['order_number'])
  134. ->select(RevenueCost::$field_hkd_detail)
  135. ->get()->toArray();
  136. if(empty($result)) return [false, "回款单不存在或已被删除"];
  137. $detail = [];
  138. foreach ($result as $value){
  139. $detail[] = [
  140. 'order_number' => $value['order_number_upstream'],
  141. 'customer_code' => $value['customer_code'],
  142. 'customer_title' => $value['customer_title'],
  143. 'product_code' => $value['product_code'],
  144. 'product_title' => $value['product_title'],
  145. 'product_size' => $value['product_size'],
  146. 'unit' => $value['unit'],
  147. 'quantity' => $value['quantity'],
  148. 'payment_amount' => $value['payment_amount'],
  149. 'price_1' => $value['price_1'],
  150. 'price_1_total' => $value['price_1_total'],
  151. 'price_4' => $value['price_4'],
  152. 'price_4_total' => $value['price_4_total'],
  153. 'profit' => $value['profit'],
  154. 'profit_rate' => bcmul($value['profit_rate'],100,2) . '%',
  155. 'customer_profit_rate' => bcmul($value['customer_profit_rate'],100,2) . '%',
  156. 'voucher_type_title' => RevenueCost::$voucher_type[$value['voucher_type']] ?? "",
  157. ];
  158. }
  159. $first = $result[0];
  160. $order = [
  161. 'order_number' => $data['order_number'],
  162. 'order_time' => date("Y-m-d",$first['order_time']),
  163. 'employee_id_1_title' => $first['employee_id_1_title'],
  164. 'detail' => $detail
  165. ];
  166. return [true, $order];
  167. }
  168. public function statisticsProfitCommon($data,$user, $field = []){
  169. if(empty($field)) {
  170. $field = RevenueCost::$field_hkd_profit_main;
  171. $field[] = DB::raw('COALESCE(SUM(rc.payment_amount), 0) AS payment_amount');
  172. }
  173. $type = RevenueCost::ORDER_THREE;
  174. $model = EmployeeIndex::Clear($user, $data)
  175. ->where('employee_index.del_time', 0)
  176. ->where('employee_index.type', EmployeeIndex::TYPE_THREE)
  177. ->leftJoin(DB::raw('revenue_cost as rc'), function ($join) use ($type) {
  178. $join->on('rc.employee_id_2', '=', 'employee_index.employee_id')
  179. ->where('rc.del_time', 0)
  180. ->where('rc.order_type', $type)
  181. ->whereRaw('rc.order_time >= employee_index.start_time')
  182. ->whereRaw('rc.order_time <= employee_index.end_time');
  183. })
  184. ->select($field)
  185. ->groupBy('employee_index.employee_id', 'employee_index.start_time', 'employee_index.end_time')
  186. ->orderBy('employee_index.end_time', 'desc');
  187. // $type = RevenueCost::ORDER_THREE;
  188. // $model = EmployeeIndex::where('employee_index.del_time',0)
  189. // ->where('employee_index.type',EmployeeIndex::TYPE_THREE)
  190. // ->leftJoin(DB::raw('revenue_cost as rc'), function ($join) use ($type) {
  191. // $join->on('rc.employee_id_2', '=', 'employee_index.employee_id')
  192. // ->where('rc.del_time', 0)
  193. // ->where('rc.order_type', $type)
  194. // ->whereRaw('rc.order_time >= employee_index.start_time')
  195. // ->whereRaw('rc.order_time <= employee_index.end_time');
  196. // })
  197. // ->select($field)
  198. // ->groupBy('employee_index.employee_id', 'employee_index.start_time', 'employee_index.end_time')
  199. // ->orderBy('employee_index.end_time','desc');
  200. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){;
  201. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  202. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  203. $model->where('employee_index.start_time', '<=', $end_time)
  204. ->where('employee_index.end_time', '>=', $start_time);
  205. }
  206. if(! empty($data['employee_id_2_title'])) $model->where('rc.employee_id_2_title', 'LIKE', '%'.$data['employee_id_2_title'].'%');
  207. return [true, $model];
  208. }
  209. public function statisticsProfit($data,$user){
  210. list($status, $model) = $this->statisticsProfitCommon($data, $user);
  211. if(! $status) return [false, $model];
  212. $list = $this->limit($model,'',$data);
  213. $list = $this->statisticsProfitFillData($list);
  214. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  215. return [true, $list];
  216. }
  217. public function statisticsProfitFillData($data){
  218. if(empty($data['data'])) return $data;
  219. //获取应发放金额需要的指标
  220. $indexes = $this->getEmployeeIndex($data['data']);
  221. //已发
  222. $indexes_give = $this->getEmployeeGiveOut($data['data']);
  223. foreach ($data['data'] as $key => $value){
  224. $start = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  225. $end = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
  226. $string = "";
  227. if(! empty($start) && ! empty($end)) $string = $start . "-" . $end;
  228. $value['check_time'] = $string;
  229. $value['index_' . EmployeeIndex::TYPE_ONE] = 0;
  230. $value['index_' . EmployeeIndex::TYPE_FIVE] = 0;
  231. //指标
  232. $this->makeIndex($indexes, $value, $data['data'][$key]);
  233. //应付
  234. $this->makeShouldPay($value,$data['data'][$key]);
  235. //已付
  236. $this->makePayed($indexes_give, $value,$data['data'][$key]);
  237. //未付
  238. $not_pay = bcsub($data['data'][$key]['should_pay'], $data['data'][$key]['payed'],2);
  239. $data['data'][$key]['not_pay'] = $not_pay;
  240. }
  241. return $data;
  242. }
  243. private function getEmployeeIndex($existingData)
  244. {
  245. if (empty($existingData)) {
  246. return collect();
  247. }
  248. // 取出所有涉及的 employee_id 和时间区间
  249. $employeeIds = array_column($existingData, 'employee_id_2');
  250. $start_time = array_column($existingData, 'start_time');
  251. $minStart = ! empty($start_time) ? min($start_time) : 0;
  252. $end_time = array_column($existingData, 'end_time');
  253. $maxEnd = ! empty($end_time) ? max($end_time) : 0;
  254. // 一次性查出这些员工在最大区间范围内的所有指标
  255. $results = EmployeeIndex::where('del_time', 0)
  256. ->whereIn('type', [EmployeeIndex::TYPE_ONE, EmployeeIndex::TYPE_FIVE])
  257. ->whereIn('employee_id', $employeeIds)
  258. ->where('start_time', '<=', $maxEnd)
  259. ->where('end_time', '>=', $minStart)
  260. ->select('start_time','end_time','employee_id','index','type')
  261. ->get();
  262. return $results;
  263. }
  264. private function getEmployeeGiveOut($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 = GiveOut::where('del_time', 0)
  277. ->whereIn('employee_id_1', $employeeIds)
  278. ->where('start_time', '<=', $maxEnd)
  279. ->where('end_time', '>=', $minStart)
  280. ->select('start_time','end_time','employee_id_1 as employee_id','give_out_amount')
  281. ->get();
  282. return $results;
  283. }
  284. private function makeIndex($indexes, $value, &$data){
  285. // 找到所有符合条件的 index(可能多个 type)
  286. $matchedIndexes = $indexes->filter(function ($item) use ($value) {
  287. return $item['employee_id'] == $value['employee_id_2']
  288. && $item['start_time'] <= $value['end_time']
  289. && $item['end_time'] >= $value['start_time'];
  290. });
  291. // 按 type 去重,只保留第一次出现的
  292. $uniqueByType = [];
  293. foreach ($matchedIndexes as $item) {
  294. $index = "index_" . $item['type'];
  295. if (! isset($uniqueByType[$index])) {
  296. $uniqueByType[$index] = $item['index'];
  297. }
  298. }
  299. $data = array_merge($value, $uniqueByType);
  300. }
  301. private function makeShouldPay($value, &$data){
  302. $index_1 = bcdiv($data['index_1'],100,2);
  303. $index_5 = bcdiv($data['index_5'],100,2);
  304. // 分红比例/2
  305. $a = bcdiv($index_5,2,2);
  306. // 季度预支分红比例/2
  307. $b = bcdiv($index_1,2,2);
  308. //(分红比例/2-季度预支分红比例/2)
  309. $c = bcsub($a, $b,2);
  310. $data['part_left'] = $c;
  311. if($value['payment_amount'] < $value['index']){
  312. //回款金额*(分红比例/2-季度预支分红比例/2);
  313. $shouldPay = bcmul($value['payment_amount'], $c,2);
  314. }else{
  315. //回款金额-季度指标金额
  316. $d = bcsub($value['payment_amount'], $value['index'],2);
  317. //(回款金额-季度指标金额)*15%
  318. $e = bcmul($d, 0.15,2);
  319. //(回款金额-季度指标金额)*15%/2
  320. $e = bcdiv($e,2,2);
  321. //(分红比例/2-季度预支分红比例/2+(回款金额-季度指标金额)*15%/2)
  322. $rate = bcadd($c, $e,2);
  323. // $data['data'][$key]['rate'] = $rate;
  324. //回款金额*(分红比例/2-回款金额*季度预支分红比例/2+(回款金额-季度指标金额)*15%/2)
  325. $shouldPay = bcmul($value['payment_amount'], $rate,2);
  326. }
  327. $data['should_pay'] = $shouldPay;
  328. }
  329. private function makePayed($indexes, $value, &$data){
  330. $matchedIndexes_give = $indexes->filter(function ($item) use ($value) {
  331. return $item['employee_id'] == $value['employee_id_2']
  332. && $item['start_time'] <= $value['end_time']
  333. && $item['end_time'] >= $value['start_time'];
  334. });
  335. $give_out = 0;
  336. foreach ($matchedIndexes_give as $item) {
  337. $give_out = bcadd($item['give_out_amount'], $give_out,2);
  338. }
  339. $data['payed'] = $give_out;
  340. }
  341. public function statisticsEmployeeSalaryCommon($data,$user, $field = []){
  342. if(empty($field)) {
  343. $field = SalaryEmployee::$field;
  344. }
  345. $model = SalaryEmployee::Clear($user,$data);
  346. $model = $model->where('del_time',0)
  347. ->where('order_type',RevenueCost::ORDER_THREE)
  348. ->select($field)
  349. ->orderby('order_time', 'desc');
  350. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
  351. $start_time = strtotime($data['order_time'][0] . "-01");
  352. $end_time = strtotime($data['order_time'][1] . "-01");
  353. $model->where('order_time', '>=', $start_time)
  354. ->where('order_time', '<=', $end_time);
  355. }
  356. if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
  357. return [true, $model];
  358. }
  359. public function statisticsEmployeeSalary($data,$user){
  360. list($status, $model) = $this->statisticsEmployeeSalaryCommon($data, $user);
  361. if(! $status) return [false, $model];
  362. $list = $this->limit($model,'',$data);
  363. $list = $this->statisticsEmployeeSalaryFillData($list);
  364. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  365. return [true, $list];
  366. }
  367. public function statisticsEmployeeSalaryFillData($data){
  368. if(empty($data['data'])) return $data;
  369. foreach ($data['data'] as $key => $value){
  370. $order_time = $value['order_time'] ? date('Y-m',$value['order_time']) : '';
  371. $data['data'][$key]['order_time'] = $order_time;
  372. }
  373. return $data;
  374. }
  375. public function statisticsFreightFeeCommon($data,$user, $field = []){
  376. if(empty($field)) {
  377. $field = FreightFee::$field;
  378. $field[] = DB::raw('GROUP_CONCAT(id) as result');
  379. }
  380. $model = FreightFee::Clear($user,$data);
  381. $model = $model->where('del_time',0)
  382. ->select($field)
  383. ->groupby('order_time')
  384. ->orderby('order_time', 'desc');
  385. if(! empty($data['order_time'][0]) && ! empty($data['order_time'][1])){
  386. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['order_time'],false);
  387. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "单据日期的区间无效"];
  388. $model->where('order_time', '>=', $start_time)
  389. ->where('order_time', '<=', $end_time);
  390. }
  391. if(! empty($data['employee_id_1_title'])) $model->where('employee_id_1_title', 'LIKE', '%'.$data['employee_id_1_title'].'%');
  392. return [true, $model];
  393. }
  394. public function statisticsFreightFee($data,$user){
  395. list($status, $model) = $this->statisticsFreightFeeCommon($data, $user);
  396. if(! $status) return [false, $model];
  397. $list = $this->limit($model,'',$data);
  398. $list = $this->statisticsFreightFeeFillData($list);
  399. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  400. return [true, $list];
  401. }
  402. public function statisticsFreightFeeFillData($data){
  403. if(empty($data['data'])) return $data;
  404. $id = implode(',', array_column($data['data'], 'result'));
  405. $idArr = explode(',', $id);
  406. $allFees = FreightFee::where('del_time', 0)
  407. ->whereIn('id', $idArr)
  408. ->get()
  409. ->toArray();
  410. $feesByDay = [];
  411. foreach ($allFees as $f) {
  412. $feesByDay[$f['order_time']][] = $f;
  413. }
  414. foreach ($data['data'] as $key => $value) {
  415. $fees = $feesByDay[$value['order_time']] ?? [];
  416. if (empty($fees)) {
  417. $data['data'][$key]['total_amount'] = 0;
  418. continue;
  419. }
  420. // 准备 time_amount(和详情逻辑一致)
  421. $time_amount = [];
  422. foreach ($fees as $f) {
  423. if ($f['business_type_id'] == FreightFee::businessTypeNormal) {
  424. $k = $f['order_time'] . $f['area_hs'];
  425. $time_amount[$k]['amount'] = isset($time_amount[$k]['amount'])
  426. ? bcadd($f['freight_amount'], $time_amount[$k]['amount'], 2)
  427. : $f['freight_amount'];
  428. $time_amount[$k]['is_use'] = $time_amount[$k]['is_use'] ?? 0;
  429. }
  430. }
  431. // 逐条算金额
  432. $valueTotal = 0;
  433. foreach ($fees as $f) {
  434. $f = $this->calculateFreightFeeRow($f, $time_amount);
  435. $valueTotal = bcadd($valueTotal, $f['total_amount'], 2);
  436. }
  437. $data['data'][$key]['total_amount'] = $valueTotal;
  438. $order_time = $value['order_time'] ? date('Y-m-d',$value['order_time']) : '';
  439. $data['data'][$key]['order_time_show'] = $order_time;
  440. unset($data['data'][$key]['result']);
  441. }
  442. return $data;
  443. }
  444. // public function statisticsFreightFeeDetail($data, $user){
  445. // if(empty($data['order_time'])) return [false, '单据日期不能为空'];
  446. //
  447. // $fee = FreightFee::where('del_time',0)
  448. // ->where('order_time', $data['order_time'])
  449. // ->get()->toArray();
  450. // if(empty($fee)) return [false, '单据日期下暂无数据'];
  451. //
  452. // //同一天 同一个地区 非退货的配送费总金额
  453. // $time_amount = [];
  454. // foreach ($fee as $value){
  455. // if($value['business_type_id'] == FreightFee::businessTypeNormal){
  456. // $key = $value['order_time'] . $value['area_hs'];
  457. // if(isset($time_amount[$key])){
  458. // $freight_amount = bcadd($value['freight_amount'], $time_amount[$key]['amount'],2);
  459. // $time_amount[$key]['amount'] = $freight_amount;
  460. // }else{
  461. // $time_amount[$key] = [
  462. // 'is_use' => 0,
  463. // 'amount' => $value['freight_amount'],
  464. // ];
  465. // }
  466. // }
  467. // }
  468. //
  469. // $return = [];
  470. // foreach ($fee as $value){
  471. // //结算金额
  472. // $js_amount = 0;
  473. // $key = $value['order_time'] . $value['area_hs'];
  474. // if($value['business_type_id'] == FreightFee::businessTypeNormal){
  475. // $tmp_total_arr = $time_amount[$key];
  476. // if($tmp_total_arr['amount'] < $value['min_freight_amount']){
  477. // if(! $time_amount[$key]['is_use']){
  478. // $js_amount = $value['min_freight_amount'];
  479. // $time_amount[$key]['is_use'] = 1;
  480. // }
  481. // }else{
  482. // $js_amount = $value['js_single_amount'];
  483. // }
  484. // $value['js_amount'] = $js_amount;
  485. // //加急费
  486. // $jj_fee = 0;
  487. // if($value['delivery_mode'] == FreightFee::deliveryModeRightNow) $jj_fee = bcmul($js_amount, 0.5,2);
  488. // $value['jj_fee'] = $jj_fee;
  489. // //总金额
  490. // $total_amount = bcadd($js_amount,$value['customer_store_zx_fee'],2);
  491. // $total_amount = bcadd($total_amount,$value['sl_fee'],2);
  492. // $total_amount = bcadd($total_amount,$jj_fee,2);
  493. // $total_amount = bcadd($total_amount,$value['dh_fee'],2);
  494. // $value['total_amount'] = $total_amount;
  495. // $return[FreightFee::businessTypeNormal][$key][] = $value;
  496. // }else{
  497. // $js_amount = $value['js_single_amount'];
  498. // $value['js_amount'] = $js_amount;
  499. // $jj_fee = 0;
  500. // if($value['delivery_mode'] == FreightFee::deliveryModeRightNow) $jj_fee = bcmul($js_amount, 0.5,2);
  501. // //加急费
  502. // $value['jj_fee'] = $jj_fee;
  503. // //总金额
  504. // $total_amount = bcadd($js_amount,$value['customer_store_zx_fee'],2);
  505. // $total_amount = bcadd($total_amount,$value['sl_fee'],2);
  506. // $total_amount = bcadd($total_amount,$jj_fee,2);
  507. // $total_amount = bcadd($total_amount,$value['dh_fee'],2);
  508. // $value['total_amount'] = $total_amount;
  509. // $return[FreightFee::businessTypeReturn][$key][] = $value;
  510. // }
  511. // }
  512. //
  513. // $result = [];
  514. // foreach ($return as $value){
  515. // foreach (array_values($value) as $val){
  516. // $result[] = $val;
  517. // }
  518. // }
  519. // dd($result);
  520. // }
  521. public function statisticsFreightFeeDetail($data, $user)
  522. {
  523. if (empty($data['order_time'])) {
  524. return [false, '单据日期不能为空'];
  525. }
  526. $model = FreightFee::Clear($user,$data);
  527. $fee = $model->where('del_time', 0)
  528. ->where('order_time', $data['order_time'])
  529. ->get()
  530. ->toArray();
  531. if (empty($fee)) {
  532. return [false, '单据日期下暂无数据'];
  533. }
  534. // 按时间+地区聚合同一天、同地区的非退货配送费
  535. $time_amount = [];
  536. foreach ($fee as $value) {
  537. if ($value['business_type_id'] == FreightFee::businessTypeNormal) {
  538. $key = $value['order_time'] . $value['area_hs'];
  539. $time_amount[$key]['amount'] = isset($time_amount[$key]['amount'])
  540. ? bcadd($value['freight_amount'], $time_amount[$key]['amount'], 2)
  541. : $value['freight_amount'];
  542. $time_amount[$key]['is_use'] = $time_amount[$key]['is_use'] ?? 0;
  543. }
  544. }
  545. $return = [];
  546. foreach ($fee as $value) {
  547. $value = $this->calculateFreightFeeRow($value, $time_amount);
  548. $key = $value['order_time'] . $value['area_hs'];
  549. $return[$value['business_type_id']][$key][] = $value;
  550. }
  551. $result = [];
  552. // 需要合计的字段
  553. $sumFields = ['xs', 'weight', 'freight_amount', 'js_amount', 'customer_store_zx_fee', 'jj_fee', 'dh_fee', 'total_amount'];
  554. foreach ($return as $group) {
  555. foreach ($group as $val) {
  556. // 计算合计
  557. $sumRow = [];
  558. foreach ($val as $row) {
  559. foreach ($sumFields as $field) {
  560. $sumRow[$field] = isset($sumRow[$field])
  561. ? bcadd($sumRow[$field], $row[$field], 2)
  562. : $row[$field];
  563. }
  564. }
  565. // 末行合计行(非合计字段设为空字符串)
  566. $footer = [];
  567. foreach ($val[0] as $field => $v) {
  568. $footer[$field] = in_array($field, $sumFields) ? $sumRow[$field] : '';
  569. }
  570. $val[] = $footer;
  571. $result[] = $val;
  572. }
  573. }
  574. return [true, $result];
  575. }
  576. protected function calculateFreightFeeRow(array $value, array &$time_amount)
  577. {
  578. $key = $value['order_time'] . $value['area_hs'];
  579. $js_amount = 0;
  580. if ($value['business_type_id'] == FreightFee::businessTypeNormal) {
  581. $tmp_total_arr = $time_amount[$key];
  582. // 判断是否使用最低运费
  583. if ($tmp_total_arr['amount'] < $value['min_freight_amount']) {
  584. if (!$time_amount[$key]['is_use']) {
  585. $js_amount = $value['min_freight_amount'];
  586. $time_amount[$key]['is_use'] = 1;
  587. }
  588. } else {
  589. $js_amount = $value['js_single_amount'];
  590. }
  591. } else {
  592. $js_amount = $value['js_single_amount'];
  593. }
  594. $value['js_amount'] = $js_amount;
  595. // 加急费
  596. $jj_fee = 0;
  597. if ($value['delivery_mode'] == FreightFee::deliveryModeRightNow) {
  598. $jj_fee = bcmul($js_amount, 0.5, 2);
  599. }
  600. $value['jj_fee'] = $jj_fee;
  601. // 总金额
  602. $total_amount = bcadd($js_amount, $value['customer_store_zx_fee'], 2);
  603. $total_amount = bcadd($total_amount, $value['sl_fee'], 2);
  604. $total_amount = bcadd($total_amount, $jj_fee, 2);
  605. $total_amount = bcadd($total_amount, $value['dh_fee'], 2);
  606. $value['total_amount'] = $total_amount;
  607. return $value;
  608. }
  609. }