src/Service/CalculateService.php line 139

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Product;
  4. use App\Entity\Store;
  5. use App\Repository\CountryRepository;
  6. use App\Repository\FreightPriceRepository;
  7. use App\Repository\PalletRepository;
  8. use App\Repository\ProductRepository;
  9. use App\Repository\ProvinceRepository;
  10. use DVDoug\BoxPacker\Packer;
  11. use DVDoug\BoxPacker\Test\TestBox;
  12. use DVDoug\BoxPacker\Test\TestItem;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. /**
  15.  *
  16.  */
  17. class CalculateService
  18. {
  19.     /**
  20.      * @var PalletRepository
  21.      */
  22.     private $palleteRepo;
  23.     /**
  24.      * @var ProductRepository
  25.      */
  26.     private $productRepo;
  27.     /**
  28.      * @var Packer
  29.      */
  30.     private $Packer;
  31.     /**
  32.      * @var FreightPriceRepository
  33.      */
  34.     private $FPRepo;
  35.     /**
  36.      * @var CountryRepository
  37.      */
  38.     private $countryRepo;
  39.     /**
  40.      * @var ProvinceRepository
  41.      */
  42.     private $provinceRepo;
  43.     /**
  44.      * @param ContainerInterface $container
  45.      * @param PalletRepository $palletRepository
  46.      * @param ProductRepository $productRepository
  47.      * @param FreightPriceRepository $freightPriceRepository
  48.      * @param CountryRepository $countryRepository
  49.      * @param ProvinceRepository $provinceRepository
  50.      */
  51.     public function __construct(PalletRepository       $palletRepositoryProductRepository $productRepository,
  52.                                 FreightPriceRepository $freightPriceRepositoryCountryRepository $countryRepository,
  53.                                 ProvinceRepository     $provinceRepository)
  54.     {
  55.         $this->palleteRepo $palletRepository;
  56.         $this->productRepo $productRepository;
  57.         $this->FPRepo $freightPriceRepository;
  58.         $this->countryRepo $countryRepository;
  59.         $this->provinceRepo $provinceRepository;
  60.         $this->Packer = new Packer();
  61.         $this->Packer->setMaxBoxesToBalanceWeight(1);
  62.     }
  63.     /**
  64.      * @param $cart
  65.      * @param Store $store
  66.      * @return array
  67.      */
  68.     public function calculateShipment($cartStore $store)
  69.     {
  70.         $params = [];
  71.         $origin_country $this->countryRepo->findBy(['code' => $cart->rate->origin->country]);
  72.         $destination_country $this->countryRepo->findBy(['code' => $cart->rate->destination->country]);
  73.         $origin_province $this->provinceRepo->findBy(['code' => $cart->rate->origin->province]);
  74.         $destination_province $this->provinceRepo->findBy(['code' => $cart->rate->destination->province]);
  75.         $params['origin_country'] = $origin_country;
  76.         $params['destination_country'] = $destination_country;
  77.         if ($origin_province) {
  78.             $params['origin_province'] = $origin_province;
  79.         }
  80.         if ($destination_province) {
  81.             $isDestination $this->FPRepo->findBy(
  82.                 [
  83.                     'destination_country' => $destination_country,
  84.                     'destination_province' => $destination_province,
  85.                     'origin_country' => $origin_country
  86.                 ]
  87.             );
  88.             if ($isDestination) {
  89.                 $params['destination_province'] = $destination_province;
  90.             }
  91.         }
  92.         // $params['store'] = $store;
  93.         $prices $this->FPRepo->findBy($params);
  94.         foreach ($prices as $price) {
  95.             $pallet $price->getPallete();
  96.             $this->Packer->addBox(new TestBox(
  97.                 $pallet->getName(),
  98.                 $pallet->getWidth() + 1,
  99.                 $pallet->getLength() + 1,
  100.                 $pallet->getHeight() + 1,
  101.                 0,
  102.                 $pallet->getWidth() + 0,
  103.                 $pallet->getLength() + 0,
  104.                 $pallet->getHeight() + 0,
  105.                 $pallet->getWeight() + 0
  106.             ));
  107.         }
  108.         // Products
  109.         $cartItems = [];
  110.         foreach ($cart->rate->items as $item) {
  111.             $product $this->productRepo->findOneBy([
  112.                 'product_id' => $item->product_id,
  113.                 'variant_id' => $item->variant_id,
  114.                 // 'store' => $store
  115.             ]);
  116.             $cartItems[] = [
  117.                 'quantity' => $item->quantity,
  118.                 'product' => $product,
  119.                 'position' => $product->getPosition() ?? 999
  120.             ];
  121.         }
  122.         usort($cartItems, function ($a$b) {
  123.             return $a['position'] <=> $b['position'];
  124.         });
  125.         $singlePallets = [];
  126.         foreach ($cartItems as $item) {
  127.             assert($item['product'] instanceof Product);
  128.             $product $item['product'];
  129.             if($product->getIsSinglePalette()){
  130.                 $cart = clone $cart;
  131.                 $cart->rate->items array_filter($cart->rate->items, function ($item) use ($product) {
  132.                     return $item->product_id == $product->getProductId() && $item->variant_id == $product->getVariantId();
  133.                 });
  134.                 $cart->rate->items array_map(function ($item) use ($product) {
  135.                     $item->quantity 1;
  136.                     return $item;
  137.                 }, $cart->rate->items);
  138.                 for ($i 0$i $item['quantity']; $i++){
  139.                     $singlePallets[] =  $this->calculateSinglePalet($cart$store);
  140.                 }
  141.                 continue;
  142.             }
  143.             if (count($product->getSubProducts()) > 0) {
  144.                 foreach ($product->getSubProducts() as $subProduct) {
  145.                     $this->Packer->addItem(new TestItem(
  146.                         $product->getName() . " --> " $subProduct->getName(),
  147.                         $subProduct->getWidth() + 0,
  148.                         $subProduct->getLength() + 0,
  149.                         $subProduct->getHeight() + 0,
  150.                         $subProduct->getWeight() + 0,
  151.                         $subProduct->getKeepFlat() ?? false
  152.                     ),
  153.                         $item['quantity'] + 0
  154.                     );
  155.                 }
  156.             } else {
  157.                 $this->Packer->addItem(new TestItem(
  158.                     $product->getName() . "",
  159.                     $product->getWidth() + 0,
  160.                     $product->getLength() + 0,
  161.                     $product->getHeight() + 0,
  162.                     $product->getWeight() + 0,
  163.                     $product->getKeepFlat() ?? false
  164.                 ),
  165.                     $item['quantity'] + 0
  166.                 );
  167.             }
  168.         }
  169.         $pallets = [];
  170.         $pallets[] = $this->Packer->pack();
  171.         $totalPallets 0;
  172.         $total_shipping 0;
  173.         $palletTypes = [
  174.             'count' => [],
  175.             'name' => [],
  176.             'typecount' => [],
  177.             'items' => [],
  178.             'order' => []
  179.         ];
  180.         $boxIndex 1;
  181.         $addedBoxIndex 1;
  182.         $usedPalletes = [];
  183.         if (count($singlePallets) > 0) {
  184.             foreach ($singlePallets as $singlePallet) {
  185.                 $pallets[] = $singlePallet;
  186.             }
  187.         }
  188.         foreach ($pallets as $packedBoxes) {
  189.             foreach ($packedBoxes as $packedBox) {
  190.                 $palletType $packedBox->getBox();
  191.                 $pallet $this->palleteRepo->findOneBy(['name' => $palletType->getReference(), 'store' => $store]);
  192.                 $params['pallete'] = $pallet;
  193.                 $params['store'] = $store;
  194.                 $prices $this->FPRepo->findBy($params);
  195.                 if (!$prices || count($prices) < 1) {
  196.                     continue;
  197.                 }
  198.                 if (!isset($usedPalletes[$pallet->getName()])) {
  199.                     $usedPalletes[$pallet->getName()] = 0;
  200.                 }
  201.                 $usedPalletes[$pallet->getName()] += 1;
  202.                 if ($pallet->getId() == 20) {
  203.                     $total_shipping += $addedBoxIndex 50 100 $prices[0]->getPrice() :
  204.                         ($addedBoxIndex 60 100 $prices[0]->getPrice() :
  205.                             $prices[0]->getPrice());
  206.                     $addedBoxIndex++;
  207.                 }
  208.                 if (isset($palletTypes['typecount'][$pallet->getId()])) {
  209.                     $palletTypes['typecount'][$pallet->getId()]++;
  210.                 } else {
  211.                     $palletTypes['typecount'][$pallet->getId()] = 1;
  212.                 }
  213.                 $palletTypes['order'][$boxIndex] = [
  214.                     'pallet' => $pallet,
  215.                     'prices' => $prices
  216.                 ];
  217.                 $palletTypes['count'][$boxIndex] = $pallet->getId();
  218.                 $palletTypes['name'][$pallet->getId()] = $pallet->getName();
  219.                 $packedItems $packedBox->getItems();
  220.                 $palletTypes['items'][$boxIndex]['weight'] = 0;
  221.                 foreach ($packedItems as $packedItem) {
  222.                     $palletTypes['items'][$boxIndex]['name'][] = $packedItem->getItem()->getDescription();
  223.                     $palletTypes['items'][$boxIndex]['weight'] += $packedItem->getItem()->getWeight();
  224.                 }
  225.                 $boxIndex++;
  226.                 $totalPallets++;
  227.             }
  228.         }
  229.         if (count($palletTypes['order'])) {
  230.             foreach ($palletTypes['order'] as $boxIndex => $palletData) {
  231.                 if ($palletData['pallet']->getId() != 20) {
  232.                     $total_shipping += $addedBoxIndex 50 100 $palletData['prices'][0]->getPrice() :
  233.                         ($addedBoxIndex 60 100 $palletData['prices'][0]->getPrice() :
  234.                             $palletData['prices'][0]->getPrice());
  235.                     $addedBoxIndex++;
  236.                 }
  237.             }
  238.         }
  239.         $description " ( ";
  240.         foreach ($palletTypes['typecount'] as $key => $value) {
  241.             $description .= $value " - " $palletTypes['name'][$key];
  242.             if (end($palletTypes['typecount']) != $key) {
  243.                 $description .= ", ";
  244.             }
  245.         }
  246.         $description .= " ) ";
  247.         $totalPrice round((($total_shipping 1.1) + 25) * 100);
  248.         if ($store->getStoreId() == '55440933032') {
  249.             $totalPrice round($total_shipping 100);
  250.         }
  251.         return [
  252.             "rates" => [
  253.                 "rates" => [
  254.                     [
  255.                         'service_name' => "Calculating Shipping Rates",
  256.                         'service_code' => "PPAPP",
  257.                         'total_price' => $totalPrice,
  258.                         'description' => " Total Pallets  : $totalPallets \n $description",
  259.                         'currency' => $cart->rate->currency,
  260.                         'min_delivery_date' => date("Y-m-d H:i:s P"),
  261.                         'max_delivery_date' => date("Y-m-d H:i:s P")
  262.                     ]
  263.                 ]
  264.             ],
  265.             "content" => $palletTypes['items']
  266.         ];
  267.     }
  268.     public function calculateSinglePalet($cartStore $store)
  269.     {
  270.         $packer = new Packer();
  271.         $packer->setMaxBoxesToBalanceWeight(1);
  272.         $params = [];
  273.         $origin_country $this->countryRepo->findBy(['code' => $cart->rate->origin->country]);
  274.         $destination_country $this->countryRepo->findBy(['code' => $cart->rate->destination->country]);
  275.         $origin_province $this->provinceRepo->findBy(['code' => $cart->rate->origin->province]);
  276.         $destination_province $this->provinceRepo->findBy(['code' => $cart->rate->destination->province]);
  277.         $params['origin_country'] = $origin_country;
  278.         $params['destination_country'] = $destination_country;
  279.         if ($origin_province) {
  280.             $params['origin_province'] = $origin_province;
  281.         }
  282.         if ($destination_province) {
  283.             $isDestination $this->FPRepo->findBy(
  284.                 [
  285.                     'destination_country' => $destination_country,
  286.                     'destination_province' => $destination_province,
  287.                     'origin_country' => $origin_country
  288.                 ]
  289.             );
  290.             if ($isDestination) {
  291.                 $params['destination_province'] = $destination_province;
  292.             }
  293.         }
  294.         // $params['store'] = $store;
  295.         $prices $this->FPRepo->findBy($params);
  296.         foreach ($prices as $price) {
  297.             $pallet $price->getPallete();
  298.             $packer->addBox(new TestBox(
  299.                 $pallet->getName(),
  300.                 $pallet->getWidth() + 1,
  301.                 $pallet->getLength() + 1,
  302.                 $pallet->getHeight() + 1,
  303.                 0,
  304.                 $pallet->getWidth() + 0,
  305.                 $pallet->getLength() + 0,
  306.                 $pallet->getHeight() + 0,
  307.                 $pallet->getWeight() + 0
  308.             ));
  309.         }
  310.         // Products
  311.         $cartItems = [];
  312.         foreach ($cart->rate->items as $item) {
  313.             $product $this->productRepo->findOneBy([
  314.                 'product_id' => $item->product_id,
  315.                 'variant_id' => $item->variant_id,
  316.                 // 'store' => $store
  317.             ]);
  318.             $cartItems[] = [
  319.                 'quantity' => $item->quantity,
  320.                 'product' => $product,
  321.                 'position' => $product->getPosition() ?? 999
  322.             ];
  323.         }
  324.         usort($cartItems, function ($a$b) {
  325.             return $a['position'] <=> $b['position'];
  326.         });
  327.         foreach ($cartItems as $item) {
  328.             $product $item['product'];
  329.             if (count($product->getSubProducts()) > 0) {
  330.                 foreach ($product->getSubProducts() as $subProduct) {
  331.                     $packer->addItem(new TestItem(
  332.                         $product->getName() . " --> " $subProduct->getName(),
  333.                         $subProduct->getWidth() + 0,
  334.                         $subProduct->getLength() + 0,
  335.                         $subProduct->getHeight() + 0,
  336.                         $subProduct->getWeight() + 0,
  337.                         $subProduct->getKeepFlat() ?? false
  338.                     ),
  339.                         $item['quantity'] + 0
  340.                     );
  341.                 }
  342.             } else {
  343.                 $packer->addItem(new TestItem(
  344.                     $product->getName() . "",
  345.                     $product->getWidth() + 0,
  346.                     $product->getLength() + 0,
  347.                     $product->getHeight() + 0,
  348.                     $product->getWeight() + 0,
  349.                     $subProduct->getKeepFlat() ?? false
  350.                 ),
  351.                     $item['quantity'] + 0
  352.                 );
  353.             }
  354.         }
  355.         return $packer->pack();
  356.     }
  357. }