Merge pull request #2540 from CihanSenturk/master

Create new if import has no currency code
This commit is contained in:
Cüneyt Şentürk 2022-07-19 14:11:28 +03:00 committed by GitHub
commit 67199c6d60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 56 additions and 1 deletions

View File

@ -20,6 +20,7 @@ class Transactions extends Import
$row['account_id'] = $this->getAccountId($row);
$row['category_id'] = $this->getCategoryId($row);
$row['contact_id'] = $this->getContactId($row);
$row['currency_code'] = $this->getCurrencyCode($row);
$row['document_id'] = $this->getDocumentId($row);
return $row;

View File

@ -27,6 +27,8 @@ class Transfers extends Import
$row['transferred_at'] = Date::parse($row['transferred_at'])->format('Y-m-d');
$row['from_account_id'] = $this->getFromAccountId($row);
$row['to_account_id'] = $this->getToAccountId($row);
$row['from_currency_code'] = $this->getFromCurrencyCode($row);
$row['to_currency_code'] = $this->getToCurrencyCode($row);
$row['expense_transaction_id'] = $this->getExpenseTransactionId($row);
$row['income_transaction_id'] = $this->getIncomeTransactionId($row);
@ -124,4 +126,20 @@ class Transfers extends Import
return $this->getAccountId($row);
}
private function getFromCurrencyCode($row)
{
$row['currency_code'] = $row['from_currency_code'] ?? null;
$row['currency_rate'] = $row['from_currency_rate'] ?? null;
return $this->getCurrencyCode($row);
}
private function getToCurrencyCode($row)
{
$row['currency_code'] = $row['to_currency_code'] ?? null;
$row['currency_rate'] = $row['to_currency_rate'] ?? null;
return $this->getCurrencyCode($row);
}
}

View File

@ -25,6 +25,7 @@ class BillTransactions extends Import
$row['account_id'] = $this->getAccountId($row);
$row['category_id'] = $this->getCategoryId($row, 'expense');
$row['contact_id'] = $this->getContactId($row, 'vendor');
$row['currency_code'] = $this->getCurrencyCode($row);
$row['document_id'] = $this->getDocumentId($row);
$row['number'] = $row['transaction_number'];

View File

@ -28,6 +28,7 @@ class Bills extends Import
$row['issued_at'] = $row['billed_at'];
$row['category_id'] = $this->getCategoryId($row, 'expense');
$row['contact_id'] = $this->getContactId($row, 'vendor');
$row['currency_code'] = $this->getCurrencyCode($row);
$row['type'] = Model::BILL_TYPE;
$row['contact_country'] = !empty($country) ? $country : null;

View File

@ -21,6 +21,7 @@ class Vendors extends Import
$row['type'] = 'vendor';
$row['country'] = !empty($country) ? $country : null;
$row['currency_code'] = $this->getCurrencyCode($row);
$row['user_id'] = null;
return $row;

View File

@ -21,6 +21,7 @@ class Customers extends Import
$row['type'] = 'customer';
$row['country'] = !empty($country) ? $country : null;
$row['currency_code'] = $this->getCurrencyCode($row);
$row['user_id'] = null;
return $row;

View File

@ -25,6 +25,7 @@ class InvoiceTransactions extends Import
$row['account_id'] = $this->getAccountId($row);
$row['category_id'] = $this->getCategoryId($row, 'income');
$row['contact_id'] = $this->getContactId($row, 'customer');
$row['currency_code'] = $this->getCurrencyCode($row);
$row['document_id'] = $this->getDocumentId($row);
$row['number'] = $row['transaction_number'];

View File

@ -28,6 +28,7 @@ class Invoices extends Import
$row['issued_at'] = $row['invoiced_at'];
$row['category_id'] = $this->getCategoryId($row, 'income');
$row['contact_id'] = $this->getContactId($row, 'customer');
$row['currency_code'] = $this->getCurrencyCode($row);
$row['type'] = Model::INVOICE_TYPE;
$row['contact_country'] = !empty($country) ? $country : null;

View File

@ -6,17 +6,20 @@ use App\Http\Requests\Banking\Account as AccountRequest;
use App\Http\Requests\Common\Contact as ContactRequest;
use App\Http\Requests\Common\Item as ItemRequest;
use App\Http\Requests\Setting\Category as CategoryRequest;
use App\Http\Requests\Setting\Currency as CurrencyRequest;
use App\Http\Requests\Setting\Tax as TaxRequest;
use App\Jobs\Banking\CreateAccount;
use App\Jobs\Common\CreateContact;
use App\Jobs\Common\CreateItem;
use App\Jobs\Setting\CreateCategory;
use App\Jobs\Setting\CreateCurrency;
use App\Jobs\Setting\CreateTax;
use App\Models\Banking\Account;
use App\Models\Common\Contact;
use App\Models\Common\Item;
use App\Models\Document\Document;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Models\Setting\Tax;
use App\Traits\Jobs;
use Illuminate\Support\Facades\Validator;
@ -74,6 +77,33 @@ trait Import
return is_null($id) ? $id : (int) $id;
}
public function getCurrencyCode($row)
{
$currency = Currency::where('code', $row['currency_code'])->first();
if (!empty($currency)) {
return $currency->code;
}
$data = [
'company_id' => company_id(),
'code' => $row['currency_code'],
'name' => isset($row['currency_name']) ? $row['currency_name'] : config('money.' . $row['currency_code'] . '.name'),
'rate' => isset($row['currency_rate']) ? $row['currency_rate'] : 1,
'symbol' => isset($row['currency_symbol']) ? $row['currency_symbol'] : config('money.' . $row['currency_code'] . '.symbol'),
'precision' => isset($row['currency_precision']) ? $row['currency_precision'] : config('money.' . $row['currency_code'] . '.precision'),
'decimal_mark' => isset($row['currency_decimal_mark']) ? $row['currency_decimal_mark'] : config('money.' . $row['currency_code'] . '.decimal_mark'),
'created_from' => $row['created_from'],
'created_by' => $row['created_by'],
];
Validator::validate($data, [(new CurrencyRequest)->rules()]);
$currency = $this->dispatch(new CreateCurrency($data));
return $currency->code;
}
public function getDocumentId($row)
{
$id = isset($row['document_id']) ? $row['document_id'] : null;