From 2b3ffb9854829bb32b6b63aab37d47ec667c0b3b Mon Sep 17 00:00:00 2001 From: Saku Laesvuori Date: Wed, 24 Jan 2024 16:47:24 +0200 Subject: [PATCH] =?UTF-8?q?Siirr=C3=A4=20aiemmat=20maksut=20omaan=20kentt?= =?UTF-8?q?=C3=A4=C3=A4ns=C3=A4=20laskun=20erittelyst=C3=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Laskutin.hs | 10 +++------- src/Laskutin/Email.hs | 11 ++++++++--- src/Laskutin/Types.hs | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Laskutin.hs b/src/Laskutin.hs index 4f4a0c7..f222252 100644 --- a/src/Laskutin.hs +++ b/src/Laskutin.hs @@ -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, ..}) diff --git a/src/Laskutin/Email.hs b/src/Laskutin/Email.hs index f9c3724..09782b7 100644 --- a/src/Laskutin/Email.hs +++ b/src/Laskutin/Email.hs @@ -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" diff --git a/src/Laskutin/Types.hs b/src/Laskutin/Types.hs index c9373ce..049583f 100644 --- a/src/Laskutin/Types.hs +++ b/src/Laskutin/Types.hs @@ -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}