$label) {
$billing_month = "";
if ($key == PAYMENT['BILLING']['PREPAYMENT']) { //선결제의 우우
$initalTitle = $viewDatas['formDatas']['title'] ?? "";
$initalAmount = $viewDatas['formDatas']['amount'] ?? 0;
$initalPayMonth = $viewDatas['formDatas']['billing_month'] ?? "";
$initalBillingAt = $viewDatas['formDatas']['billing_at'] ?? "";
$months = ["" => "개월 선택"];
for ($i = 2; $i <= 12; $i++) {
$months[$i] = "{$i}개월";
}
$js_onChange = "
var originalText = '{$initalTitle}';
var originalAmount = parseInt({$initalAmount});
var originalBillingAt = '{$initalBillingAt}';
var originalPayMonth = '{$initalPayMonth}';
var payMonth = parseInt(this.options[this.selectedIndex].value) || 1; // 숫자가 아니면 1로 처리
// 선택된 개월 수에 따라 제목과 금액 결제일 업데이트
document.querySelector('input[name=\'title\']').value = originalText + ' '+payMonth + '개월';
document.querySelector('input[name=\'amount\']').value = originalAmount * payMonth;
// billing_at 날짜 계산
var parts = originalBillingAt.split('-'); // 예: ['2025', '1', '25']
// 1. Date 객체 생성 시 월(parts[1])에서 1을 빼줍니다. JavaScript의 월 인덱스는 0부터 시작하기 때문입니다 (0=1월, 1=2월...).
var year = parseInt(parts[0]);
var monthIndex = parseInt(parts[1]) - 1;
var day = parseInt(parts[2]);
var newDate = new Date(year, monthIndex, day);
// 2. setMonth 로직 수정:
// month 변수 (1~12)를 더하는 것이 아니라,
// 현재 월 인덱스에 선택된 개월 수를 더해야 합니다.
// getMonth()는 현재 인덱스를 반환하며, month는 추가할 개월 수입니다.
// (예: 1월(index 0)에 1개월(month 1) 추가 -> newDate.setMonth(0 + 1))
newDate.setMonth(newDate.getMonth() + payMonth);
// 3. YYYY-MM-DD 형식으로 다시 포맷팅 (toISOString 사용 시 타임존 오류 발생 방지)
var formattedDate = newDate.getFullYear() + '-' +
('0' + (newDate.getMonth() + 1)).slice(-2) + '-' + // 월을 1부터 시작하게 하고 2자리로 포맷
('0' + newDate.getDate()).slice(-2); // 일을 2자리로 포맷
document.querySelector('input[name=\'billing_at\']').value = formattedDate;
";
$dropdown_attrs = ['id' => 'billing_month', 'onChange' => $js_onChange];
$billing_month = form_dropdown('billing_month', $months, $viewDatas['billing_month'] ?? '', $dropdown_attrs);
}
$radio_attrs = ['id' => $field . '_' . $key];
if ($key == $value) {
$radio_attrs['checked'] = true;
}
$forms[] = form_radio($field, $key, false, $radio_attrs) . $label . " " . $billing_month;
}
$form = implode(" ", $forms);
break;
case 'pay':
$forms = [];
array_shift($viewDatas['formOptions'][$field]['options']);
foreach ($viewDatas['formOptions'][$field]['options'] as $key => $label) {
$forms[] = form_radio($field, $key, $key == $value, $extras) . $label;
}
$form = implode(" ", $forms);
break;
default:
$form = parent::getFieldForm($field, $value, $viewDatas, $extras);
break;
}
return $form;
} //
public function getFieldView(string $field, mixed $value, array $viewDatas, array $extras = []): string|null
{
switch ($field) {
case "countdown": //결제일Countdown
$value = $viewDatas['entity']->getCountDueAt();
break;
case 'amount':
$value = sprintf("%s%s", number_format($value), $viewDatas['entity']->getPay() == 'coupon' ? "개" : "원");
break;
case 'billing':
$value = parent::getFieldView($field, $value, $viewDatas, $extras);
$value .= $viewDatas['entity']->getBilling() === 'prepayment' ? "/" . $viewDatas['entity']->getBillingMonth() . "개월" : "";
break;
default:
$value = parent::getFieldView($field, $value, $viewDatas, $extras);
break;
}
return $value;
}
public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
{
switch ($action) {
case 'create':
$action = "";
break;
case 'modify':
//역활이 보안관리자가 아니면 수정불가
if ($this->getAuthContext()->isAccessRole([ROLE['USER']['SECURITY']])) {
$action = parent::getListButton($action, $label, $viewDatas, $extras);
} else {
$oldBatchJobUids = old("batchjob_uids", null);
$oldBatchJobUids = is_array($oldBatchJobUids) ? $oldBatchJobUids : [$oldBatchJobUids];
$action = form_checkbox([
"id" => "checkbox_uid_{$viewDatas['entity']->getPK()}",
"name" => "batchjob_uids[]",
"value" => $viewDatas['entity']->getPK(),
"class" => "batchjobuids_checkboxs",
"checked" => in_array($viewDatas['entity']->getPK(), $oldBatchJobUids)
]);
$action .= $label;
}
break;
case 'delete':
//역활이 보안관리자가 아니면 삭제불가
$action = $this->getAuthContext()->isAccessRole([ROLE['USER']['SECURITY']]) ? parent::getListButton($action, $label, $viewDatas, $extras) : "";
break;
case 'invoice':
$action = form_submit($action . "_submit", $label ? $label : '청구서 발행', [
"formaction" => current_url() . '/' . $action,
"class" => "btn btn-sm btn-outline btn-primary",
]);
break;
case 'paid':
$action = "완료";
if ($viewDatas['entity']->getStatus() === STATUS['UNPAID']) {
$action = sprintf("%s", $viewDatas['entity']->getPK(), $label ? $label : '결제');
}
break;
default:
$action = parent::getListButton($action, $label, $viewDatas, $extras);
break;
}
return $action;
}
}