# -*- coding: utf-8 -*-
##############################################################################
#
#    Odoo, Open Source Management Solution
#    Copyright (C) 2016 Prime Consulting SA, Cape Verde (<http://prime.cv>).
#
##############################################################################

from odoo import models, fields, api

class ProductPricelistItem(models.Model):
    _inherit = "product.pricelist.item"
    _order = "min_quantity,product_tmpl_id"
   
    product_category_id = fields.Many2one('product.category', string="Product Type")

    @api.model
    def create(self, vals):
        print "########################### CREATE PRICELIST ITEM ###########################"
        """
        Overrides orm create method.
        @param self: The object pointer
        @param vals: dictionary of fields value.
        """
        if not vals:
            vals = {}
        if self._context is None:
            self._context = {}
        if 'product_tmpl_id' in vals:
            if vals['product_tmpl_id']:
                prd_pricelist = self.env['product.template'].search([('id', '=', vals['product_tmpl_id'])], limit=1)
                if prd_pricelist:
                    vals['product_category_id'] = prd_pricelist.categ_id.id
        return super(ProductPricelistItem, self).create(vals)

    @api.multi
    def write(self, vals):
        print "WRITE PRICELIST ITEM: ", vals
        """
        Overrides orm write method.
        @param self: The object pointer
        @param vals: dictionary of fields value.
        update Hotel Room Reservation line history"""
        product_tmpl_id = False
        if self.product_tmpl_id:
            product_tmpl_id=self.product_tmpl_id.id
        if 'product_tmpl_id' in vals:
            product_tmpl_id=vals['product_tmpl_id']

        if product_tmpl_id:
            prd_pricelist = self.env['product.template'].search([('id', '=', product_tmpl_id)], limit=1)
            if prd_pricelist:
                vals['product_category_id'] = prd_pricelist.categ_id.id
        return super(ProductPricelistItem, self).write(vals)


class ProductPricelist(models.Model):
    _inherit = "product.pricelist"

    min_quantity = fields.Integer(related="item_ids.min_quantity", string="Min Quantity")
    product_tmpl_id = fields.Many2one('product.template', related="item_ids.product_tmpl_id", string="Product")

class ProductTemplate(models.Model):
    _inherit = "product.template"

    @api.multi
    def name_get(self):
        """Show Product name with reference on Pricelist form
        """
        result = []
        context = self.env.context or {}
        if context and 'no_ref' in context:
            for product in self:
            	productname = product.name
            	if product.default_code:
            		productname = '%s [%s]'%(product.name, product.default_code)
                result.append((product.id, productname))
        else:
            result = super(ProductTemplate, self).name_get()
        return result
