/// <summary> /// Parse XElement to header /// </summary> /// <param name="coreDriver"></param> /// <param name="management"></param> /// <param name="elem"></param> /// <returns></returns> /// <exception cref="TransactionDataFileFormatException"></exception> public static HeadEntity Parse(CoreDriver coreDriver, MasterDataManagement management, XElement elem) { HeadEntity head = new HeadEntity(coreDriver, management); head._isSaved = true; #region get document number XAttribute docNumStr = elem.Attribute(TransDataUtils.XML_DOC_NUM); if (docNumStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 271, String.Format( "Field {0} is missing in.", TransDataUtils.XML_DOC_NUM), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } try { head._docNumber = new DocumentNumber(docNumStr.Value.ToCharArray()); } catch (Exception) { coreDriver.logDebugInfo(typeof(HeadEntity), 271, String.Format( "Format of field {0} is error.", TransDataUtils.XML_DOC_NUM), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } #endregion #region fiscal year XAttribute yearStr = elem.Attribute(TransDataUtils.XML_YEAR); if (yearStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 283, String.Format( "Field %s is missing in.", TransDataUtils.XML_YEAR), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } int year; if (!Int32.TryParse(yearStr.Value, out year)) { coreDriver.logDebugInfo(typeof(HeadEntity), 271, String.Format( "Format of field {0} is error.", TransDataUtils.XML_YEAR), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } #endregion #region fiscal month XAttribute monthStr = elem.Attribute(TransDataUtils.XML_MONTH); if (monthStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 295, String.Format( "Field %s is missing in.", TransDataUtils.XML_MONTH), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } int month; if (!Int32.TryParse(monthStr.Value, out month)) { coreDriver.logDebugInfo(typeof(HeadEntity), 271, String.Format( "Format of field {0} is error.", TransDataUtils.XML_MONTH), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } #endregion try { head._monthId = new MonthIdentity(year, month); } catch (FiscalMonthRangeException) { coreDriver.logDebugInfo(typeof(HeadEntity), 271, String.Format( "Format of field {0} is error.", TransDataUtils.XML_MONTH), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } catch (FiscalYearRangeException) { coreDriver.logDebugInfo(typeof(HeadEntity), 271, String.Format( "Format of field {0} is error.", TransDataUtils.XML_YEAR), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } // posting date XAttribute dateStr = elem.Attribute(TransDataUtils.XML_DATE); if (dateStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 307, String.Format( "Field %s is missing in.", TransDataUtils.XML_DATE), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } if (!DateTime.TryParse(dateStr.Value, out head._postingDate)) { coreDriver.logDebugInfo(typeof(HeadEntity), 271, String.Format( "Format of field {0} is error.", TransDataUtils.XML_DATE), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } // text XAttribute text = elem.Attribute(TransDataUtils.XML_TEXT); if (text == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 325, String.Format( "Field %s is missing in.", TransDataUtils.XML_TEXT), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } head._docText = text.Value; // document type XAttribute docTypeStr = elem.Attribute(TransDataUtils.XML_DOC_TYPE); if (docTypeStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 325, String.Format( "Field %s is missing in.", TransDataUtils.XML_DOC_TYPE), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } if (docTypeStr.Value.Length != 1 || (docTypeStr.Value[0] != (char)DocumentType.CUSTOMER_INVOICE && docTypeStr.Value[0] != (char)DocumentType.GL && docTypeStr.Value[0] != (char)DocumentType.VENDOR_INVOICE)) { coreDriver.logDebugInfo(typeof(HeadEntity), 271, String.Format( "Format of field {0} is error.", TransDataUtils.XML_DOC_TYPE), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } head._type = (DocumentType)docTypeStr.Value[0]; // is reversed XAttribute isReversedStr = elem .Attribute(TransDataUtils.XML_IS_REVERSED); if (isReversedStr == null) { coreDriver.logDebugInfo(typeof(HeadEntity), 338, String.Format( "Field %s is missing in.", TransDataUtils.XML_IS_REVERSED), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } if (!bool.TryParse(isReversedStr.Value, out head._isReversed)) { coreDriver.logDebugInfo(typeof(HeadEntity), 271, String.Format( "Format of field {0} is error.", TransDataUtils.XML_IS_REVERSED), MessageType.ERRO); throw new TransactionDataFileFormatException(""); } // parse item foreach (XElement itemElem in elem.Elements(TransDataUtils.XML_ITEM)) { ItemEntity item = ItemEntity.Parse(coreDriver, management, head, itemElem); item._isSaved = true; coreDriver .logDebugInfo( typeof(HeadEntity), 377, String.Format( "Line Item %d appended during parsing document.", item.LineNum), MessageType.INFO); head._items.Add(item); } // addition attributes foreach (XAttribute attr in elem.Attributes()) { head._fields.Add(attr.Name.LocalName, attr.Value); } // remove fields is not additional fields foreach (String str in TransDataUtils.HEAD_XML_TAGS) { head._fields.Remove(str); } // check balance CurrencyAmount sum = new CurrencyAmount(); foreach (ItemEntity item in head._items) { if (item.CdIndicator == CreditDebitIndicator.DEBIT) { sum.AddTo(item.Amount); } else { sum.MinusTo(item.Amount); } } if (sum.IsZero() == false) { throw new TransactionDataFileFormatException("No Balance"); } StringBuilder strBuilder = new StringBuilder( String.Format( "Parse document %s with posting date %s, text %s, type %s, is_reversed %s", head.DocIdentity, head.PstDate, head.DocText, head.DocType, head.IsReversed)); coreDriver.logDebugInfo(typeof(HeadEntity), 377, strBuilder.ToString(), MessageType.INFO); return(head); }