summaryrefslogtreecommitdiff
path: root/Handler
diff options
context:
space:
mode:
Diffstat (limited to 'Handler')
-rw-r--r--Handler/Common.hs100
-rw-r--r--Handler/List.hs20
-rw-r--r--Handler/OpenItem.hs2
3 files changed, 96 insertions, 26 deletions
diff --git a/Handler/Common.hs b/Handler/Common.hs
index a1ae34b..990732d 100644
--- a/Handler/Common.hs
+++ b/Handler/Common.hs
@@ -28,9 +28,19 @@ import Handler.Common.Types
28 28
29import Text.Julius (RawJS(..)) 29import Text.Julius (RawJS(..))
30 30
31import Data.List.NonEmpty (NonEmpty)
32import Data.Semigroup hiding (First(..))
33import Data.Monoid (First(..))
34
31dayFormat :: Day -> String 35dayFormat :: Day -> String
32dayFormat = formatTime defaultTimeLocale "%e. %b %y" 36dayFormat = formatTime defaultTimeLocale "%e. %b %y"
33 37
38data DayFormConfig = DayFormConfig
39 { dfNever :: Bool
40 , dfUnknown :: Bool
41 , dfKnown :: Bool
42 }
43
34itemForm :: Maybe Item -- ^ Update existing item or insert new? 44itemForm :: Maybe Item -- ^ Update existing item or insert new?
35 -> Html -> MForm Handler (FormResult (WithType Item), Widget) 45 -> Html -> MForm Handler (FormResult (WithType Item), Widget)
36itemForm proto identView = do 46itemForm proto identView = do
@@ -41,12 +51,12 @@ itemForm proto identView = do
41 let kt kWidget tWidget = 51 let kt kWidget tWidget =
42 [whamlet| 52 [whamlet|
43 <div .td>^{kWidget} 53 <div .td>^{kWidget}
44 <div .td>^{tWidget} 54 <div .td>^{tWidget}
45 |] 55 |]
46 ((kindRes, typeRes), typedKindWidget) <- typedKindField kt ((itemKind <$> proto), t) 56 ((kindRes, typeRes), typedKindWidget) <- typedKindField kt ((itemKind <$> proto), t)
47 (boughtRes, boughtWidget) <- dayForm (maybe (Just $ Just today) Just $ fmap itemBought proto) "Unknown" 57 (boughtRes, boughtWidget) <- dayForm (Just . fromMaybe (DateKnown today) $ itemBought <$> proto) $ DayFormConfig False True True
48 (expiresRes, expiresWidget) <- dayForm (fmap itemExpires proto) "Never" 58 (expiresRes, expiresWidget) <- dayForm (itemExpires <$> proto) $ DayFormConfig True False True
49 (openedRes, openedWidget) <- dayForm (fmap itemOpened proto) "Never" 59 (openedRes, openedWidget) <- dayForm (itemOpened <$> proto) $ DayFormConfig True True True
50 60
51 let itemRes = do 61 let itemRes = do
52 itemKind <- kindRes 62 itemKind <- kindRes
@@ -66,33 +76,79 @@ itemForm proto identView = do
66 <div .td>^{openedWidget} 76 <div .td>^{openedWidget}
67 |] 77 |]
68 where 78 where
69 dayForm :: Maybe (Maybe Day) -> String -> MForm Handler (FormResult (Maybe Day), Widget) 79 dayForm :: Maybe ItemDate -> DayFormConfig -> MForm Handler (FormResult ItemDate, Widget)
70 dayForm proto label = do 80 dayForm proto DayFormConfig{..} = do
71 today <- utctDay <$> liftIO getCurrentTime 81 today <- utctDay <$> liftIO getCurrentTime
72 82
73 checkboxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique 83 let mWhen = bool (Nothing <$) (fmap Just)
84
85 neverBoxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique
86 unknownBoxId <- ("check" <>) . show . hashUnique <$> liftIO newUnique
87 groupId <- ("dateGroup" <>) . show . hashUnique <$> liftIO newUnique
74 88
75 (fmap (fromMaybe False) -> isNothingRes, isNothingView) <- 89 dNever <- mWhen dfNever $
76 mopt checkBoxField ("" { fsId = Just $ Text.pack checkboxId }) . Just . Just . fromMaybe True $ fmap isNothing proto 90 mopt checkBoxField ("" { fsId = Just $ Text.pack neverBoxId }) . Just . Just . fromMaybe True $ fmap isNever proto
77 (dayRes, dayView) <- 91 dUnknown <- mWhen dfUnknown $
78 mreq dayField "" . Just . fromMaybe today $ join proto 92 mopt checkBoxField ("" { fsId = Just $ Text.pack unknownBoxId }) . Just . Just . fromMaybe False $ fmap isUnknown proto
93 dDay <- mWhen dfKnown $
94 mopt dayField "" . Just . Just $ case proto of
95 Just (DateKnown d) -> d
96 _ -> today
79 97
80 let res = (bool Just (const Nothing) <$> isNothingRes) <*> dayRes 98 let res = resFromMaybe . fromMaybe FormMissing . fmap (fmap getFirst) $ mconcat
99 [ fmap (fmap (First . bool Nothing (Just DateNever) . fromMaybe False) . fst) dNever
100 , fmap (fmap (First . bool Nothing (Just DateUnknown) . fromMaybe False) . fst) dUnknown
101 , fmap (fmap (First . fmap DateKnown) . fst) dDay
102 ]
103 resFromMaybe (FormSuccess Nothing) = FormFailure ["Missing required information"]
104 resFromMaybe (FormSuccess (Just x)) = FormSuccess x
105 resFromMaybe FormMissing = FormMissing
106 resFromMaybe (FormFailure es) = FormFailure es
81 return . (res, ) $ do 107 return . (res, ) $ do
108 toWidget $
109 [julius|
110 $(function () {
111 var updateInput = function() {
112 $('##{rawJS groupId} :input').filter(':not(:checkbox)').prop("disabled", $('##{rawJS groupId} :checkbox').filter(':checked').length > 0);
113 };
114
115 $('##{rawJS groupId} :checkbox').change(function() {
116 if (this.checked) {
117 $('##{rawJS groupId} :checkbox').not(this).prop('checked', false);
118 }
119 updateInput();
120 });
121
122 updateInput();
123 });
124 |]
125 let width = length $ (filter id [ isJust dNever, isJust dUnknown ] :: [Bool])
82 [whamlet| 126 [whamlet|
83 $newline never 127 $newline never
84 <div .table> 128 <table ##{groupId} .dayField>
85 <div .tr> 129 <tr>
86 <label for=#{checkboxId} .checkbox .td> 130 $maybe (_, isNeverView) <- dNever
87 ^{fvInput isNothingView} 131 <td>
88 <span> 132 <label for=#{neverBoxId} .checkbox>
89 #{label} 133 ^{fvInput isNeverView}
90 <div .tr> 134 <span>
91 <div .td .dayInput>^{fvInput dayView} 135 Never
136 $maybe (_, isUnknownView) <- dUnknown
137 <td>
138 <label for=#{unknownBoxId} .checkbox>
139 ^{fvInput isUnknownView}
140 <span>
141 Unknown
142 $maybe (_, dayView) <- dDay
143 <tr>
144 <td .dayInput :width > 0:colspan=#{width}>
145 ^{fvInput dayView}
92 |] 146 |]
93 147
94inventoryListing :: InventoryState -> Widget 148inventoryListing :: InventoryState -> Widget
95inventoryListing InventoryState{ invFormState = formState, ..} = $(widgetFile "inventoryListing") 149inventoryListing InventoryState{ invFormState = formState, ..} = do
150 today <- liftIO $ utctDay <$> getCurrentTime
151 $(widgetFile "inventoryListing")
96 152
97referenceForm :: Maybe Reference -- ^ Update existing item or insert new? 153referenceForm :: Maybe Reference -- ^ Update existing item or insert new?
98 -> Html -> MForm Handler (FormResult (WithType Reference), Widget) 154 -> Html -> MForm Handler (FormResult (WithType Reference), Widget)
@@ -188,7 +244,7 @@ kinds = do
188 stock <- runDB $ selectList [] [] 244 stock <- runDB $ selectList [] []
189 reference <- runDB $ selectList [] [] 245 reference <- runDB $ selectList [] []
190 246
191 return . Set.fromList $ concat 247 return . Set.fromList $ (concat :: [[a]] -> [a])
192 [ [ itemKind | Entity _ Item{..} <- stock ] 248 [ [ itemKind | Entity _ Item{..} <- stock ]
193 , [ referenceKind | Entity _ Reference{..} <- reference ] 249 , [ referenceKind | Entity _ Reference{..} <- reference ]
194 ] 250 ]
diff --git a/Handler/List.hs b/Handler/List.hs
index 70f323a..4209651 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