Файловый менеджер - Редактировать - /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Jobs/ProcessProductsImport.php
Назад
<?php namespace App\Jobs; use App\Mail\ImportCompleted; use App\Models\Category; use App\Models\Product; use App\Models\ProductPrice; use App\Models\ProductsImport; use App\Models\ProductVariation; use App\Models\ProductVariationCombination; use App\Models\ProductVariationRange; use App\Models\SiteSetting; use App\Models\TempProductUrl; use App\Models\Variation; use App\Models\Vendor; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use PhpOffice\PhpSpreadsheet\Exception; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; use Symfony\Component\HttpFoundation\Response; class ProcessProductsImport implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public ProductsImport $import; public function __construct(ProductsImport $import) { $this->import = $import; } /** * @throws Exception * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception */ public function handle() { \Log::channel('import_logs')->info('Import job started at : '. now()->format('Y-m-d h:i:s')); \Log::channel('import_logs')->info($this->import); $reader = new Xlsx(); $spreadsheet = $reader->load($this->import->getFirstMediaPath('file_import')); $data = $spreadsheet->getSheet(0)->toArray(); foreach ($data as $key => $datum){ if ($key == 0 || $datum[0] == null) continue; $row = $this->convertKeysIndexToAssociative($datum); if (!$row) continue; $slug = \str()->slug($row['product_name']); $parent_category = $this->getParentCategory(name: $row['parent_category']); $category = $this->getCategory(name: $row['category'], parent_category: $parent_category->id); $vendor = $this->getVendor(name: $row['vendor_name'], email: $row['vendor_email']); $dimension = $this->getDimensionVariation(dimension: $row['product_dimension']); $colors = $this->getColorVariations(data: $datum); $price_ranges = $this->getPriceRangeArray(data: $datum); $product_date = [ 'category_id' => $category->id, 'vendor_id' => $vendor->id, 'featured_image' => $row['product_image'], 'sku' => $row['supplier_prod_number'], 'name' => $row['product_name'], 'slug' => $slug, 'engraving' => strtolower($row['engraving']) == 'yes' ? 1 : 0, 'color_engraving' => strtolower($row['color_engraving']) == 'yes' ? 1 : 0, 'has_variations' => 1, 'description' => $row['product_description'], 'engraving_fee' => str_replace('$', '', $data['engraving_fee'] ?? 0), 'status' => 1, ]; $product = $this->getProduct(data: $product_date); $combinations = $this->getCombinations($product, $row, $dimension, $colors, $price_ranges); $this->getProductPrice(product: $product, combinations: $combinations); } $this->import->status = 'Completed'; $this->import->completed_at = new \DateTime(); $this->import->save(); \Log::channel('import_logs')->info('Import job finished at : '. now()->format('Y-m-d h:i:s')); \Bus::dispatch((new AssociateProductMedia())->delay(now()->addSeconds(5))); try { $admin_email = SiteSetting::where('key', 'Notification Email')->first(); Mail::to($admin_email->value)->later(now()->addSeconds(5),new ImportCompleted()); }catch (\Exception $e){ Log::channel('import_logs')->info('Mail Error => SendingImport Completed Mail.'); Log::channel('import_logs')->info($e->getMessage()); } } public function convertKeysIndexToAssociative($row): array { $header = [ 'category', 'parent_category', 'vendor_name', 'vendor_email', 'supplier_prod_number', 'product_name', 'product_template', 'product_image', 'product_description', 'product_dimension', 'engraving', 'color_engraving', 'engraving_fee', 'vendor_price', 'weight', 'width', 'height', 'length', 'price_range_1', 'price_range_2', 'price_range_3', 'price_range_4', 'price_range_5', 'price_range_6', 'color_1', 'color_2', 'color_3', 'color_4', 'color_5', ]; return count($header) == count($row) ? array_combine($header, $row) : []; } public function getParentCategory($name) { $name = ucwords(strtolower($name)); return Category::firstOrCreate([ 'name' => $name ],[ 'name' => $name, 'slug' => \Str::slug($name), 'parent_id' => null, 'status' => 1, 'show_in_navigation' => 0, ]); } public function getCategory($name, $parent_category) { $name = ucwords(strtolower($name)); return Category::firstOrCreate([ 'name' => $name ],[ 'name' => $name, 'slug' => \Str::slug($name), 'parent_id' => $parent_category, 'status' => 1, 'show_in_navigation' => 0, ]); } public function getVendor($name, $email) { $name = ucwords(strtolower($name)); return Vendor::firstOrCreate([ 'name' => $name ],[ 'name' => $name, 'email' => $email, ]); } public function getDimensionVariation($dimension) { return Variation::firstOrCreate([ 'type' => 'dimension', 'value' => $dimension ],[ 'type' => 'dimension', 'value' => $dimension, 'status' => 1, ]); } public function getColorVariations($data) { $colors = []; for ($i = 24; $i <= 28; $i++){ if (!$data[$i]) continue; $color = Variation::firstOrCreate([ 'type' => 'color', 'value' => $data[$i] ],[ 'type' => 'color', 'value' => $data[$i], 'status' => 1, ]); $colors['id'][] = $color->id; $colors['value'][] = $color->value; } return $colors; } public function getPriceRangeArray($data) { $price_range = [ [ 'qty_from' => 1, 'qty_to' => 9, 'price' => 0, ], [ 'qty_from' => 10, 'qty_to' => 24, 'price' => 0, ], [ 'qty_from' => 25, 'qty_to' => 49, 'price' => 0, ], [ 'qty_from' => 50, 'qty_to' => 99, 'price' => 0, ], [ 'qty_from' => 100, 'qty_to' => 249, 'price' => 0, ], ]; $min_price = $max_price = 0; for ($i = 18, $j = 0; $i <= 22; $i++, $j++){ $price_range[$j]['price'] = (float) str_replace('$', '', $data[$i]); $min_price = $min_price == 0 ? $price_range[$j]['price'] : min($price_range[$j]['price'], $min_price); $max_price = $max_price == 0 ? $price_range[$j]['price'] : max($price_range[$j]['price'], $max_price); } return [ 'price_ranges' => $price_range, 'min_price' => $min_price, 'max_price' => $max_price, ]; } public function getProduct($data) { \DB::beginTransaction(); $product = []; try { $product = Product::where('name',$data['name'])->first(); if ($product) { $product->update([ 'category_id' => $data['category_id'], 'vendor_id' => $data['vendor_id'], 'engraving' => $data['engraving'], 'color_engraving' => $data['color_engraving'], 'description' => $data['description'], 'engraving_fee' => $data['engraving_fee'], ]); } else { $product = Product::create($data); } if ($data['featured_image'] && filter_var($data['featured_image'], FILTER_VALIDATE_URL)){ TempProductUrl::updateOrCreate([ 'model_id' => $product->id, 'type' => 'image', ],[ 'model_id' => $product->id, 'type' => 'image', 'url' => $data['featured_image'], ]); } \DB::commit(); } catch (\Exception $e) { \Log::channel('import_logs')->info('Import getProduct function error :'); \Log::channel('import_logs')->info($e->getMessage()); \DB::rollBack(); } return $product; } public function getCombinations($product, $row, $dimension, $colors, $price_ranges): array { $combinations = []; $range_min_price = $price_ranges['min_price']; $range_max_price = $price_ranges['max_price']; foreach ($colors['id'] ?? [0] as $key => $color){ $v = [$dimension->id]; if ($color != 0 ) { $v[] = $color; } $combinations[] = [ 'variations' => $v, 'product_id' => $product->id, 'vendor_price' => str_replace('$', '', $row['vendor_price']), 'supplier_prod_number' => $row['supplier_prod_number'] .'-'. ($colors['value'][$key] ?? $key), 'product_sketch' => $row['product_template'], 'shipping_weight' => $row['weight'], 'width' => $row['width'], 'height' => $row['height'], 'length' => $row['length'], 'min_price' => $range_min_price, 'max_price' => $range_max_price, 'avg_price' => ($range_min_price + $range_max_price) / 2, 'price_ranges' => $price_ranges['price_ranges'], ]; } return $combinations; } public function getProductPrice($product, $combinations): bool { foreach ($combinations as $combination){ $price = ProductPrice::updateOrCreate([ 'product_id' => $combination['product_id'], 'supplier_prod_number' => $combination['supplier_prod_number'], ],[ 'vendor_price' => $combination['vendor_price'], 'shipping_weight' => $combination['shipping_weight'], 'width' => $combination['width'], 'height' => $combination['height'], 'length' => $combination['length'], 'min_price' => $combination['min_price'], 'max_price' => $combination['max_price'], 'avg_price' => $combination['avg_price'], ]); if($combination['product_sketch'] ?? ''){ TempProductUrl::updateOrCreate([ 'model_id' => $price->id, 'type' => 'sketch', ],[ 'model_id' => $price->id, 'type' => 'sketch', 'url' => $combination['product_sketch'], ]); } foreach ($combination['price_ranges'] as $range){ ProductVariationRange::updateOrCreate([ 'product_id' => $product->id, 'product_price_id' => $price->id, 'qty_from' => $range['qty_from'], 'qty_to' => $range['qty_to'], 'price' => $range['price'], ],[ 'product_id' => $product->id, 'product_price_id' => $price->id, 'qty_from' => $range['qty_from'], 'qty_to' => $range['qty_to'], 'price' => $range['price'], ]); } foreach ($combination['variations'] as $variation){ ProductVariation::updateOrCreate([ 'product_id' => $product->id, 'variation_id' => $variation, ],[ 'product_id' => $product->id, 'variation_id' => $variation, ]); ProductVariationCombination::updateOrCreate([ 'product_id' => $product->id, 'variation_id' => $variation, 'product_price_id' => $price->id, ],[ 'product_id' => $product->id, 'variation_id' => $variation, 'product_price_id' => $price->id, ]); } } return true; } }
| ver. 1.4 |
Github
|
.
| PHP 8.1.29 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка