getSupplierDigestR :: SupplierId -> Handler Html
getSupplierDigestR sId = do
  mSup <- runDB $ get sId
  case mSup of
    Just sup -> do
      master <- getYesod
      bevs <- runDB $ selectList [BeverageSupplier ==. (Just sId)] [Asc BeverageIdent]
      digests <- return $ map genBevDigest bevs
      w <- return $ [whamlet|$newline always
        <p>
          #{supplierIdent sup}<br>
          #{unTextarea $ supplierAddress sup}<br>
          #{supplierTel sup}<br>
          #{supplierEmail sup}<br>
        <hr>
        <p>
          <b>
            _{MsgCustomerId}: #{supplierCustomerId sup}
        <p>&nbsp;
        <table>
          <thead>
            <tr>
              <th>
                <span .transp>|
                _{MsgArtNr}
                <span .transp>|
              <th>
                _{MsgName}
                <span .transp>|
              <th>
                _{MsgVolume}
                <span .transp>|
              <th>
                _{MsgCrateCount}
                <span .transp>|
              <th>
                _{MsgPricePerCrate}
                <span .transp>|
              <th>
                _{MsgTotalValue}
                <span .transp>|
            <tr>
              <th colspan="6" .transp>
                |---:|---:|---:|---:|---:|---:|
            
          $forall dig <- digests
            $if bdCrates dig /= 0
              <tr>
                <td>
                  <span .transp>|
                  #{fromMaybe "" $ beverageArtNr $ bdBev dig}
                  <span .transp>|
                <td>#{beverageIdent $ bdBev dig}
                  <span .transp>|
                <td>#{formatIntVolume $ beverageMl $ bdBev dig}
                  <span .transp>|
                <td>#{T.pack $ show $ bdCrates dig}
                  <span .transp>|
                <td style="text-align: right;">#{formatIntCurrency $ fromMaybe 0 $ beveragePricePerCrate $ bdBev dig} #{appCurrency $ appSettings master}
                  <span .transp>|
                <td style="text-align: right;">#{formatIntCurrency $ bdTotal dig} #{appCurrency $ appSettings master}
                  <span .transp>|
          <tr>
            <td colspan="3">
              <span .transp>
                |&nbsp;
                |_{MsgTotalCrates}
              <span .transp>
                |&nbsp;
              <span .transp>|
            <td>#{T.pack $ show $ sum $ map bdCrates digests}
              <span .transp>|
            <td>_{MsgBuyValue}
              <span .transp>|
            <td style="text-align: right;">#{formatIntCurrency $ sum $ map bdTotal digests} #{appCurrency $ appSettings master}
              <span .transp>|
        |]
      tableLayout w
    Nothing -> do
      setMessageI MsgSupplierUnknown
      redirect SupplierR

-- tableLayout :: Widget -> WidgetT site0 IO ()
getSupplierDigestR :: SupplierId -> Handler Html
getSupplierDigestR sId = do
  mSup <- runDB $ get sId
  case mSup of
    Just sup -> do
      master <- getYesod
      bevs <- runDB $ selectList [BeverageSupplier ==. (Just sId)] [Asc BeverageIdent]
      digests <- return $ map genBevDigest bevs
      w <- return $ [whamlet|$newline always
        <p>
          #{supplierIdent sup}<br>
          #{unTextarea $ supplierAddress sup}<br>
          #{supplierTel sup}<br>
          #{supplierEmail sup}<br>
        <hr>
        <p>
          <b>
            _{MsgCustomerId}: #{supplierCustomerId sup}
        <p>&nbsp;
        <table>
          <thead>
            <tr>
              <th>
                <span .transp>|
                _{MsgArtNr}
                <span .transp>|
              <th>
                _{MsgName}
                <span .transp>|
              <th>
                _{MsgVolume}
                <span .transp>|
              <th>
                _{MsgCrateCount}
                <span .transp>|
              <th>
                _{MsgPricePerCrate}
                <span .transp>|
              <th>
                _{MsgTotalValue}
                <span .transp>|
            <tr>
              <th colspan="6" .transp>
                |---:|---:|---:|---:|---:|---:|
            
          $forall dig <- digests
            $if bdCrates dig /= 0
              <tr>
                <td>
                  <span .transp>|
                  #{fromMaybe "" $ beverageArtNr $ bdBev dig}
                  <span .transp>|
                <td>#{beverageIdent $ bdBev dig}
                  <span .transp>|
                <td>#{formatIntVolume $ beverageMl $ bdBev dig}
                  <span .transp>|
                <td>#{T.pack $ show $ bdCrates dig}
                  <span .transp>|
                <td style="text-align: right;">#{formatIntCurrency $ fromMaybe 0 $ beveragePricePerCrate $ bdBev dig} #{appCurrency $ appSettings master}
                  <span .transp>|
                <td style="text-align: right;">#{formatIntCurrency $ bdTotal dig} #{appCurrency $ appSettings master}
                  <span .transp>|
          <tr>
            <td colspan="3">
              <span .transp>
                |&nbsp;
                |_{MsgTotalCrates}
              <span .transp>
                |&nbsp;
              <span .transp>|
            <td>#{T.pack $ show $ sum $ map bdCrates digests}
              <span .transp>|
            <td>_{MsgBuyValue}
              <span .transp>|
            <td style="text-align: right;">#{formatIntCurrency $ sum $ map bdTotal digests} #{appCurrency $ appSettings master}
              <span .transp>|
        |]
      tableLayout w
    Nothing -> do
      setMessageI MsgSupplierUnknown
      redirect SupplierR

