StatisticsService.php 79 KB

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