diff --git a/app/Exports/Purchases/Sheets/BillHistories.php b/app/Exports/Purchases/Sheets/BillHistories.php index 177bfbd9c..86b26b0f9 100644 --- a/app/Exports/Purchases/Sheets/BillHistories.php +++ b/app/Exports/Purchases/Sheets/BillHistories.php @@ -9,7 +9,7 @@ class BillHistories extends Export { public function collection() { - $model = Model::usingSearchString(request('search')); + $model = Model::with(['bill'])->usingSearchString(request('search')); if (!empty($this->ids)) { $model->whereIn('bill_id', (array) $this->ids); @@ -18,10 +18,17 @@ class BillHistories extends Export return $model->get(); } + public function map($model): array + { + $model->bill_number = $model->bill->bill_number; + + return parent::map($model); + } + public function fields(): array { return [ - 'bill_id', + 'bill_number', 'status', 'notify', 'description', diff --git a/app/Exports/Purchases/Sheets/BillItemTaxes.php b/app/Exports/Purchases/Sheets/BillItemTaxes.php index 46ff81c08..7ceed7662 100644 --- a/app/Exports/Purchases/Sheets/BillItemTaxes.php +++ b/app/Exports/Purchases/Sheets/BillItemTaxes.php @@ -9,7 +9,7 @@ class BillItemTaxes extends Export { public function collection() { - $model = Model::usingSearchString(request('search')); + $model = Model::with(['bill', 'item', 'tax'])->usingSearchString(request('search')); if (!empty($this->ids)) { $model->whereIn('bill_id', (array) $this->ids); @@ -18,13 +18,21 @@ class BillItemTaxes extends Export return $model->get(); } + public function map($model): array + { + $model->bill_number = $model->bill->bill_number; + $model->item_name = $model->item->name; + $model->tax_rate = $model->tax->rate; + + return parent::map($model); + } + public function fields(): array { return [ - 'bill_id', - 'bill_item_id', - 'tax_id', - 'name', + 'bill_number', + 'item_name', + 'tax_rate', 'amount', ]; } diff --git a/app/Exports/Purchases/Sheets/BillItems.php b/app/Exports/Purchases/Sheets/BillItems.php index a77ef4c5a..b131339f2 100644 --- a/app/Exports/Purchases/Sheets/BillItems.php +++ b/app/Exports/Purchases/Sheets/BillItems.php @@ -9,7 +9,7 @@ class BillItems extends Export { public function collection() { - $model = Model::usingSearchString(request('search')); + $model = Model::with(['bill', 'item'])->usingSearchString(request('search')); if (!empty($this->ids)) { $model->whereIn('bill_id', (array) $this->ids); @@ -18,12 +18,19 @@ class BillItems extends Export return $model->get(); } + public function map($model): array + { + $model->bill_number = $model->bill->bill_number; + $model->item_name = $model->item->name; + + return parent::map($model); + } + public function fields(): array { return [ - 'bill_id', - 'item_id', - 'name', + 'bill_number', + 'item_name', 'quantity', 'price', 'total', diff --git a/app/Exports/Purchases/Sheets/BillTotals.php b/app/Exports/Purchases/Sheets/BillTotals.php index 2cc0ea82f..388574f5d 100644 --- a/app/Exports/Purchases/Sheets/BillTotals.php +++ b/app/Exports/Purchases/Sheets/BillTotals.php @@ -9,7 +9,7 @@ class BillTotals extends Export { public function collection() { - $model = Model::usingSearchString(request('search')); + $model = Model::with(['bill'])->usingSearchString(request('search')); if (!empty($this->ids)) { $model->whereIn('bill_id', (array) $this->ids); @@ -18,11 +18,17 @@ class BillTotals extends Export return $model->get(); } + public function map($model): array + { + $model->bill_number = $model->bill->bill_number; + + return parent::map($model); + } public function fields(): array { return [ - 'bill_id', + 'bill_number', 'code', 'name', 'amount', diff --git a/app/Exports/Purchases/Sheets/BillTransactions.php b/app/Exports/Purchases/Sheets/BillTransactions.php index 7325ad2b6..7b19345fc 100644 --- a/app/Exports/Purchases/Sheets/BillTransactions.php +++ b/app/Exports/Purchases/Sheets/BillTransactions.php @@ -9,7 +9,7 @@ class BillTransactions extends Export { public function collection() { - $model = Model::type('expense')->isDocument()->usingSearchString(request('search')); + $model = Model::with(['account', 'category', 'contact', 'bill'])->type('expense')->isDocument()->usingSearchString(request('search')); if (!empty($this->ids)) { $model->whereIn('document_id', (array) $this->ids); @@ -18,17 +18,27 @@ class BillTransactions extends Export return $model->get(); } + public function map($model): array + { + $model->bill_number = $model->bill->bill_number; + $model->account_name = $model->account->name; + $model->category_name = $model->category->name; + $model->contact_email = $model->contact->email; + + return parent::map($model); + } + public function fields(): array { return [ + 'bill_number', 'paid_at', 'amount', 'currency_code', 'currency_rate', - 'account_id', - 'document_id', - 'contact_id', - 'category_id', + 'account_name', + 'contact_email', + 'category_name', 'description', 'payment_method', 'reference', diff --git a/app/Exports/Purchases/Sheets/Bills.php b/app/Exports/Purchases/Sheets/Bills.php index 6e674c6aa..2cf71415e 100644 --- a/app/Exports/Purchases/Sheets/Bills.php +++ b/app/Exports/Purchases/Sheets/Bills.php @@ -9,7 +9,7 @@ class Bills extends Export { public function collection() { - $model = Model::usingSearchString(request('search')); + $model = Model::with(['category'])->usingSearchString(request('search')); if (!empty($this->ids)) { $model->whereIn('id', (array) $this->ids); @@ -18,6 +18,13 @@ class Bills extends Export return $model->get(); } + public function map($model): array + { + $model->category_name = $model->category->name; + + return parent::map($model); + } + public function fields(): array { return [ @@ -29,8 +36,7 @@ class Bills extends Export 'amount', 'currency_code', 'currency_rate', - 'category_id', - 'contact_id', + 'category_name', 'contact_name', 'contact_email', 'contact_tax_number', diff --git a/app/Http/Requests/Purchase/BillItemTax.php b/app/Http/Requests/Purchase/BillItemTax.php new file mode 100644 index 000000000..c60c0828b --- /dev/null +++ b/app/Http/Requests/Purchase/BillItemTax.php @@ -0,0 +1,34 @@ + 'required|integer', + 'bill_item_id' => 'required|integer', + 'tax_id' => 'required|integer', + 'name' => 'required|string', + 'amount' => 'required', + ]; + } +} diff --git a/app/Imports/Purchases/Sheets/BillHistories.php b/app/Imports/Purchases/Sheets/BillHistories.php index 6a3d18e38..b534ef5cf 100644 --- a/app/Imports/Purchases/Sheets/BillHistories.php +++ b/app/Imports/Purchases/Sheets/BillHistories.php @@ -3,13 +3,19 @@ namespace App\Imports\Purchases\Sheets; use App\Abstracts\Import; -use App\Models\Purchase\BillHistory as Model; use App\Http\Requests\Purchase\BillHistory as Request; +use App\Models\Purchase\Bill; +use App\Models\Purchase\BillHistory as Model; class BillHistories extends Import { public function model(array $row) { + // @todo remove after 3.2 release + if ($row['bill_number'] == $this->empty_field) { + return null; + } + return new Model($row); } @@ -17,6 +23,8 @@ class BillHistories extends Import { $row = parent::map($row); + $row['bill_id'] = Bill::number($row['bill_number'])->pluck('id')->first(); + $row['notify'] = (int) $row['notify']; return $row; @@ -24,6 +32,11 @@ class BillHistories extends Import public function rules(): array { - return (new Request())->rules(); + $rules = (new Request())->rules(); + + $rules['bill_number'] = 'required|string'; + unset($rules['bill_id']); + + return $rules; } } diff --git a/app/Imports/Purchases/Sheets/BillItemTaxes.php b/app/Imports/Purchases/Sheets/BillItemTaxes.php index f0ba171a8..817ccb955 100644 --- a/app/Imports/Purchases/Sheets/BillItemTaxes.php +++ b/app/Imports/Purchases/Sheets/BillItemTaxes.php @@ -3,12 +3,53 @@ namespace App\Imports\Purchases\Sheets; use App\Abstracts\Import; +use App\Http\Requests\Purchase\BillItemTax as Request; +use App\Models\Common\Item; +use App\Models\Purchase\Bill; +use App\Models\Purchase\BillItem; use App\Models\Purchase\BillItemTax as Model; class BillItemTaxes extends Import { public function model(array $row) { + // @todo remove after 3.2 release + if ($row['bill_number'] == $this->empty_field) { + return null; + } + return new Model($row); } + + public function map($row): array + { + $row = parent::map($row); + + $row['bill_id'] = Bill::number($row['bill_number'])->pluck('id')->first(); + + if (empty($row['invoice_item_id']) && !empty($row['item_name'])) { + $item_id = Item::name($row['item_name'])->pluck('id')->first(); + $row['invoice_item_id'] = BillItem::where('item_id', $item_id)->pluck('id')->first(); + } + + $row['tax_id'] = $this->getTaxId($row); + + if (empty($row['name']) && !empty($row['item_name'])) { + $row['name'] = $row['item_name']; + } + + $row['amount'] = (double) $row['amount']; + + return $row; + } + + public function rules(): array + { + $rules = (new Request())->rules(); + + $rules['bill_number'] = 'required|string'; + unset($rules['bill_id']); + + return $rules; + } } diff --git a/app/Imports/Purchases/Sheets/BillItems.php b/app/Imports/Purchases/Sheets/BillItems.php index 51acb287d..5e1f254e9 100644 --- a/app/Imports/Purchases/Sheets/BillItems.php +++ b/app/Imports/Purchases/Sheets/BillItems.php @@ -3,8 +3,9 @@ namespace App\Imports\Purchases\Sheets; use App\Abstracts\Import; -use App\Models\Purchase\BillItem as Model; use App\Http\Requests\Purchase\BillItem as Request; +use App\Models\Purchase\Bill; +use App\Models\Purchase\BillItem as Model; class BillItems extends Import { @@ -13,8 +14,31 @@ class BillItems extends Import return new Model($row); } + public function map($row): array + { + $row = parent::map($row); + + $row['bill_id'] = Bill::number($row['bill_number'])->pluck('id')->first(); + + if (empty($row['item_id']) && !empty($row['item_name'])) { + $row['item_id'] = $this->getItemIdFromName($row); + + $row['name'] = $row['item_name']; + } + + $row['tax'] = (double) $row['tax']; + $row['tax_id'] = 0; + + return $row; + } + public function rules(): array { - return (new Request())->rules(); + $rules = (new Request())->rules(); + + $rules['bill_number'] = 'required|string'; + unset($rules['bill_id']); + + return $rules; } } diff --git a/app/Imports/Purchases/Sheets/BillTotals.php b/app/Imports/Purchases/Sheets/BillTotals.php index 18b93a0a2..b425d9f26 100644 --- a/app/Imports/Purchases/Sheets/BillTotals.php +++ b/app/Imports/Purchases/Sheets/BillTotals.php @@ -3,8 +3,9 @@ namespace App\Imports\Purchases\Sheets; use App\Abstracts\Import; -use App\Models\Purchase\BillTotal as Model; use App\Http\Requests\Purchase\BillTotal as Request; +use App\Models\Purchase\Bill; +use App\Models\Purchase\BillTotal as Model; class BillTotals extends Import { @@ -13,8 +14,22 @@ class BillTotals extends Import return new Model($row); } + public function map($row): array + { + $row = parent::map($row); + + $row['bill_id'] = Bill::number($row['bill_number'])->pluck('id')->first(); + + return $row; + } + public function rules(): array { - return (new Request())->rules(); + $rules = (new Request())->rules(); + + $rules['bill_number'] = 'required|string'; + unset($rules['bill_id']); + + return $rules; } } diff --git a/app/Imports/Purchases/Sheets/BillTransactions.php b/app/Imports/Purchases/Sheets/BillTransactions.php index dcbafbf0c..6f6952687 100644 --- a/app/Imports/Purchases/Sheets/BillTransactions.php +++ b/app/Imports/Purchases/Sheets/BillTransactions.php @@ -18,12 +18,20 @@ class BillTransactions extends Import $row = parent::map($row); $row['type'] = 'expense'; + $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; } public function rules(): array { - return (new Request())->rules(); + $rules = (new Request())->rules(); + + $rules['bill_number'] = 'required|string'; + + return $rules; } } diff --git a/app/Imports/Purchases/Sheets/Bills.php b/app/Imports/Purchases/Sheets/Bills.php index f50ca9d45..768b1fa97 100644 --- a/app/Imports/Purchases/Sheets/Bills.php +++ b/app/Imports/Purchases/Sheets/Bills.php @@ -13,6 +13,16 @@ class Bills extends Import return new Model($row); } + public function map($row): array + { + $row = parent::map($row); + + $row['category_id'] = $this->getCategoryId($row, 'expense'); + $row['contact_id'] = $this->getContactId($row, 'vendor'); + + return $row; + } + public function rules(): array { return (new Request())->rules(); diff --git a/app/Imports/Sales/Sheets/InvoiceHistories.php b/app/Imports/Sales/Sheets/InvoiceHistories.php index f4b09d503..a40afcdd2 100644 --- a/app/Imports/Sales/Sheets/InvoiceHistories.php +++ b/app/Imports/Sales/Sheets/InvoiceHistories.php @@ -11,6 +11,11 @@ class InvoiceHistories extends Import { public function model(array $row) { + // @todo remove after 3.2 release + if ($row['invoice_number'] == $this->empty_field) { + return null; + } + return new Model($row); } diff --git a/app/Imports/Sales/Sheets/InvoiceItemTaxes.php b/app/Imports/Sales/Sheets/InvoiceItemTaxes.php index 58ab7e812..68cfaf91a 100644 --- a/app/Imports/Sales/Sheets/InvoiceItemTaxes.php +++ b/app/Imports/Sales/Sheets/InvoiceItemTaxes.php @@ -8,7 +8,6 @@ use App\Models\Common\Item; use App\Models\Sale\Invoice; use App\Models\Sale\InvoiceItem; use App\Models\Sale\InvoiceItemTax as Model; -use App\Models\Setting\Tax; class InvoiceItemTaxes extends Import { @@ -19,8 +18,6 @@ class InvoiceItemTaxes extends Import return null; } - $row['invoice_id'] = Invoice::number($row['invoice_number'])->pluck('id')->first(); - return new Model($row); } diff --git a/app/Traits/Import.php b/app/Traits/Import.php index 704c4c578..5644300f8 100644 --- a/app/Traits/Import.php +++ b/app/Traits/Import.php @@ -50,14 +50,14 @@ trait Import $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); } + if (empty($id) && !empty($row['contact_name'])) { + $id = $this->getContactIdFromName($row, $type); + } + return $id; } @@ -116,9 +116,9 @@ trait Import 'currency_code' => $row['currency_code'], ], [ 'company_id' => session('company_id'), - 'name' => $row['currency_code'], - 'number' => Account::max('number') + 1, - 'opening_balance' => 0, + 'name' => !empty($row['account_name']) ? $row['account_name'] : $row['currency_code'], + 'number' => !empty($row['account_number']) ? $row['account_number'] : Account::max('number') + 1, + 'opening_balance' => !empty($row['opening_balance']) ? $row['opening_balance'] : 0, 'enabled' => 1, ])->id; } @@ -129,9 +129,9 @@ trait Import 'name' => $row['account_name'], ], [ 'company_id' => session('company_id'), - 'number' => Account::max('number') + 1, - 'currency_code' => setting('default.currency'), - 'opening_balance' => 0, + 'number' => !empty($row['account_number']) ? $row['account_number'] : Account::max('number') + 1, + 'currency_code' => !empty($row['currency_code']) ? $row['currency_code'] : setting('default.currency'), + 'opening_balance' => !empty($row['opening_balance']) ? $row['opening_balance'] : 0, 'enabled' => 1, ])->id; } @@ -142,9 +142,9 @@ trait Import 'number' => $row['account_number'], ], [ 'company_id' => session('company_id'), - 'name' => $row['account_number'], - 'currency_code' => setting('default.currency'), - 'opening_balance' => 0, + 'name' => !empty($row['account_name']) ? $row['account_name'] : $row['account_number'], + 'currency_code' => !empty($row['currency_code']) ? $row['currency_code'] : setting('default.currency'), + 'opening_balance' => !empty($row['opening_balance']) ? $row['opening_balance'] : 0, 'enabled' => 1, ])->id; } @@ -156,7 +156,7 @@ trait Import ], [ 'company_id' => session('company_id'), 'type' => $type, - 'color' => '#' . dechex(rand(0x000000, 0xFFFFFF)), + 'color' => !empty($row['category_color']) ? $row['category_color'] : '#' . dechex(rand(0x000000, 0xFFFFFF)), 'enabled' => 1, ])->id; } @@ -168,8 +168,8 @@ trait Import ], [ 'company_id' => session('company_id'), 'type' => $type, - 'name' => $row['contact_email'], - 'currency_code' => setting('default.currency'), + 'name' => !empty($row['contact_name']) ? $row['contact_name'] : $row['contact_email'], + 'currency_code' => !empty($row['contact_currency']) ? $row['contact_currency'] : setting('default.currency'), 'enabled' => 1, ])->id; } @@ -181,7 +181,7 @@ trait Import ], [ 'company_id' => session('company_id'), 'type' => $type, - 'currency_code' => setting('default.currency'), + 'currency_code' => !empty($row['contact_currency']) ? $row['contact_currency'] : setting('default.currency'), 'enabled' => 1, ])->id; } @@ -192,8 +192,8 @@ trait Import 'name' => $row['item_name'], ], [ 'company_id' => session('company_id'), - 'sale_price' => $row['price'], - 'purchase_price' => $row['price'], + 'sale_price' => !empty($row['sale_price']) ? $row['sale_price'] : $row['price'], + 'purchase_price' => !empty($row['purchase_price']) ? $row['purchase_price'] : $row['price'], 'enabled' => 1, ])->id; } @@ -205,7 +205,7 @@ trait Import ], [ 'company_id' => session('company_id'), 'type' => $type, - 'name' => $row['tax_rate'], + 'name' => !empty($row['tax_name']) ? $row['tax_name'] : $row['tax_rate'], 'enabled' => 1, ])->id; } diff --git a/public/files/import/bills.xlsx b/public/files/import/bills.xlsx index a97039531..053cd8509 100644 Binary files a/public/files/import/bills.xlsx and b/public/files/import/bills.xlsx differ diff --git a/public/files/import/customers.xlsx b/public/files/import/customers.xlsx index 286a86032..06c6044a1 100644 Binary files a/public/files/import/customers.xlsx and b/public/files/import/customers.xlsx differ