# -*- coding: utf-8 -*-
###############################################################################
#
#    Copyright (C) 2001-2014 Micronaet SRL (<http://www.micronaet.it>).
#
#    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/>.
#
###############################################################################
import os
import sys
import logging
import openerp
import openerp.netsvc as netsvc
import openerp.addons.decimal_precision as dp
from openerp.osv import fields, osv, expression, orm
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from openerp import SUPERUSER_ID, api
from openerp import tools
from openerp.tools.translate import _
from openerp.tools.float_utils import float_round as round
from openerp.tools import (DEFAULT_SERVER_DATE_FORMAT, 
    DEFAULT_SERVER_DATETIME_FORMAT, 
    DATETIME_FORMATS_MAP, 
    float_compare)


_logger = logging.getLogger(__name__)

class ResCompany(orm.Model):
    """ Model name: ResCompany
    """
    _inherit = 'res.company'

    def history_previous_year_net(self, cr, uid, ids, context=None):
        ''' Call procedure in product obj
        ''' 
        return self.pool.get('product.product').history_previous_year_net(
            cr, uid, False, context=context)


class ProductProduct(orm.Model):
    """ Model name: ProductProduct
    """    
    _inherit = 'product.product'
    
    def history_previous_year_net(self, cr, uid, ids, context=None):
        ''' History previous data for new management
        '''
        if context is None:
            context = {}
            
        product_ids = self.search(cr, uid, [], context=context)
        ctx = context.copy()
        ctx['limit_up_date'] = '2016-12-31 23:59:59' # set limit

        log_file = open('/home/administrator/photo/update_history.txt', 'w')
        not_update = ''   
        i = 0
        for product in self.browse(         
                cr, uid, product_ids, context=ctx):
            i += 1
            try:    
                mx_net_qty = product.mx_net_qty
                default_code = product.default_code or ''
                if mx_net_qty:
                    self.write(cr, uid, product.id, {
                        'mx_history_net_qty': product.mx_net_qty,
                        }, context=ctx)
                    _logger.info('Update: %s > %s' % (
                        i, default_code))
                else:        
                    _logger.info('Jumped: %s > %s' % (
                        i, default_code))
            except:
                _logger.error('Error: %s > %s' % (
                    product.id,
                    default_code,
                    ))
                log_file.write('not update: %s\n' % product.id)                        
        log_file.close()
        return True
        
    _columns = {
        'mx_history_net_qty': fields.float('History net', digits=(16, 2)),
        }
    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

@@ -2386,117 +2386,37 @@
     
-log_file = open('/home/administrator/photo/update_history.txt', 'w')%0A        not_update = ''   %0A        i = 0
+i = 0%0A        product_db = %7B%7D
 %0A   
@@ -2532,33 +2532,8 @@
 = 1%0A
-            try:    %0A    
     
@@ -2588,193 +2588,58 @@
     
-    default_code = product.default_code or ''%0A                if mx_net_qty:%0A                    self.write(cr, uid, product.id, %7B%0A                        'mx_history_net_qty': 
+if mx_net_qty:%0A                product_db%5B
 product.
 mx_n
@@ -2626,32 +2626,38 @@
 duct_db%5Bproduct.
+id%5D = 
 mx_net_qty,%0A    
@@ -2654,54 +2654,9 @@
 _qty
-,%0A                        %7D, context=ctx)%0A    
+%0A
     
@@ -2685,39 +2685,40 @@
 fo('
-Update: %25s %3E %25s' %25 (%0A  
+%25s. Found product' %25 i)%0A
         
     
@@ -2709,32 +2709,33 @@
  i)%0A        
+%0A
         
       i, def
@@ -2726,116 +2726,157 @@
     
-      i, default_code))%0A                else:        %0A                    _logger.info('Jumped: %25s %3E %25s' %25 (
+for item, qty in product_db.iteritems(): %0A            try:%0A                self.write(cr, uid, item, %7B%0A                    'mx_history_net_qty': qty,
 %0A   
@@ -2896,28 +2896,22 @@
     
-    i, default_code)
+%7D, context=ctx
 )%0A  
@@ -2972,220 +2972,42 @@
 : %25s
- %3E %25s' %25 (%0A                    product.id,%0A                    default_code,%0A                    ))%0A                log_file.write('not update: %25s%5Cn' %25 product.id)                        %0A        log_file.close()
+' %25 item)%0A                continue
 %0A   

remove all use a loop after