Siirrä aiemmat maksut omaan kenttäänsä laskun erittelystä

This commit is contained in:
Saku Laesvuori 2024-01-24 16:47:24 +02:00
parent 65bd11f156
commit 2b3ffb9854
Signed by: slaesvuo
GPG Key ID: 257D284A2A1D3A32
3 changed files with 12 additions and 11 deletions

View File

@ -74,11 +74,7 @@ updateInvoices transactions invoice@CsvInvoice {reference, ..}
readInvoices :: FilePath -> SendOptions -> IO [([Address], Invoice)]
readInvoices csv SendOptions {account, recipient, subject, message, due} = do
(_, csvInvoices) <- parseCsvFile csv
forM csvInvoices $ \csvInvoice@CsvInvoice {..} -> do
invoiceRows' <- maybe (hPutStrLn stderr ("No invoice rows in invoice") >> exitFailure) pure $
forM csvInvoices $ \CsvInvoice {..} -> do
invoiceRows <- maybe (hPutStrLn stderr "No invoice rows in invoice" >> exitFailure) pure $
nonEmpty $ mapMaybe invoiceRowFromCsv rows
let isPaid = fromMaybe 0 paid >= csvInvoiceSum csvInvoice
invoiceRows = case paid of
Nothing -> invoiceRows'
Just euro -> invoiceRows' <> singleton InvoiceRow {name = "Aiemmin maksettu", amount = 1, price = negate euro}
pure ([invoiceRecipient], Invoice { rows = invoiceRows, ..})
pure ([invoiceRecipient], Invoice { rows = invoiceRows, paid = fromMaybe 0 paid, ..})

View File

@ -38,12 +38,14 @@ sendEmail sendmail email = do
ExitFailure code -> throwIO $ ErrorCall ("sendmail exited with error code " <> show code)
renderInvoice :: Bool -> Invoice -> Maybe (Text, Text)
renderInvoice _ Invoice {isPaid = True} = Nothing
renderInvoice isReminder Invoice {rows, reference, account, recipient, due, subject, message, isPaid = False} = Just $ (subjectPrefix <> " " <> subject,) $
renderInvoice isReminder invoice@Invoice {rows, reference, account, recipient, due, subject, message, paid}
| invoiceSum invoice <= 0 = Nothing
| otherwise = Just $ (subjectPrefix <> " " <> subject,) $
(if isReminder then "Muistutus alla olevan laskun maksamisesta. Jos olet juuri maksanut kyseisen laskun, tämä muistutus on aiheeton.\n\n" else mempty)
<> (if T.null message then mempty else (message <> "\n\n\n"))
<> sconcat (NE.map renderInvoiceRow rows) <> "\n"
<> renderInvoiceRow InvoiceRow {name = "YHTEENSÄ", amount = 1, price = sum $ invoiceRowSum <$> rows} <> "\n"
<> (if paid > 0 then renderInvoiceRow InvoiceRow {name = "Aiemmat maksut", amount = 1, price = paid} <> "\n" else mempty)
<> renderInvoiceRow InvoiceRow {name = "MAKSETTAVAA", amount = 1, price = invoiceSum invoice} <> "\n"
<> "Maksu tilisiirrolla\n\n"
<> "Viitenumero: " <> renderReference reference <> "\n"
<> "Tilinumero: " <> renderIBAN account <> "\n"
@ -56,6 +58,9 @@ renderInvoice isReminder Invoice {rows, reference, account, recipient, due, subj
invoiceRowSum :: InvoiceRow -> Euro
invoiceRowSum InvoiceRow {amount, price} = fromIntegral amount * price
invoiceSum :: Invoice -> Euro
invoiceSum Invoice {rows, paid} = subtract paid $ sum $ invoiceRowSum <$> rows
renderInvoiceRow :: InvoiceRow -> Text
renderInvoiceRow row@InvoiceRow {name, price, amount}
| amount > 1 = name <> ", " <> renderEuro price <> "/kpl * " <> T.pack (show amount) <> "kpl: " <> renderEuro (invoiceRowSum row) <> "\n"

View File

@ -44,7 +44,7 @@ data Invoice = Invoice
, subject :: Text
, message :: Text
, due :: Maybe Day
, isPaid :: Bool
, paid :: Euro
} deriving (Show, Eq)
data Reference = Reference {base :: [Digit], checksum :: Digit}