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",
 | 
					        "dereuromark/cakephp-tools": "^3.9",
 | 
				
			||||||
        "muffin/trash": "^4.2",
 | 
					        "muffin/trash": "^4.2",
 | 
				
			||||||
        "hi-powered-dev/cheese-cake": "dev-prod",
 | 
					        "hi-powered-dev/cheese-cake": "dev-prod",
 | 
				
			||||||
        "cakephp/cakephp": "^5.0.1"
 | 
					        "cakephp/cakephp": "^5.0.1",
 | 
				
			||||||
 | 
					        "bentools/cartesian-product": "dev-master"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "require-dev": {
 | 
					    "require-dev": {
 | 
				
			||||||
        "phpunit/phpunit": "^10.1",
 | 
					        "phpunit/phpunit": "^10.1",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -150,21 +150,4 @@ class ProductCategoryVariantsController extends AppController
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $this->redirect(['action' => 'index']);
 | 
					        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;
 | 
					namespace CakeProducts\Controller;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use Cake\Log\Log;
 | 
					use Cake\Log\Log;
 | 
				
			||||||
 | 
					use Cake\Utility\Hash;
 | 
				
			||||||
use CakeProducts\Controller\AppController;
 | 
					use CakeProducts\Controller\AppController;
 | 
				
			||||||
use CheeseCake\Controller\Traits\OverrideTableTrait;
 | 
					use CheeseCake\Controller\Traits\OverrideTableTrait;
 | 
				
			||||||
 | 
					use function BenTools\CartesianProduct\combinations;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * ProductSkus Controller
 | 
					 * ProductSkus Controller
 | 
				
			||||||
| 
						 | 
					@ -58,30 +60,52 @@ class ProductSkusController extends AppController
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
 | 
					     * @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')) {
 | 
					        if ($this->request->is('post')) {
 | 
				
			||||||
            $postData = $this->request->getData();
 | 
					            $postData = $this->request->getData('skus', []);
 | 
				
			||||||
            $saveOptions = [
 | 
					            $saveOptions = [
 | 
				
			||||||
                'associated' => [],
 | 
					                '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($postData, true));
 | 
					//            Log::debug(print_r($postData, true));
 | 
				
			||||||
//            Log::debug(print_r('$saveOptions', true));
 | 
					//            Log::debug(print_r('$saveOptions', true));
 | 
				
			||||||
//            Log::debug(print_r($saveOptions, true));
 | 
					//            Log::debug(print_r($saveOptions, true));
 | 
				
			||||||
            $productSku = $this->ProductSkus->patchEntity($productSku, $postData, $saveOptions);
 | 
					            $productSkus = $table->patchEntities($productSkus, $postData, $saveOptions);
 | 
				
			||||||
            if ($this->ProductSkus->save($productSku)) {
 | 
					            if ($table->saveManyOrFail($productSkus)) {
 | 
				
			||||||
                $this->Flash->success(__('The product sku has been saved.'));
 | 
					                $this->Flash->success(__('The product SKU(s) have been saved.'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                return $this->redirect(['action' => 'index']);
 | 
					                return $this->redirect(['action' => 'index']);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            Log::debug(print_r('$productSku->getErrors() next - failed in productSkus/add', true));
 | 
					            $this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.'));
 | 
				
			||||||
            Log::debug(print_r($productSku->getErrors(), true));
 | 
					 | 
				
			||||||
            $this->Flash->error(__('The product skus could not be saved. Please, try again.'));
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        $products = $this->ProductSkus->Products->find('list', limit: 200)->all();
 | 
					        $this->set(compact(
 | 
				
			||||||
        $this->set(compact('productSku', 'products'));
 | 
					            'productSkus',
 | 
				
			||||||
 | 
					            'productCategoryVariants',
 | 
				
			||||||
 | 
					            'toGetCartesianProductsFrom',
 | 
				
			||||||
 | 
					            'optionMapping',
 | 
				
			||||||
 | 
					            'variantNameMapping',
 | 
				
			||||||
 | 
					            'numSkusToAdd'
 | 
				
			||||||
 | 
					        ));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,22 +1,67 @@
 | 
				
			||||||
<?php
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use function BenTools\CartesianProduct\combinations;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @var \App\View\AppView $this
 | 
					 * @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">
 | 
					<div class="row">
 | 
				
			||||||
    <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(__('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>
 | 
				
			||||||
    <div class="column column-80">
 | 
					    <div class="column column-80">
 | 
				
			||||||
        <div class="productSkus form content">
 | 
					        <div class="productSkus form content">
 | 
				
			||||||
            <?= $this->Form->create($productSku) ?>
 | 
					            <?= $this->Form->create($productSkus) ?>
 | 
				
			||||||
            <fieldset>
 | 
					            <fieldset>
 | 
				
			||||||
                <legend><?= __('Add Product Sku') ?></legend>
 | 
					                <legend><?= __('Add Product Skus') ?></legend>
 | 
				
			||||||
                <?= $this->element('ProductSkus/form'); ?>
 | 
					                <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>
 | 
					            </fieldset>
 | 
				
			||||||
            <?= $this->Form->button(__('Submit')) ?>
 | 
					            <?= $this->Form->button(__('Submit')) ?>
 | 
				
			||||||
            <?= $this->Form->end() ?>
 | 
					            <?= $this->Form->end() ?>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue