initModel(new ProductModel()); $this->_viewDatas['className'] = 'Product'; $this->_viewPath .= strtolower($this->_viewDatas['className']); $this->_viewDatas['title'] = lang($this->_viewDatas['className'] . '.title'); $this->_viewDatas['class_icon'] = CLASS_ICONS[strtoupper($this->_viewDatas['className'])]; helper($this->_viewDatas['className']); } final protected function getDeviceModel(): DeviceModel { return $this->_deviceModel = $this->_deviceModel ?: new DeviceModel(); } final protected function getProductDeviceModel(): ProductDeviceModel { return $this->_productDeviceModel = $this->_productDeviceModel ?: new ProductDeviceModel(); } public function getFields(string $action = ""): array { $fields = ["category", 'type', 'name', "photo", "device", "cost", "sale", "stock", "view_cnt", "status", "content",]; switch ($action) { case "index": case "excel": return ["category", "user_uid", 'type', 'name', "cost", "sale", "price", "stock", "view_cnt", "status", "created_at"]; break; case "view": return [...$fields, "created_at"]; break; default: return $fields; break; } } public function getFieldFilters(): array { return ["category", "user_uid", 'type', "status"]; } public function getFieldBatchFilters(): array { return parent::getFieldBatchFilters(); } //Field별 Form Datas 처리용 protected function getFieldFormData(string $field, $entity = null): array { switch ($field) { case 'photo': $file = $this->upload_image_procedure($field); if (!is_null($file)) { $this->_viewDatas['fieldDatas'][$field] = $file; } break; default: return parent::getFieldFormData($field, $entity); break; } return $this->_viewDatas['fieldDatas']; } private function calculate_price(): int { if ($this->_viewDatas['fieldDatas']['cost'] < $this->_viewDatas['fieldDatas']['sale']) { throw new \Exception(sprintf( "%s가[%s] %s[%s]보다 작습니다.", lang($this->_viewDatas['className'] . '.label.cost'), number_format($this->_viewDatas['fieldDatas']['cost']), lang($this->_viewDatas['className'] . '.label.sale'), number_format($this->_viewDatas['fieldDatas']['sale']), )); } return $this->_viewDatas['fieldDatas']['cost'] - $this->_viewDatas['fieldDatas']['sale']; } //가상서버 protected function virtual_process(ProductEntity $entity) { //가상서버정보 $protudctDatas = array( 'category' => 'virtual', 'name' => '', 'content' => '', 'cost' => $this->_viewDatas['fieldDatas']['price'], 'price' => $this->_viewDatas['fieldDatas']['price'], 'sale' => 0, 'stock' => 1, 'view_cnt' => 1, 'status' => 'use', ); //서버부품정보검증 $titles = array('가상서버'); //foreach (Product['parts']['virtual']['category'] as $category => $attrs) { foreach ([] as $category => $attrs) { if (!$this->_viewDatas['fieldDatas'][$category]) { throw new \Exception($category . "의 값이 지정되지 않았습니다."); } else { $protudctDatas[$category . "_model"] = $attrs['label']; $protudctDatas[$category . "_cnt"] = $this->_viewDatas['fieldDatas'][$category]; array_push( $titles, sprintf( "%s * %s%s,", $protudctDatas[$category . "_model"], $protudctDatas[$category . "_cnt"], $attrs['unit'], ), ); } } $protudctDatas['name'] = implode(" ", $titles); $protudctDatas['content'] = implode("\n", $titles); $product = $this->getModel()->create($protudctDatas); //return $this->add_procedure($product, 1, $this->_viewDatas['fieldDatas']['paymentday']); } //주문처리 protected function device_process(ProductEntity $entity) { switch ($this->_viewDatas['fieldDatas']['category']) { case 'virtual': return $this->virtual_process($entity); break; case 'beremetal': //상품관련 tw_product_device 기존정보 삭제처리 $productDeviceEntitys = $this->getProductDeviceModel()->getEntitys(['product_uid' => $entity->getPrimaryKey()]); foreach ($productDeviceEntitys as $productDeviceEntity) { $this->getProductDeviceModel()->delete($productDeviceEntity->getPrimaryKey()); } //상품관련 tw_product_device 생성처리 foreach (DEVICE['CATEGORYS'] as $field) { $device_uids = $this->request->getPost($field) ?: []; $productDeviceEntitys = []; foreach ($device_uids as $device_uid) { $formDatas = ['product_uid' => $entity->getPrimaryKey()]; $formDatas['device_uid'] = $device_uid; $productDeviceEntitys[] = $this->getProductDeviceModel()->create($formDatas); } //dd($productDeviceEntitys); } break; default: throw new \Exception($this->_viewDatas['fieldDatas']['category'] . "는 알수없는 상품 구분입니다. 다시 확인 부탁드립니다."); break; } } //Insert관련 protected function insert_process(): ProductEntity { $this->_viewDatas['fieldDatas']['price'] = $this->calculate_price(); $entity = parent::insert_process(); $this->device_process($entity); return $entity; } //Update관련 protected function update_process($entity): ProductEntity { $this->_viewDatas['fieldDatas']['price'] = $this->calculate_price(); $entity = parent::update_process($entity); $this->device_process($entity); // dd($entity); return $entity; } }