File: D:/HostingSpaces/brameda/brameda.nl/app/Komma/Shop/seeds/SampleProducts.php
<?php
/**
* Created by PhpStorm.
* User: julesgraus
* Date: 02/02/2018
* Time: 11:03
*/
namespace App\Komma\Shop\seeds;
use App\Komma\Globalization\Languages\Models\Language;
use App\Komma\Shop\Products\Product\Product;
use App\Komma\Shop\Products\Product\ProductTranslation;
use App\Komma\Shop\Products\ProductComposite\ProductComposite;
use App\Komma\Shop\Products\ProductComposite\ProductCompositeTranslation;
use App\Komma\Shop\Products\ProductGroup\ProductGroup;
use App\Komma\Shop\Products\ProductGroup\ProductGroupTranslation;
use App\Komma\Shop\Products\ProductGroupBehaviour\ProductGroupBehaviour;
use App\Komma\Sites\Models\Site;
use Illuminate\Database\Seeder;
class SampleProducts extends Seeder
{
public function run()
{
// for($i = 0; $i < 50; $i++) {
// $model = factory(Product::class);
// $model->create();
// }
$productsData = [
[
'attributes' => [
'active' => '1',
'Price' => '799',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Basic - Attachment materials'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '2499',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Cassette - Plastic - Aluminum'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '1999',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Cassette - Plastic - White'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '1999',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Cassette - Plastic - Beige'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '299',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Bottom bar - Plastic - Aluminium'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '299',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Bottom bar - White - Aluminium'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '299',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Bottom bar - Beige - Aluminium'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '299',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Control left - Chain'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '299',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Control right - Chain'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - White'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - White'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - Blackout dark'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - Cream'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - Beige'
]
]
],
],
[
'attributes' => [
'active' => '1',
'Price' => '794',
'stock_keeping_unit' => 'EAN' . mt_rand(1000000, 9999999)
],
'translations' => [
'en' => [
'attributes' => [
'name' => 'Fabric - Grey'
]
]
],
],
];
$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);
}
$productComposite = new ProductComposite(['active' => '1']);
$productComposite->save();
$translation = new ProductCompositeTranslation(['name' => '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->save();
}
return $product;
}
private function createProductGroup(array $productGroupData, ProductGroupBehaviour $productGroupBehaviour): 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->fill(
$languageData['attributes']
);
$translation->save();
}
return $productGroup;
}
}