# -*- coding: utf-8 -*-
###############################################################################
#                                                                             #
# Copyright (C) 2016  Luis Felipe Mileo - KMEE                                #
#                                                                             #
# 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 <http://www.gnu.org/licenses/>.       #
###############################################################################

from openerp import models, api


class ProcurementOrder(models.Model):
    _inherit = "procurement.order"

    @api.model
    def _run_move_create(self, procurement):
        result = super(ProcurementOrder, self)._run_move_create(procurement)
        if procurement.rule_id and procurement.rule_id.fiscal_category_id:
            result.update({
                'fiscal_category_id':
                    procurement.rule_id.fiscal_category_id.id,
            })

            if not procurement.move_dest_id:
                return result

            ctx = dict(self.env.context)
            ctx.update({'use_domain': ('use_picking', '=', True)})
            move = procurement.move_dest_id
            rule = procurement.rule_id
            kwargs = {
                'partner_id': move.partner_id.id,
                'product_id': procurement.product_id.id,
                'partner_invoice_id':  move.partner_id.id,
                # TODO: Implement fuction to compute partner invoice id
                'partner_shipping_id': move.partner_id.id,
                'fiscal_category_id': rule.fiscal_category_id.id,
                'company_id': procurement.company_id.id,
                'context': ctx,
            }

            partner = move.picking_id.partner_id
            obj_fp_rule = self.env['account.fiscal.position.rule']
            product_fc_id = obj_fp_rule.with_context(
                ctx).product_fiscal_category_map(
                    kwargs.get('product_id'),
                    kwargs.get('fiscal_category_id'),
                    partner.state_id.id)

            if product_fc_id:
                kwargs['fiscal_category_id'] = product_fc_id
                result['fiscal_category_id'] = product_fc_id
            else:
                result['fiscal_category_id'] = kwargs.get(
                    'fiscal_category_id')

            result_fr = obj_fp_rule.with_context(ctx).apply_fiscal_mapping(
                {'value': {}}, **kwargs)

            result.update({
                'fiscal_position': result_fr['value']['fiscal_position']})
        return result

@@ -1634,16 +1634,17 @@
      if 
+(
 procurem
@@ -1697,17 +1697,20 @@
 egory_id
-:
+ and
 %0A       
@@ -1718,124 +1718,142 @@
     
-result.update(%7B%0A                'fiscal_category_id':%0A                    procurement.rule_id.fiscal_category_id.id,
+procurement.move_dest_id):%0A            ctx = dict(self.env.context)%0A            ctx.update(%7B'use_domain': ('use_picking', '=', True)%7D)
 %0A   
@@ -1861,19 +1861,27 @@
         
-%7D)%0A
+partner = (
 %0A       
@@ -1889,14 +1889,11 @@
     
-if not
+   
  pro
@@ -1905,159 +1905,130 @@
 ent.
-move_dest_id:%0A                return result%0A%0A            ctx = dict(self.env.context)%0A            ctx.update(%7B'use_domain': ('use_picking', '=', True)%7D
+rule_id.partner_address_id or (%0A                    procurement.group_id and procurement.group_id.partner_id)%0A            
 )%0A  
@@ -2041,15 +2041,19 @@
     
-move
+company
  = 
+(
 proc
@@ -2060,28 +2060,42 @@
 urement.
-move_dest_id
+warehouse_id.company_id or
 %0A       
@@ -2099,22 +2099,26 @@
         
-rule =
+          
  procure
@@ -2122,23 +2122,27 @@
 urement.
-rule
+company
 _id
+)
 %0A       
@@ -2183,39 +2183,31 @@
 artner_id': 
-move.
 partner
-_id
 .id,%0A       
@@ -2295,31 +2295,23 @@
 e_id':  
-move.
 partner
-_id
 .id,%0A   
@@ -2422,23 +2422,15 @@
 d': 
-move.
 partner
-_id
 .id,
@@ -2468,20 +2468,57 @@
 ry_id': 
+(%0A                    procurement.
 rule
+_id
 .fiscal_
@@ -2527,24 +2527,42 @@
 tegory_id.id
+%0A                )
 ,%0A          
@@ -2581,38 +2581,23 @@
 ny_id': 
-procurement.
 company
-_id
 .id,%0A   
@@ -2644,57 +2644,8 @@
  %7D%0A%0A
-            partner = move.picking_id.partner_id%0A
     

Fix procurement rule company_id