213 lines
6.3 KiB
PHP
213 lines
6.3 KiB
PHP
|
<?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;
|
||
|
}
|
||
|
}
|