existing skus working now too
CI / testsuite (mysql, 8.2, ) (push) Waiting to run Details
CI / testsuite (mysql, 8.4, ) (push) Waiting to run Details
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Waiting to run Details
CI / Coding Standard & Static Analysis (push) Waiting to run Details

This commit is contained in:
Brandon Shipley 2025-10-03 03:04:57 -07:00
parent 49912abd3a
commit 73349b736e
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
4 changed files with 203 additions and 25 deletions

View File

@ -82,7 +82,7 @@ class ProductSkusController extends AppController
$existingProductSkusForMapping = Hash::combine($product->product_skus ?? [], '{n}.id', '{n}.product_sku_variant_values'); $existingProductSkusForMapping = Hash::combine($product->product_skus ?? [], '{n}.id', '{n}.product_sku_variant_values');
$existingSkusForCartesianComparison = []; $existingSkusForCartesianComparison = [];
foreach ($existingProductSkusForMapping as $existingProductSkuId => $existingProductSku) { 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 : []; $productVariants = isset($product->product_variants) ? $product->product_variants : [];
// dd($productVariants); // dd($productVariants);
@ -97,7 +97,7 @@ class ProductSkusController extends AppController
foreach ($productCategoryVariants as $productCategoryVariant) { foreach ($productCategoryVariants as $productCategoryVariant) {
$options = Hash::extract($productCategoryVariant['product_category_variant_options'] ?? [], '{n}.id'); $options = Hash::extract($productCategoryVariant['product_category_variant_options'] ?? [], '{n}.id');
$toGetCartesianProductsFrom[$productCategoryVariant['id']] = $options; $toGetCartesianProductsFrom[$productVariantsMapping[$productCategoryVariant['id']]] = $options;
} }
// dd($toGetCartesianProductsFrom); // dd($toGetCartesianProductsFrom);
@ -109,6 +109,7 @@ class ProductSkusController extends AppController
'product', 'product',
'productSkus', 'productSkus',
'productCategoryVariants', 'productCategoryVariants',
'productVariantsMapping',
'toGetCartesianProductsFrom', 'toGetCartesianProductsFrom',
'optionMapping', 'optionMapping',
'variantNameMapping', 'variantNameMapping',

View File

@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Controller;
use App\Controller\AppController;
use Cake\Log\Log;
/**
* ProductVariants Controller
*
* @property \App\Model\Table\ProductVariantsTable $ProductVariants
*/
class ProductVariantsController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$query = $this->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']);
}
}

View File

