1 {-# LANGUAGE OverloadedLists #-}
2 {-# LANGUAGE PolyKinds #-}
4 {-# LANGUAGE UndecidableInstances #-}
5 {-# OPTIONS_GHC -Wno-orphans #-}
7 module Literate.Invoice.HTML where
9 import Data.List qualified as List
10 import Data.Map.Strict qualified as Map
11 import Data.Time qualified as Time
12 import System.FilePath.Posix qualified as File
13 import Text.Printf qualified as Printf
14 import Prelude qualified
16 import Literate.Accounting qualified as Accounting
17 import Literate.Accounting.Amount
18 import Literate.Accounting.Quantity
19 import Literate.Accounting.Unit
20 import Literate.Database
21 import Literate.Document.HTML
22 import Literate.Document.HTML qualified as HTML
23 import Literate.Document.Table qualified as Doc
24 import Literate.Document.Type qualified as Doc
25 import Literate.Invoice
26 import Literate.Organization
27 import Literate.Prelude
28 import Paths_literate_business qualified as Self
30 class HTMLIOable a where
31 htmlIO :: a -> IO Html
33 instance Doc.ToBlock InvoiceType where
35 InvoiceTypeProForma -> "pro forma"
36 InvoiceTypeSale -> "sale"
37 InvoiceTypeVoucher -> "voucher"
38 instance Doc.ToBlock InvoiceItemType where
40 InvoiceItemTypeItem -> "item"
41 InvoiceItemTypeService -> "service"
43 instance (ToMarkup (Quantity qf), UnitShowS unit) => ToMarkup (Amount qf unit) where
45 (amountQuantity & HTML.toMarkup)
46 <> (if null unit then "" else "\x202F" <> (unit & toHtml))
49 instance (Doc.ToInline (Quantity qf), UnitShowS unit) => Doc.ToInline (Amount qf unit) where
51 (amountQuantity & Doc.toInline)
52 <> (if null unit then "" else "\x202F" <> (unit & Doc.toInline))
55 instance (Doc.ToBlock (Quantity qf), QuantFact qf, UnitShowS unit) => Doc.ToBlock (Amount qf unit) where
56 toBlock x = Doc.BlockPara $ x & Doc.toInline
58 instance QuantFact qf => ToMarkup (Quantity qf) where
63 & (Prelude.toRational >>> Prelude.fromRational :: Ratio Natural -> Double)
64 & ( `Printf.formatArg`
67 , fmtAlternate = False
72 quantisationFactor @qf
73 & (Prelude.fromIntegral :: _ -> Double)
81 instance QuantFact qf => Doc.ToInline (Quantity qf) where
82 toInline qty = Doc.toInline do
85 & (Prelude.toRational >>> Prelude.fromRational :: Ratio Natural -> Double)
86 & ( `Printf.formatArg`
89 , fmtAlternate = False
94 quantisationFactor @qf
95 & (Prelude.fromIntegral :: _ -> Double)
103 instance QuantFact qf => Doc.ToBlock (Quantity qf) where
104 toBlock x = Doc.BlockPara $ x & Doc.toInline
107 forall scopeId actionId entId invId.
108 Get (Organization entId) entId =>
109 Get (Entity entId) entId =>
111 Doc.ToInline entId =>
112 Invoice scopeId actionId entId invId ->
114 invoiceIdInline inv =
118 & get @(Organization entId)
120 & get @(Entity entId)
123 , case inv & invoiceType of
124 InvoiceTypeProForma -> "pro-forma"
125 InvoiceTypeSale -> "sale"
126 InvoiceTypeVoucher -> "voucher"
127 , inv & invoiceId & fromEnum & show & Doc.toInline
132 ( Get (Organization entId) entId
133 , Get (Entity entId) entId
139 , Doc.ToInline scopeId
140 , Doc.ToInline actionId
143 HTMLIOable (Invoice scopeId actionId entId invId)
146 -- FixMe(portability): this absolute path is not portable out of my system
147 dataPath <- Self.getDataDir <&> File.normalise
148 -- paperCSS <- dataPath </> "styles" </> "Paper.css" & BS.readFile <&> Text.decodeUtf8
149 -- invoiceCSS <- dataPath </> "styles" </> "Invoice.css" & BS.readFile <&> Text.decodeUtf8
150 let invSummary :: [InvoiceItem _ _ _ _] =
151 invoiceSummary (inv & invoiceRates) (inv & invoiceWorks)
152 & foldMap (foldMap pure)
153 let invSummaryQuantityTotal :: Accounting.Quantity 100 =
154 invSummary <&> invoiceItemQuantity <&> Accounting.amountQuantity & Accounting.sumQuantities & fromMaybe 0
155 let invTitle :: Doc.Inline =
157 List.intersperse " - " $
161 & get @(Organization entId)
163 & get @(Entity entId)
166 Just n -> [Doc.toInline n]
167 , [inv & invoiceEmittedOn & Doc.toInline]
168 , ["Invoice #" <> (inv & invoiceIdInline)]
169 , inv & invoiceOrders
175 { documentTitle = invTitle
176 , documentAttachments =
178 [ "styles/Document.css"
179 , "styles/Invoice.css"
188 { Doc.pageSection = Just $ invTitle
191 { Doc.flexDirection = Doc.FlexDirectionColumn
192 , Doc.flexGap = 0.5 & Doc.cm
194 [ "Invoice" & Doc.classes ["title"]
196 ( [ ["InvoiceIdentifier" := inv & invoiceIdInline & Doc.toBlock]
197 , ["InvoiceType" := inv & invoiceType & Doc.toBlock]
198 , ["InvoiceOrders" := inv & invoiceOrders <&> (\x -> "—" := Doc.toBlock x) & Doc.List & Doc.toBlock]
199 , ["InvoiceEmittedOn" := inv & invoiceEmittedOn & Doc.toBlock]
200 , ["InvoicePaymentDueBefore" := inv & invoicePaymentDueBefore & Doc.toBlock]
206 & (\x -> x{Doc.containerClasses = ["invoice-headers"]})
209 (\x -> x{Doc.containerClasses = ["invoice-from-to"]}) $
217 & get @(Organization entId)
222 & get @(Organization entId)
228 [ "Grand totals" & Doc.classes ["title"]
234 , "To pay (excl. taxes)"
239 { Doc.tableCellJustify = Doc.JustifyCenter
240 , Doc.tableCellContent =
242 <&> invoiceItemPeriod
246 { periodBeginning = min (x & periodBeginning) (y & periodBeginning)
247 , periodEnd = max (x & periodEnd) (y & periodEnd)
251 [ period & periodBeginning & Doc.toInline & Doc.BlockPara
252 , period & periodEnd & Doc.toInline & Doc.BlockPara
256 { Doc.tableCellJustify = Doc.JustifyCenter
257 , Doc.tableCellContent =
259 [ itm & invoiceItemQuantity
266 { Doc.tableCellJustify = Doc.JustifyCenter
267 , Doc.tableCellContent =
269 [ itm & invoiceItemTotal
282 [ "Mandatory legal notices" & Doc.classes ["title"]
285 InvoiceMentionTVANonApplicable ->
286 -- "TVA non applicable, art. 293 B du code général des impôts."
287 "—" := "VAT not applicable, art. 293 B of the French General Tax Code."
288 InvoiceMentionIndemnitéForfaitaire ->
290 [ -- "Indemnité forfaitaire pour frais de recouvrement en cas de retard de paiement\x202F: "
291 "Fixed compensation for recovery costs in case of late payment: "
292 , (40 :: Accounting.Amount 100 (Accounting.UnitName "€"))
296 InvoiceMentionIndemnitéTaux rate ->
298 [ -- "Taux des pénalités pour retard de paiement exigibles à compter du "
299 "Late payment penalty rate (applicable from "
300 , inv & invoicePaymentDueBefore & Time.addLocalTime Time.nominalDay & Doc.toInline
303 , rate & Doc.toInline
304 , -- , " × montant impayé × nombre de jours de retard / 365.25"
305 " × unpaid amount × number of days late / 365.25"
308 | mention <- inv & invoiceMentions
314 <&> \blk -> Doc.flexItem{Doc.flexItemContent = blk}
319 , invSummary & ol1 & chunksOf 15 <&> \invItemsChunk ->
321 { Doc.pageSection = Just $ invTitle <> " — Summary"
324 { Doc.flexDirection = Doc.FlexDirectionColumn
325 , Doc.flexGap = 0.5 & Doc.cm
328 { Doc.flexItemContent =
329 [ [ ("Summary" :: Text) & Doc.toBlock & Doc.classes ["title"]
330 | invItemsChunk & headMaybe & maybe False (fst >>> (== 1))
335 { Doc.tableTemplate =
336 [ Doc.LengthRelative $ Doc.LengthRelativeMaxContent
337 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
338 , Doc.LengthRelative $ 1 & Doc.fr
339 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
340 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
341 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
342 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
343 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
345 , Doc.tableRowsEvenOdd = True
354 { Doc.tableCellContent =
355 [ "Rate" & Doc.BlockPara
356 , "(excl.\xA0taxes.)" & Doc.BlockPara
358 , Doc.tableCellJustify = Doc.JustifyCenter
362 { Doc.tableCellContent =
363 [ "Total" & Doc.BlockPara
364 , "(excl.\xA0taxes.)" & Doc.BlockPara
366 , Doc.tableCellJustify = Doc.JustifyCenter
371 { Doc.tableCellContent = itemCount & Printf.printf "%i" & Doc.toBlock @String
372 , Doc.tableCellJustify = Doc.JustifyEnd
375 { Doc.tableCellContent = invItem & invoiceItemType & Doc.toBlock
376 , Doc.tableCellJustify = Doc.JustifyCenter
379 { Doc.tableCellContent = invItem & invoiceItemScope & Doc.toInline & Doc.toBlock
380 , Doc.tableCellJustify = Doc.JustifyStart
383 { Doc.tableCellContent = invItem & invoiceItemAction & Doc.toInline & Doc.toBlock
384 , Doc.tableCellJustify = Doc.JustifyStart
387 { Doc.tableCellContent =
388 [ invItem & invoiceItemPeriod & periodBeginning & Doc.toInline & Doc.BlockPara
389 , invItem & invoiceItemPeriod & periodEnd & Doc.toInline & Doc.BlockPara
391 , Doc.tableCellJustify = Doc.JustifyStart
394 { Doc.tableCellContent = invItem & invoiceItemRate & Doc.toBlock
395 , Doc.tableCellJustify = Doc.JustifyEnd
398 { Doc.tableCellContent =
399 [ invItem & invoiceItemQuantity & Doc.toBlock
400 , let (qtyPercent, _actualRate) =
402 (invSummaryQuantityTotal & Accounting.quantityToRatio & (100 Prelude./))
403 (invItem & invoiceItemQuantity & Accounting.amountQuantity)
405 & Accounting.quantityToRational
406 & (fromRational :: _ -> Double)
407 & (Printf.printf "(%02f%%)" :: _ -> String)
410 , Doc.tableCellJustify = Doc.JustifyEnd
413 { Doc.tableCellContent = invItem & invoiceItemTotal & Doc.toBlock
414 , Doc.tableCellJustify = Doc.JustifyEnd
417 | (itemCount, invItem) <- invItemsChunk
429 x{Doc.containerClasses = ["invoice-summary"]}
433 , inv & invoiceWorks & ol1 & chunksOf 15 <&> \worksChunk ->
435 { Doc.pageSection = Just $ invTitle <> " — Details"
438 { Doc.flexDirection = Doc.FlexDirectionColumn
439 , Doc.flexGap = 0.5 & Doc.cm
442 { Doc.flexItemContent =
443 [ [ ("Details" :: Text) & Doc.toBlock & Doc.classes ["title"]
444 | worksChunk & headMaybe & maybe False (fst >>> (== 1))
448 { Doc.tableTemplate =
449 [ Doc.LengthRelative $ Doc.LengthRelativeMaxContent
450 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
451 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
452 , Doc.LengthRelative $ 1 & Doc.fr
453 , Doc.LengthRelative $ 1 & Doc.fr
454 , Doc.LengthAbsolute $ 5 & Doc.cm
455 , Doc.LengthRelative $ 2 & Doc.fr
457 , Doc.tableRowsEvenOdd = True
470 { Doc.tableCellContent = workCount & Printf.printf "%i" & Doc.toBlock @String
471 , Doc.tableCellJustify = Doc.JustifyEnd
474 { Doc.tableCellContent = work & workDate & Doc.toBlock
475 , Doc.tableCellJustify = Doc.JustifyCenter
478 { Doc.tableCellContent = work & workDuration & Doc.toBlock
479 , Doc.tableCellJustify = Doc.JustifyEnd
482 { Doc.tableCellContent = work & workScope & Doc.toInline & Doc.toBlock
483 , Doc.tableCellJustify = Doc.JustifyStart
486 { Doc.tableCellContent = work & workAction & Doc.toInline & Doc.toBlock
487 , Doc.tableCellJustify = Doc.JustifyStart
490 { Doc.tableCellContent =
492 | ref <- work & workReferences
496 , Doc.tableCellJustify = Doc.JustifyStart
499 { Doc.tableCellContent = work & workDescription & Doc.toBlock
500 , Doc.tableCellJustify = Doc.JustifyStart
503 | (workCount, work) <- worksChunk
513 & (\x -> x{Doc.containerClasses = ["invoice-details"]})
520 l & List.map \(pageIdx, pag) ->
522 { Doc.pageOrientation = Doc.PageOrientationPortrait
523 , Doc.pageSize = Doc.PageSizeA4
524 , Doc.pageNumber = Just pageIdx
525 , Doc.pageNumberTotal = Just $ l & List.length & fromIntegral
529 then Doc.PageSideLeft
530 else Doc.PageSideRight
538 Map actionId (Amount 100 unit) ->
539 [Work scopeId actionId] ->
540 Map (Ands scopeId) (Map actionId (InvoiceItem scopeId actionId _ _))
541 invoiceSummary invRates works =
546 { invoiceItemScope = x & invoiceItemScope
547 , invoiceItemAction = x & invoiceItemAction
548 , invoiceItemPeriod =
550 { periodBeginning = min (x & invoiceItemPeriod & periodBeginning) (y & invoiceItemPeriod & periodBeginning)
551 , periodEnd = max (x & invoiceItemPeriod & periodEnd) (y & invoiceItemPeriod & periodEnd)
553 , invoiceItemQuantity = (x & invoiceItemQuantity) + (y & invoiceItemQuantity)
554 , invoiceItemType = x & invoiceItemType
555 , invoiceItemRate = x & invoiceItemRate
559 [ Map.singleton (work & workScope) $
560 Map.singleton (work & workAction) $
562 { invoiceItemScope = work & workScope
563 , invoiceItemAction = work & workAction
564 , invoiceItemType = InvoiceItemTypeService
565 , invoiceItemQuantity = work & workDuration
566 , invoiceItemPeriod =
568 { periodBeginning = work & workDate
569 , periodEnd = work & workDate
573 & Map.lookup (work & workAction)
574 & fromMaybe (errorShow ("missing action" :: Text, work & workAction))