StatisticsService.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Customer;
  5. use App\Model\Depart;
  6. use App\Model\DepartIndex;
  7. use App\Model\InOutRecord;
  8. use App\Model\InvoiceOrder;
  9. use App\Model\InvoiceOrderInfo;
  10. use App\Model\LastJc;
  11. use App\Model\Product;
  12. use App\Model\ProductCategory;
  13. use App\Model\PurchaseOrder;
  14. use App\Model\PurchaseOrderInfo;
  15. use App\Model\ReturnExchangeOrder;
  16. use App\Model\ReturnExchangeOrderProductInfo;
  17. use App\Model\SalesOrder;
  18. use App\Model\SalesOrderProductInfo;
  19. use App\Model\Setting;
  20. use Illuminate\Support\Facades\DB;
  21. class StatisticsService extends Service
  22. {
  23. public function statisticsCustomer($data,$user){
  24. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择客资创建时间区间'];
  25. $model = Customer::Clear($user,$data);
  26. $model = $model->where('del_time',0)
  27. ->select('id','model_type','customer_from','top_depart_id','fp_top_depart_id')
  28. ->orderby('id', 'desc');
  29. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  30. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  31. $model->where('crt_time','>=',$return[0]);
  32. $model->where('crt_time','<=',$return[1]);
  33. }
  34. $list = $model->get()->toArray();
  35. $list1 = $this->fillStatisticsCustomer($list);
  36. $list2 = $this->fillStatisticsCustomer2($list);
  37. return [true, ['bt' => $list1, 'zz' => $list2]];
  38. }
  39. public function fillStatisticsCustomer($data){
  40. if(empty($data)) return $data;
  41. $customer_from = array_unique(array_column($data,'customer_from'));
  42. $basic_type = BasicType::where('del_time',0)
  43. ->whereIn('id',$customer_from)
  44. ->where('type',2)
  45. ->pluck('title','id')
  46. ->toArray();
  47. $return = [];
  48. foreach ($data as $value){
  49. $tmp = $basic_type[$value['customer_from']] ?? "";
  50. if(! $tmp) continue;
  51. if(isset($return[$tmp])){
  52. $return[$tmp]['total'] += 1;
  53. }else{
  54. $return[$tmp] = [
  55. 'total' => 1,
  56. 'title' => $tmp
  57. ];
  58. }
  59. }
  60. return array_values($return);
  61. }
  62. public function fillStatisticsCustomer2($data){
  63. if(empty($data)) return $data;
  64. $depart = Depart::where('del_time',0)
  65. ->where('parent_id',0)
  66. ->where('is_main',0)
  67. ->pluck('title','id')
  68. ->toArray();
  69. $return = [];
  70. foreach ($depart as $key => $value){
  71. $return[$key] = [
  72. 'title' => $value,
  73. 'total' => 0
  74. ];
  75. }
  76. foreach ($data as $value){
  77. if($value['top_depart_id'] == $value['fp_top_depart_id']){
  78. if(isset($return[$value['fp_top_depart_id']])) $return[$value['fp_top_depart_id']]['total'] += 1;
  79. }else{
  80. if($value['fp_top_depart_id'] > 0){
  81. if(isset($return[$value['fp_top_depart_id']])) $return[$value['fp_top_depart_id']]['total'] += 1;
  82. }
  83. if($value['top_depart_id'] > 0){
  84. if(isset($return[$value['top_depart_id']])) $return[$value['top_depart_id']]['total'] += 1;
  85. }
  86. }
  87. }
  88. $return = array_values($return);
  89. usort($return, function($a, $b) {
  90. return $b['total'] - $a['total'];
  91. });
  92. return $return;
  93. }
  94. //销售统计饼图 根据合同类型区分(例如线上合同、线下合同)
  95. public function statisticsBt($data,$user){
  96. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择销售订单时间区间'];
  97. $model = SalesOrder::where('del_time',0)
  98. ->select('model_type','contract_fee as total','top_depart_id');
  99. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  100. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  101. $model->where('crt_time','>=',$return[0]);
  102. $model->where('crt_time','<=',$return[1]);
  103. }
  104. $list = $model->get()->toArray();
  105. $list = $this->fillStatisticsBt($list);
  106. return [true, $list];
  107. }
  108. public function fillStatisticsBt($data){
  109. if(empty($data)) return $data;
  110. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  111. $bt_top_depart_id = $setting['setting_value'] ?? [];
  112. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  113. $return = [];
  114. foreach ($data as $value){
  115. $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  116. if($value['model_type'] != SalesOrder::Model_type_two){
  117. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  118. $this->makeThis($return,$value,$model_type_title);
  119. }
  120. }else{
  121. $this->makeThis($return,$value,$model_type_title);
  122. }
  123. }
  124. return array_values($return);
  125. }
  126. public function makeThis(&$return,$value,$model_type_title){
  127. if(isset($return[$value['model_type']])){
  128. $total = bcadd($value['total'], $return[$value['model_type']]['total'],2);
  129. $return[$value['model_type']]['total'] = $total;
  130. }else{
  131. $return[$value['model_type']] = [
  132. 'model_type' => $value['model_type'],
  133. 'model_type_title' => $model_type_title,
  134. 'total' => $value['total'],
  135. ];
  136. }
  137. }
  138. //省 订单类型细分统计
  139. public function statisticsProvince($data,$user){
  140. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  141. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  142. //销售订单类型
  143. $model_type = 0;
  144. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  145. //门店设置的指标 按照区域汇总
  146. $index_map = DepartIndex::where('del_time',0)
  147. ->pluck('param_one','top_depart_id')
  148. ->toArray();
  149. //分社所属省
  150. $depart_map = $province_map = [];
  151. $depart = Depart::where('parent_id',0)
  152. ->where('province','<>','')
  153. ->where('del_time',0)
  154. ->select('province','id')
  155. ->get()->toArray();
  156. foreach ($depart as $value){
  157. $depart_map[$value['id']] = $value['province'];
  158. $province_map[$value['province']][] = $value['id'];
  159. }
  160. $address_map = config('address');
  161. $address_map = array_column($address_map,'label','value');
  162. //省
  163. $final_address_map = [];
  164. foreach ($address_map as $key => $value){
  165. $tmp = [
  166. 'key' => $key,
  167. 'title' => $value,
  168. 'total' => 0,
  169. 'index' => 0
  170. ];
  171. $top_depart_id = $province_map[$key] ?? [];
  172. if(! empty($top_depart_id)){
  173. foreach ($top_depart_id as $depart_id){
  174. $index = $index_map[$depart_id] ?? 0;
  175. $tmp['index'] = bcadd($tmp['index'], $index,2);
  176. }
  177. }
  178. $final_address_map[] = $tmp;
  179. }
  180. //特殊的门店
  181. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  182. $bt_top_depart_id = $setting['setting_value'] ?? [];
  183. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  184. $sale_order = SalesOrder::where("del_time",0)
  185. ->where('crt_time','>=',$return[0])
  186. ->where('crt_time','<=',$return[1])
  187. ->when(! empty($model_type), function ($query) use ($model_type) {
  188. return $query->where('model_type',$model_type);
  189. })
  190. ->select('id','top_depart_id','contract_fee','model_type')
  191. ->get()->toArray();
  192. //合同
  193. $purchase_map = $sale_order_id = [];
  194. foreach ($sale_order as $value){
  195. $area = $depart_map[$value['top_depart_id']] ?? 0;
  196. if(! $area) continue;
  197. if($value['model_type'] != SalesOrder::Model_type_two){
  198. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  199. $sale_order_id[] = $value['id'];
  200. $this->makeData($purchase_map,$value,$area);
  201. }
  202. }else{
  203. $sale_order_id[] = $value['id'];
  204. $this->makeData($purchase_map,$value,$area);
  205. }
  206. }
  207. //退货的差异
  208. $returnExchange_map = [];
  209. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  210. ->where('type',ReturnExchangeOrder::Order_type)
  211. ->whereIn('data_id',array_unique($sale_order_id))
  212. ->select('top_depart_id','difference_amount')
  213. ->get()->toArray();
  214. foreach ($returnExchange as $value){
  215. $area = $depart_map[$value['top_depart_id']] ?? 0;
  216. if(! $area) continue;
  217. if(isset($returnExchange_map[$area])){
  218. $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
  219. $returnExchange_map[$area] = $total;
  220. }else{
  221. $returnExchange_map[$area] = $value['difference_amount'];
  222. }
  223. }
  224. foreach ($final_address_map as $key => $value){
  225. $purchase_tmp = $purchase_map[$value['key']] ?? 0;
  226. $return_tmp = $returnExchange_map[$value['key']] ?? 0;
  227. $final_address_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  228. }
  229. usort($final_address_map, function($a, $b) {
  230. return $b['total'] - $a['total'];
  231. });
  232. return [true, $final_address_map];
  233. }
  234. public function makeData(&$purchase_map, $value,$area){
  235. if(isset($purchase_map[$area])){
  236. $total = bcadd($purchase_map[$area], $value['contract_fee'],2);
  237. $purchase_map[$area] = $total;
  238. }else{
  239. $purchase_map[$area] = $value['contract_fee'];
  240. }
  241. }
  242. //大区 订单类型细分统计
  243. public function statisticsArea($data,$user){
  244. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  245. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  246. //销售订单类型
  247. $model_type = 0;
  248. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  249. //门店设置的指标 按照区域汇总
  250. $index_map = DepartIndex::where('del_time',0)
  251. ->pluck('param_one','top_depart_id')
  252. ->toArray();
  253. //分社所属大区
  254. $depart_map = $area_map = [];
  255. $depart = Depart::where('parent_id',0)
  256. ->where('area','>',0)
  257. ->where('del_time',0)
  258. ->select('area','id')
  259. ->get()->toArray();
  260. foreach ($depart as $value){
  261. $depart_map[$value['id']] = $value['area'];
  262. $area_map[$value['area']][] = $value['id'];
  263. }
  264. //大区
  265. $area = Depart::$area;
  266. $area_map = [];
  267. foreach ($area as $key => $value){
  268. $tmp = [
  269. 'key' => $key,
  270. 'title' => $value,
  271. 'total' => 0,
  272. 'index' => 0
  273. ];
  274. $top_depart_id = $area_map[$key] ?? [];
  275. if(! empty($top_depart_id)){
  276. foreach ($top_depart_id as $depart_id){
  277. $index = $index_map[$depart_id] ?? 0;
  278. $tmp['index'] = bcadd($tmp['index'], $index,2);
  279. }
  280. }
  281. $area_map[] = $tmp;
  282. }
  283. //特殊的门店
  284. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  285. $bt_top_depart_id = $setting['setting_value'] ?? [];
  286. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  287. $sale_order = SalesOrder::where("del_time",0)
  288. ->where('crt_time','>=',$return[0])
  289. ->where('crt_time','<=',$return[1])
  290. ->when(! empty($model_type), function ($query) use ($model_type) {
  291. return $query->where('model_type',$model_type);
  292. })
  293. ->select('top_depart_id','contract_fee','model_type','id')
  294. ->get()->toArray();
  295. $purchase_map = $sale_order_id = [];
  296. foreach ($sale_order as $value){
  297. $area = $depart_map[$value['top_depart_id']] ?? 0;
  298. if(! $area) continue;
  299. if($value['model_type'] != SalesOrder::Model_type_two){
  300. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  301. $sale_order_id[] = $value['id'];
  302. $this->makeData($purchase_map,$value,$area);
  303. }
  304. }else{
  305. $sale_order_id[] = $value['id'];
  306. $this->makeData($purchase_map,$value,$area);
  307. }
  308. }
  309. //退货的差异
  310. $returnExchange_map = [];
  311. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  312. ->where('type',ReturnExchangeOrder::Order_type)
  313. ->whereIn('data_id',array_unique($sale_order_id))
  314. ->select('top_depart_id','difference_amount')
  315. ->get()->toArray();
  316. foreach ($returnExchange as $value){
  317. $area = $depart_map[$value['top_depart_id']] ?? 0;
  318. if(! $area) continue;
  319. if(isset($returnExchange_map[$area])){
  320. $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
  321. $returnExchange_map[$area] = $total;
  322. }else{
  323. $returnExchange_map[$area] = $value['difference_amount'];
  324. }
  325. }
  326. foreach ($area_map as $key => $value){
  327. $purchase_tmp = $purchase_map[$value['key']] ?? 0;
  328. $return_tmp = $returnExchange_map[$value['key']] ?? 0;
  329. $area_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  330. }
  331. usort($area_map, function($a, $b) {
  332. return $b['total'] - $a['total'];
  333. });
  334. return [true, $area_map];
  335. }
  336. // 省|大区下的门店 订货统计
  337. public function statisticsAreaDepart($data,$user){
  338. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  339. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  340. if(empty($data['area']) && empty($data['province'])) return [false, '大区或省份必须选择一个'];
  341. if(! empty($data['area']) && ! empty($data['province']))return [false, '大区或省份有且只能有一个'];
  342. if(! empty($data['area']) && ! isset(Depart::$area[$data['area']])) return [false, '该大区不存在'];
  343. $address_map = config('address');
  344. $address_map = array_column($address_map,'label','value');
  345. if(! empty($data['province']) && ! isset($address_map[$data['province']])) return [false, '该省不存在'];
  346. //销售订单类型
  347. $model_type = 0;
  348. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  349. if(! empty($data['area'])){
  350. $depart = Depart::where('parent_id',0)
  351. ->where('del_time',0)
  352. ->where('area',$data['area'])
  353. ->select('id','title')
  354. ->get()
  355. ->toArray();
  356. if(empty($depart)) return [false, '该大区下不存在门店'];
  357. }else{
  358. $depart = Depart::where('parent_id',0)
  359. ->where('del_time',0)
  360. ->where('province',$data['province'])
  361. ->select('id','title')
  362. ->get()
  363. ->toArray();
  364. if(empty($depart)) return [false, '该省下不存在门店'];
  365. }
  366. //门店设置的指标
  367. $index_map = DepartIndex::where('del_time',0)
  368. ->whereIn('top_depart_id',array_column($depart,'id'))
  369. ->pluck('param_one','top_depart_id')
  370. ->toArray();
  371. //特殊的门店
  372. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  373. $bt_top_depart_id = $setting['setting_value'] ?? [];
  374. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  375. //合同
  376. $sale_order = SalesOrder::where("del_time",0)
  377. ->where('crt_time','>=',$return[0])
  378. ->where('crt_time','<=',$return[1])
  379. ->when(! empty($model_type), function ($query) use ($model_type) {
  380. return $query->where('model_type',$model_type);
  381. })
  382. ->select('top_depart_id','contract_fee','id','model_type')
  383. ->get()->toArray();
  384. //向总社采购的钱
  385. $purchase_map = $sale_order_id = [];
  386. foreach ($sale_order as $value){
  387. if($value['model_type'] != SalesOrder::Model_type_two){
  388. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  389. $sale_order_id[] = $value['id'];
  390. $this->makeData($purchase_map,$value,$value['top_depart_id']);
  391. }
  392. }else{
  393. $sale_order_id[] = $value['id'];
  394. $this->makeData($purchase_map,$value,$value['top_depart_id']);
  395. }
  396. }
  397. //退货的差异
  398. $returnExchange_map = [];
  399. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  400. ->whereIn('top_depart_id',array_column($depart,'id'))
  401. ->where('type',ReturnExchangeOrder::Order_type)
  402. ->whereIn('data_id',array_unique($sale_order_id))
  403. ->select('top_depart_id','difference_amount')
  404. ->get()->toArray();
  405. foreach ($returnExchange as $value){
  406. if(isset($returnExchange_map[$value['top_depart_id']])){
  407. $total = bcadd($returnExchange_map[$value['top_depart_id']], $value['difference_amount'],2);
  408. $returnExchange_map[$value['top_depart_id']] = $total;
  409. }else{
  410. $returnExchange_map[$value['top_depart_id']] = $value['difference_amount'];
  411. }
  412. }
  413. foreach ($depart as $key => $value){
  414. $purchase_tmp = $purchase_map[$value['id']] ?? 0;
  415. $return_tmp = $returnExchange_map[$value['id']] ?? 0;
  416. $depart[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  417. $depart[$key]['index'] = $index_map[$value['id']] ?? 0;
  418. }
  419. return [true, $depart];
  420. }
  421. //某个门店 关于产品以及分类的统计 订货统计
  422. public function statisticsAreaDepartProduct($data,$user){
  423. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  424. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  425. if(empty($data['top_depart_id'])) return [false, '请选择门店'];
  426. //销售订单类型
  427. $model_type = 0;
  428. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  429. //时间 =》 钱
  430. $purchase_map1 = $this->getTime($return);
  431. //产品 =》 数量
  432. $purchase_category_map = $purchase_map = [];
  433. $sale_order = SalesOrder::where("del_time",0)
  434. ->where('top_depart_id',$data['top_depart_id'])
  435. ->where('crt_time','>=',$return[0])
  436. ->where('crt_time','<=',$return[1])
  437. ->when(! empty($model_type), function ($query) use ($model_type) {
  438. return $query->where('model_type',$model_type);
  439. })
  440. ->select('id','crt_time')
  441. ->get()->toArray();
  442. $purchase_for_time = array_column($sale_order,'crt_time','id');
  443. $sale_order_id = array_column($sale_order,'id');
  444. $purchase_product = SalesOrderProductInfo::where("del_time",0)
  445. ->whereIn('sales_order_id',$sale_order_id)
  446. ->select('sales_order_id','product_id','number','price','final_amount','sports_bag_id')
  447. ->get()->toArray();
  448. //退换货
  449. list($returnExchange_map,$returnExchange_map_2) = $this->returnExchange($data,$sale_order_id);
  450. //产品
  451. $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys($returnExchange_map,'product_id'))))
  452. ->select('id','product_category','title','code')
  453. ->get()->toArray();
  454. $product_list_map = array_column($product_list,null,'id');
  455. $category_return = ProductCategory::where('del_time',0)
  456. ->where('parent_id',0)
  457. ->pluck('title','id')
  458. ->toArray();
  459. foreach ($purchase_product as $value){
  460. if($value['sports_bag_id'] > 0){
  461. $money = $value['final_amount'];
  462. }elseif($value['final_amount'] > 0){
  463. $money = $value['final_amount'];
  464. }else{
  465. $money = bcmul($value['price'],$value['number'],2);
  466. }
  467. $crt_time = $purchase_for_time[$value['sales_order_id']];
  468. $crt_time = date("Y-m-d",$crt_time);
  469. //产品信息
  470. $product_tmp = $product_list_map[$value['product_id']] ?? [];
  471. $time_money = 0;
  472. if(isset($returnExchange_map_2[$crt_time])) {
  473. $time_money = $returnExchange_map_2[$crt_time] ?? 0;
  474. unset($returnExchange_map_2[$crt_time]);
  475. }
  476. //钱
  477. if(isset($purchase_map1[$crt_time])){
  478. $time_total = bcadd($purchase_map1[$crt_time]['total'],$money,2);
  479. $time_total = bcsub($time_total,$time_money,2);
  480. $purchase_map1[$crt_time]['total'] = $time_total;
  481. }
  482. $return_number = $return_total = 0;
  483. $return_category_number = $return_category_total = 0;
  484. if(isset($returnExchange_map[$value['product_id']])){
  485. $tmp = $returnExchange_map[$value['product_id']];
  486. $return_number = bcsub($value['number'], $tmp['number'],2);
  487. $return_total = bcsub($money, $tmp['total'],2);
  488. $return_category_number = $return_number;
  489. $return_category_total = $return_total;
  490. }
  491. //-------根据产品
  492. //数量
  493. if(isset($purchase_map[$value['product_id']])){
  494. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  495. $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
  496. $purchase_map[$value['product_id']]['number'] = $tmp_number;
  497. $purchase_map[$value['product_id']]['total'] = $tmp_money;
  498. }else{
  499. //减去退换货
  500. $number = bcsub($value['number'], $return_number,2);
  501. $total = bcsub($money, $return_total,2);
  502. $purchase_map[$value['product_id']] = [
  503. 'title' => $product_tmp['title'] . "(" . $product_tmp['code'] .")",
  504. 'number' => $number,
  505. 'total' => $total
  506. ];
  507. }
  508. //-------根据产品
  509. //-------根据产品大类
  510. //数量
  511. $category_tmp = json_decode($product_tmp['product_category']);
  512. $category_tmp = min($category_tmp);
  513. //退换货
  514. $number_2 = bcsub($value['number'], $return_category_number,2);
  515. $total_2 = bcsub($money, $return_category_total,2);
  516. if(isset($purchase_category_map[$category_tmp])){
  517. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  518. $tmp_number = bcsub($tmp_number,$number_2,2);
  519. $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
  520. $tmp_money = bcsub($tmp_money,$total_2,2);
  521. $purchase_category_map[$category_tmp]['number'] = $tmp_number;
  522. $purchase_category_map[$category_tmp]['total'] = $tmp_money;
  523. }else{
  524. $num = bcsub($value['number'],$number_2,2);
  525. $tol = bcsub($money,$total_2,2);
  526. $purchase_category_map[$category_tmp] = [
  527. 'number' => $num,
  528. 'title' => $category_return[$category_tmp] ?? "",
  529. 'total' => $tol
  530. ];
  531. }
  532. }
  533. return [true, ['money' => array_values($purchase_map1), 'product_num' => array_values($purchase_map), 'category_num' => array_values($purchase_category_map)]];
  534. }
  535. public function getTime($data){
  536. $startTimestamp = $data[0]; // 起始时间戳
  537. $endTimestamp = $data[1]; // 结束时间戳
  538. // 创建 DateTime 对象
  539. $startDate = new \DateTime();
  540. $endDate = new \DateTime();
  541. $startDate->setTimestamp($startTimestamp);
  542. $endDate->setTimestamp($endTimestamp);
  543. // 创建 DatePeriod 对象
  544. $interval = new \DateInterval('P1D'); // 每天的间隔
  545. $dateRange = new \DatePeriod($startDate, $interval, $endDate);
  546. // 遍历日期范围并输出每个日期
  547. $return = [];
  548. foreach ($dateRange as $date) {
  549. $day = $date->format('Y-m-d');
  550. $return[$day] = [
  551. 'day' => $day,
  552. 'total' => 0
  553. ];
  554. }
  555. return $return;
  556. }
  557. public function returnExchange($data,$sale_order_id){
  558. $returnExchange_map = $returnExchange_map_2 = [];
  559. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  560. ->where('type',ReturnExchangeOrder::Order_type)
  561. ->whereIn('data_id', $sale_order_id)
  562. ->select('id')
  563. ->get()->toArray();
  564. $returnExchange_product = ReturnExchangeOrderProductInfo::where("del_time",0)
  565. ->whereIn('return_exchange_id',array_column($returnExchange,'id'))
  566. ->select('return_exchange_id','product_id','number','return_exchange_price as price','crt_time')
  567. ->get()->toArray();
  568. foreach ($returnExchange_product as $value){
  569. $money = bcmul($value['price'],$value['number'],2);
  570. if(isset($returnExchange_map[$value['product_id']])){
  571. $tmp_money = bcadd($returnExchange_map[$value['product_id']]['total'], $money,2);
  572. $tmp_number = bcadd($returnExchange_map[$value['product_id']]['number'], $value['number'],2);
  573. $returnExchange_map[$value['product_id']] = [
  574. 'number' => $tmp_number,
  575. 'total' => $tmp_money
  576. ];
  577. }else{
  578. $returnExchange_map[$value['product_id']] = [
  579. 'number' => $value['number'],
  580. 'total' => $money
  581. ];
  582. }
  583. $crt_time = date("Y-m-d",$value['crt_time']);
  584. if(isset($returnExchange_map_2[$crt_time])){
  585. $tmp_money_2 = bcadd($returnExchange_map_2[$crt_time], $money,2);
  586. $returnExchange_map_2[$crt_time] = $tmp_money_2;
  587. }else{
  588. $returnExchange_map_2[$crt_time] = $money;
  589. }
  590. }
  591. return [$returnExchange_map,$returnExchange_map_2];
  592. }
  593. public function statisticsJc($data,$user){
  594. $model = Product::ProductClear2($user,$data);
  595. $model = $model->where('del_time',0)
  596. ->select('title','id','code','depart_id','top_depart_id','product_attribute')
  597. ->orderby('product_attribute', 'desc')
  598. ->orderby('id', 'desc');
  599. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  600. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  601. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  602. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  603. if(! empty($data['product_category'])) {
  604. $product_category = ProductCategory::where('del_time',0)
  605. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  606. ->select('id')
  607. ->get()->toArray();
  608. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  609. }
  610. $list = $this->limit($model,'',$data);
  611. $list = $this->fillData($list,$user,$data);
  612. return [true, $list];
  613. }
  614. public function fillData($data, $user, $search){
  615. if(empty($data['data'])) return $data;
  616. //产品
  617. $product = array_column($data['data'],'id');
  618. //本月入库 本月出库
  619. list($in, $out) = $this->getThisMonthData($product,$user,$search);
  620. //上月结存 汇总这个月之前的出入
  621. list($last_in,$last_out) = $this->getLastMonthBalance($product,$user,$search);
  622. foreach ($data['data'] as $key => $value){
  623. $last_in_tmp = $last_in[$value['id']] ?? [];
  624. $last_in_money = $last_in_tmp['total'] ?? 0;//本月之前入的金额
  625. $last_in_number = $last_in_tmp['number'] ?? 0;//本月之前入的数量
  626. $last_out_tmp = $last_out[$value['id']] ?? [];
  627. $last_out_money = $last_out_tmp['total'] ?? 0;//本月之前出的金额
  628. $last_out_number = $last_out_tmp['number'] ?? 0;//本月之前出的数量
  629. //上月结存
  630. $last_jc_money = bcsub($last_in_money,$last_out_money,2);//结存金额
  631. $last_jc_number = bcsub($last_in_number,$last_out_number,2);//结存数量
  632. $last_jc_price = floatval($last_jc_number) ? bcdiv($last_jc_money,$last_jc_number,2) : 0;//结存单价
  633. $data['data'][$key]['last_jc_money'] = $last_jc_money;
  634. $data['data'][$key]['last_jc_number'] = $last_jc_number;
  635. $data['data'][$key]['last_jc_price'] = $last_jc_price;
  636. //本月入库
  637. $this_in_tmp = $in[$value['id']] ?? [];
  638. $this_in_money = $this_in_tmp['total'] ?? 0;//本月入的金额
  639. $this_in_number = $this_in_tmp['number'] ?? 0;//本月入的数量
  640. $this_in_price = floatval($this_in_number) ? bcdiv($this_in_money,$this_in_number,2) : 0;//本月入单价
  641. $data['data'][$key]['this_in_money'] = $this_in_money;
  642. $data['data'][$key]['this_in_number'] = $this_in_number;
  643. $data['data'][$key]['this_in_price'] = $this_in_price;
  644. //本月出库
  645. $this_out_tmp = $out[$value['id']] ?? [];
  646. $this_out_money = $this_out_tmp['total'] ?? 0;//本月出的金额
  647. $this_out_number = $this_out_tmp['number'] ?? 0;//本月出的数量
  648. //单价= (上月结存金额+本月入库金额) /上月结存数量+本月入库数量
  649. $amount = bcadd($last_jc_money, $this_in_money,2);
  650. $number = bcadd($last_jc_number, $this_in_number,2);
  651. $this_out_price = floatval($number) ? bcdiv($amount,$number,2) : 0;//本月出单价
  652. $data['data'][$key]['this_out_money'] = $this_out_money;
  653. $data['data'][$key]['this_out_number'] = $this_out_number;
  654. $data['data'][$key]['this_out_price'] = $this_out_price;
  655. //本月结存
  656. //本月结存 数量/金额=本月入库+上月结存-本月出库
  657. $this_jc_money = bcsub(bcadd($this_in_money,$last_jc_money,2),$this_out_money);
  658. $this_jc_number = bcsub(bcadd($this_in_number,$last_jc_number,2),$this_out_number);
  659. $this_jc_price = floatval($this_jc_number) ? bcdiv($this_jc_money,$this_jc_number,2) : 0;//本月结存单价
  660. $data['data'][$key]['this_jc_money'] = $this_jc_money;
  661. $data['data'][$key]['this_jc_number'] = $this_jc_number;
  662. $data['data'][$key]['this_jc_price'] = $this_jc_price;
  663. }
  664. return $data;
  665. }
  666. //本月入库 出库(库存流水)
  667. public function getThisMonthData($product = [], $user = [], $search = []){
  668. $in = $out = [];
  669. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  670. $endStamp = strtotime(date("Y-m-t 23:59:59"));
  671. //本月出和入的数据
  672. $model = InOutRecord::TopClear($user,$search);
  673. $list = $model->where('del_time',0)
  674. ->where('crt_time','>=',$startStamp)
  675. ->where('crt_time','<=',$endStamp)
  676. ->whereIn('product_id',$product)
  677. ->select('product_id','number','price')
  678. ->get()->toArray();
  679. foreach ($list as $value){
  680. if($value['number'] >= 0){
  681. $tmp_total = bcmul($value['number'], $value['price'], 2);
  682. if(isset($in[$value['product_id']])){
  683. $number = bcadd($in[$value['product_id']]['number'], $value['number'],0);
  684. $total = bcadd($in[$value['product_id']]['total'], $tmp_total,2);
  685. $in[$value['product_id']]['number'] = $number;
  686. $in[$value['product_id']]['total'] = $total;
  687. }else{
  688. $in[$value['product_id']] = [
  689. 'number' => $value['number'],
  690. 'total' => $tmp_total,
  691. ];
  692. }
  693. }else{
  694. $number = abs($value['number']);
  695. $tmp_total = bcmul($number, $value['price'], 2);
  696. if(isset($out[$value['product_id']])){
  697. $number = bcadd($out[$value['product_id']]['number'], $number,0);
  698. $total = bcadd($out[$value['product_id']]['total'], $tmp_total,2);
  699. $out[$value['product_id']]['number'] = $number;
  700. $out[$value['product_id']]['total'] = $total;
  701. }else{
  702. $out[$value['product_id']] = [
  703. 'number' => $number,
  704. 'total' => $tmp_total,
  705. ];
  706. }
  707. }
  708. }
  709. return [$in, $out];
  710. }
  711. //上月结存(汇总这个月之前的出入)(库存流水)
  712. public function getLastMonthBalance($product = [], $user = [], $search = []){
  713. //用户提交的上月结存
  714. $lastData = $this->getLastMonthJc($product,$user,$search);
  715. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  716. $model = InOutRecord::TopClear($user,$search);
  717. $list = $model->where('del_time',0)
  718. ->where('crt_time','<',$startStamp)
  719. ->whereIn('product_id',$product)
  720. ->select('product_id','number','price','top_depart_id')
  721. ->get()->toArray();
  722. $map = [];
  723. foreach ($list as $val){
  724. $map[$val['product_id'] . $val['top_depart_id']] = $val;
  725. }
  726. //先结存表
  727. $in = $out = [];
  728. foreach ($lastData as $value){
  729. $key = $value['product_id'] . '|' . $value['top_depart_id'];
  730. if($value['number'] >= 0){
  731. if(isset($in[$key])){
  732. $number = bcadd($in[$key]['number'], $value['number'],0);
  733. $total = bcadd($in[$key]['total'], $value['total'],2);
  734. $in[$key]['number'] = $number;
  735. $in[$key]['total'] = $total;
  736. }else{
  737. $in[$key] = [
  738. 'number' => $value['number'],
  739. 'total' => $value['total'],
  740. ];
  741. }
  742. }else{
  743. $number = abs($value['number']);
  744. $total = abs($value['total']);
  745. if(isset($out[$key])){
  746. $number = bcadd($out[$key]['number'], $number,0);
  747. $total = bcadd($out[$key]['total'], $total,2);
  748. $out[$key]['number'] = $number;
  749. $out[$key]['total'] = $total;
  750. }else{
  751. $out[$key] = [
  752. 'number' => $number,
  753. 'total' => $total,
  754. ];
  755. }
  756. }
  757. }
  758. //后库存流水
  759. foreach ($list as $value){
  760. $key = $value['product_id'] . '|' . $value['top_depart_id'];
  761. if($value['number'] >= 0){
  762. if(isset($in[$key])) continue;
  763. $tmp_total = bcmul($value['number'], $value['price'], 2);
  764. if(isset($in[$value['product_id']])){
  765. $number = bcadd($in[$value['product_id']]['number'], $value['number'],0);
  766. $total = bcadd($in[$value['product_id']]['total'], $tmp_total,2);
  767. $in[$value['product_id']]['number'] = $number;
  768. $in[$value['product_id']]['total'] = $total;
  769. }else{
  770. $in[$value['product_id']] = [
  771. 'number' => $value['number'],
  772. 'total' => $tmp_total,
  773. ];
  774. }
  775. }else{
  776. if(isset($out[$key])) continue;
  777. $number = abs($value['number']);
  778. $tmp_total = bcmul($number, $value['price'], 2);
  779. if(isset($out[$value['product_id']])){
  780. $number = bcadd($out[$value['product_id']]['number'], $number,0);
  781. $total = bcadd($out[$value['product_id']]['total'], $tmp_total,2);
  782. $out[$value['product_id']]['number'] = $number;
  783. $out[$value['product_id']]['total'] = $total;
  784. }else{
  785. $out[$value['product_id']] = [
  786. 'number' => $number,
  787. 'total' => $tmp_total,
  788. ];
  789. }
  790. }
  791. }
  792. //两个数组组合过滤
  793. $new_in = $new_out = [];
  794. foreach ($in as $key => $value){
  795. $tmp = explode('|', $key);
  796. $product_id = $tmp[0];
  797. if(isset($new_in[$product_id])){
  798. $number = bcadd($new_in[$product_id]['number'], $value['number'],0);
  799. $total = bcadd($new_in[$product_id]['total'], $value['total'],2);
  800. $new_in[$product_id] = [
  801. 'number' => $number,
  802. 'total' => $total,
  803. ];
  804. }else{
  805. $new_in[$product_id] = [
  806. 'number' => $value['number'],
  807. 'total' => $value['total'],
  808. ];
  809. }
  810. }
  811. foreach ($out as $key => $value){
  812. $tmp = explode('|', $key);
  813. $product_id = $tmp[0];
  814. if(isset($new_out[$product_id])){
  815. $number = bcadd($new_out[$product_id]['number'], $value['number'],0);
  816. $total = bcadd($new_out[$product_id]['total'], $value['total'],2);
  817. $new_out[$product_id] = [
  818. 'number' => $number,
  819. 'total' => $total,
  820. ];
  821. }else{
  822. $new_out[$product_id] = [
  823. 'number' => $value['number'],
  824. 'total' => $value['total'],
  825. ];
  826. }
  827. }
  828. return [$new_in, $new_out];
  829. }
  830. public function getLastMonthJc($product = [], $user = [], $search = []){
  831. // 如果存在上月结存数据则使用这个
  832. $top_depart_id = 0;
  833. if(empty($search['top_depart_id'])) $top_depart_id = $search['top_depart_id'];
  834. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  835. $result = LastJc::where('del_time',0)
  836. ->whereIn('product_id',$product)
  837. ->where('time','<',$startStamp)
  838. ->when(! empty($top_depart_id), function ($query) use ($top_depart_id) {
  839. return $query->where('top_depart_id',$top_depart_id);
  840. })
  841. ->select('product_id','top_depart_id','total','number','price')
  842. ->orderBy('time','desc')
  843. ->get()->toArray();
  844. $return = [];
  845. $tmp = [];
  846. foreach ($result as $value){
  847. $key = $value['product_id'] . $value['top_depart_id'];
  848. if(in_array($key, $tmp)) continue;
  849. $return[] = $value;
  850. $tmp[] = $key;
  851. }
  852. return $return;
  853. }
  854. //本月入库(单据)暂时没用
  855. public function getThisMonthIn1($product = [], $user = [], $search = []){
  856. $return = [];
  857. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  858. $endStamp = strtotime(date("Y-m-t 23:59:59"));
  859. //本月采购单
  860. $model = PurchaseOrder::Clear($user,$search);
  861. $list = $model->where('del_time',0)
  862. ->where('state', PurchaseOrder::STATE_Four)
  863. ->where('crt_time','>=',$startStamp)
  864. ->where('crt_time','<=',$endStamp)
  865. ->select('id')
  866. ->get()->toArray();
  867. if(empty($list)) return $return;
  868. //本月采购产品
  869. $purchase_product_array = [];
  870. $purchase_product = PurchaseOrderInfo::where('del_time',0)
  871. ->whereIn('product_id',$product)
  872. ->whereIn('purchase_order_id',array_column($list,'id'))
  873. ->select('product_id','number','price')
  874. ->get()->toArray();
  875. foreach ($purchase_product as $value){
  876. $total = bcmul($value['number'],$value['price'],2);
  877. if(isset($purchase_product_array[$value['product_id']])){
  878. $purchase_product_array[$value['product_id']]['number'] += $value['number'];
  879. $total_tmp = bcadd($purchase_product_array[$value['product_id']]['total'], $total, 2);
  880. $purchase_product_array[$value['product_id']]['total'] = $total_tmp;
  881. }else{
  882. $purchase_product_array[$value['product_id']] = [
  883. 'number' => $value['number'],
  884. 'total' => $total,
  885. ];
  886. }
  887. }
  888. //本月退货(采购)
  889. $model2 = ReturnExchangeOrder::Clear($user, $search);
  890. $return_list = $model2->where('del_time',0)
  891. ->where('state', ReturnExchangeOrder::State_two)
  892. ->where('type',ReturnExchangeOrder::Order_type2)
  893. ->where('crt_time','>=',$startStamp)
  894. ->where('crt_time','<=',$endStamp)
  895. ->select('id')
  896. ->get()->toArray();
  897. //本月退货产品
  898. $return_product_array = [];
  899. if(! empty($return_list)){
  900. $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
  901. ->where('return_exchange_id', array_column($return_list, 'id'))
  902. ->whereIn('product_id',$product)
  903. ->where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
  904. ->select('product_id','number','return_or_exchange')
  905. ->get()->toArray();
  906. foreach ($return_product as $value){
  907. $total = bcmul($value['number'],$value['return_or_exchange'],2);
  908. if(isset($return_product_array[$value['product_id']])){
  909. $return_product_array[$value['product_id']]['number'] += $value['number'];
  910. $total_tmp = bcadd($return_product_array[$value['product_id']]['total'], $total, 2);
  911. $return_product_array[$value['product_id']]['total'] = $total_tmp;
  912. }else{
  913. $return_product_array[$value['product_id']] = [
  914. 'number' => $value['number'],
  915. 'total' => $total,
  916. ];
  917. }
  918. }
  919. }
  920. foreach ($return_product_array as $p => $n){
  921. $number_tmp = -$n['number'];
  922. $total_tmp = -$n['total'];
  923. $purchase_product_tmp = $purchase_product_array[$p] ?? [];
  924. if(empty($purchase_product_tmp)){
  925. $purchase_product_array[$p] = [
  926. 'number' => $number_tmp,
  927. 'total' => $total_tmp,
  928. ];
  929. }else{
  930. $purchase_product_array[$p]['number'] += $number_tmp;
  931. $total_tmp2 = bcadd($purchase_product_array[$p]['total'], $total_tmp, 2);
  932. $purchase_product_array[$p]['total'] += $total_tmp2;
  933. }
  934. }
  935. return $purchase_product_array;
  936. }
  937. //--------------------------脚本
  938. public function inoutrecord(){return;
  939. $in_data = InvoiceOrder::where('del_time',0)
  940. ->select('id','sales_order_id')
  941. ->get()->toArray();
  942. $map = [];
  943. $s_p = SalesOrderProductInfo::whereIn('sales_order_id',array_column($in_data,'sales_order_id'))
  944. ->where('del_time',0)
  945. ->select('sales_order_id','product_id','final_amount','price')
  946. ->get()->toArray();
  947. foreach ($s_p as $value){
  948. $map[$value['sales_order_id']][] = [
  949. 'product_id' => $value['product_id'],
  950. 'final_amount' => $value['final_amount'],
  951. 'price' => $value['price'],
  952. ];
  953. }
  954. DB::beginTransaction();
  955. try {
  956. foreach ($in_data as $value){
  957. $tmp = $map[$value['sales_order_id']] ?? [];
  958. if(empty($tmp)) continue;
  959. foreach ($tmp as $val){
  960. InvoiceOrderInfo::where('del_time',0)
  961. ->where('invoice_id', $value['id'])
  962. ->where('product_id', $val['product_id'])
  963. ->update([
  964. 'final_amount' => $val['final_amount'],
  965. 'price' => $val['price'],
  966. ]);
  967. }
  968. }
  969. }catch (\Throwable $exception){
  970. DB::rollBack();
  971. dd($exception->getMessage());
  972. }
  973. DB::commit();
  974. dd(1);
  975. }
  976. public function inoutrecord2(){return;
  977. DB::beginTransaction();
  978. try {
  979. DB::table('in_out_record')
  980. ->where('price',0)
  981. ->whereIn('order_type', array_values(ReturnExchangeOrder::$prefix))
  982. ->select('id','order_number','product_id')
  983. ->orderBy('id','asc')
  984. ->chunk(200,function ($data) {;
  985. $data = Collect($data)->map(function ($object) {
  986. return (array)$object;
  987. })->toArray();
  988. $map = ReturnExchangeOrder::where('del_time',0)
  989. ->whereIn('order_number',array_column($data,'order_number'))
  990. ->pluck('id','order_number')
  991. ->toArray();
  992. $result = ReturnExchangeOrderProductInfo::where('del_time',0)
  993. ->whereIn('return_exchange_id',array_values($map))
  994. ->select('return_exchange_id','return_exchange_price','product_id')
  995. ->get()->toArray();
  996. $result_map = [];
  997. foreach ($result as $v){
  998. $result_map[$v['return_exchange_id']][$v['product_id']] = [
  999. 'product_id' => $v['product_id'],
  1000. 'price' => $v['return_exchange_price'],
  1001. ];
  1002. }
  1003. foreach ($data as $value){
  1004. $tmp_id = $map[$value['order_number']] ?? 0;
  1005. if($tmp_id < 0) continue;
  1006. $tmp = $result_map[$tmp_id][$value['product_id']] ?? [];
  1007. if(empty($tmp)) continue;
  1008. InOutRecord::where('id',$value['id'])->update([
  1009. 'price' => $tmp['price']
  1010. ]);
  1011. }
  1012. });
  1013. DB::commit();
  1014. }catch (\Throwable $exception){
  1015. DB::rollBack();
  1016. dd($exception->getMessage());
  1017. }
  1018. dd(1);
  1019. }
  1020. public function statisticsAreaDepartProduct222($data,$user){
  1021. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  1022. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  1023. if(empty($data['top_depart_id'])) return [false, '请选择门店'];
  1024. //向总社采购的钱
  1025. $purchase_map = [];
  1026. $purchase = PurchaseOrder::where('del_time',0)
  1027. ->where('top_depart_id',$data['top_depart_id'])
  1028. ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  1029. ->where('crt_time','>=',$return[0])
  1030. ->where('crt_time','<=',$return[1])
  1031. ->select('id')
  1032. ->get()->toArray();
  1033. $purchase_product = PurchaseOrderInfo::where("del_time",0)
  1034. ->whereIn('purchase_order_id',array_column($purchase,'id'))
  1035. ->select('product_id','number','price','final_amount','sports_bag_id')
  1036. ->get()->toArray();
  1037. foreach ($purchase_product as $value){
  1038. if($value['sports_bag_id'] > 0){
  1039. $money = $value['final_amount'];
  1040. }elseif($value['final_amount'] > 0){
  1041. $money = $value['final_amount'];
  1042. }else{
  1043. $money = bcmul($value['price'],$value['number'],2);
  1044. }
  1045. if(isset($purchase_map[$value['product_id']])){
  1046. $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
  1047. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  1048. $purchase_map[$value['product_id']] = [
  1049. 'number' => $tmp_number,
  1050. 'total' => $tmp_money
  1051. ];
  1052. }else{
  1053. $purchase_map[$value['product_id']] = [
  1054. 'number' => $value['number'],
  1055. 'total' => $money
  1056. ];
  1057. }
  1058. }
  1059. //退货的差异
  1060. // $returnExchange_map = $this->returnExchange($data,$return);
  1061. //产品
  1062. $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys([],'product_id'))))
  1063. ->select('id','product_category','title')
  1064. ->get()->toArray();
  1065. $product_list_map = array_column($product_list,null,'id');
  1066. $category = $category_sum = [];
  1067. foreach ($purchase_map as $key => $value){
  1068. // if(isset($returnExchange_map[$key])){
  1069. // $tmp = $returnExchange_map[$key];
  1070. // $number = bcsub($value['number'], $tmp['number'],2);
  1071. // $total = bcsub($value['total'], $tmp['total'],2);
  1072. // $purchase_map[$key] = [
  1073. // 'number' => $number,
  1074. // 'total' => $total,
  1075. // ];
  1076. // }
  1077. $product_tmp = $product_list_map[$key] ?? [];
  1078. $purchase_map[$key]['title'] = $product_tmp['title'];
  1079. $category_tmp = json_decode($product_tmp['product_category']);
  1080. $category_tmp = $category_tmp[0];
  1081. if(! in_array($category_tmp,$category)) $category[] = $category_tmp;
  1082. if(isset($category_sum[$category_tmp])){
  1083. $tmp_number = bcadd($category_sum[$category_tmp]['number'], $purchase_map[$key]['number'],2);
  1084. $tmp_total = bcadd($category_sum[$category_tmp]['total'], $purchase_map[$key]['total'],2);
  1085. $category_sum[$category_tmp] = [
  1086. 'number' => $tmp_number,
  1087. 'total' => $tmp_total,
  1088. ];
  1089. }else{
  1090. $category_sum[$category_tmp] = [
  1091. 'number' => $purchase_map[$key]['number'],
  1092. 'total' => $purchase_map[$key]['total'],
  1093. ];
  1094. }
  1095. }
  1096. //根据产品大类 $category_sum
  1097. $category_return = ProductCategory::whereIn('id',$category)
  1098. ->select('id','title')
  1099. ->get()->toArray();
  1100. foreach ($category_return as $key => $value){
  1101. $tmp = $category_sum[$value['id']] ?? [];
  1102. $category_return[$key]['number'] = $tmp['number'];
  1103. $category_return[$key]['total'] = $tmp['total'];
  1104. }
  1105. //根据产品 $purchase_map
  1106. dd($purchase_map);
  1107. return [true, ''];
  1108. }
  1109. public function statisticsProvince2222($data,$user){
  1110. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  1111. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  1112. //门店设置的指标 按照区域汇总
  1113. $index_map = DepartIndex::where('del_time',0)
  1114. ->pluck('param_one','top_depart_id')
  1115. ->toArray();
  1116. //分社所属省
  1117. $depart_map = $province_map = [];
  1118. $depart = Depart::where('parent_id',0)
  1119. ->where('province','<>','')
  1120. ->where('del_time',0)
  1121. ->select('province','id')
  1122. ->get()->toArray();
  1123. foreach ($depart as $value){
  1124. $depart_map[$value['id']] = $value['province'];
  1125. $province_map[$value['province']][] = $value['id'];
  1126. }
  1127. $address_map = config('address');
  1128. $address_map = array_column($address_map,'label','value');
  1129. //大区
  1130. $final_address_map = [];
  1131. foreach ($address_map as $key => $value){
  1132. $tmp = [
  1133. 'key' => $key,
  1134. 'title' => $value,
  1135. 'total' => 0,
  1136. 'index' => 0
  1137. ];
  1138. $top_depart_id = $province_map[$key] ?? [];
  1139. if(! empty($top_depart_id)){
  1140. foreach ($top_depart_id as $depart_id){
  1141. $index = $index_map[$depart_id] ?? 0;
  1142. $tmp['index'] = bcadd($tmp['index'], $index,2);
  1143. }
  1144. }
  1145. $final_address_map[] = $tmp;
  1146. }
  1147. //向总社采购的钱
  1148. $purchase_map = [];
  1149. $purchase = PurchaseOrder::where('del_time',0)
  1150. ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  1151. ->where('crt_time','>=',$return[0])
  1152. ->where('crt_time','<=',$return[1])
  1153. ->select('top_depart_id','purchase_total')
  1154. ->get()->toArray();
  1155. foreach ($purchase as $value){
  1156. $area = $depart_map[$value['top_depart_id']] ?? 0;
  1157. if(! $area) continue;
  1158. if(isset($purchase_map[$area])){
  1159. $total = bcadd($purchase_map[$area], $value['purchase_total'],2);
  1160. $purchase_map[$area] = $total;
  1161. }else{
  1162. $purchase_map[$area] = $value['purchase_total'];
  1163. }
  1164. }
  1165. //退货的差异
  1166. $returnExchange_map = [];
  1167. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  1168. ->where('type',ReturnExchangeOrder::Order_type2)
  1169. ->where('crt_time','>=',$return[0])
  1170. ->where('crt_time','<=',$return[1])
  1171. ->where('crt_time','<=',$return[1])
  1172. ->select('top_depart_id','difference_amount')
  1173. ->get()->toArray();
  1174. foreach ($returnExchange as $value){
  1175. $area = $depart_map[$value['top_depart_id']] ?? 0;
  1176. if(! $area) continue;
  1177. if(isset($returnExchange_map[$area])){
  1178. $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
  1179. $returnExchange_map[$area] = $total;
  1180. }else{
  1181. $returnExchange_map[$area] = $value['difference_amount'];
  1182. }
  1183. }
  1184. foreach ($final_address_map as $key => $value){
  1185. $purchase_tmp = $purchase_map[$value['key']] ?? 0;
  1186. $return_tmp = $returnExchange_map[$value['key']] ?? 0;
  1187. $final_address_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  1188. }
  1189. return [true, $final_address_map];
  1190. }
  1191. public function fillStatisticsBt1($data){
  1192. if(empty($data)) return $data;
  1193. $total = floatval(array_sum(array_column($data,'total')));
  1194. // 设置一个最小显示阈值,比如0.5%
  1195. $threshold = 0.5;
  1196. // 用于存储调整后的数据
  1197. $adjustedData = [];
  1198. $otherTotal = 0;
  1199. $other = [];
  1200. foreach ($data as $value) {
  1201. $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  1202. $rate = $total ? $value['total'] / $total : 0;
  1203. // 检查是否低于阈值
  1204. if ($rate * 100 < $threshold) {
  1205. // 小于阈值的部分加到"其他"中
  1206. $otherTotal += $value['total'];
  1207. $other[] = $model_type_title;
  1208. } else {
  1209. // 保留更多小数位数
  1210. $adjustedData[] = [
  1211. 'model_type' => $value['model_type'],
  1212. 'total' => $value['total'],
  1213. 'model_type_title' => $model_type_title,
  1214. 'rate' => round($rate * 100, 2)
  1215. ];
  1216. }
  1217. }
  1218. // 如果有"其他"值,则添加到数据集中
  1219. if ($otherTotal > 0) {
  1220. $other_title = implode(',',$other);
  1221. $adjustedData[] = [
  1222. 'model_type' => -1, // 自定义一个类型标识
  1223. 'total' => $otherTotal,
  1224. 'model_type_title' => '其他(' . $other_title . ')',
  1225. 'rate' => round($otherTotal / $total * 100, 2)
  1226. ];
  1227. }
  1228. return $adjustedData;
  1229. }
  1230. }