StatisticsService.php 30 KB

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