StatisticsService.php 84 KB

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