########## AP INVOICES ########## ActiveRecord::Base.connection.execute "SELECT DISTINCT voucher_number INTO #glt_vouchers FROM gl_transactions WHERE object_type = 'ApInvoice'" tmp = ActiveRecord::Base.connection.select_all "SELECT DISTINCT api.accounting_date, api.voucher_number, api.status, api. [source], count(gld.id) AS gld_count, sum(gld.amount) AS amount FROM ap_invoices api JOIN gl_distributions gld ON gld.object_id = api.id AND gld.object_type = 'ApInvoice' LEFT JOIN #glt_vouchers gv on gv.voucher_number = api.voucher_number WHERE gv.voucher_number IS NULL AND api.status in('Open', 'Paid', 'Voided') GROUP BY api.accounting_date, api.voucher_number, api.status, api.[source]" ap_vouchers = tmp.map {|a| a["voucher_number"]}.uniq paid_records = [] voided_records = [] ap_invoice_records = ApInvoice.where(voucher_number: ap_vouchers) ap_invoice_records.each do |api| paid_records << api.id if api.status == "Paid" voided_records << api.id if api.status == "Voided" api.update_attributes status: "Approved" VoucherPostState.where(voucher_number: api.voucher_number).delete_all ApInvoiceGlPoster.new.post(api.voucher_number, true) end;nil ApInvoice.where(id: paid_records).update_all status: "Paid" ApInvoice.where(id: voided_records).update_all status: "Voided" ActiveRecord::Base.connection.execute "DROP TABLE #glt_vouchers"