def pay_invoices_fifo (self): c = DB.cursor() c_id = self.customer_id c.execute("(SELECT id, total - amount_due AS discount FROM " "(SELECT id, total, amount_due, SUM(amount_due) " "OVER (ORDER BY date_created, id) invoice_totals " "FROM invoices WHERE (paid, posted, canceled, customer_id) " "= (False, True, False, %s)" ") i " "WHERE invoice_totals <= " "(SELECT payment_total - invoice_total FROM " "(SELECT COALESCE(SUM(amount_due), 0.0) " "AS invoice_total FROM invoices " "WHERE (paid, canceled, customer_id) = " "(True, False, %s)" ") it, " "(SELECT amount + amount_owed AS payment_total FROM " "(SELECT COALESCE(SUM(amount), 0.0) " "AS amount " "FROM payments_incoming " "WHERE (customer_id, misc_income) = " "(%s, False)" ") pi, " "(SELECT COALESCE(SUM(amount_owed), 0.0) " "AS amount_owed " "FROM credit_memos " "WHERE (customer_id, posted) = " "(%s, True)" ") cm " ") pt " ")" "ORDER BY id);", (c_id, c_id, c_id, c_id )) for row in c.fetchall(): invoice_id = row[0] discount = row[1] if discount != Decimal('0.00'): self.payment.customer_discount (discount) if self.accrual == False: transactor.post_invoice_accounts (self.date, invoice_id) c.execute("UPDATE invoices " "SET (paid, payments_incoming_id, date_paid) " "= (True, %s, %s) " "WHERE id = %s", (self.payment_id, self.date, invoice_id)) c.close()
def pay_selected_invoices (self): c = DB.cursor() selection = self.builder.get_object('treeview-selection1') model, paths = selection.get_selected_rows() discount = Decimal('0.00') for row in paths: invoice_id = model[row][0] if self.accrual == False: transactor.post_invoice_accounts (self.date, invoice_id) c.execute("UPDATE invoices " "SET (paid, payments_incoming_id, date_paid) = " "(True, %s, %s) WHERE id = %s " "RETURNING total - amount_due AS discount", (self.payment_id, self.date, invoice_id)) discount += c.fetchone()[0] if discount != Decimal('0.00'): self.payment.customer_discount (discount) c.close()
def post(self): document = "/tmp/" + self.document_pdf f = open(document, 'rb') dat = f.read() binary = psycopg2.Binary(dat) f.close() self.cursor.execute( "UPDATE invoices SET(name, " "pdf_data, posted, amount_due, dated_for) " "= (%s, %s, %s, total, %s) " "WHERE id = %s RETURNING gl_entries_id, total", (self.document_name, binary, True, self.date, self.invoice_id)) for row in self.cursor.fetchall(): gl_entries_id = row[0] total = row[1] transactor.post_invoice_receivables(self.db, total, self.date, self.invoice_id, gl_entries_id) self.cursor.execute("SELECT accrual_based FROM settings") if self.cursor.fetchone()[0] == True: transactor.post_invoice_accounts(self.db, self.date, self.invoice_id)