protected static void CopyForUpdate(CABankStatementDetail aDest, STMTTRN aSrc)
 {
     if (aSrc.TRNAMT >= 0)
     {
         aDest.CuryDebitAmt = aSrc.TRNAMT;
     }
     else
     {
         aDest.CuryCreditAmt = -aSrc.TRNAMT;
     }
 }
 protected static void Copy(CABankStatementDetail aDest, STMTTRN aSrc)
 {
     aDest.TranCode  = aSrc.TRNTYPE;
     aDest.TranDate  = aSrc.DTPOSTED;
     aDest.ExtTranID = aSrc.FITID;
     aDest.ExtRefNbr = (string.IsNullOrEmpty(aSrc.CHECKNUM) == false) ? aSrc.CHECKNUM
                         : (string.IsNullOrEmpty(aSrc.REFNUM) == false && aSrc.REFNUM.Length < 15 ? aSrc.REFNUM : (aSrc.FITID.Length < 15 ? aSrc.FITID : null));
     aDest.TranDesc = aSrc.NAME + " " + aSrc.MEMO;
     if (aSrc.HasPayeeInfo())
     {
         aDest.PayeeName       = aSrc.PAYEE.NAME;
         aDest.PayeeAddress1   = aSrc.PAYEE.ADDR1;
         aDest.PayeePostalCode = aSrc.PAYEE.POSTALCODE;
         aDest.PayeePhone      = aSrc.PAYEE.PHONE;
         aDest.PayeeState      = aSrc.PAYEE.STATE;
         aDest.PayeeCity       = aSrc.PAYEE.CITY;
     }
     else
     {
         aDest.PayeeName = aSrc.NAME;
     }
 }
		protected static void CopyForUpdate(CABankStatementDetail aDest, STMTTRN aSrc)
		{
			if (aSrc.TRNAMT >= 0)
			{
				aDest.CuryDebitAmt = aSrc.TRNAMT;
			}
			else
			{
				aDest.CuryCreditAmt = -aSrc.TRNAMT;
			}
		}
		protected static void Copy(CABankStatementDetail aDest, STMTTRN aSrc)
		{
			aDest.TranCode = aSrc.TRNTYPE;
			aDest.TranDate = aSrc.DTPOSTED;
			aDest.ExtTranID = aSrc.FITID;
			aDest.ExtRefNbr = (string.IsNullOrEmpty(aSrc.CHECKNUM) == false) ? aSrc.CHECKNUM
				: (string.IsNullOrEmpty(aSrc.REFNUM) == false && aSrc.REFNUM.Length < 15 ? aSrc.REFNUM : (aSrc.FITID.Length < 15 ? aSrc.FITID : null));
			aDest.TranDesc = aSrc.NAME + " " + aSrc.MEMO;
			if (aSrc.HasPayeeInfo())
			{
				aDest.PayeeName = aSrc.PAYEE.NAME;
				aDest.PayeeAddress1 = aSrc.PAYEE.ADDR1;
				aDest.PayeePostalCode = aSrc.PAYEE.POSTALCODE;
				aDest.PayeePhone = aSrc.PAYEE.PHONE;
				aDest.PayeeState = aSrc.PAYEE.STATE;
				aDest.PayeeCity = aSrc.PAYEE.CITY;
			}
			else
			{
				aDest.PayeeName = aSrc.NAME;
			}
		}
		protected void Export(CABankStatementEntry aGraph, STMTRS aAcctStatement, bool updateCurrent) 
		{
			if (!aAcctStatement.HasAccountInfo())
				throw new PXException(Messages.OFXImportErrorAccountInfoIsIncorrect);
			
			CashAccount acct = aGraph.cashAccountByExtRef.Select(aAcctStatement.IsBankAccount() ? aAcctStatement.BANKACCTFROM.ACCTID : aAcctStatement.CCACCTFROM.ACCTID);
			if (acct == null) throw new PXException(Messages.CashAccountWithExtRefNbrIsNotFoundInTheSystem, aAcctStatement.BANKACCTFROM.ACCTID);
			if (aGraph.casetup.Current.IgnoreCuryCheckOnImport != true && acct.CuryID != aAcctStatement.CURDEF)
                throw new PXException(Messages.CashAccountHasCurrencyDifferentFromOneInStatement, acct.CashAccountCD, acct.CuryID, aAcctStatement.CURDEF);

			foreach (OFXStatementReader.STMTTRN iTran in aAcctStatement.BANKTRANLIST.Trans)
			{
				string stmtRefNbr;
				if (aGraph.IsAlreadyImported(acct.CashAccountID, iTran.FITID, out stmtRefNbr))
				{
                    throw new PXException(Messages.TransactionWithFitIdHasAlreadyBeenImported, iTran.FITID, stmtRefNbr, acct.CashAccountCD, acct.Descr);
				}
			}

			CABankStatement statement = null;
			if (updateCurrent == false || aGraph.BankStatement.Current == null || aGraph.BankStatement.Current.CashAccountID == null)
			{
				aGraph.Clear();
				statement = new CABankStatement();
				statement.CashAccountID = acct.CashAccountID;
				statement = aGraph.BankStatement.Insert(statement);
				aGraph.BankStatement.Current = statement;
			}
			else
			{
				statement = aGraph.BankStatement.Current ;
				if (statement.CashAccountID.HasValue)
				{
					if (statement.CashAccountID != acct.CashAccountID)
					{
                        throw new PXException(Messages.ImportedStatementIsMadeForAnotherAccount, acct.CashAccountCD, acct.Descr);
					}
				}				
			}
			CABankStatement copy = (CABankStatement)aGraph.BankStatement.Cache.CreateCopy(statement);
			Copy(copy, aAcctStatement);
			statement = aGraph.BankStatement.Update(copy);
			foreach (OFXStatementReader.STMTTRN iTran in aAcctStatement.BANKTRANLIST.Trans)
			{
				CABankStatementDetail detail = new CABankStatementDetail();
				Copy(detail,iTran);				
				detail = aGraph.Details.Insert(detail);
				CABankStatementDetail detail1 = (CABankStatementDetail)aGraph.Details.Cache.CreateCopy(detail);
				//Must be done after to avoid overwriting of debit by credit
				CopyForUpdate(detail1, iTran);
				detail = aGraph.Details.Update(detail1);
			}
			aGraph.Save.Press();
		}
        protected void Export(CABankStatementEntry aGraph, STMTRS aAcctStatement, bool updateCurrent)
        {
            if (!aAcctStatement.HasAccountInfo())
            {
                throw new PXException(Messages.OFXImportErrorAccountInfoIsIncorrect);
            }

            CashAccount acct = aGraph.cashAccountByExtRef.Select(aAcctStatement.IsBankAccount() ? aAcctStatement.BANKACCTFROM.ACCTID : aAcctStatement.CCACCTFROM.ACCTID);

            if (acct == null)
            {
                throw new PXException(Messages.CashAccountWithExtRefNbrIsNotFoundInTheSystem, aAcctStatement.BANKACCTFROM.ACCTID);
            }
            if (aGraph.casetup.Current.IgnoreCuryCheckOnImport != true && acct.CuryID != aAcctStatement.CURDEF)
            {
                throw new PXException(Messages.CashAccountHasCurrencyDifferentFromOneInStatement, acct.CashAccountCD, acct.CuryID, aAcctStatement.CURDEF);
            }

            foreach (OFXStatementReader.STMTTRN iTran in aAcctStatement.BANKTRANLIST.Trans)
            {
                string stmtRefNbr;
                if (aGraph.IsAlreadyImported(acct.CashAccountID, iTran.FITID, out stmtRefNbr))
                {
                    throw new PXException(Messages.TransactionWithFitIdHasAlreadyBeenImported, iTran.FITID, stmtRefNbr, acct.CashAccountCD, acct.Descr);
                }
            }

            CABankStatement statement = null;

            if (updateCurrent == false || aGraph.BankStatement.Current == null || aGraph.BankStatement.Current.CashAccountID == null)
            {
                aGraph.Clear();
                statement = new CABankStatement();
                statement.CashAccountID = acct.CashAccountID;
                statement = aGraph.BankStatement.Insert(statement);
                aGraph.BankStatement.Current = statement;
            }
            else
            {
                statement = aGraph.BankStatement.Current;
                if (statement.CashAccountID.HasValue)
                {
                    if (statement.CashAccountID != acct.CashAccountID)
                    {
                        throw new PXException(Messages.ImportedStatementIsMadeForAnotherAccount, acct.CashAccountCD, acct.Descr);
                    }
                }
            }
            CABankStatement copy = (CABankStatement)aGraph.BankStatement.Cache.CreateCopy(statement);

            Copy(copy, aAcctStatement);
            statement = aGraph.BankStatement.Update(copy);
            foreach (OFXStatementReader.STMTTRN iTran in aAcctStatement.BANKTRANLIST.Trans)
            {
                CABankStatementDetail detail = new CABankStatementDetail();
                Copy(detail, iTran);
                detail = aGraph.Details.Insert(detail);
                CABankStatementDetail detail1 = (CABankStatementDetail)aGraph.Details.Cache.CreateCopy(detail);
                //Must be done after to avoid overwriting of debit by credit
                CopyForUpdate(detail1, iTran);
                detail = aGraph.Details.Update(detail1);
            }
            aGraph.Save.Press();
        }