/// <summary> /// get file /// </summary> /// <param name="monthId"></param> /// <returns></returns> /// <exception cref="SystemException"></exception> private async Task <StorageFile> generateFilePath(MonthIdentity monthId, bool createIfNotExist) { StorageFolder transactionFolder = await _coreDriver.RootFolder .GetFolderAsync(TRANSACTION_DATA_FOLDER); if (transactionFolder == null) { throw new SystemException(null); // bug } String fileName = String.Format("{0}.xml", monthId.ToString()); if (createIfNotExist == false) { try { StorageFile file = await transactionFolder.GetFileAsync(fileName); return(file); } catch (FileNotFoundException) { return(null); } } else { StorageFile file = await transactionFolder.CreateFileAsync(fileName , CreationCollisionOption.OpenIfExists); return(file); } }
/// <summary> /// Parse string to Document Identity /// </summary> /// <param name="docIdStr"></param> /// <returns></returns> /// <exception cref="DocumentIdentityFormatException">Document Identity Exception</exception> public static DocumentIdentity Parse(String docIdStr) { try { String docNumStr = docIdStr.Substring(0, 10); String monthIdStr = docIdStr.Substring(11, 7); DocumentNumber id = new DocumentNumber(docNumStr.ToCharArray()); MonthIdentity monthId = MonthIdentity.Parse(monthIdStr); return(new DocumentIdentity(id, monthId)); } catch (ArgumentOutOfRangeException) { throw new DocumentIdentityFormatException(docIdStr); } catch (IdentityTooLong) { throw new DocumentIdentityFormatException(docIdStr); } catch (IdentityNoData) { throw new DocumentIdentityFormatException(docIdStr); } catch (IdentityInvalidChar) { throw new DocumentIdentityFormatException(docIdStr); } catch (MonthIdentityFormatException) { throw new DocumentIdentityFormatException(docIdStr); } }
/// <summary> /// Get group balance from start month to end month /// </summary> /// <param name="accountGroup"></param> /// <param name="startMonthId"></param> /// <param name="endMonthId"></param> /// <returns></returns> /// <exception cref="SystemException"></exception> public CurrencyAmount GetGroupBalance(GLAccountGroupENUM accountGroup, MonthIdentity startMonthId, MonthIdentity endMonthId) { CurrencyAmount ret = new CurrencyAmount(); MasterDataManagement mdMgmt = this._mdMgmt; foreach (var item in _items) { GLAccountMasterData glAccount; try { glAccount = (GLAccountMasterData)mdMgmt .GetMasterData(item.Key, MasterDataType.GL_ACCOUNT); } catch (Exception e) { _coreDriver.logDebugInfo(this.GetType(), 183, e.Message, MessageType.ERRO); throw new SystemException(e); } if (glAccount.Group.Identity == accountGroup) { ret.AddTo(item.Value.GetSumAmount(startMonthId, endMonthId)); } } return(ret); }
/// <summary> /// Get amount /// </summary> /// <param name="id"></param> /// <returns></returns> public CurrencyAmount GetAmount(MonthIdentity id) { if (_list.ContainsKey(id)) { CurrencyAmount amount; _list.TryGetValue(id, out amount); return(new CurrencyAmount(amount)); } return(new CurrencyAmount()); }
/// <summary> /// store month ledger to file system /// </summary> /// <param name="monthId"></param> /// <exception cref="SystemException"></exception> public async Task StoreAsync(MonthIdentity monthId) { // store master data _coreDriver.logDebugInfo(this.GetType(), 293, String.Format("Start storing %s to disk", monthId), MessageType.INFO); _coreDriver.logDebugInfo(this.GetType(), 297, "Store master data at first.", MessageType.INFO); await this._masterDataMgmt.StoreAsync(); MonthLedger collection = this.GetLedger(monthId); StorageFile file = await this.generateFilePath(monthId, true); _coreDriver.logDebugInfo(this.GetType(), 297, "Generate file path: " + file.Path, MessageType.INFO); if (file == null) { StorageFolder transactionFolder = await _coreDriver.RootFolder .GetFolderAsync(TRANSACTION_DATA_FOLDER); if (transactionFolder == null) { throw new SystemException(null); // bug } String fileName = String.Format("{0}.xml", monthId.ToString()); file = await transactionFolder.CreateFileAsync(fileName , CreationCollisionOption.OpenIfExists); } XDocument xdoc = collection.toXML(); _coreDriver .logDebugInfo(this.GetType(), 297, "Parsed document collections to XML document", MessageType.INFO); try { await FileIO.WriteTextAsync(file, xdoc.ToString()); } catch (FileNotFoundException e) { _coreDriver.logDebugInfo(this.GetType(), 316, e.Message, MessageType.ERRO); throw new SystemException(e); } _coreDriver.logDebugInfo(this.GetType(), 335, "Save document collection successfully.", MessageType.INFO); }
/// <summary> /// Get sum of amount from start month to end month /// </summary> /// <param name="startId">id of start month</param> /// <param name="endId">id of end month</param> /// <returns></returns> public CurrencyAmount GetSumAmount(MonthIdentity startId, MonthIdentity endId) { CurrencyAmount sum = new CurrencyAmount(); foreach (var item in _list) { if (item.Key.CompareTo(startId) >= 0 && item.Key.CompareTo(endId) <= 0) { sum.AddTo(item.Value); } } return(sum); }
/// <summary> /// Add amount /// </summary> /// <param name="monthId"></param> /// <param name="amount"></param> internal void AddAmount(MonthIdentity monthId, CurrencyAmount amount) { _sum.AddTo(amount); if (_list.ContainsKey(monthId)) { CurrencyAmount sum; _list.TryGetValue(monthId, out sum); sum.AddTo(amount); } else { _list.Add(monthId, new CurrencyAmount(amount)); } }
/// <summary> /// get ledger /// </summary> /// <param name="fiscalYear"></param> /// <param name="fiscalMonth"></param> /// <returns></returns> public MonthLedger GetLedger(int fiscalYear, int fiscalMonth) { MonthIdentity monthId; try { monthId = new MonthIdentity(fiscalYear, fiscalMonth); } catch (FiscalYearRangeException) { return(null); } catch (FiscalMonthRangeException) { return(null); } return(GetLedger(monthId)); }
/// <summary> /// set posting date /// </summary> /// <param name="postingDate"></param> /// <returns></returns> /// <exception cref="SystemException"></exception> public bool setPostingDate(DateTime postingDate) { if (_isSaved) { return(false); } _postingDate = postingDate; try { _monthId = new MonthIdentity(postingDate.Year, postingDate.Month); } catch (FiscalYearRangeException e) { throw new SystemException(e); } catch (FiscalMonthRangeException e) { throw new SystemException(e); } return(true); }
/// <summary> /// Get Ledger /// </summary> /// <param name="monthId"></param> /// <returns></returns> public MonthLedger GetLedger(MonthIdentity monthId) { // get current calendar month MonthIdentity curMonthId = _coreDriver.CurCalendarMonthId; // check whether ledger beyond the range if (_coreDriver.StartMonthId.CompareTo(monthId) > 0 || curMonthId.CompareTo(monthId) < 0) { return(null); } // create new month ledger MonthLedger monthLedger; if (!_list.TryGetValue(monthId, out monthLedger)) { monthLedger = new MonthLedger(monthId); _list.Add(monthId, monthLedger); } return(monthLedger); }
/// <summary> /// Month ledger /// </summary> /// <param name="monthId"></param> public MonthLedger(MonthIdentity monthId) { _monthId = monthId; _list = new Dictionary <DocumentIdentity, HeadEntity>(); }
/// <summary> /// constructor /// </summary> /// <param name="docNum"></param> /// <param name="monthIdentity"></param> public DocumentIdentity(DocumentNumber docNum, MonthIdentity monthIdentity) { _docNumber = docNum; _monthIdentity = monthIdentity; }
/// <summary> /// Constructor /// </summary> /// <param name="docNum"></param> /// <param name="fiscalYear"></param> /// <param name="fiscalMonth"></param> /// <exception cref="FiscalYearRangeException"></exception> /// <exception cref="FiscalMonthRangeException"></exception> public DocumentIdentity(DocumentNumber docNum, int fiscalYear, int fiscalMonth) { _docNumber = docNum; _monthIdentity = new MonthIdentity(fiscalYear, fiscalMonth); }
/// <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); } }
/// <summary> /// store month identity /// </summary> /// <param name="monthId"></param> public async void Store(MonthIdentity monthId) { await StoreAsync(monthId); }
/// <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); }