diff --git a/src/Controller/ProductSkusController.php b/src/Controller/ProductSkusController.php index 2f17ce0..e862198 100644 --- a/src/Controller/ProductSkusController.php +++ b/src/Controller/ProductSkusController.php @@ -82,7 +82,7 @@ class ProductSkusController extends AppController $existingProductSkusForMapping = Hash::combine($product->product_skus ?? [], '{n}.id', '{n}.product_sku_variant_values'); $existingSkusForCartesianComparison = []; foreach ($existingProductSkusForMapping as $existingProductSkuId => $existingProductSku) { - $existingSkusForCartesianComparison[$existingProductSkuId] = Hash::combine($existingProductSku, '{n}.product_category_variant_id', '{n}.product_category_variant_option_id'); + $existingSkusForCartesianComparison[$existingProductSkuId] = Hash::combine($existingProductSku, '{n}.product_variant_id', '{n}.product_category_variant_option_id'); } $productVariants = isset($product->product_variants) ? $product->product_variants : []; // dd($productVariants); @@ -97,7 +97,7 @@ class ProductSkusController extends AppController foreach ($productCategoryVariants as $productCategoryVariant) { $options = Hash::extract($productCategoryVariant['product_category_variant_options'] ?? [], '{n}.id'); - $toGetCartesianProductsFrom[$productCategoryVariant['id']] = $options; + $toGetCartesianProductsFrom[$productVariantsMapping[$productCategoryVariant['id']]] = $options; } // dd($toGetCartesianProductsFrom); @@ -109,6 +109,7 @@ class ProductSkusController extends AppController 'product', 'productSkus', 'productCategoryVariants', + 'productVariantsMapping', 'toGetCartesianProductsFrom', 'optionMapping', 'variantNameMapping', diff --git a/src/Controller/ProductVariantsController.php b/src/Controller/ProductVariantsController.php new file mode 100644 index 0000000..233864b --- /dev/null +++ b/src/Controller/ProductVariantsController.php @@ -0,0 +1,119 @@ +ProductVariants->find() + ->contain(['ProductCategoryVariants', 'Products']); + $productVariants = $this->paginate($query); + + $this->set(compact('productVariants')); + } + + /** + * View method + * + * @param string|null $id Product Variant id. + * @return \Cake\Http\Response|null|void Renders view + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function view($id = null) + { + $productVariant = $this->ProductVariants->get($id, contain: ['ProductCategoryVariants', 'Products']); + $this->set(compact('productVariant')); + } + + /** + * Add method + * + * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise. + */ + public function add($productId) + { + $product = $this->ProductVariants->Products->get($productId); + $productVariant = $this->ProductVariants->newEmptyEntity(); + if ($this->request->is('post')) { + $saveOptions = []; + $postData = $this->request->getData(); + $productCategoryVariant = $this->ProductVariants->ProductCategoryVariants->get($this->request->getData('product_category_variant_id', '-1')); + $postData['name'] = $productCategoryVariant->name; + $postData['product_id'] = $productId; + $productVariant = $this->ProductVariants->patchEntity($productVariant, $postData); + if ($this->ProductVariants->save($productVariant)) { + $this->Flash->success(__('The product variant has been saved.')); + + return $this->redirect(['action' => 'index']); + } + + Log::debug(print_r('$productVariant->getErrors()', true)); + Log::debug(print_r($productVariant->getErrors(), true)); + $this->Flash->error(__('The product variant could not be saved. Please, try again.')); + } + $productCategoryVariants = $this->ProductVariants->ProductCategoryVariants + ->find('list', limit: 200) + ->where(['product_category_id' => $product->product_category_id]) + ->toArray(); + $this->set(compact('productVariant', 'productCategoryVariants', 'product')); + } + + /** + * Edit method + * + * @param string|null $id Product Variant id. + * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise. + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function edit($id = null) + { + $productVariant = $this->ProductVariants->get($id, contain: []); + if ($this->request->is(['patch', 'post', 'put'])) { + $productVariant = $this->ProductVariants->patchEntity($productVariant, $this->request->getData()); + if ($this->ProductVariants->save($productVariant)) { + $this->Flash->success(__('The product variant has been saved.')); + + return $this->redirect(['action' => 'index']); + } + $this->Flash->error(__('The product variant could not be saved. Please, try again.')); + } + $productCategoryVariants = $this->ProductVariants->ProductCategoryVariants->find('list', limit: 200)->all(); + $products = $this->ProductVariants->Products->find('list', limit: 200)->all(); + $this->set(compact('productVariant', 'productCategoryVariants', 'products')); + } + + /** + * Delete method + * + * @param string|null $id Product Variant id. + * @return \Cake\Http\Response|null Redirects to index. + * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. + */ + public function delete($id = null) + { + $this->request->allowMethod(['post', 'delete']); + $productVariant = $this->ProductVariants->get($id); + if ($this->ProductVariants->delete($productVariant)) { + $this->Flash->success(__('The product variant has been deleted.')); + } else { + $this->Flash->error(__('The product variant could not be deleted. Please, try again.')); + } + + return $this->redirect(['action' => 'index']); + } +} diff --git a/templates/ProductSkus/add.php b/templates/ProductSkus/add.php index 994aefd..9d0ef10 100644 --- a/templates/ProductSkus/add.php +++ b/templates/ProductSkus/add.php @@ -1,13 +1,12 @@