@ -1,13 +1,12 @@
<?php <?php
use Cake\Log\Log;
use function BenTools\CartesianProduct\combinations; use function BenTools\CartesianProduct\combinations;
/** /**
* @var \App\View\AppView $this * @var \App\View\AppView $this
* @var \App\Model\Entity\ProductSku $productSku * @var \App\Model\Entity\ProductSku[] $productSkus
* @var \Cake\Collection\CollectionInterface|string[] $products * @var \App\Model\Entity\ProductSku $product
* @var array $optionMapping
* @var array $variantNameMapping
* @var array $toGetCartesianProductsFrom * @var array $toGetCartesianProductsFrom
*/ */
@ -17,6 +16,11 @@ use function BenTools\CartesianProduct\combinations;
<aside class="column"> <aside class="column">
<div class="side-nav"> <div class="side-nav">
<h4 class="heading"><?= __('Actions') ?></h4> <h4 class="heading"><?= __('Actions') ?></h4>
<?= $this->Html->link(__('View Product'), [
'controller' => 'Products',
'action' => 'view',
$product->id,
], ['class' => 'side-nav-item']) ?>
<?= $this->Html->link(__('List Product SKUs'), ['action' => 'index'], ['class' => 'side-nav-item']) ?> <?= $this->Html->link(__('List Product SKUs'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
</div> </div>
</aside> </aside>
@ -24,12 +28,11 @@ use function BenTools\CartesianProduct\combinations;
<div class="productSkus form content"> <div class="productSkus form content">
<?= $this->Form->create($productSkus) ?> <?= $this->Form->create($productSkus) ?>
<fieldset> <fieldset>
<legend><?= __('Add Product Skus') ?></legend> <legend><?= __('Add Product SKU(s) for Product: ' . $product->name) ?></legend>
<div id="product-skus-container" class="container"> <div id="product-skus-container" class="container">
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th>Add?</th>
<th>SKU</th> <th>SKU</th>
<th>Barcode</th> <th>Barcode</th>
<th>Price</th> <th>Price</th>
@ -45,28 +48,83 @@ use function BenTools\CartesianProduct\combinations;
<?php <?php
$cnt = 0; $cnt = 0;
$labelFalse = ['label' => false]; $labelFalse = ['label' => false];
$found = [];
foreach (combinations($toGetCartesianProductsFrom) as $c => $combination) : ?> foreach (combinations($toGetCartesianProductsFrom) as $c => $combination) : ?>
<?php
// $singleVariantId
$foundSku = null;
// dd($existingSkusForCartesianComparison);
foreach($existingSkusForCartesianComparison as $existingSkuForCartesianComparisonId => $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']);
// }
?>
<tr> <tr>
<td><?= $this->Form->control($cnt . '.add', ['label' => false, 'type' => 'checkbox', 'checked' => true]); ?></td> <td>
<td><?= $this->Form->control($cnt . '.sku', $labelFalse); ?></td> <?= $this->Form->hidden($cnt . '.id', ['value' => $foundSku]); ?>
<?= $this->Form->control($cnt . '.sku', $skuInputOptions); ?>
</td>
<td><?= $this->Form->control($cnt . '.barcode', $labelFalse); ?></td> <td><?= $this->Form->control($cnt . '.barcode', $labelFalse); ?></td>
<td><?= $this->Form->control($cnt . '.price', $labelFalse); ?></td> <td><?= $this->Form->control($cnt . '.price', $labelFalse); ?></td>
<td><?= $this->Form->control($cnt . '.cost', $labelFalse); ?></td> <td><?= $this->Form->control($cnt . '.cost', $labelFalse); ?></td>
<?php <?php
$variantCnt = 0; $variantCnt = 0;
foreach ($variantNameMapping as $singleVariantId => $singleVariantName) : ?> if (isset($foundSku) && isset($existingProductSkus[$foundSku])) : ?>
<?php foreach ($existingProductSkus[$foundSku]['product_sku_variant_values'] as $existingVariantValueRecord) : ?>
<td> <td>
<?= $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_id', ['value' => $singleVariantId ?? null]); ?> <?= $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.id', ['value' => $existingVariantValueRecord->id]); ?>
<?= $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_option_id', ['value' => $combination[$singleVariantId] ?? null]); ?> <?= $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_sku_id', ['value' => $foundSku]); ?>
<?= $optionMapping[$combination[$singleVariantId]]; ?> <?= $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_id', ['value' => $existingVariantValueRecord->product_category_variant_id ?? null]); ?>
<?= $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_option_id', ['value' => $existingVariantValueRecord->product_category_variant_option_id ?? null]); ?>
<?= $optionMapping[$combination[$existingVariantValueRecord->product_variant_id]]; ?>
</td> </td>
<?php <?php
$variantCnt++; $variantCnt++;
endforeach; ?> endforeach; ?>
<?php else : ?>
<?php foreach ($variantNameMapping as $singleVariantId => $singleVariantName) : ?>
<td>
<?= isset($foundSku) ? $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_sku_id', ['value' => $foundSku]) : ''; ?>
<?= $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_variant_id', ['value' => $productVariantsMapping[$singleVariantId] ?? null]); ?>
<?= $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_option_id', ['value' => $combination[$productVariantsMapping[$singleVariantId]] ?? null]); ?>
<?= $optionMapping[$combination[$productVariantsMapping[$singleVariantId]]]; ?>
</td>
<?php
$variantCnt++;
endforeach; ?>
<?php endif; ?>
</tr> </tr>
<?php <?php
$cnt++; $cnt++;
endforeach; ?>
endforeach;
// dd($found);
?>
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* @var \App\View\AppView $this * @var \App\View\AppView $this
* @var \Cake\Datasource\EntityInterface $productSku * @var \App\Model\Entity\ProductSkus $productSku
*/ */
?> ?>
<div class="row"> <div class="row">
@ -52,22 +52,22 @@
</tr> </tr>
</table> </table>
<div class="related"> <div class="related">
<h4><?= __('Product Attributes') ?></h4> <h4><?= __('SKU Variant Attributes') ?></h4>
<?php if (!empty($productSku->product_sku_variant_values)) : ?> <?php if (!empty($productSku->product_sku_variant_values)) : ?>
<div class="table-responsive"> <div class="table-responsive">
<table> <table>
<?php foreach ($productSku->product_sku_variant_values as $variantValue) : ?> <?php foreach ($productSku->product_sku_variant_values as $variantValue) : ?>
<?php <?php
if (!$variantValue->hasValue('product_category_variant')) { if (!isset($variantValue->product_variant->product_category_variant)) {
continue; continue;
} }
if (!$variantValue->hasValue('product_category_variant_option')) { if (!isset($variantValue->product_category_variant_option)) {
continue; continue;
} }
?> ?>
<tr> <tr>
<th><?= h($variantValue->product_category_variant->name) ?></th> <th><?= h($variantValue->product_variant->product_category_variant->name) ?></th>
<td><?= h($variantValue->product_category_variant_option->variant_label ?? $variantValue->product_category_variant_option->variant_value) ?></td> <td><?= h($variantValue->product_category_variant_option->variant_label ?? $variantValue->product_category_variant_option->variant_value) ?></td>
</tr> </tr>