# -*- coding: utf-8 -*-
##############################################################################
#
#    datenpol gmbh
#    Copyright (C) 2013-TODAY datenpol gmbh ()
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see .
#
##############################################################################
import re
from datetime import datetime
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError, UserError
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.tools import float_compare
from odoo.tools import float_is_zero
class SaleOrder(models.Model):
    _name = 'sale.order'
    _inherit = ['sale.order', 'dp_custom.helper']
    ASSEMBLY_STATES = [('import','Import'),
                       ('import_failed','Fehler Import'),
                       ('created', 'nicht freigegeben'),
                       ('approved', 'Produktionsfreigabe'),
                       ('wait', 'freigegeben'),
                       ('failed', 'Fehler Freigabe'),
                       ('started', 'Produktion begonnen'),
                       ('done', 'Produktion fertig'),
                       ('packed', 'Verpackt'),
                       ('delivered', 'Geliefert')]
    ORDER_TYPES = [
        ('M', 'Manuell'),
        ('D', 'DesignBox'),
        ('I', 'Industrie'),
        ('T', 'TZBox')
    ]
    assembled = fields.Boolean(string='Zusammengebaut')
    line_id = fields.Many2one(comodel_name='res.line', string='Produktionslinie')
    assembly_state = fields.Selection(ASSEMBLY_STATES, string="Status PG", track_visibility='onchange', help='Bitte nicht manuell ändern', default='')
    quote_name = fields.Char(compute='_compute_quote_name')
    internal_notes = fields.Text()
    assembly_notes = fields.Text()
    earliest_scheduled_date = fields.Datetime(compute='_compute_earliest_scheduled_date')
    positions = fields.Integer(string='Positionen', compute='_compute_positions')
    num_items = fields.Integer(string='Anzahl der Artikel', compute='_compute_num_items')
    weight_total = fields.Float(string='Gesamtgewicht', compute='_compute_weight_total')
    confirmation_nr = fields.Char('Freigabenummer')
    order_type = fields.Selection(ORDER_TYPES, string='Auftragsart', default='M')
    pg9_call = fields.Char(string='PG9-Auftrag', compute='_pg9_call', store=False)
    @api.multi
    def _pg9_call(self):
        for record in self:
            if record.origin != False:
                xref = 'xref:' + record.origin
                record.pg9_call = 'http://localhost:9531/pg9/order/' + xref
    # end def _pg9_call
    @api.multi
    @api.onchange('partner_invoice_id')
    def _onchange_partner_invoice_id(self):
        for record in self:
            if record.partner_invoice_id.is_retailer:
                record.payment_term_id = record.partner_invoice_id.property_payment_term_id
    @api.multi
    def _compute_weight_total(self):
        for record in self:
            _sum = 0
            for line in record.order_line:
                _sum += line.weight * line.product_uom_qty
            record.weight_total = _sum
    @api.multi
    def _compute_positions(self):
        for record in self:
            record.positions = len(record.order_line)
    @api.multi
    def _compute_num_items(self):
        for record in self:
            num_items = 0
            for line in record.order_line:
                if line.product_uom == self.env.ref('product.product_uom_unit'):  # wenn die Mengeneinheit Stk. ist
                    num_items += line.product_uom_qty
            record.num_items = num_items
    @api.multi
    def _compute_earliest_scheduled_date(self):
        for record in self:
            earliest_scheduled_date = False
            for picking in record.picking_ids:
                if not earliest_scheduled_date or earliest_scheduled_date > fields.Datetime.from_string(
                        picking.scheduled_date):
                    earliest_scheduled_date = fields.Datetime.from_string(picking.scheduled_date)
            if earliest_scheduled_date:
                record.earliest_scheduled_date = earliest_scheduled_date
    @api.multi
    def _compute_quote_name(self):
        for record in self:
            if record.state in ['draft', 'sent']:
                record.quote_name = record.name.replace('ATOC', 'ATOF')
            else:
                record.quote_name = record.name
    @api.model
    def pg_get_orders(self, line, state, limit):
        """
        SST-4
        :param line:
        :param state:
        :param limit:
        :return:
        """
        line_id = self.env['res.line'].search([('name', '=', line)])
        orders = self.search([('line_id', '=', line_id.id), ('assembly_state', '=', state)], order='id ASC',
                             limit=limit)
        order_list = []
        for order in orders:
            attachmets = self.env['ir.attachment'].search([('res_model', '=', 'sale.order'), ('res_id', '=', order.id)])
            attachment_list = []
            for attachment in attachmets:
                attachment_list.append({
                    'filename': attachment.name,
                    'binary': attachment.datas.decode()
                })
            delivery_date = False
            for picking_id in order.picking_ids:
                if not delivery_date:
                    delivery_date = picking_id.scheduled_date
                elif datetime.strptime(picking_id.scheduled_date, DEFAULT_SERVER_DATETIME_FORMAT) < datetime.strptime(
                        delivery_date, DEFAULT_SERVER_DATETIME_FORMAT):
                    delivery_date = picking_id.scheduled_date
            order_list.append({
                'id': order.id,
                'name': order.name,
                'attachments': attachment_list,
                'internal_notes': order.internal_notes,
                'assembly_notes': order.assembly_notes,
                'user_id': order.user_id.name,
                'delivery_date': delivery_date
            })
        return order_list
    @api.model
    def pg_update_quotation(self, vals):
        """
        SST-3a
        :param order_name:
        :return:
        """
        order_name = vals.get('order_name', False)
        order_line_vals = vals.get('order_lines', False)
        order_id = self.search([('name', '=', order_name)], order='id DESC',limit=1)
        order_id.pg_create_order_lines(order_line_vals)
        return {'id': order_id.id, 'name': order_id.name}
    @api.model
    def pg_create_quotation(self, vals):
        """
        SST-3
        :param vals:
        :return:
        """
        if not vals.get('portal_id', False):
            raise ValidationError(
                _("Der Kunde mit der Portal-ID \'%s\' kann nicht zugeordnet werden") % vals['portal_id'])
        partner = self.env['res.partner'].search([('portal_id', '=', vals['portal_id'])])
        if not partner.parent_id:
            raise ValidationError(
                _('Zu dem Kontakt mit der portal-ID %s existiert kein Unternehmen') % vals['portal_id'])
        partner = partner.parent_id
        vals['partner_id'] = partner.id
        if not partner:
            raise ValidationError(
                _("Der Kunde mit der Portal-ID \'%s\' kann nicht zugeordnet werden") % vals['portal_id'])
        delivery_partner = self.env['res.partner']
        delivery_vals = {}
        if vals.get('portal_delivery_id', False):
            delivery_partner = self.env['res.partner'].search([('portal_id', '=', vals['portal_delivery_id'])])
            delivery_vals['portal_id'] = vals['portal_delivery_id']
        for key in list(vals.keys()):
            if key.startswith('delivery_'):
                delivery_vals[key.replace('delivery_', '')] = vals[key]
        delivery_partner.with_context(delivery_partner=True).check_not_specified_fields(delivery_vals)
        delivery_vals = delivery_partner.correct_values(delivery_vals)
        delivery_vals['parent_id'] = partner.id
        if delivery_partner:
            allowed_update_keys = ['firstname', 'lastname', 'midname', 'email', 'phone']
            for key in list(delivery_vals.keys()):
                if key not in allowed_update_keys:
                    del delivery_vals[key]
            delivery_partner.write(delivery_vals)
        else:
            if not delivery_vals.get('type', False):
                delivery_vals['type'] = 'delivery'
            delivery_partner = delivery_partner.create(delivery_vals)
        attachment_vals = vals.get('attachment_ids', False)
        order_line_vals = vals.get('order_lines', False)
        self.check_not_specified_fields(vals)
        vals = self.correct_values(vals)
        payment_term = partner.property_payment_term_id
        if partner.retail_partner_id:
            payment_term = partner.retail_partner_id.property_payment_term_id
        addr = partner.address_get(['delivery', 'invoice'])
        vals['partner_invoice_id'] = vals.setdefault('partner_invoice_id', addr['invoice'])
        partner_invoice_id = self.env['res.partner'].browse([vals['partner_invoice_id']])
        fiscal_position_id = False
        if partner_invoice_id:
            if partner_invoice_id.property_account_position_id:
                fiscal_position_id = partner_invoice_id.property_account_position_id.id
        vals.update({
            'partner_id': partner.id,
            'partner_invoice_id': partner_invoice_id.id,
            'fiscal_position_id': fiscal_position_id,
            'user_id': partner.user_id.id,
            'payment_term_id': payment_term.id,
            'partner_shipping_id': delivery_partner.id,
            'incoterm': partner.sale_incoterm_id.id,
            'carrier_id': partner.property_delivery_carrier_id.id
        })
        order_id = self.create(vals)
        if attachment_vals:
            order_id.pg_create_sale_order_attachments(attachment_vals)
        if order_line_vals:
            order_id.pg_create_order_lines(order_line_vals)
        if vals.get('confirm_order'):
            order_id.action_confirm()
        return {'id': order_id.id, 'name': order_id.name}
    @api.multi
    def pg_create_sale_order_attachments(self, values):
        self.ensure_one()
        if isinstance(values, list):
            for vals in values:
                self.create_attachment(self, vals)
        else:
            self.create_attachment(self, values)
    @api.multi
    def pg_create_order_lines(self, values):
        order_lines = []
        for vals in values:
            vals = self.env['sale.order.line'].correct_values(vals)
            lot_id = False
            if vals.get('lot_id', False):
                if vals['lot_id'].get('attachment_ids', False):
                    lot_attachment_values = vals['lot_id']['attachment_ids']
                else:
                    lot_attachment_values = []
                lot = self.env['stock.production.lot'].create({
                    'name': vals['lot_id']['name'],
                    'product_id': vals['product_id'],
                    'weight': vals['lot_id'].get('weight'),
                    'image': vals['lot_id'].get('image'),
                    'notes': vals['lot_id']['notes']
                })
                for lot_attachment_vals in lot_attachment_values:
                    self.create_attachment(lot, lot_attachment_vals)
                lot_id = lot.id
            order_lines.append(self.env['sale.order.line'].create({
                'order_id': self.id,
                'name': vals['name'],
                'product_id': vals['product_id'],
                'price_unit': vals['price_unit'],
                'product_uom_qty': vals['product_uom_qty'],
                'lot_id': lot_id,
                'from_designbox': True,
            }))
        return order_lines
    @api.model
    def create_attachment(self, record, vals):
        attachment_vals = {
            'name': vals['filename'],
            'datas': vals['binary'],
            'from_designbox': True,
            'datas_fname': vals['filename'],
            'res_model': record._name,
            'res_id': record.id,
        }
        self.env['ir.attachment'].create(attachment_vals)
    @api.model
    def correct_values(self, vals):
        if vals.get('line_id', False):
            line_id = self.env['res.line'].search([('name', '=', vals['line_id'])])
            if line_id:
                vals['line_id'] = line_id.id
            else:
                raise ValidationError(
                    _("Produktionslinie \'%s\' kann nicht zugeordnet werden") % vals['line_id'])
        if vals.get('order_type', False):
            selections = [selection[0] for selection in self._fields['order_type'].selection]
            if vals.get('order_type') not in selections:
                raise ValidationError(_("Diese Auftragsart ist nicht bekannt!"))
        return vals
    @api.model
    def _get_specified_fields(self):
        return ['origin', 'client_order_ref', 'note', 'date_order', 'assembled', 'line_id', 'partner_id',
                'fiscal_position_id', 'user_id', 'payment_term_id', 'partner_delivery_id', 'partner_invoice_id',
                'assembly_state', 'confirmation_nr', 'confirm_order', 'order_type', 'internal_notes']
    @api.multi
    def write(self, vals):
        res = super(SaleOrder, self).write(vals)
        if vals.get('assembly_state', False) and vals.get('assembly_state', False) == 'done':
            self.message_post(body='Produktion fertig')
        return res
    @api.multi
    def unlink(self):
        for record in self:
            for line in record.order_line:
                line.lot_id.unlink()
        return super(SaleOrder, self).unlink()
    @api.multi
    def action_invoice_create(self, grouped=False, final=False):
        inv_obj = self.env['account.invoice']
        precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
        invoices = {}
        references = {}
        for order in self:
            collective_bill = order.partner_id.collective_bill and "x" or order.id
            group_key = order.id if grouped else (collective_bill, order.partner_invoice_id.id, order.currency_id.id)
            for line in order.order_line.sorted(key=lambda l: l.qty_to_invoice < 0):
                if float_is_zero(line.qty_to_invoice, precision_digits=precision):
                    continue
                if group_key not in invoices:
                    inv_data = order._prepare_invoice()
                    invoice = inv_obj.create(inv_data)
                    references[invoice] = order
                    invoices[group_key] = invoice
                elif group_key in invoices:
                    vals = {}
                    if order.name == "siehe Detail" or order.name not in invoices[group_key].origin.split(', '):
                        vals['origin'] = "siehe Detail"
                    if order.client_order_ref and (order.client_order_ref == "siehe Detail" or (
                            order.client_order_ref not in invoices[group_key].name.split(
                        ', ') and order.client_order_ref != invoices[group_key].name)):
                        vals['name'] = "siehe Detail"
                    invoices[group_key].write(vals)
                if line.qty_to_invoice > 0:
                    line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice)
                elif line.qty_to_invoice < 0 and final:
                    line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice)
            if references.get(invoices.get(group_key)):
                if order not in references[invoices[group_key]]:
                    references[invoice] = references[invoice] | order
        if not invoices:
            raise UserError(_('There is no invoicable line.'))
        for invoice in invoices.values():
            if not invoice.invoice_line_ids:
                raise UserError(_('There is no invoicable line.'))
            # If invoice is negative, do a refund invoice instead
            if invoice.amount_untaxed < 0:
                invoice.type = 'out_refund'
                for line in invoice.invoice_line_ids:
                    line.quantity = -line.quantity
            # Use additional field helper function (for account extensions)
            for line in invoice.invoice_line_ids:
                line._set_additional_fields(invoice)
            # Necessary to force computation of taxes. In account_invoice, they are triggered
            # by onchanges, which are not triggered when doing a create.
            invoice.compute_taxes()
            invoice.message_post_with_view('mail.message_origin_link',
                                           values={'self': invoice, 'origin': references[invoice]},
                                           subtype_id=self.env.ref('mail.mt_note').id)
        return [inv.id for inv in invoices.values()]
    @api.multi
    def action_confirm(self):
        # change name on order confirmation
        if self.name.startswith('ATOF'):
            new_name = re.sub(r"^ATOF", "ATOC", self.name)
            self.name = new_name
        return super(SaleOrder, self).action_confirm()
    @api.model
    def _formatLang(self, value, currency=True):
        lang = self.partner_id.lang
        lang_objs = self.env['res.lang'].search([('code', '=', lang)])
        if not lang_objs:
            lang_objs = self.env['res.lang'].search([], limit=1)
        lang_obj = lang_objs[0]
        res = lang_obj.format('%.' + str(2) + 'f', value, grouping=True, monetary=True)
        currency_obj = self.currency_id
        if currency_obj and currency_obj.symbol and currency:
            if currency_obj.position == 'after':
                res = '%s %s' % (res, currency_obj.symbol)
            elif currency_obj and currency_obj.position == 'before':
                res = '%s %s' % (currency_obj.symbol, res)
        return res
    @api.multi
    @api.onchange('partner_id')
    def onchange_partner_id(self):
        if not self.partner_id:
            self.update({
                'partner_invoice_id': False,
                'partner_shipping_id': False,
            })
            return
        addr = self.partner_id.address_get(['delivery', 'invoice'])
        values = {
            'partner_invoice_id': addr['invoice'],
            'partner_shipping_id': addr['delivery'],
            'pricelist_id': self.partner_id.property_product_pricelist and self.partner_id.property_product_pricelist.id or False,
            'user_id': self.partner_id.user_id.id or self.env.uid
        }
        if self.env['ir.config_parameter'].sudo().get_param(
                'sale.use_sale_note') and self.env.user.company_id.sale_note:
            values['note'] = self.with_context(lang=self.partner_id.lang).env.user.company_id.sale_note
        if self.partner_id.team_id:
            values['team_id'] = self.partner_id.team_id.id
        self.update(values)
    @api.multi
    @api.onchange('partner_invoice_id')
    def onchange_partner_invoice_id(self):
        if not self.partner_invoice_id:
            self.update({
                'payment_term_id': False,
                'fiscal_position_id': False,
            })
            return
        values = {
            'fiscal_position_id': self.partner_invoice_id.property_account_position_id and self.partner_invoice_id.property_account_position_id.id or False,
            'payment_term_id': self.partner_invoice_id.property_payment_term_id and self.partner_invoice_id.property_payment_term_id.id or False,
        }
        self.update(values)
