Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Last active December 17, 2015 04:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mike-neck/5551749 to your computer and use it in GitHub Desktop.
fc2ブログがクソいので、記事をGroovyで書いている
import groovy.xml.MarkupBuilder
class Subject {
String item
int value
}
class Journal {
String item
Subject input
Subject output
String memo
static Journal newInstance(String itemName, String inputName, String outputName, int value, String memo) {
def input = new Subject(item : inputName, value : value)
def output = new Subject(item : outputName, value : value)
new Journal (item : itemName, input : input, output : output, memo : memo)
}
}
def w = new StringWriter()
def x = new MarkupBuilder(w)
def bdr = 'border : solid 2px #093; padding : 4px;'
def ths = "background : rgba( 0, 102, 51, 0.7); color : #fff; ${bdr} text-align : center;"
def tds = "background : rgba(255, 255, 255, 0.7); ${bdr}"
def num = "${tds} text-align : right;"
def trs = ['background-color : #fff;', 'background-color : #cff;']
def data = []
4.times {indx ->
data << Journal.newInstance("仕入れ - ${indx + 1}回目", '銀の鉱石', '現金', 8900, '99個/単価89.9')
}
33.times {indx ->
data << Journal.newInstance("出品 - ${indx + 1}回目" as String, '手数料', '現金', 10, '出品手数料 : 10')
data << Journal.newInstance("売上 - ${indx + 1}回目" as String, '仕入費用', '銀の鉱石', 1078 + (indx % 5 == 0? 0 : 1), '12個')
data << Journal.newInstance("売上 - ${indx + 1}回目" as String, '現金', '売上', 1305, '出品額 : 1305')
data << Journal.newInstance("売上 - ${indx + 1}回目" as String, '手数料', '現金', 65, '売上手数料5%(65)')
}
def debit = [:].withDefault {0}
def credit = [:].withDefault {0}
x.h4('仕訳詳細')
x.br()
x.table(id : 'bookkeeping-detail', style : 'border-collapse : collapse; margin : 5px;'){
tr {
th(style : "width :120px; ${ths}", '摘要')
th(style : "width : 70px; ${ths}", '借方')
th(style : "width : 70px; ${ths}", '借方金額')
th(style : "width : 70px; ${ths}", '貸方')
th(style : "width : 70px; ${ths}", '貸方金額')
th(style : "width :150px; ${ths}", 'メモ')
}
data.eachWithIndex {jnl, indx ->
tr(style : trs[(indx + 1) % 2]) {
td(style : tds, jnl.item)
td(style : tds, jnl.input.item)
td(style : num, jnl.input.value)
td(style : tds, jnl.output.item)
td(style : num, jnl.output.value)
td(style : tds, jnl.memo)
debit[jnl.input.item] += jnl.input.value
credit[jnl.output.item] += jnl.output.value
}
}
}
def summary = {dbit, crdt, html ->
def dlist = dbit.sort{it.key}.collect {[(it.key):it.value]}
def clist = crdt.sort{it.key}.collect {[(it.key):it.value]}
int size = [dlist.size(), clist.size()].max()
html.table (style : 'border-collapse : collapse; margin : 5px;') {
tr {
th(style : "width : 70px; ${ths}", '借方')
th(style : "width : 70px; ${ths}", '借方合計')
th(style : "width : 70px; ${ths}", '貸方')
th(style : "width : 70px; ${ths}", '貸方合計')
}
for (int indx = 0; indx < size; indx ++) {
def d = dlist[indx] ?: [(' ') : ' ']
def c = clist[indx] ?: [(' ') : ' ']
tr (style : trs[indx % 2]) {
def closure = {detail ->
def k = detail.key?: ' '
def v = detail.value?: ' '
td(style : tds, k)
td(style : num, v)
}
d.each closure
c.each closure
}
}
}
}
x.br()
x.h4('仕訳まとめ')
x.br()
summary (debit, credit, x)
def dbt = debit.collectEntries{[it.key, it.value]}
def crd = credit.collectEntries{[it.key, it.value]}
debit.each {
def key = it.key
def val = it.value
if (crd[key]) {
def diff = val - crd[key]
if (diff < 0) {
crd[key] = -diff
dbt.remove(key)
} else if (diff > 0) {
dbt[key] = diff
crd.remove(key)
} else {
dbt.remove(key)
crd.remove(key)
}
}
}
x.br()
x.h4('総勘定元帳')
x.br()
summary (dbt, crd, x)
def writer = new StringWriter()
w.toString().eachLine{writer << it.trim()}
new File('/Path/to/the/file.html').write(writer.toString().replace('<br />', '\n<br />\n'), 'UTF-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment