Merge pull request #1149 from denisdulici/master

Human friendly import/export
This commit is contained in:
Denis Duliçi 2020-01-21 09:58:01 +03:00 committed by GitHub
commit a600e9145b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
38 changed files with 558 additions and 195 deletions

View File

@ -2,6 +2,7 @@
namespace App\Abstracts;
use App\Traits\Import as ImportHelper;
use Illuminate\Support\Str;
use Jenssegers\Date\Date;
use Maatwebsite\Excel\Concerns\Importable;
@ -17,7 +18,7 @@ use Maatwebsite\Excel\Validators\Failure;
abstract class Import implements ToModel, SkipsOnError, SkipsOnFailure, WithBatchInserts, WithChunkReading, WithHeadingRow, WithMapping, WithValidation
{
use Importable;
use Importable, ImportHelper;
public $empty_field = 'empty---';

View File

@ -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',

View File

@ -9,7 +9,7 @@ class Items extends Export
{
public function collection()
{
$model = Model::usingSearchString(request('search'));
$model = Model::with(['category', 'tax'])->usingSearchString(request('search'));
if (!empty($this->ids)) {
$model->whereIn('id', (array) $this->ids);
@ -18,6 +18,14 @@ class Items extends Export
return $model->get();
}
public function map($model): array
{
$model->category_name = $model->category->name;
$model->tax_rate = $model->tax->rate;
return parent::map($model);
}
public function fields(): array
{
return [
@ -25,8 +33,8 @@ class Items extends Export
'description',
'sale_price',
'purchase_price',
'category_id',
'tax_id',
'category_name',
'tax_rate',
'enabled',
];
}

View File

@ -9,7 +9,7 @@ class Payments extends Export
{
public function collection()
{
$model = Model::type('expense')->usingSearchString(request('search'));
$model = Model::with(['account', 'bill', 'category', 'contact'])->type('expense')->usingSearchString(request('search'));
if (!empty($this->ids)) {
$model->whereIn('id', (array) $this->ids);
@ -18,6 +18,16 @@ class Payments extends Export
return $model->get();
}
public function map($model): array
{
$model->account_name = $model->account->name;
$model->bill_number = $model->bill ? $model->bill->bill_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 Payments extends Export
'amount',
'currency_code',
'currency_rate',
'account_id',
'document_id',
'contact_id',
'category_id',
'account_name',
'bill_number',
'contact_email',
'category_name',
'description',
'payment_method',
'reference',

View File

@ -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',

View File

@ -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',
];
}

View File

@ -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',

View File

@ -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',

View File

@ -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',

View File

@ -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',

View File

@ -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',

View File

@ -0,0 +1,34 @@
<?php
namespace App\Http\Requests\Purchase;
use App\Abstracts\Http\FormRequest;
class BillItemTax extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'bill_id' => 'required|integer',
'bill_item_id' => 'required|integer',
'tax_id' => 'required|integer',
'name' => 'required|string',
'amount' => 'required',
];
}
}

View File

@ -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();

View File

@ -3,18 +3,24 @@
namespace App\Imports\Common;
use App\Abstracts\Import;
use App\Models\Common\Item as Model;
use App\Http\Requests\Common\Item as Request;
use App\Jobs\Common\CreateItem;
use App\Models\Common\Item as Model;
class Items extends Import
{
public function model(array $row)
{
return new Model($row);
//$request = (new Request())->merge($row);
}
//return dispatch_now(new CreateItem($request));
public function map($row): array
{
$row = parent::map($row);
$row['category_id'] = $this->getCategoryId($row, 'item');
$row['tax_id'] = $this->getTaxId($row);
return $row;
}
public function rules(): array

View File

@ -18,6 +18,10 @@ class Payments 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;
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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();

View File

@ -3,8 +3,8 @@
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;
class Revenues extends Import
{
@ -18,6 +18,10 @@ class Revenues extends Import
$row = parent::map($row);
$row['type'] = 'income';
$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;
}

View File

@ -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);
}

View File

