diff --git a/app/Abstracts/Import.php b/app/Abstracts/Import.php index 2165336f6..66f5edefa 100644 --- a/app/Abstracts/Import.php +++ b/app/Abstracts/Import.php @@ -2,11 +2,7 @@ namespace App\Abstracts; -use App\Models\Banking\Account; -use App\Models\Common\Contact; -use App\Models\Common\Item; -use App\Models\Setting\Category; -use App\Models\Setting\Tax; +use App\Traits\Import as ImportHelpers; use Illuminate\Support\Str; use Jenssegers\Date\Date; use Maatwebsite\Excel\Concerns\Importable; @@ -22,7 +18,7 @@ use Maatwebsite\Excel\Validators\Failure; abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatchInserts, WithChunkReading, WithHeadingRow, WithMapping, WithValidation { - use Importable; + use Importable, ImportHelpers; public $empty_field = 'empty---'; @@ -91,104 +87,4 @@ abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatc { flash($e->getMessage())->error()->important(); } - - public function getAccountIdFromCurrency($row) - { - return Account::firstOrCreate([ - 'currency_code' => $row['currency_code'], - ], [ - 'company_id' => session('company_id'), - 'name' => $row['currency_code'], - 'number' => Account::max('number') + 1, - 'opening_balance' => 0, - 'enabled' => 1, - ])->id; - } - - public function getAccountIdFromName($row) - { - return Account::firstOrCreate([ - 'name' => $row['account_name'], - ], [ - 'company_id' => session('company_id'), - 'number' => Account::max('number') + 1, - 'currency_code' => setting('default.currency'), - 'opening_balance' => 0, - 'enabled' => 1, - ])->id; - } - - public function getAccountIdFromNumber($row) - { - return Account::firstOrCreate([ - 'number' => $row['account_number'], - ], [ - 'company_id' => session('company_id'), - 'name' => $row['account_number'], - 'currency_code' => setting('default.currency'), - 'opening_balance' => 0, - 'enabled' => 1, - ])->id; - } - - public function getCategoryIdFromName($row, $type) - { - return Category::firstOrCreate([ - 'name' => $row['category_name'], - ], [ - 'company_id' => session('company_id'), - 'type' => $type, - 'color' => '#' . dechex(rand(0x000000, 0xFFFFFF)), - 'enabled' => 1, - ])->id; - } - - public function getContactIdFromEmail($row, $type) - { - return Contact::firstOrCreate([ - 'email' => $row['contact_email'], - ], [ - 'company_id' => session('company_id'), - 'type' => $type, - 'name' => $row['contact_email'], - 'currency_code' => setting('default.currency'), - 'enabled' => 1, - ])->id; - } - - public function getContactIdFromName($row, $type) - { - return Contact::firstOrCreate([ - 'name' => $row['contact_name'], - ], [ - 'company_id' => session('company_id'), - 'type' => $type, - 'currency_code' => setting('default.currency'), - 'enabled' => 1, - ])->id; - } - - public function getItemIdFromName($row) - { - return Item::firstOrCreate([ - 'name' => $row['item_name'], - ], [ - 'company_id' => session('company_id'), - 'sale_price' => $row['price'], - 'purchase_price' => $row['price'], - 'enabled' => 1, - ])->id; - } - - public function getTaxIdFromRate($row, $type = 'normal') - { - return Tax::firstOrCreate([ - 'rate' => $row['tax_rate'], - ], [ - 'company_id' => session('company_id'), - 'type' => $type, - 'name' => $row['tax_rate'], - 'enabled' => 1, - ])->id; - } } diff --git a/app/Exports/Banking/Transactions.php b/app/Exports/Banking/Transactions.php index d991e6a22..828ef3e3c 100644 --- a/app/Exports/Banking/Transactions.php +++ b/app/Exports/Banking/Transactions.php @@ -9,7 +9,28 @@ class Transactions extends Export { public function collection() { - return Model::usingSearchString(request('search'))->get(); + $model = Model::with(['account', 'bill', 'category', 'contact', 'invoice'])->usingSearchString(request('search'))->get(); + + if (!empty($this->ids)) { + $model->whereIn('id', (array) $this->ids); + } + + return $model->get(); + } + + public function map($model): array + { + $model->account_name = $model->account->name; + $model->contact_email = $model->contact->email; + $model->category_name = $model->category->name; + + if ($model->type == 'income') { + $model->invoice_bill_number = $model->invoice ? $model->invoice->invoice_number : 0; + } else { + $model->invoice_bill_number = $model->bill ? $model->bill->bill_number : 0; + } + + return parent::map($model); } public function fields(): array @@ -20,10 +41,10 @@ class Transactions extends Export 'amount', 'currency_code', 'currency_rate', - 'account_id', - 'document_id', - 'contact_id', - 'category_id', + 'account_name', + 'invoice_bill_number', + 'contact_email', + 'category_name', 'description', 'payment_method', 'reference', diff --git a/app/Imports/Banking/Transactions.php b/app/Imports/Banking/Transactions.php index 75a25e98e..03d86d35a 100644 --- a/app/Imports/Banking/Transactions.php +++ b/app/Imports/Banking/Transactions.php @@ -3,8 +3,8 @@ namespace App\Imports\Banking; use App\Abstracts\Import; -use App\Models\Banking\Transaction as Model; use App\Http\Requests\Banking\Transaction as Request; +use App\Models\Banking\Transaction as Model; class Transactions extends Import { @@ -13,6 +13,18 @@ class Transactions extends Import return new Model($row); } + public function map($row): array + { + $row = parent::map($row); + + $row['account_id'] = $this->getAccountId($row); + $row['category_id'] = $this->getCategoryId($row); + $row['contact_id'] = $this->getContactId($row); + $row['document_id'] = $this->getDocumentId($row); + + return $row; + } + public function rules(): array { return (new Request())->rules(); diff --git a/app/Imports/Common/Items.php b/app/Imports/Common/Items.php index 6560b1ab9..f3e78b060 100644 --- a/app/Imports/Common/Items.php +++ b/app/Imports/Common/Items.php @@ -17,13 +17,8 @@ class Items extends Import { $row = parent::map($row); - if (empty($row['category_id']) && !empty($row['category_name'])) { - $row['category_id'] = $this->getCategoryIdFromName($row, 'item'); - } - - if (empty($row['tax_id']) && !empty($row['tax_rate'])) { - $row['tax_id'] = $this->getTaxIdFromRate($row); - } + $row['category_id'] = $this->getCategoryId($row, 'item'); + $row['tax_id'] = $this->getTaxId($row); return $row; } diff --git a/app/Imports/Purchases/Payments.php b/app/Imports/Purchases/Payments.php index 6c6a20e9f..e5b9a9dde 100644 --- a/app/Imports/Purchases/Payments.php +++ b/app/Imports/Purchases/Payments.php @@ -5,7 +5,6 @@ namespace App\Imports\Purchases; use App\Abstracts\Import; use App\Models\Banking\Transaction as Model; use App\Http\Requests\Banking\Transaction as Request; -use App\Models\Purchase\Bill; class Payments extends Import { @@ -19,34 +18,10 @@ class Payments extends Import $row = parent::map($row); $row['type'] = 'expense'; - - if (empty($row['account_id']) && !empty($row['account_name'])) { - $row['account_id'] = $this->getAccountIdFromName($row); - } - - if (empty($row['account_id']) && !empty($row['account_number'])) { - $row['account_id'] = $this->getAccountIdFromNumber($row); - } - - if (empty($row['account_id']) && !empty($row['currency_code'])) { - $row['account_id'] = $this->getAccountIdFromCurrency($row); - } - - if (empty($row['contact_id']) && !empty($row['contact_name'])) { - $row['contact_id'] = $this->getContactIdFromName($row, 'vendor'); - } - - if (empty($row['contact_id']) && !empty($row['contact_email'])) { - $row['contact_id'] = $this->getContactIdFromEmail($row, 'vendor'); - } - - if (empty($row['category_id']) && !empty($row['category_name'])) { - $row['category_id'] = $this->getCategoryIdFromName($row, 'expense'); - } - - if (!empty($row['bill_number'])) { - $row['document_id'] = Bill::number($row['bill_number'])->pluck('id')->first(); - } + $row['account_id'] = $this->getAccountId($row); + $row['category_id'] = $this->getCategoryId($row, 'expense'); + $row['contact_id'] = $this->getContactId($row, 'vendor'); + $row['document_id'] = $this->getDocumentId($row); return $row; } diff --git a/app/Imports/Sales/Revenues.php b/app/Imports/Sales/Revenues.php index 57e4da863..babc5e92a 100644 --- a/app/Imports/Sales/Revenues.php +++ b/app/Imports/Sales/Revenues.php @@ -5,7 +5,6 @@ namespace App\Imports\Sales; use App\Abstracts\Import; use App\Http\Requests\Banking\Transaction as Request; use App\Models\Banking\Transaction as Model; -use App\Models\Sale\Invoice; class Revenues extends Import { @@ -19,34 +18,10 @@ class Revenues extends Import $row = parent::map($row); $row['type'] = 'income'; - - if (empty($row['account_id']) && !empty($row['account_name'])) { - $row['account_id'] = $this->getAccountIdFromName($row); - } - - if (empty($row['account_id']) && !empty($row['account_number'])) { - $row['account_id'] = $this->getAccountIdFromNumber($row); - } - - if (empty($row['account_id']) && !empty($row['currency_code'])) { - $row['account_id'] = $this->getAccountIdFromCurrency($row); - } - - if (empty($row['contact_id']) && !empty($row['contact_name'])) { - $row['contact_id'] = $this->getContactIdFromName($row, 'customer'); - } - - if (empty($row['contact_id']) && !empty($row['contact_email'])) { - $row['contact_id'] = $this->getContactIdFromEmail($row, 'customer'); - } - - if (empty($row['category_id']) && !empty($row['category_name'])) { - $row['category_id'] = $this->getCategoryIdFromName($row, 'income'); - } - - if (!empty($row['invoice_number'])) { - $row['document_id'] = Invoice::number($row['invoice_number'])->pluck('id')->first(); - } + $row['account_id'] = $this->getAccountId($row); + $row['category_id'] = $this->getCategoryId($row, 'income'); + $row['contact_id'] = $this->getContactId($row, 'customer'); + $row['document_id'] = $this->getDocumentId($row); return $row; } diff --git a/app/Imports/Sales/Sheets/InvoiceItemTaxes.php b/app/Imports/Sales/Sheets/InvoiceItemTaxes.php index 54a770c95..58ab7e812 100644 --- a/app/Imports/Sales/Sheets/InvoiceItemTaxes.php +++ b/app/Imports/Sales/Sheets/InvoiceItemTaxes.php @@ -35,13 +35,7 @@ class InvoiceItemTaxes extends Import $row['invoice_item_id'] = InvoiceItem::where('item_id', $item_id)->pluck('id')->first(); } - if (empty($row['tax_id']) && !empty($row['tax_name'])) { - $row['tax_id'] = Tax::name($row['tax_name'])->pluck('id')->first(); - } - - if (empty($row['tax_id']) && !empty($row['tax_rate'])) { - $row['tax_id'] = $this->getTaxIdFromRate($row); - } + $row['tax_id'] = $this->getTaxId($row); if (empty($row['name']) && !empty($row['item_name'])) { $row['name'] = $row['item_name']; diff --git a/app/Imports/Sales/Sheets/InvoiceTransactions.php b/app/Imports/Sales/Sheets/InvoiceTransactions.php index 429177e9d..1b4ec7e9f 100644 --- a/app/Imports/Sales/Sheets/InvoiceTransactions.php +++ b/app/Imports/Sales/Sheets/InvoiceTransactions.php @@ -5,7 +5,6 @@ namespace App\Imports\Sales\Sheets; use App\Abstracts\Import; use App\Http\Requests\Banking\Transaction as Request; use App\Models\Banking\Transaction as Model; -use App\Models\Sale\Invoice; class InvoiceTransactions extends Import { @@ -19,32 +18,10 @@ class InvoiceTransactions extends Import $row = parent::map($row); $row['type'] = 'income'; - - if (empty($row['account_id']) && !empty($row['account_name'])) { - $row['account_id'] = $this->getAccountIdFromName($row); - } - - if (empty($row['account_id']) && !empty($row['account_number'])) { - $row['account_id'] = $this->getAccountIdFromNumber($row); - } - - if (empty($row['account_id']) && !empty($row['currency_code'])) { - $row['account_id'] = $this->getAccountIdFromCurrency($row); - } - - if (empty($row['contact_id']) && !empty($row['contact_name'])) { - $row['contact_id'] = $this->getContactIdFromName($row, 'customer'); - } - - if (empty($row['contact_id']) && !empty($row['contact_email'])) { - $row['contact_id'] = $this->getContactIdFromEmail($row, 'customer'); - } - - if (empty($row['category_id']) && !empty($row['category_name'])) { - $row['category_id'] = $this->getCategoryIdFromName($row, 'income'); - } - - $row['document_id'] = Invoice::number($row['invoice_number'])->pluck('id')->first(); + $row['account_id'] = $this->getAccountId($row); + $row['category_id'] = $this->getCategoryId($row, 'income'); + $row['contact_id'] = $this->getContactId($row, 'customer'); + $row['document_id'] = $this->getDocumentId($row); return $row; } diff --git a/app/Imports/Sales/Sheets/Invoices.php b/app/Imports/Sales/Sheets/Invoices.php index 5d1afb140..d13377113 100644 --- a/app/Imports/Sales/Sheets/Invoices.php +++ b/app/Imports/Sales/Sheets/Invoices.php @@ -17,17 +17,8 @@ class Invoices extends Import { $row = parent::map($row); - if (empty($row['contact_id']) && !empty($row['contact_name'])) { - $row['contact_id'] = $this->getContactIdFromName($row, 'customer'); - } - - if (empty($row['contact_id']) && !empty($row['contact_email'])) { - $row['contact_id'] = $this->getContactIdFromEmail($row, 'customer'); - } - - if (empty($row['category_id']) && !empty($row['category_name'])) { - $row['category_id'] = $this->getCategoryIdFromName($row, 'income'); - } + $row['category_id'] = $this->getCategoryId($row, 'income'); + $row['contact_id'] = $this->getContactId($row, 'customer'); return $row; } diff --git a/app/Traits/Import.php b/app/Traits/Import.php new file mode 100644 index 000000000..704c4c578 --- /dev/null +++ b/app/Traits/Import.php @@ -0,0 +1,212 @@ +getAccountIdFromName($row); + } + + if (empty($id) && !empty($row['account_number'])) { + $id = $this->getAccountIdFromNumber($row); + } + + if (empty($id) && !empty($row['currency_code'])) { + $id = $this->getAccountIdFromCurrency($row); + } + + return $id; + } + + public function getCategoryId($row, $type = null) + { + $id = isset($row['category_id']) ? $row['category_id'] : null; + + $type = !empty($type) ? $type : (!empty($row['type']) ? $row['type'] : 'income'); + + if (empty($id) && !empty($row['category_name'])) { + $id = $this->getCategoryIdFromName($row, $type); + } + + return $id; + } + + public function getContactId($row, $type = null) + { + $id = isset($row['contact_id']) ? $row['contact_id'] : null; + + $type = !empty($type) ? $type : (!empty($row['type']) ? (($row['type'] == 'income') ? 'customer' : 'vendor') : 'customer'); + + if (empty($id) && !empty($row['contact_name'])) { + $id = $this->getContactIdFromName($row, $type); + } + + if (empty($row['contact_id']) && !empty($row['contact_email'])) { + $id = $this->getContactIdFromEmail($row, $type); + } + + return $id; + } + + public function getDocumentId($row) + { + $id = isset($row['document_id']) ? $row['document_id'] : null; + + if (empty($id) && !empty($row['invoice_number'])) { + $id = Invoice::number($row['invoice_number'])->pluck('id')->first(); + } + + if (empty($id) && !empty($row['bill_number'])) { + $id = Bill::number($row['bill_number'])->pluck('id')->first(); + } + + if (empty($id) && !empty($row['invoice_bill_number'])) { + if ($row['type'] == 'income') { + $id = Invoice::number($row['invoice_bill_number'])->pluck('id')->first(); + } else { + $id = Bill::number($row['invoice_bill_number'])->pluck('id')->first(); + } + } + + return $id; + } + + public function getItemId($row) + { + $id = isset($row['item_id']) ? $row['item_id'] : null; + + if (empty($id) && !empty($row['item_name'])) { + $id = $this->getItemIdFromName($row); + } + + return $id; + } + + public function getTaxId($row) + { + $id = isset($row['tax_id']) ? $row['tax_id'] : null; + + if (empty($id) && !empty($row['tax_name'])) { + $id = Tax::name($row['tax_name'])->pluck('id')->first(); + } + + if (empty($id) && !empty($row['tax_rate'])) { + $id = $this->getTaxIdFromRate($row); + } + + return $id; + } + + public function getAccountIdFromCurrency($row) + { + return Account::firstOrCreate([ + 'currency_code' => $row['currency_code'], + ], [ + 'company_id' => session('company_id'), + 'name' => $row['currency_code'], + 'number' => Account::max('number') + 1, + 'opening_balance' => 0, + 'enabled' => 1, + ])->id; + } + + public function getAccountIdFromName($row) + { + return Account::firstOrCreate([ + 'name' => $row['account_name'], + ], [ + 'company_id' => session('company_id'), + 'number' => Account::max('number') + 1, + 'currency_code' => setting('default.currency'), + 'opening_balance' => 0, + 'enabled' => 1, + ])->id; + } + + public function getAccountIdFromNumber($row) + { + return Account::firstOrCreate([ + 'number' => $row['account_number'], + ], [ + 'company_id' => session('company_id'), + 'name' => $row['account_number'], + 'currency_code' => setting('default.currency'), + 'opening_balance' => 0, + 'enabled' => 1, + ])->id; + } + + public function getCategoryIdFromName($row, $type) + { + return Category::firstOrCreate([ + 'name' => $row['category_name'], + ], [ + 'company_id' => session('company_id'), + 'type' => $type, + 'color' => '#' . dechex(rand(0x000000, 0xFFFFFF)), + 'enabled' => 1, + ])->id; + } + + public function getContactIdFromEmail($row, $type) + { + return Contact::firstOrCreate([ + 'email' => $row['contact_email'], + ], [ + 'company_id' => session('company_id'), + 'type' => $type, + 'name' => $row['contact_email'], + 'currency_code' => setting('default.currency'), + 'enabled' => 1, + ])->id; + } + + public function getContactIdFromName($row, $type) + { + return Contact::firstOrCreate([ + 'name' => $row['contact_name'], + ], [ + 'company_id' => session('company_id'), + 'type' => $type, + 'currency_code' => setting('default.currency'), + 'enabled' => 1, + ])->id; + } + + public function getItemIdFromName($row) + { + return Item::firstOrCreate([ + 'name' => $row['item_name'], + ], [ + 'company_id' => session('company_id'), + 'sale_price' => $row['price'], + 'purchase_price' => $row['price'], + 'enabled' => 1, + ])->id; + } + + public function getTaxIdFromRate($row, $type = 'normal') + { + return Tax::firstOrCreate([ + 'rate' => $row['tax_rate'], + ], [ + 'company_id' => session('company_id'), + 'type' => $type, + 'name' => $row['tax_rate'], + 'enabled' => 1, + ])->id; + } +} diff --git a/public/files/import/transactions.xlsx b/public/files/import/transactions.xlsx index 7b6db1042..a84bce0ba 100644 Binary files a/public/files/import/transactions.xlsx and b/public/files/import/transactions.xlsx differ