StatisticsService.php 56 KB

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