ReplenishmentService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Services;
  3. use App\Traits\ServiceAppAop;
  4. use Carbon\Carbon;
  5. use Illuminate\Support\Facades\DB;
  6. class ReplenishmentService
  7. {
  8. use ServiceAppAop;
  9. /**
  10. * @param $customer
  11. * @return array
  12. * 根据货主,订单状态为拣货完成之前的
  13. * 订单信息
  14. */
  15. public function getOrderInfoRecentMonthByCustomer($customer): array
  16. {
  17. $db = DB::connection("oracle");
  18. $sql = <<<sql
  19. select orders.customerid, orders.sku,orders.amount,BAS_SKU.ALTERNATE_SKU1,BAS_SKU.ALTERNATE_SKU2,BAS_SKU.ALTERNATE_SKU3
  20. from (
  21. select customerid, sku, sum(QTYORDERED_EACH) amount
  22. from DOC_ORDER_DETAILS
  23. where ORDERNO in (select ORDERNO
  24. from DOC_ORDER_HEADER
  25. where SOSTATUS IN ('00', '10', '20', '30', '40', '50')
  26. AND customerid = ?
  27. AND ADDTIME >= TO_DATE(?, 'yyyy-mm-dd hh24:mi:ss')
  28. AND ADDTIME <= TO_DATE(?, 'yyyy-mm-dd hh24:mi:ss'))
  29. group by customerid, sku
  30. ) orders
  31. left join BAS_SKU on orders.customerid=BAS_SKU.customerid and orders.sku = BAS_SKU.SKU
  32. order by orders.amount desc
  33. sql;
  34. $res= $db->select(DB::raw($sql),[$customer,Carbon::now()->subDays(30)->toDateTimeString(),Carbon::now()->toDateTimeString()]);
  35. $orders=array();
  36. foreach ($res as $item){
  37. $orders[]=json_decode( json_encode( $item),true);
  38. }
  39. return $orders;
  40. }
  41. /**
  42. * @param $customer
  43. * @param array $skuArr
  44. * @return array
  45. * 根据货主和发货sku 获取库存拣货位及拣货位商品数量
  46. * 并整合为sku 为key的数组
  47. */
  48. public function getEaInventoryByCustomerAndSku($customer,array $skuArr): array
  49. {
  50. $db = DB::connection("oracle");
  51. $info=array();
  52. foreach (array_chunk($skuArr,1000) as $item){
  53. $sql = <<<sql
  54. select INV_LOT_LOC_ID.CUSTOMERID,INV_LOT_LOC_ID.SKU,INV_LOT_LOC_ID.LOCATIONID,sum(INV_LOT_LOC_ID.QTY) qty
  55. from INV_LOT_LOC_ID
  56. left join BAS_LOCATION on BAS_LOCATION.LOCATIONID = INV_LOT_LOC_ID.LOCATIONID
  57. where BAS_LOCATION.LOCATIONUSAGE = 'EA'
  58. AND customerid = ?
  59. sql;
  60. if (count($item) > 0) {
  61. $sql .= " and sku in (";
  62. foreach ($item as $index => $str) {
  63. if ($index == 0) {
  64. $sql .= "'" . $str . "'";
  65. continue;
  66. }
  67. $sql .= ",'" . $str . "'";
  68. }
  69. $sql .= ")";
  70. }
  71. $sql.= ' group by INV_LOT_LOC_ID.CUSTOMERID,INV_LOT_LOC_ID.SKU,INV_LOT_LOC_ID.LOCATIONID';
  72. $info[]= $db->select(DB::raw($sql),[$customer]);
  73. }
  74. $info = array_merge(...$info);
  75. //整合同sku下的拣货库位
  76. $tmpArray = array();
  77. foreach ($info as $row) {
  78. $key = $row->sku;
  79. if (array_key_exists($key, $tmpArray)) {
  80. if (is_array($tmpArray[$key]['locationid'])) {
  81. $tmpArray[$key]['locationid'][] = $row->locationid;
  82. } else {
  83. $tmpArray[$key]['locationid'] = array($tmpArray[$key]['locationid'], $row->locationid);
  84. }
  85. if ($tmpArray[$key]['qty']) {
  86. $tmpArray[$key]['qty'] += $row->qty;
  87. } else {
  88. $tmpArray[$key]['qty'] = $row->qty;
  89. }
  90. } else {
  91. $tmpArray[$key] = array('sku'=>$row->sku,'locationid'=>array($row->locationid),'qty'=>$row->qty);
  92. }
  93. }
  94. return $tmpArray;
  95. }
  96. /**
  97. * @param $customer
  98. * @param array $skuArr
  99. * @return array
  100. * 根据货主和发货sku 获取库存存储位及存储位商品数量
  101. * 并整合为sku 为key的数组
  102. */
  103. public function getRsInventoryByCustomerAndSku($customer,array $skuArr): array
  104. {
  105. $db = DB::connection("oracle");
  106. $info=array();
  107. foreach (array_chunk($skuArr,1000) as $item){
  108. $sql = <<<sql
  109. select INV_LOT_LOC_ID.CUSTOMERID,INV_LOT_LOC_ID.SKU,INV_LOT_LOC_ID.LOCATIONID,INV_LOT_LOC_ID.LOTNUM,sum(INV_LOT_LOC_ID.QTY) qty
  110. from INV_LOT_LOC_ID
  111. left join BAS_LOCATION on BAS_LOCATION.LOCATIONID = INV_LOT_LOC_ID.LOCATIONID
  112. where BAS_LOCATION.LOCATIONUSAGE = 'RS'
  113. AND customerid = ?
  114. sql;
  115. if (count($item) > 0) {
  116. $sql .= " and sku in (";
  117. foreach ($item as $index => $str) {
  118. if ($index == 0) {
  119. $sql .= "'" . $str . "'";
  120. continue;
  121. }
  122. $sql .= ",'" . $str . "'";
  123. }
  124. $sql .= ")";
  125. }
  126. $sql.= ' group by INV_LOT_LOC_ID.CUSTOMERID,INV_LOT_LOC_ID.SKU,INV_LOT_LOC_ID.LOCATIONID,INV_LOT_LOC_ID.LOTNUM order by qty desc';
  127. $info[]= $db->select(DB::raw($sql),[$customer]);
  128. }
  129. $info=array_merge(...$info);
  130. //整合同sku下的存储库位
  131. $tmpArray = array();
  132. foreach ($info as $row) {
  133. $key = $row->sku;
  134. if (array_key_exists($key, $tmpArray)) {
  135. if (is_array($tmpArray[$key]['locationid'])) {
  136. if (count($tmpArray[$key]['locationid'])<5){
  137. $tmpArray[$key]['locationid'][] = ['location'=>$row->locationid,'qty'=>$row->qty,'lot'=>$row->lotnum];
  138. }
  139. } else {
  140. $tmpArray[$key]['locationid'] = array($tmpArray[$key]['locationid'], ['location'=>$row->locationid,'qty'=>$row->qty,'lot'=>$row->lotnum]);
  141. }
  142. } else {
  143. $tmpArray[$key] = array('sku'=>$row->sku,'locationid'=>array(['location'=>$row->locationid,'qty'=>$row->qty,'lot'=>$row->lotnum]));
  144. }
  145. }
  146. return $tmpArray;
  147. }
  148. public function combinationSql(array $skuArr,$sql)
  149. {
  150. foreach (array_chunk($skuArr,1000) as $index=>$item){
  151. if ($index==0){
  152. $sql .= " and sku in (";
  153. foreach ($item as $i => $str) {
  154. if ($i == 0) {
  155. $sql .= "'" . $str . "'";
  156. continue;
  157. }
  158. $sql .= ",'" . $str . "'";
  159. }
  160. }else{
  161. $sql .= " or sku in (";
  162. foreach ($item as $a => $str) {
  163. if ($a == 0) {
  164. $sql .= "'" . $str . "'";
  165. continue;
  166. }
  167. $sql .= ",'" . $str . "'";
  168. }
  169. }
  170. $sql .= ")";
  171. }
  172. return $sql;
  173. }
  174. }