ReplenishmentController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\AsyncResponse;
  4. use App\Owner;
  5. use App\Services\ReplenishmentService;
  6. use Illuminate\Http\Request;
  7. use Oursdreams\Export\Export;
  8. class ReplenishmentController extends Controller
  9. {
  10. use AsyncResponse;
  11. public function getReplenishmentInfoByCustomer(Request $request)
  12. {
  13. $this->gate("库存管理-补货列表");
  14. $customer = $request->input('customer');
  15. if (!$customer) $this->error('请选择指定货主!');
  16. $orders = $this->getOrders($customer);
  17. $orders = array_filter($orders,function($item){
  18. return $item['amount'] > 0;
  19. });
  20. if (count($orders) > 0) $this->success($orders);
  21. else $this->error('未查询到相应信息!');
  22. }
  23. public function index(Request $request)
  24. {
  25. $this->gate("库存管理-补货列表");
  26. $owners = Owner::query()->whereIn('id', app("OwnerService")->getQuery())->get();
  27. return view('inventory.stockOut.index', compact('owners'));
  28. }
  29. public function export(Request $request)
  30. {
  31. $this->gate("库存管理-补货列表");
  32. $customer = $request->input('customer');
  33. $orders = array_filter($this->getOrders($customer),function($item){
  34. return $item['amount'] > 0;
  35. });
  36. $row = [
  37. '货主',
  38. '商品名称',
  39. '商品条码',
  40. '拣货库位',
  41. '缺货数',
  42. '存储库位',
  43. '数量',
  44. '批次号'
  45. ];
  46. $json = [];
  47. foreach ($orders as $order) {
  48. $eaLocation = '';
  49. $rsLocation = '';
  50. $qty = '';
  51. $lot = '';
  52. foreach ($order['eaLocation'] as $v) {
  53. $eaLocation .= $v . "\r\n";
  54. }
  55. foreach ($order['rsLocation'] as $v) {
  56. $rsLocation .= $v['location'] ."\r\n";
  57. }
  58. foreach ($order['rsLocation'] as $v) {
  59. $qty .= $v['qty'] ."\r\n";
  60. }
  61. foreach ($order['rsLocation'] as $v) {
  62. $lot .= $v['lot'] ."\r\n";
  63. }
  64. $data = [
  65. $order['customerid'],
  66. $order['sku'],
  67. $order['alternate_sku1'],
  68. $eaLocation,
  69. $order['amount'],
  70. $rsLocation,
  71. $qty,
  72. $lot,
  73. ];
  74. $json[] = $data;
  75. }
  76. return Export::make($row, $json, "库存管理-补货列表");
  77. }
  78. /**
  79. * @param $customer
  80. * @return array
  81. */
  82. private function getOrders($customer): array
  83. {
  84. /** @var ReplenishmentService $service */
  85. $service = app('ReplenishmentService');
  86. $orders = $service->getOrderInfoRecentMonthByCustomer($customer);
  87. $skuArr = array_diff(array_unique(data_get($orders, '*.sku')), ['', '*', null]);
  88. $info = $service->getEaInventoryByCustomerAndSku($customer, $skuArr);
  89. $rsInfo = $service->getRsInventoryByCustomerAndSku($customer, $skuArr);
  90. //将整合后拣货位信息 结合订单汇总
  91. foreach ($orders as &$order) {
  92. if (isset($info[$order['sku']])) {
  93. $item = $info[$order['sku']];
  94. $order['amount'] = ($order['amount'] - $item['qty']);
  95. $order['eaLocation'] = $item['locationid'];
  96. } else {
  97. $order['eaLocation'] = [];
  98. }
  99. if (isset($rsInfo[$order['sku']])) {
  100. $rsItem = $rsInfo[$order['sku']];
  101. $order['rsLocation'] = $rsItem['locationid'];
  102. } else {
  103. $order['rsLocation'] = [];
  104. }
  105. }
  106. return $orders;
  107. }
  108. }