2025-04-15 02:39:35 -07:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace CakeProducts\Controller;
|
|
|
|
|
|
|
|
|
|
use Cake\Log\Log;
|
2025-09-05 02:51:43 -07:00
|
|
|
use Cake\Utility\Hash;
|
2025-04-15 02:39:35 -07:00
|
|
|
use CheeseCake\Controller\Traits\OverrideTableTrait;
|
2025-09-05 02:51:43 -07:00
|
|
|
use function BenTools\CartesianProduct\combinations;
|
2025-04-15 02:39:35 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ProductSkus Controller
|
|
|
|
|
*
|
|
|
|
|
* @property \CakeProducts\Model\Table\ProductSkusTable $ProductSkus
|
|
|
|
|
*/
|
|
|
|
|
class ProductSkusController extends AppController
|
|
|
|
|
{
|
|
|
|
|
use OverrideTableTrait;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function initialize(): void
|
|
|
|
|
{
|
|
|
|
|
parent::initialize(); // TODO: Change the autogenerated stub
|
|
|
|
|
// $this->_defaultTable = 'CakeProducts.ProductSkus';
|
|
|
|
|
// $this->_tableConfigKey = 'CakeProducts.ProductSkus.table';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Index method
|
|
|
|
|
*
|
|
|
|
|
* @return \Cake\Http\Response|null|void Renders view
|
|
|
|
|
*/
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
$query = $this->ProductSkus->find()
|
|
|
|
|
->contain(['Products']);
|
|
|
|
|
$productSkus = $this->paginate($query);
|
|
|
|
|
|
|
|
|
|
$this->set(compact('productSkus'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* View method
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $id Product Skus id.
|
|
|
|
|
* @return \Cake\Http\Response|null|void Renders view
|
|
|
|
|
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
|
|
|
|
|
*/
|
|
|
|
|
public function view($id = null)
|
|
|
|
|
{
|
2025-09-10 22:06:12 -07:00
|
|
|
$productSku = $this->ProductSkus->get($id, contain: [
|
|
|
|
|
'Products',
|
|
|
|
|
'ProductSkuVariantValues',
|
2025-10-03 02:16:24 -07:00
|
|
|
'ProductSkuVariantValues.ProductVariants',
|
|
|
|
|
'ProductSkuVariantValues.ProductVariants.ProductCategoryVariants',
|
2025-09-10 22:06:12 -07:00
|
|
|
'ProductSkuVariantValues.ProductCategoryVariantOptions',
|
|
|
|
|
]);
|
2025-04-15 02:39:35 -07:00
|
|
|
$this->set(compact('productSku'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add method
|
|
|
|
|
*
|
|
|
|
|
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
|
|
|
|
|
*/
|
2025-09-05 02:51:43 -07:00
|
|
|
public function add($productId = null)
|
2025-04-15 02:39:35 -07:00
|
|
|
{
|
2025-09-05 02:51:43 -07:00
|
|
|
$toGetCartesianProductsFrom = [];
|
|
|
|
|
$productSkus = [];
|
|
|
|
|
$table = $this->getTable();
|
|
|
|
|
|
2025-10-03 02:16:24 -07:00
|
|
|
$product = $table->Products->get($productId, contain: [
|
|
|
|
|
'ProductSkus',
|
|
|
|
|
'ProductSkus.ProductSkuVariantValues',
|
|
|
|
|
'ProductVariants',
|
|
|
|
|
'ProductVariants.ProductCategoryVariants',
|
|
|
|
|
'ProductVariants.ProductCategoryVariants.ProductCategoryVariantOptions',
|
|
|
|
|
]);
|
|
|
|
|
$existingProductSkus = Hash::combine($product->product_skus ?? [], '{n}.id', '{n}');
|
|
|
|
|
$existingProductSkusForMapping = Hash::combine($product->product_skus ?? [], '{n}.id', '{n}.product_sku_variant_values');
|
|
|
|
|
$existingSkusForCartesianComparison = [];
|
|
|
|
|
foreach ($existingProductSkusForMapping as $existingProductSkuId => $existingProductSku) {
|
2025-10-03 03:04:57 -07:00
|
|
|
$existingSkusForCartesianComparison[$existingProductSkuId] = Hash::combine($existingProductSku, '{n}.product_variant_id', '{n}.product_category_variant_option_id');
|
2025-10-03 02:16:24 -07:00
|
|
|
}
|
|
|
|
|
$productVariants = isset($product->product_variants) ? $product->product_variants : [];
|
|
|
|
|
// dd($productVariants);
|
|
|
|
|
$productVariantsMapping = Hash::combine($productVariants, '{n}.product_category_variant.id', '{n}.id');
|
|
|
|
|
$productCategoryVariants = Hash::extract($productVariants, '{n}.product_category_variant');
|
|
|
|
|
// dd($productCategoryVariants);
|
2025-09-05 02:51:43 -07:00
|
|
|
$optionMapping = Hash::combine($productCategoryVariants, '{n}.product_category_variant_options.{n}.id', '{n}.product_category_variant_options.{n}.variant_value');
|
2025-10-03 02:16:24 -07:00
|
|
|
// dd($optionMapping);
|
|
|
|
|
|
2025-09-05 02:51:43 -07:00
|
|
|
$variantNameMapping = Hash::combine($productCategoryVariants, '{n}.id', '{n}.name');
|
2025-10-03 02:16:24 -07:00
|
|
|
// dd($variantNameMapping);
|
|
|
|
|
|
2025-09-05 02:51:43 -07:00
|
|
|
foreach ($productCategoryVariants as $productCategoryVariant) {
|
2025-10-03 02:16:24 -07:00
|
|
|
$options = Hash::extract($productCategoryVariant['product_category_variant_options'] ?? [], '{n}.id');
|
2025-10-03 03:04:57 -07:00
|
|
|
$toGetCartesianProductsFrom[$productVariantsMapping[$productCategoryVariant['id']]] = $options;
|
2025-09-05 02:51:43 -07:00
|
|
|
}
|
2025-10-03 02:16:24 -07:00
|
|
|
// dd($toGetCartesianProductsFrom);
|
|
|
|
|
|
2025-09-05 02:51:43 -07:00
|
|
|
$numSkusToAdd = count(combinations($toGetCartesianProductsFrom));
|
|
|
|
|
for ($i = 0; $i < $numSkusToAdd; $i++) {
|
|
|
|
|
$productSkus[$i] = $this->getTable()->newEmptyEntity();
|
|
|
|
|
}
|
2025-09-06 01:11:51 -07:00
|
|
|
$this->set(compact(
|
2025-10-03 02:16:24 -07:00
|
|
|
'product',
|
2025-09-06 01:11:51 -07:00
|
|
|
'productSkus',
|
|
|
|
|
'productCategoryVariants',
|
2025-10-03 03:04:57 -07:00
|
|
|
'productVariantsMapping',
|
2025-09-06 01:11:51 -07:00
|
|
|
'toGetCartesianProductsFrom',
|
|
|
|
|
'optionMapping',
|
|
|
|
|
'variantNameMapping',
|
2025-10-03 02:16:24 -07:00
|
|
|
'numSkusToAdd',
|
|
|
|
|
'existingProductSkus',
|
|
|
|
|
'existingSkusForCartesianComparison'
|
2025-09-06 01:11:51 -07:00
|
|
|
));
|
|
|
|
|
|
2025-04-15 02:39:35 -07:00
|
|
|
if ($this->request->is('post')) {
|
2025-09-06 01:11:51 -07:00
|
|
|
$postedSkus = $this->request->getData();
|
2025-04-15 02:39:35 -07:00
|
|
|
$saveOptions = [
|
2025-09-10 22:58:37 -07:00
|
|
|
'fields' => [
|
|
|
|
|
'product_id',
|
|
|
|
|
'sku',
|
|
|
|
|
'barcode',
|
|
|
|
|
'price',
|
|
|
|
|
'cost',
|
|
|
|
|
'product_sku_variant_values',
|
|
|
|
|
'created',
|
|
|
|
|
'modified',
|
|
|
|
|
'enabled',
|
2025-10-06 23:49:48 -07:00
|
|
|
'default_sku',
|
2025-09-10 22:58:37 -07:00
|
|
|
],
|
2025-09-10 22:06:12 -07:00
|
|
|
'associated' => [
|
2025-09-10 22:58:37 -07:00
|
|
|
'ProductSkuVariantValues' => [
|
|
|
|
|
'fields' => [
|
2025-10-03 02:16:24 -07:00
|
|
|
'product_variant_id',
|
2025-09-10 22:58:37 -07:00
|
|
|
'product_category_variant_option_id',
|
|
|
|
|
],
|
|
|
|
|
],
|
2025-09-10 22:06:12 -07:00
|
|
|
],
|
2025-04-15 02:39:35 -07:00
|
|
|
];
|
2025-09-10 22:06:12 -07:00
|
|
|
$finalPostData = [];
|
2025-09-06 01:11:51 -07:00
|
|
|
$postedSkus = Hash::insert($postedSkus, '{n}.product_id', $productId);
|
|
|
|
|
|
|
|
|
|
foreach ($postedSkus as $postedSkuCnt => $postedSku) {
|
2025-10-03 02:16:24 -07:00
|
|
|
if (!isset($postedSku['sku']) || !$postedSku['sku']) {
|
2025-09-06 01:11:51 -07:00
|
|
|
unset($productSkus[$postedSkuCnt]);
|
2025-09-10 22:06:12 -07:00
|
|
|
|
|
|
|
|
continue;
|
2025-09-06 01:11:51 -07:00
|
|
|
}
|
2025-09-10 22:06:12 -07:00
|
|
|
$finalPostData[$postedSkuCnt] = $postedSku;
|
2025-09-06 01:11:51 -07:00
|
|
|
}
|
|
|
|
|
if (!$productSkus || !$postedSkus) {
|
|
|
|
|
$this->Flash->error('Nothing to save! Add at least one SKU next time.');
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-10-03 02:16:24 -07:00
|
|
|
// dd($finalPostData);
|
2025-09-10 22:06:12 -07:00
|
|
|
$productSkus = $table->patchEntities($productSkus, $finalPostData, $saveOptions);
|
|
|
|
|
$errors = [];
|
|
|
|
|
$successes = [];
|
|
|
|
|
foreach ($productSkus as $productSkuToSave) {
|
2025-09-10 22:58:37 -07:00
|
|
|
// dd($productSkuToSave);
|
|
|
|
|
|
2025-09-10 22:06:12 -07:00
|
|
|
if (!$table->save($productSkuToSave, $saveOptions)) {
|
|
|
|
|
Log::debug(print_r('$productSkuToSave->getErrors()', true));
|
|
|
|
|
Log::debug(print_r($productSkuToSave->getErrors(), true));
|
2025-10-03 02:16:24 -07:00
|
|
|
dd($productSkuToSave->getErrors());
|
2025-09-10 22:06:12 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$successes[] = $productSkuToSave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($successes) {
|
|
|
|
|
$this->Flash->success(__(count($successes) . ' New SKUs have been saved.'));
|
2025-04-15 02:39:35 -07:00
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
|
}
|
2025-09-06 01:11:51 -07:00
|
|
|
|
2025-09-05 02:51:43 -07:00
|
|
|
$this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.'));
|
2025-04-15 02:39:35 -07:00
|
|
|
}
|
2025-09-05 02:51:43 -07:00
|
|
|
$this->set(compact(
|
2025-09-06 01:11:51 -07:00
|
|
|
'productSkus'
|
2025-09-05 02:51:43 -07:00
|
|
|
));
|
2025-04-15 02:39:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Edit method
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $id Product Skus 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)
|
|
|
|
|
{
|
|
|
|
|
$productSku = $this->ProductSkus->get($id, contain: []);
|
|
|
|
|
if ($this->request->is(['patch', 'post', 'put'])) {
|
|
|
|
|
$postData = $this->request->getData();
|
|
|
|
|
$saveOptions = [
|
|
|
|
|
'associated' => [],
|
|
|
|
|
];
|
|
|
|
|
// 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 skus has been saved.'));
|
|
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
|
}
|
|
|
|
|
Log::debug(print_r('$productSku->getErrors() next - failed in productSkus/edit', true));
|
|
|
|
|
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('productSku', 'products'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete method
|
|
|
|
|
*
|
|
|
|
|
* @param string|null $id Product Skus 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']);
|
|
|
|
|
$productSku = $this->ProductSkus->get($id);
|
|
|
|
|
if ($this->ProductSkus->delete($productSku)) {
|
|
|
|
|
$this->Flash->success(__('The product skus has been deleted.'));
|
|
|
|
|
} else {
|
|
|
|
|
$this->Flash->error(__('The product skus could not be deleted. Please, try again.'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->redirect(['action' => 'index']);
|
|
|
|
|
}
|
|
|
|
|
}
|