/// <summary> /// Retrieve document, or prepare new one /// </summary> public JObject document(int id, DocType type) { Title = Title.Replace("Document", type.UnCamel()); Extended_Document header = getDocument<Extended_Document>(id); if (header.idDocument == null) { header.DocumentTypeId = (int)type; header.DocType = type.UnCamel(); header.DocumentDate = Utils.Today; header.DocumentName = ""; header.DocumentIdentifier = Settings.NextNumber(type).ToString(); if (GetParameters["name"].IsInteger()) { JObject name = Database.QueryOne("*", "WHERE Type = " + Database.Quote(NameType) + " AND idNameAddress = " + GetParameters["name"], "NameAddress"); if (name != null) { checkNameType(name.AsString("Type"), NameType); header.DocumentNameAddressId = name.AsInt("idNameAddress"); header.DocumentAddress = name.AsString("Address"); header.DocumentName = name.AsString("Name"); } } } else { checkDocType(header.DocumentTypeId, type); checkNameType(header.DocumentNameAddressId, NameType); } JObject record = new JObject().AddRange("header", header); Database.NextPreviousDocument(record, "WHERE DocumentTypeId = " + (int)type); Select s = new Select(); record.AddRange("VatCodes", s.VatCode(""), "Names", s.Name(NameType, "")); return record; }
/// <summary> /// Get a Buy or Sell document for editing /// </summary> public void Document(int id, DocType type) { Title = Title.Replace("Document", type.UnCamel()); InvestmentDocument header = getDocument<InvestmentDocument>(id); if (header.idDocument == null) { header.DocumentTypeId = (int)type; header.DocType = type.UnCamel(); header.DocumentDate = Utils.Today; header.DocumentName = ""; if (GetParameters["acct"].IsInteger()) { FullAccount acct = Database.QueryOne<FullAccount>("*", "WHERE idAccount = " + GetParameters["acct"], "Account"); if (acct.idAccount != null) { header.DocumentAccountId = (int)acct.idAccount; header.DocumentAccountName = acct.AccountName; header.FeeAccount = Database.QueryOne("SELECT idAccount FROM Account WHERE AccountName = " + Database.Quote(acct.AccountName + " fees")).AsInt("idAccount"); } } } else { checkDocType(header.DocumentTypeId, DocType.Buy, DocType.Sell); List<JObject> journals = Database.Query(@"SELECT * FROM Journal LEFT JOIN StockTransaction ON idStockTransaction = idJournal LEFT JOIN Security ON idSecurity = SecurityId WHERE JournalNum > 1 AND DocumentId = " + id).ToList(); header.SecurityId = journals[0].AsInt("SecurityId"); header.SecurityName = journals[0].AsString("SecurityName"); header.Quantity = journals[0].AsDouble("Quantity"); header.Price = journals[0].AsDouble("Price"); if (journals.Count > 1) { header.FeeAccount = journals[1].AsInt("AccountId"); header.Fee = journals[1].AsDecimal("Amount"); header.FeeMemo = journals[1].AsString("Memo"); } if (type == DocType.Sell) header.Quantity = -header.Quantity; } JObject record = new JObject().AddRange("header", header); Database.NextPreviousDocument(record, "JOIN Journal ON DocumentId = idDocument WHERE DocumentTypeId " + Database.In(DocType.Buy, DocType.Sell) + (header.DocumentAccountId > 0 ? " AND AccountId = " + header.DocumentAccountId : "")); Select s = new Select(); record.AddRange("Accounts", s.ExpenseAccount(""), "Names", s.Other(""), "Securities", s.Security("")); Record = record; }
/// <summary> /// Flatten an audit header-detail set into multiple records containing the header and 1 detail /// </summary> IEnumerable<JObject> auditFlatten(IEnumerable<JObject> data) { foreach (JObject record in data) { JObject r = JObject.Parse(record.AsString("Record")); record.Remove("Record"); JObject header = (JObject)r["header"]; if (header == null) { record.AddRange(r); yield return record; } else { if(header["DocumentAmount"] == null) header["DocumentAmount"] = header["Amount"]; if (header["DocumentName"] == null) header["DocumentName"] = header["Name"]; if (header["DocumentOutstanding"] == null) header["DocumentOutstanding"] = header["Outstanding"]; foreach (JObject detail in (JArray)r["detail"]) { JObject result = new JObject(); result.AddRange(record, header, detail); yield return result; } } } }