HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/database/seeds/SampleProducts.php
<?php
namespace App\seeds;

use App\Vat\VatScenarioEnum;
use Komma\KMS\Globalization\Languages\Models\Language;
use App\Products\Product\Product;
use App\Products\Product\ProductTranslation;
use App\Products\ProductComposite\ProductComposite;
use App\Products\ProductComposite\ProductCompositeTranslation;
use App\Products\ProductGroup\ProductGroup;
use App\Products\ProductGroup\ProductGroupTranslation;
use App\Products\ProductGroupBehaviour\ProductGroupBehaviour;
use Komma\KMS\Sites\Models\Site;
use Illuminate\Database\Seeder;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

/**
 * Class SampleProducts
 *
 * Needed for showcasing. Not for testing.
 *
 * @package App\seeds
 */
class SampleProducts extends Seeder
{
    public function run()
    {

        $productsData = [
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '799',
                    'vat_scenario_enum' => VatScenarioEnum::high_inc,
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Basic - Attachment materials'
                        ]
                    ],
                    'de' => [
                        'attributes' => [
                            'name' => 'Basic - Befestigungsmaterial'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Basis - Bevestigings materialen'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '2499',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Cassette - Plastic - Aluminum'
                        ]
                    ],
                    'de' => [
                        'attributes' => [
                            'name' => 'Kassette - Kunststoff - Aluminium'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Casette - Plastic - Aluminium'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '1999',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Cassette - Plastic - White'
                        ]
                    ],
                    'de' => [
                        'attributes' => [
                            'name' => 'Kassette - Kunststoff - Weiß'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Cassette - Plastic - Wit'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '1999',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Cassette - Plastic - Beige'
                        ]
                    ],
                    'de' => [
                        'attributes' => [
                            'name' => 'Kassette - Kunststoff - Beige'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Cassette - Plastic - Beige'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '299',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Bottom bar - Plastic - Aluminium'
                        ]
                    ],
                    'de' => [
                        'attributes' => [
                            'name' => 'Bodenleiste - Kunststoff - Aluminium'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Onderste balk - Plastic - Aluminium'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '299',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Bottom bar - White - Aluminium'
                        ]
                    ],
                    'de' => [
                        'attributes' => [
                            'name' => 'Fußleiste - Weiß - Aluminium'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Onderste balk - Wit - Aluminium'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '299',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Bottom bar - Beige - Aluminium'
                        ]
                    ],
                    'de' => [
                        'attributes' => [
                            'name' => 'Fußleiste - Beige - Aluminium'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Onderste balk - Beige - Aluminium'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '299',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Control left - Chain'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Bediening links - Ketting'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '299',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Control right - Chain'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Bediening rechts - Ketting'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '794',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Fabric - White'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Stof - Wit'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '794',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Fabric - White'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Stof - Wit'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '794',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Fabric - Blackout dark'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Stof - Verduisterend zwart'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '794',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Fabric - Cream'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Stof - Crème'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '794',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Fabric - Beige'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Stof - Gebroken'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '794',
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Fabric - Grey'
                        ]
                    ],
                    'nl' => [
                        'attributes' => [
                            'name' => 'Stof - Grijs'
                        ]
                    ]
                ],
            ],
            [
                'attributes' => [
                    'active' => '1',
                    'price' => '100',
                    'vat_scenario_enum' => VatScenarioEnum::high_inc,
                    'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999),
                    'stock' => mt_rand(1, 100),
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Acme Food'
                        ]
                    ]
                ],
            ],
        ];

        $productGroupsData = [
            [
                'attributes' => [
                    'active' => '1'
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Roller blind basic set'
                        ]
                    ]
                ]
            ],
            [
                'attributes' => [
                    'active' => '1'
                ],
                'translations' => [
                    'en' => [
                        'attributes' => [
                            'name' => 'Cassette'
                        ]
                    ]
                ]
            ],
//            [
//                'attributes' => [
//                    'active' => '1'
//                ],
//                'translations' => [
//                    'en' => [
//                        'attributes' => [
//                            'name' => 'Control'
//                        ]
//                    ]
//                ]
//            ],
//            [
//                'attributes' => [
//                    'active' => '1'
//                ],
//                'translations' => [
//                    'en' => [
//                        'attributes' => [
//                            'name' => 'Bottom bar'
//                        ]
//                    ]
//                ]
//            ],
//            [
//                'attributes' => [
//                    'active' => '1'
//                ],
//                'translations' => [
//                    'en' => [
//                        'attributes' => [
//                            'name' => 'Fabric'
//                        ]
//                    ]
//                ]
//            ]
        ];


        $orBehaviour = ProductGroupBehaviour::where('name', 'or')->first();
        $andBehaviour = ProductGroupBehaviour::where('name', 'and')->first();
        $xorBehaviour = ProductGroupBehaviour::where('name', 'xor')->first();

        $products = [];
        foreach($productsData as $productData)
        {
            $products[] = $this->createProduct($productData);
        }

        $productGroups = [];
        foreach($productGroupsData as $productGroupData)
        {
            $productGroups[] = $this->createProductGroup($productGroupData, $andBehaviour, $products);
        }

        $productComposite = new ProductComposite(['active' => '1']);
        $productComposite->save();
        $translation = new ProductCompositeTranslation(['name' => 'Roller blind', 'slug' => Str::slug('Roller blind')]);
        $translation->language()->associate(Language::where('iso_2', '=', 'en')->first());
        $translation->translatable()->associate($productComposite);
        $translation->save();

        foreach($productGroups as $productGroup) {
            $productComposite->groups()->attach($productGroup);
        }
    }

    private function createProduct(array $productData): Product
    {
        $product = new Product($productData['attributes']);

        $product->save();

        foreach ($productData['translations'] as $iso_2 => $languageData) {
            $language = Language::where('iso_2', '=', $iso_2)->first();
            if (!$language) {
                break;
            }

            $translation = new ProductTranslation();
            $translation->language()->associate($language);
            $translation->translatable()->associate($product);
            $translation->fill(
                $languageData['attributes']
            );
            $translation->slug = Str::slug($translation->name);
            $translation->save();
        }

        return $product;
    }

    private function createProductGroup(array $productGroupData, ProductGroupBehaviour $productGroupBehaviour, array $products): ProductGroup
    {
        $productGroup = new ProductGroup($productGroupData['attributes']);
        $productGroup->productGroupBehaviour()->associate($productGroupBehaviour);
        $productGroup->save();

        foreach ($productGroupData['translations'] as $iso_2 => $languageData) {
            $language = Language::where('iso_2', '=', $iso_2)->first();
            if (!$language) {
                break;
            }
            $translation = new ProductGroupTranslation();
            $translation->language()->associate($language);
            $translation->translatable()->associate($productGroup);
            $translation->slug = Str::slug($translation->name);
            $translation->fill(
                $languageData['attributes']
            );
            $translation->save();
        }

        $randomProducts = Arr::random($products, mt_rand(1, 3));
        $productGroup->products()->saveMany($randomProducts);

        return $productGroup;
    }
}