2022-02-08 10:32:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Sarga\API\Http\Resources\Checkout;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
|
use Sarga\API\Http\Resources\Customer\AddressResource;
|
2022-02-10 09:41:13 +00:00
|
|
|
use Webkul\Marketplace\Repositories\ProductRepository;
|
2022-02-08 10:32:33 +00:00
|
|
|
use Webkul\Tax\Helpers\Tax;
|
|
|
|
|
|
|
|
|
|
class CartResource extends JsonResource
|
|
|
|
|
{
|
2022-02-10 09:41:13 +00:00
|
|
|
public function __construct($resource)
|
|
|
|
|
{
|
|
|
|
|
$this->sellerProductRepository = app(ProductRepository::class);
|
|
|
|
|
parent::__construct($resource);
|
|
|
|
|
}
|
2022-02-08 10:32:33 +00:00
|
|
|
/**
|
|
|
|
|
* Transform the resource into an array.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function toArray($request): array
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => $this->id,
|
|
|
|
|
'shipping_method' => $this->shipping_method,
|
|
|
|
|
'coupon_code' => $this->coupon_code,
|
|
|
|
|
'is_gift' => $this->is_gift,
|
2022-03-10 07:42:10 +00:00
|
|
|
'items_count' => (int) $this->items_count,
|
2022-02-28 10:00:38 +00:00
|
|
|
'items_qty' => (int) $this->items_qty,
|
|
|
|
|
'grand_total' => (double) $this->grand_total,
|
2022-03-02 13:10:22 +00:00
|
|
|
'formatted_grand_total' => core()->formatPrice($this->base_grand_total),
|
2022-02-28 10:00:38 +00:00
|
|
|
'sub_total' => (double) $this->sub_total,
|
2022-03-02 13:10:22 +00:00
|
|
|
'formatted_sub_total' => core()->formatPrice($this->base_sub_total, $this->cart_currency_code),
|
2022-02-28 10:00:38 +00:00
|
|
|
'tax_total' => (double) $this->tax_total,
|
2022-03-02 13:10:22 +00:00
|
|
|
'formatted_tax_total' => core()->formatPrice($this->base_tax_total, $this->cart_currency_code),
|
2022-02-28 10:00:38 +00:00
|
|
|
'discount' => (double) $this->discount_amount,
|
2022-03-02 13:10:22 +00:00
|
|
|
'formatted_discount' => core()->formatPrice($this->base_discount_amount, $this->cart_currency_code),
|
2022-02-08 10:32:33 +00:00
|
|
|
'checkout_method' => $this->checkout_method,
|
2022-02-10 09:41:13 +00:00
|
|
|
'vendors' => $this->groupByVendors($this->items),
|
2022-02-08 10:32:33 +00:00
|
|
|
'payment' => new CartPaymentResource($this->payment),
|
|
|
|
|
'billing_address' => new AddressResource($this->billing_address),
|
|
|
|
|
'shipping_address' => new AddressResource($this->shipping_address),
|
2022-03-02 13:10:22 +00:00
|
|
|
'formatted_discounted_sub_total' => core()->formatPrice($this->base_sub_total - $this->base_discount_amount, $this->cart_currency_code)
|
2022-02-08 10:32:33 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Format tax amounts.
|
|
|
|
|
*
|
|
|
|
|
* @param array $taxes
|
|
|
|
|
* @param bool $isBase
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function formatTaxAmounts(array $taxes, bool $isBase = false): array
|
|
|
|
|
{
|
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
|
|
foreach ($taxes as $taxRate => $taxAmount) {
|
|
|
|
|
if ($isBase === true) {
|
|
|
|
|
$result[$taxRate] = core()->formatBasePrice($taxAmount);
|
|
|
|
|
} else {
|
|
|
|
|
$result[$taxRate] = core()->formatPrice($taxAmount, $this->cart_currency_code);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2022-02-10 09:41:13 +00:00
|
|
|
|
|
|
|
|
private function groupByVendors($items){
|
|
|
|
|
$data = array();
|
|
|
|
|
foreach($items as $item){
|
|
|
|
|
$seller = $this->sellerProductRepository->getSellerByProductId($item->product_id);
|
|
|
|
|
$data[$seller->shop_title ?? 'default'][] = CartItemResource::make($item);
|
|
|
|
|
}
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
2022-02-08 10:32:33 +00:00
|
|
|
}
|