# -*- coding: utf-8 -*-
##############################################################################
#
#    Odoo, Open Source Management Solution
#    Copyright (C) 2014-TODAY Prime Consulting SA, Cape Verde (<http://prime.cv>).
#
##############################################################################
from odoo import models, fields, api
from odoo.exceptions import AccessError
from odoo.tools.translate import _

class SaleOrder(models.Model):
    _inherit = "sale.order"

    state = fields.Selection(selection_add=[('to_approve', 'To Approve (Overdue)')])

    @api.multi
    def action_confirm(self):
        context = self.env.context or {}
        for sale in self:
            if sale.state != 'to_approve':
                invoices = self.env['account.invoice'].sudo().search([('partner_id','=',sale.partner_id.id), ('state', '=', 'open'), ('amount_total', '>', 0)])
                allow_confirm = context.get('allow_confirm', False)
                if invoices and not allow_confirm:
                    return {
                        'type': 'ir.actions.act_window',
                        'name': 'Confirm Sales',
                        'res_model': 'sale.overdue.confirm',
                        'view_type': 'form',
                        'view_mode': 'form',
                        'target': 'new',
                        'context': {'total_invoices': len(invoices), 'customer': sale.partner_id.name}
                    }
            return super(SaleOrder, sale).action_confirm()
