Skip to content

Commit

Permalink
optimized the code of ups shipping
Browse files Browse the repository at this point in the history
  • Loading branch information
antuchaudharyqlo246 committed Apr 16, 2024
1 parent 940103a commit f173202
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 128 deletions.
111 changes: 39 additions & 72 deletions packages/Webkul/UpsShipping/src/Carriers/Ups.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@

class Ups extends AbstractShipping
{
/**
* The shipping method helper instance.
*
* @var \Webkul\UpsShipping\Helpers\ShippingMethodHelper $shippingMethodHelper
* @return void
*/
public function __construct(
protected ShippingMethodHelper $shippingMethodHelper,
)
{
}

/**
* Payment method code
*
Expand All @@ -39,80 +27,59 @@ public function calculate()
return false;
}

$shippingMethods = $rates = [];
$shippingMethods = [];

$cart = Cart::getCart();

$address = $cart->shipping_address;

$data = $this->shippingMethodHelper->getAllCartProducts($address);

$marketplaceShipping = session()->get('marketplace_shipping_rates');

$data = app('Webkul\UpsShipping\Helpers\ShippingMethodHelper')->getAllCartProducts($address);

This comment has been minimized.

Copy link
@VikassWebkul214254

VikassWebkul214254 Apr 17, 2024

import class

This comment has been minimized.

Copy link
@VikassWebkul214254

VikassWebkul214254 Apr 17, 2024

variable name is incorrect


if (! $this->isAvailable())
if (! $data) {
return false;
}

if (isset ($data) && $data == true) {
foreach ($data as $key => $fedexServices) {
$rate = $totalShippingCost = 0;

$upsMethod = $methodCode = $key;

foreach ($fedexServices as $methods => $upsRate) {
$rate += $upsRate['rate'] * $upsRate['itemQuantity'];
$sellerId = $upsRate['marketplace_seller_id'];

$itemShippingCost = $upsRate['rate'] * $upsRate['itemQuantity'];

$rates[$key][$sellerId] = [
'amount' => core()->convertPrice($itemShippingCost),
'base_amount' => $itemShippingCost
];

if (isset ($rates[$key][$sellerId])) {
$rates[$key][$sellerId] = [
'amount' => core()->convertPrice($rates[$key][$sellerId]['amount'] + $itemShippingCost),
'base_amount' => $rates[$key][$sellerId]['base_amount'] + $itemShippingCost
];
}

$totalShippingCost += $itemShippingCost;
}

$object = new CartShippingRate;

$object->carrier = 'mpups';

$object->carrier_title = $this->getConfigData('title');

$object->method = 'mpups_' . '' . $methodCode;

$object->method_title = $this->getConfigData('title');

$object->method_description = $upsMethod;

$object->price = core()->convertPrice($totalShippingCost);
foreach ($data as $key => $fedexServices) {
$totalShippingCost = 0;

$rates = [];

foreach ($fedexServices as $methods => $upsRate) {
$itemShippingCost = $upsRate['rate'] * $upsRate['itemQuantity'];
$sellerId = $upsRate['marketplace_seller_id'];

$rates[$sellerId] = [
'amount' => core()->convertPrice($itemShippingCost),
'base_amount' => $itemShippingCost,
];

$totalShippingCost += $itemShippingCost;
}

$object = new CartShippingRate;

$object->base_price = $totalShippingCost;
$object->carrier = 'mpups';

$marketplaceShippingRates = session()->get('marketplace_shipping_rates');
$object->carrier_title = $this->getConfigData('title');

if (! is_array($marketplaceShipping)) {
$marketplaceShippingRates['mpupsshipping'] = ['mpupsshipping' => $rates];
session()->put('marketplace_shipping_rates', $marketplaceShippingRates);
$object->method = 'mpups_' . $key;

} else {
$marketplaceFedexShipping = ['mpupshipping' => $rates];
}
$object->method_title = $this->getConfigData('title');

array_push($shippingMethods, $object);
}
$object->method_description = $key;

$object->price = core()->convertPrice($totalShippingCost);

if (isset ($marketplaceFedexShipping)) {
session()->put('marketplace_shipping_rates.mpupshipping', $marketplaceFedexShipping);
}
$object->base_price = $totalShippingCost;

return $shippingMethods;
$shippingMethods[] = $object;
// Store rates in session
$marketplaceShippingRates = session()->get('marketplace_shipping_rates', []);

$marketplaceShippingRates['mpupsshipping'][$key] = $rates;
session()->put('marketplace_shipping_rates', $marketplaceShippingRates);
}

return $shippingMethods;
}
}
2 changes: 1 addition & 1 deletion packages/Webkul/UpsShipping/src/Config/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
], [
'name' => 'weight_unit',
'title' => 'ups::app.admin.system.weight-unit',
'type' => 'multiselect',
'type' => 'select',
'validation' => 'required',
'channel_based' => false,
'locale_based' => true,
Expand Down
90 changes: 43 additions & 47 deletions packages/Webkul/UpsShipping/src/Helpers/ShippingMethodHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,28 @@ public function getAllCartProducts($address)
*/
protected function _createSoapClient($address)
{
$cart = Cart::getCart();
if (! core()->getConfigData('sales.carriers.ups.ups_active')) {
return false;
}

$cart = Cart::getCart();

$defaultChannel = core()->getCurrentChannelCode();

$adminData = $this->channel->findByField('code', $defaultChannel)->first();

$adminName = $adminData->name;

$adminCompany = $adminData->hostname;

if (! core()->getConfigData('sales.carriers.ups.ups_active')) {
return false;
} else {
$status = true;
}

if ($status) {
$sellerAdminData = $this->upsRepository->getSellerAdminData($cart->items()->get(), 'ups_postcode');
}

$sellerAdminData = $this->upsRepository->getSellerAdminData($cart->items()->get(), 'ups_postcode');

$sellerAdminServices = $allServices = [];

foreach ($sellerAdminData as $cartProduct) {
$sellerAdminServices[0] = explode(",", core()->getConfigData('sales.carriers.ups.services'));
}

foreach ($sellerAdminData as $cartProduct) {
$weight = $this->getWeight($cartProduct->weight);

Expand Down Expand Up @@ -186,11 +183,11 @@ protected function _createSoapClient($address)
$sellerId = $cartProduct->marketplace_seller_id;
}

if ( $response
if ($response
&& isset($upsServices->Response->ResponseStatusDescription)
&& $upsServices->Response->ResponseStatusDescription == 'Success'
) {
if ( isset($upsServices->RatedShipment)) {
if (isset($upsServices->RatedShipment)) {
foreach ($upsServices->RatedShipment as $services) {
$serviceCode = $services->Service->Code;
$matchResult = $this->upsRepository->validateAllowedMethods($serviceCode, $sellerAdminServices);
Expand All @@ -204,7 +201,7 @@ protected function _createSoapClient($address)
'weight' => $services->BillingWeight->Weight,
'weightUnit' => $services->BillingWeight->UnitOfMeasurement->Code,
'marketplace_seller_id' => $sellerId,
'itemQuantity' => $cartProduct->quantity
'itemQuantity' => $cartProduct->quantity,
];
}
}
Expand All @@ -219,7 +216,6 @@ protected function _createSoapClient($address)
return false;
//to check the error generated in the method;
}

} catch (\Exception $e) {
return false;
}
Expand All @@ -232,7 +228,6 @@ protected function _createSoapClient($address)
}
}


