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"]}) $
214 [ "Seller" := inv & invoiceIssuer & get @(Entity entId) & Doc.toBlock
215 , "Buyer" := inv & invoiceRecipient & get @(Entity entId) & Doc.toBlock
220 [ "Grand totals" & Doc.classes ["title"]
226 , "To pay (excl. taxes)"
231 { Doc.tableCellJustify = Doc.JustifyCenter
232 , Doc.tableCellContent =
234 <&> invoiceItemPeriod
238 { periodBeginning = min (x & periodBeginning) (y & periodBeginning)
239 , periodEnd = max (x & periodEnd) (y & periodEnd)
243 [ period & periodBeginning & Doc.toInline & Doc.BlockPara
244 , period & periodEnd & Doc.toInline & Doc.BlockPara
248 { Doc.tableCellJustify = Doc.JustifyCenter
249 , Doc.tableCellContent =
251 [ itm & invoiceItemQuantity
258 { Doc.tableCellJustify = Doc.JustifyCenter
259 , Doc.tableCellContent =
261 [ itm & invoiceItemTotal
274 [ "Mandatory legal notices" & Doc.classes ["title"]
277 InvoiceMentionTVANonApplicable ->
278 -- "TVA non applicable, art. 293 B du code général des impôts."
279 "—" := "VAT not applicable, art. 293 B of the French General Tax Code."
280 InvoiceMentionIndemnitéForfaitaire ->
282 [ -- "Indemnité forfaitaire pour frais de recouvrement en cas de retard de paiement\x202F: "
283 "Fixed compensation for recovery costs in case of late payment: "
284 , (40 :: Accounting.Amount 100 (Accounting.UnitName "€"))
288 InvoiceMentionIndemnitéTaux rate ->
290 [ -- "Taux des pénalités pour retard de paiement exigibles à compter du "
291 "Late payment penalty rate (applicable from "
292 , inv & invoicePaymentDueBefore & Time.addLocalTime Time.nominalDay & Doc.toInline
295 , rate & Doc.toInline
296 , -- , " × montant impayé × nombre de jours de retard / 365.25"
297 " × unpaid amount × number of days late / 365.25"
300 | mention <- inv & invoiceMentions
306 <&> \blk -> Doc.flexItem{Doc.flexItemContent = blk}
311 , invSummary & ol1 & chunksOf 15 <&> \invItemsChunk ->
313 { Doc.pageSection = Just $ invTitle <> " — Summary"
316 { Doc.flexDirection = Doc.FlexDirectionColumn
317 , Doc.flexGap = 0.5 & Doc.cm
320 { Doc.flexItemContent =
321 [ [ ("Summary" :: Text) & Doc.toBlock & Doc.classes ["title"]
322 | invItemsChunk & headMaybe & maybe False (fst >>> (== 1))
327 { Doc.tableTemplate =
328 [ Doc.LengthRelative $ Doc.LengthRelativeMaxContent
329 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
330 , Doc.LengthRelative $ 1 & Doc.fr
331 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
332 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
333 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
334 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
335 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
337 , Doc.tableRowsEvenOdd = True
346 { Doc.tableCellContent =
347 [ "Rate" & Doc.BlockPara
348 , "(excl.\xA0taxes.)" & Doc.BlockPara
350 , Doc.tableCellJustify = Doc.JustifyCenter
354 { Doc.tableCellContent =
355 [ "Total" & Doc.BlockPara
356 , "(excl.\xA0taxes.)" & Doc.BlockPara
358 , Doc.tableCellJustify = Doc.JustifyCenter
363 { Doc.tableCellContent = itemCount & Printf.printf "%i" & Doc.toBlock @String
364 , Doc.tableCellJustify = Doc.JustifyEnd
367 { Doc.tableCellContent = invItem & invoiceItemType & Doc.toBlock
368 , Doc.tableCellJustify = Doc.JustifyCenter
371 { Doc.tableCellContent = invItem & invoiceItemScope & Doc.toInline & Doc.toBlock
372 , Doc.tableCellJustify = Doc.JustifyStart
375 { Doc.tableCellContent = invItem & invoiceItemAction & Doc.toInline & Doc.toBlock
376 , Doc.tableCellJustify = Doc.JustifyStart
379 { Doc.tableCellContent =
380 [ invItem & invoiceItemPeriod & periodBeginning & Doc.toInline & Doc.BlockPara
381 , invItem & invoiceItemPeriod & periodEnd & Doc.toInline & Doc.BlockPara
383 , Doc.tableCellJustify = Doc.JustifyStart
386 { Doc.tableCellContent = invItem & invoiceItemRate & Doc.toBlock
387 , Doc.tableCellJustify = Doc.JustifyEnd
390 { Doc.tableCellContent =
391 [ invItem & invoiceItemQuantity & Doc.toBlock
392 , let (qtyPercent, _actualRate) =
394 (invSummaryQuantityTotal & Accounting.quantityToRatio & (100 Prelude./))
395 (invItem & invoiceItemQuantity & Accounting.amountQuantity)
397 & Accounting.quantityToRational
398 & (fromRational :: _ -> Double)
399 & (Printf.printf "(%02f%%)" :: _ -> String)
402 , Doc.tableCellJustify = Doc.JustifyEnd
405 { Doc.tableCellContent = invItem & invoiceItemTotal & Doc.toBlock
406 , Doc.tableCellJustify = Doc.JustifyEnd
409 | (itemCount, invItem) <- invItemsChunk
421 x{Doc.containerClasses = ["invoice-summary"]}
425 , inv & invoiceWorks & ol1 & chunksOf 15 <&> \worksChunk ->
427 { Doc.pageSection = Just $ invTitle <> " — Details"
430 { Doc.flexDirection = Doc.FlexDirectionColumn
431 , Doc.flexGap = 0.5 & Doc.cm
434 { Doc.flexItemContent =
435 [ [ ("Details" :: Text) & Doc.toBlock & Doc.classes ["title"]
436 | worksChunk & headMaybe & maybe False (fst >>> (== 1))
440 { Doc.tableTemplate =
441 [ Doc.LengthRelative $ Doc.LengthRelativeMaxContent
442 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
443 , Doc.LengthRelative $ Doc.LengthRelativeMaxContent
444 , Doc.LengthRelative $ 1 & Doc.fr
445 , Doc.LengthRelative $ 1 & Doc.fr
446 , Doc.LengthAbsolute $ 5 & Doc.cm
447 , Doc.LengthRelative $ 2 & Doc.fr
449 , Doc.tableRowsEvenOdd = True
462 { Doc.tableCellContent = workCount & Printf.printf "%i" & Doc.toBlock @String
463 , Doc.tableCellJustify = Doc.JustifyEnd
466 { Doc.tableCellContent = work & workDate & Doc.toBlock
467 , Doc.tableCellJustify = Doc.JustifyCenter
470 { Doc.tableCellContent = work & workDuration & Doc.toBlock
471 , Doc.tableCellJustify = Doc.JustifyEnd
474 { Doc.tableCellContent = work & workScope & Doc.toInline & Doc.toBlock
475 , Doc.tableCellJustify = Doc.JustifyStart
478 { Doc.tableCellContent = work & workAction & Doc.toInline & Doc.toBlock
479 , Doc.tableCellJustify = Doc.JustifyStart
482 { Doc.tableCellContent =
484 | ref <- work & workReferences
488 , Doc.tableCellJustify = Doc.JustifyStart
491 { Doc.tableCellContent = work & workDescription & Doc.toBlock
492 , Doc.tableCellJustify = Doc.JustifyStart
495 | (workCount, work) <- worksChunk
505 & (\x -> x{Doc.containerClasses = ["invoice-details"]})
512 l & List.map \(pageIdx, pag) ->
514 { Doc.pageOrientation = Doc.PageOrientationPortrait
515 , Doc.pageSize = Doc.PageSizeA4
516 , Doc.pageNumber = Just pageIdx
517 , Doc.pageNumberTotal = Just $ l & List.length & fromIntegral
521 then Doc.PageSideLeft
522 else Doc.PageSideRight
530 Map actionId (Amount 100 unit) ->
531 [Work scopeId actionId] ->
532 Map (Ands scopeId) (Map actionId (InvoiceItem scopeId actionId _ _))
533 invoiceSummary invRates works =
538 { invoiceItemScope = x & invoiceItemScope
539 , invoiceItemAction = x & invoiceItemAction
540 , invoiceItemPeriod =
542 { periodBeginning = min (x & invoiceItemPeriod & periodBeginning) (y & invoiceItemPeriod & periodBeginning)
543 , periodEnd = max (x & invoiceItemPeriod & periodEnd) (y & invoiceItemPeriod & periodEnd)
545 , invoiceItemQuantity = (x & invoiceItemQuantity) + (y & invoiceItemQuantity)
546 , invoiceItemType = x & invoiceItemType
547 , invoiceItemRate = x & invoiceItemRate
551 [ Map.singleton (work & workScope) $
552 Map.singleton (work & workAction) $
554 { invoiceItemScope = work & workScope
555 , invoiceItemAction = work & workAction
556 , invoiceItemType = InvoiceItemTypeService
557 , invoiceItemQuantity = work & workDuration
558 , invoiceItemPeriod =
560 { periodBeginning = work & workDate
561 , periodEnd = work & workDate
565 & Map.lookup (work & workAction)
566 & fromMaybe (errorShow ("missing action" :: Text, work & workAction))