StatisticsService.php 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\BasicTypeAllUse;
  5. use App\Model\Customer;
  6. use App\Model\CustomerReport;
  7. use App\Model\CustomerReportDepart;
  8. use App\Model\Depart;
  9. use App\Model\DepartIndex;
  10. use App\Model\Employee;
  11. use App\Model\EmployeeDepartPermission;
  12. use App\Model\InOutRecord;
  13. use App\Model\InvoiceOrder;
  14. use App\Model\InvoiceOrderInfo;
  15. use App\Model\LastJc;
  16. use App\Model\Product;
  17. use App\Model\ProductAdjustment;
  18. use App\Model\ProductCategory;
  19. use App\Model\PurchaseOrder;
  20. use App\Model\PurchaseOrderInfo;
  21. use App\Model\ReturnExchangeOrder;
  22. use App\Model\ReturnExchangeOrderProductInfo;
  23. use App\Model\SalesOrder;
  24. use App\Model\SalesOrderProductInfo;
  25. use App\Model\SeeRange;
  26. use App\Model\Setting;
  27. use Carbon\Carbon;
  28. use Illuminate\Support\Facades\DB;
  29. class StatisticsService extends Service
  30. {
  31. public function statisticsCustomer($data,$user){
  32. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择客资创建时间区间'];
  33. $model = Customer::Clear($user,$data);
  34. $model = $model->where('del_time',0)
  35. ->select('id','model_type','customer_from','top_depart_id')
  36. ->orderby('id', 'desc');
  37. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  38. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  39. $model->where('crt_time','>=',$return[0]);
  40. $model->where('crt_time','<=',$return[1]);
  41. }
  42. $list = $model->get()->toArray();
  43. $list1 = $this->fillStatisticsCustomer($list);
  44. $list2 = $this->fillStatisticsCustomer2($list);
  45. $list3 = $this->fillStatisticsCustomer3($list);
  46. return [true, ['bt' => $list1, 'zz' => $list2, 'table' => $list3]];
  47. }
  48. public function fillStatisticsCustomer($data){
  49. if(empty($data)) return $data;
  50. $customer_from = array_unique(array_column($data,'customer_from'));
  51. $basic_type = BasicType::where('del_time',0)
  52. ->whereIn('id',$customer_from)
  53. ->where('type',2)
  54. ->pluck('title','id')
  55. ->toArray();
  56. $return = [];
  57. foreach ($data as $value){
  58. $tmp = $basic_type[$value['customer_from']] ?? "";
  59. if(! $tmp) continue;
  60. if(isset($return[$tmp])){
  61. $return[$tmp]['total'] += 1;
  62. }else{
  63. $return[$tmp] = [
  64. 'total' => 1,
  65. 'title' => $tmp
  66. ];
  67. }
  68. }
  69. return array_values($return);
  70. }
  71. public function fillStatisticsCustomer2($data){
  72. if(empty($data)) return $data;
  73. $depart = Depart::where('del_time',0)
  74. ->where('parent_id',0)
  75. ->where('is_main',0)
  76. ->pluck('title','id')
  77. ->toArray();
  78. $return = [];
  79. foreach ($depart as $key => $value){
  80. $return[$key] = [
  81. 'title' => $value,
  82. 'total' => 0
  83. ];
  84. }
  85. $top_depart_id = SeeRange::where('del_time',0)
  86. ->where('data_type',SeeRange::type_one)
  87. ->where('type',SeeRange::data_three)
  88. ->whereIn('data_id', array_column($data,'id'))
  89. ->select('param_id as fp_top_depart_id')
  90. ->get()->toArray();
  91. $top_depart_map = [];
  92. foreach ($top_depart_id as $value){
  93. if(isset($top_depart_map[$value['fp_top_depart_id']])){
  94. $top_depart_map[$value['fp_top_depart_id']] += 1;
  95. }else{
  96. $top_depart_map[$value['fp_top_depart_id']] = 1;
  97. }
  98. }
  99. foreach ($return as $key => $value){
  100. if(isset($top_depart_map[$key])) $return[$key]['total'] = $top_depart_map[$key];
  101. }
  102. $return = array_values($return);
  103. usort($return, function($a, $b) {
  104. return $b['total'] - $a['total'];
  105. });
  106. return $return;
  107. }
  108. public function fillStatisticsCustomer3($data){
  109. if(empty($data)) return $data;
  110. $array = array_unique(array_column($data,'customer_from'));
  111. $basic_map = BasicType::whereIn('id',$array)
  112. ->pluck('title','id')
  113. ->toArray();
  114. $return_first = $customer_from_map = $header = [];
  115. foreach ($data as $value){
  116. $title = $basic_map[$value['customer_from']] ?? "";
  117. if(! $title) continue;
  118. if(isset($return_first[$title])){
  119. $return_first[$title]['total'] += 1;
  120. }else{
  121. $return_first[$title] = [
  122. 'title' => $title,
  123. 'total' => 1,
  124. ];
  125. }
  126. $customer_from_map[$value['id']] = $value['customer_from'];
  127. if(! in_array($title, $header)) $header[] = $title;
  128. }
  129. $header_tmp = $header_return = [];
  130. foreach ($header as $value){
  131. $header_tmp[] = [
  132. "title" => $value,
  133. "total" => 0,
  134. ];
  135. $header_return[] = [
  136. "title" => $value
  137. ];
  138. }
  139. $return_first = array_values($return_first);
  140. $depart = Depart::where('del_time',0)
  141. ->where('parent_id',0)
  142. ->where('is_main',0)
  143. ->pluck('title','id')
  144. ->toArray();
  145. $return = [];
  146. foreach ($depart as $key => $value){
  147. $return[$key] = [
  148. 'title' => $value,
  149. 'total' => 0,
  150. 'customer_from' => $header_tmp,
  151. ];
  152. }
  153. $top_depart_id = SeeRange::where('del_time',0)
  154. ->where('data_type',SeeRange::type_one)
  155. ->where('type',SeeRange::data_three)
  156. ->whereIn('data_id', array_column($data,'id'))
  157. ->select('param_id as fp_top_depart_id','data_id as customer_id')
  158. ->get()->toArray();
  159. $top_depart_map = $top_depart_map2 = [];
  160. foreach ($top_depart_id as $value){
  161. if(isset($top_depart_map[$value['fp_top_depart_id']])){
  162. $top_depart_map[$value['fp_top_depart_id']] += 1;
  163. }else{
  164. $top_depart_map[$value['fp_top_depart_id']] = 1;
  165. }
  166. $customer_from_tmp = $customer_from_map[$value['customer_id']] ?? 0;
  167. if($customer_from_tmp){
  168. if(isset($top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp])){
  169. $top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp] += 1;
  170. }else{
  171. $top_depart_map2[$value['fp_top_depart_id']][$customer_from_tmp] = 1;
  172. }
  173. }
  174. }
  175. foreach ($return as $key => $value){
  176. if(isset($top_depart_map[$key])) {
  177. $num = $top_depart_map[$key];
  178. if($num <= 0) {
  179. unset($return[$key]);
  180. continue;
  181. }else{
  182. $return[$key]['total'] = $top_depart_map[$key];
  183. }
  184. }else{
  185. unset($return[$key]);
  186. continue;
  187. }
  188. if(isset($top_depart_map2[$key])) {
  189. $tmp = $top_depart_map2[$key];
  190. foreach ($tmp as $k => $v){
  191. $title = $basic_map[$k] ?? "";
  192. if(! $title) continue;
  193. foreach ($return[$key]['customer_from'] as $kk => $vv){
  194. if($vv['title'] == $title){
  195. $return[$key]['customer_from'][$kk]['total'] = $v;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. $return = array_values($return);
  202. usort($return, function($a, $b) {
  203. return $b['total'] - $a['total'];
  204. });
  205. return ['header' => $header_return,'left' => $return_first, 'right' => $return];
  206. }
  207. //销售统计饼图 根据合同类型区分(例如线上合同、线下合同)
  208. public function statisticsBt($data,$user){
  209. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择销售订单时间区间'];
  210. //总社id
  211. $head = $user['head']['id'] ?? 0;
  212. //当前门店
  213. $current_top_depart_id = $this->getMyTopDepart($user);
  214. //需要所有数据的门店
  215. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  216. $bt_top_depart_id = $setting['setting_value'] ?? [];
  217. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  218. //当前门店是否在设置门店中
  219. $bool = in_array($current_top_depart_id,$bt_top_depart_id);
  220. $model = SalesOrder::where('del_time',0)
  221. ->when(! $bool, function ($query) use ($current_top_depart_id) {
  222. return $query->where('top_depart_id', $current_top_depart_id);
  223. })
  224. ->select('model_type','contract_fee as total','top_depart_id');
  225. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  226. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  227. $model->where('crt_time','>=',$return[0]);
  228. $model->where('crt_time','<=',$return[1]);
  229. }
  230. $list = $model->get()->toArray();
  231. $list = $this->fillStatisticsBt($list,$bt_top_depart_id,$bool);
  232. return [true, $list];
  233. }
  234. public function fillStatisticsBt($data,$bt_top_depart_id,$bool){
  235. if(empty($data)) return $data;
  236. $return = [];
  237. foreach ($data as $value){
  238. $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  239. if($bool){
  240. if($value['model_type'] != SalesOrder::Model_type_two){
  241. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  242. $this->makeThis($return,$value,$model_type_title);
  243. }
  244. }else{
  245. $this->makeThis($return,$value,$model_type_title);
  246. }
  247. }else{
  248. $this->makeThis($return,$value,$model_type_title);
  249. }
  250. }
  251. return array_values($return);
  252. }
  253. public function makeThis(&$return,$value,$model_type_title){
  254. if(isset($return[$value['model_type']])){
  255. $total = bcadd($value['total'], $return[$value['model_type']]['total'],2);
  256. $return[$value['model_type']]['total'] = $total;
  257. }else{
  258. $return[$value['model_type']] = [
  259. 'model_type' => $value['model_type'],
  260. 'model_type_title' => $model_type_title,
  261. 'total' => $value['total'],
  262. ];
  263. }
  264. }
  265. //省 订单类型细分统计
  266. public function statisticsProvince($data,$user){
  267. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  268. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  269. //总社id
  270. $head = $user['head']['id'] ?? 0;
  271. //当前门店
  272. $current_top_depart_id = $this->getMyTopDepart($user);
  273. //销售订单类型
  274. $model_type = 0;
  275. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  276. //门店设置的指标 按照区域汇总
  277. $index_map = DepartIndex::where('del_time',0)
  278. ->pluck('param_one','top_depart_id')
  279. ->toArray();
  280. //分社所属省
  281. $depart_map = $province_map = [];
  282. $depart = Depart::where('parent_id',0)
  283. ->where('province','<>','')
  284. ->where('del_time',0)
  285. ->select('province','id')
  286. ->get()->toArray();
  287. foreach ($depart as $value){
  288. $depart_map[$value['id']] = $value['province'];
  289. $province_map[$value['province']][] = $value['id'];
  290. }
  291. $address_map = config('address');
  292. $address_map = array_column($address_map,'label','value');
  293. //省
  294. $final_address_map = [];
  295. foreach ($address_map as $key => $value){
  296. $tmp = [
  297. 'key' => $key,
  298. 'title' => $value,
  299. 'total' => 0,
  300. 'index' => 0
  301. ];
  302. $top_depart_id = $province_map[$key] ?? [];
  303. if(! empty($top_depart_id)){
  304. foreach ($top_depart_id as $depart_id){
  305. $index = $index_map[$depart_id] ?? 0;
  306. $tmp['index'] = bcadd($tmp['index'], $index,2);
  307. }
  308. }
  309. $final_address_map[] = $tmp;
  310. }
  311. //特殊的门店
  312. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  313. $bt_top_depart_id = $setting['setting_value'] ?? [];
  314. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  315. //当前门店是否在设置门店中
  316. $bool = in_array($current_top_depart_id,$bt_top_depart_id);
  317. $sale_order = SalesOrder::where("del_time",0)
  318. ->where('crt_time','>=',$return[0])
  319. ->where('crt_time','<=',$return[1])
  320. ->when(! $bool, function ($query) use ($current_top_depart_id) {
  321. return $query->where('top_depart_id', $current_top_depart_id);
  322. })
  323. ->when(! empty($model_type), function ($query) use ($model_type) {
  324. return $query->where('model_type',$model_type);
  325. })
  326. ->select('id','top_depart_id','contract_fee','model_type')
  327. ->get()->toArray();
  328. //合同
  329. $purchase_map = $sale_order_id = [];
  330. foreach ($sale_order as $value){
  331. $area = $depart_map[$value['top_depart_id']] ?? 0;
  332. if(! $area) continue;
  333. if($value['model_type'] != SalesOrder::Model_type_two){
  334. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  335. $sale_order_id[] = $value['id'];
  336. $this->makeData($purchase_map,$value,$area);
  337. }
  338. }else{
  339. $sale_order_id[] = $value['id'];
  340. $this->makeData($purchase_map,$value,$area);
  341. }
  342. }
  343. //退货的差异
  344. $returnExchange_map = [];
  345. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  346. ->where('type',ReturnExchangeOrder::Order_type)
  347. ->whereIn('data_id',array_unique($sale_order_id))
  348. ->select('top_depart_id','difference_amount')
  349. ->get()->toArray();
  350. foreach ($returnExchange as $value){
  351. $area = $depart_map[$value['top_depart_id']] ?? 0;
  352. if(! $area) continue;
  353. if(isset($returnExchange_map[$area])){
  354. $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
  355. $returnExchange_map[$area] = $total;
  356. }else{
  357. $returnExchange_map[$area] = $value['difference_amount'];
  358. }
  359. }
  360. foreach ($final_address_map as $key => $value){
  361. $purchase_tmp = $purchase_map[$value['key']] ?? 0;
  362. $return_tmp = $returnExchange_map[$value['key']] ?? 0;
  363. $final_address_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  364. }
  365. usort($final_address_map, function($a, $b) {
  366. return $b['total'] - $a['total'];
  367. });
  368. return [true, $final_address_map];
  369. }
  370. public function makeData(&$purchase_map, $value,$area){
  371. if(isset($purchase_map[$area])){
  372. $total = bcadd($purchase_map[$area], $value['contract_fee'],2);
  373. $purchase_map[$area] = $total;
  374. }else{
  375. $purchase_map[$area] = $value['contract_fee'];
  376. }
  377. }
  378. //大区 订单类型细分统计
  379. public function statisticsArea($data,$user){
  380. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  381. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  382. //总社id
  383. $head = $user['head']['id'] ?? 0;
  384. //当前门店
  385. $current_top_depart_id = $this->getMyTopDepart($user);
  386. //销售订单类型
  387. $model_type = 0;
  388. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  389. //门店设置的指标 按照区域汇总
  390. $index_map = DepartIndex::where('del_time',0)
  391. ->pluck('param_one','top_depart_id')
  392. ->toArray();
  393. //分社所属大区
  394. $depart_map = $area_map = [];
  395. $depart = Depart::where('parent_id',0)
  396. ->where('area','>',0)
  397. ->where('del_time',0)
  398. ->select('area','id')
  399. ->get()->toArray();
  400. foreach ($depart as $value){
  401. $depart_map[$value['id']] = $value['area'];
  402. $area_map[$value['area']][] = $value['id'];
  403. }
  404. //大区
  405. $area = Depart::$area;
  406. $area_map = [];
  407. foreach ($area as $key => $value){
  408. $tmp = [
  409. 'key' => $key,
  410. 'title' => $value,
  411. 'total' => 0,
  412. 'index' => 0
  413. ];
  414. $top_depart_id = $area_map[$key] ?? [];
  415. if(! empty($top_depart_id)){
  416. foreach ($top_depart_id as $depart_id){
  417. $index = $index_map[$depart_id] ?? 0;
  418. $tmp['index'] = bcadd($tmp['index'], $index,2);
  419. }
  420. }
  421. $area_map[] = $tmp;
  422. }
  423. //特殊的门店
  424. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  425. $bt_top_depart_id = $setting['setting_value'] ?? [];
  426. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  427. //当前门店是否在设置门店中
  428. $bool = in_array($current_top_depart_id,$bt_top_depart_id);
  429. $sale_order = SalesOrder::where("del_time",0)
  430. ->where('crt_time','>=',$return[0])
  431. ->where('crt_time','<=',$return[1])
  432. ->when(! $bool, function ($query) use ($current_top_depart_id) {
  433. return $query->where('top_depart_id', $current_top_depart_id);
  434. })
  435. ->when(! empty($model_type), function ($query) use ($model_type) {
  436. return $query->where('model_type',$model_type);
  437. })
  438. ->select('top_depart_id','contract_fee','model_type','id')
  439. ->get()->toArray();
  440. $purchase_map = $sale_order_id = [];
  441. foreach ($sale_order as $value){
  442. $area = $depart_map[$value['top_depart_id']] ?? 0;
  443. if(! $area) continue;
  444. if($value['model_type'] != SalesOrder::Model_type_two){
  445. if(in_array($value['top_depart_id'], $bt_top_depart_id)){
  446. $sale_order_id[] = $value['id'];
  447. $this->makeData($purchase_map,$value,$area);
  448. }
  449. }else{
  450. $sale_order_id[] = $value['id'];
  451. $this->makeData($purchase_map,$value,$area);
  452. }
  453. }
  454. //退货的差异
  455. $returnExchange_map = [];
  456. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  457. ->where('type',ReturnExchangeOrder::Order_type)
  458. ->whereIn('data_id',array_unique($sale_order_id))
  459. ->select('top_depart_id','difference_amount')
  460. ->get()->toArray();
  461. foreach ($returnExchange as $value){
  462. $area = $depart_map[$value['top_depart_id']] ?? 0;
  463. if(! $area) continue;
  464. if(isset($returnExchange_map[$area])){
  465. $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
  466. $returnExchange_map[$area] = $total;
  467. }else{
  468. $returnExchange_map[$area] = $value['difference_amount'];
  469. }
  470. }
  471. foreach ($area_map as $key => $value){
  472. $purchase_tmp = $purchase_map[$value['key']] ?? 0;
  473. $return_tmp = $returnExchange_map[$value['key']] ?? 0;
  474. $area_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  475. }
  476. usort($area_map, function($a, $b) {
  477. return $b['total'] - $a['total'];
  478. });
  479. return [true, $area_map];
  480. }
  481. // 省|大区下的门店 订货统计
  482. public function statisticsAreaDepart($data,$user){
  483. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  484. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  485. if(empty($data['area']) && empty($data['province'])) return [false, '大区或省份必须选择一个'];
  486. if(! empty($data['area']) && ! empty($data['province']))return [false, '大区或省份有且只能有一个'];
  487. if(! empty($data['area']) && ! isset(Depart::$area[$data['area']])) return [false, '该大区不存在'];
  488. $address_map = config('address');
  489. $address_map = array_column($address_map,'label','value');
  490. if(! empty($data['province']) && ! isset($address_map[$data['province']])) return [false, '该省不存在'];
  491. //总社id
  492. $head = $user['head']['id'] ?? 0;
  493. //当前门店
  494. $current_top_depart_id = $this->getMyTopDepart($user);
  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 = $this->getMyTopDepart($user);
  584. //特殊的门店
  585. $setting = Setting::where('setting_name','bt_top_depart_id')->first();
  586. $bt_top_depart_id = $setting['setting_value'] ?? [];
  587. $bt_top_depart_id = json_decode($bt_top_depart_id,true);
  588. //当前门店是否在设置门店中
  589. $bool = in_array($current_top_depart_id,$bt_top_depart_id);
  590. //销售订单类型
  591. $model_type = 0;
  592. if(! empty($data['model_type'])) $model_type = $data['model_type'];
  593. //时间 =》 钱
  594. $purchase_map1 = $this->getTime($return);
  595. //产品 =》 数量
  596. $purchase_category_map = $purchase_map = [];
  597. $sale_order = SalesOrder::where("del_time",0)
  598. ->where('top_depart_id',$data['top_depart_id'])
  599. ->where('crt_time','>=',$return[0])
  600. ->where('crt_time','<=',$return[1])
  601. ->when(! $bool, function ($query) use ($current_top_depart_id) {
  602. return $query->where('top_depart_id', $current_top_depart_id);
  603. })
  604. ->when(! empty($model_type), function ($query) use ($model_type) {
  605. return $query->where('model_type',$model_type);
  606. })
  607. ->select('id','crt_time')
  608. ->get()->toArray();
  609. $purchase_for_time = array_column($sale_order,'crt_time','id');
  610. $sale_order_id = array_column($sale_order,'id');
  611. $purchase_product = SalesOrderProductInfo::where("del_time",0)
  612. ->whereIn('sales_order_id',$sale_order_id)
  613. ->select('sales_order_id','product_id','number','price','final_amount','sports_bag_id')
  614. ->get()->toArray();
  615. //退换货
  616. list($returnExchange_map,$returnExchange_map_2) = $this->returnExchange($data,$sale_order_id);
  617. //产品
  618. $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys($returnExchange_map,'product_id'))))
  619. ->select('id','product_category','title','code')
  620. ->get()->toArray();
  621. $product_list_map = array_column($product_list,null,'id');
  622. $category_return = ProductCategory::where('del_time',0)
  623. ->where('parent_id',0)
  624. ->pluck('title','id')
  625. ->toArray();
  626. foreach ($purchase_product as $value){
  627. if($value['sports_bag_id'] > 0){
  628. $money = $value['final_amount'];
  629. }elseif($value['final_amount'] > 0){
  630. $money = $value['final_amount'];
  631. }else{
  632. $money = bcmul($value['price'],$value['number'],2);
  633. }
  634. $crt_time = $purchase_for_time[$value['sales_order_id']];
  635. $crt_time = date("Y-m-d",$crt_time);
  636. //产品信息
  637. $product_tmp = $product_list_map[$value['product_id']] ?? [];
  638. $time_money = 0;
  639. if(isset($returnExchange_map_2[$crt_time])) {
  640. $time_money = $returnExchange_map_2[$crt_time] ?? 0;
  641. unset($returnExchange_map_2[$crt_time]);
  642. }
  643. //钱
  644. if(isset($purchase_map1[$crt_time])){
  645. $time_total = bcadd($purchase_map1[$crt_time]['total'],$money,2);
  646. $time_total = bcsub($time_total,$time_money,2);
  647. $purchase_map1[$crt_time]['total'] = $time_total;
  648. }
  649. $return_number = $return_total = 0;
  650. $return_category_number = $return_category_total = 0;
  651. if(isset($returnExchange_map[$value['product_id']])){
  652. $tmp = $returnExchange_map[$value['product_id']];
  653. $return_number = bcsub($value['number'], $tmp['number'],2);
  654. $return_total = bcsub($money, $tmp['total'],2);
  655. $return_category_number = $return_number;
  656. $return_category_total = $return_total;
  657. }
  658. //-------根据产品
  659. //数量
  660. if(isset($purchase_map[$value['product_id']])){
  661. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  662. $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
  663. $purchase_map[$value['product_id']]['number'] = $tmp_number;
  664. $purchase_map[$value['product_id']]['total'] = $tmp_money;
  665. }else{
  666. //减去退换货
  667. $number = bcsub($value['number'], $return_number,2);
  668. $total = bcsub($money, $return_total,2);
  669. $purchase_map[$value['product_id']] = [
  670. 'title' => $product_tmp['title'] . "(" . $product_tmp['code'] .")",
  671. 'number' => $number,
  672. 'total' => $total
  673. ];
  674. }
  675. //-------根据产品
  676. //-------根据产品大类
  677. //数量
  678. $category_tmp = json_decode($product_tmp['product_category']);
  679. $category_tmp = min($category_tmp);
  680. //退换货
  681. $number_2 = bcsub($value['number'], $return_category_number,2);
  682. $total_2 = bcsub($money, $return_category_total,2);
  683. if(isset($purchase_category_map[$category_tmp])){
  684. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  685. $tmp_number = bcsub($tmp_number,$number_2,2);
  686. $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
  687. $tmp_money = bcsub($tmp_money,$total_2,2);
  688. $purchase_category_map[$category_tmp]['number'] = $tmp_number;
  689. $purchase_category_map[$category_tmp]['total'] = $tmp_money;
  690. }else{
  691. $num = bcsub($value['number'],$number_2,2);
  692. $tol = bcsub($money,$total_2,2);
  693. $purchase_category_map[$category_tmp] = [
  694. 'number' => $num,
  695. 'title' => $category_return[$category_tmp] ?? "",
  696. 'total' => $tol
  697. ];
  698. }
  699. }
  700. return [true, ['money' => array_values($purchase_map1), 'product_num' => array_values($purchase_map), 'category_num' => array_values($purchase_category_map)]];
  701. }
  702. public function getTime($data){
  703. $startTimestamp = $data[0]; // 起始时间戳
  704. $endTimestamp = $data[1]; // 结束时间戳
  705. // 创建 DateTime 对象
  706. $startDate = new \DateTime();
  707. $endDate = new \DateTime();
  708. $startDate->setTimestamp($startTimestamp);
  709. $endDate->setTimestamp($endTimestamp);
  710. // 创建 DatePeriod 对象
  711. $interval = new \DateInterval('P1D'); // 每天的间隔
  712. $dateRange = new \DatePeriod($startDate, $interval, $endDate);
  713. // 遍历日期范围并输出每个日期
  714. $return = [];
  715. foreach ($dateRange as $date) {
  716. $day = $date->format('Y-m-d');
  717. $return[$day] = [
  718. 'day' => $day,
  719. 'total' => 0
  720. ];
  721. }
  722. return $return;
  723. }
  724. public function returnExchange($data,$sale_order_id){
  725. $returnExchange_map = $returnExchange_map_2 = [];
  726. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  727. ->where('type',ReturnExchangeOrder::Order_type)
  728. ->whereIn('data_id', $sale_order_id)
  729. ->select('id')
  730. ->get()->toArray();
  731. $returnExchange_product = ReturnExchangeOrderProductInfo::where("del_time",0)
  732. ->whereIn('return_exchange_id',array_column($returnExchange,'id'))
  733. ->select('return_exchange_id','product_id','number','return_exchange_price as price','crt_time')
  734. ->get()->toArray();
  735. foreach ($returnExchange_product as $value){
  736. $money = bcmul($value['price'],$value['number'],2);
  737. if(isset($returnExchange_map[$value['product_id']])){
  738. $tmp_money = bcadd($returnExchange_map[$value['product_id']]['total'], $money,2);
  739. $tmp_number = bcadd($returnExchange_map[$value['product_id']]['number'], $value['number'],2);
  740. $returnExchange_map[$value['product_id']] = [
  741. 'number' => $tmp_number,
  742. 'total' => $tmp_money
  743. ];
  744. }else{
  745. $returnExchange_map[$value['product_id']] = [
  746. 'number' => $value['number'],
  747. 'total' => $money
  748. ];
  749. }
  750. $crt_time = date("Y-m-d",$value['crt_time']);
  751. if(isset($returnExchange_map_2[$crt_time])){
  752. $tmp_money_2 = bcadd($returnExchange_map_2[$crt_time], $money,2);
  753. $returnExchange_map_2[$crt_time] = $tmp_money_2;
  754. }else{
  755. $returnExchange_map_2[$crt_time] = $money;
  756. }
  757. }
  758. return [$returnExchange_map,$returnExchange_map_2];
  759. }
  760. //线下订单
  761. public function statisticsModelTypeOne($data,$user){
  762. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  763. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  764. if(empty($data['x'])) return [false, '请选择X轴维度'];
  765. //总社id
  766. $head = $user['head']['id'] ?? 0;
  767. //当前门店
  768. $current_top_depart_id = $this->getMyTopDepart($user);
  769. //线下 销售订单类型
  770. $model_type = SalesOrder::Model_type_one;
  771. $sale_order = SalesOrder::where("del_time",0)
  772. ->where('crt_time','>=',$return[0])
  773. ->where('crt_time','<=',$return[1])
  774. ->where('model_type',$model_type)
  775. ->when(! empty($current_top_depart_id) && $current_top_depart_id != $head, function ($query) use ($current_top_depart_id) {
  776. return $query->where('top_depart_id', $current_top_depart_id);
  777. })
  778. ->select('id','top_depart_id','contract_fee','crt_id')
  779. ->get()->toArray();
  780. //合同
  781. $sale_order_id = array_column($sale_order,'id');
  782. $statistics = [];
  783. if($data['x'] == "crt_id"){
  784. $emp = Employee::whereIn('id', array_unique(array_column($sale_order,'crt_id')))
  785. ->pluck('emp_name','id')
  786. ->toArray();
  787. //退货的差异
  788. $returnExchange_map = [];
  789. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  790. ->where('type',ReturnExchangeOrder::Order_type)
  791. ->whereIn('data_id',array_unique($sale_order_id))
  792. ->select('top_depart_id','difference_amount','data_id')
  793. ->get()->toArray();
  794. foreach ($returnExchange as $value){
  795. if(isset($returnExchange_map[$value['data_id']])){
  796. $total = bcadd($returnExchange_map[$value['data_id']], $value['difference_amount'],2);
  797. $returnExchange_map[$value['data_id']] = $total;
  798. }else{
  799. $returnExchange_map[$value['data_id']] = $value['difference_amount'];
  800. }
  801. }
  802. foreach ($sale_order as $value){
  803. $crt_name = $emp[$value['crt_id']] ?? "";
  804. if(! $crt_name) continue;
  805. $return_money = $returnExchange_map[$value['id']] ?? 0;
  806. $tmp = bcsub($value['contract_fee'], $return_money,2);
  807. if(isset($statistics[$value['crt_id']])){
  808. $total = bcadd($tmp,$statistics[$value['crt_id']]['total'],2);
  809. $statistics[$value['crt_id']]['total'] = $total;
  810. }else{
  811. $statistics[$value['crt_id']] = [
  812. 'title' => $crt_name,
  813. 'total' => $tmp,
  814. ];
  815. }
  816. }
  817. }elseif($data['x'] == "product"){
  818. $sales_product = SalesOrderProductInfo::where('del_time',0)
  819. ->whereIn('sales_order_id',$sale_order_id)
  820. ->select('product_id','number','retail_price','price')
  821. ->get()->toArray();
  822. $product = Product::whereIn('id',array_unique(array_column($sales_product,'product_id')))
  823. ->select('id','title','code')
  824. ->get()->toArray();
  825. $product_map = [];
  826. foreach ($product as $value){
  827. $product_map[$value['id']] = $value['title'] . "(" . $value['code']. ")";
  828. }
  829. //退货的差异
  830. $returnExchange_map = [];
  831. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  832. ->where('type',ReturnExchangeOrder::Order_type)
  833. ->whereIn('data_id',array_unique($sale_order_id))
  834. ->select('id')
  835. ->get()->toArray();
  836. $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
  837. ->whereIn('return_exchange_id', array_column($returnExchange,'id'))
  838. ->select('product_id','number','return_exchange_price')
  839. ->get()->toArray();
  840. foreach ($return_product as $value){
  841. $tmp = bcmul($value['number'],$value['return_exchange_price'],2);
  842. if(isset($returnExchange_map[$value['product_id']])){
  843. $total = bcadd($returnExchange_map[$value['product_id']], $tmp,2);
  844. $returnExchange_map[$value['product_id']] = $total;
  845. }else{
  846. $returnExchange_map[$value['product_id']] = $tmp;
  847. }
  848. }
  849. foreach ($sales_product as $value){
  850. $product_tmp = $product_map[$value['product_id']] ?? "";
  851. if(! $product_tmp) continue;
  852. if($value['price'] > 0){
  853. $tmp = bcmul($value['price'], $value['number'],2);
  854. }else{
  855. $tmp = bcmul($value['retail_price'], $value['number'],2);
  856. }
  857. $return_tmp = 0;
  858. if(isset($returnExchange_map[$value['product_id']])){
  859. $return_tmp = $returnExchange_map[$value['product_id']] ?? 0;
  860. unset($returnExchange_map[$value['product_id']]);
  861. }
  862. $tmp = bcsub($tmp,$return_tmp,2);
  863. if(isset($statistics[$value['product_id']])){
  864. $total = bcadd($tmp,$statistics[$value['product_id']]['total'],2);
  865. $statistics[$value['product_id']]['total'] = $total;
  866. }else{
  867. $statistics[$value['product_id']] = [
  868. 'title' => $product_tmp,
  869. 'total' => $tmp,
  870. ];
  871. }
  872. }
  873. }
  874. foreach ($statistics as $key => $value){
  875. if(floatval($value['total']) <= 0) unset($statistics[$key]);
  876. }
  877. $statistics = array_values($statistics);
  878. usort($statistics, function($a, $b) {
  879. return $b['total'] - $a['total'];
  880. });
  881. return [true, $statistics];
  882. }
  883. //线上订单
  884. public function statisticsModelTypeFour($data,$user){
  885. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  886. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  887. if(empty($data['x'])) return [false, '请选择X轴维度'];
  888. //总社id
  889. $head = $user['head']['id'] ?? 0;
  890. //当前门店
  891. $current_top_depart_id = $this->getMyTopDepart($user);
  892. //线下 销售订单类型
  893. $model_type = SalesOrder::Model_type_four;
  894. $sale_order = SalesOrder::where("del_time",0)
  895. ->where('crt_time','>=',$return[0])
  896. ->where('crt_time','<=',$return[1])
  897. ->where('model_type',$model_type)
  898. ->when(! empty($current_top_depart_id) && $current_top_depart_id != $head, function ($query) use ($current_top_depart_id) {
  899. return $query->where('top_depart_id', $current_top_depart_id);
  900. })
  901. ->select('id','top_depart_id','contract_fee','plat_type')
  902. ->get()->toArray();
  903. //合同
  904. $sale_order_id = array_column($sale_order,'id');
  905. $statistics = [];
  906. if($data['x'] == "order_from"){
  907. $array = array_unique(array_column($sale_order,'plat_type'));
  908. $basic_map = BasicType::whereIn('id',$array)
  909. ->pluck('title','id')
  910. ->toArray();
  911. //退货的差异
  912. $returnExchange_map = [];
  913. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  914. ->where('type',ReturnExchangeOrder::Order_type)
  915. ->whereIn('data_id',array_unique($sale_order_id))
  916. ->select('top_depart_id','difference_amount','data_id')
  917. ->get()->toArray();
  918. foreach ($returnExchange as $value){
  919. if(isset($returnExchange_map[$value['data_id']])){
  920. $total = bcadd($returnExchange_map[$value['data_id']], $value['difference_amount'],2);
  921. $returnExchange_map[$value['data_id']] = $total;
  922. }else{
  923. $returnExchange_map[$value['data_id']] = $value['difference_amount'];
  924. }
  925. }
  926. foreach ($sale_order as $value){
  927. $plat_type = $basic_map[$value['plat_type']] ?? "";
  928. if(! $plat_type) continue;
  929. $return_money = $returnExchange_map[$value['id']] ?? 0;
  930. $tmp = bcsub($value['contract_fee'], $return_money,2);
  931. if(isset($statistics[$value['plat_type']])){
  932. $total = bcadd($tmp,$statistics[$value['plat_type']]['total'],2);
  933. $statistics[$value['plat_type']]['total'] = $total;
  934. }else{
  935. $statistics[$value['plat_type']] = [
  936. 'title' => $plat_type,
  937. 'total' => $tmp,
  938. ];
  939. }
  940. }
  941. }elseif($data['x'] == "product"){
  942. $sales_product = SalesOrderProductInfo::where('del_time',0)
  943. ->whereIn('sales_order_id',$sale_order_id)
  944. ->select('product_id','number','retail_price','price')
  945. ->get()->toArray();
  946. $product = Product::whereIn('id',array_unique(array_column($sales_product,'product_id')))
  947. ->select('id','title','code')
  948. ->get()->toArray();
  949. $product_map = [];
  950. foreach ($product as $value){
  951. $product_map[$value['id']] = $value['title'] . "(" . $value['code']. ")";
  952. }
  953. //退货的差异
  954. $returnExchange_map = [];
  955. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  956. ->where('type',ReturnExchangeOrder::Order_type)
  957. ->whereIn('data_id',array_unique($sale_order_id))
  958. ->select('id')
  959. ->get()->toArray();
  960. $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
  961. ->whereIn('return_exchange_id', array_column($returnExchange,'id'))
  962. ->select('product_id','number','return_exchange_price')
  963. ->get()->toArray();
  964. foreach ($return_product as $value){
  965. $tmp = bcmul($value['number'],$value['return_exchange_price'],2);
  966. if(isset($returnExchange_map[$value['product_id']])){
  967. $total = bcadd($returnExchange_map[$value['product_id']], $tmp,2);
  968. $returnExchange_map[$value['product_id']] = $total;
  969. }else{
  970. $returnExchange_map[$value['product_id']] = $tmp;
  971. }
  972. }
  973. foreach ($sales_product as $value){
  974. $product_tmp = $product_map[$value['product_id']] ?? "";
  975. if(! $product_tmp) continue;
  976. if($value['price'] > 0){
  977. $tmp = bcmul($value['price'], $value['number'],2);
  978. }else{
  979. $tmp = bcmul($value['retail_price'], $value['number'],2);
  980. }
  981. $return_tmp = 0;
  982. if(isset($returnExchange_map[$value['product_id']])){
  983. $return_tmp = $returnExchange_map[$value['product_id']] ?? 0;
  984. unset($returnExchange_map[$value['product_id']]);
  985. }
  986. $tmp = bcsub($tmp,$return_tmp,2);
  987. if(isset($statistics[$value['product_id']])){
  988. $total = bcadd($tmp,$statistics[$value['product_id']]['total'],2);
  989. $statistics[$value['product_id']]['total'] = $total;
  990. }else{
  991. $statistics[$value['product_id']] = [
  992. 'title' => $product_tmp,
  993. 'total' => $tmp,
  994. ];
  995. }
  996. }
  997. }
  998. foreach ($statistics as $key => $value){
  999. if(floatval($value['total']) <= 0) unset($statistics[$key]);
  1000. }
  1001. $statistics = array_values($statistics);
  1002. usort($statistics, function($a, $b) {
  1003. return $b['total'] - $a['total'];
  1004. });
  1005. return [true, $statistics];
  1006. }
  1007. public function statisticsJc($data,$user){
  1008. $model = Product::ProductClear2($user,$data);
  1009. $model = $model->where('del_time',0)
  1010. ->select('title','id','code','depart_id','top_depart_id','product_attribute')
  1011. ->orderby('product_attribute', 'desc')
  1012. ->orderby('id', 'desc');
  1013. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  1014. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  1015. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  1016. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  1017. if(! empty($data['product_category'])) {
  1018. $product_category = ProductCategory::where('del_time',0)
  1019. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  1020. ->select('id')
  1021. ->get()->toArray();
  1022. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  1023. }
  1024. $list = $this->limit($model,'',$data);
  1025. $list = $this->fillData($list,$user,$data);
  1026. return [true, $list];
  1027. }
  1028. public function fillData($data, $user, $search){
  1029. if(empty($data['data'])) return $data;
  1030. //产品
  1031. $product = array_column($data['data'],'id');
  1032. //本月入库 本月出库
  1033. list($in, $out) = $this->getThisMonthData($product,$user,$search);
  1034. //上月结存 汇总这个月之前的出入
  1035. list($last_in,$last_out) = $this->getLastMonthBalance($product,$user,$search);
  1036. foreach ($data['data'] as $key => $value){
  1037. $last_in_tmp = $last_in[$value['id']] ?? [];
  1038. $last_in_money = $last_in_tmp['total'] ?? 0;//本月之前入的金额
  1039. $last_in_number = $last_in_tmp['number'] ?? 0;//本月之前入的数量
  1040. $last_out_tmp = $last_out[$value['id']] ?? [];
  1041. $last_out_money = $last_out_tmp['total'] ?? 0;//本月之前出的金额
  1042. $last_out_number = $last_out_tmp['number'] ?? 0;//本月之前出的数量
  1043. //上月结存
  1044. $last_jc_money = bcsub($last_in_money,$last_out_money,2);//结存金额
  1045. $last_jc_number = bcsub($last_in_number,$last_out_number,2);//结存数量
  1046. $last_jc_price = floatval($last_jc_number) ? bcdiv($last_jc_money,$last_jc_number,2) : 0;//结存单价
  1047. $data['data'][$key]['last_jc_money'] = $last_jc_money;
  1048. $data['data'][$key]['last_jc_number'] = $last_jc_number;
  1049. $data['data'][$key]['last_jc_price'] = $last_jc_price;
  1050. //本月入库
  1051. $this_in_tmp = $in[$value['id']] ?? [];
  1052. $this_in_money = $this_in_tmp['total'] ?? 0;//本月入的金额
  1053. $this_in_number = $this_in_tmp['number'] ?? 0;//本月入的数量
  1054. $this_in_price = floatval($this_in_number) ? bcdiv($this_in_money,$this_in_number,2) : 0;//本月入单价
  1055. $data['data'][$key]['this_in_money'] = $this_in_money;
  1056. $data['data'][$key]['this_in_number'] = $this_in_number;
  1057. $data['data'][$key]['this_in_price'] = $this_in_price;
  1058. //本月出库
  1059. $this_out_tmp = $out[$value['id']] ?? [];
  1060. $this_out_money = $this_out_tmp['total'] ?? 0;//本月出的金额
  1061. $this_out_number = $this_out_tmp['number'] ?? 0;//本月出的数量
  1062. //单价= (上月结存金额+本月入库金额) /上月结存数量+本月入库数量
  1063. $amount = bcadd($last_jc_money, $this_in_money,2);
  1064. $number = bcadd($last_jc_number, $this_in_number,2);
  1065. $this_out_price = floatval($number) ? bcdiv($amount,$number,2) : 0;//本月出单价
  1066. $data['data'][$key]['this_out_money'] = $this_out_money;
  1067. $data['data'][$key]['this_out_number'] = $this_out_number;
  1068. $data['data'][$key]['this_out_price'] = $this_out_price;
  1069. //本月结存
  1070. //本月结存 数量/金额=本月入库+上月结存+(-本月出库)
  1071. $this_jc_money = bcsub(bcadd($this_in_money,$last_jc_money,2),$this_out_money,2);
  1072. $this_jc_number = bcadd(bcadd($this_in_number,$last_jc_number,2),$this_out_number,2);
  1073. $this_jc_price = floatval($this_jc_number) ? bcdiv($this_jc_money,$this_jc_number,2) : 0;//本月结存单价
  1074. $data['data'][$key]['this_jc_money'] = $this_jc_money;
  1075. $data['data'][$key]['this_jc_number'] = $this_jc_number;
  1076. $data['data'][$key]['this_jc_price'] = $this_jc_price;
  1077. }
  1078. return $data;
  1079. }
  1080. //本月入库 出库(库存流水)
  1081. public function getThisMonthData($product = [], $user = [], $search = []){
  1082. $in = $out = [];
  1083. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  1084. $endStamp = strtotime(date("Y-m-t 23:59:59"));
  1085. //本月出和入的数据
  1086. $model = InOutRecord::TopClear($user,$search);
  1087. $list = $model->where('del_time',0)
  1088. ->where('crt_time','>=',$startStamp)
  1089. ->where('crt_time','<=',$endStamp)
  1090. ->whereIn('product_id',$product)
  1091. ->select('product_id','number','price')
  1092. ->get()->toArray();
  1093. foreach ($list as $value){
  1094. if($value['number'] >= 0){
  1095. $tmp_total = bcmul($value['number'], $value['price'], 2);
  1096. if(isset($in[$value['product_id']])){
  1097. $number = bcadd($in[$value['product_id']]['number'], $value['number'],2);
  1098. $total = bcadd($in[$value['product_id']]['total'], $tmp_total,2);
  1099. $in[$value['product_id']]['number'] = $number;
  1100. $in[$value['product_id']]['total'] = $total;
  1101. }else{
  1102. $in[$value['product_id']] = [
  1103. 'number' => $value['number'],
  1104. 'total' => $tmp_total,
  1105. ];
  1106. }
  1107. }else{
  1108. $number = abs($value['number']);
  1109. $tmp_total = bcmul($number, $value['price'], 2);
  1110. if(isset($out[$value['product_id']])){
  1111. $number = bcadd($out[$value['product_id']]['number'], $number,2);
  1112. $total = bcadd($out[$value['product_id']]['total'], $tmp_total,2);
  1113. $out[$value['product_id']]['number'] = $number;
  1114. $out[$value['product_id']]['total'] = $total;
  1115. }else{
  1116. $out[$value['product_id']] = [
  1117. 'number' => $number,
  1118. 'total' => $tmp_total,
  1119. ];
  1120. }
  1121. }
  1122. }
  1123. return [$in, $out];
  1124. }
  1125. //上月结存(汇总这个月之前的出入)(库存流水)
  1126. public function getLastMonthBalance($product = [], $user = [], $search = []){
  1127. //用户提交的上月结存
  1128. $lastData = $this->getLastMonthJc($product,$user,$search);
  1129. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  1130. $model = InOutRecord::TopClear($user,$search);
  1131. $list = $model->where('del_time',0)
  1132. ->where('crt_time','<',$startStamp)
  1133. ->whereIn('product_id',$product)
  1134. ->select('product_id','number','price','top_depart_id')
  1135. ->get()->toArray();
  1136. $map = [];
  1137. foreach ($list as $val){
  1138. $map[$val['product_id'] . $val['top_depart_id']] = $val;
  1139. }
  1140. //先结存表
  1141. $in = $out = [];
  1142. foreach ($lastData as $value){
  1143. $key = $value['product_id'] . '|' . $value['top_depart_id'];
  1144. if($value['number'] >= 0){
  1145. if(isset($in[$key])){
  1146. $number = bcadd($in[$key]['number'], $value['number'],2);
  1147. $total = bcadd($in[$key]['total'], $value['total'],2);
  1148. $in[$key]['number'] = $number;
  1149. $in[$key]['total'] = $total;
  1150. }else{
  1151. $in[$key] = [
  1152. 'number' => $value['number'],
  1153. 'total' => $value['total'],
  1154. ];
  1155. }
  1156. }else{
  1157. $number = abs($value['number']);
  1158. $total = abs($value['total']);
  1159. if(isset($out[$key])){
  1160. $number = bcadd($out[$key]['number'], $number,2);
  1161. $total = bcadd($out[$key]['total'], $total,2);
  1162. $out[$key]['number'] = $number;
  1163. $out[$key]['total'] = $total;
  1164. }else{
  1165. $out[$key] = [
  1166. 'number' => $number,
  1167. 'total' => $total,
  1168. ];
  1169. }
  1170. }
  1171. }
  1172. //后库存流水
  1173. foreach ($list as $value){
  1174. $key = $value['product_id'] . '|' . $value['top_depart_id'];
  1175. if($value['number'] >= 0){
  1176. if(isset($in[$key])) continue;
  1177. $tmp_total = bcmul($value['number'], $value['price'], 2);
  1178. if(isset($in[$value['product_id']])){
  1179. $number = bcadd($in[$value['product_id']]['number'], $value['number'],2);
  1180. $total = bcadd($in[$value['product_id']]['total'], $tmp_total,2);
  1181. $in[$value['product_id']]['number'] = $number;
  1182. $in[$value['product_id']]['total'] = $total;
  1183. }else{
  1184. $in[$value['product_id']] = [
  1185. 'number' => $value['number'],
  1186. 'total' => $tmp_total,
  1187. ];
  1188. }
  1189. }else{
  1190. if(isset($out[$key])) continue;
  1191. $number = abs($value['number']);
  1192. $tmp_total = bcmul($number, $value['price'], 2);
  1193. if(isset($out[$value['product_id']])){
  1194. $number = bcadd($out[$value['product_id']]['number'], $number,2);
  1195. $total = bcadd($out[$value['product_id']]['total'], $tmp_total,2);
  1196. $out[$value['product_id']]['number'] = $number;
  1197. $out[$value['product_id']]['total'] = $total;
  1198. }else{
  1199. $out[$value['product_id']] = [
  1200. 'number' => $number,
  1201. 'total' => $tmp_total,
  1202. ];
  1203. }
  1204. }
  1205. }
  1206. //两个数组组合过滤
  1207. $new_in = $new_out = [];
  1208. foreach ($in as $key => $value){
  1209. $tmp = explode('|', $key);
  1210. $product_id = $tmp[0];
  1211. if(isset($new_in[$product_id])){
  1212. $number = bcadd($new_in[$product_id]['number'], $value['number'],2);
  1213. $total = bcadd($new_in[$product_id]['total'], $value['total'],2);
  1214. $new_in[$product_id] = [
  1215. 'number' => $number,
  1216. 'total' => $total,
  1217. ];
  1218. }else{
  1219. $new_in[$product_id] = [
  1220. 'number' => $value['number'],
  1221. 'total' => $value['total'],
  1222. ];
  1223. }
  1224. }
  1225. foreach ($out as $key => $value){
  1226. $tmp = explode('|', $key);
  1227. $product_id = $tmp[0];
  1228. if(isset($new_out[$product_id])){
  1229. $number = bcadd($new_out[$product_id]['number'], $value['number'],2);
  1230. $total = bcadd($new_out[$product_id]['total'], $value['total'],2);
  1231. $new_out[$product_id] = [
  1232. 'number' => $number,
  1233. 'total' => $total,
  1234. ];
  1235. }else{
  1236. $new_out[$product_id] = [
  1237. 'number' => $value['number'],
  1238. 'total' => $value['total'],
  1239. ];
  1240. }
  1241. }
  1242. return [$new_in, $new_out];
  1243. }
  1244. public function getLastMonthJc($product = [], $user = [], $search = []){
  1245. // 如果存在上月结存数据则使用这个
  1246. $top_depart_id = 0;
  1247. if(! empty($search['top_depart_id'])) $top_depart_id = $search['top_depart_id'];
  1248. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  1249. $result = LastJc::where('del_time',0)
  1250. ->whereIn('product_id',$product)
  1251. ->where('time','<',$startStamp)
  1252. ->when(! empty($top_depart_id), function ($query) use ($top_depart_id) {
  1253. return $query->where('top_depart_id',$top_depart_id);
  1254. })
  1255. ->select('product_id','top_depart_id','total','number','price')
  1256. ->orderBy('time','desc')
  1257. ->get()->toArray();
  1258. $return = [];
  1259. $tmp = [];
  1260. foreach ($result as $value){
  1261. $key = $value['product_id'] . $value['top_depart_id'];
  1262. if(in_array($key, $tmp)) continue;
  1263. $return[] = $value;
  1264. $tmp[] = $key;
  1265. }
  1266. return $return;
  1267. }
  1268. //本月入库(单据)暂时没用
  1269. public function getThisMonthIn1($product = [], $user = [], $search = []){
  1270. $return = [];
  1271. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  1272. $endStamp = strtotime(date("Y-m-t 23:59:59"));
  1273. //本月采购单
  1274. $model = PurchaseOrder::Clear($user,$search);
  1275. $list = $model->where('del_time',0)
  1276. ->where('state', PurchaseOrder::STATE_Four)
  1277. ->where('crt_time','>=',$startStamp)
  1278. ->where('crt_time','<=',$endStamp)
  1279. ->select('id')
  1280. ->get()->toArray();
  1281. if(empty($list)) return $return;
  1282. //本月采购产品
  1283. $purchase_product_array = [];
  1284. $purchase_product = PurchaseOrderInfo::where('del_time',0)
  1285. ->whereIn('product_id',$product)
  1286. ->whereIn('purchase_order_id',array_column($list,'id'))
  1287. ->select('product_id','number','price')
  1288. ->get()->toArray();
  1289. foreach ($purchase_product as $value){
  1290. $total = bcmul($value['number'],$value['price'],2);
  1291. if(isset($purchase_product_array[$value['product_id']])){
  1292. $purchase_product_array[$value['product_id']]['number'] += $value['number'];
  1293. $total_tmp = bcadd($purchase_product_array[$value['product_id']]['total'], $total, 2);
  1294. $purchase_product_array[$value['product_id']]['total'] = $total_tmp;
  1295. }else{
  1296. $purchase_product_array[$value['product_id']] = [
  1297. 'number' => $value['number'],
  1298. 'total' => $total,
  1299. ];
  1300. }
  1301. }
  1302. //本月退货(采购)
  1303. $model2 = ReturnExchangeOrder::Clear($user, $search);
  1304. $return_list = $model2->where('del_time',0)
  1305. ->where('state', ReturnExchangeOrder::State_two)
  1306. ->where('type',ReturnExchangeOrder::Order_type2)
  1307. ->where('crt_time','>=',$startStamp)
  1308. ->where('crt_time','<=',$endStamp)
  1309. ->select('id')
  1310. ->get()->toArray();
  1311. //本月退货产品
  1312. $return_product_array = [];
  1313. if(! empty($return_list)){
  1314. $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
  1315. ->where('return_exchange_id', array_column($return_list, 'id'))
  1316. ->whereIn('product_id',$product)
  1317. ->where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
  1318. ->select('product_id','number','return_or_exchange')
  1319. ->get()->toArray();
  1320. foreach ($return_product as $value){
  1321. $total = bcmul($value['number'],$value['return_or_exchange'],2);
  1322. if(isset($return_product_array[$value['product_id']])){
  1323. $return_product_array[$value['product_id']]['number'] += $value['number'];
  1324. $total_tmp = bcadd($return_product_array[$value['product_id']]['total'], $total, 2);
  1325. $return_product_array[$value['product_id']]['total'] = $total_tmp;
  1326. }else{
  1327. $return_product_array[$value['product_id']] = [
  1328. 'number' => $value['number'],
  1329. 'total' => $total,
  1330. ];
  1331. }
  1332. }
  1333. }
  1334. foreach ($return_product_array as $p => $n){
  1335. $number_tmp = -$n['number'];
  1336. $total_tmp = -$n['total'];
  1337. $purchase_product_tmp = $purchase_product_array[$p] ?? [];
  1338. if(empty($purchase_product_tmp)){
  1339. $purchase_product_array[$p] = [
  1340. 'number' => $number_tmp,
  1341. 'total' => $total_tmp,
  1342. ];
  1343. }else{
  1344. $purchase_product_array[$p]['number'] += $number_tmp;
  1345. $total_tmp2 = bcadd($purchase_product_array[$p]['total'], $total_tmp, 2);
  1346. $purchase_product_array[$p]['total'] += $total_tmp2;
  1347. }
  1348. }
  1349. return $purchase_product_array;
  1350. }
  1351. //新的进销存统计通用搜索底层抽象
  1352. public function statisticsJcNewCommonOrigin($data,$user,$field = []){
  1353. if(! empty($data['count_month'])) {
  1354. $startStamp = $this->changeDateToDate($data['count_month']);
  1355. }else{
  1356. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  1357. }
  1358. $endTimeStamp = strtotime('first day of next month', $startStamp) - 1;
  1359. //-------- 结存数量汇总 ---------- //
  1360. //上月结存
  1361. $last_month_stock = "SUM(CASE WHEN crt_time < $startStamp THEN number ELSE 0 END)";
  1362. //本月入库
  1363. $this_month_in = "SUM(CASE WHEN crt_time >= $startStamp AND crt_time <= $endTimeStamp AND number > 0 THEN number ELSE 0 END)";
  1364. //本月出库
  1365. $this_month_out = "SUM(CASE WHEN crt_time >= $startStamp AND crt_time <= $endTimeStamp AND number < 0 THEN number ELSE 0 END)";
  1366. //本月结存
  1367. $this_month_stock = "($last_month_stock + $this_month_in + $this_month_out)";
  1368. //-------- 结存数量汇总 ---------- //
  1369. //-------- 结存金额汇总 ---------- //
  1370. $order_type = ProductAdjustment::prefix;
  1371. //上月结存
  1372. $last_month_stock_m = "ROUND(
  1373. SUM(
  1374. CASE
  1375. WHEN crt_time < $startStamp and number <> 0 THEN (number * price)
  1376. WHEN crt_time < $startStamp and number = 0 and order_type = '$order_type' THEN price
  1377. ELSE 0
  1378. END
  1379. ),
  1380. 2)";
  1381. //本月入库
  1382. $this_month_in_m = "ROUND(
  1383. SUM(
  1384. CASE
  1385. WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number > 0 THEN (number * price)
  1386. WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number = 0 and price >= 0 and order_type = '$order_type' THEN price
  1387. ELSE 0
  1388. END
  1389. ),
  1390. 2)";
  1391. //本月出库
  1392. $this_month_out_m = "ROUND(
  1393. SUM(
  1394. CASE
  1395. WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number < 0 THEN (number * price)
  1396. WHEN crt_time >= $startStamp and crt_time <= $endTimeStamp and number = 0 and price < 0 and order_type = '$order_type' THEN price
  1397. ELSE 0
  1398. END
  1399. ),
  1400. 2)";
  1401. //上月结存
  1402. // $last_month_stock_m = "ROUND(SUM(CASE WHEN crt_time < $startStamp THEN (number * price) ELSE 0 END), 2)";
  1403. // //本月入库
  1404. // $this_month_in_m = "ROUND(SUM(CASE WHEN crt_time >= $startStamp AND number > 0 THEN (number * price) ELSE 0 END), 2)";
  1405. // //本月出库
  1406. // $this_month_out_m = "ROUND(SUM(CASE WHEN crt_time >= $startStamp AND number < 0 THEN (number * price) ELSE 0 END), 2)";
  1407. //-------- 结存金额汇总 ---------- //
  1408. if(empty($field)){
  1409. $field = ['product_id as id',
  1410. DB::raw("$last_month_stock as last_jc_number"),
  1411. DB::raw("$last_month_stock_m as last_jc_money"),
  1412. DB::raw("$this_month_in as this_in_number"),
  1413. DB::raw("$this_month_in_m as this_in_money"),
  1414. DB::raw("$this_month_out as this_out_number"),
  1415. DB::raw("$this_month_out_m as this_out_money"),
  1416. // DB::raw("$this_month_stock as this_jc_number")];
  1417. ];
  1418. }
  1419. $model = InOutRecord::TopClear($user,$data);
  1420. $model = $model->where('del_time',0)
  1421. ->select($field)
  1422. ->groupBy('product_id')
  1423. ->havingRaw("$last_month_stock != 0 OR $this_month_in != 0 OR $this_month_out != 0 OR $this_month_stock != 0");
  1424. return $model;
  1425. }
  1426. //新的进销存统计通用搜索
  1427. public function statisticsJcNewCommon($data,$user, $field = []){
  1428. $model = $this->statisticsJcNewCommonOrigin($data, $user, $field);
  1429. if(! empty($data['product_name']) || ! empty($data['product_category_name']) || ! empty($data['code'])) {
  1430. $id = $this->searchForProduct($data, $user);
  1431. $model->whereIn('product_id', $id);
  1432. }
  1433. return $model;
  1434. }
  1435. //新的进销存统计
  1436. public function statisticsJcNew($data,$user){
  1437. $model = $this->statisticsJcNewCommon($data, $user);
  1438. $model->orderBy('product_id','desc');
  1439. $list = $this->limit($model,'',$data);
  1440. $list = $this->fillDataNew($list);
  1441. return [true, $list];
  1442. }
  1443. public function fillDataNew($data){
  1444. if(empty($data['data'])) return $data;
  1445. //产品
  1446. $map = (new ProductService())->getProductDetail(array_column($data['data'],'id'));
  1447. foreach ($data['data'] as $key => $value){
  1448. //上月结存
  1449. $last_jc_price = floatval($value['last_jc_number']) ? bcdiv($value['last_jc_money'],$value['last_jc_number'],2) : 0;//结存单价
  1450. $data['data'][$key]['last_jc_price'] = $last_jc_price;
  1451. //本月入库
  1452. $this_in_price = floatval($value['this_in_number']) ? bcdiv($value['this_in_money'],$value['this_in_number'],2) : 0;//本月入单价
  1453. $data['data'][$key]['this_in_price'] = $this_in_price;
  1454. //本月出库
  1455. //单价= (上月结存金额+本月入库金额) /上月结存数量+本月入库数量
  1456. $amount = bcadd($value['last_jc_money'], $value['this_in_money'],2);
  1457. $number = bcadd($value['last_jc_number'], $value['this_in_number'],2);
  1458. $this_out_price = floatval($number) ? bcdiv($amount,$number,2) : 0;//本月出单价
  1459. $data['data'][$key]['this_out_price'] = $this_out_price;
  1460. //本月结存
  1461. //本月结存 数量/金额=本月入库+上月结存+(-本月出库)
  1462. $this_jc_money = bcadd(bcadd($value['this_in_money'],$value['last_jc_money'],2),$value['this_out_money'],2);
  1463. $this_jc_number = bcadd(bcadd($value['this_in_number'],$value['last_jc_number'],2),$value['this_out_number'],2);
  1464. $this_jc_price = floatval($this_jc_number) ? bcdiv($this_jc_money,$this_jc_number,2) : 0;//本月结存单价
  1465. $data['data'][$key]['this_jc_money'] = $this_jc_money;
  1466. $data['data'][$key]['this_jc_number'] = $this_jc_number;
  1467. $data['data'][$key]['this_jc_price'] = $this_jc_price;
  1468. //产品
  1469. $tmp = $map[$value['id']] ?? [];
  1470. $data['data'][$key]['title'] = $tmp['title'] ?? "";
  1471. $data['data'][$key]['code'] = $tmp['code'] ?? "";
  1472. //负数改为正数
  1473. $data['data'][$key]['this_out_number'] = abs($value['this_out_number']);
  1474. $data['data'][$key]['this_out_money'] = abs($value['this_out_money']);
  1475. }
  1476. return $data;
  1477. }
  1478. public function searchForProduct($data, $user){
  1479. $search = [];
  1480. if(! empty($data['product_name'])) $search['title'] = $data['product_name'];
  1481. if(! empty($data['product_category_name'])) $search['product_category'] = $data['product_category_name'];
  1482. if(! empty($data['code'])) $search['code'] = $data['code'];
  1483. $model = (new ProductService())->productCommon($search, $user, ['id']);
  1484. $product = $model->get()->toArray();
  1485. return array_column($product,'id');
  1486. }
  1487. public function statisticsAreaDepartProduct222($data,$user){
  1488. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  1489. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  1490. if(empty($data['top_depart_id'])) return [false, '请选择门店'];
  1491. //向总社采购的钱
  1492. $purchase_map = [];
  1493. $purchase = PurchaseOrder::where('del_time',0)
  1494. ->where('top_depart_id',$data['top_depart_id'])
  1495. ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  1496. ->where('crt_time','>=',$return[0])
  1497. ->where('crt_time','<=',$return[1])
  1498. ->select('id')
  1499. ->get()->toArray();
  1500. $purchase_product = PurchaseOrderInfo::where("del_time",0)
  1501. ->whereIn('purchase_order_id',array_column($purchase,'id'))
  1502. ->select('product_id','number','price','final_amount','sports_bag_id')
  1503. ->get()->toArray();
  1504. foreach ($purchase_product as $value){
  1505. if($value['sports_bag_id'] > 0){
  1506. $money = $value['final_amount'];
  1507. }elseif($value['final_amount'] > 0){
  1508. $money = $value['final_amount'];
  1509. }else{
  1510. $money = bcmul($value['price'],$value['number'],2);
  1511. }
  1512. if(isset($purchase_map[$value['product_id']])){
  1513. $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
  1514. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  1515. $purchase_map[$value['product_id']] = [
  1516. 'number' => $tmp_number,
  1517. 'total' => $tmp_money
  1518. ];
  1519. }else{
  1520. $purchase_map[$value['product_id']] = [
  1521. 'number' => $value['number'],
  1522. 'total' => $money
  1523. ];
  1524. }
  1525. }
  1526. //退货的差异
  1527. // $returnExchange_map = $this->returnExchange($data,$return);
  1528. //产品
  1529. $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys([],'product_id'))))
  1530. ->select('id','product_category','title')
  1531. ->get()->toArray();
  1532. $product_list_map = array_column($product_list,null,'id');
  1533. $category = $category_sum = [];
  1534. foreach ($purchase_map as $key => $value){
  1535. // if(isset($returnExchange_map[$key])){
  1536. // $tmp = $returnExchange_map[$key];
  1537. // $number = bcsub($value['number'], $tmp['number'],2);
  1538. // $total = bcsub($value['total'], $tmp['total'],2);
  1539. // $purchase_map[$key] = [
  1540. // 'number' => $number,
  1541. // 'total' => $total,
  1542. // ];
  1543. // }
  1544. $product_tmp = $product_list_map[$key] ?? [];
  1545. $purchase_map[$key]['title'] = $product_tmp['title'];
  1546. $category_tmp = json_decode($product_tmp['product_category']);
  1547. $category_tmp = $category_tmp[0];
  1548. if(! in_array($category_tmp,$category)) $category[] = $category_tmp;
  1549. if(isset($category_sum[$category_tmp])){
  1550. $tmp_number = bcadd($category_sum[$category_tmp]['number'], $purchase_map[$key]['number'],2);
  1551. $tmp_total = bcadd($category_sum[$category_tmp]['total'], $purchase_map[$key]['total'],2);
  1552. $category_sum[$category_tmp] = [
  1553. 'number' => $tmp_number,
  1554. 'total' => $tmp_total,
  1555. ];
  1556. }else{
  1557. $category_sum[$category_tmp] = [
  1558. 'number' => $purchase_map[$key]['number'],
  1559. 'total' => $purchase_map[$key]['total'],
  1560. ];
  1561. }
  1562. }
  1563. //根据产品大类 $category_sum
  1564. $category_return = ProductCategory::whereIn('id',$category)
  1565. ->select('id','title')
  1566. ->get()->toArray();
  1567. foreach ($category_return as $key => $value){
  1568. $tmp = $category_sum[$value['id']] ?? [];
  1569. $category_return[$key]['number'] = $tmp['number'];
  1570. $category_return[$key]['total'] = $tmp['total'];
  1571. }
  1572. //根据产品 $purchase_map
  1573. dd($purchase_map);
  1574. return [true, ''];
  1575. }
  1576. public function statisticsProvince2222($data,$user){
  1577. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  1578. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  1579. //门店设置的指标 按照区域汇总
  1580. $index_map = DepartIndex::where('del_time',0)
  1581. ->pluck('param_one','top_depart_id')
  1582. ->toArray();
  1583. //分社所属省
  1584. $depart_map = $province_map = [];
  1585. $depart = Depart::where('parent_id',0)
  1586. ->where('province','<>','')
  1587. ->where('del_time',0)
  1588. ->select('province','id')
  1589. ->get()->toArray();
  1590. foreach ($depart as $value){
  1591. $depart_map[$value['id']] = $value['province'];
  1592. $province_map[$value['province']][] = $value['id'];
  1593. }
  1594. $address_map = config('address');
  1595. $address_map = array_column($address_map,'label','value');
  1596. //大区
  1597. $final_address_map = [];
  1598. foreach ($address_map as $key => $value){
  1599. $tmp = [
  1600. 'key' => $key,
  1601. 'title' => $value,
  1602. 'total' => 0,
  1603. 'index' => 0
  1604. ];
  1605. $top_depart_id = $province_map[$key] ?? [];
  1606. if(! empty($top_depart_id)){
  1607. foreach ($top_depart_id as $depart_id){
  1608. $index = $index_map[$depart_id] ?? 0;
  1609. $tmp['index'] = bcadd($tmp['index'], $index,2);
  1610. }
  1611. }
  1612. $final_address_map[] = $tmp;
  1613. }
  1614. //向总社采购的钱
  1615. $purchase_map = [];
  1616. $purchase = PurchaseOrder::where('del_time',0)
  1617. ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  1618. ->where('crt_time','>=',$return[0])
  1619. ->where('crt_time','<=',$return[1])
  1620. ->select('top_depart_id','purchase_total')
  1621. ->get()->toArray();
  1622. foreach ($purchase as $value){
  1623. $area = $depart_map[$value['top_depart_id']] ?? 0;
  1624. if(! $area) continue;
  1625. if(isset($purchase_map[$area])){
  1626. $total = bcadd($purchase_map[$area], $value['purchase_total'],2);
  1627. $purchase_map[$area] = $total;
  1628. }else{
  1629. $purchase_map[$area] = $value['purchase_total'];
  1630. }
  1631. }
  1632. //退货的差异
  1633. $returnExchange_map = [];
  1634. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  1635. ->where('type',ReturnExchangeOrder::Order_type2)
  1636. ->where('crt_time','>=',$return[0])
  1637. ->where('crt_time','<=',$return[1])
  1638. ->where('crt_time','<=',$return[1])
  1639. ->select('top_depart_id','difference_amount')
  1640. ->get()->toArray();
  1641. foreach ($returnExchange as $value){
  1642. $area = $depart_map[$value['top_depart_id']] ?? 0;
  1643. if(! $area) continue;
  1644. if(isset($returnExchange_map[$area])){
  1645. $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
  1646. $returnExchange_map[$area] = $total;
  1647. }else{
  1648. $returnExchange_map[$area] = $value['difference_amount'];
  1649. }
  1650. }
  1651. foreach ($final_address_map as $key => $value){
  1652. $purchase_tmp = $purchase_map[$value['key']] ?? 0;
  1653. $return_tmp = $returnExchange_map[$value['key']] ?? 0;
  1654. $final_address_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  1655. }
  1656. return [true, $final_address_map];
  1657. }
  1658. public function fillStatisticsBt1($data){
  1659. if(empty($data)) return $data;
  1660. $total = floatval(array_sum(array_column($data,'total')));
  1661. // 设置一个最小显示阈值,比如0.5%
  1662. $threshold = 0.5;
  1663. // 用于存储调整后的数据
  1664. $adjustedData = [];
  1665. $otherTotal = 0;
  1666. $other = [];
  1667. foreach ($data as $value) {
  1668. $model_type_title = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  1669. $rate = $total ? $value['total'] / $total : 0;
  1670. // 检查是否低于阈值
  1671. if ($rate * 100 < $threshold) {
  1672. // 小于阈值的部分加到"其他"中
  1673. $otherTotal += $value['total'];
  1674. $other[] = $model_type_title;
  1675. } else {
  1676. // 保留更多小数位数
  1677. $adjustedData[] = [
  1678. 'model_type' => $value['model_type'],
  1679. 'total' => $value['total'],
  1680. 'model_type_title' => $model_type_title,
  1681. 'rate' => round($rate * 100, 2)
  1682. ];
  1683. }
  1684. }
  1685. // 如果有"其他"值,则添加到数据集中
  1686. if ($otherTotal > 0) {
  1687. $other_title = implode(',',$other);
  1688. $adjustedData[] = [
  1689. 'model_type' => -1, // 自定义一个类型标识
  1690. 'total' => $otherTotal,
  1691. 'model_type_title' => '其他(' . $other_title . ')',
  1692. 'rate' => round($otherTotal / $total * 100, 2)
  1693. ];
  1694. }
  1695. return $adjustedData;
  1696. }
  1697. //改装 客户模板 t9改装 不是分配的客户 是所有的 =》 点击省 展示市 只有这个有产品
  1698. //分社 客户模板 t9改装 不是分配的客户 是所有的 =》 点击省 展示分社
  1699. //加盟 客户模板 t9分社 不是分配的客户 是所有的 =》 点击省 展示市
  1700. //销售 不是分配的客户 是所有的 =》 点击分社 展示销售人员(客户的负责人)
  1701. public function customerReportStepOfFirst($data, $user){
  1702. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  1703. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  1704. if(empty($data['type'])) return [false, '请选择报表查看类型'];
  1705. $type = $data['type'];
  1706. $result = [];
  1707. if($type == 1){
  1708. $type_detail = 1;
  1709. if(! empty($data['type_detail'])) $type_detail = $data['type_detail'];
  1710. if($type_detail == 1){
  1711. $result = $this->customerReportStepOneForProvince($return,$type);
  1712. }else{
  1713. $result = $this->customerReportStepOneForProduct($return);
  1714. }
  1715. }elseif ($type == 2){
  1716. $result = $this->customerReportStepOneForProvinceFs($return);
  1717. }elseif ($type == 3){
  1718. $result = $this->customerReportStepOneForProvince($return,$type);
  1719. }elseif ($type == 4){
  1720. $result = $this->customerReportStepOneForProvinceXs($return);
  1721. }else{
  1722. return [false ,'报表查看类型错误'];
  1723. }
  1724. return [true, ['list' => $result]];
  1725. }
  1726. private function customerReportStepOneForProduct($time){
  1727. //基础产品数据
  1728. $basic_all_data = BasicTypeAllUse::where('del_time',0)
  1729. ->where('type', BasicTypeAllUse::type_one)
  1730. ->select('title','id')
  1731. ->get()->toArray();
  1732. $basic_all = [];
  1733. foreach ($basic_all_data as $value){
  1734. $basic_all[] = [
  1735. 'label' => $value['title'],
  1736. 'value' => $value['id'],
  1737. 'all_num' => 0
  1738. ];
  1739. }
  1740. $start = $time[0];
  1741. $end = $time[1];
  1742. $model_type = Customer::Model_type_one;
  1743. $label = "所有产品";
  1744. $total = [
  1745. 'label' => $label,
  1746. 'value' => "",
  1747. 'all_num' => 0
  1748. ];
  1749. $list = CustomerReport::where('del_time',0)
  1750. ->where('enter_time','>=', $start)
  1751. ->where('enter_time','<=', $end)
  1752. ->where('model_type',$model_type)
  1753. ->select('consulting_product_new')
  1754. ->get()->toArray();
  1755. $map = [];
  1756. foreach ($list as $value) {
  1757. if(isset($map[$value['consulting_product_new']])) {
  1758. $map[$value['consulting_product_new']] += 1;
  1759. }else{
  1760. $map[$value['consulting_product_new']] = 1;
  1761. }
  1762. $total['all_num'] += 1;
  1763. }
  1764. foreach ($basic_all as $key => $value){
  1765. if(isset($map[$value['value']])){
  1766. $tmp = $map[$value['value']];
  1767. $basic_all[$key]['all_num'] = $tmp;
  1768. }
  1769. }
  1770. usort($basic_all, function($a, $b) {
  1771. return $b['all_num'] - $a['all_num'];
  1772. });
  1773. array_unshift($basic_all, $total);
  1774. return $basic_all;
  1775. }
  1776. private function customerReportStepOneForProvince($time, $type){
  1777. //省市数据
  1778. $addressData = config('address3');
  1779. if(is_string($addressData)) $addressData = json_decode($addressData, true);
  1780. $province = [];
  1781. foreach ($addressData as $value){
  1782. $province[] = [
  1783. 'label' => $value['label'],
  1784. 'value' => $value['value'],
  1785. 'follow_num' => 0,
  1786. 'all_num' => 0
  1787. ];
  1788. }
  1789. $start = $time[0];
  1790. $end = $time[1];
  1791. $model_type = Customer::Model_type_one;
  1792. if($type == 3) $model_type = Customer::Model_type_three;
  1793. $label = "所有客资";
  1794. $total = [
  1795. 'label' => $label,
  1796. 'value' => "",
  1797. 'follow_num' => 0,
  1798. 'all_num' => 0
  1799. ];
  1800. $list = CustomerReport::where('del_time',0)
  1801. ->where('enter_time','>=', $start)
  1802. ->where('enter_time','<=', $end)
  1803. ->where('model_type',$model_type)
  1804. ->where('province_code','<>','')
  1805. ->select('province_code','follow_num')
  1806. ->get()->toArray();
  1807. $map = [];
  1808. foreach ($list as $value) {
  1809. if(isset($map[$value['province_code']])) {
  1810. $tmp = bcadd($value['follow_num'], $map[$value['province_code']]['num_1']);
  1811. $map[$value['province_code']]['num_1'] = $tmp;
  1812. $map[$value['province_code']]['num_2'] += 1;
  1813. }else{
  1814. $map[$value['province_code']] = [
  1815. 'num_1' => $value['follow_num'],
  1816. 'num_2' => 1,
  1817. ];
  1818. }
  1819. $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
  1820. $total['follow_num'] = $tmp_t;
  1821. $total['all_num'] += 1;
  1822. }
  1823. foreach ($province as $key => $value){
  1824. if(isset($map[$value['value']])){
  1825. $tmp = $map[$value['value']];
  1826. $province[$key]['follow_num'] = intval($tmp['num_1']);
  1827. $province[$key]['all_num'] = $tmp['num_2'];
  1828. }
  1829. }
  1830. usort($province, function($a, $b) {
  1831. // 先比较 all_num
  1832. if ($b['all_num'] !== $a['all_num']) {
  1833. return $b['all_num'] - $a['all_num']; // all_num 降序
  1834. }
  1835. // 如果 all_num 相同,再比较 value
  1836. return $a['value'] - $b['value']; // value 正序
  1837. });
  1838. array_unshift($province, $total);
  1839. return $province;
  1840. }
  1841. private function customerReportStepOneForProvinceFs($time){
  1842. //省市数据
  1843. $addressData = config('address3');
  1844. if(is_string($addressData)) $addressData = json_decode($addressData, true);
  1845. $province = [];
  1846. foreach ($addressData as $value){
  1847. $province[] = [
  1848. 'label' => $value['label'],
  1849. 'value' => $value['value'],
  1850. 'follow_num' => 0,
  1851. 'all_num' => 0
  1852. ];
  1853. }
  1854. //门店归属省
  1855. $depart = Depart::where('del_time',0)
  1856. ->where('parent_id',0)
  1857. ->where('province','<>','')
  1858. ->select('id','province as province_code')
  1859. ->get()->toArray();
  1860. $depart_map = [];
  1861. foreach ($depart as $value){
  1862. $depart_map[$value['id']] = $value['province_code'];
  1863. }
  1864. $start = $time[0];
  1865. $end = $time[1];
  1866. $model_type = Customer::Model_type_one;
  1867. $label = "全国分社";
  1868. $total = [
  1869. 'label' => $label,
  1870. 'value' => "",
  1871. 'follow_num' => 0,
  1872. 'all_num' => 0
  1873. ];
  1874. $list = CustomerReport::where('del_time',0)
  1875. ->where('enter_time','>=', $start)
  1876. ->where('enter_time','<=', $end)
  1877. ->where('model_type',$model_type)
  1878. ->where('province_code','<>','')
  1879. ->select('province_code','follow_num','customer_id')
  1880. ->get()->toArray();
  1881. $detail = CustomerReportDepart::where('del_time',0)
  1882. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  1883. ->where('type',CustomerReportDepart::type_one)
  1884. ->select('customer_id','top_depart_id')
  1885. ->get()->toArray();
  1886. $detail_map = [];
  1887. foreach ($detail as $value){
  1888. $detail_map[$value['customer_id']][] = $value['top_depart_id'];
  1889. }
  1890. $map = [];
  1891. foreach ($list as $value) {
  1892. //客资所属门店
  1893. $c_belong_id = $detail_map[$value['customer_id']] ?? "";
  1894. if(empty($c_belong_id)) continue;
  1895. foreach ($c_belong_id as $id){
  1896. //客资所属门店所属省
  1897. $province_code = $depart_map[$id] ?? "";
  1898. if(empty($province_code)) continue;
  1899. if(isset($map[$province_code])) {
  1900. $tmp = bcadd($value['follow_num'], $map[$province_code]['num_1']);
  1901. $map[$province_code]['num_1'] = $tmp;
  1902. $map[$province_code]['num_2'] += 1;
  1903. }else{
  1904. $map[$province_code] = [
  1905. 'num_1' => $value['follow_num'],
  1906. 'num_2' => 1,
  1907. ];
  1908. }
  1909. $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
  1910. $total['follow_num'] = $tmp_t;
  1911. $total['all_num'] += 1;
  1912. }
  1913. }
  1914. foreach ($province as $key => $value){
  1915. if(isset($map[$value['value']])){
  1916. $tmp = $map[$value['value']];
  1917. $province[$key]['follow_num'] = intval($tmp['num_1']);
  1918. $province[$key]['all_num'] = $tmp['num_2'];
  1919. }
  1920. }
  1921. usort($province, function($a, $b) {
  1922. // 先比较 all_num
  1923. if ($b['all_num'] !== $a['all_num']) {
  1924. return $b['all_num'] - $a['all_num']; // all_num 降序
  1925. }
  1926. // 如果 all_num 相同,再比较 value
  1927. return $a['value'] - $b['value']; // value 正序
  1928. });
  1929. array_unshift($province, $total);
  1930. return $province;
  1931. }
  1932. private function customerReportStepOneForProvinceXs($time){
  1933. //门店
  1934. $depart = Depart::where('del_time',0)
  1935. ->where('parent_id',0)
  1936. ->select('id','title')
  1937. ->get()->toArray();
  1938. $departArray = [];
  1939. foreach ($depart as $value){
  1940. $departArray[]= [
  1941. 'label' => $value['title'],
  1942. 'value' => $value['id'],
  1943. 'follow_num' => 0,
  1944. 'all_num' => 0
  1945. ];
  1946. }
  1947. $start = $time[0];
  1948. $end = $time[1];
  1949. $label = "所有门店";
  1950. $total = [
  1951. 'label' => $label,
  1952. 'value' => "",
  1953. 'follow_num' => 0,
  1954. 'all_num' => 0
  1955. ];
  1956. $list = CustomerReport::where('del_time',0)
  1957. ->where('enter_time','>=', $start)
  1958. ->where('enter_time','<=', $end)
  1959. ->select('follow_num','customer_id')
  1960. ->get()->toArray();
  1961. $detail = CustomerReportDepart::where('del_time',0)
  1962. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  1963. ->where('type',CustomerReportDepart::type_one)
  1964. ->select('customer_id','top_depart_id')
  1965. ->get()->toArray();
  1966. $detail_map = [];
  1967. foreach ($detail as $value){
  1968. $detail_map[$value['customer_id']][] = $value['top_depart_id'];
  1969. }
  1970. $map = [];
  1971. foreach ($list as $value) {
  1972. if(isset($detail_map[$value['customer_id']])){
  1973. //客资所属门店
  1974. $c_belong_id = $detail_map[$value['customer_id']];
  1975. if(empty($c_belong_id)) continue;
  1976. foreach ($c_belong_id as $id){
  1977. if(isset($map[$id])) {
  1978. $tmp = bcadd($value['follow_num'], $map[$id]['num_1']);
  1979. $map[$id]['num_1'] = $tmp;
  1980. $map[$id]['num_2'] += 1;
  1981. }else{
  1982. $map[$id] = [
  1983. 'num_1' => $value['follow_num'],
  1984. 'num_2' => 1,
  1985. ];
  1986. }
  1987. $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
  1988. $total['follow_num'] = $tmp_t;
  1989. $total['all_num'] += 1;
  1990. }
  1991. }
  1992. }
  1993. foreach ($departArray as $key => $value){
  1994. if(isset($map[$value['value']])){
  1995. $tmp = $map[$value['value']];
  1996. $departArray[$key]['follow_num'] = intval($tmp['num_1']);
  1997. $departArray[$key]['all_num'] = $tmp['num_2'];
  1998. }
  1999. }
  2000. usort($departArray, function($a, $b) {
  2001. // 先比较 all_num
  2002. if ($b['all_num'] !== $a['all_num']) {
  2003. return $b['all_num'] - $a['all_num']; // all_num 降序
  2004. }
  2005. // 如果 all_num 相同,再比较 value
  2006. return $a['value'] - $b['value']; // value 正序
  2007. });
  2008. array_unshift($departArray, $total);
  2009. return $departArray;
  2010. }
  2011. public function customerReportStepOfSecond($data, $user){
  2012. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  2013. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  2014. if(empty($data['type'])) return [false, '请选择报表查看类型'];
  2015. if(empty($data['value'])) return [false, 'value不能为空'];
  2016. $type = $data['type'];
  2017. if($type == 1){
  2018. list($list, $total) = $this->customerReportStepSecondForCity($return,$data,$type);
  2019. }elseif ($type == 2){
  2020. list($list, $total) = $this->customerReportStepSecondForFs($return, $data);
  2021. }elseif ($type == 3){
  2022. list($list, $total) = $this->customerReportStepSecondForCity($return,$data,$type);
  2023. }elseif ($type == 4){
  2024. list($list, $total) = $this->customerReportStepSecondForProvinceXs($return,$data);
  2025. }else{
  2026. return [false ,'报表查看类型错误'];
  2027. }
  2028. return [true, ['list' => $list, 'total' => $total]];
  2029. }
  2030. private function customerReportStepSecondForCity($time, $data, $type){
  2031. $province_code = $data['value'];
  2032. //市数据
  2033. $addressData = config('address3');
  2034. if(is_string($addressData)) $addressData = json_decode($addressData, true);
  2035. $city = [];
  2036. foreach ($addressData as $value){
  2037. if($value['value'] != $province_code) continue;
  2038. foreach ($value['children'] as $val){
  2039. $city[] = [
  2040. 'label' => $val['label'],
  2041. 'value' => $val['value'],
  2042. 'follow_num' => 0,
  2043. 'all_num' => 0
  2044. ];
  2045. }
  2046. }
  2047. $start = $time[0];
  2048. $end = $time[1];
  2049. $model_type = Customer::Model_type_one;
  2050. if($type == 3) $model_type = Customer::Model_type_three;
  2051. // $label = "所有客资";
  2052. // $total = [
  2053. // 'label' => $label,
  2054. // 'value' => "",
  2055. // 'follow_num' => 0,
  2056. // 'all_num' => 0
  2057. // ];
  2058. $total = [
  2059. 'follow_num' => 0,
  2060. 'all_num' => 0
  2061. ];
  2062. $list = CustomerReport::where('del_time',0)
  2063. ->where('enter_time','>=', $start)
  2064. ->where('enter_time','<=', $end)
  2065. ->where('model_type',$model_type)
  2066. ->where('province_code', $province_code)
  2067. ->select('city_code','follow_num')
  2068. ->get()->toArray();
  2069. $map = [];
  2070. $first_city = $city[0];
  2071. foreach ($list as $value) {
  2072. if(empty($value['city_code'])) $value['city_code'] = $first_city['value'];
  2073. if(isset($map[$value['city_code']])) {
  2074. $tmp = bcadd($value['follow_num'], $map[$value['city_code']]['num_1']);
  2075. $map[$value['city_code']]['num_1'] = $tmp;
  2076. $map[$value['city_code']]['num_2'] += 1;
  2077. }else{
  2078. $map[$value['city_code']] = [
  2079. 'num_1' => $value['follow_num'],
  2080. 'num_2' => 1,
  2081. ];
  2082. }
  2083. $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
  2084. $total['follow_num'] = $tmp_t;
  2085. $total['all_num'] += 1;
  2086. }
  2087. foreach ($city as $key => $value){
  2088. if(isset($map[$value['value']])){
  2089. $tmp = $map[$value['value']];
  2090. $city[$key]['follow_num'] = intval($tmp['num_1']);
  2091. $city[$key]['all_num'] = $tmp['num_2'];
  2092. }
  2093. }
  2094. usort($city, function($a, $b) {
  2095. // 先比较 all_num
  2096. if ($b['all_num'] !== $a['all_num']) {
  2097. return $b['all_num'] - $a['all_num']; // all_num 降序
  2098. }
  2099. // 如果 all_num 相同,再比较 value
  2100. return $a['value'] - $b['value']; // value 正序
  2101. });
  2102. return [$city, $total];
  2103. }
  2104. private function customerReportStepSecondForFs($time, $data){
  2105. $province_code = $data['value'];
  2106. //省下的所有门店
  2107. $depart = Depart::where('del_time',0)
  2108. ->where('parent_id',0)
  2109. ->where('province', $province_code)
  2110. ->select('id','title')
  2111. ->get()->toArray();
  2112. $depart_id = array_column($depart,'id');
  2113. $depart_array = [];
  2114. foreach ($depart as $value){
  2115. $depart_array[] = [
  2116. 'label' => $value['title'],
  2117. 'value' => $value['id'],
  2118. 'follow_num' => 0,
  2119. 'all_num' => 0
  2120. ];
  2121. }
  2122. $start = $time[0];
  2123. $end = $time[1];
  2124. $model_type = Customer::Model_type_one;
  2125. $list = CustomerReport::where('del_time',0)
  2126. ->where('enter_time','>=', $start)
  2127. ->where('enter_time','<=', $end)
  2128. ->where('model_type',$model_type)
  2129. ->where('province','<>','')
  2130. ->select('province_code','follow_num','customer_id')
  2131. ->get()->toArray();
  2132. $detail = CustomerReportDepart::where('del_time',0)
  2133. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  2134. ->where('type',CustomerReportDepart::type_one)
  2135. ->whereIn('top_depart_id',$depart_id)
  2136. ->select('customer_id','top_depart_id')
  2137. ->get()->toArray();
  2138. $detail_map = [];
  2139. foreach ($detail as $value){
  2140. $detail_map[$value['customer_id']][] = $value['top_depart_id'];
  2141. }
  2142. $total = [
  2143. 'follow_num' => 0,
  2144. 'all_num' => 0
  2145. ];
  2146. $map = [];
  2147. foreach ($list as $value) {
  2148. if(isset($detail_map[$value['customer_id']])){
  2149. //客资所属门店
  2150. $c_belong_id = $detail_map[$value['customer_id']] ?? [];
  2151. if(empty($c_belong_id)) continue;
  2152. foreach ($c_belong_id as $id){
  2153. if(isset($map[$id])) {
  2154. $tmp = bcadd($value['follow_num'], $map[$id]['num_1']);
  2155. $map[$id]['num_1'] = $tmp;
  2156. $map[$id]['num_2'] += 1;
  2157. }else{
  2158. $map[$id] = [
  2159. 'num_1' => $value['follow_num'],
  2160. 'num_2' => 1,
  2161. ];
  2162. }
  2163. $tmp_t = bcadd($total['follow_num'], $value['follow_num']);
  2164. $total['follow_num'] = $tmp_t;
  2165. $total['all_num'] += 1;
  2166. }
  2167. }
  2168. }
  2169. foreach ($depart_array as $key => $value){
  2170. if(isset($map[$value['value']])){
  2171. $tmp = $map[$value['value']];
  2172. $depart_array[$key]['follow_num'] = intval($tmp['num_1']);
  2173. $depart_array[$key]['all_num'] = $tmp['num_2'];
  2174. }
  2175. }
  2176. usort($depart_array, function($a, $b) {
  2177. // 先比较 all_num
  2178. if ($b['all_num'] !== $a['all_num']) {
  2179. return $b['all_num'] - $a['all_num']; // all_num 降序
  2180. }
  2181. // 如果 all_num 相同,再比较 value
  2182. return $a['value'] - $b['value']; // value 正序
  2183. });
  2184. return [$depart_array, $total];
  2185. }
  2186. private function customerReportStepSecondForProvinceXs($time,$data){
  2187. $top_depart_id = intval($data['value']);
  2188. //门店下所有部门
  2189. $all_depart_id = DB::select("
  2190. WITH RECURSIVE sub_departments AS (
  2191. SELECT id FROM depart WHERE parent_id = ? AND del_time = 0
  2192. UNION ALL
  2193. SELECT d.id FROM depart d
  2194. INNER JOIN sub_departments s ON d.parent_id = s.id
  2195. WHERE d.del_time = 0
  2196. )
  2197. SELECT id FROM sub_departments
  2198. ", [$top_depart_id]);
  2199. $allDepartIds = collect($all_depart_id)->pluck('id')->toArray();
  2200. $allDepartIds[] = $top_depart_id;
  2201. //所属于这个门店下的人
  2202. $man = EmployeeDepartPermission::whereIn('depart_id', $allDepartIds)
  2203. ->select('employee_id')
  2204. ->get()->toArray();
  2205. $man = array_column($man,'employee_id');
  2206. $employee = Employee::where('del_time',0)
  2207. ->whereIn('id', $man)
  2208. ->select('id', 'emp_name', 'number')
  2209. ->get()->toArray();
  2210. $employee_map = array_column($employee,null,'id');
  2211. $start = $time[0];
  2212. $end = $time[1];
  2213. $total = [
  2214. 'follow_num' => 0,
  2215. 'all_num' => 0
  2216. ];
  2217. $list = CustomerReport::where('del_time',0)
  2218. ->where('enter_time','>=', $start)
  2219. ->where('enter_time','<=', $end)
  2220. ->select('follow_num','customer_id')
  2221. ->get()->toArray();
  2222. $detail = CustomerReportDepart::where('del_time',0)
  2223. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  2224. ->select('customer_id','man_id','type','top_depart_id')
  2225. ->get()->toArray();
  2226. $manArray = $man_flag = $detail_array = $man_map = [];
  2227. foreach ($detail as $value){
  2228. if($value['type'] == CustomerReportDepart::type_two){
  2229. if(isset($employee_map[$value['man_id']])){
  2230. if(in_array($value['man_id'], $man_flag)) continue;
  2231. if(! in_array($value['man_id'], $man)) continue;
  2232. $man_flag[] = $value['man_id'];
  2233. $e_t = $employee_map[$value['man_id']];
  2234. $manArray[] = [
  2235. 'label' => $e_t['emp_name'],
  2236. 'value' => $e_t['id'],
  2237. 'follow_num' => 0,
  2238. 'all_num' => 0
  2239. ];
  2240. }
  2241. }elseif ($value['type'] == CustomerReportDepart::type_one && $value['top_depart_id'] == $top_depart_id){
  2242. if(! in_array($value['customer_id'], $detail_array)) $detail_array[] = $value['customer_id'];
  2243. }elseif ($value['type'] == CustomerReportDepart::type_three){
  2244. if(isset($man_map[$value['customer_id']][$value['man_id']])){
  2245. $man_map[$value['customer_id']][$value['man_id']] += 1;
  2246. }else{
  2247. $man_map[$value['customer_id']][$value['man_id']] = 1;
  2248. }
  2249. }
  2250. }unset($man_flag);
  2251. $map = [];
  2252. foreach ($list as $value) {
  2253. //这个客户是否属于这个门店
  2254. if(in_array($value['customer_id'], $detail_array)){
  2255. if(isset($man_map[$value['customer_id']])) {
  2256. //客户由谁创建跟进记录
  2257. $tmp = $man_map[$value['customer_id']];
  2258. foreach ($tmp as $m_id => $val){
  2259. if(isset($map[$m_id])) {
  2260. $tmp = bcadd($value['follow_num'], $map[$m_id]['num_1']);
  2261. $map[$m_id]['num_1'] = $tmp;
  2262. $map[$m_id]['num_2'] += 1;
  2263. }else{
  2264. $map[$m_id] = [
  2265. 'num_1' => $val,
  2266. 'num_2' => 1,
  2267. ];
  2268. }
  2269. $tmp_t = bcadd($total['follow_num'], $val);
  2270. $total['follow_num'] = $tmp_t;
  2271. }
  2272. }
  2273. $total['all_num'] += 1;
  2274. }
  2275. }
  2276. foreach ($manArray as $key => $value){
  2277. if(isset($map[$value['value']])){
  2278. $tmp = $map[$value['value']];
  2279. $manArray[$key]['follow_num'] = intval($tmp['num_1']);
  2280. $manArray[$key]['all_num'] = $tmp['num_2'];
  2281. }
  2282. }
  2283. usort($manArray, function($a, $b) {
  2284. // 先比较 all_num
  2285. if ($b['all_num'] !== $a['all_num']) {
  2286. return $b['all_num'] - $a['all_num']; // all_num 降序
  2287. }
  2288. // 如果 all_num 相同,再比较 value
  2289. return $a['value'] - $b['value']; // value 正序
  2290. });
  2291. return [$manArray, $total];
  2292. }
  2293. public function customerReportStepOfThird($data, $user){
  2294. if(empty($data['enter_time'][0]) || empty($data['enter_time'][1])) return [false, '请选择日期'];
  2295. $return = $this->changeDateToTimeStampAboutRange($data['enter_time']);
  2296. if(empty($data['type'])) return [false, '请选择报表查看类型'];
  2297. if(empty($data['value'])) return [false, 'value不能为空'];
  2298. if(empty($data['value2'])) return [false, 'value2不能为空'];
  2299. $type = $data['type'];
  2300. $result = [];
  2301. if($type == 1){
  2302. $result = $this->customerReportStepThirdForDetail($return,$data,$type);
  2303. }elseif ($type == 2){
  2304. $result = $this->customerReportStepThirdForFsDetail($return, $data);
  2305. }elseif ($type == 3){
  2306. $result = $this->customerReportStepThirdForDetail($return,$data,$type);
  2307. }elseif ($type == 4){
  2308. $result = $this->customerReportStepThirdForProvinceXsDetail($return,$data);
  2309. }else{
  2310. return [false ,'报表查看类型错误'];
  2311. }
  2312. return [true, ['list' => $result]];
  2313. }
  2314. private function customerReportStepThirdForDetail($time, $data, $type){
  2315. $province_code = $data['value'];
  2316. $city_code = $data['value2'];
  2317. //市数据
  2318. $addressData = config('address3');
  2319. if(is_string($addressData)) $addressData = json_decode($addressData, true);
  2320. $first_city = "";
  2321. foreach ($addressData as $value){
  2322. if($value['value'] != $province_code && ! empty($city)) continue;
  2323. foreach ($value['children'] as $val){
  2324. if(! empty($city)) continue;
  2325. $first_city = $val['value'];
  2326. }
  2327. }
  2328. $customer = [];
  2329. foreach (CustomerReportDepart::type_list as $key => $value){
  2330. $customer[] = [
  2331. 'label' => $value,
  2332. 'value' => $key,
  2333. 'list' => [],
  2334. 'count' => 0
  2335. ];
  2336. }
  2337. $start = $time[0];
  2338. $end = $time[1];
  2339. $model_type = Customer::Model_type_one;
  2340. if($type == 3) $model_type = Customer::Model_type_three;
  2341. $list = CustomerReport::where('del_time',0)
  2342. ->where('enter_time','>=', $start)
  2343. ->where('enter_time','<=', $end)
  2344. ->where('model_type',$model_type)
  2345. ->where('province_code', $province_code)
  2346. ->select('city_code','type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
  2347. ->get()->toArray();
  2348. $map = [];
  2349. foreach ($list as $value) {
  2350. if(empty($value['city_code']) && $first_city != $city_code) continue;
  2351. $tmp = [
  2352. 'customer_id' => $value['customer_id'],
  2353. 'title' => $value['customer_id'],
  2354. 'customer_from' => $value['customer_from'],
  2355. 'car_type' => $value['car_type'],
  2356. 'consulting_product_new_title' => $value['consulting_product_new_title'],
  2357. 'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
  2358. 'contact_info' => $value['contact_info'],
  2359. ];
  2360. $map[$value['type']][] = $tmp;
  2361. }
  2362. foreach ($customer as $key => $value){
  2363. if(isset($map[$value['value']])){
  2364. $tmp = $map[$value['value']];
  2365. $customer[$key]['list'] = $tmp;
  2366. $customer[$key]['count'] = count($tmp);
  2367. }
  2368. }
  2369. return $customer;
  2370. }
  2371. private function customerReportStepThirdForFsDetail($time, $data){
  2372. $province_code = $data['value'];
  2373. $top_depart_id = $data['value2'];
  2374. $customer = [];
  2375. foreach (CustomerReportDepart::type_list as $key => $value){
  2376. $customer[] = [
  2377. 'label' => $value,
  2378. 'value' => $key,
  2379. 'list' => [],
  2380. 'count' => 0
  2381. ];
  2382. }
  2383. $start = $time[0];
  2384. $end = $time[1];
  2385. $model_type = Customer::Model_type_one;
  2386. $list = CustomerReport::where('del_time',0)
  2387. ->where('enter_time','>=', $start)
  2388. ->where('enter_time','<=', $end)
  2389. ->where('model_type',$model_type)
  2390. ->where('province','<>','')
  2391. ->select('type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
  2392. ->get()->toArray();
  2393. $detail = CustomerReportDepart::where('del_time',0)
  2394. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  2395. ->where('type',CustomerReportDepart::type_one)
  2396. ->where('top_depart_id', $top_depart_id)
  2397. ->select('customer_id')
  2398. ->get()->toArray();
  2399. $detail_array = array_column($detail,'customer_id');
  2400. $map = [];
  2401. foreach ($list as $value) {
  2402. if(in_array($value['customer_id'], $detail_array)){
  2403. $tmp = [
  2404. 'customer_id' => $value['customer_id'],
  2405. 'title' => $value['customer_id'],
  2406. 'customer_from' => $value['customer_from'],
  2407. 'car_type' => $value['car_type'],
  2408. 'consulting_product_new_title' => $value['consulting_product_new_title'],
  2409. 'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
  2410. 'contact_info' => $value['contact_info'],
  2411. ];
  2412. $map[$value['type']][] = $tmp;
  2413. }
  2414. }
  2415. foreach ($customer as $key => $value){
  2416. if(isset($map[$value['value']])){
  2417. $tmp = $map[$value['value']];
  2418. $customer[$key]['list'] = $tmp;
  2419. $customer[$key]['count'] = count($tmp);
  2420. }
  2421. }
  2422. return $customer;
  2423. }
  2424. private function customerReportStepThirdForProvinceXsDetail($time, $data){
  2425. $top_depart_id = intval($data['value']);
  2426. $man_id = $data['value2'];
  2427. $customer = [];
  2428. foreach (CustomerReportDepart::type_list as $key => $value){
  2429. $customer[] = [
  2430. 'label' => $value,
  2431. 'value' => $key,
  2432. 'list' => [],
  2433. 'count' => 0
  2434. ];
  2435. }
  2436. //门店下所有部门
  2437. $all_depart_id = DB::select("
  2438. WITH RECURSIVE sub_departments AS (
  2439. SELECT id FROM depart WHERE parent_id = ? AND del_time = 0
  2440. UNION ALL
  2441. SELECT d.id FROM depart d
  2442. INNER JOIN sub_departments s ON d.parent_id = s.id
  2443. WHERE d.del_time = 0
  2444. )
  2445. SELECT id FROM sub_departments
  2446. ", [$top_depart_id]);
  2447. $allDepartIds = collect($all_depart_id)->pluck('id')->toArray();
  2448. $allDepartIds[] = $top_depart_id;
  2449. //所属于这个门店下的人
  2450. $exists = EmployeeDepartPermission::whereIn('depart_id', $allDepartIds)
  2451. ->where('employee_id',$man_id)
  2452. ->exists();
  2453. if(! $exists) return [];
  2454. $start = $time[0];
  2455. $end = $time[1];
  2456. $list = CustomerReport::where('del_time',0)
  2457. ->where('enter_time','>=', $start)
  2458. ->where('enter_time','<=', $end)
  2459. ->select('type','customer_id','title','customer_from','car_type','consulting_product_new_title','enter_time','contact_info')
  2460. ->get()->toArray();
  2461. $detail = CustomerReportDepart::where('del_time',0)
  2462. ->whereIn('customer_id',array_unique(array_column($list,'customer_id')))
  2463. ->select('customer_id','man_id','type','top_depart_id')
  2464. ->get()->toArray();
  2465. $detail_array = $man_map = [];
  2466. foreach ($detail as $value){
  2467. if($value['type'] == CustomerReportDepart::type_two){
  2468. }elseif ($value['type'] == CustomerReportDepart::type_one && $value['top_depart_id'] == $top_depart_id){
  2469. if(! in_array($value['customer_id'], $detail_array)) $detail_array[] = $value['customer_id'];
  2470. }elseif ($value['type'] == CustomerReportDepart::type_three){
  2471. if($value['man_id'] == $man_id) $man_map[$value['customer_id']] = 1;
  2472. }
  2473. }
  2474. $map = [];
  2475. foreach ($list as $value) {
  2476. //这个客户是否属于当前这个门店
  2477. if(in_array($value['customer_id'], $detail_array)){
  2478. if(isset($man_map[$value['customer_id']])) {
  2479. $tmp = [
  2480. 'customer_id' => $value['customer_id'],
  2481. 'title' => $value['customer_id'],
  2482. 'customer_from' => $value['customer_from'],
  2483. 'car_type' => $value['car_type'],
  2484. 'consulting_product_new_title' => $value['consulting_product_new_title'],
  2485. 'enter_time' => $value['enter_time'] ? date("Y-m-d H:i:s", $value['enter_time']) : "",
  2486. 'contact_info' => $value['contact_info'],
  2487. ];
  2488. $map[$value['type']][] = $tmp;
  2489. }
  2490. }
  2491. }
  2492. foreach ($customer as $key => $value){
  2493. if(isset($map[$value['value']])){
  2494. $tmp = $map[$value['value']];
  2495. $customer[$key]['list'] = $tmp;
  2496. $customer[$key]['count'] = count($tmp);
  2497. }
  2498. }
  2499. return $customer;
  2500. }
  2501. }