-
Notifications
You must be signed in to change notification settings - Fork 0
/
freee_util.py
228 lines (177 loc) · 6.08 KB
/
freee_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import csv
from dataclasses import dataclass
from datetime import datetime
from decimal import Decimal
transfer_headers = [
"日付", # Date
"借方勘定科目", # Debit Account
"借方品目", # Debit Item
"借方金額", # Debit Amount
"借方税区分", # Debit Tax Segment
"借方税額", # Debit Tax Amount
"貸方勘定科目", # Credit Account
"貸方品目", # Credit Item
"貸方金額", # Credit Amount
"貸方税区分", # Credit Tax Segment
"貸方税額", # Credit Tax Amount
"摘要", # Remarks
]
TAX_NA = "対象外" # Not Applicable
TAX_EXPORT_SALE = "輸出売返"
TAX_PURCHASE = "課対仕入 10%"
@dataclass
class TransferDetail:
account: str
item: str
amount: Decimal
tax_segment: str
tax_amount: Decimal
def to_list(self):
return [
self.account,
self.item,
self.amount,
self.tax_segment,
self.tax_amount
]
@dataclass
class Transfer:
date: datetime
debit: TransferDetail
credit: TransferDetail
remarks: str
def get_header(self):
return transfer_headers
def to_list(self):
header = [ self.date.strftime("%Y-%m-%d") ]
footer = [ self.remarks ]
return header + self.debit.to_list() + self.credit.to_list() + footer
def make_xlsx(filename, itemlist):
if itemlist:
wb = xlsxwriter.Workbook(filename)
ws = wb.add_worksheet()
ws.write_row(0, 0, itemlist[0].get_header())
for idx, item in enumerate(itemlist):
ws.write_row(idx + 1, 0, item.to_list())
wb.close()
def make_new_xlsx(filename, itemlist):
if itemlist:
wb = xlsxwriter.Workbook(filename)
ws = wb.add_worksheet()
ws.write_row(0, 0, itemlist[0].get_new_header())
for idx, item in enumerate(itemlist):
ws.write_row(idx + 1, 0, item.to_new_list())
wb.close()
def make_new_csv(filename, itemlist):
if itemlist:
with open(filename, "w", newline='', encoding='utf-8') as of:
writer = csv.writer(of)
writer.writerow(itemlist[0].get_new_header())
writer.writerows([i.to_new_list() for i in itemlist])
expendit_headers = [
"発生日", # Accrual Date
#"決済期日", # Settlement Date
"収支区分", # category
"取引先", # Client
"勘定科目", # reason?
"決済口座", # Settlement Account
"金額", # Amount
"税区分", # Tax Segment
"備考", # Remarks
]
expendit_new_headers = [
"取引日", # Accrual Date
"入金額", # Amount
"残高", # remaining balance
"取引内容", # transaction details
]
EXP_COMMISSION_PAID = "支払手数料"
EXP_OWNER_LOAN = "事業主貸"
EXP_MISC = "雑費"
EXP_FX_LOSS = "為替差損"
EXP_EXPENDIBLES = "消耗品費"
EXP_COMMUNICATION = "通信費"
@dataclass
class Expenditure:
date: datetime
client: str
category: str
account: str
amount: Decimal
tax_segment: str
remarks: str
balance: Decimal
def get_header(self):
return expendit_headers
def get_new_header(self):
return expendit_new_headers
def to_list(self):
header = [ self.date.strftime("%Y-%m-%d"), "支出" ]
center = [
self.client,
self.category,
self.account,
self.amount,
self.tax_segment
]
footer = [ self.remarks ]
return header + center + footer
def to_new_list(self):
return [
self.date.strftime("%Y-%m-%d"),
-self.amount,
self.balance,
f"{self.category} - {self.remarks} - {self.client}"
]
def expend_private(date, client, account, amount, remarks, balance):
return Expenditure(date, client, EXP_OWNER_LOAN, account, amount, TAX_NA, remarks, balance)
def expend_fx(date, client, account, amount, remarks, balance):
return Expenditure(date, client, EXP_MISC, account, amount, TAX_NA, f"{EXP_FX_LOSS} {remarks}", balance)
def expend_purchase(date, client, account, amount, remarks, balance):
return Expenditure(date, client, EXP_EXPENDIBLES, account, amount, TAX_PURCHASE, remarks, balance)
def expend_communication(date, client, account, amount, remarks, balance):
return Expenditure(date, client, EXP_COMMUNICATION, account, amount, TAX_PURCHASE, remarks, balance)
def expend_travel(date, client, account, amount, remarks, balance):
return Expenditure(date, client, EXP_COMMUNICATION, account, amount, TAX_PURCHASE, remarks, balance)
INC_ONWER_LOAN = "事業主借"
INC_MISC = "雑収入"
INC_FX_GAIN = "為替差益"
INC_SALE = "売上高"
@dataclass
class Income:
date: datetime
client: str
category: str
account: str
amount: Decimal
tax_segment: str
remarks: str
balance: Decimal
def get_header(self):
return expendit_headers
def get_new_header(self):
return expendit_new_headers
def to_list(self):
header = [ self.date.strftime("%Y-%m-%d"), "収入" ]
center = [
self.client,
self.category,
self.account,
self.amount,
self.tax_segment
]
footer = [ self.remarks ]
return header + center + footer
def to_new_list(self):
return [
self.date.strftime("%Y-%m-%d"),
self.amount,
self.balance,
f"{self.category} - {self.remarks} - {self.client}"
]
def income_sale_export(date, client, account, amount, remarks, balance):
return Income(date, client, INC_SALE, account, amount, TAX_EXPORT_SALE, remarks, balance)
def income_private(date, client, account, amount, remarks, balance):
return Income(date, client, INC_ONWER_LOAN, account, amount, TAX_NA, remarks, balance)
def income_fx(date, client, account, amount, remarks, balance):
return Income(date, client, INC_MISC, account, amount, TAX_NA, f"{INC_FX_GAIN} {remarks}", balance)