StatisticsService.php 30 KB

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