summaryrefslogtreecommitdiff
path: root/Handler
diff options
context:
space:
mode:
Diffstat (limited to 'Handler')
-rw-r--r--Handler/Common.hs102
-rw-r--r--Handler/List.hs20
-rw-r--r--Handler/OpenItem.hs2
3 files changed, 97 insertions, 27 deletions
diff --git a/Handler/Common.hs b/Handler/Common.hs
index c2788e8..65e6ce1 100644
--- a/Handler/Common.hs
+++ b/Handler/Common.hs
@@ -34,6 +34,10 @@ import Database.Persist.Sql (fromSqlKey)
34import qualified Web.Hashids as HID 34import qualified Web.Hashids as HID
35import Data.List ((\\)) 35import Data.List ((\\))
36 36
37import Data.List.NonEmpty (NonEmpty)
38import Data.Semigroup hiding (First(..))
39import Data.Monoid (First(..))
40
37humanId :: ItemId -> Text 41humanId :: ItemId -> Text
38humanId = Text.pack . CBS.unpack . HID.encode ctx . fromIntegral . fromSqlKey 42humanId = Text.pack . CBS.unpack . HID.encode ctx . fromIntegral . fromSqlKey
39 where 43 where
@@ -42,6 +46,12 @@ humanId = Text.pack . CBS.unpack . HID.encode ctx . fromIntegral . fromSqlKey
42dayFormat :: Day -> String 46dayFormat :: Day -> String
43dayFormat = formatTime defaultTimeLocale "%e. %b %y" 47dayFormat = formatTime defaultTimeLocale "%e. %b %y"
44 48
49data DayFormConfig = DayFormConfig
50 { dfNever :: Bool
51 , dfUnknown :: Bool
52 , dfKnown :: Bool
53 }
54
45itemForm :: Maybe Item -- ^ Update existing item or insert new? 55itemForm :: Maybe Item -- ^ Update existing item or insert new?
46 -> Html -> MForm Handler (FormResult (WithType Item), Widget) 56 -> Html -> MForm Handler (FormResult (WithType Item), Widget)
47itemForm proto identView = do 57itemForm proto identView = do
@@ -52,12 +62,12 @@ itemForm proto identView = do
52 let kt kWidget tWidget = 62 let kt kWidget tWidget =
53 [whamlet| 63 [whamlet|
54 <div .td>^{kWidget} 64 <div .td>^{kWidget}
55 <div .td>^{tWidget} 65 <div .td>^{tWidget}
56 |] 66 |]
57 ((kindRes, typeRes), typedKindWidget) <- typedKindField kt ((itemKind <$> proto), t) 67 ((kindRes, typeRes), typedKindWidget) <- typedKindField kt ((itemKind <$> proto), t)
58 (boughtRes, boughtWidget) <- dayForm (maybe (Just $ Just today) Just $ fmap itemBought proto) "Unknown" 68 (boughtRes, boughtWidget) <- dayForm (Just . fromMaybe (DateKnown today) $ itemBought <$> proto) $ DayFormConfig False True True
59 (expiresRes, expiresWidget) <- dayForm (fmap itemExpires proto) "Never" 69 (expiresRes, expiresWidget) <- dayForm (itemExpires <$> proto) $ DayFormConfig True False True
60 (openedRes, openedWidget) <- dayForm (fmap itemOpened proto) "Never" 70 (openedRes, openedWidget) <- dayForm (itemOpened <$> proto) $ DayFormConfig True True True
61 71
62 let itemRes = do 72 let itemRes = do
63 itemKind <- kindRes 73 itemKind <- kindRes
@@ -77,33 +87,79 @@ itemForm proto identView = do
77 <div .td>^{openedWidget} 87 <div .td>^{openedWidget}
78 |] 88 |]
79 where 89 where
80 dayForm :: Maybe (Maybe Day) -> String -> MForm Handler (FormResult (Maybe Day), Widget) 90 dayForm :: Maybe ItemDate -> DayFormConfig -> MForm Handler (FormResult ItemDate, Widget)
81 dayForm proto label = do 91 dayForm proto DayFormConfig{..} = do
82 today <- utctDay <$> liftIO getCurrentTime 92 today <- utctDay <$> liftIO getCurrentTime
83 93
84 checkboxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique 94 let mWhen = bool (Nothing <$) (fmap Just)
85
86 (fmap (fromMaybe False) -> isNothingRes, isNothingView) <-
87 mopt checkBoxField ("" { fsId = Just $ Text.pack checkboxId }) . Just . Just . fromMaybe True $ fmap isNothing proto
88 (dayRes, dayView) <-
89 mreq dayField "" . Just . fromMaybe today $ join proto
90 95
91 let res = (bool Just (const Nothing) <$> isNothingRes) <*> dayRes 96 neverBoxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique
97 unknownBoxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique
98 groupId <- ("dateGroup" <>) . show . hashUnique <$> liftIO newUnique
99
100 dNever <- mWhen dfNever $
101 mopt checkBoxField ("" { fsId = Just $ Text.pack neverBoxId }) . Just . Just . fromMaybe True $ fmap isNever proto
102 dUnknown <- mWhen dfUnknown $
103 mopt checkBoxField ("" { fsId = Just $ Text.pack unknownBoxId }) . Just . Just . fromMaybe False $ fmap isUnknown proto
104 dDay <- mWhen dfKnown $
105 mopt dayField "" . Just . Just $ case proto of
106 Just (DateKnown d) -> d
107 _ -> today
108
109 let res = resFromMaybe . fromMaybe FormMissing . fmap (fmap getFirst) $ mconcat
110 [ fmap (fmap (First . bool Nothing (Just DateNever) . fromMaybe False) . fst) dNever
111 , fmap (fmap (First . bool Nothing (Just DateUnknown) . fromMaybe False) . fst) dUnknown
112 , fmap (fmap (First . fmap DateKnown) . fst) dDay
113 ]
114 resFromMaybe (FormSuccess Nothing) = FormFailure ["Missing required information"]
115 resFromMaybe (FormSuccess (Just x)) = FormSuccess x
116 resFromMaybe FormMissing = FormMissing
117 resFromMaybe (FormFailure es) = FormFailure es
92 return . (res, ) $ do 118 return . (res, ) $ do
119 toWidget $
120 [julius|
121 $(function () {
122 var updateInput = function() {
123 $('##{rawJS groupId} :input').filter(':not(:checkbox)').prop("disabled", $('##{rawJS groupId} :checkbox').filter(':checked').length > 0);
124 };
125
126 $('##{rawJS groupId} :checkbox').change(function() {
127 if (this.checked) {
128 $('##{rawJS groupId} :checkbox').not(this).prop('checked', false);
129 }
130 updateInput();
131 });
132
133 updateInput();
134 });
135 |]
136 let width = length $ (filter id [ isJust dNever, isJust dUnknown ] :: [Bool])
93 [whamlet| 137 [whamlet|
94 $newline never 138 $newline never
95 <div .table> 139 <table ##{groupId} .dayField>
96 <div .tr> 140 <tr>
97 <label for=#{checkboxId} .checkbox .td> 141 $maybe (_, isNeverView) <- dNever
98 ^{fvInput isNothingView} 142 <td>
99 <span> 143 <label for=#{neverBoxId} .checkbox>
100 #{label} 144 ^{fvInput isNeverView}
101 <div .tr> 145 <span>
102 <div .td .dayInput>^{fvInput dayView} 146 Never
147 $maybe (_, isUnknownView) <- dUnknown
148 <td>
149 <label for=#{unknownBoxId} .checkbox>
150 ^{fvInput isUnknownView}
151 <span>
152 Unknown
153 $maybe (_, dayView) <- dDay
154 <tr>
155 <td .dayInput :width > 0:colspan=#{width}>
156 ^{fvInput dayView}
103 |] 157 |]
104 158
105inventoryListing :: InventoryState -> Widget 159inventoryListing :: InventoryState -> Widget
106inventoryListing InventoryState{ invFormState = formState, ..} = $(widgetFile "inventoryListing") 160inventoryListing InventoryState{ invFormState = formState, ..} = do
161 today <- liftIO $ utctDay <$> getCurrentTime
162 $(widgetFile "inventoryListing")
107 163
108referenceForm :: Maybe Reference -- ^ Update existing item or insert new? 164referenceForm :: Maybe Reference -- ^ Update existing item or insert new?
109 -> Html -> MForm Handler (FormResult (WithType Reference), Widget) 165 -> Html -> MForm Handler (FormResult (WithType Reference), Widget)
@@ -199,7 +255,7 @@ kinds = do
199 stock <- runDB $ selectList [] [] 255 stock <- runDB $ selectList [] []
200 reference <- runDB $ selectList [] [] 256 reference <- runDB $ selectList [] []
201 257
202 return . Set.fromList $ concat 258 return . Set.fromList $ (concat :: [[a]] -> [a])
203 [ [ itemKind | Entity _ Item{..} <- stock ] 259 [ [ itemKind | Entity _ Item{..} <- stock ]
204 , [ referenceKind | Entity _ Reference{..} <- reference ] 260 , [ referenceKind | Entity _ Reference{..} <- reference ]
205 ] 261 ]
diff --git a/Handler/List.hs b/Handler/List.hs
index 21b735b..7ab4ebe 100644
--- a/Handler/List.hs
+++ b/Handler/List.hs
@@ -19,9 +19,23 @@ import Database.Persist.Sql (Single(..), rawSql)
19import Thermoprint.Client 19import Thermoprint.Client
20 20
21list :: Handler (Set (WithType Text)) 21list :: Handler (Set (WithType Text))
22list = do 22list = runDB $ do
23 (map (uncurry WithType . bimap unSingle unSingle) -> kinds) <- runDB $ rawSql "select reference.kind, kind.type from reference,kind where (not exists (select * from item where COALESCE(item.expires >= CURRENT_DATE, TRUE) and (item.norm_kind = reference.norm_kind))) and (reference.norm_kind = kind.norm_kind)" [] 23 today <- liftIO $ utctDay <$> getCurrentTime
24 return $ Set.fromList kinds 24
25 items <- map entityVal <$> selectList [] []
26 references <- Set.fromList <$> (withTypes . fmap entityVal =<< selectList [] [])
27
28 let
29 references' = Set.filter (isNothing . flip find items . matches) references
30 matches (Reference{..} `WithType` _) Item{..}
31 | today `isBefore` itemExpires = itemNormKind == referenceNormKind
32 | otherwise = False
33
34 isBefore _ DateNever = True
35 isBefore _ DateUnknown = False
36 isBefore d1 (DateKnown d2) = d1 < d2
37
38 return $ Set.map (fmap referenceKind) references'
25 39
26mkPrintout :: Set (WithType Text) -> Printout 40mkPrintout :: Set (WithType Text) -> Printout
27mkPrintout list = Printout ps 41mkPrintout list = Printout ps
diff --git a/Handler/OpenItem.hs b/Handler/OpenItem.hs
index 468c6ec..3b1dfeb 100644
--- a/Handler/OpenItem.hs
+++ b/Handler/OpenItem.hs
@@ -5,7 +5,7 @@ import Import
5postOpenItemR :: ItemId -> Handler TypedContent 5postOpenItemR :: ItemId -> Handler TypedContent
6postOpenItemR itemId = do 6postOpenItemR itemId = do
7 today <- utctDay <$> liftIO getCurrentTime 7 today <- utctDay <$> liftIO getCurrentTime
8 result <- fmap (Entity itemId) . runDB $ updateGet itemId [ ItemOpened =. Just today 8 result <- fmap (Entity itemId) . runDB $ updateGet itemId [ ItemOpened =. DateKnown today
9 ] 9 ]
10 selectRep $ do 10 selectRep $ do
11 provideJson result 11 provideJson result