StatisticsService.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Depart;
  4. use App\Model\InOutRecord;
  5. use App\Model\InvoiceOrder;
  6. use App\Model\InvoiceOrderInfo;
  7. use App\Model\Product;
  8. use App\Model\ProductCategory;
  9. use App\Model\PurchaseOrder;
  10. use App\Model\PurchaseOrderInfo;
  11. use App\Model\ReturnExchangeOrder;
  12. use App\Model\ReturnExchangeOrderProductInfo;
  13. use App\Model\SalesOrder;
  14. use App\Model\SalesOrderProductInfo;
  15. use Illuminate\Support\Facades\DB;
  16. class StatisticsService extends Service
  17. {
  18. //销售统计饼图 根据合同类型区分(例如线上合同、线下合同)
  19. public function statisticsBt($data,$user){
  20. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择销售订单时间区间'];
  21. $model = SalesOrder::Clear($user,$data);
  22. $model = $model->where('del_time',0)
  23. ->select('model_type',DB::raw("sum(contract_fee) as total"))
  24. ->groupBy('model_type');
  25. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  26. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  27. $model->where('crt_time','>=',$return[0]);
  28. $model->where('crt_time','<=',$return[1]);
  29. }
  30. $list = $model->get()->toArray();
  31. $list = $this->fillStatisticsBt($list);
  32. return [true, $list];
  33. }
  34. public function fillStatisticsBt($data){
  35. if(empty($data)) return $data;
  36. foreach ($data as $key => $value){
  37. $data[$key]['model_type_title'] = SalesOrder::$model_type_title[$value['model_type']] ?? "";
  38. }
  39. return $data;
  40. }
  41. //大区 订货统计
  42. public function statisticsArea($data,$user){
  43. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  44. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  45. //大区
  46. $area = Depart::$area;
  47. $area_map = [];
  48. foreach ($area as $key => $value){
  49. $area_map[] = [
  50. 'key' => $key,
  51. 'title' => $value,
  52. 'total' => 0
  53. ];
  54. }
  55. //分社所属大区
  56. $depart_map = Depart::where('parent_id',0)
  57. ->where('del_time',0)
  58. ->pluck('area','id')
  59. ->toArray();
  60. //向总社采购的钱
  61. $purchase_map = [];
  62. $purchase = PurchaseOrder::where('del_time',0)
  63. ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  64. ->where('crt_time','>=',$return[0])
  65. ->where('crt_time','<=',$return[1])
  66. ->select('top_depart_id','purchase_total')
  67. ->get()->toArray();
  68. foreach ($purchase as $value){
  69. $area = $depart_map[$value['top_depart_id']] ?? 0;
  70. if(! $area) continue;
  71. if(isset($purchase_map[$area])){
  72. $total = bcadd($purchase_map[$area], $value['purchase_total'],2);
  73. $purchase_map[$area] = $total;
  74. }else{
  75. $purchase_map[$area] = $value['purchase_total'];
  76. }
  77. }
  78. //退货的差异
  79. $returnExchange_map = [];
  80. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  81. ->where('type',ReturnExchangeOrder::Order_type2)
  82. ->where('crt_time','>=',$return[0])
  83. ->where('crt_time','<=',$return[1])
  84. ->where('crt_time','<=',$return[1])
  85. ->select('top_depart_id','difference_amount')
  86. ->get()->toArray();
  87. foreach ($returnExchange as $value){
  88. $area = $depart_map[$value['top_depart_id']] ?? 0;
  89. if(! $area) continue;
  90. if(isset($returnExchange_map[$area])){
  91. $total = bcadd($returnExchange_map[$area], $value['difference_amount'],2);
  92. $returnExchange_map[$area] = $total;
  93. }else{
  94. $returnExchange_map[$area] = $value['difference_amount'];
  95. }
  96. }
  97. foreach ($area_map as $key => $value){
  98. $purchase_tmp = $purchase_map[$value['key']] ?? 0;
  99. $return_tmp = $returnExchange_map[$value['key']] ?? 0;
  100. $area_map[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  101. }
  102. return [true, $area_map];
  103. }
  104. // 大区下的门店 订货统计
  105. public function statisticsAreaDepart($data,$user){
  106. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  107. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  108. if(empty($data['area']) || ! isset(Depart::$area[$data['area']])) return [false, '该大区不存在'];
  109. $depart = Depart::where('parent_id',0)
  110. ->where('del_time',0)
  111. ->where('area',$data['area'])
  112. ->select('id','title')
  113. ->get()
  114. ->toArray();
  115. if(empty($depart)) return [false, '该大区下不存在门店'];
  116. //向总社采购的钱
  117. $purchase_map = [];
  118. $purchase = PurchaseOrder::where('del_time',0)
  119. ->whereIn('top_depart_id',array_column($depart,'id'))
  120. ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  121. ->where('crt_time','>=',$return[0])
  122. ->where('crt_time','<=',$return[1])
  123. ->select('top_depart_id','purchase_total')
  124. ->get()->toArray();
  125. foreach ($purchase as $value){
  126. if(isset($purchase_map[$value['top_depart_id']])){
  127. $total = bcadd($purchase_map[$value['top_depart_id']], $value['purchase_total'],2);
  128. $purchase_map[$value['top_depart_id']] = $total;
  129. }else{
  130. $purchase_map[$value['top_depart_id']] = $value['purchase_total'];
  131. }
  132. }
  133. //退货的差异
  134. $returnExchange_map = [];
  135. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  136. ->whereIn('top_depart_id',array_column($depart,'id'))
  137. ->where('type',ReturnExchangeOrder::Order_type2)
  138. ->where('crt_time','>=',$return[0])
  139. ->where('crt_time','<=',$return[1])
  140. ->where('crt_time','<=',$return[1])
  141. ->select('top_depart_id','difference_amount')
  142. ->get()->toArray();
  143. foreach ($returnExchange as $value){
  144. if(isset($returnExchange_map[$value['top_depart_id']])){
  145. $total = bcadd($returnExchange_map[$value['top_depart_id']], $value['difference_amount'],2);
  146. $returnExchange_map[$value['top_depart_id']] = $total;
  147. }else{
  148. $returnExchange_map[$value['top_depart_id']] = $value['difference_amount'];
  149. }
  150. }
  151. foreach ($depart as $key => $value){
  152. $purchase_tmp = $purchase_map[$value['id']] ?? 0;
  153. $return_tmp = $returnExchange_map[$value['id']] ?? 0;
  154. $depart[$key]['total'] = bcsub($purchase_tmp, $return_tmp,2);
  155. }
  156. return [true, $depart];
  157. }
  158. // 大区下的门店 某个门店 关于产品以及分类的统计 订货统计
  159. public function statisticsAreaDepartProduct($data,$user){
  160. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  161. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  162. if(empty($data['top_depart_id'])) return [false, '请选择门店'];
  163. //时间 =》 钱
  164. $purchase_map1 = $this->getTime($return);
  165. //产品 =》 数量
  166. $purchase_category_map = $purchase_map = [];
  167. $purchase = PurchaseOrder::where('del_time',0)
  168. ->where('top_depart_id',$data['top_depart_id'])
  169. ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  170. ->where('crt_time','>=',$return[0])
  171. ->where('crt_time','<=',$return[1])
  172. ->select('id','crt_time')
  173. ->get()->toArray();
  174. $purchase_for_time = array_column($purchase,'crt_time','id');
  175. $purchase_product = PurchaseOrderInfo::where("del_time",0)
  176. ->whereIn('purchase_order_id',array_column($purchase,'id'))
  177. ->select('purchase_order_id','product_id','number','price','final_amount','sports_bag_id')
  178. ->get()->toArray();
  179. //退换货
  180. $returnExchange_map = $this->returnExchange($data,$return);
  181. //产品
  182. $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys($returnExchange_map,'product_id'))))
  183. ->select('id','product_category','title')
  184. ->get()->toArray();
  185. $product_list_map = array_column($product_list,null,'id');
  186. $category_return = ProductCategory::where('del_time',0)
  187. ->where('parent_id',0)
  188. ->pluck('title','id')
  189. ->toArray();
  190. foreach ($purchase_product as $value){
  191. if($value['sports_bag_id'] > 0){
  192. $money = $value['final_amount'];
  193. }elseif($value['final_amount'] > 0){
  194. $money = $value['final_amount'];
  195. }else{
  196. $money = bcmul($value['price'],$value['number'],2);
  197. }
  198. $crt_time = $purchase_for_time[$value['purchase_order_id']];
  199. $crt_time = date("Y-m-d",$crt_time);
  200. //产品信息
  201. $product_tmp = $product_list_map[$value['product_id']] ?? [];
  202. //钱
  203. if(isset($purchase_map1[$crt_time])){
  204. $purchase_map1[$crt_time]['total'] = bcadd($purchase_map1[$crt_time]['total'],$money,2);
  205. }
  206. //-------根据产品
  207. //数量
  208. if(isset($purchase_map[$value['product_id']])){
  209. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  210. $purchase_map[$value['product_id']]['number'] = $tmp_number;
  211. }else{
  212. $purchase_map[$value['product_id']] = [
  213. 'title' => $product_tmp['title'],
  214. 'number' => $value['number']
  215. ];
  216. }
  217. //-------根据产品
  218. //-------根据产品大类
  219. //数量
  220. $category_tmp = json_decode($product_tmp['product_category']);
  221. $category_tmp = $category_tmp[0];
  222. if(isset($purchase_category_map[$category_tmp])){
  223. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  224. $purchase_category_map[$category_tmp]['number'] = $tmp_number;
  225. }else{
  226. $purchase_category_map[$category_tmp] = [
  227. 'number' => $value['number'],
  228. 'title' => $category_return[$category_tmp] ?? ""
  229. ];
  230. }
  231. }
  232. return [true, ['money' => array_values($purchase_map1), 'product_num' => array_values($purchase_map), 'category_num' => array_values($purchase_category_map)]];
  233. }
  234. public function getTime($data){
  235. $startTimestamp = $data[0]; // 起始时间戳
  236. $endTimestamp = $data[1]; // 结束时间戳
  237. // 创建 DateTime 对象
  238. $startDate = new \DateTime();
  239. $endDate = new \DateTime();
  240. $startDate->setTimestamp($startTimestamp);
  241. $endDate->setTimestamp($endTimestamp);
  242. // 创建 DatePeriod 对象
  243. $interval = new \DateInterval('P1D'); // 每天的间隔
  244. $dateRange = new \DatePeriod($startDate, $interval, $endDate);
  245. // 遍历日期范围并输出每个日期
  246. $return = [];
  247. foreach ($dateRange as $date) {
  248. $day = $date->format('Y-m-d');
  249. $return[$day] = [
  250. 'day' => $day,
  251. 'total' => 0
  252. ];
  253. }
  254. return $return;
  255. }
  256. public function returnExchange($data,$return){ return [];
  257. $returnExchange_map = [];
  258. $returnExchange = ReturnExchangeOrder::where('del_time',0)
  259. ->where('top_depart_id',$data['top_depart_id'])
  260. ->where('type',ReturnExchangeOrder::Order_type2)
  261. ->where('crt_time','>=',$return[0])
  262. ->where('crt_time','<=',$return[1])
  263. ->select('id')
  264. ->get()->toArray();
  265. $returnExchange_product = ReturnExchangeOrderProductInfo::where("del_time",0)
  266. ->whereIn('return_exchange_id',array_column($returnExchange,'id'))
  267. ->select('product_id','number','return_exchange_price as price')
  268. ->get()->toArray();
  269. foreach ($returnExchange_product as $value){
  270. $money = bcmul($value['price'],$value['number'],2);
  271. if(isset($returnExchange_map[$value['product_id']])){
  272. $tmp_money = bcadd($returnExchange_map[$value['product_id']]['total'], $money,2);
  273. $tmp_number = bcadd($returnExchange_map[$value['product_id']]['number'], $value['number'],2);
  274. $returnExchange_map[$value['product_id']] = [
  275. 'number' => $tmp_number,
  276. 'total' => $tmp_money
  277. ];
  278. }else{
  279. $returnExchange_map[$value['product_id']] = [
  280. 'number' => $value['number'],
  281. 'total' => $money
  282. ];
  283. }
  284. }
  285. return $returnExchange_map;
  286. }
  287. public function statisticsJc($data,$user){
  288. if(empty($data['top_depart_id'])) return [false, '请选择门店'];
  289. $model = Product::ProductClear2($user,$data);
  290. $model = $model->where('del_time',0)
  291. ->select('title','id','code','depart_id','top_depart_id','product_attribute')
  292. ->orderby('product_attribute', 'desc')
  293. ->orderby('id', 'desc');
  294. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  295. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  296. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  297. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  298. if(! empty($data['product_category'])) {
  299. $product_category = ProductCategory::where('del_time',0)
  300. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  301. ->select('id')
  302. ->get()->toArray();
  303. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  304. }
  305. $list = $this->limit($model,'',$data);
  306. $list = $this->fillData($list,$user,$data);
  307. return [true, $list];
  308. }
  309. public function fillData($data, $user, $search){
  310. if(empty($data['data'])) return $data;
  311. //产品
  312. $product = array_column($data['data'],'id');
  313. //本月入库 本月出库
  314. list($in, $out) = $this->getThisMonthData($product,$user,$search);
  315. //上月结存 汇总这个月之前的出入
  316. list($last_in,$last_out) = $this->getLastMonthBalance($product,$user,$search);
  317. foreach ($data['data'] as $key => $value){
  318. $last_in_tmp = $last_in[$value['id']] ?? [];
  319. $last_in_money = $last_in_tmp['total'] ?? 0;//本月之前入的金额
  320. $last_in_number = $last_in_tmp['number'] ?? 0;//本月之前入的数量
  321. $last_out_tmp = $last_out[$value['id']] ?? [];
  322. $last_out_money = $last_out_tmp['total'] ?? 0;//本月之前出的金额
  323. $last_out_number = $last_out_tmp['number'] ?? 0;//本月之前出的数量
  324. //上月结存
  325. $last_jc_money = bcsub($last_in_money,$last_out_money,2);//结存金额
  326. $last_jc_number = bcsub($last_in_number,$last_out_number,2);//结存数量
  327. $last_jc_price = floatval($last_jc_number) ? bcdiv($last_jc_money,$last_jc_number,2) : 0;//结存单价
  328. $data['data'][$key]['last_jc_money'] = $last_jc_money;
  329. $data['data'][$key]['last_jc_number'] = $last_jc_number;
  330. $data['data'][$key]['last_jc_price'] = $last_jc_price;
  331. //本月入库
  332. $this_in_tmp = $in[$value['id']] ?? [];
  333. $this_in_money = $this_in_tmp['total'] ?? 0;//本月入的金额
  334. $this_in_number = $this_in_tmp['number'] ?? 0;//本月入的数量
  335. $this_in_price = floatval($this_in_number) ? bcdiv($this_in_money,$this_in_number,2) : 0;//本月入单价
  336. $data['data'][$key]['this_in_money'] = $this_in_money;
  337. $data['data'][$key]['this_in_number'] = $this_in_number;
  338. $data['data'][$key]['this_in_price'] = $this_in_price;
  339. //本月出库
  340. $this_out_tmp = $out[$value['id']] ?? [];
  341. $this_out_money = $this_out_tmp['total'] ?? 0;//本月出的金额
  342. $this_out_number = $this_out_tmp['number'] ?? 0;//本月出的数量
  343. //单价= (上月结存金额+本月入库金额) /上月结存数量+本月入库数量
  344. $amount = bcadd($last_jc_money, $this_in_money,2);
  345. $number = bcadd($last_jc_number, $this_in_number,2);
  346. $this_out_price = floatval($number) ? bcdiv($amount,$number,2) : 0;//本月出单价
  347. $data['data'][$key]['this_out_money'] = $this_out_money;
  348. $data['data'][$key]['this_out_number'] = $this_out_number;
  349. $data['data'][$key]['this_out_price'] = $this_out_price;
  350. //本月结存
  351. //本月结存 数量/金额=本月入库+上月结存-本月出库
  352. $this_jc_money = bcsub(bcadd($this_in_money,$last_jc_money,2),$this_out_money);
  353. $this_jc_number = bcsub(bcadd($this_in_number,$last_jc_number,2),$this_out_number);
  354. $this_jc_price = floatval($this_jc_number) ? bcdiv($this_jc_money,$this_jc_number,2) : 0;//本月结存单价
  355. $data['data'][$key]['this_jc_money'] = $this_jc_money;
  356. $data['data'][$key]['this_jc_number'] = $this_jc_number;
  357. $data['data'][$key]['this_jc_price'] = $this_jc_price;
  358. }
  359. return $data;
  360. }
  361. //本月入库 出库(库存流水)
  362. public function getThisMonthData($product = [], $user = [], $search = []){
  363. $in = $out = [];
  364. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  365. $endStamp = strtotime(date("Y-m-t 23:59:59"));
  366. //本月出和入的数据
  367. $model = InOutRecord::TopClear($user,$search);
  368. $list = $model->where('del_time',0)
  369. ->where('crt_time','>=',$startStamp)
  370. ->where('crt_time','<=',$endStamp)
  371. ->whereIn('product_id',$product)
  372. ->select('product_id','number','price')
  373. ->get()->toArray();
  374. foreach ($list as $value){
  375. if($value['number'] >= 0){
  376. $tmp_total = bcmul($value['number'], $value['price'], 2);
  377. if(isset($in[$value['product_id']])){
  378. $number = bcadd($in[$value['product_id']]['number'], $value['number'],0);
  379. $total = bcadd($in[$value['product_id']]['total'], $tmp_total,2);
  380. $in[$value['product_id']]['number'] = $number;
  381. $in[$value['product_id']]['total'] = $total;
  382. }else{
  383. $in[$value['product_id']] = [
  384. 'number' => $value['number'],
  385. 'total' => $tmp_total,
  386. ];
  387. }
  388. }else{
  389. $number = abs($value['number']);
  390. $tmp_total = bcmul($number, $value['price'], 2);
  391. if(isset($out[$value['product_id']])){
  392. $number = bcadd($out[$value['product_id']]['number'], $number,0);
  393. $total = bcadd($out[$value['product_id']]['total'], $tmp_total,2);
  394. $out[$value['product_id']]['number'] = $number;
  395. $out[$value['product_id']]['total'] = $total;
  396. }else{
  397. $out[$value['product_id']] = [
  398. 'number' => $number,
  399. 'total' => $tmp_total,
  400. ];
  401. }
  402. }
  403. }
  404. return [$in, $out];
  405. }
  406. //上月结存(汇总这个月之前的出入)(库存流水)
  407. public function getLastMonthBalance($product = [], $user = [], $search = []){
  408. $return = [];
  409. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  410. $model = InOutRecord::TopClear($user,$search);
  411. $list = $model->where('del_time',0)
  412. ->where('crt_time','<',$startStamp)
  413. ->whereIn('product_id',$product)
  414. ->select('product_id','number','price')
  415. ->get()->toArray();
  416. foreach ($list as $value){
  417. if($value['number'] >= 0){
  418. $tmp_total = bcmul($value['number'], $value['price'], 2);
  419. if(isset($in[$value['product_id']])){
  420. $number = bcadd($in[$value['product_id']]['number'], $value['number'],0);
  421. $total = bcadd($in[$value['product_id']]['total'], $tmp_total,2);
  422. $in[$value['product_id']]['number'] = $number;
  423. $in[$value['product_id']]['total'] = $total;
  424. }else{
  425. $in[$value['product_id']] = [
  426. 'number' => $value['number'],
  427. 'total' => $tmp_total,
  428. ];
  429. }
  430. }else{
  431. $number = abs($value['number']);
  432. $tmp_total = bcmul($number, $value['price'], 2);
  433. if(isset($out[$value['product_id']])){
  434. $number = bcadd($out[$value['product_id']]['number'], $number,0);
  435. $total = bcadd($out[$value['product_id']]['total'], $tmp_total,2);
  436. $out[$value['product_id']]['number'] = $number;
  437. $out[$value['product_id']]['total'] = $total;
  438. }else{
  439. $out[$value['product_id']] = [
  440. 'number' => $number,
  441. 'total' => $tmp_total,
  442. ];
  443. }
  444. }
  445. }
  446. return [$in, $out];
  447. }
  448. //本月入库(单据)暂时没用
  449. public function getThisMonthIn1($product = [], $user = [], $search = []){
  450. $return = [];
  451. $startStamp = strtotime(date("Y-m-01 00:00:00"));
  452. $endStamp = strtotime(date("Y-m-t 23:59:59"));
  453. //本月采购单
  454. $model = PurchaseOrder::Clear($user,$search);
  455. $list = $model->where('del_time',0)
  456. ->where('state', PurchaseOrder::STATE_Four)
  457. ->where('crt_time','>=',$startStamp)
  458. ->where('crt_time','<=',$endStamp)
  459. ->select('id')
  460. ->get()->toArray();
  461. if(empty($list)) return $return;
  462. //本月采购产品
  463. $purchase_product_array = [];
  464. $purchase_product = PurchaseOrderInfo::where('del_time',0)
  465. ->whereIn('product_id',$product)
  466. ->whereIn('purchase_order_id',array_column($list,'id'))
  467. ->select('product_id','number','price')
  468. ->get()->toArray();
  469. foreach ($purchase_product as $value){
  470. $total = bcmul($value['number'],$value['price'],2);
  471. if(isset($purchase_product_array[$value['product_id']])){
  472. $purchase_product_array[$value['product_id']]['number'] += $value['number'];
  473. $total_tmp = bcadd($purchase_product_array[$value['product_id']]['total'], $total, 2);
  474. $purchase_product_array[$value['product_id']]['total'] = $total_tmp;
  475. }else{
  476. $purchase_product_array[$value['product_id']] = [
  477. 'number' => $value['number'],
  478. 'total' => $total,
  479. ];
  480. }
  481. }
  482. //本月退货(采购)
  483. $model2 = ReturnExchangeOrder::Clear($user, $search);
  484. $return_list = $model2->where('del_time',0)
  485. ->where('state', ReturnExchangeOrder::State_two)
  486. ->where('type',ReturnExchangeOrder::Order_type2)
  487. ->where('crt_time','>=',$startStamp)
  488. ->where('crt_time','<=',$endStamp)
  489. ->select('id')
  490. ->get()->toArray();
  491. //本月退货产品
  492. $return_product_array = [];
  493. if(! empty($return_list)){
  494. $return_product = ReturnExchangeOrderProductInfo::where('del_time',0)
  495. ->where('return_exchange_id', array_column($return_list, 'id'))
  496. ->whereIn('product_id',$product)
  497. ->where('return_or_exchange',ReturnExchangeOrderProductInfo::type_one)
  498. ->select('product_id','number','return_or_exchange')
  499. ->get()->toArray();
  500. foreach ($return_product as $value){
  501. $total = bcmul($value['number'],$value['return_or_exchange'],2);
  502. if(isset($return_product_array[$value['product_id']])){
  503. $return_product_array[$value['product_id']]['number'] += $value['number'];
  504. $total_tmp = bcadd($return_product_array[$value['product_id']]['total'], $total, 2);
  505. $return_product_array[$value['product_id']]['total'] = $total_tmp;
  506. }else{
  507. $return_product_array[$value['product_id']] = [
  508. 'number' => $value['number'],
  509. 'total' => $total,
  510. ];
  511. }
  512. }
  513. }
  514. foreach ($return_product_array as $p => $n){
  515. $number_tmp = -$n['number'];
  516. $total_tmp = -$n['total'];
  517. $purchase_product_tmp = $purchase_product_array[$p] ?? [];
  518. if(empty($purchase_product_tmp)){
  519. $purchase_product_array[$p] = [
  520. 'number' => $number_tmp,
  521. 'total' => $total_tmp,
  522. ];
  523. }else{
  524. $purchase_product_array[$p]['number'] += $number_tmp;
  525. $total_tmp2 = bcadd($purchase_product_array[$p]['total'], $total_tmp, 2);
  526. $purchase_product_array[$p]['total'] += $total_tmp2;
  527. }
  528. }
  529. return $purchase_product_array;
  530. }
  531. //--------------------------脚本
  532. public function inoutrecord(){return;
  533. $in_data = InvoiceOrder::where('del_time',0)
  534. ->select('id','sales_order_id')
  535. ->get()->toArray();
  536. $map = [];
  537. $s_p = SalesOrderProductInfo::whereIn('sales_order_id',array_column($in_data,'sales_order_id'))
  538. ->where('del_time',0)
  539. ->select('sales_order_id','product_id','final_amount','price')
  540. ->get()->toArray();
  541. foreach ($s_p as $value){
  542. $map[$value['sales_order_id']][] = [
  543. 'product_id' => $value['product_id'],
  544. 'final_amount' => $value['final_amount'],
  545. 'price' => $value['price'],
  546. ];
  547. }
  548. DB::beginTransaction();
  549. try {
  550. foreach ($in_data as $value){
  551. $tmp = $map[$value['sales_order_id']] ?? [];
  552. if(empty($tmp)) continue;
  553. foreach ($tmp as $val){
  554. InvoiceOrderInfo::where('del_time',0)
  555. ->where('invoice_id', $value['id'])
  556. ->where('product_id', $val['product_id'])
  557. ->update([
  558. 'final_amount' => $val['final_amount'],
  559. 'price' => $val['price'],
  560. ]);
  561. }
  562. }
  563. }catch (\Throwable $exception){
  564. DB::rollBack();
  565. dd($exception->getMessage());
  566. }
  567. DB::commit();
  568. dd(1);
  569. }
  570. public function inoutrecord2(){return;
  571. DB::beginTransaction();
  572. try {
  573. DB::table('in_out_record')
  574. ->where('price',0)
  575. ->whereIn('order_type', array_values(ReturnExchangeOrder::$prefix))
  576. ->select('id','order_number','product_id')
  577. ->orderBy('id','asc')
  578. ->chunk(200,function ($data) {;
  579. $data = Collect($data)->map(function ($object) {
  580. return (array)$object;
  581. })->toArray();
  582. $map = ReturnExchangeOrder::where('del_time',0)
  583. ->whereIn('order_number',array_column($data,'order_number'))
  584. ->pluck('id','order_number')
  585. ->toArray();
  586. $result = ReturnExchangeOrderProductInfo::where('del_time',0)
  587. ->whereIn('return_exchange_id',array_values($map))
  588. ->select('return_exchange_id','return_exchange_price','product_id')
  589. ->get()->toArray();
  590. $result_map = [];
  591. foreach ($result as $v){
  592. $result_map[$v['return_exchange_id']][$v['product_id']] = [
  593. 'product_id' => $v['product_id'],
  594. 'price' => $v['return_exchange_price'],
  595. ];
  596. }
  597. foreach ($data as $value){
  598. $tmp_id = $map[$value['order_number']] ?? 0;
  599. if($tmp_id < 0) continue;
  600. $tmp = $result_map[$tmp_id][$value['product_id']] ?? [];
  601. if(empty($tmp)) continue;
  602. InOutRecord::where('id',$value['id'])->update([
  603. 'price' => $tmp['price']
  604. ]);
  605. }
  606. });
  607. DB::commit();
  608. }catch (\Throwable $exception){
  609. DB::rollBack();
  610. dd($exception->getMessage());
  611. }
  612. dd(1);
  613. }
  614. public function statisticsAreaDepartProduct222($data,$user){
  615. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '请选择时间区间'];
  616. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  617. if(empty($data['top_depart_id'])) return [false, '请选择门店'];
  618. //向总社采购的钱
  619. $purchase_map = [];
  620. $purchase = PurchaseOrder::where('del_time',0)
  621. ->where('top_depart_id',$data['top_depart_id'])
  622. ->where('order_type',[PurchaseOrder::Order_type_three,PurchaseOrder::Order_type_four])
  623. ->where('crt_time','>=',$return[0])
  624. ->where('crt_time','<=',$return[1])
  625. ->select('id')
  626. ->get()->toArray();
  627. $purchase_product = PurchaseOrderInfo::where("del_time",0)
  628. ->whereIn('purchase_order_id',array_column($purchase,'id'))
  629. ->select('product_id','number','price','final_amount','sports_bag_id')
  630. ->get()->toArray();
  631. foreach ($purchase_product as $value){
  632. if($value['sports_bag_id'] > 0){
  633. $money = $value['final_amount'];
  634. }elseif($value['final_amount'] > 0){
  635. $money = $value['final_amount'];
  636. }else{
  637. $money = bcmul($value['price'],$value['number'],2);
  638. }
  639. if(isset($purchase_map[$value['product_id']])){
  640. $tmp_money = bcadd($purchase_map[$value['product_id']]['total'], $money,2);
  641. $tmp_number = bcadd($purchase_map[$value['product_id']]['number'], $value['number'],2);
  642. $purchase_map[$value['product_id']] = [
  643. 'number' => $tmp_number,
  644. 'total' => $tmp_money
  645. ];
  646. }else{
  647. $purchase_map[$value['product_id']] = [
  648. 'number' => $value['number'],
  649. 'total' => $money
  650. ];
  651. }
  652. }
  653. //退货的差异
  654. $returnExchange_map = $this->returnExchange($data,$return);
  655. //产品
  656. $product_list = Product::whereIn('id',array_unique(array_merge_recursive(array_column($purchase_product,'product_id'),array_keys($returnExchange_map,'product_id'))))
  657. ->select('id','product_category','title')
  658. ->get()->toArray();
  659. $product_list_map = array_column($product_list,null,'id');
  660. $category = $category_sum = [];
  661. foreach ($purchase_map as $key => $value){
  662. // if(isset($returnExchange_map[$key])){
  663. // $tmp = $returnExchange_map[$key];
  664. // $number = bcsub($value['number'], $tmp['number'],2);
  665. // $total = bcsub($value['total'], $tmp['total'],2);
  666. // $purchase_map[$key] = [
  667. // 'number' => $number,
  668. // 'total' => $total,
  669. // ];
  670. // }
  671. $product_tmp = $product_list_map[$key] ?? [];
  672. $purchase_map[$key]['title'] = $product_tmp['title'];
  673. $category_tmp = json_decode($product_tmp['product_category']);
  674. $category_tmp = $category_tmp[0];
  675. if(! in_array($category_tmp,$category)) $category[] = $category_tmp;
  676. if(isset($category_sum[$category_tmp])){
  677. $tmp_number = bcadd($category_sum[$category_tmp]['number'], $purchase_map[$key]['number'],2);
  678. $tmp_total = bcadd($category_sum[$category_tmp]['total'], $purchase_map[$key]['total'],2);
  679. $category_sum[$category_tmp] = [
  680. 'number' => $tmp_number,
  681. 'total' => $tmp_total,
  682. ];
  683. }else{
  684. $category_sum[$category_tmp] = [
  685. 'number' => $purchase_map[$key]['number'],
  686. 'total' => $purchase_map[$key]['total'],
  687. ];
  688. }
  689. }
  690. //根据产品大类 $category_sum
  691. $category_return = ProductCategory::whereIn('id',$category)
  692. ->select('id','title')
  693. ->get()->toArray();
  694. foreach ($category_return as $key => $value){
  695. $tmp = $category_sum[$value['id']] ?? [];
  696. $category_return[$key]['number'] = $tmp['number'];
  697. $category_return[$key]['total'] = $tmp['total'];
  698. }
  699. //根据产品 $purchase_map
  700. dd($purchase_map);
  701. return [true, ''];
  702. }
  703. }