/**
* Map service code
*
Expand All @@ -241,24 +236,24 @@ protected function _createSoapClient($address)
protected function getServiceName($serviceCode)
{
$mapServices = [
'01' => 'Next Day Air',
'02' => '2nd Day Air',
'03' => 'Ups Ground',
'07' => 'Ups Worldwide Express',
'08' => 'Ups Worldwide Expedited',
'11' => 'Standard',
'12' => '3 Day Select',
'13' => 'Next Day Air Saver',
'14' => 'Next Day Air Early A.M.',
'54' => 'Ups Worldwide Express Plus',
'59' => '2nd Day Air A.M.',
'65' => 'UPS World Wide Saver',
'82' => 'Today Standard',
'83' => 'Today Dedicated Courier',
'84' => 'Today Intercity',
'85' => 'Today Express',
'86' => 'Today Express Saver',
'03' => 'Ups Ground',
'01' => 'Next Day Air',
'02' => '2nd Day Air',
'03' => 'Ups Ground',
'07' => 'Ups Worldwide Express',
'08' => 'Ups Worldwide Expedited',
'11' => 'Standard',
'12' => '3 Day Select',
'13' => 'Next Day Air Saver',
'14' => 'Next Day Air Early A.M.',
'54' => 'Ups Worldwide Express Plus',
'59' => '2nd Day Air A.M.',
'65' => 'UPS World Wide Saver',
'82' => 'Today Standard',
'83' => 'Today Dedicated Courier',
'84' => 'Today Intercity',
'85' => 'Today Express',
'86' => 'Today Express Saver',
'03' => 'Ups Ground',
];

foreach ($mapServices as $key => $service) {
Expand Down Expand Up @@ -289,7 +284,6 @@ public function getWeight($weight)
} else {
//kgs to lbs
$convertedWeight = $weight/0.45359237;

}
} else {
$convertedWeight = $weight/0.45359237;
Expand All @@ -305,20 +299,22 @@ public function getWeight($weight)
**/
public function getErrorLog($errors)
{
$exception = [];

foreach ($errors->Response->Error as $errorLog){
$exception[] = $errorLog->ErrorDescription;
}

$status = $errors->Response->ResponseStatusDescription;

if (gettype($errors->Response->Error) !== 'array') {
$status = $errors->Response->Error->ErrorSeverity;

if (is_array($errors->Response->Error)) {
foreach ($errors->Response->Error as $errorLog) {
$exception[] = $errorLog->ErrorDescription;
}
} else {
$exception[] = $errors->Response->Error->ErrorDescription;
}

$logs = ['status' => $status, 'description' => $exception];
$status = is_array($errors->Response->Error) ? $errors->Response->ResponseStatusDescription : $errors->Response->Error->ErrorSeverity;

$logs = [
'status' => $status,
'description' => $exception,
];

$shippingLog = new Logger('shipping');
$shippingLog->pushHandler(new StreamHandler(storage_path('logs/ups.log')), Logger::INFO);
Expand Down
15 changes: 7 additions & 8 deletions packages/Webkul/UpsShipping/src/Repositories/UpsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function validateAllowedMethods($service, $allowedServices)
return true;
}
}

return false;
}

Expand Down Expand Up @@ -66,25 +67,23 @@ public function getAllowedMethods($allServices)
*/
public function getCommonMethods($methods)
{
$avilableServicesArray = [];
$availableServicesArray = [];

foreach ($methods as $fedexMethods) {
foreach ($fedexMethods as $key => $fedexMethod) {
$avilableServicesArray[] = $key;
foreach (array_keys($fedexMethods) as $serviceType) {
$availableServicesArray[] = $serviceType;
}
}

$countMethods = count($methods);
$countServices = array_count_values($avilableServicesArray);
$countServices = array_count_values($availableServicesArray);
$finalServices = [];

foreach ($countServices as $serviceType => $servicesCount) {
if ($servicesCount == $countMethods) {
foreach ($methods as $fedexMethods) {
foreach ($fedexMethods as $type => $fedexMethod) {
if ($serviceType == $type) {
$finalServices[$serviceType][] = $fedexMethod;
}
if (isset($fedexMethods[$serviceType])) {
$finalServices[$serviceType][] = $fedexMethods[$serviceType];
}
}
}
Expand Down

0 comments on commit f173202

Please sign in to comment.