Fix tax per item issue & check currency key

This commit is contained in:
harshjagad20
2022-03-04 12:08:03 +05:30
parent 83a7c97e9e
commit fadef0ea07
3 changed files with 49 additions and 5 deletions

View File

@ -23,11 +23,13 @@ class UpdateCompanySettingsController extends Controller
$companyCurrency = CompanySetting::getSetting('currency', $request->header('company')); $companyCurrency = CompanySetting::getSetting('currency', $request->header('company'));
$data = $request->settings; $data = $request->settings;
if ($companyCurrency !== $data['currency'] && $company->hasTransactions()) { if (array_key_exists('currency', $data)) {
return response()->json([ if ($companyCurrency !== $data['currency'] && $company->hasTransactions()) {
'success' => false, return response()->json([
'message' => 'Cannot update company currency after transactions are created.' 'success' => false,
]); 'message' => 'Cannot update company currency after transactions are created.'
]);
}
} }
CompanySetting::setSettings($data, $request->header('company')); CompanySetting::setSettings($data, $request->header('company'));

View File

@ -498,6 +498,10 @@ class Invoice extends Model implements HasMedia
if (array_key_exists('taxes', $invoiceItem) && $invoiceItem['taxes']) { if (array_key_exists('taxes', $invoiceItem) && $invoiceItem['taxes']) {
foreach ($invoiceItem['taxes'] as $tax) { foreach ($invoiceItem['taxes'] as $tax) {
$tax['company_id'] = $invoice->company_id; $tax['company_id'] = $invoice->company_id;
$tax['exchnage_rate'] = $invoice->exchange_rate;
$tax['base_amount'] = $tax['amount'] * $exchange_rate;
$tax['currency_id'] = $invoice->currency_id;
if (gettype($tax['amount']) !== "NULL") { if (gettype($tax['amount']) !== "NULL") {
if (array_key_exists('recurring_invoice_id', $invoiceItem)) { if (array_key_exists('recurring_invoice_id', $invoiceItem)) {
unset($invoiceItem['recurring_invoice_id']); unset($invoiceItem['recurring_invoice_id']);

View File

@ -0,0 +1,38 @@
<?php
use Crater\Models\InvoiceItem;
use Crater\Models\Tax;
use Illuminate\Database\Migrations\Migration;
class CalculateBaseValuesForInvoiceItems extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$taxes = Tax::whereRelation('invoiceItem', 'base_amount', null)->get();
if ($taxes) {
$taxes->map(function ($tax) {
$invoiceItem = InvoiceItem::find($tax->invoice_item_id);
$exchange_rate = $invoiceItem->exchange_rate;
$tax->exchange_rate = $exchange_rate;
$tax->base_amount = $tax->amount * $exchange_rate;
$tax->save();
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}