+ Html->link(__('View Product'), [ + 'controller' => 'Products', + 'action' => 'view', + $product->id, + ], ['class' => 'side-nav-item']) ?> Html->link(__('List Product SKUs'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
@@ -24,12 +28,11 @@ use function BenTools\CartesianProduct\combinations;
Form->create($productSkus) ?>
- + name) ?>
- @@ -45,28 +48,83 @@ use function BenTools\CartesianProduct\combinations; false]; + $found = []; foreach (combinations($toGetCartesianProductsFrom) as $c => $combination) : ?> + + $existingSkuForCartesianComparison) { + $allMatch = false; + Log::debug(print_r(['$combination' => $combination, '$existingSkuForCartesianComparison' => $existingSkuForCartesianComparison], true)); + if ($existingSkuForCartesianComparison == $combination) { + $foundSku = $existingSkuForCartesianComparisonId; + } + } + $found[$c] = $foundSku; + + $addInputOptions = [ + 'label' => false, + 'type' => 'checkbox', + 'checked' => true, + 'readonly' => isset($foundSku), + ]; + + $skuInputOptions = [ + 'label' => false, + 'required' => isset($foundSku), + 'value' => $foundSku && isset($existingProductSkus[$foundSku]['sku']) ? $existingProductSkus[$foundSku]['sku'] : '', + ]; + +// if ($foundSku) { +// dd($existingProductSkus[$foundSku]['sku']); +// } + ?> - - + $singleVariantName) : ?> - - + if (isset($foundSku) && isset($existingProductSkus[$foundSku])) : ?> + + + + + $singleVariantName) : ?> + + + + + endforeach; + + // dd($found); + ?>
Add? SKU Barcode Price
Form->control($cnt . '.add', ['label' => false, 'type' => 'checkbox', 'checked' => true]); ?>Form->control($cnt . '.sku', $labelFalse); ?> + Form->hidden($cnt . '.id', ['value' => $foundSku]); ?> + Form->control($cnt . '.sku', $skuInputOptions); ?> + + Form->control($cnt . '.barcode', $labelFalse); ?> Form->control($cnt . '.price', $labelFalse); ?> Form->control($cnt . '.cost', $labelFalse); ?> - Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_id', ['value' => $singleVariantId ?? null]); ?> - Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_option_id', ['value' => $combination[$singleVariantId] ?? null]); ?> - - + Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.id', ['value' => $existingVariantValueRecord->id]); ?> + Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_sku_id', ['value' => $foundSku]); ?> + Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_id', ['value' => $existingVariantValueRecord->product_category_variant_id ?? null]); ?> + Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_option_id', ['value' => $existingVariantValueRecord->product_category_variant_option_id ?? null]); ?> + product_variant_id]]; ?> + + Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_sku_id', ['value' => $foundSku]) : ''; ?> + Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_variant_id', ['value' => $productVariantsMapping[$singleVariantId] ?? null]); ?> + Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_option_id', ['value' => $combination[$productVariantsMapping[$singleVariantId]] ?? null]); ?> + +
diff --git a/templates/ProductSkus/view.php b/templates/ProductSkus/view.php index 4282e09..aae8b32 100644 --- a/templates/ProductSkus/view.php +++ b/templates/ProductSkus/view.php @@ -1,7 +1,7 @@
@@ -52,22 +52,22 @@