StatisticsService.php 83 KB

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