ProductsImport.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Product;
  4. use App\Models\ProductStock;
  5. use App\Models\User;
  6. use Maatwebsite\Excel\Concerns\ToModel;
  7. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  8. use Maatwebsite\Excel\Concerns\WithValidation;
  9. use Illuminate\Support\Collection;
  10. use Maatwebsite\Excel\Concerns\ToCollection;
  11. use Illuminate\Support\Str;
  12. use Auth;
  13. use Carbon\Carbon;
  14. use Storage;
  15. //class ProductsImport implements ToModel, WithHeadingRow, WithValidation
  16. class ProductsImport implements ToCollection, WithHeadingRow, WithValidation, ToModel
  17. {
  18. private $rows = 0;
  19. public function collection(Collection $rows)
  20. {
  21. $canImport = true;
  22. $user = Auth::user();
  23. if ($user->user_type == 'seller' && addon_is_activated('seller_subscription')) {
  24. if ((count($rows) + $user->products()->count()) > $user->shop->product_upload_limit
  25. || $user->shop->package_invalid_at == null
  26. || Carbon::now()->diffInDays(Carbon::parse($user->shop->package_invalid_at), false) < 0
  27. ) {
  28. $canImport = false;
  29. flash(translate('Please upgrade your package.'))->warning();
  30. }
  31. }
  32. if ($canImport) {
  33. foreach ($rows as $row) {
  34. $approved = 1;
  35. if ($user->user_type == 'seller' && get_setting('product_approve_by_admin') == 1) {
  36. $approved = 0;
  37. }
  38. $productId = Product::create([
  39. 'name' => $row['name'],
  40. 'description' => $row['description'],
  41. 'added_by' => $user->user_type == 'seller' ? 'seller' : 'admin',
  42. 'user_id' => $user->user_type == 'seller' ? $user->id : User::where('user_type', 'admin')->first()->id,
  43. 'approved' => $approved,
  44. 'category_id' => $row['category_id'],
  45. 'brand_id' => $row['brand_id'],
  46. 'video_provider' => $row['video_provider'],
  47. 'video_link' => $row['video_link'],
  48. 'tags' => $row['tags'],
  49. 'unit_price' => $row['unit_price'],
  50. 'unit' => $row['unit'],
  51. 'meta_title' => $row['meta_title'],
  52. 'meta_description' => $row['meta_description'],
  53. 'colors' => json_encode(array()),
  54. 'choice_options' => json_encode(array()),
  55. 'variations' => json_encode(array()),
  56. 'slug' => preg_replace('/[^A-Za-z0-9\-]/', '', str_replace(' ', '-', strtolower($row['slug']))) . '-' . Str::random(5),
  57. 'thumbnail_img' => $this->downloadThumbnail($row['thumbnail_img']),
  58. 'photos' => $this->downloadGalleryImages($row['photos']),
  59. ]);
  60. ProductStock::create([
  61. 'product_id' => $productId->id,
  62. 'qty' => $row['current_stock'],
  63. 'price' => $row['unit_price'],
  64. 'sku' => $row['sku'],
  65. 'variant' => '',
  66. ]);
  67. }
  68. flash(translate('Products imported successfully'))->success();
  69. }
  70. }
  71. public function model(array $row)
  72. {
  73. ++$this->rows;
  74. }
  75. public function getRowCount(): int
  76. {
  77. return $this->rows;
  78. }
  79. public function rules(): array
  80. {
  81. return [
  82. // Can also use callback validation rules
  83. 'unit_price' => function ($attribute, $value, $onFailure) {
  84. if (!is_numeric($value)) {
  85. $onFailure('Unit price is not numeric');
  86. }
  87. }
  88. ];
  89. }
  90. public function downloadThumbnail($url)
  91. {
  92. try {
  93. $upload = new Upload;
  94. $upload->external_link = $url;
  95. $upload->type = 'image';
  96. $upload->save();
  97. return $upload->id;
  98. } catch (\Exception $e) {
  99. }
  100. return null;
  101. }
  102. public function downloadGalleryImages($urls)
  103. {
  104. $data = array();
  105. foreach (explode(',', str_replace(' ', '', $urls)) as $url) {
  106. $data[] = $this->downloadThumbnail($url);
  107. }
  108. return implode(',', $data);
  109. }
  110. }