2020-01-21 06:10:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
2024-12-03 18:18:48 +00:00
|
|
|
use App\Events\Common\ImportViewCreating;
|
|
|
|
|
use App\Events\Common\ImportViewCreated;
|
2021-10-05 10:53:56 +00:00
|
|
|
use App\Http\Requests\Banking\Account as AccountRequest;
|
|
|
|
|
use App\Http\Requests\Common\Contact as ContactRequest;
|
|
|
|
|
use App\Http\Requests\Common\Item as ItemRequest;
|
|
|
|
|
use App\Http\Requests\Setting\Category as CategoryRequest;
|
2022-07-19 09:09:04 +00:00
|
|
|
use App\Http\Requests\Setting\Currency as CurrencyRequest;
|
2021-10-05 10:53:56 +00:00
|
|
|
use App\Http\Requests\Setting\Tax as TaxRequest;
|
|
|
|
|
use App\Jobs\Banking\CreateAccount;
|
|
|
|
|
use App\Jobs\Common\CreateContact;
|
|
|
|
|
use App\Jobs\Common\CreateItem;
|
|
|
|
|
use App\Jobs\Setting\CreateCategory;
|
2022-07-19 09:09:04 +00:00
|
|
|
use App\Jobs\Setting\CreateCurrency;
|
2021-10-05 10:53:56 +00:00
|
|
|
use App\Jobs\Setting\CreateTax;
|
2020-01-21 06:10:12 +00:00
|
|
|
use App\Models\Banking\Account;
|
2023-07-20 12:39:23 +00:00
|
|
|
use App\Models\Banking\Transaction;
|
2020-01-21 06:10:12 +00:00
|
|
|
use App\Models\Common\Contact;
|
|
|
|
|
use App\Models\Common\Item;
|
2020-12-23 22:28:38 +00:00
|
|
|
use App\Models\Document\Document;
|
2020-01-21 06:10:12 +00:00
|
|
|
use App\Models\Setting\Category;
|
2022-07-19 09:09:04 +00:00
|
|
|
use App\Models\Setting\Currency;
|
2020-01-21 06:10:12 +00:00
|
|
|
use App\Models\Setting\Tax;
|
2021-10-05 10:53:56 +00:00
|
|
|
use App\Traits\Jobs;
|
2023-03-22 13:30:04 +00:00
|
|
|
use App\Traits\Sources;
|
2024-04-24 08:53:17 +00:00
|
|
|
use App\Utilities\Modules;
|
2021-10-05 10:53:56 +00:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2024-12-03 18:18:48 +00:00
|
|
|
use Akaunting\Module\Module;
|
2020-01-21 06:10:12 +00:00
|
|
|
|
|
|
|
|
trait Import
|
|
|
|
|
{
|
2023-03-22 13:30:04 +00:00
|
|
|
use Jobs, Sources;
|
2021-10-05 10:53:56 +00:00
|
|
|
|
2024-12-03 18:18:48 +00:00
|
|
|
public function getImportView($group, $type, $route = null)
|
|
|
|
|
{
|
|
|
|
|
$path = company_id() . '/' . $group . '/' . $type;
|
|
|
|
|
|
|
|
|
|
$module = module($group);
|
|
|
|
|
|
|
|
|
|
if ($module instanceof Module) {
|
|
|
|
|
$title_type = trans_choice($group . '::general.' . str_replace('-', '_', $type), 2);
|
|
|
|
|
$sample_file = url('modules/' . $module->getStudlyName() . '/Resources/assets/' . $type . '.xlsx');
|
|
|
|
|
} else {
|
|
|
|
|
$title_type = trans_choice('general.' . str_replace('-', '_', $type), 2);
|
|
|
|
|
$sample_file = url('public/files/import/' . $type . '.xlsx');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$form_params = [
|
|
|
|
|
'id' => 'import',
|
|
|
|
|
'@submit.prevent' => 'onSubmit',
|
|
|
|
|
'@keydown' => 'form.errors.clear($event.target.name)',
|
|
|
|
|
'files' => true,
|
|
|
|
|
'role' => 'form',
|
|
|
|
|
'class' => 'form-loading-button',
|
|
|
|
|
'novalidate' => true,
|
|
|
|
|
'route' => '',
|
|
|
|
|
'url' => '',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (! empty($route)) {
|
|
|
|
|
$form_params['route'] = $route;
|
|
|
|
|
} else {
|
|
|
|
|
$form_params['url'] = $path . '/import';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$document_link = 'https://akaunting.com/hc/docs/import-export/';
|
|
|
|
|
|
|
|
|
|
$view = 'common.import.create';
|
|
|
|
|
|
|
|
|
|
$import = new \stdClass();
|
|
|
|
|
$import->view = 'common.import.create';
|
|
|
|
|
$import->data = compact('group', 'type', 'path', 'route', 'form_params', 'title_type', 'sample_file', 'document_link');
|
|
|
|
|
|
|
|
|
|
event(new ImportViewCreating($import));
|
|
|
|
|
|
|
|
|
|
event(new ImportViewCreated($import));
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
$import->view,
|
|
|
|
|
$import->data
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 06:10:12 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 08:34:34 +00:00
|
|
|
return is_null($id) ? $id : (int) $id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 08:34:34 +00:00
|
|
|
return is_null($id) ? $id : (int) $id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-21 09:17:58 +00:00
|
|
|
public function getCategoryType($type)
|
|
|
|
|
{
|
|
|
|
|
return array_key_exists($type, config('type.category')) ? $type : 'other';
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 06:10:12 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 06:55:12 +00:00
|
|
|
if (empty($id) && !empty($row['contact_name'])) {
|
|
|
|
|
$id = $this->getContactIdFromName($row, $type);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 08:34:34 +00:00
|
|
|
return is_null($id) ? $id : (int) $id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
2022-07-19 09:09:04 +00:00
|
|
|
public function getCurrencyCode($row)
|
|
|
|
|
{
|
|
|
|
|
$currency = Currency::where('code', $row['currency_code'])->first();
|
|
|
|
|
|
|
|
|
|
if (!empty($currency)) {
|
|
|
|
|
return $currency->code;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 09:43:45 +00:00
|
|
|
try {
|
|
|
|
|
$data = [
|
|
|
|
|
'company_id' => company_id(),
|
|
|
|
|
'code' => $row['currency_code'],
|
|
|
|
|
'name' => isset($row['currency_name']) ? $row['currency_name'] : currency($row['currency_code'])->getName(),
|
|
|
|
|
'rate' => isset($row['currency_rate']) ? $row['currency_rate'] : 1,
|
|
|
|
|
'symbol' => isset($row['currency_symbol']) ? $row['currency_symbol'] : currency($row['currency_code'])->getSymbol(),
|
|
|
|
|
'precision' => isset($row['currency_precision']) ? $row['currency_precision'] : currency($row['currency_code'])->getPrecision(),
|
|
|
|
|
'decimal_mark' => isset($row['currency_decimal_mark']) ? $row['currency_decimal_mark'] : currency($row['currency_code'])->getDecimalMark(),
|
|
|
|
|
'created_from' => !empty($row['created_from']) ? $row['created_from'] : $this->getSourcePrefix() . 'import',
|
|
|
|
|
'created_by' => !empty($row['created_by']) ? $row['created_by'] : user()?->id,
|
|
|
|
|
];
|
|
|
|
|
} catch (\OutOfBoundsException $e) {
|
|
|
|
|
return default_currency();
|
|
|
|
|
}
|
2022-07-19 09:09:04 +00:00
|
|
|
|
2023-02-07 22:35:41 +00:00
|
|
|
Validator::validate($data, (new CurrencyRequest)->rules());
|
2022-07-19 09:09:04 +00:00
|
|
|
|
|
|
|
|
$currency = $this->dispatch(new CreateCurrency($data));
|
|
|
|
|
|
|
|
|
|
return $currency->code;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 14:23:56 +00:00
|
|
|
public function getCreatedById($row)
|
|
|
|
|
{
|
2023-11-03 08:58:02 +00:00
|
|
|
if (empty($row['created_by'])) {
|
2024-02-07 14:27:08 +00:00
|
|
|
return $this->user?->id;
|
2023-11-03 08:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
2023-10-03 08:06:08 +00:00
|
|
|
$user = user_model_class()::where('email', $row['created_by'])->first();
|
2023-06-16 14:23:56 +00:00
|
|
|
|
|
|
|
|
if (! empty($user)) {
|
|
|
|
|
return $user->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->user->id;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 06:10:12 +00:00
|
|
|
public function getDocumentId($row)
|
|
|
|
|
{
|
|
|
|
|
$id = isset($row['document_id']) ? $row['document_id'] : null;
|
|
|
|
|
|
2021-02-15 20:11:10 +00:00
|
|
|
if (empty($id) && !empty($row['document_number'])) {
|
|
|
|
|
$id = Document::number($row['document_number'])->pluck('id')->first();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 06:10:12 +00:00
|
|
|
if (empty($id) && !empty($row['invoice_number'])) {
|
2020-12-23 22:28:38 +00:00
|
|
|
$id = Document::invoice()->number($row['invoice_number'])->pluck('id')->first();
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($id) && !empty($row['bill_number'])) {
|
2020-12-23 22:28:38 +00:00
|
|
|
$id = Document::bill()->number($row['bill_number'])->pluck('id')->first();
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($id) && !empty($row['invoice_bill_number'])) {
|
|
|
|
|
if ($row['type'] == 'income') {
|
2020-12-23 22:28:38 +00:00
|
|
|
$id = Document::invoice()->number($row['invoice_bill_number'])->pluck('id')->first();
|
2020-01-21 06:10:12 +00:00
|
|
|
} else {
|
2020-12-23 22:28:38 +00:00
|
|
|
$id = Document::bill()->number($row['invoice_bill_number'])->pluck('id')->first();
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 08:34:34 +00:00
|
|
|
return is_null($id) ? $id : (int) $id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-20 12:39:23 +00:00
|
|
|
public function getParentId($row)
|
|
|
|
|
{
|
|
|
|
|
$id = isset($row['parent_id']) ? $row['parent_id'] : null;
|
|
|
|
|
|
2024-02-19 13:06:47 +00:00
|
|
|
if (empty($row['parent_number']) && empty($row['parent_name'])){
|
2023-07-24 11:55:44 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($id) && (!empty($row['document_number']) || !empty($row['invoice_number']) || !empty($row['bill_number']))) {
|
2023-07-20 12:39:23 +00:00
|
|
|
$id = Document::number($row['parent_number'])->pluck('id')->first();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 11:55:44 +00:00
|
|
|
if (empty($id) && isset($row['number'])) {
|
2023-07-20 12:39:23 +00:00
|
|
|
$id = Transaction::number($row['parent_number'])->pluck('id')->first();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:06:47 +00:00
|
|
|
if (empty($id) && isset($row['parent_name'])) {
|
|
|
|
|
$id = Category::type($row['type'])->withSubCategory()->where('name', $row['parent_name'])->pluck('id')->first();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 12:39:23 +00:00
|
|
|
return is_null($id) ? $id : (int) $id;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-24 08:53:17 +00:00
|
|
|
public function getPaymentMethod($row)
|
|
|
|
|
{
|
|
|
|
|
Modules::clearPaymentMethodsCache();
|
|
|
|
|
|
|
|
|
|
$methods = Modules::getPaymentMethods('all');
|
|
|
|
|
|
|
|
|
|
$payment_method = isset($row['payment_method']) ? $row['payment_method'] : null;
|
|
|
|
|
|
|
|
|
|
if (array_key_exists($payment_method, $methods)) {
|
|
|
|
|
return $payment_method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (module_is_enabled('offline-payments')) {
|
|
|
|
|
$offline_payment = $this->dispatch(new \Modules\OfflinePayments\Jobs\CreatePaymentMethod([
|
|
|
|
|
'name' => $payment_method,
|
|
|
|
|
'customer' => 1,
|
|
|
|
|
'order' => count($methods) + 1,
|
|
|
|
|
'description' => '',
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$payment_method = $offline_payment['code'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $payment_method;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 10:26:48 +00:00
|
|
|
public function getItemId($row, $type = null)
|
2020-01-21 06:10:12 +00:00
|
|
|
{
|
|
|
|
|
$id = isset($row['item_id']) ? $row['item_id'] : null;
|
|
|
|
|
|
2023-05-26 10:26:48 +00:00
|
|
|
$type = !empty($type) ? $type : (!empty($row['item_type']) ? $row['item_type'] : 'product');
|
|
|
|
|
|
2020-01-21 06:10:12 +00:00
|
|
|
if (empty($id) && !empty($row['item_name'])) {
|
2023-05-26 10:26:48 +00:00
|
|
|
$id = $this->getItemIdFromName($row, $type);
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-26 08:34:34 +00:00
|
|
|
return is_null($id) ? $id : (int) $id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-26 08:34:34 +00:00
|
|
|
return is_null($id) ? $id : (int) $id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAccountIdFromCurrency($row)
|
|
|
|
|
{
|
2021-10-05 10:53:56 +00:00
|
|
|
$account_id = Account::where('currency_code', $row['currency_code'])->pluck('id')->first();
|
|
|
|
|
|
|
|
|
|
if (!empty($account_id)) {
|
|
|
|
|
return $account_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
2021-04-15 21:59:43 +00:00
|
|
|
'company_id' => company_id(),
|
2023-03-07 12:39:39 +00:00
|
|
|
'type' => !empty($row['account_type']) ? $row['account_type'] : 'bank',
|
2020-01-21 06:10:12 +00:00
|
|
|
'currency_code' => $row['currency_code'],
|
2020-01-21 06:55:12 +00:00
|
|
|
'name' => !empty($row['account_name']) ? $row['account_name'] : $row['currency_code'],
|
2021-10-05 10:53:56 +00:00
|
|
|
'number' => !empty($row['account_number']) ? $row['account_number'] : (string) rand(1, 10000),
|
2020-01-21 06:55:12 +00:00
|
|
|
'opening_balance' => !empty($row['opening_balance']) ? $row['opening_balance'] : 0,
|
2020-01-21 06:10:12 +00:00
|
|
|
'enabled' => 1,
|
2023-03-22 13:30:04 +00:00
|
|
|
'created_from' => !empty($row['created_from']) ? $row['created_from'] : $this->getSourcePrefix() . 'import',
|
2024-02-07 14:27:08 +00:00
|
|
|
'created_by' => !empty($row['created_by']) ? $row['created_by'] : user()?->id,
|
2021-10-05 10:53:56 +00:00
|
|
|
];
|
|
|
|
|
|
2023-02-07 22:35:41 +00:00
|
|
|
Validator::validate($data, (new AccountRequest)->rules());
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
$account = $this->dispatch(new CreateAccount($data));
|
|
|
|
|
|
|
|
|
|
return $account->id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAccountIdFromName($row)
|
|
|
|
|
{
|
2021-10-05 10:53:56 +00:00
|
|
|
$account_id = Account::where('name', $row['account_name'])->pluck('id')->first();
|
|
|
|
|
|
|
|
|
|
if (!empty($account_id)) {
|
|
|
|
|
return $account_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
2021-04-15 21:59:43 +00:00
|
|
|
'company_id' => company_id(),
|
2023-03-07 12:39:39 +00:00
|
|
|
'type' => !empty($row['account_type']) ? $row['account_type'] : 'bank',
|
2020-01-21 06:10:12 +00:00
|
|
|
'name' => $row['account_name'],
|
2021-10-05 10:53:56 +00:00
|
|
|
'number' => !empty($row['account_number']) ? $row['account_number'] : (string) rand(1, 10000),
|
2022-10-05 07:35:19 +00:00
|
|
|
'currency_code' => !empty($row['currency_code']) ? $row['currency_code'] : default_currency(),
|
2020-01-21 06:55:12 +00:00
|
|
|
'opening_balance' => !empty($row['opening_balance']) ? $row['opening_balance'] : 0,
|
2020-01-21 06:10:12 +00:00
|
|
|
'enabled' => 1,
|
2023-03-22 13:30:04 +00:00
|
|
|
'created_from' => !empty($row['created_from']) ? $row['created_from'] : $this->getSourcePrefix() . 'import',
|
2024-02-07 14:27:08 +00:00
|
|
|
'created_by' => !empty($row['created_by']) ? $row['created_by'] : user()?->id,
|
2021-10-05 10:53:56 +00:00
|
|
|
];
|
|
|
|
|
|
2023-02-07 22:35:41 +00:00
|
|
|
Validator::validate($data, (new AccountRequest)->rules());
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
$account = $this->dispatch(new CreateAccount($data));
|
|
|
|
|
|
|
|
|
|
return $account->id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAccountIdFromNumber($row)
|
|
|
|
|
{
|
2021-10-05 10:53:56 +00:00
|
|
|
$account_id = Account::where('account_number', $row['account_number'])->pluck('id')->first();
|
|
|
|
|
|
|
|
|
|
if (!empty($account_id)) {
|
|
|
|
|
return $account_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
2021-04-15 21:59:43 +00:00
|
|
|
'company_id' => company_id(),
|
2023-03-07 12:39:39 +00:00
|
|
|
'type' => !empty($row['account_type']) ? $row['account_type'] : 'bank',
|
2020-01-21 06:10:12 +00:00
|
|
|
'number' => $row['account_number'],
|
2020-01-21 06:55:12 +00:00
|
|
|
'name' => !empty($row['account_name']) ? $row['account_name'] : $row['account_number'],
|
2022-10-05 07:35:19 +00:00
|
|
|
'currency_code' => !empty($row['currency_code']) ? $row['currency_code'] : default_currency(),
|
2020-01-21 06:55:12 +00:00
|
|
|
'opening_balance' => !empty($row['opening_balance']) ? $row['opening_balance'] : 0,
|
2020-01-21 06:10:12 +00:00
|
|
|
'enabled' => 1,
|
2023-03-22 13:30:04 +00:00
|
|
|
'created_from' => !empty($row['created_from']) ? $row['created_from'] : $this->getSourcePrefix() . 'import',
|
2024-02-07 14:27:08 +00:00
|
|
|
'created_by' => !empty($row['created_by']) ? $row['created_by'] : user()?->id,
|
2021-10-05 10:53:56 +00:00
|
|
|
];
|
|
|
|
|
|
2023-02-07 22:35:41 +00:00
|
|
|
Validator::validate($data, (new AccountRequest)->rules());
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
$account = $this->dispatch(new CreateAccount($data));
|
|
|
|
|
|
|
|
|
|
return $account->id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCategoryIdFromName($row, $type)
|
|
|
|
|
{
|
2023-05-26 10:26:48 +00:00
|
|
|
$category_id = Category::type($type)->withSubCategory()->where('name', $row['category_name'])->pluck('id')->first();
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
if (!empty($category_id)) {
|
|
|
|
|
return $category_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
2021-04-15 21:59:43 +00:00
|
|
|
'company_id' => company_id(),
|
2020-01-21 06:10:12 +00:00
|
|
|
'name' => $row['category_name'],
|
|
|
|
|
'type' => $type,
|
2020-01-21 06:55:12 +00:00
|
|
|
'color' => !empty($row['category_color']) ? $row['category_color'] : '#' . dechex(rand(0x000000, 0xFFFFFF)),
|
2020-01-21 06:10:12 +00:00
|
|
|
'enabled' => 1,
|
2023-03-22 13:30:04 +00:00
|
|
|
'created_from' => !empty($row['created_from']) ? $row['created_from'] : $this->getSourcePrefix() . 'import',
|
2024-02-07 14:27:08 +00:00
|
|
|
'created_by' => !empty($row['created_by']) ? $row['created_by'] : user()?->id,
|
2021-10-05 10:53:56 +00:00
|
|
|
];
|
|
|
|
|
|
2023-02-07 22:35:41 +00:00
|
|
|
Validator::validate($data, (new CategoryRequest)->rules());
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
$category = $this->dispatch(new CreateCategory($data));
|
|
|
|
|
|
|
|
|
|
return $category->id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getContactIdFromEmail($row, $type)
|
|
|
|
|
{
|
2023-05-26 10:26:48 +00:00
|
|
|
$contact_id = Contact::type($type)->where('email', $row['contact_email'])->pluck('id')->first();
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
if (!empty($contact_id)) {
|
|
|
|
|
return $contact_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
2021-04-15 21:59:43 +00:00
|
|
|
'company_id' => company_id(),
|
2020-01-21 06:10:12 +00:00
|
|
|
'email' => $row['contact_email'],
|
|
|
|
|
'type' => $type,
|
2020-01-21 06:55:12 +00:00
|
|
|
'name' => !empty($row['contact_name']) ? $row['contact_name'] : $row['contact_email'],
|
2022-10-05 07:35:19 +00:00
|
|
|
'currency_code' => !empty($row['contact_currency']) ? $row['contact_currency'] : default_currency(),
|
2020-01-21 06:10:12 +00:00
|
|
|
'enabled' => 1,
|
2023-03-22 13:30:04 +00:00
|
|
|
'created_from' => !empty($row['created_from']) ? $row['created_from'] : $this->getSourcePrefix() . 'import',
|
2024-02-07 14:27:08 +00:00
|
|
|
'created_by' => !empty($row['created_by']) ? $row['created_by'] : user()?->id,
|
2021-10-05 10:53:56 +00:00
|
|
|
];
|
|
|
|
|
|
2023-02-07 22:35:41 +00:00
|
|
|
Validator::validate($data, (new ContactRequest)->rules());
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
$contact = $this->dispatch(new CreateContact($data));
|
|
|
|
|
|
|
|
|
|
return $contact->id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getContactIdFromName($row, $type)
|
|
|
|
|
{
|
2023-05-26 10:26:48 +00:00
|
|
|
$contact_id = Contact::type($type)->where('name', $row['contact_name'])->pluck('id')->first();
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
if (!empty($contact_id)) {
|
|
|
|
|
return $contact_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
2021-04-15 21:59:43 +00:00
|
|
|
'company_id' => company_id(),
|
2020-01-21 06:10:12 +00:00
|
|
|
'name' => $row['contact_name'],
|
|
|
|
|
'type' => $type,
|
2021-10-05 10:53:56 +00:00
|
|
|
'email' => !empty($row['contact_email']) ? $row['contact_email'] : null,
|
2022-10-05 07:35:19 +00:00
|
|
|
'currency_code' => !empty($row['contact_currency']) ? $row['contact_currency'] : default_currency(),
|
2020-01-21 06:10:12 +00:00
|
|
|
'enabled' => 1,
|
2023-03-22 13:30:04 +00:00
|
|
|
'created_from' => !empty($row['created_from']) ? $row['created_from'] : $this->getSourcePrefix() . 'import',
|
2024-02-07 14:27:08 +00:00
|
|
|
'created_by' => !empty($row['created_by']) ? $row['created_by'] : user()?->id,
|
2021-10-05 10:53:56 +00:00
|
|
|
];
|
|
|
|
|
|
2023-02-07 22:35:41 +00:00
|
|
|
Validator::validate($data, (new ContactRequest)->rules());
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
$contact = $this->dispatch(new CreateContact($data));
|
|
|
|
|
|
|
|
|
|
return $contact->id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-04 15:04:32 +00:00
|
|
|
public function getItemIdFromName($row, $type = null)
|
2020-01-21 06:10:12 +00:00
|
|
|
{
|
2023-06-04 15:04:32 +00:00
|
|
|
$type = !empty($type) ? $type : (!empty($row['item_type']) ? $row['item_type'] : 'product');
|
|
|
|
|
|
2023-05-26 10:26:48 +00:00
|
|
|
$item_id = Item::type($type)->where('name', $row['item_name'])->pluck('id')->first();
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
if (!empty($item_id)) {
|
|
|
|
|
return $item_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
2021-04-15 21:59:43 +00:00
|
|
|
'company_id' => company_id(),
|
2023-05-26 10:26:48 +00:00
|
|
|
'type' => $type,
|
2020-01-21 06:10:12 +00:00
|
|
|
'name' => $row['item_name'],
|
2023-03-09 13:45:20 +00:00
|
|
|
'description' => !empty($row['item_description']) ? $row['item_description'] : null,
|
2021-01-18 08:08:41 +00:00
|
|
|
'sale_price' => !empty($row['sale_price']) ? $row['sale_price'] : (!empty($row['price']) ? $row['price'] : 0),
|
|
|
|
|
'purchase_price' => !empty($row['purchase_price']) ? $row['purchase_price'] : (!empty($row['price']) ? $row['price'] : 0),
|
2020-01-21 06:10:12 +00:00
|
|
|
'enabled' => 1,
|
2023-03-22 13:30:04 +00:00
|
|
|
'created_from' => !empty($row['created_from']) ? $row['created_from'] : $this->getSourcePrefix() . 'import',
|
2024-02-07 14:27:08 +00:00
|
|
|
'created_by' => !empty($row['created_by']) ? $row['created_by'] : user()?->id,
|
2021-10-05 10:53:56 +00:00
|
|
|
];
|
|
|
|
|
|
2023-02-07 22:35:41 +00:00
|
|
|
Validator::validate($data, (new ItemRequest())->rules());
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
$item = $this->dispatch(new CreateItem($data));
|
|
|
|
|
|
|
|
|
|
return $item->id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTaxIdFromRate($row, $type = 'normal')
|
|
|
|
|
{
|
2023-05-26 10:26:48 +00:00
|
|
|
$tax_id = Tax::type($type)->where('rate', $row['tax_rate'])->pluck('id')->first();
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
if (!empty($tax_id)) {
|
|
|
|
|
return $tax_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
2021-04-15 21:59:43 +00:00
|
|
|
'company_id' => company_id(),
|
2020-01-21 06:10:12 +00:00
|
|
|
'rate' => $row['tax_rate'],
|
|
|
|
|
'type' => $type,
|
2023-02-07 22:35:41 +00:00
|
|
|
'name' => !empty($row['tax_name']) ? $row['tax_name'] : (string) $row['tax_rate'],
|
2020-01-21 06:10:12 +00:00
|
|
|
'enabled' => 1,
|
2023-03-22 13:30:04 +00:00
|
|
|
'created_from' => !empty($row['created_from']) ? $row['created_from'] : $this->getSourcePrefix() . 'import',
|
2024-02-07 14:27:08 +00:00
|
|
|
'created_by' => !empty($row['created_by']) ? $row['created_by'] : user()?->id,
|
2021-10-05 10:53:56 +00:00
|
|
|
];
|
|
|
|
|
|
2023-02-07 22:35:41 +00:00
|
|
|
Validator::validate($data, (new TaxRequest())->rules());
|
2021-10-05 10:53:56 +00:00
|
|
|
|
|
|
|
|
$tax = $this->dispatch(new CreateTax($data));
|
|
|
|
|
|
|
|
|
|
return $tax->id;
|
2020-01-21 06:10:12 +00:00
|
|
|
}
|
|
|
|
|
}
|