# -*- 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 api, fields, models
class SaleLayoutCategory(models.Model):
    _inherit = 'sale.layout_category'
    order_id = fields.Many2one(comodel_name='sale.order')
class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'
    layout_category_id = fields.Many2one(domain=[('order_id', '=', False)])
    @api.multi
    def _prepare_invoice_line(self, qty):
        res = super(SaleOrderLine, self)._prepare_invoice_line(qty)
        layout_category_id = self.env['sale.layout_category'].search([('order_id', '=', self.order_id.id)])
        if not layout_category_id:
            layout_category_id = self.env['sale.layout_category'].create({
                'order_id': self.order_id.id,
                'name': self.order_id.name
            })
        res.update({'layout_category_id': layout_category_id.id})
        return res