/// <summary>
        /// Only internal can invoke
        /// </summary>
        /// <param name="coreDriver"></param>
        /// <param name="management"></param>
        /// <param name="head"></param>
        /// <param name="lineNum"></param>
        internal ItemEntity(CoreDriver coreDriver, MasterDataManagement management,
                            HeadEntity head, int lineNum)
        {
            _coreDriver = coreDriver;
            _management = management;

            _head    = head;
            _lineNum = lineNum;

            _glAccount    = null;
            _customer     = null;
            _vendor       = null;
            _amount       = new CurrencyAmount();
            _businessArea = null;
        }
Exemple #2
0
        /// <summary>
        /// reverse document
        /// </summary>
        /// <param name="doc"></param>
        private void reverseDoc(HeadEntity doc)
        {
            foreach (ItemEntity item in doc.DocItems)
            {
                CurrencyAmount amount = item.Amount;
                if (item.CdIndicator == CreditDebitIndicator.DEBIT)
                {
                    amount.Negate();
                }
                GLAccountBalanceItem balItem;
                if (!_items.TryGetValue(item.GLAccount, out balItem))
                {
                    continue;
                }

                balItem.AddAmount(doc.MonthID, amount);
            }
        }
Exemple #3
0
        /// <summary>
        /// set report when new doc
        /// </summary>
        /// <param name="head"></param>
        private void newDoc(HeadEntity head)
        {
            foreach (ItemEntity item in head.DocItems)
            {
                CurrencyAmount amount = item.Amount;
                if (item.CdIndicator == CreditDebitIndicator.CREDIT)
                {
                    amount.Negate();
                }
                GLAccountBalanceItem balItem;
                if (!_items.TryGetValue(item.GLAccount, out balItem))
                {
                    continue;
                }

                balItem.AddAmount(head.MonthID, amount);
            }
        }
Exemple #4
0
        /// <summary>
        /// Reverse Document
        /// </summary>
        /// <param name="docId"></param>
        /// <exception cref="SystemException"></exception>
        /// <exception cref="ReverseOrgDocNotExistException"></exception>
        /// <exception cref="DocReservedException"></exception>
        public void ReverseDocument(DocumentIdentity docId)
        {
            _coreDriver.logDebugInfo(this.GetType(), 348,
                                     "Start reversing document " + docId.ToString(),
                                     MessageType.INFO);

            // ------------------------------------------
            // check document
            HeadEntity orgHead = this.GetEntity(docId);

            if (orgHead == null)
            {
                _coreDriver.logDebugInfo(this.GetType(), 348, "No such document",
                                         MessageType.ERRO);
                throw new ReverseOrgDocNotExistException();
            }
            if (orgHead.IsReversed)
            {
                _coreDriver.logDebugInfo(this.GetType(), 348,
                                         "Document has been reserved before", MessageType.ERRO);
                throw new DocReservedException();
            }

            _coreDriver.logDebugInfo(this.GetType(), 399,
                                     "Update reverse document information", MessageType.INFO);
            // update reverse information
            orgHead._isReversed = true;

            // raise event to update balance
            _coreDriver.ListenerMgmt.ReverseDoc(orgHead);

            _coreDriver.logDebugInfo(this.GetType(), 399,
                                     "Store memory to disk during reverse document",
                                     MessageType.INFO);

            // store to file system
            this.Store(orgHead.MonthID);


            String info = String.Format("Document {0} is reversed.",
                                        orgHead.DocNumber);

            _coreDriver.logDebugInfo(this.GetType(), 416, info, MessageType.INFO);
        }
        /// <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>
        /// Add new entity
        /// </summary>
        /// <param name="head"></param>
        internal void Add(HeadEntity head)
        {
            DocumentIdentity id = head.DocIdentity;

            _list.Add(id, head);
        }
