SHARE
TWEET

Untitled

a guest May 19th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Imports;
  4.  
  5. use App\Models\Product;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\DB;
  8. use Maatwebsite\Excel\Concerns\ToCollection;
  9.  
  10. class PriceListImport implements ToCollection
  11. {
  12.     public function collection(Collection $rows)
  13.     {
  14.         foreach ($rows as $row) {
  15.             $description = $row[0];
  16.             $part_number = $row[1];
  17.             $price = $row[2];
  18.  
  19.             if (!$part_number || $part_number === 'Part Number') {
  20.                 continue;
  21.             }
  22.  
  23.             $product = DB::table('products')
  24.                 ->select('*')
  25.                 ->where('sku', '=', $part_number)
  26.                 ->first();
  27.  
  28.             if (!$product) {
  29.                 $new_product = new Product;
  30.                 $new_product->description = $description;
  31.                 $new_product->sku = $part_number;
  32.                 $new_product->price = 0;
  33.                 $new_product->is_available = true;
  34.  
  35.                 if (is_numeric($price)) {
  36.                     $new_product->price_type = 'fix';
  37.                     $new_product->price = (int) $price;
  38.                 } elseif (substr($price, -1) == '%') {
  39.                     $new_product->price_type = 'percent';
  40.                     $new_product->percent = rtrim($price, '%');
  41.                 }
  42.  
  43.                 $new_product->save();
  44.             } else {
  45.                 $product = Product::find($product->id);
  46.  
  47.                 if (!is_null($price)) {
  48.                     if ($product->price_type === 'percent') {
  49.                         $product->percent = $price;
  50.                     } elseif ($product->price_type === 'fix') {
  51.                         $product->price = $price;
  52.                     }
  53.                 }
  54.  
  55.                 $product->description = $description;
  56.                 $product->save();
  57.             }
  58.         }
  59.     }
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top