![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/shiplab/Helper/BoxPacker/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Helper\BoxPacker; use DVDoug\BoxPacker\InfalliblePacker; use Exception; use Extmag\Shiplab\Api\Data\DimensionBoxInterface; use Extmag\Shiplab\Helper\Manager; use Extmag\Shiplab\Model\Source\Boxes; use Extmag\Shiplab\Model\Source\CarrierCodes; class Packer { /** * @var Boxes */ protected $boxes; /** * @var Manager */ protected $manager; /** * @var CarrierCodes */ protected $carrierCodes; /** * @param Boxes $boxes * @param Manager $manager * @param CarrierCodes $carrierCodes */ public function __construct( Boxes $boxes, Manager $manager, CarrierCodes $carrierCodes ) { $this->boxes = $boxes; $this->manager = $manager; $this->carrierCodes = $carrierCodes; } /** * Get filtered Dimension Boxes * * @param string $carrier * @param array $allowedBoxes * @return array * @throws Exception */ public function getFilteredBoxes(string $carrier, array $allowedBoxes) { $boxes = []; $allBoxes = $this->boxes->toArray(true); if (!empty($allowedBoxes)) { foreach ($allBoxes as $key => $box) { if (!in_array($key, $allowedBoxes)) { unset($allBoxes[$key]); } } } if ($this->manager->getConfig($carrier . "_packaging/dimension/boxes_specific") == 'specific') { $specificBoxes = explode( ",", (string)$this->manager->getConfig($carrier . "_packaging/dimension/boxes") ); foreach ($allBoxes as $key => $box) { if (in_array($key, $specificBoxes)) { $boxes[$key] = $box; } } return $boxes; } return $allBoxes; } /** * Get Boxes * * @param InfalliblePacker $packer * @param string $carrier * @param array $data * @throws Exception */ public function getBoxes(InfalliblePacker &$packer, string $carrier, array $data) { $allowedBoxes = []; if (!empty($data['allowed_boxes'])) { $allowedBoxes = is_array($data['allowed_boxes']) ? $data['allowed_boxes'] : explode(",", $data['allowed_boxes'] ?? ''); } if ($data['route'] != 'inverse') { $countryCode = $data['shipper_country_code']; } else { $countryCode = $data['ship_to_country_code']; } $isImperialUnit = $this->isImperialUnit($countryCode); $boxes = $this->getFilteredBoxes($carrier, $allowedBoxes); if (!empty($boxes)) { /** * @var DimensionBoxInterface $box */ foreach ($boxes as $box) { $packer->addBox( new Box( $box->getId(), $box->getTitle(), (int)$this->convertByUnitDimension($box->getOuterWidth(), $isImperialUnit, $box->getUnitDimension()), (int)$this->convertByUnitDimension($box->getOuterLength(), $isImperialUnit, $box->getUnitDimension()), (int)$this->convertByUnitDimension($box->getOuterHeight(), $isImperialUnit, $box->getUnitDimension()), (int)$this->convertByUnitWeight($box->getEmptyWeight(), $isImperialUnit, $box->getUnitWeight()), (int)$this->convertByUnitDimension($box->getWidth(), $isImperialUnit, $box->getUnitDimension()), (int)$this->convertByUnitDimension($box->getLength(), $isImperialUnit, $box->getUnitDimension()), (int)$this->convertByUnitDimension($box->getHeight(), $isImperialUnit, $box->getUnitDimension()), (int)$this->convertByUnitWeight($box->getMaxWeight(), $isImperialUnit, $box->getUnitWeight()), (int)$box->getQty(), (int)$box->getIsUsedDimensions() ) ); } } else { $packer->addBox( new Box( 0, "Zero box", 0, 0, 0, 0, 0, 0, 0, (int)$this->getMaxWeightPackage($carrier, $isImperialUnit) ) ); } } /** * Get Max Weight Package * * @return float|int */ public function getMaxWeightPackage(string $carrier, bool $isImperialUnit) { $weight = 0; $carrierData = $this->carrierCodes->getCarriers($carrier); if (!empty($carrierData['max_weight']['weight']) && !empty($carrierData['max_weight']['unit'])) { $weight = $this->convertByUnitWeight( $carrierData['max_weight']['weight'], $isImperialUnit, $carrierData['max_weight']['unit'] ); } return $weight; } /** * Is Imperial Unit * * @param string $countryCode * @return bool */ public function isImperialUnit(string $countryCode) { $countriesWithImperialUnit = [ 'AG', 'AI', 'AS', 'AW', 'BB', 'BM', 'BS', 'CA', 'DM', 'DO', 'FM', 'GD', 'GU', 'GY', 'HT', 'KN', 'KY', 'LC', 'MH', 'MP', 'MS', 'PR', 'TC', 'TT', 'US', 'VC', 'VG', 'VI', 'XB', 'XC', 'XE', 'XM', 'XN', 'XY', ]; if (in_array($countryCode, $countriesWithImperialUnit)) { return true; } return false; } /** * Convert By Unit Weight * * @param $weight * @param null $isImperialUnit * @param null $from * @param bool $smallUnit * @return false|float|int|mixed */ public function convertByUnitWeight($weight, $isImperialUnit, $from = null, $smallUnit = true) { if ($weight) { if ($smallUnit === true) { $to = "G"; $weight = $weight * 100; } else { $to = "KG"; if ($isImperialUnit) { $to = "LB"; } if ($from == null) { $weight = $weight / 100; } } if ($from == null) { $from = "G"; } if ($from != $to) { if ($to == "G") { if ($from == 'KG') { $weight = ceil($weight * 1000); } elseif ($from == 'LB') { $weight = ceil($weight * 453.592); } } elseif ($to == "KG") { if ($from == 'LB') { $weight = $weight / 2.205; } elseif ($from == 'G') { $weight = $weight / 1000; } } elseif ($to == "LB") { if ($from == 'KG') { $weight = $weight * 2.205; } elseif ($from == 'G') { $weight = $weight / 453.592; } } } } return $weight; } /** * Convert By Unit Dimension * * @param $dimension * @param $isImperialUnit * @param null $from * @param bool $smallUnit * @return float|int */ public function convertByUnitDimension($dimension, $isImperialUnit, $from = null, $smallUnit = true) { if ($dimension) { if ($smallUnit === true) { $to = "MM"; $dimension = $dimension * 100; } else { $to = "CM"; if ($isImperialUnit) { $to = "IN"; } if ($from == null) { $dimension = $dimension / 100; } } if ($from == null) { $from = "MM"; } if ($from != $to) { if ($to == "MM") { if ($from == 'CM') { $dimension = ceil($dimension * 10); } elseif ($from == 'IN') { $dimension = ceil($dimension * 25.4); } } elseif ($to == "CM") { if ($from == 'MM') { $dimension = $dimension / 10; } elseif ($from == 'IN') { $dimension = $dimension * 2.54; } } elseif ($to == "IN") { if ($from == 'MM') { $dimension = $dimension / 25.4; } elseif ($from == 'CM') { $dimension = $dimension / 2.54; } } } } return $dimension; } }