# Copyright 2017-2021 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields, api, _
from odoo.exceptions import UserError
from odoo.addons.base.models.res_partner import WARNING_MESSAGE


class SaleConfirm(models.TransientModel):
    _name = 'sale.confirm'
    _description = 'Wizard to confirm a sale order'

    sale_id = fields.Many2one(
        'sale.order', string='Sale Order', readonly=True)
    client_order_ref = fields.Char(string='Customer PO Number')
    payment_term_id = fields.Many2one(
        'account.payment.term', string='Payment Terms')
    partner_invoice_id = fields.Many2one(
        'res.partner', 'Invoice Address', required=True)
    partner_shipping_id = fields.Many2one(
        'res.partner', 'Delivery Address', required=True)
    sale_warn = fields.Selection(
        WARNING_MESSAGE, 'Sale Warning Type', readonly=True)
    sale_warn_msg = fields.Text(string='Sale Warning Message', readonly=True)
    commitment_date = fields.Datetime(string='Delivery Date')

    @api.model
    def _prepare_default_get(self, order):
        partner = order.partner_id.commercial_partner_id
        default = {
            'sale_id': order.id,
            'client_order_ref': order.client_order_ref,
            'payment_term_id': order.payment_term_id.id or False,
            'partner_invoice_id': order.partner_invoice_id.id,
            'partner_shipping_id': order.partner_shipping_id.id,
            'sale_warn_msg': partner.sale_warn_msg,
            'sale_warn': partner.sale_warn,
            'commitment_date': order.commitment_date,
        }
        return default

    @api.model
    def default_get(self, fields):
        res = super().default_get(fields)
        assert self._context.get('active_model') == 'sale.order',\
            'active_model should be sale.order'
        order = self.env['sale.order'].browse(self._context.get('active_id'))
        default = self._prepare_default_get(order)
        res.update(default)
        return res

    def _prepare_update_so(self):
        self.ensure_one()
        return {
            'client_order_ref': self.client_order_ref,
            'payment_term_id': self.payment_term_id.id or False,
            'partner_invoice_id': self.partner_invoice_id.id,
            'partner_shipping_id': self.partner_shipping_id.id,
            'commitment_date': self.commitment_date,
            }

    def confirm(self):
        self.ensure_one()
        partner = self.sale_id.partner_id.commercial_partner_id
        if partner.sale_warn == 'block':
            raise UserError(_(
                "You cannot confirm this quotation because "
                "customer '%s' has a blocker sale warning:\n\n%s")
                % (partner.display_name, partner.sale_warn_msg))
        vals = self._prepare_update_so()
        self.sale_id.write(vals)
        # confirm sale order
        self.sale_id.action_confirm()
