Ejemplo n.º 1
0
 /// <summary>
 /// A balance adjustment posts enough to to reach the new balance.
 /// Important fields are ExistingBalance and NewBalance
 /// </summary>
 public void BalanceAdjustment(int id, int acct)
 {
     checkAccountIsAcctType(acct, AcctType.Investment);
     BalanceAdjustmentDocument doc = Database.Get<BalanceAdjustmentDocument>(id);
     doc.NewBalance = doc.ExistingBalance = Database.QueryOne(@"SELECT SUM(Amount) AS Amount FROM Journal WHERE AccountId = " + acct).AsDecimal("Amount");
     if (doc.idDocument == null) {
         doc.DocumentAccountId = acct;
         doc.DocumentDate = Utils.Today;
         doc.DocumentMemo = "Balance Adjustment";
         if(string.IsNullOrEmpty(doc.DocumentIdentifier))
             doc.DocumentIdentifier = "Balance Adjustment";
         JObject o = Database.QueryOne(@"SELECT J.AccountId, AccountName
     FROM Journal
     JOIN Document ON idDocument = Journal.DocumentId
     JOIN Journal AS J ON J.DocumentId = idDocument AND J.JournalNum = 2
     JOIN Account ON idAccount = J.AccountId
     WHERE Journal.JournalNum = 1
     AND DocumentTypeID IN (" + (int)DocType.Cheque + "," + (int)DocType.Deposit + @")
     AND Journal.AccountId = " + acct);
         doc.AccountId = o.AsInt("AccountId");
         doc.AccountName = o.AsString("AccountName");
         doc.NameAddressId = 1;
         doc.Name = "";
     } else {
         checkDocType(doc.DocumentTypeId, DocType.Cheque, DocType.Deposit);
         foreach (Journal j in Database.Query<Journal>("SELECT * FROM Journal WHERE DocumentId = " + id)) {
             switch (j.JournalNum) {
                 case 1:
                     doc.DocumentAccountId = j.AccountId;
                     doc.NameAddressId = j.NameAddressId;
                     doc.Amount = j.Amount;
                     break;
                 case 2:
                     doc.AccountId = j.AccountId;
                     break;
                 default:
                     throw new CheckException("Document is not a balance adjustment");
             }
         }
         Utils.Check(acct == doc.DocumentAccountId, "Document is for a different account");
         doc.Name = Database.QueryOne("SELECT Name FROM NameAddress WHERE idNameAddress = " + doc.NameAddressId).AsString("Name");
         doc.AccountName = Database.QueryOne("SELECT AccountName FROM Account WHERE idAccount = " + doc.AccountId).AsString("AccountName");
         doc.ExistingBalance -= doc.Amount;
     }
     Select s = new Select();
     Record = new JObject().AddRange(
         "header", doc,
         "Accounts", s.Account(""),
         "Names", s.Other(""));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Get a document for editing
 /// </summary>
 public void Document(int id, DocType type)
 {
     Title = Title.Replace("Document", type.UnCamel());
     JObject record = GetDocument(id, type);
     dynamic header = ((dynamic)record).header;
     Database.NextPreviousDocument(record, "JOIN Journal ON DocumentId = idDocument WHERE DocumentTypeId = " + (int)type
         + (header.DocumentAccountId > 0 ? " AND AccountId = " + header.DocumentAccountId : ""));
     Select s = new Select();
     record.AddRange("Accounts", s.Account(""),
         "VatCodes", s.VatCode(""),
         "Names", s.Other(""));
     Record = record;
 }