@ -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);
}
@ -35,20 +32,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'] = Tax::firstOrCreate([
'rate' => $row['tax_rate'],
], [
'company_id' => session('company_id'),
'type' => 'normal',
'name' => $row['tax_rate'],
'enabled' => 1,
])->id;
}
$row['tax_id'] = $this->getTaxId($row);
if (empty($row['name']) && !empty($row['item_name'])) {
$row['name'] = $row['item_name'];

View File

@ -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'];
}

View File

@ -4,11 +4,7 @@ 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
{
@ -22,78 +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'] = 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;
}
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;
}
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;
}
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;
}
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;
}
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['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;
}

View File

@ -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
{
@ -19,39 +17,8 @@ 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;
}
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;
}
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->getCategoryId($row, 'income');
$row['contact_id'] = $this->getContactId($row, 'customer');
return $row;
}

View File

@ -41,7 +41,7 @@ class Item extends Model
public function tax()
{
return $this->belongsTo('App\Models\Setting\Tax')->withDefault(['name' => trans('general.na')]);
return $this->belongsTo('App\Models\Setting\Tax')->withDefault(['name' => trans('general.na'), 'rate' => 0]);
}
public function bill_items()

View File

@ -117,6 +117,11 @@ class Bill extends Model
return $query->where('status', '<>', 'paid');
}
public function scopeNumber($query, $number)
{
return $query->where('bill_number', '=', $number);
}
public function onCloning($src, $child = null)
{
$this->status = 'draft';

View File

@ -34,7 +34,7 @@ class BillItem extends Model
public function item()
{
return $this->belongsTo('App\Models\Common\Item');
return $this->belongsTo('App\Models\Common\Item')->withDefault(['name' => trans('general.na')]);
}
public function taxes()

View File

@ -4,11 +4,11 @@ namespace App\Models\Purchase;
use App\Abstracts\Model;
use App\Traits\Currencies;
use Znck\Eloquent\Traits\BelongsToThrough;
class BillItemTax extends Model
{
use Currencies;
use Currencies, BelongsToThrough;
protected $table = 'bill_item_taxes';
@ -24,9 +24,14 @@ class BillItemTax extends Model
return $this->belongsTo('App\Models\Purchase\Bill');
}
public function item()
{
return $this->belongsToThrough('App\Models\Common\Item', 'App\Models\Purchase\BillItem', 'bill_item_id');
}
public function tax()
{
return $this->belongsTo('App\Models\Setting\Tax');
return $this->belongsTo('App\Models\Setting\Tax')->withDefault(['name' => trans('general.na')]);
}
/**

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($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;
}
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' => !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;
}
public function getAccountIdFromName($row)
{
return Account::firstOrCreate([
'name' => $row['account_name'],
], [
'company_id' => session('company_id'),
'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;
}
public function getAccountIdFromNumber($row)
{
return Account::firstOrCreate([
'number' => $row['account_number'],
], [
'company_id' => session('company_id'),
'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;
}
public function getCategoryIdFromName($row, $type)
{
return Category::firstOrCreate([
'name' => $row['category_name'],
], [
'company_id' => session('company_id'),
'type' => $type,
'color' => !empty($row['category_color']) ? $row['category_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' => !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;
}
public function getContactIdFromName($row, $type)
{
return Contact::firstOrCreate([
'name' => $row['contact_name'],
], [
'company_id' => session('company_id'),
'type' => $type,
'currency_code' => !empty($row['contact_currency']) ? $row['contact_currency'] : setting('default.currency'),
'enabled' => 1,
])->id;
}
public function getItemIdFromName($row)
{
return Item::firstOrCreate([
'name' => $row['item_name'],
], [
'company_id' => session('company_id'),
'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;
}
public function getTaxIdFromRate($row, $type = 'normal')
{
return Tax::firstOrCreate([
'rate' => $row['tax_rate'],
], [
'company_id' => session('company_id'),
'type' => $type,
'name' => !empty($row['tax_name']) ? $row['tax_name'] : $row['tax_rate'],
'enabled' => 1,
])->id;
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.