product skus v1

This commit is contained in:
2025-04-15 02:39:35 -07:00
parent 4868064c44
commit a01805dc53
10 changed files with 913 additions and 0 deletions

View File

@@ -0,0 +1,139 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Controller;
use Cake\Log\Log;
use CakeProducts\Controller\AppController;
use CheeseCake\Controller\Traits\OverrideTableTrait;
/**
* 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)
{
$productSku = $this->ProductSkus->get($id, contain: ['Products']);
$this->set(compact('productSku'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$productSku = $this->ProductSkus->newEmptyEntity();
if ($this->request->is('post')) {
$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 sku has 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.'));
}
$products = $this->ProductSkus->Products->find('list', limit: 200)->all();
$this->set(compact('productSku', 'products'));
}
/**
* 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']);
}
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Entity;
use Cake\ORM\Entity;
/**
* ProductSkus Entity
*
* @property string $id
* @property string $product_id
* @property string $sku
* @property string|null $barcode
* @property string|null $price
* @property string|null $cost
* @property \Cake\I18n\DateTime $created
* @property \Cake\I18n\DateTime|null $modified
* @property \Cake\I18n\DateTime|null $deleted
*
* @property \App\Model\Entity\Product $product
*/
class ProductSku extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array<string, bool>
*/
protected array $_accessible = [
'product_id' => true,
'sku' => true,
'barcode' => true,
'price' => true,
'cost' => true,
'created' => true,
'modified' => true,
'deleted' => true,
'product' => false,
];
}

View File

@@ -0,0 +1,114 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Table;
use CakeProducts\Model\Table\ProductsTable;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\ResultSetInterface;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Behavior\TimestampBehavior;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use CakeProducts\Model\Entity\ProductSku;
use Closure;
use Psr\SimpleCache\CacheInterface;
/**
* ProductSkus Model
*
* @property ProductsTable&BelongsTo $Products
*
* @method ProductSku newEmptyEntity()
* @method ProductSku newEntity(array $data, array $options = [])
* @method array<ProductSku> newEntities(array $data, array $options = [])
* @method ProductSku get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
* @method ProductSku findOrCreate($search, ?callable $callback = null, array $options = [])
* @method ProductSku patchEntity(EntityInterface $entity, array $data, array $options = [])
* @method array<ProductSku> patchEntities(iterable $entities, array $data, array $options = [])
* @method ProductSku|false save(EntityInterface $entity, array $options = [])
* @method ProductSku saveOrFail(EntityInterface $entity, array $options = [])
* @method iterable<ProductSku>|ResultSetInterface<ProductSku>|false saveMany(iterable $entities, array $options = [])
* @method iterable<ProductSku>|ResultSetInterface<ProductSku> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<ProductSku>|ResultSetInterface<ProductSku>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<ProductSku>|ResultSetInterface<ProductSku> deleteManyOrFail(iterable $entities, array $options = [])
*
* @mixin TimestampBehavior
*/
class ProductSkusTable extends Table
{
/**
* Initialize method
*
* @param array<string, mixed> $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('product_skus');
$this->setDisplayField('sku');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER',
]);
}
/**
* Default validation rules.
*
* @param Validator $validator Validator instance.
* @return Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->uuid('product_id')
->notEmptyString('product_id');
$validator
->scalar('sku')
->maxLength('sku', 255)
->requirePresence('sku', 'create')
->notEmptyString('sku');
$validator
->scalar('barcode')
->maxLength('barcode', 255)
->allowEmptyString('barcode');
$validator
->decimal('price')
->allowEmptyString('price');
$validator
->decimal('cost')
->allowEmptyString('cost');
$validator
->dateTime('deleted')
->allowEmptyDateTime('deleted');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param RulesChecker $rules The rules object to be modified.
* @return RulesChecker
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);
return $rules;
}
}