diff --git a/app/Controllers/Admin/BagInventory.php b/app/Controllers/Admin/BagInventory.php index 6b229d4..dfdb251 100644 --- a/app/Controllers/Admin/BagInventory.php +++ b/app/Controllers/Admin/BagInventory.php @@ -11,24 +11,23 @@ class BagInventory extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $invModel = model(BagInventoryModel::class); - $list = $invModel->where('bi_lg_idx', $lgIdx)->orderBy('bi_bag_code', 'ASC')->paginate(20); - $pager = $invModel->pager; + $list = $invModel->where('bi_lg_idx', $lgIdx)->orderBy('bi_bag_code', 'ASC')->paginate(20); + $pager = $invModel->pager; - return view('admin/layout', [ - 'title' => '재고 현황', - 'content' => view('admin/bag_inventory/index', ['list' => $list, 'pager' => $pager]), - ]); + return $this->renderWorkPage('재고 현황', 'admin/bag_inventory/index', ['list' => $list, 'pager' => $pager]); } public function export() { helper(['admin', 'export']); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) { - return redirect()->to(site_url('admin/bag-inventory'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(mgmt_url('bag-inventory'))->with('error', '지자체를 선택해 주세요.'); } $list = model(BagInventoryModel::class)->where('bi_lg_idx', $lgIdx)->orderBy('bi_bag_code', 'ASC')->findAll(); diff --git a/app/Controllers/Admin/BagIssue.php b/app/Controllers/Admin/BagIssue.php index 72b68b5..c8207d9 100644 --- a/app/Controllers/Admin/BagIssue.php +++ b/app/Controllers/Admin/BagIssue.php @@ -21,33 +21,34 @@ class BagIssue extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $builder = $this->issueModel->where('bi2_lg_idx', $lgIdx); $startDate = $this->request->getGet('start_date'); $endDate = $this->request->getGet('end_date'); - if ($startDate) $builder->where('bi2_issue_date >=', $startDate); - if ($endDate) $builder->where('bi2_issue_date <=', $endDate); + if ($startDate) { + $builder->where('bi2_issue_date >=', $startDate); + } + if ($endDate) { + $builder->where('bi2_issue_date <=', $endDate); + } - $list = $builder->orderBy('bi2_issue_date', 'DESC')->orderBy('bi2_idx', 'DESC')->paginate(20); + $list = $builder->orderBy('bi2_issue_date', 'DESC')->orderBy('bi2_idx', 'DESC')->paginate(20); $pager = $this->issueModel->pager; - return view('admin/layout', [ - 'title' => '무료용 불출 관리', - 'content' => view('admin/bag_issue/index', compact('list', 'startDate', 'endDate', 'pager')), - ]); + return $this->renderWorkPage('무료용 불출 관리', 'admin/bag_issue/index', compact('list', 'startDate', 'endDate', 'pager')); } public function create() { helper('admin'); - $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true) : []; + $lgIdx = admin_effective_lg_idx(); + $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true, $lgIdx) : []; - return view('admin/layout', [ - 'title' => '무료용 불출 처리', - 'content' => view('admin/bag_issue/create', compact('bagCodes')), - ]); + return $this->renderWorkPage('무료용 불출 처리', 'admin/bag_issue/create', compact('bagCodes')); } public function store() @@ -71,8 +72,8 @@ class BagIssue extends BaseController $bagCode = $this->request->getPost('bi2_bag_code'); $qty = (int) $this->request->getPost('bi2_qty'); - $kindO = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $detail = $kindO ? model(CodeDetailModel::class)->where('cd_ck_idx', $kindO->ck_idx)->where('cd_code', $bagCode)->first() : null; + $kindO = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $detail = $kindO ? model(CodeDetailModel::class)->findResolvedByKindAndCode((int) $kindO->ck_idx, (string) $bagCode, $lgIdx) : null; $bagName = $detail ? $detail->cd_name : ''; $db = \Config\Database::connect(); @@ -95,24 +96,22 @@ class BagIssue extends BaseController $this->issueModel->insert($issueData); $bi2Idx = (int) $this->issueModel->getInsertID(); - // CT-05: 감사 로그 helper('audit'); audit_log('create', 'bag_issue', $bi2Idx, null, array_merge($issueData, ['bi2_idx' => $bi2Idx])); - // 재고 감산 model(BagInventoryModel::class)->adjustQty($lgIdx, $bagCode, $bagName, -$qty); $db->transComplete(); - return redirect()->to(site_url('admin/bag-issues'))->with('success', '불출 처리되었습니다.'); + return redirect()->to(mgmt_url('bag-issues'))->with('success', '불출 처리되었습니다.'); } public function cancel(int $id) { helper('admin'); $item = $this->issueModel->find($id); - if (!$item || (int) $item->bi2_lg_idx !== admin_effective_lg_idx()) { - return redirect()->to(site_url('admin/bag-issues'))->with('error', '불출 내역을 찾을 수 없습니다.'); + if (! $item || (int) $item->bi2_lg_idx !== admin_effective_lg_idx()) { + return redirect()->to(mgmt_url('bag-issues'))->with('error', '불출 내역을 찾을 수 없습니다.'); } $db = \Config\Database::connect(); @@ -120,14 +119,12 @@ class BagIssue extends BaseController $before = (array) $item; $this->issueModel->update($id, ['bi2_status' => 'cancelled']); - // CT-05: 감사 로그 helper('audit'); audit_log('update', 'bag_issue', $id, $before, ['bi2_status' => 'cancelled']); - // 재고 복원 model(BagInventoryModel::class)->adjustQty((int) $item->bi2_lg_idx, $item->bi2_bag_code, $item->bi2_bag_name, (int) $item->bi2_qty); $db->transComplete(); - return redirect()->to(site_url('admin/bag-issues'))->with('success', '불출이 취소되었습니다.'); + return redirect()->to(mgmt_url('bag-issues'))->with('success', '불출이 취소되었습니다.'); } } diff --git a/app/Controllers/Admin/BagOrder.php b/app/Controllers/Admin/BagOrder.php index 2e557cd..2511cfb 100644 --- a/app/Controllers/Admin/BagOrder.php +++ b/app/Controllers/Admin/BagOrder.php @@ -11,8 +11,6 @@ use App\Models\CompanyModel; use App\Models\SalesAgencyModel; use App\Models\CodeKindModel; use App\Models\CodeDetailModel; -use Ramsey\Uuid\Uuid; - class BagOrder extends BaseController { private BagOrderModel $orderModel; @@ -28,8 +26,8 @@ class BagOrder extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) { - return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); } $builder = $this->orderModel->where('bo_lg_idx', $lgIdx); @@ -57,20 +55,19 @@ class BagOrder extends BaseController // 제작업체/대행소 이름 매핑 $companyMap = []; $agencyMap = []; foreach (model(CompanyModel::class)->where('cp_lg_idx', $lgIdx)->findAll() as $c) $companyMap[$c->cp_idx] = $c->cp_name; - foreach (model(SalesAgencyModel::class)->where('sa_lg_idx', $lgIdx)->findAll() as $a) $agencyMap[$a->sa_idx] = $a->sa_name; + foreach (model(SalesAgencyModel::class)->where('sa_lg_idx', $lgIdx)->orderForDisplay()->findAll() as $a) { + $agencyMap[$a->sa_idx] = '[' . ($a->sa_kind ?? '') . '] ' . ($a->sa_code ?? '') . ' — ' . ($a->sa_name ?? ''); + } - return view('admin/layout', [ - 'title' => '발주 현황', - 'content' => view('admin/bag_order/index', compact('list', 'itemSummary', 'companyMap', 'agencyMap', 'startDate', 'endDate', 'status', 'pager')), - ]); + return $this->renderWorkPage('발주 현황', 'admin/bag_order/index', compact('list', 'itemSummary', 'companyMap', 'agencyMap', 'startDate', 'endDate', 'status', 'pager')); } public function export() { helper(['admin', 'export']); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) { - return redirect()->to(site_url('admin/bag-orders'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(mgmt_url('bag-orders'))->with('error', '지자체를 선택해 주세요.'); } $builder = $this->orderModel->where('bo_lg_idx', $lgIdx); @@ -115,20 +112,19 @@ class BagOrder extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin/bag-orders'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(mgmt_url('bag-orders'))->with('error', '지자체를 선택해 주세요.'); + } // 봉투 종류 + 단가 + 포장단위 - $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true) : []; + $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true, $lgIdx) : []; $prices = model(BagPriceModel::class)->where('bp_lg_idx', $lgIdx)->where('bp_state', 1)->findAll(); $units = model(PackagingUnitModel::class)->where('pu_lg_idx', $lgIdx)->where('pu_state', 1)->findAll(); $companies = model(CompanyModel::class)->where('cp_lg_idx', $lgIdx)->where('cp_type', '제작업체')->where('cp_state', 1)->findAll(); - $agencies = model(SalesAgencyModel::class)->where('sa_lg_idx', $lgIdx)->where('sa_state', 1)->findAll(); + $agencies = model(SalesAgencyModel::class)->where('sa_lg_idx', $lgIdx)->orderForDisplay()->findAll(); - return view('admin/layout', [ - 'title' => '발주 등록', - 'content' => view('admin/bag_order/create', compact('bagCodes', 'prices', 'units', 'companies', 'agencies')), - ]); + return $this->renderWorkPage('발주 등록', 'admin/bag_order/create', compact('bagCodes', 'prices', 'units', 'companies', 'agencies')); } public function store() @@ -200,8 +196,8 @@ class BagOrder extends BaseController $unitPrice = $price ? (float) $price->bp_order_price : 0; // 봉투명 - $kindO = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $detail = $kindO ? model(CodeDetailModel::class)->where('cd_ck_idx', $kindO->ck_idx)->where('cd_code', $code)->first() : null; + $kindO = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $detail = $kindO ? model(CodeDetailModel::class)->findResolvedByKindAndCode((int) $kindO->ck_idx, (string) $code, $lgIdx) : null; $this->itemModel->insert([ 'boi_bo_idx' => $boIdx, @@ -216,7 +212,7 @@ class BagOrder extends BaseController $db->transComplete(); - return redirect()->to(site_url('admin/bag-orders'))->with('success', '발주가 등록되었습니다. LOT: ' . $lotNo); + return redirect()->to(mgmt_url('bag-orders'))->with('success', '발주가 등록되었습니다. LOT: ' . $lotNo); } public function detail(int $id) @@ -224,7 +220,7 @@ class BagOrder extends BaseController helper('admin'); $order = $this->orderModel->find($id); if (!$order || (int) $order->bo_lg_idx !== admin_effective_lg_idx()) { - return redirect()->to(site_url('admin/bag-orders'))->with('error', '발주를 찾을 수 없습니다.'); + return redirect()->to(mgmt_url('bag-orders'))->with('error', '발주를 찾을 수 없습니다.'); } $items = $this->itemModel->where('boi_bo_idx', $id)->findAll(); @@ -237,13 +233,12 @@ class BagOrder extends BaseController $agencyName = ''; if ($order->bo_agency_idx) { $a = model(SalesAgencyModel::class)->find($order->bo_agency_idx); - $agencyName = $a ? $a->sa_name : ''; + if ($a) { + $agencyName = '[' . ($a->sa_kind ?? '') . '] ' . ($a->sa_code ?? '') . ' — ' . ($a->sa_name ?? ''); + } } - return view('admin/layout', [ - 'title' => '발주 상세 — ' . $order->bo_lot_no, - 'content' => view('admin/bag_order/detail', compact('order', 'items', 'companyName', 'agencyName')), - ]); + return $this->renderWorkPage('발주 상세 — ' . $order->bo_lot_no, 'admin/bag_order/detail', compact('order', 'items', 'companyName', 'agencyName')); } public function cancel(int $id) @@ -251,14 +246,15 @@ class BagOrder extends BaseController helper('admin'); $order = $this->orderModel->find($id); if (!$order || (int) $order->bo_lg_idx !== admin_effective_lg_idx()) { - return redirect()->to(site_url('admin/bag-orders'))->with('error', '발주를 찾을 수 없습니다.'); + return redirect()->to(mgmt_url('bag-orders'))->with('error', '발주를 찾을 수 없습니다.'); } $before = (array) $order; $this->orderModel->update($id, ['bo_status' => 'cancelled', 'bo_moddate' => date('Y-m-d H:i:s')]); helper('audit'); audit_log('update', 'bag_order', $id, $before, ['bo_status' => 'cancelled']); - return redirect()->to(site_url('admin/bag-orders'))->with('success', '발주가 취소되었습니다.'); + + return redirect()->to(mgmt_url('bag-orders'))->with('success', '발주가 취소되었습니다.'); } public function delete(int $id) @@ -266,13 +262,14 @@ class BagOrder extends BaseController helper('admin'); $order = $this->orderModel->find($id); if (!$order || (int) $order->bo_lg_idx !== admin_effective_lg_idx()) { - return redirect()->to(site_url('admin/bag-orders'))->with('error', '발주를 찾을 수 없습니다.'); + return redirect()->to(mgmt_url('bag-orders'))->with('error', '발주를 찾을 수 없습니다.'); } $before = (array) $order; $this->orderModel->update($id, ['bo_status' => 'deleted', 'bo_moddate' => date('Y-m-d H:i:s')]); helper('audit'); audit_log('delete', 'bag_order', $id, $before, ['bo_status' => 'deleted']); - return redirect()->to(site_url('admin/bag-orders'))->with('success', '발주가 삭제 처리되었습니다.'); + + return redirect()->to(mgmt_url('bag-orders'))->with('success', '발주가 삭제 처리되었습니다.'); } } diff --git a/app/Controllers/Admin/BagReceiving.php b/app/Controllers/Admin/BagReceiving.php index 5c21a2b..7eb13d2 100644 --- a/app/Controllers/Admin/BagReceiving.php +++ b/app/Controllers/Admin/BagReceiving.php @@ -22,36 +22,37 @@ class BagReceiving extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $builder = $this->recvModel->where('br_lg_idx', $lgIdx); $startDate = $this->request->getGet('start_date'); $endDate = $this->request->getGet('end_date'); - if ($startDate) $builder->where('br_receive_date >=', $startDate); - if ($endDate) $builder->where('br_receive_date <=', $endDate); + if ($startDate) { + $builder->where('br_receive_date >=', $startDate); + } + if ($endDate) { + $builder->where('br_receive_date <=', $endDate); + } - $list = $builder->orderBy('br_receive_date', 'DESC')->orderBy('br_idx', 'DESC')->paginate(20); + $list = $builder->orderBy('br_receive_date', 'DESC')->orderBy('br_idx', 'DESC')->paginate(20); $pager = $this->recvModel->pager; - return view('admin/layout', [ - 'title' => '입고 현황', - 'content' => view('admin/bag_receiving/index', compact('list', 'startDate', 'endDate', 'pager')), - ]); + return $this->renderWorkPage('입고 현황', 'admin/bag_receiving/index', compact('list', 'startDate', 'endDate', 'pager')); } public function create() { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin/bag-receivings'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(mgmt_url('bag-receivings'))->with('error', '지자체를 선택해 주세요.'); + } - // 미입고 발주 목록 $orders = model(BagOrderModel::class)->where('bo_lg_idx', $lgIdx)->where('bo_status', 'normal')->orderBy('bo_order_date', 'DESC')->findAll(); - return view('admin/layout', [ - 'title' => '입고 처리', - 'content' => view('admin/bag_receiving/create', compact('orders')), - ]); + return $this->renderWorkPage('입고 처리', 'admin/bag_receiving/create', compact('orders')); } public function store() @@ -73,14 +74,12 @@ class BagReceiving extends BaseController $bagCode = $this->request->getPost('br_bag_code'); $qtyBox = (int) $this->request->getPost('br_qty_box'); - // 포장단위로 낱장 환산 $unit = model(\App\Models\PackagingUnitModel::class)->where('pu_lg_idx', $lgIdx)->where('pu_bag_code', $bagCode)->where('pu_state', 1)->first(); $totalPerBox = $unit ? (int) $unit->pu_total_per_box : 1; - $qtySheet = $qtyBox * $totalPerBox; + $qtySheet = $qtyBox * $totalPerBox; - // 봉투명 - $kindO = model(\App\Models\CodeKindModel::class)->where('ck_code', 'O')->first(); - $detail = $kindO ? model(\App\Models\CodeDetailModel::class)->where('cd_ck_idx', $kindO->ck_idx)->where('cd_code', $bagCode)->first() : null; + $kindO = model(\App\Models\CodeKindModel::class)->where('ck_code', 'O')->first(); + $detail = $kindO ? model(\App\Models\CodeDetailModel::class)->findResolvedByKindAndCode((int) $kindO->ck_idx, (string) $bagCode, $lgIdx) : null; $bagName = $detail ? $detail->cd_name : ''; $db = \Config\Database::connect(); @@ -100,11 +99,10 @@ class BagReceiving extends BaseController 'br_regdate' => date('Y-m-d H:i:s'), ]); - // 재고 가산 model(BagInventoryModel::class)->adjustQty($lgIdx, $bagCode, $bagName, $qtySheet); $db->transComplete(); - return redirect()->to(site_url('admin/bag-receivings'))->with('success', '입고 처리되었습니다. (' . $bagName . ' ' . $qtyBox . '박스)'); + return redirect()->to(mgmt_url('bag-receivings'))->with('success', '입고 처리되었습니다. (' . $bagName . ' ' . $qtyBox . '박스)'); } } diff --git a/app/Controllers/Admin/BagSale.php b/app/Controllers/Admin/BagSale.php index 5aa447c..7933964 100644 --- a/app/Controllers/Admin/BagSale.php +++ b/app/Controllers/Admin/BagSale.php @@ -23,45 +23,56 @@ class BagSale extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); - - $builder = $this->saleModel->where('bs_lg_idx', $lgIdx); - $startDate = $this->request->getGet('start_date'); - $endDate = $this->request->getGet('end_date'); - $type = $this->request->getGet('type'); - if ($startDate) $builder->where('bs_sale_date >=', $startDate); - if ($endDate) $builder->where('bs_sale_date <=', $endDate); - if ($type) $builder->where('bs_type', $type); - - $list = $builder->orderBy('bs_sale_date', 'DESC')->orderBy('bs_idx', 'DESC')->paginate(20); - $pager = $this->saleModel->pager; - - return view('admin/layout', [ - 'title' => '판매/반품 관리', - 'content' => view('admin/bag_sale/index', compact('list', 'startDate', 'endDate', 'type', 'pager')), - ]); - } - - public function export() - { - helper(['admin', 'export']); - $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) { - return redirect()->to(site_url('admin/bag-sales'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); } $builder = $this->saleModel->where('bs_lg_idx', $lgIdx); $startDate = $this->request->getGet('start_date'); $endDate = $this->request->getGet('end_date'); $type = $this->request->getGet('type'); - if ($startDate) $builder->where('bs_sale_date >=', $startDate); - if ($endDate) $builder->where('bs_sale_date <=', $endDate); - if ($type) $builder->where('bs_type', $type); + if ($startDate) { + $builder->where('bs_sale_date >=', $startDate); + } + if ($endDate) { + $builder->where('bs_sale_date <=', $endDate); + } + if ($type) { + $builder->where('bs_type', $type); + } + + $list = $builder->orderBy('bs_sale_date', 'DESC')->orderBy('bs_idx', 'DESC')->paginate(20); + $pager = $this->saleModel->pager; + + return $this->renderWorkPage('판매/반품 관리', 'admin/bag_sale/index', compact('list', 'startDate', 'endDate', 'type', 'pager')); + } + + public function export() + { + helper(['admin', 'export']); + $lgIdx = admin_effective_lg_idx(); + if (! $lgIdx) { + return redirect()->to(mgmt_url('bag-sales'))->with('error', '지자체를 선택해 주세요.'); + } + + $builder = $this->saleModel->where('bs_lg_idx', $lgIdx); + $startDate = $this->request->getGet('start_date'); + $endDate = $this->request->getGet('end_date'); + $type = $this->request->getGet('type'); + if ($startDate) { + $builder->where('bs_sale_date >=', $startDate); + } + if ($endDate) { + $builder->where('bs_sale_date <=', $endDate); + } + if ($type) { + $builder->where('bs_type', $type); + } $list = $builder->orderBy('bs_sale_date', 'DESC')->orderBy('bs_idx', 'DESC')->findAll(); $typeMap = ['sale' => '판매', 'return' => '반품', 'cancel' => '취소']; - $rows = []; + $rows = []; foreach ($list as $row) { $rows[] = [ $row->bs_idx, @@ -87,16 +98,15 @@ class BagSale extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin/bag-sales'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(mgmt_url('bag-sales'))->with('error', '지자체를 선택해 주세요.'); + } $shops = model(DesignatedShopModel::class)->where('ds_lg_idx', $lgIdx)->where('ds_state', 1)->findAll(); - $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true) : []; + $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true, $lgIdx) : []; - return view('admin/layout', [ - 'title' => '판매 등록', - 'content' => view('admin/bag_sale/create', compact('shops', 'bagCodes')), - ]); + return $this->renderWorkPage('판매 등록', 'admin/bag_sale/create', compact('shops', 'bagCodes')); } public function store() @@ -120,10 +130,10 @@ class BagSale extends BaseController $qty = (int) $this->request->getPost('bs_qty'); $type = $this->request->getPost('bs_type'); - $shop = model(DesignatedShopModel::class)->find($dsIdx); - $kindO = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $detail = $kindO ? model(CodeDetailModel::class)->where('cd_ck_idx', $kindO->ck_idx)->where('cd_code', $bagCode)->first() : null; - $price = model(BagPriceModel::class)->where('bp_lg_idx', $lgIdx)->where('bp_bag_code', $bagCode)->where('bp_state', 1)->first(); + $shop = model(DesignatedShopModel::class)->find($dsIdx); + $kindO = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $detail = $kindO ? model(CodeDetailModel::class)->findResolvedByKindAndCode((int) $kindO->ck_idx, (string) $bagCode, $lgIdx) : null; + $price = model(BagPriceModel::class)->where('bp_lg_idx', $lgIdx)->where('bp_bag_code', $bagCode)->where('bp_state', 1)->first(); $unitPrice = $price ? (float) $price->bp_consumer : 0; $actualQty = ($type === 'return') ? -$qty : $qty; @@ -132,31 +142,30 @@ class BagSale extends BaseController $db->transStart(); $saleData = [ - 'bs_lg_idx' => $lgIdx, - 'bs_ds_idx' => $dsIdx, - 'bs_ds_name' => $shop ? $shop->ds_name : '', - 'bs_sale_date' => $this->request->getPost('bs_sale_date'), - 'bs_bag_code' => $bagCode, - 'bs_bag_name' => $detail ? $detail->cd_name : '', - 'bs_qty' => $actualQty, - 'bs_unit_price'=> $unitPrice, - 'bs_amount' => $unitPrice * abs($actualQty), - 'bs_type' => $type, - 'bs_regdate' => date('Y-m-d H:i:s'), + 'bs_lg_idx' => $lgIdx, + 'bs_ds_idx' => $dsIdx, + 'bs_ds_name' => $shop ? $shop->ds_name : '', + 'bs_sale_date' => $this->request->getPost('bs_sale_date'), + 'bs_bag_code' => $bagCode, + 'bs_bag_name' => $detail ? $detail->cd_name : '', + 'bs_qty' => $actualQty, + 'bs_unit_price' => $unitPrice, + 'bs_amount' => $unitPrice * abs($actualQty), + 'bs_type' => $type, + 'bs_regdate' => date('Y-m-d H:i:s'), ]; $this->saleModel->insert($saleData); $bsIdx = (int) $this->saleModel->getInsertID(); - // CT-05: 감사 로그 helper('audit'); audit_log('create', 'bag_sale', $bsIdx, null, array_merge($saleData, ['bs_idx' => $bsIdx])); - // 재고 감산(판매) / 가산(반품) model(BagInventoryModel::class)->adjustQty($lgIdx, $bagCode, $detail ? $detail->cd_name : '', -$actualQty); $db->transComplete(); $msg = ($type === 'sale') ? '판매 처리되었습니다.' : '반품 처리되었습니다.'; - return redirect()->to(site_url('admin/bag-sales'))->with('success', $msg); + + return redirect()->to(mgmt_url('bag-sales'))->with('success', $msg); } } diff --git a/app/Controllers/Admin/SalesReport.php b/app/Controllers/Admin/SalesReport.php index c263ee3..dbdb658 100644 --- a/app/Controllers/Admin/SalesReport.php +++ b/app/Controllers/Admin/SalesReport.php @@ -17,7 +17,9 @@ class SalesReport extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $startDate = $this->request->getGet('start_date') ?? date('Y-m-01'); $endDate = $this->request->getGet('end_date') ?? date('Y-m-d'); @@ -50,10 +52,7 @@ class SalesReport extends BaseController ", [$lgIdx, $startDate, $endDate])->getResult(); } - return view('admin/layout', [ - 'title' => '판매 대장', - 'content' => view('admin/sales_report/sales_ledger', compact('result', 'startDate', 'endDate', 'mode')), - ]); + return $this->renderWorkPage('판매 대장', 'admin/sales_report/sales_ledger', compact('result', 'startDate', 'endDate', 'mode')); } /** @@ -63,7 +62,9 @@ class SalesReport extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $date = $this->request->getGet('date') ?? date('Y-m-d'); $db = \Config\Database::connect(); @@ -91,10 +92,7 @@ class SalesReport extends BaseController ORDER BY bs_bag_code ", [$lgIdx, $monthStart, $date])->getResult(); - return view('admin/layout', [ - 'title' => '일계표', - 'content' => view('admin/sales_report/daily_summary', compact('daily', 'monthly', 'date')), - ]); + return $this->renderWorkPage('일계표', 'admin/sales_report/daily_summary', compact('daily', 'monthly', 'date')); } /** @@ -104,7 +102,9 @@ class SalesReport extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $startDate = $this->request->getGet('start_date') ?? date('Y-m-01'); $endDate = $this->request->getGet('end_date') ?? date('Y-m-d'); @@ -122,10 +122,7 @@ class SalesReport extends BaseController ORDER BY bs_bag_code ", [$lgIdx, $startDate, $endDate])->getResult(); - return view('admin/layout', [ - 'title' => '기간별 판매현황', - 'content' => view('admin/sales_report/period_sales', compact('result', 'startDate', 'endDate')), - ]); + return $this->renderWorkPage('기간별 판매현황', 'admin/sales_report/period_sales', compact('result', 'startDate', 'endDate')); } /** @@ -135,7 +132,9 @@ class SalesReport extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $year = $this->request->getGet('year') ?? date('Y'); $db = \Config\Database::connect(); @@ -161,10 +160,7 @@ class SalesReport extends BaseController ORDER BY bs_bag_code ", [$lgIdx, $year])->getResult(); - return view('admin/layout', [ - 'title' => '년 판매 현황', - 'content' => view('admin/sales_report/yearly_sales', compact('result', 'year')), - ]); + return $this->renderWorkPage('년 판매 현황', 'admin/sales_report/yearly_sales', compact('result', 'year')); } /** @@ -174,7 +170,9 @@ class SalesReport extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $startDate = $this->request->getGet('start_date') ?? date('Y-m-01'); $endDate = $this->request->getGet('end_date') ?? date('Y-m-d'); @@ -192,10 +190,7 @@ class SalesReport extends BaseController ORDER BY bs_ds_name ", [$lgIdx, $startDate, $endDate])->getResult(); - return view('admin/layout', [ - 'title' => '지정판매소별 판매현황', - 'content' => view('admin/sales_report/shop_sales', compact('result', 'startDate', 'endDate')), - ]); + return $this->renderWorkPage('지정판매소별 판매현황', 'admin/sales_report/shop_sales', compact('result', 'startDate', 'endDate')); } /** @@ -205,7 +200,9 @@ class SalesReport extends BaseController { helper(['admin', 'export']); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $startDate = $this->request->getGet('start_date') ?? date('Y-m-01'); $endDate = $this->request->getGet('end_date') ?? date('Y-m-d'); @@ -257,7 +254,9 @@ class SalesReport extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $startDate = $this->request->getGet('start_date') ?? date('Y-m-01'); $endDate = $this->request->getGet('end_date') ?? date('Y-m-d'); @@ -271,10 +270,7 @@ class SalesReport extends BaseController ORDER BY bs_sale_date DESC, bs_ds_name ", [$lgIdx, $startDate, $endDate])->getResult(); - return view('admin/layout', [ - 'title' => '반품/파기 현황', - 'content' => view('admin/sales_report/returns', compact('result', 'startDate', 'endDate')), - ]); + return $this->renderWorkPage('반품/파기 현황', 'admin/sales_report/returns', compact('result', 'startDate', 'endDate')); } /** @@ -284,7 +280,9 @@ class SalesReport extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $lotNo = $this->request->getGet('lot_no') ?? ''; $order = null; @@ -300,10 +298,7 @@ class SalesReport extends BaseController } } - return view('admin/layout', [ - 'title' => 'LOT 수불 조회', - 'content' => view('admin/sales_report/lot_flow', compact('lotNo', 'order', 'items', 'receivings')), - ]); + return $this->renderWorkPage('LOT 수불 조회', 'admin/sales_report/lot_flow', compact('lotNo', 'order', 'items', 'receivings')); } /** @@ -313,7 +308,9 @@ class SalesReport extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $startDate = $this->request->getGet('start_date') ?? date('Y-m-01'); $endDate = $this->request->getGet('end_date') ?? date('Y-m-d'); @@ -332,12 +329,9 @@ class SalesReport extends BaseController // 봉투 코드 목록 $kindO = model(\App\Models\CodeKindModel::class)->where('ck_code', 'O')->first(); - $bagCodes = $kindO ? model(\App\Models\CodeDetailModel::class)->getByKind((int) $kindO->ck_idx, true) : []; + $bagCodes = $kindO ? model(\App\Models\CodeDetailModel::class)->getByKind((int) $kindO->ck_idx, true, $lgIdx) : []; - return view('admin/layout', [ - 'title' => '기타 입출고', - 'content' => view('admin/sales_report/misc_flow', compact('result', 'startDate', 'endDate', 'bagCodes', 'tableExists')), - ]); + return $this->renderWorkPage('기타 입출고', 'admin/sales_report/misc_flow', compact('result', 'startDate', 'endDate', 'bagCodes', 'tableExists')); } /** @@ -347,7 +341,7 @@ class SalesReport extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin/reports/misc-flow'))->with('error', '지자체를 선택해 주세요.'); + if (!$lgIdx) return redirect()->to(mgmt_url('reports/misc-flow'))->with('error', '지자체를 선택해 주세요.'); $rules = [ 'bmf_type' => 'required|in_list[in,out]', @@ -366,7 +360,7 @@ class SalesReport extends BaseController // 봉투명 조회 $kindO = model(\App\Models\CodeKindModel::class)->where('ck_code', 'O')->first(); - $detail = $kindO ? model(\App\Models\CodeDetailModel::class)->where('cd_ck_idx', $kindO->ck_idx)->where('cd_code', $bagCode)->first() : null; + $detail = $kindO ? model(\App\Models\CodeDetailModel::class)->findResolvedByKindAndCode((int) $kindO->ck_idx, (string) $bagCode, $lgIdx) : null; $bagName = $detail ? $detail->cd_name : ''; $db = \Config\Database::connect(); @@ -383,7 +377,7 @@ class SalesReport extends BaseController $db->transComplete(); - return redirect()->to(site_url('admin/reports/misc-flow'))->with('success', '기타 입출고가 등록되었습니다.'); + return redirect()->to(mgmt_url('reports/misc-flow'))->with('success', '기타 입출고가 등록되었습니다.'); } /** @@ -393,7 +387,9 @@ class SalesReport extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $startDate = $this->request->getGet('start_date') ?? date('Y-m-01'); $endDate = $this->request->getGet('end_date') ?? date('Y-m-d'); @@ -430,9 +426,6 @@ class SalesReport extends BaseController // 현재 재고 $inventory = model(BagInventoryModel::class)->where('bi_lg_idx', $lgIdx)->findAll(); - return view('admin/layout', [ - 'title' => '봉투 수불 현황', - 'content' => view('admin/sales_report/supply_demand', compact('receiving', 'sales', 'issues', 'inventory', 'startDate', 'endDate')), - ]); + return $this->renderWorkPage('봉투 수불 현황', 'admin/sales_report/supply_demand', compact('receiving', 'sales', 'issues', 'inventory', 'startDate', 'endDate')); } } diff --git a/app/Controllers/Admin/ShopOrder.php b/app/Controllers/Admin/ShopOrder.php index 24bcde2..acb1fd3 100644 --- a/app/Controllers/Admin/ShopOrder.php +++ b/app/Controllers/Admin/ShopOrder.php @@ -26,37 +26,39 @@ class ShopOrder extends BaseController { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(work_area_home_url())->with('error', '지자체를 선택해 주세요.'); + } $builder = $this->orderModel->where('so_lg_idx', $lgIdx); $startDate = $this->request->getGet('start_date'); $endDate = $this->request->getGet('end_date'); - if ($startDate) $builder->where('so_delivery_date >=', $startDate); - if ($endDate) $builder->where('so_delivery_date <=', $endDate); + if ($startDate) { + $builder->where('so_delivery_date >=', $startDate); + } + if ($endDate) { + $builder->where('so_delivery_date <=', $endDate); + } - $list = $builder->orderBy('so_idx', 'DESC')->paginate(20); + $list = $builder->orderBy('so_idx', 'DESC')->paginate(20); $pager = $this->orderModel->pager; - return view('admin/layout', [ - 'title' => '주문 접수 관리', - 'content' => view('admin/shop_order/index', compact('list', 'startDate', 'endDate', 'pager')), - ]); + return $this->renderWorkPage('주문 접수 관리', 'admin/shop_order/index', compact('list', 'startDate', 'endDate', 'pager')); } public function create() { helper('admin'); $lgIdx = admin_effective_lg_idx(); - if (!$lgIdx) return redirect()->to(site_url('admin/shop-orders'))->with('error', '지자체를 선택해 주세요.'); + if (! $lgIdx) { + return redirect()->to(mgmt_url('shop-orders'))->with('error', '지자체를 선택해 주세요.'); + } $shops = model(DesignatedShopModel::class)->where('ds_lg_idx', $lgIdx)->where('ds_state', 1)->findAll(); - $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true) : []; + $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true, $lgIdx) : []; - return view('admin/layout', [ - 'title' => '주문 접수', - 'content' => view('admin/shop_order/create', compact('shops', 'bagCodes')), - ]); + return $this->renderWorkPage('주문 접수', 'admin/shop_order/create', compact('shops', 'bagCodes')); } public function store() @@ -65,9 +67,9 @@ class ShopOrder extends BaseController $lgIdx = admin_effective_lg_idx(); $rules = [ - 'so_ds_idx' => 'required|is_natural_no_zero', - 'so_delivery_date'=> 'required|valid_date[Y-m-d]', - 'so_payment_type' => 'required|in_list[이체,가상계좌]', + 'so_ds_idx' => 'required|is_natural_no_zero', + 'so_delivery_date' => 'required|valid_date[Y-m-d]', + 'so_payment_type' => 'required|in_list[이체,가상계좌]', ]; if (! $this->validate($rules)) { return redirect()->back()->withInput()->with('errors', $this->validator->getErrors()); @@ -77,57 +79,62 @@ class ShopOrder extends BaseController $db->transStart(); $dsIdx = (int) $this->request->getPost('so_ds_idx'); - $shop = model(DesignatedShopModel::class)->find($dsIdx); + $shop = model(DesignatedShopModel::class)->find($dsIdx); $this->orderModel->insert([ - 'so_lg_idx' => $lgIdx, - 'so_ds_idx' => $dsIdx, - 'so_ds_name' => $shop ? $shop->ds_name : '', - 'so_order_date' => date('Y-m-d'), - 'so_delivery_date'=> $this->request->getPost('so_delivery_date'), - 'so_payment_type' => $this->request->getPost('so_payment_type'), - 'so_status' => 'normal', - 'so_orderer_idx' => session()->get('mb_idx'), - 'so_regdate' => date('Y-m-d H:i:s'), + 'so_lg_idx' => $lgIdx, + 'so_ds_idx' => $dsIdx, + 'so_ds_name' => $shop ? $shop->ds_name : '', + 'so_order_date' => date('Y-m-d'), + 'so_delivery_date' => $this->request->getPost('so_delivery_date'), + 'so_payment_type' => $this->request->getPost('so_payment_type'), + 'so_status' => 'normal', + 'so_orderer_idx' => session()->get('mb_idx'), + 'so_regdate' => date('Y-m-d H:i:s'), ]); $soIdx = (int) $this->orderModel->getInsertID(); $bagCodes = $this->request->getPost('item_bag_code') ?? []; $qtys = $this->request->getPost('item_qty') ?? []; - $totalQty = 0; $totalAmt = 0; + $totalQty = 0; + $totalAmt = 0; foreach ($bagCodes as $i => $code) { - if (empty($code) || empty($qtys[$i])) continue; + if (empty($code) || empty($qtys[$i])) { + continue; + } $qty = (int) $qtys[$i]; - $price = model(BagPriceModel::class)->where('bp_lg_idx', $lgIdx)->where('bp_bag_code', $code)->where('bp_state', 1)->first(); + $price = model(BagPriceModel::class)->where('bp_lg_idx', $lgIdx)->where('bp_bag_code', $code)->where('bp_state', 1)->first(); $unitPrice = $price ? (float) $price->bp_consumer : 0; - $amount = $unitPrice * $qty; + $amount = $unitPrice * $qty; $unit = model(PackagingUnitModel::class)->where('pu_lg_idx', $lgIdx)->where('pu_bag_code', $code)->where('pu_state', 1)->first(); - $boxCount = 0; $packCount = 0; $sheetCount = $qty; + $boxCount = 0; + $packCount = 0; + $sheetCount = $qty; if ($unit && (int) $unit->pu_total_per_box > 0) { - $boxCount = intdiv($qty, (int) $unit->pu_total_per_box); - $remainder = $qty % (int) $unit->pu_total_per_box; + $boxCount = intdiv($qty, (int) $unit->pu_total_per_box); + $remainder = $qty % (int) $unit->pu_total_per_box; if ((int) $unit->pu_pack_per_sheet > 0) { - $packCount = intdiv($remainder, (int) $unit->pu_pack_per_sheet); + $packCount = intdiv($remainder, (int) $unit->pu_pack_per_sheet); $sheetCount = $remainder % (int) $unit->pu_pack_per_sheet; } } - $kindO = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $detail = $kindO ? model(CodeDetailModel::class)->where('cd_ck_idx', $kindO->ck_idx)->where('cd_code', $code)->first() : null; + $kindO = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $detail = $kindO ? model(CodeDetailModel::class)->findResolvedByKindAndCode((int) $kindO->ck_idx, (string) $code, $lgIdx) : null; $this->itemModel->insert([ - 'soi_so_idx' => $soIdx, - 'soi_bag_code' => $code, - 'soi_bag_name' => $detail ? $detail->cd_name : '', - 'soi_unit_price' => $unitPrice, - 'soi_qty' => $qty, - 'soi_amount' => $amount, - 'soi_box_count' => $boxCount, - 'soi_pack_count' => $packCount, - 'soi_sheet_count'=> $sheetCount, + 'soi_so_idx' => $soIdx, + 'soi_bag_code' => $code, + 'soi_bag_name' => $detail ? $detail->cd_name : '', + 'soi_unit_price' => $unitPrice, + 'soi_qty' => $qty, + 'soi_amount' => $amount, + 'soi_box_count' => $boxCount, + 'soi_pack_count' => $packCount, + 'soi_sheet_count' => $sheetCount, ]); $totalQty += $qty; @@ -137,18 +144,19 @@ class ShopOrder extends BaseController $this->orderModel->update($soIdx, ['so_total_qty' => $totalQty, 'so_total_amount' => $totalAmt]); $db->transComplete(); - return redirect()->to(site_url('admin/shop-orders'))->with('success', '주문이 접수되었습니다.'); + return redirect()->to(mgmt_url('shop-orders'))->with('success', '주문이 접수되었습니다.'); } public function cancel(int $id) { helper('admin'); $order = $this->orderModel->find($id); - if (!$order || (int) $order->so_lg_idx !== admin_effective_lg_idx()) { - return redirect()->to(site_url('admin/shop-orders'))->with('error', '주문을 찾을 수 없습니다.'); + if (! $order || (int) $order->so_lg_idx !== admin_effective_lg_idx()) { + return redirect()->to(mgmt_url('shop-orders'))->with('error', '주문을 찾을 수 없습니다.'); } $this->orderModel->update($id, ['so_status' => 'cancelled']); - return redirect()->to(site_url('admin/shop-orders'))->with('success', '주문이 취소되었습니다.'); + + return redirect()->to(mgmt_url('shop-orders'))->with('success', '주문이 취소되었습니다.'); } } diff --git a/app/Controllers/Bag.php b/app/Controllers/Bag.php index 117f5d8..ae75b80 100644 --- a/app/Controllers/Bag.php +++ b/app/Controllers/Bag.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Controllers; use CodeIgniter\Database\Exceptions\DatabaseException; +use CodeIgniter\HTTP\RedirectResponse; use App\Models\BagInventoryModel; use App\Models\BagIssueModel; use App\Models\BagOrderModel; @@ -19,6 +20,7 @@ use App\Models\PackagingUnitModel; use App\Models\SalesAgencyModel; use App\Models\ShopOrderModel; use App\Models\DesignatedShopModel; +use App\Models\LocalGovernmentModel; use Config\Roles; class Bag extends BaseController @@ -41,30 +43,303 @@ class Bag extends BaseController } // ────────────────────────────────────────────── - // 기본정보관리 + // 기본정보관리 (단가·포장 단위 진입 허브) // ────────────────────────────────────────────── public function basicInfo(): string { - $lgIdx = $this->lgIdx(); - $data = [ - 'bagPrices' => [], - 'packagingUnits' => [], - ]; + return $this->render('기본정보관리', 'bag/basic_info', []); + } - if ($lgIdx) { - try { - $data['bagPrices'] = model(BagPriceModel::class)->where('bp_lg_idx', $lgIdx)->orderBy('bp_bag_code', 'ASC')->findAll(); - } catch (DatabaseException $e) { - log_message('error', '[basicInfo] bag_price 조회 실패(테이블 미생성 등): ' . $e->getMessage()); + /** 봉투 단가 조회 (사이트) — 기간·봉투구분·봉투코드 필터, 적용기간 겹침, 페이징·인쇄 */ + public function prices(): string|RedirectResponse + { + helper('admin'); + if ($this->request->is('post')) { + $post = $this->request->getPost(); + $pick = static function (array $src, string $key): ?string { + if (! array_key_exists($key, $src)) { + return null; + } + $v = $src[$key]; + if ($v === null || is_array($v)) { + return null; + } + $s = trim((string) $v); + + return $s === '' ? null : $s; + }; + session()->setFlashdata('bag_prices_filter', [ + 'start_y' => $pick($post, 'start_y'), + 'start_m' => $pick($post, 'start_m'), + 'start_d' => $pick($post, 'start_d'), + 'end_y' => $pick($post, 'end_y'), + 'end_m' => $pick($post, 'end_m'), + 'end_d' => $pick($post, 'end_d'), + 'bag_kind_e' => $pick($post, 'bag_kind_e'), + 'bag_code' => $pick($post, 'bag_code'), + ]); + + return redirect()->to(site_url('bag/prices')); + } + + $lgIdx = $this->lgIdx(); + $bagPrices = []; + + $get = $this->request->getGet(); + $flash = session()->getFlashdata('bag_prices_filter'); + + $readSrc = static function (array $src, string $key): ?string { + if (! array_key_exists($key, $src)) { + return null; } - try { - $data['packagingUnits'] = model(PackagingUnitModel::class)->where('pu_lg_idx', $lgIdx)->orderBy('pu_bag_code', 'ASC')->findAll(); - } catch (DatabaseException $e) { - log_message('error', '[basicInfo] packaging_unit 조회 실패: ' . $e->getMessage()); + $v = $src[$key]; + if ($v === null || is_array($v)) { + return null; + } + $s = trim((string) $v); + + return $s === '' ? null : $s; + }; + + $filterKeys = [ + 'start_y', 'start_m', 'start_d', + 'end_y', 'end_m', 'end_d', + 'bag_kind_e', 'bag_code', + 'start_date', 'end_date', + ]; + $hasExplicitGetFilter = false; + foreach ($filterKeys as $fk) { + $v = $get[$fk] ?? null; + if ($v !== null && ! is_array($v) && trim((string) $v) !== '') { + $hasExplicitGetFilter = true; + break; } } - return $this->render('기본정보관리', 'bag/basic_info', $data); + $src = []; + if ($hasExplicitGetFilter) { + $src = $get; + } elseif (is_array($flash)) { + $src = $flash; + } + + $sy = $readSrc($src, 'start_y'); + $sm = $readSrc($src, 'start_m'); + $sd = $readSrc($src, 'start_d'); + $ey = $readSrc($src, 'end_y'); + $em = $readSrc($src, 'end_m'); + $ed = $readSrc($src, 'end_d'); + + $startDate = null; + if ($sy !== null && $sy !== '' && $sm !== null && $sm !== '' && $sd !== null && $sd !== '') { + $startDate = parse_ymd_from_triple($sy, $sm, $sd); + } + if ($startDate === null) { + $g = $readSrc($src, 'start_date'); + $startDate = ($g !== null && $g !== '') ? $g : null; + } + + $endDate = null; + if ($ey !== null && $ey !== '' && $em !== null && $em !== '' && $ed !== null && $ed !== '') { + $endDate = parse_ymd_from_triple($ey, $em, $ed); + } + if ($endDate === null) { + $g = $readSrc($src, 'end_date'); + $endDate = ($g !== null && $g !== '') ? $g : null; + } + + $startParts = ['y' => '', 'm' => '', 'd' => '']; + $endParts = ['y' => '', 'm' => '', 'd' => '']; + if ($startDate !== null && preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $startDate, $m)) { + $startParts = ['y' => $m[1], 'm' => (int) $m[2], 'd' => (int) $m[3]]; + } elseif ($sy !== null && $sm !== null && $sd !== null && $sy !== '' && $sm !== '' && $sd !== '') { + $iy = (int) $sy; + $im = (int) $sm; + $id = (int) $sd; + if ($iy >= 1000 && $iy <= 9999 && $im >= 1 && $im <= 12 && $id >= 1 && $id <= 31) { + $startParts = ['y' => (string) $iy, 'm' => $im, 'd' => $id]; + } + } + if ($endDate !== null && preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $endDate, $m)) { + $endParts = ['y' => $m[1], 'm' => (int) $m[2], 'd' => (int) $m[3]]; + } elseif ($ey !== null && $em !== null && $ed !== null && $ey !== '' && $em !== '' && $ed !== '') { + $iy = (int) $ey; + $im = (int) $em; + $id = (int) $ed; + if ($iy >= 1000 && $iy <= 9999 && $im >= 1 && $im <= 12 && $id >= 1 && $id <= 31) { + $endParts = ['y' => (string) $iy, 'm' => $im, 'd' => $id]; + } + } + + $dateYearMin = (int) date('Y') - 12; + $dateYearMax = (int) date('Y') + 2; + + $bagKindE = $readSrc($src, 'bag_kind_e'); + $bagCode = $readSrc($src, 'bag_code'); + $pager = null; + $bagCodes = []; + $bagKindOpts = []; + $printLines = []; + $printLgName = ''; + + if ($lgIdx !== null) { + try { + $priceModel = model(BagPriceModel::class); + $builder = $priceModel->where('bp_lg_idx', $lgIdx); + + if (($startDate !== null && $startDate !== '') || ($endDate !== null && $endDate !== '')) { + $qStart = ($startDate !== null && $startDate !== '') ? $startDate : $endDate; + $qEnd = ($endDate !== null && $endDate !== '') ? $endDate : $startDate; + if (strcmp((string) $qStart, (string) $qEnd) > 0) { + [$qStart, $qEnd] = [$qEnd, $qStart]; + } + $builder->where('bp_start_date <=', $qEnd); + $builder->groupStart() + ->where('bp_end_date IS NULL') + ->orWhere('bp_end_date >=', $qStart) + ->groupEnd(); + } + + if ($bagKindE !== null && $bagKindE !== '') { + $ek = model(CodeKindModel::class)->where('ck_code', 'E')->first(); + if ($ek) { + $eDetail = model(CodeDetailModel::class) + ->where('cd_ck_idx', (int) $ek->ck_idx) + ->where('cd_code', $bagKindE) + ->where('cd_state', 1) + ->first(); + if ($eDetail !== null) { + $builder->like('bp_bag_code', (string) $bagKindE, 'after'); + } + } + } + + if ($bagCode !== null && $bagCode !== '') { + $ok = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + if ($ok) { + $oDetail = model(CodeDetailModel::class)->findResolvedByKindAndCode((int) $ok->ck_idx, (string) $bagCode, $lgIdx); + if ($oDetail !== null) { + $builder->where('bp_bag_code', $bagCode); + } + } + } + + $bagPrices = $builder->orderBy('bp_bag_code', 'ASC')->orderBy('bp_start_date', 'DESC')->paginate(20); + + $queryForPager = []; + $tripleS = $sy !== null && $sy !== '' && $sm !== null && $sm !== '' && $sd !== null && $sd !== ''; + $tripleE = $ey !== null && $ey !== '' && $em !== null && $em !== '' && $ed !== null && $ed !== ''; + if ($tripleS) { + $queryForPager['start_y'] = $sy; + $queryForPager['start_m'] = $sm; + $queryForPager['start_d'] = $sd; + } else { + $legacyS = $readSrc($src, 'start_date'); + if ($legacyS !== null) { + $queryForPager['start_date'] = $legacyS; + } + } + if ($tripleE) { + $queryForPager['end_y'] = $ey; + $queryForPager['end_m'] = $em; + $queryForPager['end_d'] = $ed; + } else { + $legacyE = $readSrc($src, 'end_date'); + if ($legacyE !== null) { + $queryForPager['end_date'] = $legacyE; + } + } + if ($bagKindE !== null && $bagKindE !== '') { + $queryForPager['bag_kind_e'] = $bagKindE; + } + if ($bagCode !== null && $bagCode !== '') { + $queryForPager['bag_code'] = $bagCode; + } + $queryForPager = array_filter( + $queryForPager, + static fn ($v) => $v !== null && $v !== '' + ); + $pagerPath = site_url('bag/prices'); + if ($queryForPager !== []) { + $pagerPath .= '?' . http_build_query($queryForPager); + } + $priceModel->pager->setPath($pagerPath); + $pager = $priceModel->pager; + + $kindO = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $bagCodes = $kindO + ? model(CodeDetailModel::class)->getByKind((int) $kindO->ck_idx, true, $lgIdx) + : []; + + $kindE = model(CodeKindModel::class)->where('ck_code', 'E')->first(); + $bagKindOpts = $kindE + ? model(CodeDetailModel::class)->getByKind((int) $kindE->ck_idx, true, null) + : []; + + $lgRow = model(LocalGovernmentModel::class)->find($lgIdx); + $printLgName = $lgRow !== null ? $lgRow->lg_name : ''; + } catch (DatabaseException $e) { + log_message('error', '[prices] bag_price 조회 실패: ' . $e->getMessage()); + } + } + + if (($startDate !== null && $startDate !== '') || ($endDate !== null && $endDate !== '')) { + $qs = ($startDate !== null && $startDate !== '') ? $startDate : $endDate; + $qe = ($endDate !== null && $endDate !== '') ? $endDate : $startDate; + if (strcmp((string) $qs, (string) $qe) > 0) { + [$qs, $qe] = [$qe, $qs]; + } + $printLines[] = '조회기간(적용기간 겹침): ' . format_ymd_korean($qs) . ' ~ ' . format_ymd_korean($qe); + } + if ($bagKindE !== null && $bagKindE !== '') { + foreach ($bagKindOpts as $cd) { + if ((string) $cd->cd_code === (string) $bagKindE) { + $printLines[] = '봉투구분: ' . $cd->cd_name . ' (' . $bagKindE . ')'; + break; + } + } + } + if ($bagCode !== null && $bagCode !== '') { + $printLines[] = '봉투코드: ' . $bagCode; + } + + $viewData = [ + 'lgIdx' => $lgIdx, + 'bagPrices' => $bagPrices, + 'pager' => $pager, + 'startDate' => $startDate, + 'endDate' => $endDate, + 'startParts' => $startParts, + 'endParts' => $endParts, + 'dateYearMin' => $dateYearMin, + 'dateYearMax' => $dateYearMax, + 'bag_kind_e' => $bagKindE, + 'bag_code' => $bagCode, + 'bag_codes' => $bagCodes, + 'bag_kind_options' => $bagKindOpts, + 'printExtraLines' => $printLines, + ]; + if ($printLgName !== '') { + $viewData['printLgName'] = $printLgName; + } + + return $this->render('봉투 단가', 'bag/prices', $viewData); + } + + /** 포장 단위 조회 (사이트) */ + public function packagingUnits(): string + { + $lgIdx = $this->lgIdx(); + $packagingUnits = []; + if ($lgIdx) { + try { + $packagingUnits = model(PackagingUnitModel::class)->where('pu_lg_idx', $lgIdx)->orderBy('pu_bag_code', 'ASC')->findAll(); + } catch (DatabaseException $e) { + log_message('error', '[packagingUnits] packaging_unit 조회 실패: ' . $e->getMessage()); + } + } + + return $this->render('포장 단위', 'bag/packaging_units', ['packagingUnits' => $packagingUnits]); } /** @@ -75,16 +350,20 @@ class Bag extends BaseController $kindModel = model(CodeKindModel::class); $detailModel = model(CodeDetailModel::class); $kinds = $kindModel->orderBy('ck_code', 'ASC')->findAll(); + $lgIdx = $this->lgIdx(); $countMap = []; foreach ($kinds as $row) { - // countAllResults() 기본값(true)으로 매번 빌더 초기화 — false 시 where 누적되어 2번째부터 0건으로 보임 - $countMap[$row->ck_idx] = (int) $detailModel->where('cd_ck_idx', $row->ck_idx)->countAllResults(); + $countMap[$row->ck_idx] = (int) $detailModel->where('cd_ck_idx', $row->ck_idx) + ->filterByTenantScope($lgIdx) + ->countAllResults(); } + $level = (int) session()->get('mb_level'); + return $this->render('기본코드관리', 'bag/code_kinds', [ - 'codeKinds' => $kinds, - 'countMap' => $countMap, - 'canManage' => Roles::canManageCodeMaster((int) session()->get('mb_level')), + 'codeKinds' => $kinds, + 'countMap' => $countMap, + 'canManageKinds' => Roles::canManageCodeKindMaster($level), ]); } @@ -100,17 +379,31 @@ class Bag extends BaseController return redirect()->to(site_url('bag/code-kinds'))->with('error', '코드 종류를 찾을 수 없습니다.'); } - $list = $detailModel->where('cd_ck_idx', $ckIdx)->orderBy('cd_sort', 'ASC')->orderBy('cd_idx', 'ASC')->paginate(20); + $lgIdx = $this->lgIdx(); + $list = $detailModel->where('cd_ck_idx', $ckIdx) + ->filterByTenantScope($lgIdx) + ->orderBy('cd_sort', 'ASC') + ->orderBy('cd_idx', 'ASC') + ->paginate(20); $pager = $detailModel->pager; - $canManage = Roles::canManageCodeMaster((int) session()->get('mb_level')); - $title = ($canManage ? '세부코드 관리' : '세부코드 조회') . ' — ' . $kind->ck_name . ' (' . $kind->ck_code . ')'; + helper('admin'); + $level = (int) session()->get('mb_level'); + $adminLg = admin_effective_lg_idx(); + $canManage = Roles::canManageCodeMaster($level); + $rowCanEdit = []; + foreach ($list as $row) { + $rowCanEdit[$row->cd_idx] = Roles::canEditCodeDetailRow($level, $row, $adminLg); + } + + $title = ($canManage ? '세부코드 관리' : '세부코드 조회') . ' — ' . $kind->ck_name . ' (' . $kind->ck_code . ')'; return $this->render($title, 'bag/code_details', [ - 'kind' => $kind, - 'list' => $list, - 'pager' => $pager, - 'canManage' => $canManage, + 'kind' => $kind, + 'list' => $list, + 'pager' => $pager, + 'canManage' => $canManage, + 'rowCanEdit' => $rowCanEdit, ]); } @@ -372,8 +665,8 @@ class Bag extends BaseController // --- 불출 등록 --- public function issueCreate(): string { - $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $bagCodes = $kind ? model(CodeDetailModel::class)->where('cd_ck_idx', $kind->ck_idx)->where('cd_state', 1)->orderBy('cd_sort')->findAll() : []; + $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true, $this->lgIdx()) : []; return $this->render('불출 처리', 'bag/create_bag_issue', compact('bagCodes')); } @@ -403,8 +696,10 @@ class Bag extends BaseController { helper('admin'); $lgIdx = $this->lgIdx(); - $companies = $lgIdx ? model(CompanyModel::class)->where('cp_lg_idx', $lgIdx)->where('cp_type', 'manufacturer')->where('cp_state', 1)->findAll() : []; - $agencies = $lgIdx ? model(SalesAgencyModel::class)->where('sa_lg_idx', $lgIdx)->where('sa_state', 1)->findAll() : []; + $companies = $lgIdx + ? model(CompanyModel::class)->where('cp_lg_idx', $lgIdx)->whereIn('cp_type', ['제작업체', 'manufacturer'])->where('cp_state', 1)->findAll() + : []; + $agencies = $lgIdx ? model(SalesAgencyModel::class)->where('sa_lg_idx', $lgIdx)->orderForDisplay()->findAll() : []; $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); $bagCodes = $kind ? model(CodeDetailModel::class)->where('cd_ck_idx', $kind->ck_idx)->where('cd_state', 1)->orderBy('cd_sort')->findAll() : []; return $this->render('발주 등록', 'bag/create_bag_order', compact('companies', 'agencies', 'bagCodes')); @@ -421,6 +716,26 @@ class Bag extends BaseController return redirect()->to(site_url('bag/purchase-inbound'))->with('success', '발주 등록되었습니다.'); } + public function orderCancel(int $id) + { + helper('admin'); + $lgIdx = $this->lgIdx(); + if (!$lgIdx) { + return redirect()->to(site_url('bag/purchase-inbound'))->with('error', '지자체를 확인할 수 없습니다.'); + } + $orderModel = model(BagOrderModel::class); + $order = $orderModel->find($id); + if (!$order || (int) $order->bo_lg_idx !== $lgIdx) { + return redirect()->to(site_url('bag/purchase-inbound'))->with('error', '발주를 찾을 수 없습니다.'); + } + $before = (array) $order; + $orderModel->update($id, ['bo_status' => 'cancelled', 'bo_moddate' => date('Y-m-d H:i:s')]); + helper('audit'); + audit_log('update', 'bag_order', $id, $before, ['bo_status' => 'cancelled']); + + return redirect()->to(site_url('bag/purchase-inbound'))->with('success', '발주가 취소되었습니다.'); + } + // --- 입고 처리 --- public function receivingCreate(): string { @@ -447,8 +762,8 @@ class Bag extends BaseController helper('admin'); $lgIdx = $this->lgIdx(); $shops = $lgIdx ? model(DesignatedShopModel::class)->where('ds_lg_idx', $lgIdx)->where('ds_state', 1)->findAll() : []; - $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $bagCodes = $kind ? model(CodeDetailModel::class)->where('cd_ck_idx', $kind->ck_idx)->where('cd_state', 1)->orderBy('cd_sort')->findAll() : []; + $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true, $lgIdx) : []; return $this->render('판매 등록', 'bag/create_bag_sale', compact('shops', 'bagCodes')); } @@ -469,8 +784,8 @@ class Bag extends BaseController helper('admin'); $lgIdx = $this->lgIdx(); $shops = $lgIdx ? model(DesignatedShopModel::class)->where('ds_lg_idx', $lgIdx)->where('ds_state', 1)->findAll() : []; - $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); - $bagCodes = $kind ? model(CodeDetailModel::class)->where('cd_ck_idx', $kind->ck_idx)->where('cd_state', 1)->orderBy('cd_sort')->findAll() : []; + $kind = model(CodeKindModel::class)->where('ck_code', 'O')->first(); + $bagCodes = $kind ? model(CodeDetailModel::class)->getByKind((int) $kind->ck_idx, true, $lgIdx) : []; return $this->render('주문 접수', 'bag/create_shop_order', compact('shops', 'bagCodes')); } diff --git a/app/Views/admin/bag_inventory/index.php b/app/Views/admin/bag_inventory/index.php index f4891af..330f7bb 100644 --- a/app/Views/admin/bag_inventory/index.php +++ b/app/Views/admin/bag_inventory/index.php @@ -3,7 +3,7 @@
diff --git a/app/Views/admin/bag_issue/create.php b/app/Views/admin/bag_issue/create.php index e064722..781e1b1 100644 --- a/app/Views/admin/bag_issue/create.php +++ b/app/Views/admin/bag_issue/create.php @@ -2,7 +2,7 @@ 무료용 불출 처리- 기본코드 종류·세부코드는 상단 메뉴 기본정보관리 → - 기본코드관리에서 확인할 수 있습니다. -
- - -| 번호 | 봉투코드 | 봉투명 | 발주단가 | 도매가 | 소비자가 | 적용시작 | 적용종료 | 상태 | -
|---|---|---|---|---|---|---|---|---|
| = $i + 1 ?> | -= esc($row->bp_bag_code) ?> | -= esc($row->bp_bag_name ?? '') ?> | -= number_format((float)($row->bp_order_price ?? 0)) ?> | -= number_format((float)($row->bp_wholesale_price ?? 0)) ?> | -= number_format((float)($row->bp_consumer_price ?? 0)) ?> | -= esc($row->bp_start_date ?? '') ?> | -= ($row->bp_end_date ?? '') ?: '현재' ?> | -= ($row->bp_status ?? 'active') === 'active' ? '사용' : '만료' ?> | -
| 등록된 단가 정보가 없습니다. | ||||||||
| 번호 | 봉투코드 | 봉투명 | 박스당 팩 수 | 팩당 낱장 수 | 1박스 총 낱장 | 적용시작 | 적용종료 | 상태 | -
|---|---|---|---|---|---|---|---|---|
| = $i + 1 ?> | -= esc($row->pu_bag_code) ?> | -= esc($row->pu_bag_name ?? '') ?> | -= number_format((int)($row->pu_packs_per_box ?? 0)) ?> | -= number_format((int)($row->pu_sheets_per_pack ?? 0)) ?> | -= number_format((int)($row->pu_packs_per_box ?? 0) * (int)($row->pu_sheets_per_pack ?? 0)) ?> | -= esc($row->pu_start_date ?? '') ?> | -= ($row->pu_end_date ?? '') ?: '현재' ?> | -= ($row->pu_status ?? 'active') === 'active' ? '사용' : '만료' ?> | -
| 등록된 포장 단위가 없습니다. | ||||||||
기본 정보 조회 메뉴입니다. 항목을 선택하세요.
+