인수인계 없이 남은 PHP 주문 계산 코드 → 업무 규칙 문서 · Claude Code · Sonnet 5.5 · 생각 깊이 중간

order_calc.php

1<?php
2// 주문 금액 계산 (결제 페이지, 모바일 주문서 공용)
3// 사용: php order_calc.php order.json
4include "inc/config.php";
5include "inc/member.php";
6include "inc/util.php";
7
8$order = json_decode(file_get_contents($argv[1]), true);
9$NOW = isset($order['now']) ? strtotime($order['now']) : time();
10$GLOBALS['NOW'] = $NOW;
11$mem = $order['member'];
12
13// 모바일 주문은 무료배송 기준을 낮춤 (2013 모바일 프로모션, 박과장 요청)
14if ($order['channel'] == 'mobile') $CFG['free_ship'] = 30000;
15
16$goods_total = 0; // 정가 합계
17$sale_total = 0; // 할인 후 합계
18foreach ($order['items'] as $it) {
19 $line = $it['price'] * $it['qty'];
20 $goods_total += $line;
21 $rate = 0;
22 // 세일 상품은 등급 할인 제외
23 if (!$it['is_sale']) {
24 $rate = get_grade_rate($mem['grade']);
25 // VIP 생일 달 추가 할인
26 if ($mem['grade'] == 'VIP' && is_birth_month($mem['birth'])) $rate += 2;
27 }
28 // 같은 상품 10개 이상 대량 구매 5% 추가 할인
29 if ($it['qty'] > 10) $rate += 5;
30 $line = $line - floor10($line * $rate / 100);
31 $sale_total += $line;
32}
33
34// 2012 창립 이벤트 10% (이벤트 기간에만)
35if ($CFG['EVENT_2012'] && date('Y', $NOW) == '2012') {
36 $sale_total = $sale_total - floor10($sale_total * 0.1);
37}
38
39// 쿠폰 (정액)
40$coupon = 0;
41if (isset($order['coupon']) && $order['coupon'] > 0) {
42 if ($sale_total >= $CFG['coupon_min']) $coupon = $order['coupon'];
43}
44$pay = $sale_total - $coupon;
45if ($pay < 0) $pay = 0;
46
47// 배송비: 무료배송 판단은 정가 합계 기준
48$ship = ($goods_total >= $CFG['free_ship']) ? 0 : $CFG['ship_fee'];
49$ship += ship_extra($order['zip']);
50
51// 적립금 사용
52$use_point = 0;
53if ($mem['point'] >= $CFG['point_min'] && $order['use_point'] > 0) {
54 $max = floor100($pay * $CFG['point_max_rate']);
55 $use_point = min($order['use_point'], $mem['point'], $max);
56 $use_point = floor100($use_point);
57}
58$pay = $pay - $use_point;
59
60$final = floor10($pay + $ship);
61
62// 적립 예정: 적립금 쓴 만큼 뺀 상품 결제액 기준, 배송비 제외
63$save = floor10(($pay) * point_save_rate($mem['grade']) / 100);
64
65echo json_encode(array(
66 'goods_total' => $goods_total,
67 'sale_total' => $sale_total,
68 'coupon' => $coupon,
69 'ship' => $ship,
70 'use_point' => $use_point,
71 'final' => $final,
72 'save_point' => $save,
73 'ship_date' => ship_date($NOW),
74), JSON_UNESCAPED_UNICODE) . "\n";
75?>

inc/config.php

1<?php
2// 쇼핑몰 설정 (2009.03 김대리 작성, 2013.07 수정)
3$CFG = array();
4$CFG['ship_fee'] = 2500; // 기본 배송비
5$CFG['free_ship'] = 50000; // 무료배송 기준 금액
6$CFG['jeju_fee'] = 3000; // 제주 추가 배송비
7$CFG['island_fee'] = 5000; // 도서산간 추가 배송비
8$CFG['coupon_min'] = 30000; // 쿠폰 사용 최소 주문금액
9$CFG['point_min'] = 3000; // 적립금 최소 보유액
10$CFG['point_max_rate']= 0.3; // 적립금 1회 최대 사용 비율
11$CFG['EVENT_2012'] = true; // 2012 창립 이벤트
12
13// 도서산간 우편번호 앞자리 (택배사 목록 2011년 기준)
14$ISLAND_ZIP = array('23100','23101','23102','40200','52570','59100','59101');
15?>

inc/member.php

1<?php
2// 회원 등급 관련
3function get_grade_rate($grade) {
4 if ($grade == 'VIP') return 5;
5 else if ($grade == 'GOLD') return 3;
6 return 0;
7}
8
9function is_birth_month($birth) {
10 // birth: YYYY-MM-DD
11 if (!$birth) return false;
12 return substr($birth, 5, 2) == date('m', $GLOBALS['NOW']);
13}
14
15function point_save_rate($grade) {
16 if ($grade == 'VIP') return 2;
17 return 1;
18}
19?>

inc/util.php

1<?php
2function floor10($n) { return floor($n / 10) * 10; }
3function floor100($n) { return floor($n / 100) * 100; }
4
5function ship_extra($zip) {
6 global $CFG, $ISLAND_ZIP;
7 $z = intval($zip);
8 if ($z >= 63000 && $z <= 63644) return $CFG['jeju_fee'];
9 foreach ($ISLAND_ZIP as $p) {
10 if (substr($zip, 0, 5) == $p) return $CFG['island_fee'];
11 }
12 return 0;
13}
14
15// 출고일: 토요일·일요일 주문은 월요일, 평일 15시 이후 주문은 다음날
16function ship_date($ts) {
17 $w = date('w', $ts);
18 if ($w == 6) return date('Y-m-d', $ts + 86400 * 2);
19 if ($w == 0) return date('Y-m-d', $ts + 86400);
20 if (date('H', $ts) >= 15) {
21 if ($w == 5) return date('Y-m-d', $ts + 86400 * 3);
22 return date('Y-m-d', $ts + 86400);
23 }
24 return date('Y-m-d', $ts);
25}
26?>