# -*- 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)

from inventory_present import product_present

_logger = logging.getLogger(__name__)

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

    # Procedure:    
    def save_cost_in_cost_method(self, cr, uid, ids, context=None):
        ''' Migrate 3 cost from old part in new cost management
        '''
        # Log operation
        log_file = '/home/administrator/photo/output/indoor_cost_migration.csv'
        f_log = open(log_file, 'w')
        
        product_pool = self.pool.get('product.product')
        product_ids = product_pool.search(cr, uid, [
            ('statistic_category', 'in', (
                'I01', 'I02', 'I03', 'I04', 'I05', 'I06')),
            ], context=context)
            
        f_log.write(
            'Codice|Cat. Stat.|Costo fornitore|Azienda|Cliente\n')
        for product in product_pool.browse(
                cr, uid, product_ids, context=context):
            product_pool.write(cr, uid, product.id, {
                'company_cost': product.cost_in_stock,
                'customer_cost': product.cost_for_sale,
                }, context=context)
            f_log.write(
                '%s|%s|%s|%s|%s\n' % (
                    product.default_code,
                    product.statistic_category,
                    product.standard_price,
                    product.cost_in_stock,
                    product.cost_for_sale,
                )                
            
    def inventory_to_reset(self, cr, uid, ids, context=None):
        ''' Check error during operation on bom
        '''
        out_file = '/home/administrator/photo/log/product_to_reset'
        out_file_jump = '/home/administrator/photo/log/product_to_reset_jumped'
        
        out_f = open(out_file, 'w')
        out_jump = open(out_file_jump, 'w')
        
        # Pool used:
        product_pool = self.pool.get('product.product')

        # Check HW bon link:
        product_ids = product_pool.search(cr, uid, [
            ('default_code', 'not in', product_present),                     
            ], context=context)

        for product in product_pool.browse(
                cr, uid, product_ids, context=context):                
            if not product.default_code or product.default_code[:1] not in \
                    '1234567890G':                    
                out_jump.write('%s|0\n' % product.default_code)
                continue     
                    
            out_f.write('%s|0\n' % product.default_code)
        out_f.close()
        out_jump.close()        

@@ -2347,16 +2347,21 @@
 enda
+ Da%7CA
 %7CCliente
 %5Cn')
@@ -2356,16 +2356,21 @@
 %7CCliente
+ Da%7CA
 %5Cn')%0A   
@@ -2470,209 +2470,8 @@
 t):%0A
-            product_pool.write(cr, uid, product.id, %7B%0A                'company_cost': product.cost_in_stock,%0A                'customer_cost': product.cost_for_sale,%0A                %7D, context=context)%0A
     
@@ -2522,16 +2522,22 @@
 %25s%7C%25s%7C%25s
+%7C%25s%7C%25s
 %5Cn' %25 (%0A
@@ -2713,16 +2713,58 @@
 _stock,%0A
+                    product.company_cost,%0A
         
@@ -2818,25 +2818,273 @@
     
-)                
+    product.customer_cost,%0A                )                %0A            #product_pool.write(cr, uid, product.id, %7B%0A            #    'company_cost': product.cost_in_stock,%0A            #    'customer_cost': product.cost_for_sale,%0A            #    %7D, context=context)
 %0A   

test for now