-- tableLayout :: Widget -> WidgetT site0 IO ()
getSupplierDigestR sId = do
  mSup <- runDB $ get sId
  case mSup of
    Just sup -> do
      master <- getYesod
      bevs <- runDB $ selectList [BeverageSupplier ==. (Just sId)] [Asc BeverageIdent]
      digests <- return $ map genBevDigest bevs
      w <- return $ [whamlet|$newline always
        <p>
          #{supplierIdent sup}<br>
          #{unTextarea $ supplierAddress sup}<br>
          #{supplierTel sup}<br>
          #{supplierEmail sup}<br>
        <hr>
        <p>
          <b>
            _{MsgCustomerId}: #{supplierCustomerId sup}
        <p>&nbsp;
        <table>
          <thead>
            <tr>
              <th>
                <span .transp>|
                _{MsgArtNr}
                <span .transp>|
              <th>
                _{MsgName}
                <span .transp>|
              <th>
                _{MsgVolume}
                <span .transp>|
              <th>
                _{MsgCrateCount}
                <span .transp>|
              <th>
                _{MsgPricePerCrate}
                <span .transp>|
              <th>
                _{MsgTotalValue}
                <span .transp>|
            <tr>
              <th colspan="6" .transp>
                |---:|---:|---:|---:|---:|---:|
            
          $forall dig <- digests
            $if bdCrates dig /= 0
              <tr>
                <td>
                  <span .transp>|
                  #{fromMaybe "" $ beverageArtNr $ bdBev dig}
                  <span .transp>|
                <td>#{beverageIdent $ bdBev dig}
                  <span .transp>|
                <td>#{formatIntVolume $ beverageMl $ bdBev dig}
                  <span .transp>|
                <td>#{T.pack $ show $ bdCrates dig}
                  <span .transp>|
                <td style="text-align: right;">#{formatIntCurrency $ fromMaybe 0 $ beveragePricePerCrate $ bdBev dig} #{appCurrency $ appSettings master}
                  <span .transp>|
                <td style="text-align: right;">#{formatIntCurrency $ bdTotal dig} #{appCurrency $ appSettings master}
                  <span .transp>|
          <tr>
            <td colspan="3">
              <span .transp>
                |&nbsp;
                |_{MsgTotalCrates}
              <span .transp>
                |&nbsp;
              <span .transp>|
            <td>#{T.pack $ show $ sum $ map bdCrates digests}
              <span .transp>|
            <td>_{MsgBuyValue}
              <span .transp>|
            <td style="text-align: right;">#{formatIntCurrency $ sum $ map bdTotal digests} #{appCurrency $ appSettings master}
              <span .transp>|
        |]
      tableLayout w
    Nothing -> do
      setMessageI MsgSupplierUnknown
      redirect SupplierR

-- tableLayout :: Widget -> WidgetT site0 IO ()