SELECT array_to_string (array_agg (fin_paymen0_.fin_payment_scheduledetail_id), '') AS col_0_0_, array_to_string (array_agg (order3_.documentno), '') AS col_1_0_, CASE WHEN invoice7_.issotrx = '' AND (organizati11_.ad_org_id IS NOT NULL) AND (organizati11_.em_aprm_payment_desc LIKE '') THEN invoice7_.poreference ELSE invoice7_.documentno END AS col_2_0_, invoice7_.poreference AS col_3_0_, COALESCE (fin_paymen6_.fin_paymentmethod_id, fin_paymen2_.fin_paymentmethod_id) AS col_4_0_, COALESCE (fin_paymen9_.name, fin_paymen5_.name) AS col_5_0_, COALESCE (fin_paymen6_.expecteddate, fin_paymen2_.expecteddate) AS col_6_0_, max (COALESCE (fin_paymen6_.amount, fin_paymen2_.amount)) AS col_7_0_, max (COALESCE (invoice7_.grandtotal, 0)) AS col_8_0_, businesspa1_.c_bpartner_id AS col_9_0_, businesspa1_.name AS col_10_0_, sum (fin_paymen0_.amount + fin_paymen0_.writeoffamt) AS col_11_0_, COALESCE ( CASE WHEN invoice7_.grandtotal < 0 THEN invoice7_.grandtotal + ( SELECT COALESCE (sum (fe_tab_egr14_.importe_aplicar), 0) FROM fe_tab_egreso fe_tab_egr14_, ad_org organizati15_, c_invoice invoice17_, c_invoice invoice18_ CROSS JOIN ad_org organizati16_ WHERE fe_tab_egr14_.ad_org_id = organizati15_.ad_org_id AND fe_tab_egr14_.c_invoice_id = invoice17_.c_invoice_id AND fe_tab_egr14_.c_invoice_ref_id = invoice18_.c_invoice_id AND invoice7_.ad_org_id = organizati16_.ad_org_id AND (fe_tab_egr14_.c_invoice_id = invoice7_.c_invoice_id OR fe_tab_egr14_.c_invoice_ref_id = invoice7_.c_invoice_id AND fe_tab_egr14_.ad_org_id = invoice7_.ad_org_id AND invoice17_.docstatus = '' AND invoice18_.docstatus = '')) ELSE abs (invoice7_.grandtotal) - ( SELECT COALESCE (sum (fe_tab_egr19_.importe_aplicar), 0) FROM fe_tab_egreso fe_tab_egr19_, ad_org organizati20_, c_invoice invoice22_, c_invoice invoice23_ CROSS JOIN ad_org organizati21_ WHERE fe_tab_egr19_.ad_org_id = organizati20_.ad_org_id AND fe_tab_egr19_.c_invoice_ref_id = invoice22_.c_invoice_id AND fe_tab_egr19_.c_invoice_id = invoice23_.c_invoice_id AND invoice7_.ad_org_id = organizati21_.ad_org_id AND (fe_tab_egr19_.c_invoice_id = invoice7_.c_invoice_id OR fe_tab_egr19_.c_invoice_ref_id = invoice7_.c_invoice_id AND fe_tab_egr19_.ad_org_id = invoice7_.ad_org_id AND invoice22_.docstatus = '' AND invoice23_.docstatus = '')) END, 0) AS col_12_0_, COALESCE (sum (fin_paymen12_.amount), 0) AS col_13_0_, CASE WHEN sum (fin_paymen0_.writeoffamt) <> 0 THEN TRUE ELSE FALSE END AS col_14_0_, CASE WHEN max (fin_paymen13_.fin_payment_id) IS NOT NULL THEN TRUE ELSE FALSE END AS col_15_0_ FROM fin_payment_scheduledetail fin_paymen0_ INNER JOIN c_bpartner businesspa1_ ON fin_paymen0_.c_bpartner_id = businesspa1_.c_bpartner_id LEFT OUTER JOIN fin_payment_schedule fin_paymen2_ ON fin_paymen0_.fin_payment_schedule_order = fin_paymen2_.fin_payment_schedule_id LEFT OUTER JOIN c_order order3_ ON fin_paymen2_.c_order_id = order3_.c_order_id AND (order3_.issotrx = '' AND order3_.c_currency_id = '' AND order3_.c_bpartner_id = '') LEFT OUTER JOIN fin_payment_priority fin_paymen4_ ON fin_paymen2_.fin_payment_priority_id = fin_paymen4_.fin_payment_priority_id LEFT OUTER JOIN fin_paymentmethod fin_paymen5_ ON fin_paymen2_.fin_paymentmethod_id = fin_paymen5_.fin_paymentmethod_id LEFT OUTER JOIN fin_payment_schedule fin_paymen6_ ON fin_paymen0_.fin_payment_schedule_invoice = fin_paymen6_.fin_payment_schedule_id LEFT OUTER JOIN c_invoice invoice7_ ON fin_paymen6_.c_invoice_id = invoice7_.c_invoice_id AND (invoice7_.issotrx = '' AND invoice7_.c_currency_id = '' AND invoice7_.c_bpartner_id = '') LEFT OUTER JOIN fin_payment_priority fin_paymen8_ ON fin_paymen6_.fin_payment_priority_id = fin_paymen8_.fin_payment_priority_id LEFT OUTER JOIN fin_paymentmethod fin_paymen9_ ON fin_paymen6_.fin_paymentmethod_id = fin_paymen9_.fin_paymentmethod_id LEFT OUTER JOIN ad_org organizati10_ ON fin_paymen0_.ad_org_id = organizati10_.ad_org_id LEFT OUTER JOIN ad_orginfo organizati11_ ON organizati10_.ad_org_id = organizati11_.ad_org_id LEFT OUTER JOIN fin_payment_detail fin_paymen12_ ON fin_paymen0_.fin_payment_detail_id = fin_paymen12_.fin_payment_detail_id LEFT OUTER JOIN fin_payment fin_paymen13_ ON fin_paymen12_.fin_payment_id = fin_paymen13_.fin_payment_id WHERE (fin_paymen0_.ad_client_id IN (...)) AND (fin_paymen0_.ad_org_id IN (...)) AND (fin_paymen0_.fin_payment_detail_id IS NULL OR fin_paymen13_.fin_payment_id = '') AND (COALESCE (fin_paymen6_.fin_payment_schedule_id, fin_paymen2_.fin_payment_schedule_id) IS NOT NULL) AND (fin_paymen0_.ad_org_id IN (...)) AND (organizati11_.ad_org_id IS NULL OR organizati11_.isactive = '') AND invoice7_.issotrx = '' AND invoice7_.c_currency_id = '' AND businesspa1_.c_bpartner_id = '' GROUP BY invoice7_.c_invoice_id, invoice7_.documentno, invoice7_.c_doctype_id, COALESCE (fin_paymen6_.fin_paymentmethod_id, fin_paymen2_.fin_paymentmethod_id), COALESCE (fin_paymen9_.name, fin_paymen5_.name), COALESCE (fin_paymen6_.expecteddate, fin_paymen2_.expecteddate), COALESCE (fin_paymen8_.priority, fin_paymen4_.priority), invoice7_.issotrx, organizati11_.ad_org_id, organizati11_.em_aprm_payment_desc, invoice7_.poreference, businesspa1_.c_bpartner_id, businesspa1_.name HAVING upper (COALESCE (fin_paymen9_.name, fin_paymen5_.name)) LIKE upper ('') ESCAPE '' ORDER BY CASE WHEN max (fin_paymen13_.fin_payment_id) IS NOT NULL THEN 0 ELSE 0 END, COALESCE (fin_paymen8_.priority, fin_paymen4_.priority), COALESCE (fin_paymen6_.expecteddate, fin_paymen2_.expecteddate), invoice7_.documentno LIMIT '';