/// <summary> /// Retrieve a payment for editing /// </summary> public void Payment(int id) { PaymentDocument document = getPayment(id); JObject record = document.ToJObject(); nextPreviousDocument(record, "WHERE DocumentTypeId = " + (int)PaymentDoc); record.Add("BankAccounts", SelectBankAccounts()); record.Add("Names", SelectNames(NameType)); Record = record; }
public AjaxReturn PaymentSave(PaymentDocument json) { decimal amount = 0; Database.BeginTransaction(); PaymentHeader document = json.header; checkDocType(document.DocumentTypeId, PaymentDoc); checkNameType(document.DocumentNameAddressId, NameType); checkAccountIsAcctType(document.DocumentAccountId, AcctType.Bank, AcctType.CreditCard); if (document.idDocument == null) { allocateDocumentIdentifier(document); } PaymentDocument oldDoc = getPayment(document.idDocument); int sign = -SignFor(PaymentDoc); // Update the outstanding on the paid documents foreach (PaymentLine payment in json.detail) { Utils.Check(!SecurityOn || !Settings.RequireAuthorisation || Authorise || NameType != "S" || payment.Authorised > 0, "Cannot pay unauthorised document"); decimal a = payment.AmountPaid; PaymentLine old = oldDoc.PaymentFor(payment.idDocument); if (old != null) { a -= old.AmountPaid; // reduce update by the amount paid last time it was saved } int?docId = payment.idDocument; if (a != 0) { Database.Execute("UPDATE Journal SET Outstanding = Outstanding - " + sign * a + " WHERE DocumentId = " + Database.Quote(docId) + " AND AccountId = " + (int)LedgerAccount); amount += a; } } json.detail = json.detail.Where(l => l.AmountPaid != 0).ToList(); document.DocumentOutstanding = json.header.Remaining; decimal changeInDocumentAmount; decimal changeInOutstanding; // Virtual method, as calculation is different for customers and suppliers calculatePaymentChanges(json, amount, out changeInDocumentAmount, out changeInOutstanding); document.DocumentTypeId = (int)PaymentDoc; Database.Update(document); // Now delete the old cross reference records, and replace with new Database.Execute("DELETE FROM Payments WHERE idPayment = " + document.idDocument); foreach (PaymentLine payment in json.detail) { if (payment.AmountPaid != 0) { Database.Execute("INSERT INTO Payments (idPayment, idPaid, PaymentAmount) VALUES(" + document.idDocument + ", " + payment.idDocument + ", " + payment.AmountPaid + ")"); } } // Journal between bank account and sales/purchase ledger Journal journal = Database.Get(new Journal() { DocumentId = (int)document.Id, JournalNum = 1 }); journal.DocumentId = (int)document.idDocument; journal.JournalNum = 1; journal.NameAddressId = document.DocumentNameAddressId; journal.Memo = document.DocumentMemo; journal.AccountId = document.DocumentAccountId; journal.Amount += changeInDocumentAmount; journal.Outstanding += changeInOutstanding; Database.Update(journal); journal = Database.Get(new Journal() { DocumentId = (int)document.Id, JournalNum = 2 }); journal.DocumentId = (int)document.idDocument; journal.JournalNum = 2; journal.NameAddressId = document.DocumentNameAddressId; journal.Memo = document.DocumentMemo; journal.AccountId = (int)LedgerAccount; journal.Amount -= changeInDocumentAmount; journal.Outstanding -= changeInOutstanding; Database.Update(journal); Line line = Database.Get(new Line() { idLine = journal.idJournal }); line.idLine = journal.idJournal; line.LineAmount += PaymentDoc == DocType.BillPayment ? -changeInDocumentAmount : changeInDocumentAmount; Database.Update(line); oldDoc = PaymentGetAudit(document.idDocument); Database.AuditUpdate("Payment", document.idDocument, oldDoc == null ? null : oldDoc.ToJObject(), json.ToJObject()); Database.Commit(); return(new AjaxReturn() { message = "Payment saved", id = document.idDocument }); }
public AjaxReturn PaymentPost(PaymentDocument json) { decimal amount = 0; Database.BeginTransaction(); PaymentHeader document = json.header; checkDocType(document.DocumentTypeId, PaymentDoc); checkNameType(document.DocumentNameAddressId, NameType); checkAccountIsAcctType(document.DocumentAccountId, AcctType.Bank, AcctType.CreditCard); if (document.idDocument == null) allocateDocumentIdentifier(document); PaymentDocument oldDoc = getPayment(document.idDocument); int sign = -SignFor(PaymentDoc); // Update the outstanding on the paid documents foreach (PaymentLine payment in json.detail) { decimal a = payment.AmountPaid; PaymentLine old = oldDoc.PaymentFor(payment.idDocument); if (old != null) a -= old.AmountPaid; // reduce update by the amount paid last time it was saved int? docId = payment.idDocument; if (a != 0) { Database.Execute("UPDATE Journal SET Outstanding = Outstanding - " + sign * a + " WHERE DocumentId = " + Database.Quote(docId) + " AND AccountId = " + (int)LedgerAccount); amount += a; } } json.detail = json.detail.Where(l => l.AmountPaid != 0).ToList(); decimal changeInDocumentAmount; decimal changeInOutstanding; // Virtual method, as calculation is different for customers and suppliers calculatePaymentChanges(json, amount, out changeInDocumentAmount, out changeInOutstanding); document.DocumentTypeId = (int)PaymentDoc; Database.Update(document); // Now delete the old cross reference records, and replace with new Database.Execute("DELETE FROM Payments WHERE idPayment = " + document.idDocument); foreach (PaymentLine payment in json.detail) { if (payment.AmountPaid != 0) { Database.Execute("INSERT INTO Payments (idPayment, idPaid, PaymentAmount) VALUES(" + document.idDocument + ", " + payment.idDocument + ", " + payment.AmountPaid + ")"); } } // Journal between bank account and sales/purchase ledger Journal journal = Database.Get(new Journal() { DocumentId = (int)document.Id, JournalNum = 1 }); journal.DocumentId = (int)document.idDocument; journal.JournalNum = 1; journal.NameAddressId = document.DocumentNameAddressId; journal.Memo = document.DocumentMemo; journal.AccountId = document.DocumentAccountId; journal.Amount += changeInDocumentAmount; journal.Outstanding += changeInOutstanding; Database.Update(journal); journal = Database.Get(new Journal() { DocumentId = (int)document.Id, JournalNum = 2 }); journal.DocumentId = (int)document.idDocument; journal.JournalNum = 2; journal.NameAddressId = document.DocumentNameAddressId; journal.Memo = document.DocumentMemo; journal.AccountId = (int)LedgerAccount; journal.Amount -= changeInDocumentAmount; journal.Outstanding -= changeInOutstanding; Database.Update(journal); Line line = Database.Get(new Line() { idLine = journal.idJournal }); line.idLine = journal.idJournal; line.LineAmount += PaymentDoc == DocType.BillPayment ? -changeInDocumentAmount : changeInDocumentAmount; Database.Update(line); oldDoc = PaymentGetAudit(document.idDocument); Database.AuditUpdate("Payment", document.idDocument, oldDoc == null ? null : oldDoc.ToJObject(), json.ToJObject()); Database.Commit(); return new AjaxReturn() { message = "Payment saved", id = document.idDocument }; }