Skip to content

Commit 86f0fd6

Browse files
committed
Fix tax calculation on quotes, orders & invoices
1 parent 30f2b8d commit 86f0fd6

File tree

6 files changed

+98
-17
lines changed

6 files changed

+98
-17
lines changed

resources/views/invoice-lines/partials/fields.blade.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
</td>
5656
</tr>
5757
<tr data-number="{{ $value }}" class="item-tr">
58-
<td colspan="3" class="border-0 pt-0">
58+
<td colspan="2" class="border-0 pt-0">
5959
@if($fromOrder)
6060
@include('laravel-crm::partials.form.text',[
6161
'name' => 'invoiceLines['.$value.'][price]',
@@ -108,6 +108,19 @@
108108
])
109109
@endif
110110
</td>
111+
<td class="border-0 pt-0">
112+
@include('laravel-crm::partials.form.text',[
113+
'name' => 'invoiceLines['.$value.'][tax_amount]',
114+
'label' => $taxName . ' (' . $tax_rate[$value] . '%)',
115+
'type' => 'number',
116+
'prepend' => '<span class="fa fa-dollar" aria-hidden="true"></span>',
117+
'attributes' => [
118+
'wire:model' => 'tax_amount.'.$value,
119+
'step' => .01,
120+
'readonly' => 'readonly'
121+
]
122+
])
123+
</td>
111124
<td class="border-0 pt-0">
112125
@include('laravel-crm::partials.form.text',[
113126
'name' => 'invoiceLines['.$value.'][amount]',

resources/views/order-products/partials/fields.blade.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<tr wire:key="select2-{{ $value }}" data-number="{{ $value }}" class="item-tr">
2-
<td colspan="5" class="pt-3 bind-select2" style="position: relative;">
2+
<td colspan="6" class="pt-3 bind-select2" style="position: relative;">
33
@include('laravel-crm::partials.form.hidden',[
44
'name' => 'products['.$value.'][quote_product_id]',
55
'attributes' => [
@@ -54,7 +54,7 @@
5454
</td>
5555
</tr>
5656
<tr data-number="{{ $value }}" class="item-tr">
57-
<td colspan="3" class="border-0 pt-0">
57+
<td colspan=2" class="border-0 pt-0">
5858
@if($fromQuote)
5959
@include('laravel-crm::partials.form.text',[
6060
'name' => 'products['.$value.'][unit_price]',
@@ -107,6 +107,19 @@
107107
])
108108
@endif
109109
</td>
110+
<td class="border-0 pt-0">
111+
@include('laravel-crm::partials.form.text',[
112+
'name' => 'products['.$value.'][tax_amount]',
113+
'label' => $taxName . ' (' . $tax_rate[$value] . '%)',
114+
'type' => 'number',
115+
'prepend' => '<span class="fa fa-dollar" aria-hidden="true"></span>',
116+
'attributes' => [
117+
'wire:model' => 'tax_amount.'.$value,
118+
'step' => .01,
119+
'readonly' => 'readonly'
120+
]
121+
])
122+
</td>
110123
<td class="border-0 pt-0">
111124
@include('laravel-crm::partials.form.text',[
112125
'name' => 'products['.$value.'][amount]',
@@ -122,7 +135,7 @@
122135
</td>
123136
</tr>
124137
<tr data-number="{{ $value }}" class="item-tr">
125-
<td colspan="5" class="border-0 pt-0 pb-4">
138+
<td colspan="6" class="border-0 pt-0 pb-4">
126139
@if($fromQuote)
127140
@include('laravel-crm::partials.form.text',[
128141
'name' => 'products['.$value.'][comments]',

resources/views/quote-products/partials/fields.blade.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
</td>
4545
</tr>
4646
<tr data-number="{{ $value }}" class="item-tr">
47-
<td colspan="3" class="border-0 pt-0">
47+
<td colspan="2" class="border-0 pt-0">
4848
@include('laravel-crm::partials.form.text',[
4949
'name' => 'products['.$value.'][unit_price]',
5050
'label' => ucfirst(__('laravel-crm::lang.price')),
@@ -68,6 +68,19 @@
6868
]
6969
])
7070
</td>
71+
<td class="border-0 pt-0">
72+
@include('laravel-crm::partials.form.text',[
73+
'name' => 'products['.$value.'][tax_amount]',
74+
'label' => $taxName . ' (' . $tax_rate[$value] . '%)',
75+
'type' => 'number',
76+
'prepend' => '<span class="fa fa-dollar" aria-hidden="true"></span>',
77+
'attributes' => [
78+
'wire:model' => 'tax_amount.'.$value,
79+
'step' => .01,
80+
'readonly' => 'readonly'
81+
]
82+
])
83+
</td>
7184
<td class="border-0 pt-0">
7285
@include('laravel-crm::partials.form.text',[
7386
'name' => 'products['.$value.'][amount]',

src/Http/Livewire/LiveInvoiceLines.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class LiveInvoiceLines extends Component
3131

3232
public $quantity;
3333

34+
public $tax_amount;
35+
36+
public $tax_rate;
37+
3438
public $amount;
3539

3640
public $comments;
@@ -79,6 +83,7 @@ public function mount($invoice, $invoiceLines, $old = null, $fromOrder = false)
7983
}
8084

8185
$this->price[$this->i] = $old['price'] ?? null;
86+
$this->tax_amount[$this->i] = $old['tax_amount'] ?? null;
8287
$this->amount[$this->i] = $old['amount'] ?? null;
8388
$this->comments[$this->i] = $old['comments'] ?? null;
8489
}
@@ -104,6 +109,7 @@ public function mount($invoice, $invoiceLines, $old = null, $fromOrder = false)
104109
}
105110

106111
$this->price[$this->i] = $invoiceLine->price / 100;
112+
$this->tax_amount[$this->i] = $invoiceLine->tax_amount / 100;
107113
$this->amount[$this->i] = $invoiceLine->amount / 100;
108114
$this->comments[$this->i] = $invoiceLine->comments;
109115
}
@@ -120,6 +126,7 @@ public function add($i)
120126
$this->i = $i;
121127
$this->price[$i] = null;
122128
$this->quantity[$i] = null;
129+
$this->tax_rate[$i] = null;
123130
array_push($this->inputs, $i);
124131

125132
$this->dispatchBrowserEvent('addedItem', ['id' => $this->i]);
@@ -147,8 +154,12 @@ public function calculateAmounts()
147154

148155
for ($i = 1; $i <= $this->i; $i++) {
149156
if (isset($this->product_id[$i])) {
150-
if($product = \VentureDrake\LaravelCrm\Models\Product::find($this->product_id[$i])) {
151-
$taxRate = $product->taxRate->rate ?? $product->tax_rate ?? 0;
157+
$product = \VentureDrake\LaravelCrm\Models\Product::find($this->product_id[$i]);
158+
159+
if($product && $product->taxRate) {
160+
$taxRate = $product->taxRate->rate;
161+
} elseif($product && $product->tax_rate) {
162+
$taxRate = $product->tax_rate;
152163
} elseif($taxRate = TaxRate::where('default', 1)->first()) {
153164
$taxRate = $taxRate->rate;
154165
} elseif($taxRate = $this->settingService->get('tax_rate')) {
@@ -157,15 +168,18 @@ public function calculateAmounts()
157168
$taxRate = 0;
158169
}
159170

171+
$this->tax_rate[$i] = $taxRate;
172+
160173
if (is_numeric($this->price[$i]) && is_numeric($this->quantity[$i])) {
161174
$this->amount[$i] = $this->price[$i] * $this->quantity[$i];
162175
$this->price[$i] = $this->currencyFormat($this->price[$i]);
176+
$this->tax_amount[$i] = $this->currencyFormat($this->amount[$i] * ($taxRate / 100));
163177
} else {
164178
$this->amount[$i] = 0;
165179
}
166180

167-
$this->sub_total += $this->amount[$i];
168-
$this->tax += $this->amount[$i] * ($taxRate / 100);
181+
$this->sub_total += round($this->amount[$i], 2);
182+
$this->tax += round($this->amount[$i] * ($taxRate / 100), 2);
169183
$this->amount[$i] = $this->currencyFormat($this->amount[$i]);
170184
}
171185
}

src/Http/Livewire/LiveOrderItems.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class LiveOrderItems extends Component
3131

3232
public $quantity;
3333

34+
public $tax_amount;
35+
36+
public $tax_rate;
37+
3438
public $amount;
3539

3640
public $comments;
@@ -85,6 +89,7 @@ public function mount($order, $products, $old = null, $fromQuote = false)
8589
}
8690

8791
$this->unit_price[$this->i] = $old['unit_price'] ?? null;
92+
$this->tax_amount[$this->i] = $old['tax_amount'] ?? null;
8893
$this->amount[$this->i] = $old['amount'] ?? null;
8994
$this->comments[$this->i] = $old['comments'] ?? null;
9095
}
@@ -110,6 +115,7 @@ public function mount($order, $products, $old = null, $fromQuote = false)
110115
}
111116

112117
$this->unit_price[$this->i] = $orderProduct->price / 100;
118+
$this->tax_amount[$this->i] = $orderProduct->tax_amount / 100;
113119
$this->amount[$this->i] = $orderProduct->amount / 100;
114120
$this->comments[$this->i] = $orderProduct->comments;
115121
}
@@ -126,6 +132,7 @@ public function add($i)
126132
$this->i = $i;
127133
$this->unit_price[$i] = null;
128134
$this->quantity[$i] = null;
135+
$this->tax_rate[$i] = null;
129136
array_push($this->inputs, $i);
130137

131138
$this->dispatchBrowserEvent('addedItem', ['id' => $this->i]);
@@ -165,8 +172,12 @@ public function calculateAmounts()
165172

166173
for ($i = 1; $i <= $this->i; $i++) {
167174
if (isset($this->product_id[$i])) {
168-
if($product = \VentureDrake\LaravelCrm\Models\Product::find($this->product_id[$i])) {
169-
$taxRate = $product->taxRate->rate ?? $product->tax_rate ?? 0;
175+
$product = \VentureDrake\LaravelCrm\Models\Product::find($this->product_id[$i]);
176+
177+
if($product && $product->taxRate) {
178+
$taxRate = $product->taxRate->rate;
179+
} elseif($product && $product->tax_rate) {
180+
$taxRate = $product->tax_rate;
170181
} elseif($taxRate = TaxRate::where('default', 1)->first()) {
171182
$taxRate = $taxRate->rate;
172183
} elseif($taxRate = $this->settingService->get('tax_rate')) {
@@ -175,15 +186,18 @@ public function calculateAmounts()
175186
$taxRate = 0;
176187
}
177188

189+
$this->tax_rate[$i] = $taxRate;
190+
178191
if (is_numeric($this->unit_price[$i]) && is_numeric($this->quantity[$i])) {
179192
$this->amount[$i] = $this->unit_price[$i] * $this->quantity[$i];
180193
$this->unit_price[$i] = $this->currencyFormat($this->unit_price[$i]);
194+
$this->tax_amount[$i] = $this->currencyFormat($this->amount[$i] * ($taxRate / 100));
181195
} else {
182196
$this->amount[$i] = 0;
183197
}
184198

185-
$this->sub_total += $this->amount[$i];
186-
$this->tax += $this->amount[$i] * ($taxRate / 100);
199+
$this->sub_total += round($this->amount[$i], 2);
200+
$this->tax += round($this->amount[$i] * ($taxRate / 100), 2);
187201
$this->amount[$i] = $this->currencyFormat($this->amount[$i]);
188202
}
189203
}

src/Http/Livewire/LiveQuoteItems.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class LiveQuoteItems extends Component
2828

2929
public $quantity;
3030

31+
public $tax_amount;
32+
33+
public $tax_rate;
34+
3135
public $amount;
3236

3337
public $comments;
@@ -67,6 +71,7 @@ public function mount($quote, $products, $old = null)
6771
$this->name[$this->i] = Product::find($old['product_id'])->name ?? null;
6872
$this->quantity[$this->i] = $old['quantity'] ?? null;
6973
$this->unit_price[$this->i] = $old['unit_price'] ?? null;
74+
$this->tax_amount[$this->i] = $old['tax_amount'] ?? null;
7075
$this->amount[$this->i] = $old['amount'] ?? null;
7176
$this->comments[$this->i] = $old['comments'] ?? null;
7277
}
@@ -78,6 +83,7 @@ public function mount($quote, $products, $old = null)
7883
$this->name[$this->i] = $quoteProduct->product->name ?? null;
7984
$this->quantity[$this->i] = $quoteProduct->quantity;
8085
$this->unit_price[$this->i] = $quoteProduct->price / 100;
86+
$this->tax_amount[$this->i] = $quoteProduct->tax_amount / 100;
8187
$this->amount[$this->i] = $quoteProduct->amount / 100;
8288
$this->comments[$this->i] = $quoteProduct->comments;
8389
}
@@ -94,6 +100,7 @@ public function add($i)
94100
$this->i = $i;
95101
$this->unit_price[$i] = null;
96102
$this->quantity[$i] = null;
103+
$this->tax_rate[$i] = null;
97104
array_push($this->inputs, $i);
98105

99106
$this->dispatchBrowserEvent('addedItem', ['id' => $this->i]);
@@ -121,8 +128,12 @@ public function calculateAmounts()
121128

122129
for ($i = 1; $i <= $this->i; $i++) {
123130
if (isset($this->product_id[$i])) {
124-
if($product = \VentureDrake\LaravelCrm\Models\Product::find($this->product_id[$i])) {
125-
$taxRate = $product->taxRate->rate ?? $product->tax_rate ?? 0;
131+
$product = \VentureDrake\LaravelCrm\Models\Product::find($this->product_id[$i]);
132+
133+
if($product && $product->taxRate) {
134+
$taxRate = $product->taxRate->rate;
135+
} elseif($product && $product->tax_rate) {
136+
$taxRate = $product->tax_rate;
126137
} elseif($taxRate = TaxRate::where('default', 1)->first()) {
127138
$taxRate = $taxRate->rate;
128139
} elseif($taxRate = $this->settingService->get('tax_rate')) {
@@ -131,15 +142,18 @@ public function calculateAmounts()
131142
$taxRate = 0;
132143
}
133144

145+
$this->tax_rate[$i] = $taxRate;
146+
134147
if (is_numeric($this->unit_price[$i]) && is_numeric($this->quantity[$i])) {
135148
$this->amount[$i] = $this->unit_price[$i] * $this->quantity[$i];
136149
$this->unit_price[$i] = $this->currencyFormat($this->unit_price[$i]);
150+
$this->tax_amount[$i] = $this->currencyFormat($this->amount[$i] * ($taxRate / 100));
137151
} else {
138152
$this->amount[$i] = 0;
139153
}
140154

141-
$this->sub_total += $this->amount[$i];
142-
$this->tax += $this->amount[$i] * ($taxRate / 100);
155+
$this->sub_total += round($this->amount[$i], 2);
156+
$this->tax += round($this->amount[$i] * ($taxRate / 100), 2);
143157
$this->amount[$i] = $this->currencyFormat($this->amount[$i]);
144158
}
145159
}

0 commit comments

Comments
 (0)