diff --git a/app/Exports/Common/Items.php b/app/Exports/Common/Items.php index 6c7bd6127..22c2aee92 100644 --- a/app/Exports/Common/Items.php +++ b/app/Exports/Common/Items.php @@ -9,7 +9,7 @@ class Items extends Export { public function collection() { - $model = Model::usingSearchString(request('search')); + $model = Model::with(['category', 'tax'])->usingSearchString(request('search')); if (!empty($this->ids)) { $model->whereIn('id', (array) $this->ids); @@ -18,6 +18,14 @@ class Items extends Export 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 { return [ @@ -25,8 +33,8 @@ class Items extends Export 'description', 'sale_price', 'purchase_price', - 'category_id', - 'tax_id', + 'category_name', + 'tax_rate', 'enabled', ]; } diff --git a/app/Imports/Common/Items.php b/app/Imports/Common/Items.php index 44c2cbd26..31fa1dfb7 100644 --- a/app/Imports/Common/Items.php +++ b/app/Imports/Common/Items.php @@ -3,18 +3,45 @@ namespace App\Imports\Common; use App\Abstracts\Import; -use App\Models\Common\Item as Model; 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 { public function model(array $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 diff --git a/app/Models/Common/Item.php b/app/Models/Common/Item.php index ea551dfc0..030cd03db 100644 --- a/app/Models/Common/Item.php +++ b/app/Models/Common/Item.php @@ -41,7 +41,7 @@ class Item extends Model 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() diff --git a/public/files/import/items.xlsx b/public/files/import/items.xlsx index b7955b797..a40ac4257 100644 Binary files a/public/files/import/items.xlsx and b/public/files/import/items.xlsx differ