From d9d77da392e6993e2dc40dbbce39cb6787f57bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=B5=20Q=C3=BAy=20Long?= Date: Tue, 3 Oct 2023 22:55:34 +0700 Subject: [PATCH 1/2] Add total_tax into db --- app/Models/Order.php | 2 +- app/Repositories/OrderRepository.php | 5 +++-- .../migrations/2022_07_04_094926_create_orders_table.php | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/Models/Order.php b/app/Models/Order.php index dd61db1..7b476f5 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -13,7 +13,7 @@ class Order extends Model protected $fillable = [ 'delivery_name', 'status', 'order_transaction', 'user_id', 'delivery_phone', 'payment_mode_id', 'delivery_address', - 'delivery_note', 'total' + 'delivery_note', 'total', 'total_tax' ]; public function __construct(array $attributes = []) diff --git a/app/Repositories/OrderRepository.php b/app/Repositories/OrderRepository.php index b552b4c..2c3ad33 100644 --- a/app/Repositories/OrderRepository.php +++ b/app/Repositories/OrderRepository.php @@ -30,13 +30,14 @@ public function create($userId, $paymentModeId, $deliveryName, $deliveryPhone, $ 'delivery_phone' => $deliveryPhone, 'delivery_address' => $deliveryAddress, 'delivery_note' => $deliveryNote, - 'total' => 0 + 'total' => 0, + 'total_tax' => 0, ]); } public function update($data) { - $fields = ['status', 'total']; + $fields = ['status', 'total', 'total_tax']; $updateData = []; foreach ($fields as $field) { diff --git a/database/migrations/2022_07_04_094926_create_orders_table.php b/database/migrations/2022_07_04_094926_create_orders_table.php index 9ba1af0..6e625f3 100644 --- a/database/migrations/2022_07_04_094926_create_orders_table.php +++ b/database/migrations/2022_07_04_094926_create_orders_table.php @@ -24,6 +24,7 @@ public function up() $table->string('delivery_address'); $table->string('delivery_name'); $table->decimal('total'); + $table->decimal('total_tax'); $table->softDeletes(); $table->timestamps(); From 4a59912a78316d5eebf10317904f69ab66706325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=B5=20Q=C3=BAy=20Long?= Date: Tue, 3 Oct 2023 22:55:45 +0700 Subject: [PATCH 2/2] Apply tax for order rows --- app/Services/OrderService.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index c3db28f..31c711e 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -189,6 +189,7 @@ public function transformOrder($orderId, $status) { $orderItems = $this->getOrderItems($orderId); $totalPrice = 0; + $tax = 0.1; foreach ($orderItems as $orderItem) { $standard_price = $orderItem->price; @@ -196,13 +197,17 @@ public function transformOrder($orderId, $status) $totalPrice = $totalPrice + ($standard_price + $extend_price)*$orderItem->quantity; } + $totalPriceTaxInclude = $totalPrice * (1 + $tax); + $totalTax = $totalPrice * $tax; + try { DB::beginTransaction(); $transformOrder = $this->orderRepository->update([ 'id' => $orderId, - 'total' => $totalPrice, - 'status' => $status + 'total' => $totalPriceTaxInclude, + 'status' => $status, + 'total_tax' => $totalTax ]); DB::commit(); @@ -282,6 +287,14 @@ public function getOrderDetail($orderId) $payment['id'] = $orderRow->payment_mode_id; $payment['name'] = $orderRow->payment_mode; + // Caculate tax + $tax = 0.1; + $itemPrice = $orderRow->price + $orderRow->extend_price; + $taxAmount = $itemPrice * $tax; + + $product['item_price'] = $itemPrice; + $product['tax_amount'] = $taxAmount; + // Collect delivery data $delivery['delivery_note'] = $orderRow->delivery_note; $delivery['delivery_phone'] = $orderRow->delivery_phone;