StatisticsService.php 30 KB

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