StatisticsService.php 53 KB

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