182 lines
6.7 KiB
PHP
182 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Front\Billing;
|
|
|
|
use App\Controllers\Front\FrontController;
|
|
use App\Entities\BillingEntity;
|
|
use App\Entities\OrderEntity;
|
|
use App\Models\BillingModel;
|
|
use App\Models\OrderBillingModel;
|
|
use App\Models\OrderModel;
|
|
use App\Models\UserModel;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class BillingController extends FrontController
|
|
{
|
|
private $_userModel = null;
|
|
private $_orderModel = null;
|
|
private $_orderBillingModel = null;
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
$this->_model = new BillingModel();
|
|
$this->_viewDatas['className'] = 'Billing';
|
|
$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']);
|
|
|
|
//Default 회원정보 Category
|
|
$this->_category = DEFAULTS['CATEGORY_BILLING'];
|
|
$this->isRole('index');
|
|
|
|
//사용자정보
|
|
$this->_viewDatas['user'] = $this->getUserModel()->getEntity([$this->getUserModel()->getPrimaryKey() => $this->_viewDatas['auth'][AUTH_FIELDS['ID']]]);
|
|
}
|
|
|
|
final protected function getUserModel(): UserModel
|
|
{
|
|
return $this->_userModel = $this->_userModel ?: new UserModel();
|
|
}
|
|
|
|
final protected function getOrderModel(): OrderModel
|
|
{
|
|
return $this->_orderModel = $this->_orderModel ?: new OrderModel();
|
|
}
|
|
final protected function getOrderBillingModel(): OrderBillingModel
|
|
{
|
|
return $this->_orderBillingModel = $this->_orderBillingModel ?: new OrderBillingModel();
|
|
}
|
|
|
|
public function getFields(string $action = ""): array
|
|
{
|
|
switch ($action) {
|
|
case "insert":
|
|
return ["price"];
|
|
break;
|
|
case "index":
|
|
case "excel":
|
|
return ["email", "phone", "title", "price", "status", "updated_at", "created_at"];
|
|
break;
|
|
case "view":
|
|
return ["email", "phone", "title", "price", "status", "updated_at", "created_at", 'response'];
|
|
break;
|
|
default:
|
|
return [];
|
|
break;
|
|
}
|
|
}
|
|
public function getFieldFilters(): array
|
|
{
|
|
return ["status"];
|
|
}
|
|
public function getFieldBatchFilters(): array
|
|
{
|
|
return ["status"];
|
|
}
|
|
|
|
//Insert관련 (결제처리용)
|
|
protected function insert_form_process()
|
|
{
|
|
parent::insert_form_process();
|
|
}
|
|
|
|
//Insert관련
|
|
private function linkOrderBilling(BillingEntity $entity, array $order_uids)
|
|
{
|
|
foreach ($order_uids as $order_uid) {
|
|
$this->getOrderBillingModel()->create([
|
|
"billing_uid" => $entity->getPrimaryKey(),
|
|
'order_uid' => $order_uid
|
|
]);
|
|
}
|
|
}
|
|
protected function insert_process()
|
|
{
|
|
//title지정하기
|
|
$this->_viewDatas['fieldDatas']['title'] = date("Y년m월") . " 청구서입니다.";
|
|
//사용자정보
|
|
$this->_viewDatas['fieldDatas']['email'] = $this->_viewDatas['user']->email;
|
|
$this->_viewDatas['fieldDatas']['phone'] = $this->_viewDatas['user']->phone;
|
|
return parent::insert_process();
|
|
}
|
|
public function insert()
|
|
{
|
|
$msg = "";
|
|
try {
|
|
$this->_viewDatas = $this->init(__FUNCTION__);
|
|
$this->insert_validate();
|
|
//주문정보 가져오기
|
|
$order_uids = $this->request->getVar('order_uids') ?: throw new \Exception("주문정보가 지정되지 않았습니다.");
|
|
//Transaction 시작
|
|
$this->_model->transStart();
|
|
//Billing 생성
|
|
$entity = $this->insert_process();
|
|
//DB연결용
|
|
$this->linkOrderBilling($entity, $order_uids);
|
|
//Transaction Commit
|
|
$this->_model->transComplete();
|
|
$msg = "{$this->_viewDatas['title']}에서 {$entity->getTitle()}의 " . __FUNCTION__ . " 완료하였습니다.";
|
|
//기존 return_url 무시
|
|
$this->_session->getFlashdata(SESSION_NAMES['RETURN_URL']);
|
|
return redirect()->to(URLS['Billing']);
|
|
} catch (\Exception $e) {
|
|
//Transaction Rollback
|
|
$this->_model->transRollback();
|
|
|
|
$msg = "{$this->_viewDatas['title']}에서 " . __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage();
|
|
log_message("error", $e->getMessage());
|
|
$this->_session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
|
|
return redirect()->back()->withInput();
|
|
} finally {
|
|
$this->_session->setFlashdata("return_message", $msg);
|
|
}
|
|
}
|
|
|
|
|
|
//Index관련
|
|
protected function index_setCondition()
|
|
{
|
|
//사용자정보(user_uid)에 맞는 Biiling정보 가져오기
|
|
$this->_model->where('user_uid', $this->_session->get(SESSION_NAMES['AUTH'])[AUTH_FIELDS['ID']]);
|
|
$this->_model->where("status", DEFAULTS['STATUS']);
|
|
parent::index_setCondition();
|
|
}
|
|
|
|
//추가기능
|
|
//청구서관련
|
|
final protected function createBilling(OrderEntity $entity, string $subject, string $html, $response = ''): BillingEntity
|
|
{
|
|
//청구서파일 생성
|
|
$fileName = sprintf("%s_%s", $entity->getPrimaryKey(), date('Y-m-d_Hm') . '.xlsx');
|
|
$writer = $this->spreadSheet($html);
|
|
$writer->save(PATHS['BILLING'] . '/' . $fileName);
|
|
//메일발송
|
|
$this->sendBilling($this->_viewDatas['fieldDatas']['email'], $subject, $html);
|
|
$fieldDatas = [
|
|
'order_uid' => $entity->getPrimaryKey(),
|
|
'user_uid' => $this->_viewDatas['auth'][AUTH_FIELDS['ID']],
|
|
'email' => $this->_viewDatas['fieldDatas']['email'],
|
|
'phone' => $this->_viewDatas['fieldDatas']['phone'],
|
|
'title' => $subject,
|
|
'content' => $html,
|
|
'upload_file' => sprintf("%s%s%s", $this->_viewDatas['className'] . '.xlsx', DEFAULTS['DELIMITER_FILE'], $fileName),
|
|
'response' => $response,
|
|
'status' => DEFAULTS['STATUS']
|
|
];
|
|
return $this->_model->create($fieldDatas);
|
|
}
|
|
final protected function sendBilling($email, string $subject, string $html): bool
|
|
{
|
|
$mail = \Config\Services::email();
|
|
$mail->setFrom(MALLS['support'], MALLS['title'], MALLS['master']);
|
|
$mail->setTo($email);
|
|
$mail->setCC(MALLS['master']);
|
|
$mail->setSubject($subject);
|
|
$mail->setMessage($html);
|
|
return $mail->send();
|
|
}
|
|
}
|