Exemple #7
0
        /// <summary>
        /// load month identity
        /// </summary>
        /// <param name="monthId"></param>
        /// <returns></returns>
        /// <exception cref="TransactionDataFileFormatException"></exception>
        public async Task <MonthLedger> Load(MonthIdentity monthId)
        {
            // construct month ledger
            MonthLedger monthledger = new MonthLedger(monthId);

            _list.Add(monthId, monthledger);

            _coreDriver.logDebugInfo(
                this.GetType(),
                102,
                String.Format("Loading transaction data {0} ...",
                              monthId.ToString()), MessageType.INFO);

            // get file path
            StorageFile file = await generateFilePath(monthId, false);

            if (file == null)
            {// empty
                return(monthledger);
            }
            _coreDriver.logDebugInfo(this.GetType(), 109,
                                     String.Format("Transaction data file: {0} .", file.Path),
                                     MessageType.INFO);
            try
            {
                string text = await FileIO.ReadTextAsync(file);

                XDocument xdoc = XDocument.Parse(text);

                XElement rootElem = xdoc.Element(TransDataUtils.XML_ROOT);
                // no root element
                if (rootElem == null)
                {
                    throw new TransactionDataFileFormatException(file.Path);
                }

                // -------------------------------------------------------------------
                // parse all the documents
                foreach (XElement elem in rootElem.Elements(TransDataUtils.XML_DOCUMENT))
                {
                    HeadEntity head = HeadEntity.Parse(_coreDriver,
                                                       _masterDataMgmt, elem);
                    _coreDriver
                    .logDebugInfo(
                        this.GetType(),
                        172,
                        String.Format(
                            "Document {0} add to list during loading.",
                            head.DocIdentity
                            .ToString()),
                        MessageType.INFO);
                    monthledger.Add(head);

                    // raise load document
                    _coreDriver.ListenerMgmt.LoadDoc(this, head);

                    // raise reverse document
                    if (head.IsReversed)
                    {
                        _coreDriver.ListenerMgmt.ReverseDoc(head);
                    }
                }
                // -----------------------------------------------------------------

                return(monthledger);
            }
            catch (TransactionDataFileFormatException e)
            {
                _coreDriver.logDebugInfo(this.GetType(), 170, e.Message,
                                         MessageType.ERRO);
                throw new TransactionDataFileFormatException(file.Path);
            }
        }
Exemple #8
0
        /// <summary>
        /// save document
        /// </summary>
        /// <param name="head"></param>
        /// <param name="needStroe"></param>
        /// <exception cref="SystemException"></exception>
        /// <exception cref="SaveClosedLedgerException"></exception>
        internal async Task saveDocumentAsync(HeadEntity head, bool needStroe)
        {
            _coreDriver.logDebugInfo(this.GetType(), 231,
                                     "Call transaction to save the document", MessageType.INFO);

            if (head.IsSaved)
            {
                _coreDriver.logDebugInfo(this.GetType(), 235,
                                         "Document is saved, just to store the update on disk.",
                                         MessageType.INFO);
            }
            else
            {
                _coreDriver.logDebugInfo(this.GetType(), 239,
                                         "Document is never saved", MessageType.INFO);

                MonthIdentity monthId = head.MonthID;
                MonthLedger   ledger  = this.GetLedger(monthId);
                if (ledger == null)
                {
                    _coreDriver.logDebugInfo(this.GetType(), 239,
                                             "Error in document month identity", MessageType.ERRO);
                    throw new SaveClosedLedgerException();
                }

                // set document number
                DocumentNumber    num;
                List <HeadEntity> entities = ledger.Entities;
                if (entities.Count == 0)
                {
                    try
                    {
                        num = new DocumentNumber("1000000001".ToCharArray());
                    }
                    catch (IdentityTooLong e)
                    {
                        _coreDriver.logDebugInfo(this.GetType(), 239,
                                                 e.ToString(), MessageType.ERRO);
                        throw new SystemException(e);
                    }
                    catch (IdentityNoData e)
                    {
                        _coreDriver.logDebugInfo(this.GetType(), 239,
                                                 e.ToString(), MessageType.ERRO);
                        throw new SystemException(e);
                    }
                    catch (IdentityInvalidChar e)
                    {
                        _coreDriver.logDebugInfo(this.GetType(), 239,
                                                 e.ToString(), MessageType.ERRO);
                        throw new SystemException(e);
                    }
                }
                else
                {
                    HeadEntity last = entities[entities.Count - 1];
                    num = last.DocNumber.Next();
                }
                _coreDriver.logDebugInfo(this.GetType(), 239,
                                         "Generate document number " + num.ToString(),
                                         MessageType.INFO);
                head._docNumber = num;

                ledger.Add(head);
            }

            if (needStroe)
            {
                await StoreAsync(head.MonthID);

                _coreDriver.logDebugInfo(this.GetType(), 465,
                                         "Memory has been stored to disk", MessageType.INFO);
            }
            else
            {
                _coreDriver.logDebugInfo(this.GetType(), 465,
                                         "Memory has NOT been stored to disk", MessageType.INFO);
            }

            _coreDriver.logDebugInfo(this.GetType(), 278,
                                     "Call transaction to save the document successfully",
                                     MessageType.INFO);
        }
Exemple #9
0
 private void documentSaved(HeadEntity document)
 {
     newDoc(document);
 }
Exemple #10
0
 private void documentReversed(HeadEntity doc)
 {
     reverseDoc(doc);
 }
Exemple #11
0
 private void documentLoad(Object source, HeadEntity document)
 {
     newDoc(document);
 }