# -*- 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 .
#
##############################################################################
from odoo import fields, models, api, _
from odoo.exceptions import ValidationError
class ProductXCategory(models.Model):
    _name = 'product.xcategory'
    _description = 'X-Kategorie'
    _order = 'name'
    name = fields.Char(string='Bezaichnung', required=True)
    _sql_constraints = [
        ('name_uniq', 'unique(name)', 'Die Bezeichnung muss eindeutig sein')
    ]
class ProductTemplate(models.Model):
    _name = 'product.template'
    _inherit = ['product.template', 'dp_custom.helper']
    SURFACE_OPTIONS = [
        ('m', 'Maserrichtung'),
        ('u', 'Einfärbig')
    ]
    length = fields.Float(string='Länge in mm')
    width = fields.Float(string='Breite in mm')
    thickness = fields.Float(string='Ficke in mm', help='Echte Dicke in mm')
    surface = fields.Selection(SURFACE_OPTIONS, string='Oberfläche')
    is_internal = fields.Boolean()
    xcat_id = fields.Many2one(comodel_name='product.xcategory', string='X-Kategorie')
    material_type_id = fields.Many2one(comodel_name='material.type', string='Materialtyp')
    assembly_line_ids = fields.Many2many(comodel_name='res.line', string='Produktionslinien')
    @api.model
    def create_product(self, vals):
        vals = self.remove_not_specified_fields(vals)
        vals = self.correct_values(vals)
        product_template = self.with_context(active_test=False).search([('default_code', '=', vals['default_code'])])
        if product_template:
            product_template.write(vals)
        else:
            self.create(vals)
        return True
    @api.model
    def correct_values(self, vals):
        if vals.get('xcat_id', False):
            xcat = self.env['product.xcategory'].search([('name', '=', vals['xcat_id'])])
            if xcat:
                vals['xcat_id'] = xcat.id
            else:
                raise ValidationError(
                    _("X-Kategorie \'%s\' kann nicht zugeordnet werden" % vals['xcat_id']))
        if vals.get('material_type_id', False):
            material_type = self.env['material.type'].search([('name', '=', vals['material_type_id'])])
            if material_type:
                vals['material_type_id'] = material_type.id
            else:
                raise ValidationError(
                    _("Materialtyp \'%s\' kann nicht zugeordnet werden" % vals['material_type_id']))
        if vals.get('intrastat_id', False):
            intrastat = self.env['report.intrastat.code'].search([('name', '=', vals['intrastat_id'])])
            if intrastat:
                vals['intrastat_id'] = intrastat.id
            else:
                raise ValidationError(
                    _("Intrastat-Code \'%s\' kann nicht zugeordnet werden" % vals['intrastat_id']))
        if vals.get('assembly_line_ids', False):
            assembly_line_ids = []
            for assembly_line_code in vals['assembly_line_ids']:
                assembly_line = self.env['res.line'].search([('name', '=', assembly_line_code)])
                if assembly_line:
                    assembly_line_ids.append(assembly_line.id)
                else:
                    raise ValidationError(
                        _("Produktionslinie \'%s\' kann nicht zugeordnet werden" % assembly_line_code))
            vals['assembly_line_ids'] = [(6, 0, assembly_line_ids)]
        return vals
    @api.model
    def _get_specified_fields(self):
        return ['default_code', 'name', 'length', 'width', 'thickness', 'surface', 'active', 'weight', 'is_internal',
                'xcat_id', 'notes', 'material_type_id', 'intrastat_id', 'sale_ok', 'assembly_line_ids', 'list_price']