# -*- 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


class SaleOverdueConfirm(models.TransientModel):
	_name = "sale.overdue.confirm"

	name = fields.Html()
	
	@api.model
	def default_get(self, fields):
		res = super(SaleOverdueConfirm, self).default_get(fields)
		context = self.env.context or {}
		if context.get('active_model', '') == 'sale.order':
			total_invoices, partner_name = context['total_invoices'], context['customer']
			res.update({'name': 'Customer <b>%s</b> has %s overdue invoices. Click <b>Confirm</b> if you still want to confirm this Sale.' % (partner_name, total_invoices)})
		return res

	@api.multi
	def action_confirm(self):
		context = self.env.context or {}
		if context.get('active_model', '') == 'sale.order':
			sale = self.env['sale.order'].browse(context['active_ids'])
			if self.env.user.has_group('sale_overdue_confirm.group_sale_allow_overdue'):
				return sale.with_context(allow_confirm=True).action_confirm()
			else:
				return sale.write({'state': 'to_approve'})