composer update + load more dev dependencies for code quality, checkstyle fix
This commit is contained in:
@@ -12,240 +12,235 @@ use function BenTools\CartesianProduct\combinations;
|
||||
*
|
||||
* @property \CakeProducts\Model\Table\ProductSkusTable $ProductSkus
|
||||
*/
|
||||
class ProductSkusController extends AppController
|
||||
{
|
||||
/**
|
||||
class ProductSkusController extends AppController {
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function initialize(): void
|
||||
{
|
||||
parent::initialize(); // TODO: Change the autogenerated stub
|
||||
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);
|
||||
public function index() {
|
||||
$query = $this->ProductSkus->find()
|
||||
->contain(['Products']);
|
||||
$productSkus = $this->paginate($query);
|
||||
|
||||
$this->set(compact('productSkus'));
|
||||
}
|
||||
$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.
|
||||
* @return \Cake\Http\Response|null|void Renders view
|
||||
*/
|
||||
public function view($id = null)
|
||||
{
|
||||
$productSku = $this->ProductSkus->get($id, contain: [
|
||||
'Products',
|
||||
'ProductSkuVariantValues',
|
||||
'ProductSkuVariantValues.ProductVariants',
|
||||
'ProductSkuVariantValues.ProductVariants.ProductCategoryVariants',
|
||||
'ProductSkuVariantValues.ProductCategoryVariantOptions',
|
||||
]);
|
||||
$this->set(compact('productSku'));
|
||||
}
|
||||
public function view($id = null) {
|
||||
$productSku = $this->ProductSkus->get($id, contain: [
|
||||
'Products',
|
||||
'ProductSkuVariantValues',
|
||||
'ProductSkuVariantValues.ProductVariants',
|
||||
'ProductSkuVariantValues.ProductVariants.ProductCategoryVariants',
|
||||
'ProductSkuVariantValues.ProductCategoryVariantOptions',
|
||||
]);
|
||||
$this->set(compact('productSku'));
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Add method
|
||||
*
|
||||
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
|
||||
*/
|
||||
public function add($productId = null)
|
||||
{
|
||||
$toGetCartesianProductsFrom = [];
|
||||
$productSkus = [];
|
||||
$product = $this->ProductSkus->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) {
|
||||
$existingSkusForCartesianComparison[$existingProductSkuId] = Hash::combine($existingProductSku, '{n}.product_variant_id', '{n}.product_category_variant_option_id');
|
||||
}
|
||||
$productVariants = isset($product->product_variants) ? $product->product_variants : [];
|
||||
public function add($productId = null) {
|
||||
$toGetCartesianProductsFrom = [];
|
||||
$productSkus = [];
|
||||
$product = $this->ProductSkus->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) {
|
||||
$existingSkusForCartesianComparison[$existingProductSkuId] = Hash::combine($existingProductSku, '{n}.product_variant_id', '{n}.product_category_variant_option_id');
|
||||
}
|
||||
$productVariants = $product->product_variants ?? [];
|
||||
// dd($productVariants);
|
||||
$productVariantsMapping = Hash::combine($productVariants, '{n}.product_category_variant.id', '{n}.id');
|
||||
$productCategoryVariants = Hash::extract($productVariants, '{n}.product_category_variant');
|
||||
$productVariantsMapping = Hash::combine($productVariants, '{n}.product_category_variant.id', '{n}.id');
|
||||
$productCategoryVariants = Hash::extract($productVariants, '{n}.product_category_variant');
|
||||
// dd($productCategoryVariants);
|
||||
$optionMapping = Hash::combine($productCategoryVariants, '{n}.product_category_variant_options.{n}.id', '{n}.product_category_variant_options.{n}.variant_value');
|
||||
$optionMapping = Hash::combine($productCategoryVariants, '{n}.product_category_variant_options.{n}.id', '{n}.product_category_variant_options.{n}.variant_value');
|
||||
// dd($optionMapping);
|
||||
|
||||
$variantNameMapping = Hash::combine($productCategoryVariants, '{n}.id', '{n}.name');
|
||||
$variantNameMapping = Hash::combine($productCategoryVariants, '{n}.id', '{n}.name');
|
||||
// dd($variantNameMapping);
|
||||
|
||||
foreach ($productCategoryVariants as $productCategoryVariant) {
|
||||
$options = Hash::extract($productCategoryVariant['product_category_variant_options'] ?? [], '{n}.id');
|
||||
$toGetCartesianProductsFrom[$productVariantsMapping[$productCategoryVariant['id']]] = $options;
|
||||
}
|
||||
foreach ($productCategoryVariants as $productCategoryVariant) {
|
||||
$options = Hash::extract($productCategoryVariant['product_category_variant_options'] ?? [], '{n}.id');
|
||||
$toGetCartesianProductsFrom[$productVariantsMapping[$productCategoryVariant['id']]] = $options;
|
||||
}
|
||||
// dd($toGetCartesianProductsFrom);
|
||||
|
||||
$numSkusToAdd = count(combinations($toGetCartesianProductsFrom));
|
||||
for ($i = 0; $i < $numSkusToAdd; $i++) {
|
||||
$productSkus[$i] = $this->ProductSkus->newEmptyEntity();
|
||||
}
|
||||
$this->set(compact(
|
||||
'product',
|
||||
'productSkus',
|
||||
'productCategoryVariants',
|
||||
'productVariantsMapping',
|
||||
'toGetCartesianProductsFrom',
|
||||
'optionMapping',
|
||||
'variantNameMapping',
|
||||
'numSkusToAdd',
|
||||
'existingProductSkus',
|
||||
'existingSkusForCartesianComparison'
|
||||
));
|
||||
$numSkusToAdd = count(combinations($toGetCartesianProductsFrom));
|
||||
for ($i = 0; $i < $numSkusToAdd; $i++) {
|
||||
$productSkus[$i] = $this->ProductSkus->newEmptyEntity();
|
||||
}
|
||||
$this->set(compact(
|
||||
'product',
|
||||
'productSkus',
|
||||
'productCategoryVariants',
|
||||
'productVariantsMapping',
|
||||
'toGetCartesianProductsFrom',
|
||||
'optionMapping',
|
||||
'variantNameMapping',
|
||||
'numSkusToAdd',
|
||||
'existingProductSkus',
|
||||
'existingSkusForCartesianComparison',
|
||||
));
|
||||
|
||||
if ($this->request->is('post')) {
|
||||
$postedSkus = $this->request->getData();
|
||||
$saveOptions = [
|
||||
'fields' => [
|
||||
'product_id',
|
||||
'sku',
|
||||
'barcode',
|
||||
'price',
|
||||
'cost',
|
||||
'product_sku_variant_values',
|
||||
'created',
|
||||
'modified',
|
||||
'enabled',
|
||||
'default_sku',
|
||||
],
|
||||
'associated' => [
|
||||
'ProductSkuVariantValues' => [
|
||||
'fields' => [
|
||||
'product_variant_id',
|
||||
'product_category_variant_option_id',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$finalPostData = [];
|
||||
$postedSkus = Hash::insert($postedSkus, '{n}.product_id', $productId);
|
||||
if ($this->request->is('post')) {
|
||||
$postedSkus = $this->request->getData();
|
||||
$saveOptions = [
|
||||
'fields' => [
|
||||
'product_id',
|
||||
'sku',
|
||||
'barcode',
|
||||
'price',
|
||||
'cost',
|
||||
'product_sku_variant_values',
|
||||
'created',
|
||||
'modified',
|
||||
'enabled',
|
||||
'default_sku',
|
||||
],
|
||||
'associated' => [
|
||||
'ProductSkuVariantValues' => [
|
||||
'fields' => [
|
||||
'product_variant_id',
|
||||
'product_category_variant_option_id',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$finalPostData = [];
|
||||
$postedSkus = Hash::insert($postedSkus, '{n}.product_id', $productId);
|
||||
|
||||
foreach ($postedSkus as $postedSkuCnt => $postedSku) {
|
||||
if (!isset($postedSku['sku']) || !$postedSku['sku']) {
|
||||
unset($productSkus[$postedSkuCnt]);
|
||||
foreach ($postedSkus as $postedSkuCnt => $postedSku) {
|
||||
if (!isset($postedSku['sku']) || !$postedSku['sku']) {
|
||||
unset($productSkus[$postedSkuCnt]);
|
||||
|
||||
continue;
|
||||
}
|
||||
$finalPostData[$postedSkuCnt] = $postedSku;
|
||||
}
|
||||
if (!$productSkus || !$postedSkus) {
|
||||
$this->Flash->error('Nothing to save! Add at least one SKU next time.');
|
||||
continue;
|
||||
}
|
||||
$finalPostData[$postedSkuCnt] = $postedSku;
|
||||
}
|
||||
if (!$productSkus || !$postedSkus) {
|
||||
$this->Flash->error('Nothing to save! Add at least one SKU next time.');
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// dd($finalPostData);
|
||||
$productSkus = $this->ProductSkus->patchEntities($productSkus, $finalPostData, $saveOptions);
|
||||
$errors = [];
|
||||
$successes = [];
|
||||
foreach ($productSkus as $productSkuToSave) {
|
||||
$productSkus = $this->ProductSkus->patchEntities($productSkus, $finalPostData, $saveOptions);
|
||||
$errors = [];
|
||||
$successes = [];
|
||||
foreach ($productSkus as $productSkuToSave) {
|
||||
// dd($productSkuToSave);
|
||||
|
||||
if (!$this->ProductSkus->save($productSkuToSave, $saveOptions)) {
|
||||
Log::debug(print_r('$productSkuToSave->getErrors()', true));
|
||||
Log::debug(print_r($productSkuToSave->getErrors(), true));
|
||||
dd($productSkuToSave->getErrors());
|
||||
continue;
|
||||
}
|
||||
$successes[] = $productSkuToSave;
|
||||
}
|
||||
if (!$this->ProductSkus->save($productSkuToSave, $saveOptions)) {
|
||||
Log::debug(print_r('$productSkuToSave->getErrors()', true));
|
||||
Log::debug(print_r($productSkuToSave->getErrors(), true));
|
||||
dd($productSkuToSave->getErrors());
|
||||
|
||||
if ($successes) {
|
||||
$this->Flash->success(__(count($successes) . ' New SKUs have been saved.'));
|
||||
continue;
|
||||
}
|
||||
$successes[] = $productSkuToSave;
|
||||
}
|
||||
|
||||
return $this->redirect(['action' => 'index']);
|
||||
}
|
||||
if ($successes) {
|
||||
$this->Flash->success(__(count($successes) . ' New SKUs have been saved.'));
|
||||
|
||||
$this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.'));
|
||||
}
|
||||
$this->set(compact(
|
||||
'productSkus'
|
||||
));
|
||||
}
|
||||
return $this->redirect(['action' => 'index']);
|
||||
}
|
||||
|
||||
/**
|
||||
$this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.'));
|
||||
}
|
||||
$this->set(compact(
|
||||
'productSkus',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
|
||||
*/
|
||||
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));
|
||||
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.'));
|
||||
$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'));
|
||||
}
|
||||
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.
|
||||
* @return \Cake\Http\Response|null Redirects to index.
|
||||
*/
|
||||
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.'));
|
||||
}
|
||||
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']);
|
||||
}
|
||||
return $this->redirect(['action' => 'index']);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* @return \Cake\Http\Response|null|void Renders view
|
||||
*/
|
||||
public function select()
|
||||
{
|
||||
$productSkus = $this->ProductSkus
|
||||
->find('list')
|
||||
->where(['product_id' => $this->request->getQuery('product_id', '-1')])
|
||||
->orderBy(['sku'])
|
||||
->toArray();
|
||||
public function select() {
|
||||
$productSkus = $this->ProductSkus
|
||||
->find('list')
|
||||
->where(['product_id' => $this->request->getQuery('product_id', '-1')])
|
||||
->orderBy(['sku'])
|
||||
->toArray();
|
||||
|
||||
$this->set(compact('productSkus'));
|
||||
}
|
||||
|
||||
$this->set(compact('productSkus'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user