Improvements to charging Tax

1) Surfaced more logic from from views into service.
2) If charge_tax is set in database tax is charged.
3) Made the name of the Order service OrderService so its not confused with the Order Eloquent Model.
4) Move order totalling logic in to Service and call service where necessary.
This commit is contained in:
Jeremy Quinton 2018-07-10 12:19:20 +02:00
parent b3dae02cef
commit c5676bbe45
4 changed files with 136 additions and 35 deletions

View File

@ -248,16 +248,14 @@ class EventCheckoutController extends Controller
$event = Event::findorFail($order_session['event_id']);
$order = new OrderService();
$orderCosts = $order->calculateFinalCosts($order_session['order_total'],
$order_session['total_booking_fee'],
$event);
$orderService = new OrderService($order_session['order_total'], $order_session['total_booking_fee'], $event);
$orderService->calculateFinalCosts();
$data = $order_session + [
'event' => $event,
'secondsToExpire' => $secondsToExpire,
'is_embedded' => $this->is_embedded,
'order_costs' => $orderCosts
'orderService' => $orderService
];
if ($this->is_embedded) {
@ -333,13 +331,12 @@ class EventCheckoutController extends Controller
'testMode' => config('attendize.enable_test_payments'),
]);
// Calculating grand total including tax
$grand_total = $ticket_order['order_total'] + $ticket_order['organiser_booking_fee'];
$tax_amt = ($grand_total * $event->organiser->tax_value) / 100;
$grand_total = $tax_amt + $grand_total;
$order = new OrderService($ticket_order['order_total'], $ticket_order['organiser_booking_fee'], $event);
$order->calculateFinalCosts();
$transaction_data = [
'amount' => $grand_total,
'amount' => $order->getGrandTotal(),
'currency' => $event->currency->code,
'description' => 'Order for customer: ' . $request->get('order_email'),
];
@ -541,10 +538,10 @@ class EventCheckoutController extends Controller
$order->is_payment_received = isset($request_data['pay_offline']) ? 0 : 1;
// Calculating grand total including tax
$grand_total = $ticket_order['order_total'] + $ticket_order['organiser_booking_fee'];
$tax_amt = ($grand_total * $event->organiser->tax_value) / 100;
$orderService = new OrderService($ticket_order['order_total'], $ticket_order['organiser_booking_fee'], $event);
$orderService->calculateFinalCosts();
$order->taxamt = $tax_amt;
$order->taxamt = $orderService->getTaxAmount();
$order->save();
/*
@ -719,11 +716,15 @@ class EventCheckoutController extends Controller
abort(404);
}
$orderService = new OrderService($order->amount, $order->booking_fee, $order->event);
$orderService->calculateFinalCosts();
$data = [
'order' => $order,
'event' => $order->event,
'tickets' => $order->event->tickets,
'is_embedded' => $this->is_embedded,
'order' => $order,
'orderService' => $orderService,
'event' => $order->event,
'tickets' => $order->event->tickets,
'is_embedded' => $this->is_embedded,
];
if ($this->is_embedded) {

View File

@ -2,20 +2,116 @@
namespace App\Services;
use App\Models\Event;
class Order
{
public function calculateFinalCosts($orderTotal, $totalBookingFee, $event)
{
$orderTotalWithBookingFee = $orderTotal + $totalBookingFee;
$taxAmount = ($event->organiser->charge_tax == 1) ? ($orderTotalWithBookingFee * $event->organiser->tax_value)/100
: 0;
/**
* @var float
*/
private $orderTotal;
$grandTotal = $orderTotalWithBookingFee + $taxAmount;
/**
* @var float
*/
private $totalBookingFee;
return ['orderTotalWithBookingFee' => money($orderTotalWithBookingFee, $event->currency),
'taxAmount' => money($taxAmount, $event->currency),
'grandTotal' => money($grandTotal, $event->currency )];
/**
* @var Event
*/
private $event;
/**
* @var float
*/
public $orderTotalWithBookingFee;
/**
* @var float
*/
public $taxAmount;
/**
* @var float
*/
public $grandTotal;
/**
* Order constructor.
* @param $orderTotal
* @param $totalBookingFee
* @param $event
*/
public function __construct($orderTotal, $totalBookingFee, $event) {
$this->orderTotal = $orderTotal;
$this->totalBookingFee = $totalBookingFee;
$this->event = $event;
}
/**
* Calculates the final costs for an event and sets the various totals
*/
public function calculateFinalCosts()
{
$this->orderTotalWithBookingFee = $this->orderTotal + $this->totalBookingFee;
if ($this->event->organiser->charge_tax == 1) {
$this->taxAmount = ($this->orderTotalWithBookingFee * $this->event->organiser->tax_value)/100;
} else {
$this->taxAmount = 0;
}
$this->grandTotal = $this->orderTotalWithBookingFee + $this->taxAmount;
}
/**
* @param bool $currencyFormatted
* @return float|string
*/
public function getOrderTotalWithBookingFee($currencyFormatted = false) {
if ($currencyFormatted == false ) {
return $this->orderTotalWithBookingFee;
}
return money($this->orderTotalWithBookingFee, $this->event->currency);
}
/**
* @param bool $currencyFormatted
* @return float|string
*/
public function getTaxAmount($currencyFormatted = false) {
if ($currencyFormatted == false ) {
return $this->taxAmount;
}
return money($this->taxAmount, $this->event->currency);
}
/**
* @param bool $currencyFormatted
* @return float|string
*/
public function getGrandTotal($currencyFormatted = false) {
if ($currencyFormatted == false ) {
return $this->grandTotal;
}
return money($this->grandTotal, $this->event->currency);
}
/**
* @return string
*/
public function getVatFormattedInBrackets() {
return "(+" . $this->getTaxAmount(true) . " " . $this->event->organiser->tax_name . ")";
}
}

