Skip to content

Commit

Permalink
Feat (configuration): Remove type option for fedex configuration
Browse files Browse the repository at this point in the history
- Remove type option for fedex configuration
- Update coding style
see #9
  • Loading branch information
hunghbmGG committed Oct 9, 2020
1 parent fd35203 commit e730f5b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 55 deletions.
70 changes: 33 additions & 37 deletions src/Carriers/Fedex.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,46 +75,42 @@ public function calculate()
];
$defaultAmount = $this->getConfigData('default_rate') ?? 0;

if ($this->getConfigData('type') == 'per_unit') {
foreach ($cart->items as $item) {
if ($item->product->getTypeInstance()->isStockable()) {
$defaultAmount = $defaultAmount + ($defaultAmount * $item->quantity);
$shipping = $item->child->product->product ?? $item->product;

$shippingInfo['package'] = [
'weight' => ($shippingInfo['package']['weight'] ?? 0) + ((int) ($shipping->weight ?? 0) * $item->quantity),
'length' => ($shippingInfo['package']['length'] ?? 0) + ((int) ($shipping->depth ?? 0) * $item->quantity),
'width' => ($shippingInfo['package']['width'] ?? 0) + ((int) ($shipping->width ?? 0) * $item->quantity),
'height' => ($shippingInfo['package']['height'] ?? 0) + ((int) ($shipping->height ?? 0) * $item->quantity),
];
}
foreach ($cart->items as $item) {
if ($item->product->getTypeInstance()->isStockable()) {
$defaultAmount = $defaultAmount + ($defaultAmount * $item->quantity);
$shipping = $item->child->product->product ?? $item->product;

$shippingInfo['package'] = [
'weight' => ($shippingInfo['package']['weight'] ?? 0) + ((int) ($shipping->weight ?? 0) * $item->quantity),
'length' => ($shippingInfo['package']['length'] ?? 0) + ((int) ($shipping->depth ?? 0) * $item->quantity),
'width' => ($shippingInfo['package']['width'] ?? 0) + ((int) ($shipping->width ?? 0) * $item->quantity),
'height' => ($shippingInfo['package']['height'] ?? 0) + ((int) ($shipping->height ?? 0) * $item->quantity),
];
}
}

// Get shipping rates
$rateDetails = fedexCalculateShippingRates($shippingInfo);

if (isset($rateDetails['status']) && !$rateDetails['status'])
return $rateDetails;

$services = [];
foreach ($rateDetails as $rateDetail) {
if (! empty($rateDetail['type']) && in_array($rateDetail['type'], explode(',', $this->getConfigData('services')))) {
$object = new CartShippingRate;
$object->carrier = 'fedexrate';
$object->carrier_title = $this->getConfigData('title');
$object->method = 'fedexrate_' . ($rateDetail['type'] ?? '');
$object->method_title = $rateDetail['name'] ?? '';
$object->method_description = ($rateDetail['name'] ?? '')
. (! empty($rateDetail['astra']) ? ' - ' . $rateDetail['astra'] : '');
$object->price += $rateDetail['amount'] ?? core()->convertPrice($defaultAmount);
$object->base_price += $rateDetail['amount'] ?? $defaultAmount;
$services[] = $object;
unset($object);
}
// Get shipping rates
$rateDetails = fedexCalculateShippingRates($shippingInfo);

if (isset($rateDetails['status']) && !$rateDetails['status'])
return $rateDetails;

$services = [];

foreach ($rateDetails as $rateDetail) {
if (! empty($rateDetail['type']) && in_array($rateDetail['type'], explode(',', $this->getConfigData('services')))) {
$object = new CartShippingRate;
$object->carrier = 'fedexrate';
$object->carrier_title = $this->getConfigData('title');
$object->method = 'fedexrate_' . ($rateDetail['type'] ?? '');
$object->method_title = $rateDetail['name'] ?? '';
$object->method_description = ($rateDetail['name'] ?? '')
. (! empty($rateDetail['astra']) ? ' - ' . $rateDetail['astra'] : '');
$object->price += $rateDetail['amount'] ?? core()->convertPrice($defaultAmount);
$object->base_price += $rateDetail['amount'] ?? $defaultAmount;
$services[] = $object;
unset($object);
}
} else {
$object->price = core()->convertPrice($this->getConfigData('default_rate'));
$object->base_price = $this->getConfigData('default_rate');
}

$data['status'] = true;
Expand Down
2 changes: 2 additions & 0 deletions src/Http/helpers.php → src/Http/fedex.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ function fedExTrackById($trackingIds)

// Track shipments
$trackRequest->SelectionDetails = [];

foreach ($trackingIds as $key => $trackingId) {
$trackRequest->SelectionDetails[] = new FedEx\TrackService\ComplexType\TrackSelectionDetail();
$trackRequest->SelectionDetails[$key]->PackageIdentifier->Value = $trackingId;
Expand All @@ -133,6 +134,7 @@ function fedExTrackById($trackingIds)
$response = $request->getTrackReply($trackRequest);

$trackReplyDetails = [];

if (! empty($response) && $response->HighestSeverity == 'SUCCESS' && ! empty($response->CompletedTrackDetails)) {
foreach ($response->CompletedTrackDetails as $trackReplyDetail) {
if (! empty($notification = $trackReplyDetail->TrackDetails[0]->Notification->Severity) && $notification == 'SUCCESS') {
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/FedExServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FedExServiceProvider extends ServiceProvider {
*/
public function boot()
{
include __DIR__ . '/../Http/helpers.php';
include __DIR__ . '/../Http/fedex.php';
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Resources/views/tracking/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
{{ __('ggphp::fedex.admin.system.tracking-title') }} #{{ $trackingId }}
</h1>
</div>

<div class="page-action">
</div>
</div>

<div class="page-content">
Expand Down
14 changes: 0 additions & 14 deletions src/config/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@
'validation' => 'required',
'channel_based' => true,
'locale_based' => false,
], [
'name' => 'type',
'title' => 'admin::app.admin.system.type',
'type' => 'select',
'options' => [
[
'title' => 'Per Unit',
'value' => 'per_unit',
], [
'title' => 'Per Order',
'value' => 'per_order',
]
],
'validation' => 'required'
], [
'name' => 'active',
'title' => 'admin::app.admin.system.status',
Expand Down

0 comments on commit e730f5b

Please sign in to comment.