ReplenishmentController.php 3.3 KB

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