require cartesian product library, product skus add update
This commit is contained in:
parent
9d9c32f0b9
commit
43a7805b5e
|
@ -9,7 +9,8 @@
|
|||
"dereuromark/cakephp-tools": "^3.9",
|
||||
"muffin/trash": "^4.2",
|
||||
"hi-powered-dev/cheese-cake": "dev-prod",
|
||||
"cakephp/cakephp": "^5.0.1"
|
||||
"cakephp/cakephp": "^5.0.1",
|
||||
"bentools/cartesian-product": "dev-master"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^10.1",
|
||||
|
|
|
@ -150,21 +150,4 @@ class ProductCategoryVariantsController extends AppController
|
|||
|
||||
return $this->redirect(['action' => 'index']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the users table instance
|
||||
*
|
||||
* @return Table
|
||||
*/
|
||||
public function getTable()
|
||||
{
|
||||
if ($this->_table instanceof Table) {
|
||||
return $this->_table;
|
||||
}
|
||||
$this->_table = TableRegistry::getTableLocator()->get(
|
||||
Configure::read('CakeProducts.ProductCategoryVariants.table', 'CakeProducts.ProductCategoryVariants')
|
||||
);
|
||||
|
||||
return $this->_table;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@ declare(strict_types=1);
|
|||
namespace CakeProducts\Controller;
|
||||
|
||||
use Cake\Log\Log;
|
||||
use Cake\Utility\Hash;
|
||||
use CakeProducts\Controller\AppController;
|
||||
use CheeseCake\Controller\Traits\OverrideTableTrait;
|
||||
use function BenTools\CartesianProduct\combinations;
|
||||
|
||||
/**
|
||||
* ProductSkus Controller
|
||||
|
@ -58,30 +60,52 @@ class ProductSkusController extends AppController
|
|||
*
|
||||
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
|
||||
*/
|
||||
public function add()
|
||||
public function add($productId = null)
|
||||
{
|
||||
$productSku = $this->ProductSkus->newEmptyEntity();
|
||||
$toGetCartesianProductsFrom = [];
|
||||
$productSkus = [];
|
||||
$table = $this->getTable();
|
||||
|
||||
$productCategoryVariants = $table->Products->ProductCategoryVariants->find()
|
||||
->contain(['ProductCategoryVariantOptions'])
|
||||
->where(['product_id' => $productId])
|
||||
->toArray();
|
||||
$optionMapping = Hash::combine($productCategoryVariants, '{n}.product_category_variant_options.{n}.id', '{n}.product_category_variant_options.{n}.variant_value');
|
||||
$variantNameMapping = Hash::combine($productCategoryVariants, '{n}.id', '{n}.name');
|
||||
foreach ($productCategoryVariants as $productCategoryVariant) {
|
||||
$options = Hash::extract($productCategoryVariant->product_category_variant_options ?? [], '{n}.id');
|
||||
$toGetCartesianProductsFrom[$productCategoryVariant->id] = $options;
|
||||
}
|
||||
$numSkusToAdd = count(combinations($toGetCartesianProductsFrom));
|
||||
for ($i = 0; $i < $numSkusToAdd; $i++) {
|
||||
$productSkus[$i] = $this->getTable()->newEmptyEntity();
|
||||
}
|
||||
if ($this->request->is('post')) {
|
||||
$postData = $this->request->getData();
|
||||
$postData = $this->request->getData('skus', []);
|
||||
$saveOptions = [
|
||||
'associated' => [],
|
||||
];
|
||||
$postData = Hash::insert($postData, '{n}.product_id', $productId);
|
||||
// Log::debug(print_r('$postData', true));
|
||||
// Log::debug(print_r($postData, true));
|
||||
// Log::debug(print_r('$saveOptions', true));
|
||||
// Log::debug(print_r($saveOptions, true));
|
||||
$productSku = $this->ProductSkus->patchEntity($productSku, $postData, $saveOptions);
|
||||
if ($this->ProductSkus->save($productSku)) {
|
||||
$this->Flash->success(__('The product sku has been saved.'));
|
||||
$productSkus = $table->patchEntities($productSkus, $postData, $saveOptions);
|
||||
if ($table->saveManyOrFail($productSkus)) {
|
||||
$this->Flash->success(__('The product SKU(s) have been saved.'));
|
||||
|
||||
return $this->redirect(['action' => 'index']);
|
||||
}
|
||||
Log::debug(print_r('$productSku->getErrors() next - failed in productSkus/add', true));
|
||||
Log::debug(print_r($productSku->getErrors(), true));
|
||||
$this->Flash->error(__('The product skus could not be saved. Please, try again.'));
|
||||
$this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.'));
|
||||
}
|
||||
$products = $this->ProductSkus->Products->find('list', limit: 200)->all();
|
||||
$this->set(compact('productSku', 'products'));
|
||||
$this->set(compact(
|
||||
'productSkus',
|
||||
'productCategoryVariants',
|
||||
'toGetCartesianProductsFrom',
|
||||
'optionMapping',
|
||||
'variantNameMapping',
|
||||
'numSkusToAdd'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,22 +1,67 @@
|
|||
<?php
|
||||
|
||||
use function BenTools\CartesianProduct\combinations;
|
||||
|
||||
/**
|
||||
* @var \App\View\AppView $this
|
||||
* @var \Cake\Datasource\EntityInterface $productSku
|
||||
* @var \App\Model\Entity\ProductSku $productSku
|
||||
* @var \Cake\Collection\CollectionInterface|string[] $products
|
||||
*/
|
||||
|
||||
|
||||
?>
|
||||
<div class="row">
|
||||
<aside class="column">
|
||||
<div class="side-nav">
|
||||
<h4 class="heading"><?= __('Actions') ?></h4>
|
||||
<?= $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>
|
||||
</aside>
|
||||
<div class="column column-80">
|
||||
<div class="productSkus form content">
|
||||
<?= $this->Form->create($productSku) ?>
|
||||
<?= $this->Form->create($productSkus) ?>
|
||||
<fieldset>
|
||||
<legend><?= __('Add Product Sku') ?></legend>
|
||||
<?= $this->element('ProductSkus/form'); ?>
|
||||
<legend><?= __('Add Product Skus') ?></legend>
|
||||
<div id="product-skus-container" class="container">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>SKU</th>
|
||||
<th>Barcode</th>
|
||||
<th>Price</th>
|
||||
<th>Cost</th>
|
||||
<?php
|
||||
$cnt = 0;
|
||||
foreach ($variantNameMapping as $singleVariantName) : ?>
|
||||
<th><?= $singleVariantName; ?></th>
|
||||
<?php endforeach; ?>
|
||||
<th>Add?</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$cnt = 0;
|
||||
$labelFalse = ['label' => false];
|
||||
foreach (combinations($toGetCartesianProductsFrom) as $c => $combination) : ?>
|
||||
<tr>
|
||||
<td><?= $this->Form->control('skus.' . $cnt . '.sku', $labelFalse); ?></td>
|
||||
<td><?= $this->Form->control('skus.' . $cnt . '.barcode', $labelFalse); ?></td>
|
||||
<td><?= $this->Form->control('skus.' . $cnt . '.price', $labelFalse); ?></td>
|
||||
<td><?= $this->Form->control('skus.' . $cnt . '.cost', $labelFalse); ?></td>
|
||||
<?php foreach ($variantNameMapping as $singleVariantId => $singleVariantName) : ?>
|
||||
<td>
|
||||
<?= $this->Form->hidden('skus.' . $cnt . '.variants.' . $singleVariantId, ['value' => $combination[$singleVariantId] ?? null]); ?>
|
||||
<?= $optionMapping[$combination[$singleVariantId]]; ?>
|
||||
</td>
|
||||
<?php endforeach; ?>
|
||||
<td><?= $this->Form->control('add', ['label' => false, 'type' => 'checkbox', 'checked' => true]); ?></td>
|
||||
</tr>
|
||||
<?php
|
||||
$cnt++;
|
||||
endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</fieldset>
|
||||
<?= $this->Form->button(__('Submit')) ?>
|
||||
<?= $this->Form->end() ?>
|
||||
|
|
Loading…
Reference in New Issue