daily_report()27줄 · 주문 파일 읽기 + 수수료 계산
weekly_report()28줄 · 주문 파일 읽기 + 수수료 계산
monthly_report()27줄 · 주문 파일 읽기 + 수수료 계산
quarterly_report()34줄 · 주문 파일 읽기 + 수수료 계산
report.py 157줄 · git diff --stat: 2 files changed, 42 insertions(+), 1 deletion(-)
report.py 전체 보기
#!/usr/bin/env python3
"""모래톱 커피 매출 보고서.
사용법:
python3 report.py daily 2026-10-03 하루 보고서
python3 report.py weekly 2026-10-05 그날부터 7일 보고서
python3 report.py monthly 2026-10 한 달 보고서
python3 report.py quarterly 2026-Q4 한 분기 보고서 (Q1=1~3월 ... Q4=10~12월)
"""
import csv
import datetime
import re
import sys
SALES_FILE = "sales.csv"
def daily_report(day):
gross = 0
fee = 0
count = 0
with open(SALES_FILE, newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
if row["date"] != day:
continue
amount = int(row["qty"]) * int(row["unit_price"])
if row["channel"] == "card":
row_fee = round(amount * 0.022)
elif row["channel"] == "naverpay":
row_fee = round(amount * 0.033)
else:
row_fee = 0
if row["status"] == "refund":
gross -= amount
fee -= row_fee
else:
gross += amount
fee += row_fee
count += 1
print("[일간] " + day)
print("주문 수: " + str(count) + "건")
print("매출: " + format(gross, ",") + "원")
print("결제 수수료: " + format(fee, ",") + "원")
print("순매출: " + format(gross - fee, ",") + "원")
def weekly_report(start):
start_date = datetime.date.fromisoformat(start)
end_date = start_date + datetime.timedelta(days=6)
gross = 0
fee = 0
count = 0
with open(SALES_FILE, newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
d = datetime.date.fromisoformat(row["date"])
if d < start_date or d > end_date:
continue
if row["status"] == "refund":
continue
amount = int(row["qty"]) * int(row["unit_price"])
if row["channel"] == "card":
row_fee = round(amount * 0.022)
elif row["channel"] == "naverpay":
row_fee = round(amount * 0.033)
else:
row_fee = 0
gross += amount
fee += row_fee
count += 1
print("[주간] " + start + " ~ " + end_date.isoformat())
print("주문 수: " + str(count) + "건")
print("매출: " + format(gross, ",") + "원")
print("결제 수수료: " + format(fee, ",") + "원")
print("순매출: " + format(gross - fee, ",") + "원")
def monthly_report(month):
gross = 0
fee = 0
count = 0
with open(SALES_FILE, newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
if not row["date"].startswith(month + "-"):
continue
amount = int(row["qty"]) * int(row["unit_price"])
if row["channel"] == "card":
row_fee = round(amount * 0.022)
elif row["channel"] == "naverpay":
row_fee = round(amount * 0.033)
else:
row_fee = 0
if row["status"] == "refund":
gross -= amount
fee -= row_fee
else:
gross += amount
fee += row_fee
count += 1
print("[월간] " + month)
print("주문 수: " + str(count) + "건")
print("매출: " + format(gross, ",") + "원")
print("결제 수수료: " + format(fee, ",") + "원")
print("순매출: " + format(gross - fee, ",") + "원")
def quarterly_report(quarter):
m = re.fullmatch(r"(\d{4})-Q([1-4])", quarter)
if not m:
print("분기는 2026-Q4 처럼 적어 주세요 (Q1~Q4): " + quarter)
sys.exit(1)
year = m.group(1)
first_month = (int(m.group(2)) - 1) * 3 + 1
months = [year + "-" + format(first_month + i, "02d") for i in range(3)]
gross = 0
fee = 0
count = 0
with open(SALES_FILE, newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
if row["date"][:7] not in months:
continue
amount = int(row["qty"]) * int(row["unit_price"])
if row["channel"] == "card":
row_fee = round(amount * 0.022)
elif row["channel"] == "naverpay":
row_fee = round(amount * 0.033)
else:
row_fee = 0
if row["status"] == "refund":
gross -= amount
fee -= row_fee
else:
gross += amount
fee += row_fee
count += 1
print("[분기] " + quarter)
print("주문 수: " + str(count) + "건")
print("매출: " + format(gross, ",") + "원")
print("결제 수수료: " + format(fee, ",") + "원")
print("순매출: " + format(gross - fee, ",") + "원")
if __name__ == "__main__":
if len(sys.argv) != 3:
print(__doc__)
sys.exit(1)
kind, arg = sys.argv[1], sys.argv[2]
if kind == "daily":
daily_report(arg)
elif kind == "weekly":
weekly_report(arg)
elif kind == "monthly":
monthly_report(arg)
elif kind == "quarterly":
quarterly_report(arg)
else:
print(__doc__)
sys.exit(1)