items import/export

This commit is contained in:
denisdulici 2020-01-20 23:18:47 +03:00
parent 6923a3d6e4
commit b119deca9f
4 changed files with 43 additions and 8 deletions

View File

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

View File

@ -3,18 +3,45 @@
namespace App\Imports\Common; namespace App\Imports\Common;
use App\Abstracts\Import; use App\Abstracts\Import;
use App\Models\Common\Item as Model;
use App\Http\Requests\Common\Item as Request; use App\Http\Requests\Common\Item as Request;
use App\Jobs\Common\CreateItem; use App\Models\Common\Item as Model;
use App\Models\Setting\Category;
use App\Models\Setting\Tax;
class Items extends Import class Items extends Import
{ {
public function model(array $row) public function model(array $row)
{ {
return new Model($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);
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;
}
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;
}
return $row;
} }
public function rules(): array public function rules(): array

View File

@ -41,7 +41,7 @@ class Item extends Model
public function tax() 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() public function bill_items()

Binary file not shown.