|
6 | 6 | use Illuminate\Support\Composer; |
7 | 7 | use VentureDrake\LaravelCrm\Models\Delivery; |
8 | 8 | use VentureDrake\LaravelCrm\Models\Invoice; |
| 9 | +use VentureDrake\LaravelCrm\Models\InvoiceLine; |
9 | 10 | use VentureDrake\LaravelCrm\Models\Order; |
| 11 | +use VentureDrake\LaravelCrm\Models\OrderProduct; |
10 | 12 | use VentureDrake\LaravelCrm\Models\Person; |
11 | 13 | use VentureDrake\LaravelCrm\Models\Quote; |
| 14 | +use VentureDrake\LaravelCrm\Models\QuoteProduct; |
| 15 | +use VentureDrake\LaravelCrm\Models\Setting; |
12 | 16 | use VentureDrake\LaravelCrm\Services\SettingService; |
13 | 17 |
|
14 | 18 | class LaravelCrmUpdate extends Command |
@@ -187,12 +191,68 @@ public function handle() |
187 | 191 | ]); |
188 | 192 | } |
189 | 193 |
|
190 | | - $this->info('Updating Laravel CRM quote numbers complete'); |
191 | | - |
192 | 194 | $this->settingService->set('db_update_0194', 1); |
193 | 195 | $this->info('Updating Laravel CRM delivery numbers complete'); |
194 | 196 | } |
195 | 197 |
|
| 198 | + if($this->settingService->get('db_update_0199')->value == 0) { |
| 199 | + $this->info('Updating Laravel CRM tax amounts...'); |
| 200 | + |
| 201 | + foreach (QuoteProduct::whereNull('tax_amount')->get() as $quoteProduct) { |
| 202 | + $this->info('Updating Laravel CRM quote product tax #'.$quoteProduct->id); |
| 203 | + |
| 204 | + if($quoteProduct->product && $quoteProduct->product->taxRate) { |
| 205 | + $taxRate = $quoteProduct->product->taxRate->rate; |
| 206 | + } elseif($quoteProduct->product && $quoteProduct->product->tax_rate) { |
| 207 | + $taxRate = $quoteProduct->product->tax_rate; |
| 208 | + } else { |
| 209 | + $taxRate = Setting::where('name', 'tax_rate')->first()->value ?? 0; |
| 210 | + } |
| 211 | + |
| 212 | + $quoteProduct->update([ |
| 213 | + 'tax_rate' => $taxRate, |
| 214 | + 'tax_amount' => $quoteProduct->amount * ($taxRate / 100) |
| 215 | + ]); |
| 216 | + } |
| 217 | + |
| 218 | + foreach (OrderProduct::whereNull('tax_amount')->get() as $orderProduct) { |
| 219 | + $this->info('Updating Laravel CRM order product tax #'.$orderProduct->id); |
| 220 | + |
| 221 | + if($orderProduct->product && $orderProduct->product->taxRate) { |
| 222 | + $taxRate = $orderProduct->product->taxRate->rate; |
| 223 | + } elseif($orderProduct->product && $orderProduct->product->tax_rate) { |
| 224 | + $taxRate = $orderProduct->product->tax_rate; |
| 225 | + } else { |
| 226 | + $taxRate = Setting::where('name', 'tax_rate')->first()->value ?? 0; |
| 227 | + } |
| 228 | + |
| 229 | + $orderProduct->update([ |
| 230 | + 'tax_rate' => $taxRate, |
| 231 | + 'tax_amount' => $orderProduct->amount * ($taxRate / 100) |
| 232 | + ]); |
| 233 | + } |
| 234 | + |
| 235 | + foreach (InvoiceLine::whereNull('tax_amount')->get() as $invoiceLine) { |
| 236 | + $this->info('Updating Laravel CRM invoice line tax #'.$invoiceLine->id); |
| 237 | + |
| 238 | + if($invoiceLine->product && $invoiceLine->product->taxRate) { |
| 239 | + $taxRate = $invoiceLine->product->taxRate->rate; |
| 240 | + } elseif($invoiceLine->product && $invoiceLine->product->tax_rate) { |
| 241 | + $taxRate = $invoiceLine->product->tax_rate; |
| 242 | + } else { |
| 243 | + $taxRate = Setting::where('name', 'tax_rate')->first()->value ?? 0; |
| 244 | + } |
| 245 | + |
| 246 | + $invoiceLine->update([ |
| 247 | + 'tax_rate' => $taxRate, |
| 248 | + 'tax_amount' => ($invoiceLine->amount * ($taxRate / 100)) / 100 |
| 249 | + ]); |
| 250 | + } |
| 251 | + |
| 252 | + $this->settingService->set('db_update_0199', 1); |
| 253 | + $this->info('Updating Laravel CRM tax amounts complete'); |
| 254 | + } |
| 255 | + |
196 | 256 | $this->info('Laravel CRM is now updated.'); |
197 | 257 | } |
198 | 258 | } |
0 commit comments