123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace App\Models;
- use App\Models\Product;
- use App\Models\ProductStock;
- use App\Models\User;
- use Maatwebsite\Excel\Concerns\ToModel;
- use Maatwebsite\Excel\Concerns\WithHeadingRow;
- use Maatwebsite\Excel\Concerns\WithValidation;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Illuminate\Support\Str;
- use Auth;
- use Carbon\Carbon;
- use Storage;
- //class ProductsImport implements ToModel, WithHeadingRow, WithValidation
- class ProductsImport implements ToCollection, WithHeadingRow, WithValidation, ToModel
- {
- private $rows = 0;
- public function collection(Collection $rows)
- {
- $canImport = true;
- $user = Auth::user();
- if ($user->user_type == 'seller' && addon_is_activated('seller_subscription')) {
- if ((count($rows) + $user->products()->count()) > $user->shop->product_upload_limit
- || $user->shop->package_invalid_at == null
- || Carbon::now()->diffInDays(Carbon::parse($user->shop->package_invalid_at), false) < 0
- ) {
- $canImport = false;
- flash(translate('Please upgrade your package.'))->warning();
- }
- }
- if ($canImport) {
- foreach ($rows as $row) {
- $approved = 1;
- if ($user->user_type == 'seller' && get_setting('product_approve_by_admin') == 1) {
- $approved = 0;
- }
- $productId = Product::create([
- 'name' => $row['name'],
- 'description' => $row['description'],
- 'added_by' => $user->user_type == 'seller' ? 'seller' : 'admin',
- 'user_id' => $user->user_type == 'seller' ? $user->id : User::where('user_type', 'admin')->first()->id,
- 'approved' => $approved,
- 'category_id' => $row['category_id'],
- 'brand_id' => $row['brand_id'],
- 'video_provider' => $row['video_provider'],
- 'video_link' => $row['video_link'],
- 'tags' => $row['tags'],
- 'unit_price' => $row['unit_price'],
- 'unit' => $row['unit'],
- 'meta_title' => $row['meta_title'],
- 'meta_description' => $row['meta_description'],
- 'colors' => json_encode(array()),
- 'choice_options' => json_encode(array()),
- 'variations' => json_encode(array()),
- 'slug' => preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', strtolower($row['slug']))) . '-' . Str::random(5),
- 'thumbnail_img' => $this->downloadThumbnail($row['thumbnail_img']),
- 'photos' => $this->downloadGalleryImages($row['photos']),
- ]);
- ProductStock::create([
- 'product_id' => $productId->id,
- 'qty' => $row['current_stock'],
- 'price' => $row['unit_price'],
- 'sku' => $row['sku'],
- 'variant' => '',
- ]);
- }
- flash(translate('Products imported successfully'))->success();
- }
- }
- public function model(array $row)
- {
- ++$this->rows;
- }
- public function getRowCount(): int
- {
- return $this->rows;
- }
- public function rules(): array
- {
- return [
- // Can also use callback validation rules
- 'unit_price' => function ($attribute, $value, $onFailure) {
- if (!is_numeric($value)) {
- $onFailure('Unit price is not numeric');
- }
- }
- ];
- }
- public function downloadThumbnail($url)
- {
- try {
- $upload = new Upload;
- $upload->external_link = $url;
- $upload->type = 'image';
- $upload->save();
- return $upload->id;
- } catch (\Exception $e) {
- }
- return null;
- }
- public function downloadGalleryImages($urls)
- {
- $data = array();
- foreach (explode(',', str_replace(' ', '', $urls)) as $url) {
- $data[] = $this->downloadThumbnail($url);
- }
- return implode(',', $data);
- }
- }
|