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

View File

@ -38,12 +38,14 @@ sendEmail sendmail email = do
ExitFailure code -> throwIO $ ErrorCall ("sendmail exited with error code " <> show code) ExitFailure code -> throwIO $ ErrorCall ("sendmail exited with error code " <> show code)
renderInvoice :: Bool -> Invoice -> Maybe (Text, Text) renderInvoice :: Bool -> Invoice -> Maybe (Text, Text)
renderInvoice _ Invoice {isPaid = True} = Nothing renderInvoice isReminder invoice@Invoice {rows, reference, account, recipient, due, subject, message, paid}
renderInvoice isReminder Invoice {rows, reference, account, recipient, due, subject, message, isPaid = False} = Just $ (subjectPrefix <> " " <> subject,) $ | 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 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")) <> (if T.null message then mempty else (message <> "\n\n\n"))
<> sconcat (NE.map renderInvoiceRow rows) <> "\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" <> "Maksu tilisiirrolla\n\n"
<> "Viitenumero: " <> renderReference reference <> "\n" <> "Viitenumero: " <> renderReference reference <> "\n"
<> "Tilinumero: " <> renderIBAN account <> "\n" <> "Tilinumero: " <> renderIBAN account <> "\n"
@ -56,6 +58,9 @@ renderInvoice isReminder Invoice {rows, reference, account, recipient, due, subj
invoiceRowSum :: InvoiceRow -> Euro invoiceRowSum :: InvoiceRow -> Euro
invoiceRowSum InvoiceRow {amount, price} = fromIntegral amount * price invoiceRowSum InvoiceRow {amount, price} = fromIntegral amount * price
invoiceSum :: Invoice -> Euro
invoiceSum Invoice {rows, paid} = subtract paid $ sum $ invoiceRowSum <$> rows
renderInvoiceRow :: InvoiceRow -> Text renderInvoiceRow :: InvoiceRow -> Text
renderInvoiceRow row@InvoiceRow {name, price, amount} renderInvoiceRow row@InvoiceRow {name, price, amount}
| amount > 1 = name <> ", " <> renderEuro price <> "/kpl * " <> T.pack (show amount) <> "kpl: " <> renderEuro (invoiceRowSum row) <> "\n" | 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 , subject :: Text
, message :: Text , message :: Text
, due :: Maybe Day , due :: Maybe Day
, isPaid :: Bool , paid :: Euro
} deriving (Show, Eq) } deriving (Show, Eq)
data Reference = Reference {base :: [Digit], checksum :: Digit} data Reference = Reference {base :: [Digit], checksum :: Digit}