StatisticsService.php 83 KB

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