StatisticsService.php 57 KB

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