StatisticsService.php 51 KB

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