From fa049bfa7e68c01b6f05954f61f9c2260f51c11d Mon Sep 17 00:00:00 2001 From: Jack Goh Date: Thu, 26 Jul 2018 15:15:18 +0800 Subject: [PATCH] fixed incorrect calculation when storing booking --- app/Http/Controllers/BookingController.php | 34 +++++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/BookingController.php b/app/Http/Controllers/BookingController.php index 298367be..5c5fc19d 100644 --- a/app/Http/Controllers/BookingController.php +++ b/app/Http/Controllers/BookingController.php @@ -249,7 +249,9 @@ class BookingController extends Controller $term = $request->input('term'); $amount = $request->input("amount"); // in RMB $rates = Rate::orderby('updated_at','desc')->first(); - $creditLimit = SettingCredit::orderby('updated_at','desc')->first(); + $creditLimit = SettingCredit::orderby('updated_at','desc')->first(); + $taxrate = SettingTaxRate::latest()->first()->tax_rate; + $billingChargeRate = SettingBillingCharge::latest()->first()->billing_charge_rate; if (!$creditLimit){ return response()->json("Credit limit not set, please contact admin", 405); @@ -291,15 +293,31 @@ class BookingController extends Controller } // Service charge (RMB) : if amount higher than 10,000, no service charge. if amount lower than 10,000, 20.00 service charge - if ($amount >= 10000 && ($term == 'x2_cash' || $term == 'x2_cheque' || $term == 'x2_ba')) { + if ($amount >= 10000 && ($term == 'x2_cash' || $term == 'x2_cheque' || $term == 'x2_ba')) { $svcharge = 0; } else { $svcharge = 20.00; } - - $bia = round(round($amount + $svcharge / $rate,2) - + round(round($amount + $svcharge / $rate,2) * $taxrate,2) - + round(($amount + $svcharge) * $billingChargeRate, 2) , 2); + + $subtotal = round(($amount + $svcharge) / $rate,2); + + // TODO : refactor + if ($taxrate !=0){ + $taxAmount = round($subtotal * $taxrate - $subtotal,2); + } + else{ + $taxAmount = 0; + } + + if ($billingChargeRate !=0){ + $billingChargeAmount = round(($amount + $svcharge) * $billingChargeRate - ($amount + $svcharge), 2); + } + else{ + $billingChargeAmount = 0; + } + + $billingChargeAmount = round((($amount / $rate) * $billingChargeRate) - ($amount / $rate), 2); + $bankin_amount = round($subtotal + $taxAmount + $billingChargeAmount , 2); $booking = new Booking(); $booking->account_name = $request->input('account_name'); @@ -310,9 +328,9 @@ class BookingController extends Controller $booking->term = $term; $booking->rate_id = $rates->id; $booking->rate = $rate; - $booking->rmb_book_amount = $request->input('amount'); + $booking->rmb_book_amount = $request->input('amount'); // BIG TODO : amount change variable name to rmb_book_amount $booking->status = 2; - $booking->bia = $bia; + $booking->bia = $bankin_amount; // BID TODO : change bia viariable name // $booking->user_id = $user->id; $booking->user()->associate($user);