Skip to content

Commit

Permalink
Merge pull request #56 from hcmut-odoo/issue/add_tax_into_order
Browse files Browse the repository at this point in the history
Issue/add tax into order
  • Loading branch information
haphuongdien authored Oct 3, 2023
2 parents 1049f8a + 4a59912 commit ca07bc3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down
5 changes: 3 additions & 2 deletions app/Repositories/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 15 additions & 2 deletions app/Services/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,25 @@ public function transformOrder($orderId, $status)
{
$orderItems = $this->getOrderItems($orderId);
$totalPrice = 0;
$tax = 0.1;

foreach ($orderItems as $orderItem) {
$standard_price = $orderItem->price;
$extend_price = $orderItem->extend_price;
$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();
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit ca07bc3

Please sign in to comment.