StatisticsService.php 62 KB

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