2019-11-16 10:21:14 +03:00
|
|
|
<?php
|
|
|
|
|
2019-12-31 15:49:09 +03:00
|
|
|
namespace App\Imports\Sales\Sheets;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-01-20 01:26:35 +03:00
|
|
|
use App\Abstracts\Import;
|
2019-12-31 15:49:09 +03:00
|
|
|
use App\Http\Requests\Sale\InvoiceItem as Request;
|
2020-01-20 22:58:49 +03:00
|
|
|
use App\Models\Common\Item;
|
|
|
|
use App\Models\Sale\Invoice;
|
|
|
|
use App\Models\Sale\InvoiceItem as Model;
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-01-20 01:26:35 +03:00
|
|
|
class InvoiceItems extends Import
|
2019-11-16 10:21:14 +03:00
|
|
|
{
|
|
|
|
public function model(array $row)
|
|
|
|
{
|
|
|
|
return new Model($row);
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:49 +03:00
|
|
|
public function map($row): array
|
|
|
|
{
|
|
|
|
$row = parent::map($row);
|
|
|
|
|
|
|
|
$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['name'] = $row['item_name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$row['tax'] = (double) $row['tax'];
|
|
|
|
$row['tax_id'] = 0;
|
|
|
|
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
public function rules(): array
|
|
|
|
{
|
2020-01-20 22:58:49 +03:00
|
|
|
$rules = (new Request())->rules();
|
|
|
|
|
|
|
|
$rules['invoice_number'] = 'required|string';
|
|
|
|
unset($rules['invoice_id']);
|
|
|
|
|
|
|
|
return $rules;
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
2020-01-20 01:26:35 +03:00
|
|
|
}
|