107 lines
3.8 KiB
PHP
107 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\PaymentEntity;
|
|
|
|
class PaymentModel extends BaseModel
|
|
{
|
|
private $_product_options = null;
|
|
protected $table = "tw_payment";
|
|
protected $returnType = PaymentEntity::class;
|
|
protected $useSoftDeletes = true;
|
|
public function __construct()
|
|
{
|
|
parent::__construct('Payment');
|
|
$this->allowedFields = [...$this->allowedFields, 'product_uid', "user_uid", "name", "cost", "sale", "quantity", "price", "status"];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
final public function getTitleField(): string
|
|
{
|
|
return 'name';
|
|
}
|
|
protected function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case 'product_uid':
|
|
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
|
|
break;
|
|
case $this->getTitleField():
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case 'cost':
|
|
case 'quantity':
|
|
case 'price':
|
|
$rules[$field] = "required|numeric";
|
|
break;
|
|
case 'sale':
|
|
$rules[$field] = "if_exist|numeric";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
//Field별 Form Option용
|
|
public function getFieldFormOption(string $field): array
|
|
{
|
|
switch ($field) {
|
|
case 'product_uid':
|
|
if (is_null($this->_product_options)) {
|
|
$productModel = new productModel();
|
|
$this->_product_options = $productModel->getOptions();
|
|
}
|
|
$options = $this->_product_options;
|
|
break;
|
|
default:
|
|
return parent::getFieldFormOption($field);
|
|
break;
|
|
}
|
|
if (!is_array($options)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$this->getClassName()}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true));
|
|
}
|
|
return $options;
|
|
}
|
|
public function getEntity($conditions): PaymentEntity
|
|
{
|
|
return parent::getEntity($conditions);
|
|
}
|
|
public function create(array $formDatas): PaymentEntity
|
|
{
|
|
return $this->create_process(new PaymentEntity(), $formDatas);
|
|
}
|
|
public function modify(PaymentEntity $entity, array $formDatas): PaymentEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
//Index관련
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
if ($word !== DEFAULTS['EMPTY']) {
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike($this->getTitleField(), $word, "both");
|
|
}
|
|
}
|
|
|
|
//장바구니에 넣기
|
|
public function addCart(array $formDatas): PaymentEntity
|
|
{
|
|
$orderFormDatas = [];
|
|
$orderFormDatas[$this->getTitleField()] = $formDatas[$this->getTitleField()];
|
|
$orderFormDatas['product_uid'] = $formDatas['product_uid'];
|
|
$orderFormDatas['cost'] = $formDatas['price'];
|
|
$orderFormDatas['sale'] = 0;
|
|
$orderFormDatas['quantity'] = $formDatas['quantity'];
|
|
$orderFormDatas['price'] = ($orderFormDatas['cost'] - $orderFormDatas['sale']) * $orderFormDatas['quantity'];
|
|
// echo var_export($orderFormDatas);
|
|
// exit;
|
|
return $this->create_process(new PaymentEntity(), $orderFormDatas);
|
|
}
|
|
//장바구니에 빼기
|
|
public function cancelCart(PaymentEntity $entity)
|
|
{
|
|
return $this->delete($entity->getPrimaryKey());
|
|
}
|
|
}
|