126 lines
4.2 KiB
PHP
126 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\OrderEntity;
|
|
use App\Entities\ProductEntity;
|
|
|
|
class OrderModel extends BaseModel
|
|
{
|
|
private $_product_options = null;
|
|
protected $table = "tw_order";
|
|
protected $useAutoIncrement = false;
|
|
protected $returnType = OrderEntity::class;
|
|
public function __construct()
|
|
{
|
|
parent::__construct('Order');
|
|
$this->allowedFields = [
|
|
...$this->allowedFields, 'product_uid', "user_uid",
|
|
"name", "cost", "sale", "quantity", "price",
|
|
"type", "paymentday", "status"
|
|
];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
final public function getTitleField(): string
|
|
{
|
|
return 'name';
|
|
}
|
|
public function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case "product_uid":
|
|
$rules[$field] = $this->getUUIDFieldRule('required');
|
|
break;
|
|
case $this->getTitleField():
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case 'cost':
|
|
case 'quantity':
|
|
case 'price':
|
|
$rules[$field] = "required|numeric";
|
|
break;
|
|
case 'sale':
|
|
case 'paymentday':
|
|
$rules[$field] = "if_exist|numeric";
|
|
break;
|
|
case 'type':
|
|
$rules[$field] = "required|string";
|
|
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): OrderEntity
|
|
{
|
|
return parent::getEntity($conditions);
|
|
}
|
|
public function create(array $formDatas): OrderEntity
|
|
{
|
|
return $this->create_process(new OrderEntity(), $formDatas);
|
|
}
|
|
public function modify(OrderEntity $entity, array $formDatas): OrderEntity
|
|
{
|
|
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");
|
|
}
|
|
}
|
|
|
|
//장바구니에 넣기()
|
|
final public function addCart(ProductEntity $product, int $quantity, string $type, $paymentDay = null): OrderEntity
|
|
{
|
|
$formDatas = [];
|
|
$formDatas['product_uid'] = $product->getPrimaryKey();
|
|
//상품명을 복사해서 구매한 상품명에 넣기
|
|
$formDatas[$this->getTitleField()] = $product->getTitle();
|
|
$formDatas['cost'] = $product->price;
|
|
$formDatas['sale'] = 0;
|
|
$formDatas['quantity'] = $quantity;
|
|
$formDatas['price'] = $formDatas['cost'] * $formDatas['quantity'];
|
|
$formDatas['type'] = $type;
|
|
if (!is_null($paymentDay)) {
|
|
$formDatas['paymentday'] = $paymentDay;
|
|
}
|
|
// echo var_export($formDatas, true);
|
|
// exit;
|
|
return $this->create($formDatas);
|
|
}
|
|
//장바구니에 빼기
|
|
public function cancelCart(OrderEntity $entity)
|
|
{
|
|
$formDatas = array();
|
|
//장바구니인경우에만
|
|
if ($entity->status == DEFAULTS['STATUS']) {
|
|
$formDatas['status'] = 'unuse';
|
|
}
|
|
return $this->modify($entity, $formDatas);
|
|
}
|
|
}
|