foreignPair
  :: OfxIn
  -- ^
  -> OfxOut
  -- ^
  -> (Tranche.Postline (), Amount)
foreignPair inp out = (Tranche.Tranche () fields, amt)
  where
    fields = Fields.PostingFields
      { Fields._number = Nothing
      , Fields._flag = toFlag . _flag $ out
      , Fields._account = _foreignAccount out
      , Fields._fitid = X.pack . OFX.txFITID . _ofxTxn $ inp
      , Fields._tags = Seq.empty
      , Fields._uid = X.empty
      , Fields._trnType = Just . OFX.txTRNTYPE . _ofxTxn $ inp
      , Fields._origDate = Just . OFX.txDTPOSTED . _ofxTxn $ inp
      , Fields._memo = Seq.empty
      }
      where
        toFlag Nothing = ""
        toFlag (Just Cleared) = "C"
        toFlag (Just Reconciled) = "R"
    amt = Amount.Amount
      { Amount._commodity = _commodity out
      , Amount._qty = flipper . _qty $ inp
      }
      where
        flipper | _flipSign out = negate
                | otherwise = id

-- | Given the OFX data, create the 'Tranche.Postline' for the
-- offsetting posting.  The offsetting posting does not get an amount
-- because ultimately we use 'twoPostingBalanced', which does not
-- require an amount for the offsetting posting.
foreignPair
  :: OfxIn
  -- ^
  -> OfxOut
  -- ^
  -> (Tranche.Postline (), Amount)
foreignPair inp out = (Tranche.Tranche () fields, amt)
  where
    fields = Fields.PostingFields
      { Fields._number = Nothing
      , Fields._flag = toFlag . _flag $ out
      , Fields._account = _foreignAccount out
      , Fields._fitid = X.pack . OFX.txFITID . _ofxTxn $ inp
      , Fields._tags = Seq.empty
      , Fields._uid = X.empty
      , Fields._trnType = Just . OFX.txTRNTYPE . _ofxTxn $ inp
      , Fields._origDate = Just . OFX.txDTPOSTED . _ofxTxn $ inp
      , Fields._memo = Seq.empty
      }
      where
        toFlag Nothing = ""
        toFlag (Just Cleared) = "C"
        toFlag (Just Reconciled) = "R"
    amt = Amount.Amount
      { Amount._commodity = _commodity out
      , Amount._qty = flipper . _qty $ inp
      }
      where
        flipper | _flipSign out = negate
                | otherwise = id

-- | Given the OFX data, create the 'Tranche.Postline' for the
-- offsetting posting.  The offsetting posting does not get an amount
-- because ultimately we use 'twoPostingBalanced', which does not
-- require an amount for the offsetting posting.
foreignPair inp out = (Tranche.Tranche () fields, amt)
  where
    fields = Fields.PostingFields
      { Fields._number = Nothing
      , Fields._flag = toFlag . _flag $ out
      , Fields._account = _foreignAccount out
      , Fields._fitid = X.pack . OFX.txFITID . _ofxTxn $ inp
      , Fields._tags = Seq.empty
      , Fields._uid = X.empty
      , Fields._trnType = Just . OFX.txTRNTYPE . _ofxTxn $ inp
      , Fields._origDate = Just . OFX.txDTPOSTED . _ofxTxn $ inp
      , Fields._memo = Seq.empty
      }
      where
        toFlag Nothing = ""
        toFlag (Just Cleared) = "C"
        toFlag (Just Reconciled) = "R"
    amt = Amount.Amount
      { Amount._commodity = _commodity out
      , Amount._qty = flipper . _qty $ inp
      }
      where
        flipper | _flipSign out = negate
                | otherwise = id

-- | Given the OFX data, create the 'Tranche.Postline' for the
-- offsetting posting.  The offsetting posting does not get an amount
-- because ultimately we use 'twoPostingBalanced', which does not
-- require an amount for the offsetting posting.