Lisää komento laskutuksen tilan tarkastamiseen
This commit is contained in:
		
							parent
							
								
									8bd1753539
								
							
						
					
					
						commit
						f11ddb8ca4
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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"
 | 
			
		||||
		Loading…
	
		Reference in New Issue