public void TestCurrencyAmount() { // test to string CurrencyAmount amount = new CurrencyAmount(123.45); Assert.AreEqual("123.45", amount.ToString()); // test parse amount = CurrencyAmount.Parse("123.45"); Assert.AreEqual("123.45", amount.ToString()); // test add CurrencyAmount amount2 = new CurrencyAmount(543.21); amount = new CurrencyAmount(123.45); Assert.AreEqual("666.66", CurrencyAmount.Add(amount, amount2).ToString()); amount.AddTo(amount2); Assert.AreEqual("666.66", amount.ToString()); // test minus amount2 = new CurrencyAmount(543.21); amount = new CurrencyAmount(123.45); Assert.AreEqual("-419.76", CurrencyAmount.Minus(amount, amount2).ToString()); Assert.AreEqual("419.76", CurrencyAmount.Minus(amount2, amount).ToString()); Assert.AreEqual("0.00", CurrencyAmount.Minus(amount2, amount2).ToString()); // is zero Assert.AreEqual(true, CurrencyAmount.Minus(amount2, amount2).IsZero()); }
/// <summary> /// Parse master data from XML /// </summary> /// <param name="coreDriver"></param> /// <param name="elem"></param> /// <returns></returns> /// <exception cref="MasterDataFileFormatException">Master Data file exception</exception> /// <exception cref="ArgumentNullException">Argument is null</exception> /// <exception cref="SystemException">Bug</exception> public override MasterDataBase ParseMasterData(CoreDriver coreDriver, XElement elem) { XAttribute id = elem.Attribute(MasterDataUtils.XML_ID); XAttribute descp = elem.Attribute(MasterDataUtils.XML_DESCP); XAttribute bankAccStr = elem.Attribute(MasterDataUtils.XML_BANK_ACCOUNT); XAttribute initAmountAttr = elem.Attribute(MasterDataUtils.XML_INIT_AMOUNT); MasterDataIdentity_GLAccount identity; MasterDataIdentity bankAccId = null; try { identity = new MasterDataIdentity_GLAccount( id.Value); // bank account if (bankAccStr != null) { bankAccId = new MasterDataIdentity( bankAccStr.Value); } } catch (Exception e) { _coreDriver.logDebugInfo(this.GetType(), 150, e.Message, MessageType.ERRO); throw new MasterDataFileFormatException(MasterDataType.GL_ACCOUNT); } CurrencyAmount initAmount = new CurrencyAmount(); if (initAmountAttr != null) { try { initAmount = CurrencyAmount.Parse(initAmountAttr.Value); } catch (CurrencyAmountFormatException e) { _coreDriver.logDebugInfo(this.GetType(), 150, e.Message, MessageType.ERRO); throw new MasterDataFileFormatException(MasterDataType.GL_ACCOUNT); } } try { GLAccountMasterData glAccount; if (bankAccId == null) { glAccount = (GLAccountMasterData)this.CreateNewMasterDataBase( identity, descp.Value); } else { glAccount = (GLAccountMasterData)this.CreateNewMasterDataBase( identity, descp.Value, bankAccId); } glAccount.setInitAmountInternal(initAmount); _coreDriver .logDebugInfo( this.GetType(), 167, String.Format("Parse G/L account ({0}).", glAccount.Identity.ToString()), MessageType.INFO); return(glAccount); } catch (Exception e) { throw new SystemException(e); } }
/// <summary> /// parse XML to item entity /// </summary> /// <param name="coreDriver"></param> /// <param name="management"></param> /// <param name="head"></param> /// <param name="elem"></param> /// <returns></returns> /// <exception cref="TransactionDataFileFormatException"></exception> /// <exception cref="SystemException"></exception> public static ItemEntity Parse(CoreDriver coreDriver, MasterDataManagement management, HeadEntity head, XElement elem) { #region get line number XAttribute lineNumStr = elem.Attribute(TransDataUtils.XML_LINE_NUM); if (lineNumStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 363, String.Format( "Field {0} is missing in.", TransDataUtils.XML_LINE_NUM), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } int lineNum; if (!Int32.TryParse(lineNumStr.Value, out lineNum)) { coreDriver.logDebugInfo(typeof(HeadEntity), 363, String.Format( "Format of field {0} is error.", TransDataUtils.XML_LINE_NUM), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } #endregion #region get account type XAttribute typeStr = elem.Attribute(TransDataUtils.XML_ACCOUNT_TYPE); if (typeStr == null) { coreDriver .logDebugInfo(typeof(HeadEntity), 375, String.Format( "Field {0} is missing in.", TransDataUtils.XML_ACCOUNT_TYPE), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } if (typeStr.Value.Length != 1 || (typeStr.Value[0] != (char)AccountType.CUSTOMER && typeStr.Value[0] != (char)AccountType.GL_ACCOUNT && typeStr.Value[0] != (char)AccountType.VENDOR)) { coreDriver .logDebugInfo(typeof(HeadEntity), 375, String.Format( "Format of field {0} is error.", TransDataUtils.XML_ACCOUNT_TYPE), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } AccountType type = (AccountType)typeStr.Value[0]; #endregion #region amount XAttribute amountStr = elem.Attribute(TransDataUtils.XML_AMOUNT); if (amountStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 375, String.Format( "Field {0} is missing in.", TransDataUtils.XML_AMOUNT), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } CurrencyAmount amount; try { amount = CurrencyAmount.Parse(amountStr.Value); } catch (Exception e) { coreDriver .logDebugInfo(typeof(HeadEntity), 375, e.Message, MessageType.ERRO); throw new TransactionDataFileFormatException(""); } #endregion #region credit debit indicator XAttribute cdIndStr = elem.Attribute(TransDataUtils.XML_CD_INDICATOR); if (cdIndStr == null) { coreDriver .logDebugInfo(typeof(HeadEntity), 375, String.Format( "Field {0} is missing in.", TransDataUtils.XML_CD_INDICATOR), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } if (cdIndStr.Value.Length != 1 || (cdIndStr.Value[0] != (char)CreditDebitIndicator.CREDIT && cdIndStr.Value[0] != (char)CreditDebitIndicator.DEBIT)) { coreDriver .logDebugInfo(typeof(HeadEntity), 375, String.Format( "Format of field {0} is error.", TransDataUtils.XML_CD_INDICATOR), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } CreditDebitIndicator indicator = (CreditDebitIndicator)cdIndStr.Value[0]; #endregion #region G/L account XAttribute glAccountStr = elem.Attribute(TransDataUtils.XML_GL_ACCOUNT); if (glAccountStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 414, String.Format( "Field {0} is missing in.", TransDataUtils.XML_GL_ACCOUNT), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } MasterDataIdentity_GLAccount glAccount; try { glAccount = new MasterDataIdentity_GLAccount( glAccountStr.Value); } catch (Exception e) { coreDriver .logDebugInfo(typeof(HeadEntity), 375, e.Message, MessageType.ERRO); throw new TransactionDataFileFormatException(""); } #endregion // vendor XAttribute vendorStr = elem.Attribute(TransDataUtils.XML_VENDOR); // customer XAttribute customerStr = elem.Attribute(TransDataUtils.XML_CUSTOMER); XAttribute businessAreaStr = elem .Attribute(TransDataUtils.XML_BUSINESS_AREA); try { ItemEntity newItem = new ItemEntity(coreDriver, management, head, lineNum); #region set account, vendor and customer if (type == AccountType.GL_ACCOUNT) { newItem.SetGLAccount(glAccount); } else if (type == AccountType.VENDOR) { if (vendorStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 414, String .Format("Field %s is missing in.", TransDataUtils.XML_VENDOR), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } MasterDataIdentity vendorId = new MasterDataIdentity( vendorStr.Value); newItem.SetVendor(vendorId, glAccount); } else if (type == AccountType.CUSTOMER) { if (customerStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 414, String .Format("Field %s is missing in.", TransDataUtils.XML_CUSTOMER), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } MasterDataIdentity customerId = new MasterDataIdentity( customerStr.Value); newItem.SetCustomer(customerId, glAccount); } #endregion newItem.SetAmount(indicator, amount); if (businessAreaStr != null) { newItem.SetBusinessArea(new MasterDataIdentity(businessAreaStr.Value)); } coreDriver.logDebugInfo( typeof(ItemEntity), 455, String.Format("Parsed line Item {0}.", newItem.LineNum), MessageType.INFO); return(newItem); } catch (IdentityTooLong e) { coreDriver.logDebugInfo(typeof(ItemEntity), 463, e.Message, MessageType.ERRO); throw new TransactionDataFileFormatException(""); } catch (IdentityNoData e) { coreDriver.logDebugInfo(typeof(ItemEntity), 463, e.Message, MessageType.ERRO); throw new TransactionDataFileFormatException(""); } catch (IdentityInvalidChar e) { coreDriver.logDebugInfo(typeof(ItemEntity), 463, e.Message, MessageType.ERRO); throw new TransactionDataFileFormatException(""); } catch (ArgumentNullException e) { coreDriver.logDebugInfo(typeof(ItemEntity), 463, e.Message, MessageType.ERRO); throw new SystemException(e); } catch (MasterDataIdentityNotDefined e) { coreDriver.logDebugInfo(typeof(ItemEntity), 463, e.Message, MessageType.ERRO); throw new TransactionDataFileFormatException(""); } catch (CurrencyAmountFormatException e) { throw new TransactionDataFileFormatException(e.Message); } }
/// <summary> /// Set value /// </summary> /// <param name="fieldName"></param> /// <param name="value"></param> /// <exception cref="NoFieldNameException"></exception> /// <exception cref="NotInValueRangeException"></exception> public void SetValue(String fieldName, Object value) { if (_isSaved) { return; } if (value == null) { throw new NotInValueRangeException(fieldName, ""); } if (fieldName.Equals(CUSTOMER)) { MasterDataIdentity customer = value as MasterDataIdentity; if (customer == null) { throw new NotInValueRangeException(fieldName, value); } setCustomer(customer); } else if (fieldName.Equals(GL_ACCOUNT)) { MasterDataIdentity_GLAccount glAccount = value as MasterDataIdentity_GLAccount; if (glAccount == null) { throw new NotInValueRangeException(fieldName, value); } setGLAccount(glAccount); } else if (fieldName.Equals(REC_ACC)) { MasterDataIdentity_GLAccount recAcc = value as MasterDataIdentity_GLAccount; if (recAcc == null) { throw new NotInValueRangeException(fieldName, value); } setRecAccount(recAcc); } else if (fieldName.Equals(EntryTemplate.POSTING_DATE)) { if (!(value is DateTime)) { throw new NotInValueRangeException(fieldName, value); } DateTime date = (DateTime)value; _date = date; } else if (fieldName.Equals(EntryTemplate.AMOUNT)) { try { CurrencyAmount amount = CurrencyAmount.Parse(value.ToString()); if (amount.IsZero() || amount.IsNegative()) { throw new NotInValueRangeException(fieldName, value); } _amount = amount; } catch (CurrencyAmountFormatException) { throw new NotInValueRangeException(fieldName, value); } } else if (fieldName.Equals(EntryTemplate.TEXT)) { _text = value.ToString(); } else { throw new NoFieldNameException(fieldName); } }
/// <summary> /// set value /// </summary> /// <param name="fieldName"></param> /// <param name="value"></param> /// <exception cref="NoFieldNameException">No such field name</exception> /// <exception cref="NotInValueRangeException">The value is not supported</exception> public void SetValue(String fieldName, Object value) { if (_isSaved) { return; } if (value == null) { throw new NotInValueRangeException(fieldName, ""); } if (fieldName.Equals(DST_ACCOUNT)) { if (!(value is MasterDataIdentity_GLAccount)) { throw new NotInValueRangeException(fieldName, value); } MasterDataIdentity_GLAccount id = (MasterDataIdentity_GLAccount)value; setDstAccount(id); } else if (fieldName.Equals(SRC_ACCOUNT)) { if (!(value is MasterDataIdentity_GLAccount)) { throw new NotInValueRangeException(fieldName, value); } MasterDataIdentity_GLAccount id = (MasterDataIdentity_GLAccount)value; setSourceAccount(id); } else if (fieldName.Equals(EntryTemplate.TEXT)) { _text = value.ToString(); } else if (fieldName.Equals(EntryTemplate.AMOUNT)) { try { CurrencyAmount amount = CurrencyAmount.Parse(value.ToString()); if (amount.IsZero() || amount.IsNegative()) { throw new NotInValueRangeException(fieldName, value); } _amount = amount; } catch (CurrencyAmountFormatException) { throw new NotInValueRangeException(fieldName, value); } } else if (fieldName.Equals(EntryTemplate.POSTING_DATE)) { if (!(value is DateTime)) { throw new NotInValueRangeException(fieldName, value); } DateTime pstDate = (DateTime)value; _pstDate = pstDate; } else { throw new NoFieldNameException(fieldName); } }
/// <summary> /// set value /// </summary> /// <param name="fieldName"></param> /// <param name="value"></param> /// <exception cref="NoFieldNameException">No such field name</exception> /// <exception cref="NotInValueRangeException">The value is not supported</exception> public void SetValue(String fieldName, Object value) { if (_isSaved) { return; } if (value == null) { throw new NotInValueRangeException(fieldName, ""); } if (fieldName.Equals(VENDOR)) { if (!(value is MasterDataIdentity)) { throw new NotInValueRangeException(fieldName, value); } MasterDataIdentity vendor = (MasterDataIdentity)value; setVendor(vendor); } else if (fieldName.Equals(GL_ACCOUNT)) { if (!(value is MasterDataIdentity_GLAccount)) { throw new NotInValueRangeException(fieldName, value); } MasterDataIdentity_GLAccount glAccount = (MasterDataIdentity_GLAccount)value; setGLAccount(glAccount); } else if (fieldName.Equals(REC_ACC)) { if (!(value is MasterDataIdentity_GLAccount)) { throw new NotInValueRangeException(fieldName, value); } MasterDataIdentity_GLAccount recAcc = (MasterDataIdentity_GLAccount)value; setRecAccount(recAcc); } else if (fieldName.Equals(EntryTemplate.POSTING_DATE)) { if (!(value is DateTime)) { throw new NotInValueRangeException(fieldName, value); } DateTime date = (DateTime)value; _date = date; } else if (fieldName.Equals(EntryTemplate.AMOUNT)) { try { CurrencyAmount amount = CurrencyAmount.Parse(value.ToString()); if (amount.IsZero() || amount.IsNegative()) { throw new NotInValueRangeException(fieldName, value); } _amount = amount; } catch (CurrencyAmountFormatException) { throw new NotInValueRangeException(fieldName, value); } } else if (fieldName.Equals(EntryTemplate.TEXT)) { _text = value.ToString(); } else if (fieldName.Equals(BUSINESS_AREA)) { if (!(value is MasterDataIdentity)) { throw new NotInValueRangeException(fieldName, value); } MasterDataIdentity businessArea = (MasterDataIdentity)value; setBusinessArea(businessArea); } else { throw new NoFieldNameException(fieldName); } }
/// <summary> /// parse XML to template /// </summary> /// <param name="coreDriver"></param> /// <param name="elem"></param> /// <returns></returns> /// <exception cref="TemplateFormatException"></exception> public static EntryTemplate Parse(CoreDriver coreDriver, MasterDataManagement mdMgmt , XElement elem) { XAttribute idStr = elem.Attribute(XML_ID); XAttribute name = elem.Attribute(XML_NAME); XAttribute typeStr = elem.Attribute(XML_TYPE); if (name == null) { coreDriver.logDebugInfo(typeof(EntryTemplate), 228, "No value in template name", MessageType.ERRO); throw new TemplateFormatException(); } int id; if (int.TryParse(idStr.Value, out id) == false) { throw new TemplateFormatException(); } EntryType type; if (typeStr.Value.Equals(XML_VENDOR)) { type = EntryType.VendorEntry; } else if (typeStr.Value.Equals(XML_CUSTOMER)) { type = EntryType.CustomerEntry; } else if (typeStr.Value.Equals(XML_GL)) { type = EntryType.GLEntry; } else { coreDriver.logDebugInfo(typeof(EntryTemplate), 252, "template type is not correct: " + typeStr.Value, MessageType.ERRO); throw new TemplateFormatException(); } EntryTemplate template = new EntryTemplate(coreDriver, mdMgmt , type, id, name.Value); foreach (XElement fieldElem in elem.Elements(XML_FIELD)) { XAttribute fieldName = fieldElem.Attribute(XML_NAME); XAttribute fieldValue = fieldElem.Attribute(XML_VALUE); if (fieldName.Value.Equals(EntryTemplate.AMOUNT)) { try { CurrencyAmount amount = CurrencyAmount .Parse(fieldValue.Value); template.AddDefaultValue(fieldName.Value, amount); } catch (CurrencyAmountFormatException e) { coreDriver.logDebugInfo(typeof(EntryTemplate), 287, e.Message, MessageType.ERRO); throw new TemplateFormatException(); } catch (NotInValueRangeException e) { coreDriver.logDebugInfo(typeof(EntryTemplate), 287, e.Message, MessageType.ERRO); throw new TemplateFormatException(); } catch (NoFieldNameException e) { coreDriver.logDebugInfo(typeof(EntryTemplate), 287, e.Message, MessageType.ERRO); throw new TemplateFormatException(); } } else if (fieldName.Value.Equals(EntryTemplate.TEXT)) { try { template.AddDefaultValue(fieldName.Value, fieldValue.Value); } catch (NotInValueRangeException e) { coreDriver.logDebugInfo(typeof(EntryTemplate), 287, e.Message, MessageType.ERRO); throw new TemplateFormatException(); } catch (NoFieldNameException e) { coreDriver.logDebugInfo(typeof(EntryTemplate), 287, e.Message, MessageType.ERRO); throw new TemplateFormatException(); } } else { try { switch (type) { case EntryType.VendorEntry: if (fieldName.Value.Equals(VendorEntry.REC_ACC) || fieldName .Value.Equals(VendorEntry.GL_ACCOUNT)) { MasterDataIdentity_GLAccount accountId = new MasterDataIdentity_GLAccount( fieldValue.Value); template.AddDefaultValue(fieldName.Value, accountId); } else { MasterDataIdentity dataId = new MasterDataIdentity( fieldValue.Value); template.AddDefaultValue(fieldName.Value, dataId); } break; case EntryType.CustomerEntry: if (fieldName.Value.Equals(CustomerEntry.REC_ACC) || fieldName.Value .Equals(CustomerEntry.GL_ACCOUNT)) { MasterDataIdentity_GLAccount accountId = new MasterDataIdentity_GLAccount( fieldValue.Value); template.AddDefaultValue(fieldName.Value, accountId); } else { MasterDataIdentity dataId = new MasterDataIdentity( fieldValue.Value); template.AddDefaultValue(fieldName.Value, dataId); } break; case EntryType.GLEntry: MasterDataIdentity_GLAccount accId = new MasterDataIdentity_GLAccount( fieldValue.Value); template.AddDefaultValue(fieldName.Value, accId); break; } } catch (Exception e) { coreDriver.logDebugInfo(typeof(EntryTemplate), 309, e.Message, MessageType.ERRO); throw new TemplateFormatException(); } } } return(template); }