'Validate totals, closed and docstatus for orders'

def validate_orders(self):

    for (reference_name, total) in self.reference_totals.iteritems():
        reference_type = self.reference_types[reference_name]
        account = self.reference_accounts[reference_name]
        if (reference_type in (u'Sales    Order', u'Purchase    Order')):
            order = frappe.get_doc(reference_type, reference_name)
            if (order.docstatus != 1):
                frappe.throw(_(u'{0}    {1}    is    not    submitted').format(reference_type, reference_name))
            if (flt(order.per_billed) >= 100):
                frappe.throw(_(u'{0}    {1}    is    fully    billed').format(reference_type, reference_name))
            if (cstr(order.status) == u'Closed'):
                frappe.throw(_(u'{0}    {1}    is    closed').format(reference_type, reference_name))
            account_currency = get_account_currency(account)
            if (account_currency == self.company_currency):
                voucher_total = order.base_grand_total
                formatted_voucher_total = fmt_money(voucher_total, order.precision(u'base_grand_total'), currency=account_currency)
            else:
                voucher_total = order.grand_total
                formatted_voucher_total = fmt_money(voucher_total, order.precision(u'grand_total'), currency=account_currency)
            if (flt(voucher_total) < (flt(order.advance_paid) + total)):
                frappe.throw(_(u'Advance    paid    against    {0}    {1}    cannot    be    greater     DCTB  DCTB  DCTB  DCTB  DCTB  DCTB than    Grand    Total    {2}').format(reference_type, reference_name, formatted_voucher_total))
