summaryrefslogtreecommitdiff
path: root/laskuri.lua
blob: 8fc2de1bcdf88cb020cfe4e7df3b43f84d61a3f9 (plain)
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
local ftcsv = require("ftcsv")
local refnum = require("laskuri.refnum")
local utils = require("laskuri.utils")
local pprint = require("pprint")

dofile("settings.lua")

-- Load data from the file `invoices.csv`
local data = ftcsv.parse("invoices.csv", ",")

-- Do some preprocessing for each row

local num_fields = { "Amount", "Price", "Vat" }

for _, row in pairs(data) do
    -- Convert applicable fields to lua numbers
    for _, field in pairs(num_fields) do
        row[field] = tonumber(row[field])
    end

    -- Calculate tax and final price for each row
    row.Tax = row.Price * (row.Vat / 100.0)
    row.FinalPrice = row.Price + row.Tax
end

-- Group the data (rows) by the invoice number.
local invoices = utils.group_by(data, function(invoice)
    return invoice.InvoiceNr
end)

-- Calculate some needed data for each invoice including price, final price, due
-- date and reference number
for invoice_nr, rows in pairs(invoices) do
    local price = 0.0
    local final_price = 0.0
    local tax = 0.0

    for _, row in pairs(rows) do
        price = price + row.Price
        final_price = final_price + row.FinalPrice
        tax = tax + row.Tax
    end

    rows.data = {
        Price = price,
        FinalPrice = final_price,
        Tax = tax,
        Due = utils.due_date(14, date_fmt),
        ReferenceNum = refnum.refnum(invoice_nr),
    }
end

local today = os.date(date_fmt)

-- pprint.pprint(invoices)

function to_filename(s)
    return string.gsub(s, "[^%w]", "_")
end

for invoice_nr, invoice in pairs(invoices) do
    doc = Document:New()

    doc:SetCompressionMode("all")
    doc:UseUTFEncodings()

    page = doc:AddPage()

    page:SetSize("A4", "portrait")

    font = doc:LoadTTFontFromFile("fonts/Roboto/Roboto-Regular.ttf", true, "UTF-8")
    font_bold = doc:LoadTTFontFromFile("fonts/Roboto/Roboto-Bold.ttf", true, "UTF-8")

    page:CreateGrid(15, 50)
    -- page:DrawGrid()

    page:BeginText()

    page:SetFontAndSize(font, 12)
    page:TextCell(0, 0, 3, 1, invoicer, "left")

    page:SetFontAndSize(font, 10)
    page:TextCell(0, 1, 3, 1, address_line_1, "left")
    page:TextCell(0, 2, 3, 1, address_line_2, "left")

    page:SetFontAndSize(font_bold, 14)
    page:TextCell(8, 0, 3, 1, "LASKU", "left")

    page:SetFontAndSize(font, 9)
    page:TextCell(8, 2, 3, 1, "Laskun päiväys:", "left")
    page:TextCell(8, 3, 3, 1, today, "left")

    page:TextCell(8, 4, 3, 1, "Laskunumero:", "left")
    page:TextCell(8, 5, 3, 1, invoice_nr, "left")

    page:TextCell(8, 6, 3, 1, "Eräpäivä:", "left")
    page:TextCell(8, 7, 3, 1, invoice.data.Due, "left")

    page:SetFontAndSize(font_bold, 10)
    page:TextCell(0, 5, 3, 1, invoice[1].Customer, "left")

    page:SetFontAndSize(font, 10)
    page:TextCell(0,  10, 3, 1,  "Nimike", "left")
    page:TextCell(3,  10, 2, 1, "Määrä", "right")
    page:TextCell(5,  10, 2, 1,  "Yks.", "left")
    page:TextCell(7,  10, 2, 1, "A'hinta EUR", "right")
    page:TextCell(9,  10, 2, 1, "Alv %", "right")
    page:TextCell(11, 10, 3, 1, "Verollinen yht. EUR", "right")

    product_row = 11

    for idx = 1, #invoice do
        product = invoice[idx]
        local row = product_row + idx - 1
        page:TextCell(0, row, 3, 1, product.Product, "left")
        page:TextCell(3, row, 2, 1, string.format("%d", product.Amount), "right")
        page:TextCell(5, row, 2, 1, product.Unit, "left")
        page:TextCell(7, row, 2, 1, string.format("%.2f", product.Price), "right")
        page:TextCell(9, row, 2, 1, string.format("%d", product.Vat), "right")
        page:TextCell(11, row, 3, 1, string.format("%.2f", product.FinalPrice), "right")
    end

    page:SetFontAndSize(font_bold, 10)
    page:TextCell(7, 38, 4, 1, "Veroton yhteensä EUR", "right")
    page:TextCell(7, 39, 4, 1, "ALV yhteensä EUR", "right")
    page:TextCell(7, 40, 4, 1, "Verollinen yhteensä EUR", "right")

    page:SetFontAndSize(font, 10)
    page:TextCell(11, 38, 3, 1, string.format("%.2f", invoice.data.Price), "right")
    page:TextCell(11, 39, 3, 1, string.format("%.2f", invoice.data.Tax), "right")
    page:TextCell(11, 40, 3, 1, string.format("%.2f", invoice.data.FinalPrice), "right")

    page:TextCell(0,  43, 7, 1, "IBAN:", "left")
    page:TextCell(0,  44, 7, 1, iban, "left")
    page:TextCell(7,  43, 4, 1, "BIC / SWIFT:", "left")
    page:TextCell(7,  44, 4, 1, bic_swift, "left")
    page:TextCell(11, 43, 3, 1, "Eräpäivä:", "left")
    page:TextCell(11, 44, 3, 1, invoice.data.Due, "left")

    page:TextCell(0,  45, 7, 1, "Viitenumero:", "left")
    page:SetFontAndSize(font_bold, 10)
    page:TextCell(0,  46, 7, 1, invoice.data.ReferenceNum, "left")
    page:SetFontAndSize(font, 10)
    page:TextCell(7,  45, 7, 1, "Yhteensä EUR:", "left")
    page:SetFontAndSize(font_bold, 10)
    page:TextCell(7,  46, 7, 1, string.format("%.2f", invoice.data.FinalPrice), "left")

    page:SetFontAndSize(font, 10)
    page:TextCell(0,  47, 7, 1, invoicer, "left")
    page:TextCell(0,  48, 7, 1, address_line_1, "left")
    page:TextCell(0,  49, 7, 1, address_line_2, "left")

    page:TextCell(7,  47, 2, 1, "Y-tunnus:", "left")
    page:TextCell(9,  47, 5, 1, business_id, "left")

    page:TextCell(7,  48, 2, 1, "WWW:", "left")
    page:TextCell(9,  48, 5, 1, www_address, "left")

    page:EndText()

    filename = to_filename(invoice_nr .. " " .. invoice[1].Customer)

    doc:Save(filename .. ".pdf")
end