OrderPackageCommoditiesService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Services;
  3. use App\Commodity;
  4. use App\OracleActAllocationDetails;
  5. use App\OracleBasSKU;
  6. use App\OracleDOCOrderDetail;
  7. use App\Order;
  8. use App\OrderPackage;
  9. use App\OrderPackageCommodities;
  10. use App\Owner;
  11. use Illuminate\Support\Arr;
  12. class OrderPackageCommoditiesService
  13. {
  14. public function basedOnOracleDetailsStore($orderNo, $orderPackage)
  15. {
  16. $details = OracleDOCOrderDetail::where('orderNo', $orderNo)->get();
  17. foreach ($details as $key => $detail) {
  18. $this->createByOrderDetail($detail, $orderPackage);
  19. }
  20. }
  21. public function basedOnActAllocationDetailsStoreByOrderNo($orderNo,$orderPackage){
  22. $details = OracleActAllocationDetails::where('orderno', $orderNo)->get();
  23. foreach ($details as $key => $detail) {
  24. $this->createByActAllocationDetails($detail, $orderPackage);
  25. }
  26. }
  27. public function basedOnActAllocationDetailsStore($orderPackage)
  28. {
  29. $details = OracleActAllocationDetails::where('picktotraceid', $orderPackage->logistic_number)->get();
  30. foreach ($details as $key => $detail) {
  31. $this->createByActAllocationDetails($detail, $orderPackage);
  32. }
  33. }
  34. private function createByActAllocationDetails($detail, OrderPackage $orderPackage)
  35. {
  36. $owner = Owner::where(['code' => $detail->customerid])->first(); // 货主
  37. $sku = $detail->sku;
  38. $owner_id = $owner['id'];
  39. $commodity = Commodity::where(['sku' => $sku, 'owner_id' => $owner_id])->first();
  40. if ($commodity == null) {
  41. $basSku = OracleBasSKU::where(['sku' => $sku, 'customerid' => $detail->customerid])->first();
  42. $commodity = Commodity::create(['sku' => $sku, 'owner_id' => $owner_id, 'name' => $basSku->descr_c]);
  43. }
  44. return OrderPackageCommodities::create(['order_package_id' => $orderPackage['id'], 'commodity_id' => $commodity['id'], 'amount' => $detail['qty']]);
  45. }
  46. public function getOrderPackageCommoditiesByOrderId($orderId)
  47. {
  48. $order = Order::where('id', $orderId)->first();
  49. if (!$order) {
  50. return null;
  51. }
  52. $orderPackageIds = OrderPackage::select('id')->where('order_id', $order['id'])->get();
  53. if (!$orderPackageIds) {
  54. return null;
  55. }
  56. return OrderPackageCommodities::with('commodity')->whereIn('order_package_id', $orderPackageIds)->get();
  57. }
  58. public function createByOrderDetail(OracleDOCOrderDetail $detail, OrderPackage $orderPackage)
  59. {
  60. $owner = Owner::where(['code' => $detail['customerid']])->first(); // 货主
  61. $sku = $detail->sku; // sku
  62. $owner_id = $owner->id; // 货主id
  63. $commodity = Commodity::where(['sku' => $sku, 'owner_id' => $owner_id])->first(); // 商品
  64. if ($commodity == null) {
  65. $basSku = OracleBasSKU::where(['sku' => $sku, 'customerid' => $detail->customerid])->first(); // 没有找到对应的商品信息
  66. $commodity = Commodity::create(['sku' => $sku, 'owner_id' => $owner_id, 'name' => $basSku->descr_c]);
  67. }
  68. return OrderPackageCommodities::create(['order_package_id' => $orderPackage['id'], 'commodity_id' => $commodity['id'], 'amount' => $detail['qtyordered']]);
  69. }
  70. }