| Code Coverage
| Classes and Traits | Functions and Methods | Lines
Total | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 2 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 22
CommodityImport | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 2 | 110.00 | 0.00% covered (danger) | 0.00% | 0 / 22
__construct | 0.00% covered (danger) | 0.00% | 0 / 1 | 6.00 | 0.00% covered (danger) | 0.00% | 0 / 3
collection | 0.00% covered (danger) | 0.00% | 0 / 1 | 72.00 | 0.00% covered (danger) | 0.00% | 0 / 19

1 | <?php
2 |
3 | namespace App\Imports;
4 |
5 | use App\Commodity;
6 | use Illuminate\Support\Collection;
7 | use Maatwebsite\Excel\Concerns\ToCollection;
8 | use Maatwebsite\Excel\Concerns\WithHeadingRow;
9 |
10 | class CommodityImport implements ToCollection, WithHeadingRow
11 | {
12 | protected $isOverride=false;
13 | public function __construct($isOverride)
14 | {
15 | if($isOverride=='1')
16 | $this->isOverride=true;
17 | }
18 |
19 | /**
20 | * @param Collection $collections
21 | */
22 | public function collection(Collection $collections)
23 | {
24 | foreach ($collections as $row)
25 | {
26 | $barcode = $row['barcode'] ?? $row['BARCODE'] ?? $row['Barcode'];
27 | if(!$barcode)continue;
28 | $name = $row['name'] ?? $row['NAME'] ?? $row['Name'] ?? '';
29 | $sku = $row['sku'] ?? $row['SKU'] ?? $row['Sku'] ?? '';
30 | $owner = $row['owner'] ?? $row['owner_name'] ?? $row['OWNER'] ?? $row['Owner'] ?? '';
31 | $commodity=Commodity::where('barcode',$row['barcode'])->first();
32 | if($commodity){
33 | if($this->isOverride){
34 | $name?$commodity['name']= $name:false;
35 | $sku?$commodity['sku']= $sku:false;
36 | $owner?$commodity['owner_name']= $owner:false;
37 | $commodity->update();
38 | }
39 | }else{
40 | Commodity::create([
41 | 'name' => $name,
42 | 'sku' => $sku,
43 | 'owner_name' => $owner,
44 | 'barcode' => $barcode,
45 | ]);
46 | }
47 | }
48 | }
49 | }