ReplenishmentService.php 6.7 KB

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