From 494c2ac1d5aba61cf6ba6f722ef4e102a13fae96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihan=20=C5=9Eent=C3=BCrk?= Date: Tue, 19 Jul 2022 12:09:04 +0300 Subject: [PATCH] create new if import has no currency code --- app/Imports/Banking/Transactions.php | 1 + app/Imports/Banking/Transfers.php | 20 ++++++++++++- .../Purchases/Sheets/BillTransactions.php | 1 + app/Imports/Purchases/Sheets/Bills.php | 1 + app/Imports/Purchases/Vendors.php | 1 + app/Imports/Sales/Customers.php | 1 + .../Sales/Sheets/InvoiceTransactions.php | 1 + app/Imports/Sales/Sheets/Invoices.php | 1 + app/Traits/Import.php | 30 +++++++++++++++++++ 9 files changed, 56 insertions(+), 1 deletion(-) diff --git a/app/Imports/Banking/Transactions.php b/app/Imports/Banking/Transactions.php index 03d86d35a..5bb9deee9 100644 --- a/app/Imports/Banking/Transactions.php +++ b/app/Imports/Banking/Transactions.php @@ -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; diff --git a/app/Imports/Banking/Transfers.php b/app/Imports/Banking/Transfers.php index 8b267b5d0..a64747696 100644 --- a/app/Imports/Banking/Transfers.php +++ b/app/Imports/Banking/Transfers.php @@ -27,9 +27,11 @@ 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); - + return $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); + } } diff --git a/app/Imports/Purchases/Sheets/BillTransactions.php b/app/Imports/Purchases/Sheets/BillTransactions.php index 532a85ebe..528d5bb16 100644 --- a/app/Imports/Purchases/Sheets/BillTransactions.php +++ b/app/Imports/Purchases/Sheets/BillTransactions.php @@ -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']; diff --git a/app/Imports/Purchases/Sheets/Bills.php b/app/Imports/Purchases/Sheets/Bills.php index e66df3aca..f3ea5a549 100644 --- a/app/Imports/Purchases/Sheets/Bills.php +++ b/app/Imports/Purchases/Sheets/Bills.php @@ -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; diff --git a/app/Imports/Purchases/Vendors.php b/app/Imports/Purchases/Vendors.php index d70fa9d0c..2e4c6c5c1 100644 --- a/app/Imports/Purchases/Vendors.php +++ b/app/Imports/Purchases/Vendors.php @@ -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; diff --git a/app/Imports/Sales/Customers.php b/app/Imports/Sales/Customers.php index c3b7a8a58..0803f6c1c 100644 --- a/app/Imports/Sales/Customers.php +++ b/app/Imports/Sales/Customers.php @@ -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; diff --git a/app/Imports/Sales/Sheets/InvoiceTransactions.php b/app/Imports/Sales/Sheets/InvoiceTransactions.php index 64880eaf4..2fb2cf21c 100644 --- a/app/Imports/Sales/Sheets/InvoiceTransactions.php +++ b/app/Imports/Sales/Sheets/InvoiceTransactions.php @@ -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']; diff --git a/app/Imports/Sales/Sheets/Invoices.php b/app/Imports/Sales/Sheets/Invoices.php index d6a9797df..e0d52e699 100644 --- a/app/Imports/Sales/Sheets/Invoices.php +++ b/app/Imports/Sales/Sheets/Invoices.php @@ -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; diff --git a/app/Traits/Import.php b/app/Traits/Import.php index bec4b881c..2ddd79853 100644 --- a/app/Traits/Import.php +++ b/app/Traits/Import.php @@ -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;