From f11ddb8ca400c3d149a6f6dcac530ae9f11d08ae Mon Sep 17 00:00:00 2001 From: Saku Laesvuori Date: Fri, 24 Nov 2023 14:48:07 +0200 Subject: [PATCH] =?UTF-8?q?Lis=C3=A4=C3=A4=20komento=20laskutuksen=20tilan?= =?UTF-8?q?=20tarkastamiseen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Laskutin.hs | 7 +++++++ src/Laskutin/Options.hs | 4 +++- src/Laskutin/Status.hs | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/Laskutin/Status.hs diff --git a/src/Laskutin.hs b/src/Laskutin.hs index e733e66..d7e63e1 100644 --- a/src/Laskutin.hs +++ b/src/Laskutin.hs @@ -22,11 +22,13 @@ import Laskutin.CSV import Laskutin.Email import Laskutin.Options import Laskutin.Types +import Laskutin.Status main :: IO () main = do Options {csv, command} <- parseOptions case command of + Status -> statusMain csv Send sendOptions -> sendInvoicesMain csv sendOptions UpdateTable bankCsv -> updateTableMain csv bankCsv @@ -46,6 +48,11 @@ updateTableMain invoiceCsv transactionCsv = do let newInvoices = fmap (updateInvoices csvTransactions) csvInvoices LBS.writeFile invoiceCsv $ encodeByName headers newInvoices +statusMain :: FilePath -> IO () +statusMain invoiceCsv = do + (_, invoices) <- parseCsvFile invoiceCsv + T.putStr $ showStatus $ invoiceStatus invoices + updateInvoices :: [CsvTransaction] -> CsvInvoice -> CsvInvoice updateInvoices transactions invoice@CsvInvoice {reference, ..} | csvIsPaid invoice = invoice diff --git a/src/Laskutin/Options.hs b/src/Laskutin/Options.hs index 9ee8256..30ddfa7 100644 --- a/src/Laskutin/Options.hs +++ b/src/Laskutin/Options.hs @@ -21,6 +21,7 @@ data Options = Options data Command = Send SendOptions | UpdateTable FilePath + | Status data SendOptions = SendOptions { account :: IBAN @@ -42,7 +43,8 @@ options = Options <$> strOption (long "file" <> help "Path to the csv file" <> metavar "FILE") <*> subparser (command "send" (info (sendOptions <**> helper) (progDesc "Send email invoices from CSV")) - <> command "update" (info (updateTableOptions <**> helper) (progDesc "Update invoice CSV from a bank CSV"))) + <> command "update" (info (updateTableOptions <**> helper) (progDesc "Update invoice CSV from a bank CSV")) + <> command "status" (info (pure Status) (progDesc "Print the status of the invoices"))) updateTableOptions :: Parser Command updateTableOptions = UpdateTable <$> strOption (long "bank-csv" <> metavar "FILE") diff --git a/src/Laskutin/Status.hs b/src/Laskutin/Status.hs new file mode 100644 index 0000000..5381d4b --- /dev/null +++ b/src/Laskutin/Status.hs @@ -0,0 +1,39 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RecordWildCards #-} + +module Laskutin.Status where + +import Data.Maybe (fromMaybe) +import Data.Text (Text) + +import qualified Data.Text as T + +import Laskutin.Types +import Laskutin.CSV + +data InvoiceStatus = InvoiceStatus + { nPaid :: Int + , nTotal :: Int + , amountPaid :: Euro + , amountMissing :: Euro + , amountTotal :: Euro + } deriving Show + +invoiceStatus :: [CsvInvoice] -> InvoiceStatus +invoiceStatus = InvoiceStatus + <$> length . filter csvIsPaid + <*> length + <*> sum . fmap (\CsvInvoice {paid} -> fromMaybe 0 paid) + <*> sum . fmap (\invoice@CsvInvoice {paid} -> max 0 $ csvInvoiceSum invoice - fromMaybe 0 paid) + <*> sum . fmap csvInvoiceSum + +showStatus :: InvoiceStatus -> Text +showStatus InvoiceStatus {..} = + "Laskuja maksettu: " <> T.pack (show nPaid) <> "\n" <> + "Laskuja maksamatta: " <> T.pack (show $ nTotal - nPaid) <> "\n" <> + "Laskuja yhteensä: " <> T.pack (show nTotal) <> "\n" <> + "\n" <> + "Maksettu: " <> renderEuro amountPaid <> "\n" <> + "Maksamatta: " <> renderEuro amountMissing <> "\n" <> + "Yhteensä saatavia: " <> renderEuro amountTotal <> "\n" <> + "Ylimaksettu: " <> renderEuro (amountTotal - amountMissing - amountPaid) <> "\n"