117 lines
4.0 KiB
PHP
117 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\BillingEntity;
|
|
use App\Entities\ProductEntity;
|
|
|
|
class BillingModel extends BaseModel
|
|
{
|
|
private $_product_options = null;
|
|
protected $table = "tw_order";
|
|
protected $useAutoIncrement = false;
|
|
protected $returnType = BillingEntity::class;
|
|
protected $useSoftDeletes = true;
|
|
public function __construct()
|
|
{
|
|
parent::__construct('Billing');
|
|
$this->allowedFields = [
|
|
...$this->allowedFields, 'order_uid', "user_uid",
|
|
"title", "upload_file", "response", "status"
|
|
];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
final public function getTitleField(): string
|
|
{
|
|
return 'title';
|
|
}
|
|
public function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case 'order_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 "upload_file": //uploaded[{$field}] == requried와 같은의미
|
|
$rules[$field] = "if_exist|string";
|
|
break;
|
|
case "response":
|
|
$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): BillingEntity
|
|
{
|
|
return parent::getEntity($conditions);
|
|
}
|
|
public function create(array $formDatas): BillingEntity
|
|
{
|
|
return $this->create_process(new BillingEntity(), $formDatas);
|
|
}
|
|
public function modify(BillingEntity $entity, array $formDatas): BillingEntity
|
|
{
|
|
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 $entity, int $quantity, $paymentDay = null): BillingEntity
|
|
{
|
|
$formDatas = [];
|
|
$formDatas['product_uid'] = $entity->getPrimaryKey();
|
|
//상품명을 복사해서 구매한 상품명에 넣기
|
|
$formDatas[$this->getTitleField()] = $entity->getTitle();
|
|
$formDatas['cost'] = $entity->price;
|
|
$formDatas['sale'] = 0;
|
|
$formDatas['quantity'] = $quantity;
|
|
$formDatas['price'] = $formDatas['cost'] * $formDatas['quantity'];
|
|
if (!is_null($paymentDay)) {
|
|
$formDatas['paymentday'] = $paymentDay;
|
|
}
|
|
return $this->create($formDatas);
|
|
}
|
|
//장바구니에 빼기
|
|
public function cancelCart(BillingEntity $entity)
|
|
{
|
|
$formDatas = array();
|
|
//장바구니인경우에만
|
|
if ($entity->status == DEFAULTS['STATUS']) {
|
|
$formDatas['status'] = 'unuse';
|
|
}
|
|
return $this->modify($entity, $formDatas);
|
|
}
|
|
}
|