used jobs for import

This commit is contained in:
Denis Duliçi 2021-10-05 13:53:56 +03:00
parent 8e5abbf743
commit a3eccceb94

View File

@ -2,15 +2,29 @@
namespace App\Traits;
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;
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;
use App\Jobs\Setting\CreateTax;
use App\Models\Banking\Account;
use App\Models\Common\Contact;
use App\Models\Common\Item;
use App\Models\Document\Document;
use App\Models\Setting\Category;
use App\Models\Setting\Tax;
use App\Traits\Jobs;
use Illuminate\Support\Facades\Validator;
trait Import
{
use Jobs;
public function getAccountId($row)
{
$id = isset($row['account_id']) ? $row['account_id'] : null;
@ -115,101 +129,206 @@ trait Import
public function getAccountIdFromCurrency($row)
{
return Account::firstOrCreate([
$account_id = Account::where('currency_code', $row['currency_code'])->pluck('id')->first();
if (!empty($account_id)) {
return $account_id;
}
$data = [
'company_id' => company_id(),
'currency_code' => $row['currency_code'],
], [
'name' => !empty($row['account_name']) ? $row['account_name'] : $row['currency_code'],
'number' => !empty($row['account_number']) ? $row['account_number'] : rand(1, 10000),
'number' => !empty($row['account_number']) ? $row['account_number'] : (string) rand(1, 10000),
'opening_balance' => !empty($row['opening_balance']) ? $row['opening_balance'] : 0,
'enabled' => 1,
])->id;
'created_from' => $row['created_from'],
'created_by' => $row['created_by'],
];
Validator::validate($data, (new AccountRequest)->rules());
$account = $this->dispatch(new CreateAccount($data));
return $account->id;
}
public function getAccountIdFromName($row)
{
return Account::firstOrCreate([
$account_id = Account::where('name', $row['account_name'])->pluck('id')->first();
if (!empty($account_id)) {
return $account_id;
}
$data = [
'company_id' => company_id(),
'name' => $row['account_name'],
], [
'number' => !empty($row['account_number']) ? $row['account_number'] : rand(1, 10000),
'number' => !empty($row['account_number']) ? $row['account_number'] : (string) rand(1, 10000),
'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;
'created_from' => $row['created_from'],
'created_by' => $row['created_by'],
];
Validator::validate($data, (new AccountRequest)->rules());
$account = $this->dispatch(new CreateAccount($data));
return $account->id;
}
public function getAccountIdFromNumber($row)
{
return Account::firstOrCreate([
$account_id = Account::where('account_number', $row['account_number'])->pluck('id')->first();
if (!empty($account_id)) {
return $account_id;
}
$data = [
'company_id' => company_id(),
'number' => $row['account_number'],
], [
'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;
'created_from' => $row['created_from'],
'created_by' => $row['created_by'],
];
Validator::validate($data, (new AccountRequest)->rules());
$account = $this->dispatch(new CreateAccount($data));
return $account->id;
}
public function getCategoryIdFromName($row, $type)
{
return Category::firstOrCreate([
$category_id = Category::where('name', $row['category_name'])->pluck('id')->first();
if (!empty($category_id)) {
return $category_id;
}
$data = [
'company_id' => company_id(),
'name' => $row['category_name'],
], [
'type' => $type,
'color' => !empty($row['category_color']) ? $row['category_color'] : '#' . dechex(rand(0x000000, 0xFFFFFF)),
'enabled' => 1,
])->id;
'created_from' => $row['created_from'],
'created_by' => $row['created_by'],
];
Validator::validate($data, (new CategoryRequest)->rules());
$category = $this->dispatch(new CreateCategory($data));
return $category->id;
}
public function getContactIdFromEmail($row, $type)
{
return Contact::firstOrCreate([
$contact_id = Contact::where('email', $row['contact_email'])->pluck('id')->first();
if (!empty($contact_id)) {
return $contact_id;
}
$data = [
'company_id' => company_id(),
'email' => $row['contact_email'],
], [
'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;
'created_from' => $row['created_from'],
'created_by' => $row['created_by'],
];
Validator::validate($data, (new ContactRequest)->rules());
$contact = $this->dispatch(new CreateContact($data));
return $contact->id;
}
public function getContactIdFromName($row, $type)
{
return Contact::firstOrCreate([
$contact_id = Contact::where('name', $row['contact_name'])->pluck('id')->first();
if (!empty($contact_id)) {
return $contact_id;
}
$data = [
'company_id' => company_id(),
'name' => $row['contact_name'],
], [
'type' => $type,
'email' => !empty($row['contact_email']) ? $row['contact_email'] : null,
'currency_code' => !empty($row['contact_currency']) ? $row['contact_currency'] : setting('default.currency'),
'enabled' => 1,
])->id;
'created_from' => $row['created_from'],
'created_by' => $row['created_by'],
];
Validator::validate($data, (new ContactRequest)->rules());
$contact = $this->dispatch(new CreateContact($data));
return $contact->id;
}
public function getItemIdFromName($row)
{
return Item::firstOrCreate([
$item_id = Item::where('name', $row['item_name'])->pluck('id')->first();
if (!empty($item_id)) {
return $item_id;
}
$data = [
'company_id' => company_id(),
'name' => $row['item_name'],
], [
'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),
'enabled' => 1,
])->id;
'created_from' => $row['created_from'],
'created_by' => $row['created_by'],
];
Validator::validate($data, (new ItemRequest())->rules());
$item = $this->dispatch(new CreateItem($data));
return $item->id;
}
public function getTaxIdFromRate($row, $type = 'normal')
{
return Tax::firstOrCreate([
$tax_id = Tax::where('rate', $row['tax_rate'])->pluck('id')->first();
if (!empty($tax_id)) {
return $tax_id;
}
$data = [
'company_id' => company_id(),
'rate' => $row['tax_rate'],
], [
'type' => $type,
'name' => !empty($row['tax_name']) ? $row['tax_name'] : $row['tax_rate'],
'enabled' => 1,
])->id;
'created_from' => $row['created_from'],
'created_by' => $row['created_by'],
];
Validator::validate($data, (new TaxRequest())->rules());
$tax = $this->dispatch(new CreateTax($data));
return $tax->id;
}
}