ReplenishmentController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /** @var UserService $userService */
  27. $userService = app('UserService');
  28. $owners = Owner::query()->whereIn('id', $userService->getPermittingOwnerIds(auth()->user()))->get();
  29. return view('inventory.stockOut.index', compact('owners'));
  30. }
  31. public function export(Request $request)
  32. {
  33. $this->gate("库存管理-补货列表");
  34. $customer = $request->input('customer');
  35. $orders = array_filter($this->getOrders($customer),function($item){
  36. return $item['amount'] > 0;
  37. });
  38. $row = [
  39. '货主',
  40. '商品名称',
  41. '商品条码',
  42. '拣货库位',
  43. '缺货数',
  44. '存储库位',
  45. '数量',
  46. '批次号'
  47. ];
  48. $json = [];
  49. foreach ($orders as $order) {
  50. $eaLocation = '';
  51. $rsLocation = '';
  52. $qty = '';
  53. $lot = '';
  54. foreach ($order['eaLocation'] as $v) {
  55. $eaLocation .= $v . "\r\n";
  56. }
  57. foreach ($order['rsLocation'] as $v) {
  58. $rsLocation .= $v['location'] ."\r\n";
  59. }
  60. foreach ($order['rsLocation'] as $v) {
  61. $qty .= $v['qty'] ."\r\n";
  62. }
  63. foreach ($order['rsLocation'] as $v) {
  64. $lot .= $v['lot'] ."\r\n";
  65. }
  66. $data = [
  67. $order['customerid'],
  68. $order['sku'],
  69. $order['alternate_sku1'],
  70. $eaLocation,
  71. $order['amount'],
  72. $rsLocation,
  73. $qty,
  74. $lot,
  75. ];
  76. $json[] = $data;
  77. }
  78. return Export::make($row, $json, "库存管理-补货列表");
  79. }
  80. /**
  81. * @param $customer
  82. * @return array
  83. */
  84. private function getOrders($customer): array
  85. {
  86. /** @var ReplenishmentService $service */
  87. $service = app('ReplenishmentService');
  88. $orders = $service->getOrderInfoRecentMonthByCustomer($customer);
  89. $skuArr = array_diff(array_unique(data_get($orders, '*.sku')), ['', '*', null]);
  90. $info = $service->getEaInventoryByCustomerAndSku($customer, $skuArr);
  91. $rsInfo = $service->getRsInventoryByCustomerAndSku($customer, $skuArr);
  92. //将整合后拣货位信息 结合订单汇总
  93. foreach ($orders as &$order) {
  94. if (isset($info[$order['sku']])) {
  95. $item = $info[$order['sku']];
  96. $order['amount'] = ($order['amount'] - $item['qty']);
  97. $order['eaLocation'] = $item['locationid'];
  98. } else {
  99. $order['eaLocation'] = [];
  100. }
  101. if (isset($rsInfo[$order['sku']])) {
  102. $rsItem = $rsInfo[$order['sku']];
  103. $order['rsLocation'] = $rsItem['locationid'];
  104. } else {
  105. $order['rsLocation'] = [];
  106. }
  107. }
  108. return $orders;
  109. }
  110. }