transactions import/export

This commit is contained in:
denisdulici 2020-01-21 09:10:12 +03:00
parent 9b42e2d127
commit 75f17e5466
11 changed files with 270 additions and 222 deletions

View File

@ -2,11 +2,7 @@
namespace App\Abstracts; namespace App\Abstracts;
use App\Models\Banking\Account; use App\Traits\Import as ImportHelpers;
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 Illuminate\Support\Str;
use Jenssegers\Date\Date; use Jenssegers\Date\Date;
use Maatwebsite\Excel\Concerns\Importable; 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 abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatchInserts, WithChunkReading, WithHeadingRow, WithMapping, WithValidation
{ {
use Importable; use Importable, ImportHelpers;
public $empty_field = 'empty---'; public $empty_field = 'empty---';
@ -91,104 +87,4 @@ abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatc
{ {
flash($e->getMessage())->error()->important(); 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;
}
} }

View File

@ -9,7 +9,28 @@ class Transactions extends Export
{ {
public function collection() 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 public function fields(): array
@ -20,10 +41,10 @@ class Transactions extends Export
'amount', 'amount',
'currency_code', 'currency_code',
'currency_rate', 'currency_rate',
'account_id', 'account_name',
'document_id', 'invoice_bill_number',
'contact_id', 'contact_email',
'category_id', 'category_name',
'description', 'description',
'payment_method', 'payment_method',
'reference', 'reference',

View File

@ -3,8 +3,8 @@
namespace App\Imports\Banking; namespace App\Imports\Banking;
use App\Abstracts\Import; use App\Abstracts\Import;
use App\Models\Banking\Transaction as Model;
use App\Http\Requests\Banking\Transaction as Request; use App\Http\Requests\Banking\Transaction as Request;
use App\Models\Banking\Transaction as Model;
class Transactions extends Import class Transactions extends Import
{ {
@ -13,6 +13,18 @@ class Transactions extends Import
return new Model($row); 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 public function rules(): array
{ {
return (new Request())->rules(); return (new Request())->rules();

View File

@ -17,13 +17,8 @@ class Items extends Import
{ {
$row = parent::map($row); $row = parent::map($row);
if (empty($row['category_id']) && !empty($row['category_name'])) { $row['category_id'] = $this->getCategoryId($row, 'item');
$row['category_id'] = $this->getCategoryIdFromName($row, 'item'); $row['tax_id'] = $this->getTaxId($row);
}
if (empty($row['tax_id']) && !empty($row['tax_rate'])) {
$row['tax_id'] = $this->getTaxIdFromRate($row);
}
return $row; return $row;
} }

View File

@ -5,7 +5,6 @@ namespace App\Imports\Purchases;
use App\Abstracts\Import; use App\Abstracts\Import;
use App\Models\Banking\Transaction as Model; use App\Models\Banking\Transaction as Model;
use App\Http\Requests\Banking\Transaction as Request; use App\Http\Requests\Banking\Transaction as Request;
use App\Models\Purchase\Bill;
class Payments extends Import class Payments extends Import
{ {
@ -19,34 +18,10 @@ class Payments extends Import
$row = parent::map($row); $row = parent::map($row);
$row['type'] = 'expense'; $row['type'] = 'expense';
$row['account_id'] = $this->getAccountId($row);
if (empty($row['account_id']) && !empty($row['account_name'])) { $row['category_id'] = $this->getCategoryId($row, 'expense');
$row['account_id'] = $this->getAccountIdFromName($row); $row['contact_id'] = $this->getContactId($row, 'vendor');
} $row['document_id'] = $this->getDocumentId($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();
}
return $row; return $row;
} }

View File

@ -5,7 +5,6 @@ namespace App\Imports\Sales;
use App\Abstracts\Import; use App\Abstracts\Import;
use App\Http\Requests\Banking\Transaction as Request; use App\Http\Requests\Banking\Transaction as Request;
use App\Models\Banking\Transaction as Model; use App\Models\Banking\Transaction as Model;
use App\Models\Sale\Invoice;
class Revenues extends Import class Revenues extends Import
{ {
@ -19,34 +18,10 @@ class Revenues extends Import
$row = parent::map($row); $row = parent::map($row);
$row['type'] = 'income'; $row['type'] = 'income';
$row['account_id'] = $this->getAccountId($row);
if (empty($row['account_id']) && !empty($row['account_name'])) { $row['category_id'] = $this->getCategoryId($row, 'income');
$row['account_id'] = $this->getAccountIdFromName($row); $row['contact_id'] = $this->getContactId($row, 'customer');
} $row['document_id'] = $this->getDocumentId($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; return $row;
} }

View File

@ -35,13 +35,7 @@ class InvoiceItemTaxes extends Import
$row['invoice_item_id'] = InvoiceItem::where('item_id', $item_id)->pluck('id')->first(); $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'] = $this->getTaxId($row);
$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);
}
if (empty($row['name']) && !empty($row['item_name'])) { if (empty($row['name']) && !empty($row['item_name'])) {
$row['name'] = $row['item_name']; $row['name'] = $row['item_name'];

View File

@ -5,7 +5,6 @@ namespace App\Imports\Sales\Sheets;
use App\Abstracts\Import; use App\Abstracts\Import;
use App\Http\Requests\Banking\Transaction as Request; use App\Http\Requests\Banking\Transaction as Request;
use App\Models\Banking\Transaction as Model; use App\Models\Banking\Transaction as Model;
use App\Models\Sale\Invoice;
class InvoiceTransactions extends Import class InvoiceTransactions extends Import
{ {
@ -19,32 +18,10 @@ class InvoiceTransactions extends Import
$row = parent::map($row); $row = parent::map($row);
$row['type'] = 'income'; $row['type'] = 'income';
$row['account_id'] = $this->getAccountId($row);
if (empty($row['account_id']) && !empty($row['account_name'])) { $row['category_id'] = $this->getCategoryId($row, 'income');
$row['account_id'] = $this->getAccountIdFromName($row); $row['contact_id'] = $this->getContactId($row, 'customer');
} $row['document_id'] = $this->getDocumentId($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();
return $row; return $row;
} }

View File

@ -17,17 +17,8 @@ class Invoices extends Import
{ {
$row = parent::map($row); $row = parent::map($row);
if (empty($row['contact_id']) && !empty($row['contact_name'])) { $row['category_id'] = $this->getCategoryId($row, 'income');
$row['contact_id'] = $this->getContactIdFromName($row, 'customer'); $row['contact_id'] = $this->getContactId($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');
}
return $row; return $row;
} }

212
app/Traits/Import.php Normal file
View File

@ -0,0 +1,212 @@
<?php
namespace App\Traits;
use App\Models\Banking\Account;
use App\Models\Common\Contact;
use App\Models\Common\Item;
use App\Models\Purchase\Bill;
use App\Models\Sale\Invoice;
use App\Models\Setting\Category;
use App\Models\Setting\Tax;
trait Import
{
public function getAccountId($row)
{
$id = isset($row['account_id']) ? $row['account_id'] : null;
if (empty($id) && !empty($row['account_name'])) {
$id = $this->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;
}
}