ReplenishmentController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. $json = [];
  47. foreach ($orders as $order) {
  48. $eaLocation = '';
  49. $rsLocation = '';
  50. foreach ($order['eaLocation'] as $v) {
  51. $eaLocation .= $v . ",\r\n";
  52. }
  53. foreach ($order['rsLocation'] as $v) {
  54. $rsLocation .= $v['location'] . ' :'.$v['qty'].",\r\n";
  55. }
  56. $data = [
  57. $order['customerid'],
  58. $order['sku'],
  59. $order['alternate_sku1'],
  60. $eaLocation,
  61. $order['amount'],
  62. $rsLocation,
  63. ];
  64. $json[] = $data;
  65. }
  66. return Export::make($row, $json, "库存管理-补货列表");
  67. }
  68. /**
  69. * @param $customer
  70. * @return array
  71. */
  72. private function getOrders($customer): array
  73. {
  74. /** @var ReplenishmentService $service */
  75. $service = app('ReplenishmentService');
  76. $orders = $service->getOrderInfoRecentMonthByCustomer($customer);
  77. $skuArr = array_diff(array_unique(data_get($orders, '*.sku')), ['', '*', null]);
  78. $info = $service->getEaInventoryByCustomerAndSku($customer, $skuArr);
  79. $rsInfo = $service->getRsInventoryByCustomerAndSku($customer, $skuArr);
  80. //将整合后拣货位信息 结合订单汇总
  81. foreach ($orders as &$order) {
  82. if (isset($info[$order['sku']])) {
  83. $item = $info[$order['sku']];
  84. $order['amount'] = ($order['amount'] - $item['qty']);
  85. $order['eaLocation'] = $item['locationid'];
  86. } else {
  87. $order['eaLocation'] = [];
  88. }
  89. if (isset($rsInfo[$order['sku']])) {
  90. $rsItem = $rsInfo[$order['sku']];
  91. $order['rsLocation'] = $rsItem['locationid'];
  92. } else {
  93. $order['rsLocation'] = [];
  94. }
  95. }
  96. return $orders;
  97. }
  98. }