diff --git a/app/Abstracts/Import.php b/app/Abstracts/Import.php index ebd1fe995..2165336f6 100644 --- a/app/Abstracts/Import.php +++ b/app/Abstracts/Import.php @@ -2,6 +2,11 @@ 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 Illuminate\Support\Str; use Jenssegers\Date\Date; use Maatwebsite\Excel\Concerns\Importable; @@ -86,4 +91,104 @@ 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/Sales/Revenues.php b/app/Exports/Sales/Revenues.php index dd592bcbf..34e803906 100644 --- a/app/Exports/Sales/Revenues.php +++ b/app/Exports/Sales/Revenues.php @@ -9,7 +9,7 @@ class Revenues extends Export { public function collection() { - $model = Model::type('income')->usingSearchString(request('search')); + $model = Model::with(['account', 'category', 'contact', 'invoice'])->type('income')->usingSearchString(request('search')); if (!empty($this->ids)) { $model->whereIn('id', (array) $this->ids); @@ -18,6 +18,16 @@ class Revenues extends Export return $model->get(); } + public function map($model): array + { + $model->account_name = $model->account->name; + $model->invoice_number = $model->invoice ? $model->invoice->invoice_number : 0; + $model->contact_email = $model->contact->email; + $model->category_name = $model->category->name; + + return parent::map($model); + } + public function fields(): array { return [ @@ -25,10 +35,10 @@ class Revenues extends Export 'amount', 'currency_code', 'currency_rate', - 'account_id', - 'document_id', - 'contact_id', - 'category_id', + 'account_name', + 'invoice_number', + 'contact_email', + 'category_name', 'description', 'payment_method', 'reference', diff --git a/app/Imports/Common/Items.php b/app/Imports/Common/Items.php index 31fa1dfb7..6560b1ab9 100644 --- a/app/Imports/Common/Items.php +++ b/app/Imports/Common/Items.php @@ -5,8 +5,6 @@ namespace App\Imports\Common; use App\Abstracts\Import; use App\Http\Requests\Common\Item as Request; use App\Models\Common\Item as Model; -use App\Models\Setting\Category; -use App\Models\Setting\Tax; class Items extends Import { @@ -20,25 +18,11 @@ class Items extends Import $row = parent::map($row); if (empty($row['category_id']) && !empty($row['category_name'])) { - $row['category_id'] = Category::firstOrCreate([ - 'name' => $row['category_name'], - ], [ - 'company_id' => session('company_id'), - 'type' => 'income', - 'color' => '#' . dechex(rand(0x000000, 0xFFFFFF)), - 'enabled' => 1, - ])->id; + $row['category_id'] = $this->getCategoryIdFromName($row, 'item'); } if (empty($row['tax_id']) && !empty($row['tax_rate'])) { - $row['tax_id'] = Tax::firstOrCreate([ - 'rate' => $row['tax_rate'], - ], [ - 'company_id' => session('company_id'), - 'type' => 'normal', - 'name' => $row['tax_rate'], - 'enabled' => 1, - ])->id; + $row['tax_id'] = $this->getTaxIdFromRate($row); } return $row; diff --git a/app/Imports/Sales/Revenues.php b/app/Imports/Sales/Revenues.php index 5fe04dbf7..57e4da863 100644 --- a/app/Imports/Sales/Revenues.php +++ b/app/Imports/Sales/Revenues.php @@ -3,8 +3,9 @@ namespace App\Imports\Sales; 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; +use App\Models\Sale\Invoice; class Revenues extends Import { @@ -19,6 +20,34 @@ class Revenues extends Import $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(); + } + return $row; } diff --git a/app/Imports/Sales/Sheets/InvoiceItemTaxes.php b/app/Imports/Sales/Sheets/InvoiceItemTaxes.php index 0dbf13c2c..54a770c95 100644 --- a/app/Imports/Sales/Sheets/InvoiceItemTaxes.php +++ b/app/Imports/Sales/Sheets/InvoiceItemTaxes.php @@ -40,14 +40,7 @@ class InvoiceItemTaxes extends Import } if (empty($row['tax_id']) && !empty($row['tax_rate'])) { - $row['tax_id'] = Tax::firstOrCreate([ - 'rate' => $row['tax_rate'], - ], [ - 'company_id' => session('company_id'), - 'type' => 'normal', - 'name' => $row['tax_rate'], - 'enabled' => 1, - ])->id; + $row['tax_id'] = $this->getTaxIdFromRate($row); } if (empty($row['name']) && !empty($row['item_name'])) { diff --git a/app/Imports/Sales/Sheets/InvoiceItems.php b/app/Imports/Sales/Sheets/InvoiceItems.php index 8872923b9..0222bf547 100644 --- a/app/Imports/Sales/Sheets/InvoiceItems.php +++ b/app/Imports/Sales/Sheets/InvoiceItems.php @@ -4,7 +4,6 @@ namespace App\Imports\Sales\Sheets; use App\Abstracts\Import; use App\Http\Requests\Sale\InvoiceItem as Request; -use App\Models\Common\Item; use App\Models\Sale\Invoice; use App\Models\Sale\InvoiceItem as Model; @@ -22,14 +21,7 @@ class InvoiceItems extends Import $row['invoice_id'] = Invoice::number($row['invoice_number'])->pluck('id')->first(); if (empty($row['item_id']) && !empty($row['item_name'])) { - $row['item_id'] = Item::firstOrCreate([ - 'name' => $row['item_name'], - ], [ - 'company_id' => session('company_id'), - 'sale_price' => $row['price'], - 'purchase_price' => $row['price'], - 'enabled' => 1, - ])->id; + $row['item_id'] = $this->getItemIdFromName($row); $row['name'] = $row['item_name']; } diff --git a/app/Imports/Sales/Sheets/InvoiceTransactions.php b/app/Imports/Sales/Sheets/InvoiceTransactions.php index fd9fb41e9..429177e9d 100644 --- a/app/Imports/Sales/Sheets/InvoiceTransactions.php +++ b/app/Imports/Sales/Sheets/InvoiceTransactions.php @@ -4,11 +4,8 @@ namespace App\Imports\Sales\Sheets; use App\Abstracts\Import; use App\Http\Requests\Banking\Transaction as Request; -use App\Models\Banking\Account; use App\Models\Banking\Transaction as Model; -use App\Models\Common\Contact; use App\Models\Sale\Invoice; -use App\Models\Setting\Category; class InvoiceTransactions extends Import { @@ -24,73 +21,27 @@ class InvoiceTransactions extends Import $row['type'] = 'income'; if (empty($row['account_id']) && !empty($row['account_name'])) { - $row['account_id'] = 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; + $row['account_id'] = $this->getAccountIdFromName($row); } if (empty($row['account_id']) && !empty($row['account_number'])) { - $row['account_id'] = 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; + $row['account_id'] = $this->getAccountIdFromNumber($row); } if (empty($row['account_id']) && !empty($row['currency_code'])) { - $row['account_id'] = 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; + $row['account_id'] = $this->getAccountIdFromCurrency($row); } if (empty($row['contact_id']) && !empty($row['contact_name'])) { - $row['contact_id'] = Contact::firstOrCreate([ - 'name' => $row['contact_name'], - ], [ - 'company_id' => session('company_id'), - 'type' => 'customer', - 'currency_code' => setting('default.currency'), - 'enabled' => 1, - ])->id; + $row['contact_id'] = $this->getContactIdFromName($row, 'customer'); } if (empty($row['contact_id']) && !empty($row['contact_email'])) { - $row['contact_id'] = Contact::firstOrCreate([ - 'email' => $row['contact_email'], - ], [ - 'company_id' => session('company_id'), - 'type' => 'customer', - 'name' => $row['contact_email'], - 'currency_code' => setting('default.currency'), - 'enabled' => 1, - ])->id; + $row['contact_id'] = $this->getContactIdFromEmail($row, 'customer'); } if (empty($row['category_id']) && !empty($row['category_name'])) { - $row['category_id'] = Category::firstOrCreate([ - 'name' => $row['category_name'], - ], [ - 'company_id' => session('company_id'), - 'type' => 'income', - 'color' => '#' . dechex(rand(0x000000, 0xFFFFFF)), - 'enabled' => 1, - ])->id; + $row['category_id'] = $this->getCategoryIdFromName($row, 'income'); } $row['document_id'] = Invoice::number($row['invoice_number'])->pluck('id')->first(); diff --git a/app/Imports/Sales/Sheets/Invoices.php b/app/Imports/Sales/Sheets/Invoices.php index 58bbc812b..5d1afb140 100644 --- a/app/Imports/Sales/Sheets/Invoices.php +++ b/app/Imports/Sales/Sheets/Invoices.php @@ -4,9 +4,7 @@ namespace App\Imports\Sales\Sheets; use App\Abstracts\Import; use App\Http\Requests\Sale\Invoice as Request; -use App\Models\Common\Contact; use App\Models\Sale\Invoice as Model; -use App\Models\Setting\Category; class Invoices extends Import { @@ -20,37 +18,15 @@ class Invoices extends Import $row = parent::map($row); if (empty($row['contact_id']) && !empty($row['contact_name'])) { - $row['contact_id'] = Contact::firstOrCreate([ - 'name' => $row['contact_name'], - ], [ - 'company_id' => session('company_id'), - 'type' => 'customer', - 'currency_code' => setting('default.currency'), - 'enabled' => 1, - ])->id; + $row['contact_id'] = $this->getContactIdFromName($row, 'customer'); } if (empty($row['contact_id']) && !empty($row['contact_email'])) { - $row['contact_id'] = Contact::firstOrCreate([ - 'email' => $row['contact_email'], - ], [ - 'company_id' => session('company_id'), - 'type' => 'customer', - 'name' => $row['contact_email'], - 'currency_code' => setting('default.currency'), - 'enabled' => 1, - ])->id; + $row['contact_id'] = $this->getContactIdFromEmail($row, 'customer'); } if (empty($row['category_id']) && !empty($row['category_name'])) { - $row['category_id'] = Category::firstOrCreate([ - 'name' => $row['category_name'], - ], [ - 'company_id' => session('company_id'), - 'type' => 'income', - 'color' => '#' . dechex(rand(0x000000, 0xFFFFFF)), - 'enabled' => 1, - ])->id; + $row['category_id'] = $this->getCategoryIdFromName($row, 'income'); } return $row; diff --git a/public/files/import/items.xlsx b/public/files/import/items.xlsx index a40ac4257..bdc881fd8 100644 Binary files a/public/files/import/items.xlsx and b/public/files/import/items.xlsx differ diff --git a/public/files/import/revenues.xlsx b/public/files/import/revenues.xlsx index e29776a03..4b720e292 100644 Binary files a/public/files/import/revenues.xlsx and b/public/files/import/revenues.xlsx differ