2026-03-25 17:41:15 +09:00
|
|
|
<?php
|
|
|
|
|
|
2026-04-08 15:22:24 +09:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-03-25 17:41:15 +09:00
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
|
use App\Models\SalesAgencyModel;
|
|
|
|
|
|
|
|
|
|
class SalesAgency extends BaseController
|
|
|
|
|
{
|
2026-04-08 15:22:24 +09:00
|
|
|
private const SCHEMA_ERROR = '판매 대행소 테이블에 sa_kind·sa_code 컬럼이 없습니다. DB에 writable/database/sales_agency_migrate_to_kind_code_name.sql(또는 신규용 sales_agency_tables.sql)을 적용한 뒤 다시 시도해 주세요.';
|
|
|
|
|
|
2026-03-25 17:41:15 +09:00
|
|
|
private SalesAgencyModel $model;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->model = model(SalesAgencyModel::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
helper('admin');
|
|
|
|
|
$lgIdx = admin_effective_lg_idx();
|
2026-04-08 15:22:24 +09:00
|
|
|
if (! $lgIdx) {
|
|
|
|
|
return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.');
|
2026-03-25 17:41:15 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-08 15:22:24 +09:00
|
|
|
$searchField = trim((string) ($this->request->getGet('search_field') ?? ''));
|
|
|
|
|
$searchQuery = trim((string) ($this->request->getGet('search_query') ?? ''));
|
|
|
|
|
$allowedFields = ['sa_idx', 'sa_kind', 'sa_code', 'sa_name'];
|
|
|
|
|
if (! in_array($searchField, $allowedFields, true)) {
|
|
|
|
|
$searchField = 'sa_name';
|
|
|
|
|
}
|
2026-03-25 17:41:15 +09:00
|
|
|
|
2026-04-08 15:22:24 +09:00
|
|
|
$builder = $this->model->where('sa_lg_idx', $lgIdx);
|
|
|
|
|
if ($searchQuery !== '') {
|
|
|
|
|
if ($searchField === 'sa_idx') {
|
|
|
|
|
if (ctype_digit($searchQuery)) {
|
|
|
|
|
$builder->where('sa_idx', (int) $searchQuery);
|
|
|
|
|
} else {
|
|
|
|
|
// 번호 검색은 숫자만 허용한다.
|
|
|
|
|
$builder->where('sa_idx', 0);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$builder->like($searchField, $searchQuery);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$list = $builder->orderBy('sa_idx', 'DESC')->paginate(20);
|
|
|
|
|
$pager = $this->model->pager;
|
|
|
|
|
// 전체 URL·쿼리를 setPath에 넣으면 Pager URI 경로가 깨져 404가 난다. 경로만 지정한다(필터는 현재 GET이 병합됨).
|
|
|
|
|
$pager->setPath('bag/sales-agencies');
|
|
|
|
|
|
|
|
|
|
return $this->renderWorkPage('판매 대행소 관리', 'admin/sales_agency/index', [
|
|
|
|
|
'list' => $list,
|
|
|
|
|
'pager' => $pager,
|
|
|
|
|
'search_field' => $searchField,
|
|
|
|
|
'search_query' => $searchQuery,
|
2026-03-25 17:41:15 +09:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create()
|
|
|
|
|
{
|
2026-04-08 15:22:24 +09:00
|
|
|
helper('admin');
|
|
|
|
|
if (! admin_effective_lg_idx()) {
|
|
|
|
|
return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->renderWorkPage('판매 대행소 등록', 'admin/sales_agency/create');
|
2026-03-25 17:41:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function store()
|
|
|
|
|
{
|
|
|
|
|
helper('admin');
|
2026-04-08 15:22:24 +09:00
|
|
|
$lgIdx = admin_effective_lg_idx();
|
|
|
|
|
if (! $lgIdx) {
|
|
|
|
|
return redirect()->to(mgmt_url('sales-agencies'))->with('error', '지자체를 선택해 주세요.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! $this->model->hasKindCodeColumns()) {
|
|
|
|
|
return redirect()->back()->withInput()->with('error', self::SCHEMA_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 17:41:15 +09:00
|
|
|
$rules = [
|
2026-04-08 15:22:24 +09:00
|
|
|
'sa_kind' => 'required|max_length[50]',
|
|
|
|
|
'sa_code' => 'required|max_length[50]',
|
|
|
|
|
'sa_name' => 'required|max_length[100]',
|
2026-03-25 17:41:15 +09:00
|
|
|
];
|
|
|
|
|
if (! $this->validate($rules)) {
|
|
|
|
|
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 15:22:24 +09:00
|
|
|
$code = trim((string) $this->request->getPost('sa_code'));
|
|
|
|
|
if ($this->model->where('sa_lg_idx', $lgIdx)->where('sa_code', $code)->first() !== null) {
|
|
|
|
|
return redirect()->back()->withInput()->with('error', '동일 지자체에 같은 대행소 코드가 이미 있습니다.');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 17:41:15 +09:00
|
|
|
$this->model->insert([
|
2026-04-08 15:22:24 +09:00
|
|
|
'sa_lg_idx' => $lgIdx,
|
|
|
|
|
'sa_kind' => trim((string) $this->request->getPost('sa_kind')),
|
|
|
|
|
'sa_code' => $code,
|
|
|
|
|
'sa_name' => trim((string) $this->request->getPost('sa_name')),
|
|
|
|
|
'sa_regdate' => date('Y-m-d H:i:s'),
|
2026-03-25 17:41:15 +09:00
|
|
|
]);
|
|
|
|
|
|
2026-04-08 15:22:24 +09:00
|
|
|
return redirect()->to(mgmt_url('sales-agencies'))->with('success', '판매 대행소가 등록되었습니다.');
|
2026-03-25 17:41:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function edit(int $id)
|
|
|
|
|
{
|
|
|
|
|
helper('admin');
|
|
|
|
|
$item = $this->model->find($id);
|
2026-04-08 15:22:24 +09:00
|
|
|
if (! $item || (int) $item->sa_lg_idx !== admin_effective_lg_idx()) {
|
|
|
|
|
return redirect()->to(mgmt_url('sales-agencies'))->with('error', '대행소를 찾을 수 없습니다.');
|
2026-03-25 17:41:15 +09:00
|
|
|
}
|
|
|
|
|
|
2026-04-08 15:22:24 +09:00
|
|
|
return $this->renderWorkPage('판매 대행소 수정', 'admin/sales_agency/edit', ['item' => $item]);
|
2026-03-25 17:41:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(int $id)
|
|
|
|
|
{
|
|
|
|
|
helper('admin');
|
2026-04-08 15:22:24 +09:00
|
|
|
$lgIdx = admin_effective_lg_idx();
|
|
|
|
|
$item = $this->model->find($id);
|
|
|
|
|
if (! $item || ! $lgIdx || (int) $item->sa_lg_idx !== $lgIdx) {
|
|
|
|
|
return redirect()->to(mgmt_url('sales-agencies'))->with('error', '대행소를 찾을 수 없습니다.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (! $this->model->hasKindCodeColumns()) {
|
|
|
|
|
return redirect()->back()->withInput()->with('error', self::SCHEMA_ERROR);
|
2026-03-25 17:41:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$rules = [
|
2026-04-08 15:22:24 +09:00
|
|
|
'sa_kind' => 'required|max_length[50]',
|
|
|
|
|
'sa_code' => 'required|max_length[50]',
|
|
|
|
|
'sa_name' => 'required|max_length[100]',
|
2026-03-25 17:41:15 +09:00
|
|
|
];
|
|
|
|
|
if (! $this->validate($rules)) {
|
|
|
|
|
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 15:22:24 +09:00
|
|
|
$code = trim((string) $this->request->getPost('sa_code'));
|
|
|
|
|
$dup = $this->model->where('sa_lg_idx', $lgIdx)->where('sa_code', $code)->where('sa_idx !=', $id)->first();
|
|
|
|
|
if ($dup !== null) {
|
|
|
|
|
return redirect()->back()->withInput()->with('error', '동일 지자체에 같은 대행소 코드가 이미 있습니다.');
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 17:41:15 +09:00
|
|
|
$this->model->update($id, [
|
2026-04-08 15:22:24 +09:00
|
|
|
'sa_kind' => trim((string) $this->request->getPost('sa_kind')),
|
|
|
|
|
'sa_code' => $code,
|
|
|
|
|
'sa_name' => trim((string) $this->request->getPost('sa_name')),
|
2026-03-25 17:41:15 +09:00
|
|
|
]);
|
|
|
|
|
|
2026-04-08 15:22:24 +09:00
|
|
|
return redirect()->to(mgmt_url('sales-agencies'))->with('success', '판매 대행소가 수정되었습니다.');
|
2026-03-25 17:41:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete(int $id)
|
|
|
|
|
{
|
|
|
|
|
helper('admin');
|
2026-04-08 15:22:24 +09:00
|
|
|
$lgIdx = admin_effective_lg_idx();
|
|
|
|
|
$item = $this->model->find($id);
|
|
|
|
|
if (! $item || ! $lgIdx || (int) $item->sa_lg_idx !== $lgIdx) {
|
|
|
|
|
return redirect()->to(mgmt_url('sales-agencies'))->with('error', '대행소를 찾을 수 없습니다.');
|
2026-03-25 17:41:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->model->delete($id);
|
2026-04-08 15:22:24 +09:00
|
|
|
|
|
|
|
|
return redirect()->to(mgmt_url('sales-agencies'))->with('success', '삭제되었습니다.');
|
2026-03-25 17:41:15 +09:00
|
|
|
}
|
|
|
|
|
}
|