Unified Address support #8
1 changed files with 43 additions and 50 deletions
93
src/Order.hs
93
src/Order.hs
|
@ -14,24 +14,22 @@ import GHC.Generics
|
||||||
import Test.QuickCheck
|
import Test.QuickCheck
|
||||||
|
|
||||||
-- | Type to represent a ZGo order
|
-- | Type to represent a ZGo order
|
||||||
data ZGoOrder =
|
data ZGoOrder = ZGoOrder
|
||||||
ZGoOrder
|
{ q_id :: Maybe ObjectId
|
||||||
{ q_id :: Maybe ObjectId
|
, qaddress :: T.Text
|
||||||
, qaddress :: T.Text
|
, qsession :: T.Text
|
||||||
, qsession :: T.Text
|
, qtimestamp :: UTCTime
|
||||||
, qtimestamp :: UTCTime
|
, qclosed :: Bool
|
||||||
, qclosed :: Bool
|
, qcurrency :: T.Text
|
||||||
, qcurrency :: T.Text
|
, qprice :: Double
|
||||||
, qprice :: Double
|
, qtotal :: Double
|
||||||
, qtotal :: Double
|
, qtotalZec :: Double
|
||||||
, qtotalZec :: Double
|
, qlines :: [LineItem]
|
||||||
, qlines :: [LineItem]
|
, qpaid :: Bool
|
||||||
, qpaid :: Bool
|
, qexternalInvoice :: T.Text
|
||||||
, qexternalInvoice :: T.Text
|
, qshortCode :: T.Text
|
||||||
, qshortCode :: T.Text
|
, qtoken :: T.Text
|
||||||
, qtoken :: T.Text
|
} deriving (Eq, Show, Generic)
|
||||||
}
|
|
||||||
deriving (Eq, Show, Generic)
|
|
||||||
|
|
||||||
instance ToJSON ZGoOrder where
|
instance ToJSON ZGoOrder where
|
||||||
toJSON (ZGoOrder i a s ts c cur p t tZ l paid eI sC tk) =
|
toJSON (ZGoOrder i a s ts c cur p t tZ l paid eI sC tk) =
|
||||||
|
@ -74,7 +72,7 @@ instance ToJSON ZGoOrder where
|
||||||
instance FromJSON ZGoOrder where
|
instance FromJSON ZGoOrder where
|
||||||
parseJSON =
|
parseJSON =
|
||||||
withObject "Order" $ \obj -> do
|
withObject "Order" $ \obj -> do
|
||||||
i <- obj .: "_id"
|
i <- obj .:? "_id"
|
||||||
a <- obj .: "address"
|
a <- obj .: "address"
|
||||||
s <- obj .: "session"
|
s <- obj .: "session"
|
||||||
ts <- obj .: "timestamp"
|
ts <- obj .: "timestamp"
|
||||||
|
@ -88,24 +86,7 @@ instance FromJSON ZGoOrder where
|
||||||
eI <- obj .: "externalInvoice"
|
eI <- obj .: "externalInvoice"
|
||||||
sC <- obj .: "shortCode"
|
sC <- obj .: "shortCode"
|
||||||
tk <- obj .: "token"
|
tk <- obj .: "token"
|
||||||
pure $
|
pure $ ZGoOrder (read =<< i) a s ts c cur p t tZ l pd eI sC tk
|
||||||
ZGoOrder
|
|
||||||
(if not (null i)
|
|
||||||
then Just (read i)
|
|
||||||
else Nothing)
|
|
||||||
a
|
|
||||||
s
|
|
||||||
ts
|
|
||||||
c
|
|
||||||
cur
|
|
||||||
p
|
|
||||||
t
|
|
||||||
tZ
|
|
||||||
l
|
|
||||||
pd
|
|
||||||
eI
|
|
||||||
sC
|
|
||||||
tk
|
|
||||||
|
|
||||||
instance Val ZGoOrder where
|
instance Val ZGoOrder where
|
||||||
val (ZGoOrder i a s ts c cur p t tZ l pd eI sC tk) =
|
val (ZGoOrder i a s ts c cur p t tZ l pd eI sC tk) =
|
||||||
|
@ -160,13 +141,11 @@ instance Val ZGoOrder where
|
||||||
cast' _ = Nothing
|
cast' _ = Nothing
|
||||||
|
|
||||||
-- Type to represent an order line item
|
-- Type to represent an order line item
|
||||||
data LineItem =
|
data LineItem = LineItem
|
||||||
LineItem
|
{ lqty :: Double
|
||||||
{ lqty :: Double
|
, lname :: T.Text
|
||||||
, lname :: T.Text
|
, lcost :: Double
|
||||||
, lcost :: Double
|
} deriving (Eq, Show)
|
||||||
}
|
|
||||||
deriving (Eq, Show)
|
|
||||||
|
|
||||||
instance ToJSON LineItem where
|
instance ToJSON LineItem where
|
||||||
toJSON (LineItem q n c) = object ["qty" .= q, "name" .= n, "cost" .= c]
|
toJSON (LineItem q n c) = object ["qty" .= q, "name" .= n, "cost" .= c]
|
||||||
|
@ -193,10 +172,10 @@ upsertOrder :: ZGoOrder -> Action IO ()
|
||||||
upsertOrder o = do
|
upsertOrder o = do
|
||||||
let order = val $ updateOrderTotals o
|
let order = val $ updateOrderTotals o
|
||||||
case order of
|
case order of
|
||||||
Doc d ->
|
Doc d ->
|
||||||
if isJust (q_id o)
|
if isJust (q_id o)
|
||||||
then upsert (select ["_id" =: q_id o] "orders") d
|
then upsert (select ["_id" =: q_id o] "orders") d
|
||||||
else insert_ "orders" d
|
else insert_ "orders" d
|
||||||
_ -> return ()
|
_ -> return ()
|
||||||
|
|
||||||
insertWooOrder :: ZGoOrder -> Action IO Database.MongoDB.Value
|
insertWooOrder :: ZGoOrder -> Action IO Database.MongoDB.Value
|
||||||
|
@ -210,7 +189,14 @@ upsertXeroOrder :: ZGoOrder -> Action IO ()
|
||||||
upsertXeroOrder o = do
|
upsertXeroOrder o = do
|
||||||
let order = val $ updateOrderTotals o
|
let order = val $ updateOrderTotals o
|
||||||
case order of
|
case order of
|
||||||
Doc d -> upsert (select ["externalInvoice" =: qexternalInvoice o, "shortCode" =: qshortCode o] "orders") d
|
Doc d ->
|
||||||
|
upsert
|
||||||
|
(select
|
||||||
|
[ "externalInvoice" =: qexternalInvoice o
|
||||||
|
, "shortCode" =: qshortCode o
|
||||||
|
]
|
||||||
|
"orders")
|
||||||
|
d
|
||||||
_ -> return ()
|
_ -> return ()
|
||||||
|
|
||||||
-- | Function to update order totals from items
|
-- | Function to update order totals from items
|
||||||
|
@ -247,13 +233,20 @@ findOrder :: T.Text -> Action IO (Maybe Document)
|
||||||
findOrder s = findOne (select ["session" =: s, "closed" =: False] "orders")
|
findOrder s = findOne (select ["session" =: s, "closed" =: False] "orders")
|
||||||
|
|
||||||
findXeroOrder :: T.Text -> T.Text -> T.Text -> Action IO (Maybe Document)
|
findXeroOrder :: T.Text -> T.Text -> T.Text -> Action IO (Maybe Document)
|
||||||
findXeroOrder a i s = findOne (select ["address" =: a, "externalInvoice" =: i, "shortCode" =: s] "orders")
|
findXeroOrder a i s =
|
||||||
|
findOne
|
||||||
|
(select ["address" =: a, "externalInvoice" =: i, "shortCode" =: s] "orders")
|
||||||
|
|
||||||
findOrderById :: String -> Action IO (Maybe Document)
|
findOrderById :: String -> Action IO (Maybe Document)
|
||||||
|
findOrderById "0" = return Nothing
|
||||||
findOrderById i = findOne (select ["_id" =: (read i :: B.ObjectId)] "orders")
|
findOrderById i = findOne (select ["_id" =: (read i :: B.ObjectId)] "orders")
|
||||||
|
|
||||||
findAllOrders :: T.Text -> Action IO [Document]
|
findAllOrders :: T.Text -> Action IO [Document]
|
||||||
findAllOrders a = rest =<< find (select ["address" =: a] "orders") {sort = ["timestamp" =: (negate 1 :: Int)]}
|
findAllOrders a =
|
||||||
|
rest =<<
|
||||||
|
find
|
||||||
|
(select ["address" =: a] "orders")
|
||||||
|
{sort = ["timestamp" =: (negate 1 :: Int)]}
|
||||||
|
|
||||||
deleteOrder :: String -> Action IO ()
|
deleteOrder :: String -> Action IO ()
|
||||||
deleteOrder i = deleteOne (select ["_id" =: (read i :: B.ObjectId)] "orders")
|
deleteOrder i = deleteOne (select ["_id" =: (read i :: B.ObjectId)] "orders")
|
||||||
|
|
Loading…
Reference in a new issue