class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'
    @api.model
    def _get_product_id_domain(self):
        return [('sale_ok', '=', True), '|', ('can_be_sold_unconfigured', '=', True), ('type', '=', 'service')]
    lot_id = fields.Many2one(comodel_name='stock.production.lot', string='Lot')
    from_designbox = fields.Boolean(string='I', readonly=True, help='Import von externem System')
    product_id = fields.Many2one(domain=_get_product_id_domain)
    weight = fields.Float(string='Gewicht', compute='_compute_weight')
    intrastat_id = fields.Many2one(comodel_name='report.intrastat.code', string='Intrastat Code')
    @api.model
    def create(self, vals):
        if not vals.get('intrastat_id', False):
            if vals.get('lot_id', False):
                vals.update(intrastat_id=self.env['stock.production.lot'].browse(vals['lot_id']).intrastat_id.id)
            elif vals.get('product_id', False):
                vals.update(intrastat_id=self.env['product.template'].browse(vals['product_id']).intrastat_id.id)
        return super(SaleOrderLine, self).create(vals)
    @api.multi
    def write(self, vals):
        for record in self:
            lot_id = record.lot_id if 'lot_id' not in vals else vals.get('lot_id', False)
            if vals.get('intrastat_id', False) and lot_id:
                self.env['stock.production.lot'].browse([lot_id.id]).write({
                    'intrastat_id': vals.get('intrastat_id')
                })
            elif vals.get('intrastat_id', False) and not lot_id:
                raise UserError(_('Der Intrastrat Code kann nur gesetzt werden wenn ein Lot angegeben wurde.'))
            not_allowed_designbox_keys = ['product_uom_qty', 'product_uom', 'price_unit']
            if not self.check_allowed_vals_from_designbox(not_allowed_designbox_keys, vals):
                raise ValidationError(_("Menge und Preis können von Produkten aus der Designbox nicht geändert werden"))
        return super(SaleOrderLine, self).write(vals)
    @api.multi
    def check_allowed_vals_from_designbox(self, not_allowed_designbox_keys, vals):
        self.ensure_one()
        allowed_write = True
        precision_digits = self.env['decimal.precision'].precision_get('Product Price')
        if self.from_designbox and set(vals.keys()).intersection(not_allowed_designbox_keys):
            for val_key in not_allowed_designbox_keys:
                if val_key in list(vals.keys()) and type(self.__getattribute__(val_key)) is float:
                    if float_compare(self.__getattribute__(val_key), vals[val_key],
                                     precision_digits=precision_digits) != 0:
                        allowed_write = False
                        break
                elif val_key in list(vals.keys()):
                    allowed_write = False
                    break
        return allowed_write
    @api.model
    def correct_values(self, vals):
        if vals.get('product_id', False):
            product_id = self.env['product.product'].search([('default_code', '=', vals['product_id'])])
            if product_id:
                vals['product_id'] = product_id.id
            else:
                raise ValidationError(
                    _("Produkt \'%s\' kann nicht zugeordnet werden") % vals['product_id'])
        return vals
    @api.multi
    def action_show_lot(self):
        self.ensure_one()
        action = self.env.ref('stock.action_production_lot_form').read()[0]
        action['res_id'] = self.lot_id.id
        action['view_mode'] = 'form'
        action['views'] = [(False, 'form')]
        return action
    @api.multi
    def _compute_weight(self):
        for record in self:
            record.weight = record.lot_id.weight or record.product_id.weight
    @api.multi
    def _prepare_invoice_line(self, qty):
        self.ensure_one()
        res = super(SaleOrderLine, self)._prepare_invoice_line(qty)
        res['lot_id'] = self.lot_id.id
        return res
    @api.multi
    def invoice_line_create(self, invoice_id, qty):
        """
        Overwritten and added a logic to create an extra line for discounts from a retailer
        :param invoice_id:
        :param qty:
        :return:
        """
        invoice_lines = self.env['account.invoice.line']
        precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
        for line in self:
            if not float_is_zero(qty, precision_digits=precision):
                vals = line._prepare_invoice_line(qty=qty)
                vals.update({'invoice_id': invoice_id, 'sale_line_ids': [(6, 0, [line.id])]})
                invoice_lines |= self.env['account.invoice.line'].create(vals)
                if line.order_id.partner_invoice_id.is_retailer:
                    discount = line.calc_discount()
                    discount = int(round(discount))
                    if discount > 0:
                        vals.update({
                            'price_unit': -invoice_lines.price_subtotal * (discount / 100),
                            'quantity': 1.0,
                            'uom_id': self.env.ref('product.product_uom_unit').id,
                            'name': 'Händlerrabatt {}%'.format(discount),
                            'hide_intrastat_code': True,
                            'dealer_discount': True
                        })
                        del vals['discount']
                        invoice_lines |= self.env['account.invoice.line'].create(vals)
        return invoice_lines
    @api.multi
    def calc_discount(self):
        discount = 0.0
        context_partner = dict(self.env.context, partner_id=self.order_id.partner_invoice_id.id,
                               date=self.order_id.date_order)
        pricelist_context = dict(context_partner, uom=self.product_uom.id)
        pricelist_id = self.order_id.partner_invoice_id.property_product_pricelist
        price, rule_id = pricelist_id.with_context(pricelist_context).get_product_price_rule(
            self.product_id, self.product_uom_qty or 1.0, self.order_id.partner_invoice_id)
        new_list_price, currency_id = self.with_context(context_partner)._get_real_price_currency(self.product_id,
                                                                                                  rule_id,
                                                                                                  self.product_uom_qty,
                                                                                                  self.product_uom,
                                                                                                  pricelist_id.id)
        if new_list_price != 0:
            if pricelist_id.currency_id.id != currency_id:
                # we need new_list_price in the same currency as price, which is in the SO's pricelist's currency
                new_list_price = self.env['res.currency'].browse(currency_id).with_context(context_partner).compute(
                    new_list_price, pricelist_id.currency_id)
            discount = (new_list_price - price) / new_list_price * 100
        return discount