add convertUnit
This commit is contained in:
@@ -139,7 +139,7 @@ class Usps_Api_Bridge extends Module
|
|||||||
return $helper->generateForm([$fields_form]);
|
return $helper->generateForm([$fields_form]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* THE CORE BRIDGE LOGIC
|
* THE CORE BRIDGE LOGIC
|
||||||
*/
|
*/
|
||||||
public function calculateRate($params, $shipping_cost, $products, $originalModule)
|
public function calculateRate($params, $shipping_cost, $products, $originalModule)
|
||||||
@@ -153,15 +153,12 @@ class Usps_Api_Bridge extends Module
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. Identify which Service (Method) PrestaShop is asking for
|
// 2. Identify which Service (Method) PrestaShop is asking for
|
||||||
// The old module links PS Carrier ID -> Internal Method Code (e.g. "USA_1")
|
|
||||||
$carrierId = $params->id_carrier;
|
$carrierId = $params->id_carrier;
|
||||||
|
|
||||||
// We query the OLD module's table directly to find the code for this carrier
|
|
||||||
$sql = 'SELECT code FROM `' . _DB_PREFIX_ . 'uspsl_method` WHERE id_carrier = ' . (int)$carrierId;
|
$sql = 'SELECT code FROM `' . _DB_PREFIX_ . 'uspsl_method` WHERE id_carrier = ' . (int)$carrierId;
|
||||||
$methodCode = Db::getInstance()->getValue($sql);
|
$methodCode = Db::getInstance()->getValue($sql);
|
||||||
|
|
||||||
if (!$methodCode) {
|
if (!$methodCode) {
|
||||||
// This carrier isn't a USPS carrier controlled by the module
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,8 +169,7 @@ class Usps_Api_Bridge extends Module
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Pack the Products (Using Old Module's Logic)
|
// 4. Pack the Products
|
||||||
// This calculates how many boxes and their dimensions/weights
|
|
||||||
$packedBoxes = $originalModule->getHelper()->getCarrierHelper()->packProducts($products, $params->id);
|
$packedBoxes = $originalModule->getHelper()->getCarrierHelper()->packProducts($products, $params->id);
|
||||||
|
|
||||||
if (empty($packedBoxes)) {
|
if (empty($packedBoxes)) {
|
||||||
@@ -198,23 +194,17 @@ class Usps_Api_Bridge extends Module
|
|||||||
// 7. Loop through every box and get a rate
|
// 7. Loop through every box and get a rate
|
||||||
foreach ($packedBoxes as $packedBox) {
|
foreach ($packedBoxes as $packedBox) {
|
||||||
|
|
||||||
// Convert Weight: Old module uses grams internally usually, spec needs Pounds/Ounces
|
// BoxPacker uses Grams and Millimeters by default
|
||||||
// We assume packedBox->getWeight() is in grams based on typical PS behavior
|
// USPS requires Pounds and Inches
|
||||||
// The old module has a UnitConverter class we can borrow
|
|
||||||
$weightInLbs = $originalModule->getHelper()->getUnitConverter()->convertUnitFromTo(
|
|
||||||
$packedBox->getWeight(),
|
|
||||||
'g',
|
|
||||||
'lbs',
|
|
||||||
3
|
|
||||||
);
|
|
||||||
|
|
||||||
// Get Dimensions (in Inches)
|
// Weight: Grams -> Pounds
|
||||||
$box = $packedBox->getBox(); // This returns the Box object
|
$weightInLbs = $this->convertUnit($packedBox->getWeight(), 'g', 'lbs', 3);
|
||||||
// The Box object from BoxPacker library stores dims in mm usually,
|
|
||||||
// we need to convert to Inches.
|
// Dimensions: Millimeters -> Inches
|
||||||
$length = $originalModule->getHelper()->getUnitConverter()->convertUnitFromTo($box->getOuterLength(), 'mm', 'in', 2);
|
$box = $packedBox->getBox();
|
||||||
$width = $originalModule->getHelper()->getUnitConverter()->convertUnitFromTo($box->getOuterWidth(), 'mm', 'in', 2);
|
$length = $this->convertUnit($box->getOuterLength(), 'mm', 'in', 2);
|
||||||
$height = $originalModule->getHelper()->getUnitConverter()->convertUnitFromTo($box->getOuterDepth(), 'mm', 'in', 2);
|
$width = $this->convertUnit($box->getOuterWidth(), 'mm', 'in', 2);
|
||||||
|
$height = $this->convertUnit($box->getOuterDepth(), 'mm', 'in', 2);
|
||||||
|
|
||||||
// Build Payload
|
// Build Payload
|
||||||
$payload = [
|
$payload = [
|
||||||
@@ -224,30 +214,26 @@ class Usps_Api_Bridge extends Module
|
|||||||
'width' => $width,
|
'width' => $width,
|
||||||
'height' => $height,
|
'height' => $height,
|
||||||
'mailClass' => $newApiClass,
|
'mailClass' => $newApiClass,
|
||||||
'priceType' => 'COMMERCIAL', // Defaulting to Commercial
|
'priceType' => 'COMMERCIAL',
|
||||||
'mailingDate' => date('Y-m-d', strtotime('+1 day')), // Future date is safer
|
'mailingDate' => date('Y-m-d', strtotime('+1 day')),
|
||||||
'processingCategory' => 'MACHINABLE', // Defaulting to simple logic
|
'processingCategory' => 'MACHINABLE',
|
||||||
'rateIndicator' => 'SP' // Single Piece (Standard)
|
'rateIndicator' => 'SP'
|
||||||
];
|
];
|
||||||
|
|
||||||
// -- HANDLE FLAT RATES --
|
// -- HANDLE FLAT RATES --
|
||||||
// If the old box name contains "Flat Rate", we must map it to correct rateIndicator
|
$rateIndicator = $this->mapBoxToRateIndicator($box->getReference());
|
||||||
// (e.g. "USPS Medium Flat Rate Box" -> "FB")
|
|
||||||
$rateIndicator = $this->mapBoxToRateIndicator($box->getReference()); // Assuming reference holds name
|
|
||||||
if ($rateIndicator) {
|
if ($rateIndicator) {
|
||||||
$payload['rateIndicator'] = $rateIndicator;
|
$payload['rateIndicator'] = $rateIndicator;
|
||||||
// Dimensions technically don't matter for Flat Rate, but API might require them anyway
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($isInternational) {
|
if ($isInternational) {
|
||||||
$payload['destinationCountryCode'] = Country::getIsoById($destAddress->id_country);
|
$payload['destinationCountryCode'] = Country::getIsoById($destAddress->id_country);
|
||||||
$payload['originZIPCode'] = $originZip;
|
$payload['originZIPCode'] = $originZip;
|
||||||
// International API needs extra fields? Spec says originZIPCode is required.
|
|
||||||
|
|
||||||
$response = $client->getInternationalRate($payload);
|
$response = $client->getInternationalRate($payload);
|
||||||
} else {
|
} else {
|
||||||
$payload['destinationZIPCode'] = $destZip;
|
$payload['destinationZIPCode'] = $destZip;
|
||||||
$payload['destinationEntryFacilityType'] = 'NONE'; // Required by V3
|
$payload['destinationEntryFacilityType'] = 'NONE';
|
||||||
|
|
||||||
$response = $client->getDomesticRate($payload);
|
$response = $client->getDomesticRate($payload);
|
||||||
}
|
}
|
||||||
@@ -261,19 +247,41 @@ class Usps_Api_Bridge extends Module
|
|||||||
if (isset($response['totalBasePrice'])) {
|
if (isset($response['totalBasePrice'])) {
|
||||||
$totalPrice += (float)$response['totalBasePrice'];
|
$totalPrice += (float)$response['totalBasePrice'];
|
||||||
} elseif (isset($response['rateOptions'][0]['totalBasePrice'])) {
|
} elseif (isset($response['rateOptions'][0]['totalBasePrice'])) {
|
||||||
// Sometimes it returns an array of options
|
|
||||||
$totalPrice += (float)$response['rateOptions'][0]['totalBasePrice'];
|
$totalPrice += (float)$response['rateOptions'][0]['totalBasePrice'];
|
||||||
} else {
|
} else {
|
||||||
$this->log("API Response missing price: " . print_r($response, true));
|
$this->log("API Response missing price: " . print_r($response, true));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->log("API Response price: " . $response['totalBasePrice']);
|
|
||||||
|
|
||||||
// Add handling fees if any from original module logic
|
|
||||||
return $totalPrice + $shipping_cost;
|
return $totalPrice + $shipping_cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple Unit Converter to replace the dependency on the old module's class
|
||||||
|
*/
|
||||||
|
private function convertUnit($value, $from, $to, $precision = 2)
|
||||||
|
{
|
||||||
|
$units = [
|
||||||
|
'lb' => 453.59237,
|
||||||
|
'lbs' => 453.59237,
|
||||||
|
'oz' => 28.3495231,
|
||||||
|
'kg' => 1000,
|
||||||
|
'g' => 1,
|
||||||
|
'in' => 25.4,
|
||||||
|
'cm' => 10,
|
||||||
|
'mm' => 1
|
||||||
|
];
|
||||||
|
|
||||||
|
// Normalize to base unit (grams or mm)
|
||||||
|
$baseValue = $value * (isset($units[$from]) ? $units[$from] : 1);
|
||||||
|
|
||||||
|
// Convert to target unit
|
||||||
|
$converted = $baseValue / (isset($units[$to]) ? $units[$to] : 1);
|
||||||
|
|
||||||
|
return round($converted, $precision);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper: Get Origin Zip from Old Module DB
|
* Helper: Get Origin Zip from Old Module DB
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user