View File

@ -36,16 +36,16 @@
@if($order_total > 0)
<div class="panel-footer">
<h5>
@lang("Public_ViewEvent.total"): <span style="float: right;"><b>{{ $order_costs['orderTotalWithBookingFee'] }}</b></span>
@lang("Public_ViewEvent.total"): <span style="float: right;"><b>{{ $orderService->getOrderTotalWithBookingFee(true) }}</b></span>
</h5>
@if($event->organiser->charge_tax)
<h5>
{{ $event->organiser->tax_name }} ({{ $event->organiser->tax_value }}%):
<span style="float: right;"><b>{{ $order_costs['taxAmount'] }}</b></span>
<span style="float: right;"><b>{{ $orderService->getTaxAmount(true) }}</b></span>
</h5>
<h5>
<strong>Grand Total:</strong>
<span style="float: right;"><b>{{ $order_costs['orderTotalWithBookingFee'] }}</b></span>
<span style="float: right;"><b>{{ $orderService->getGrandTotal(true) }}</b></span>
</h5>
@endif
</div>

View File

@ -82,8 +82,10 @@
</div>
<div class="col-sm-4 col-xs-6">
<b>@lang("Public_ViewEvent.amount")</b><br> {{$order->event->currency_symbol}}{{number_format($order->total_amount,2)}}
<small>{{ ($event->organiser->taxname && $event->organiser->taxvalue) ? '(+'.money(($order->total_amount*($event->organiser->taxvalue)/100), $event->currency).' '.$event->organiser->taxname.')' : '' }}</small>
<b>@lang("Public_ViewEvent.amount")</b><br> {{$order->event->currency_symbol}}{{number_format($order->total_amount, 2)}}
@if($event->organiser->charge_tax)
<small>{{ $orderService->getVatFormattedInBrackets() }}</small>
@endif
</div>
<div class="col-sm-4 col-xs-6">
@ -185,9 +187,10 @@
<b>@lang("Public_ViewEvent.sub_total")</b>
</td>
<td colspan="2">
{{money($order->total_amount, $order->event->currency)}}
{{ $orderService->getOrderTotalWithBookingFee(true) }}
</td>
</tr>
@if($event->organiser->charge_tax)
<tr>
<td>
</td>
@ -196,12 +199,13 @@
<td>
</td>
<td>
{{$event->organiser->taxname}}
{{$event->organiser->tax_name}}
</td>
<td colspan="2">
{{money(($order->total_amount*($event->organiser->taxvalue)/100), $event->currency)}}
{{ $orderService->getTaxAmount(true) }}
</td>
</tr>
@endif
<tr>
<td>
</td>
@ -213,7 +217,7 @@
<b>Total</b>
</td>
<td colspan="2">
{{money($order->total_amount+($order->total_amount*($event->organiser->taxvalue)/100), $event->currency)}}
{{ $orderService->getGrandTotal(true) }}
</td>
</tr>
@if($order->is_refunded || $order->is_partially_refunded)