<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    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 time

from openerp.osv import fields, osv
import openerp.addons.decimal_precision as dp
from openerp.tools.translate import _
import product

class stock_landed_cost(osv.osv):
    _name = 'stock.landed.cost'
    _description = 'Stock Landed Cost'

    def _total_amount(self, cr, uid, ids, name, args, context=None):
        result = {}
        for cost in self.browse(cr, uid, ids, context=context):
            total = 0.0
            for line in cost.cost_lines:
                total += line.price_unit
            result[cost.id] = total
        return result

    def _get_cost_line(self, cr, uid, ids, context=None):
        result = {}
        for line in self.pool.get('stock.landed.cost.lines').browse(cr, uid, ids, context=context):
            result[line.cost_id.id] = True
        return result.keys()

    def onchange_pickings(self, cr, uid, ids, picking_ids=None):
        result = {'valuation_adjustment_lines': []}
        line_obj = self.pool.get('stock.valuation.adjustment.lines')
        picking_obj = self.pool.get('stock.picking')
        lines = []
 
        for cost in self.browse(cr, uid, ids):
            line_ids = [line.id for line in cost.valuation_adjustment_lines]
            line_obj.unlink(cr, uid, line_ids)

        picking_ids = picking_ids and picking_ids[0][2] or False
        if not picking_ids:
            return {'value': result}

        for picking in picking_obj.browse(cr, uid, picking_ids):
            for move in picking.move_lines:
                total_cost = 0.0
                total_qty = 0.0
                weight = move.product_id and move.product_id.weight
                volume = move.product_id and move.product_id.volume
                for quant in move.quant_ids:
                    total_cost += quant.cost
                    total_qty += quant.qty
                vals = dict(product_id = move.product_id.id, move_id = move.id, quantity = move.product_uom_qty, former_cost = total_cost * total_qty, weight = weight, volume = volume, flag = 'original')
                lines.append(vals)
        result['valuation_adjustment_lines'] = lines
        return {'value': result}

    _columns = {
        'name': fields.char('Name', size=256, required=True, states={'open': [('readonly', True)]}),
        'date': fields.date('Date', required=True, states={'open': [('readonly', True)]}),
        'picking_ids': fields.many2many('stock.picking', string='Pickings', states={'open': [('readonly', True)]}),
        'cost_lines': fields.one2many('stock.landed.cost.lines', 'cost_id', 'Cost Lines', states={'open': [('readonly', True)]}),
        'valuation_adjustment_lines': fields.one2many('stock.valuation.adjustment.lines', 'cost_id', 'Valuation Adjustments', states={'open': [('readonly', True)]}),
        'description': fields.text('Item Description', states={'open': [('readonly', True)]}),
        'amount_total': fields.function(_total_amount, type='float', string='Total', digits_compute=dp.get_precision('Account'),
            store={
                'stock.landed.cost': (lambda self, cr, uid, ids, c={}: ids, ['cost_lines'], 20),
                'stock.landed.cost.lines': (_get_cost_line, ['price_unit', 'quantity', 'cost_id'], 20),
            }
        ),
        'state':fields.selection([('draft', 'Draft'), ('open', 'Open'), ('cancel', 'Cancelled')], 'State', readonly=True),
    }

    _defaults = {
        'state': 'draft',
        'date': lambda *a: time.strftime('%Y-%m-%d'),
    }<commit_msg>[FIX] stock_landed_costs: don't consider products for which it doesn't make sense to compute a landed cost<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    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 time

from openerp.osv import fields, osv
import openerp.addons.decimal_precision as dp
from openerp.tools.translate import _
import product

class stock_landed_cost(osv.osv):
    _name = 'stock.landed.cost'
    _description = 'Stock Landed Cost'

    def _total_amount(self, cr, uid, ids, name, args, context=None):
        result = {}
        for cost in self.browse(cr, uid, ids, context=context):
            total = 0.0
            for line in cost.cost_lines:
                total += line.price_unit
            result[cost.id] = total
        return result

    def _get_cost_line(self, cr, uid, ids, context=None):
        result = {}
        for line in self.pool.get('stock.landed.cost.lines').browse(cr, uid, ids, context=context):
            result[line.cost_id.id] = True
        return result.keys()

    def onchange_pickings(self, cr, uid, ids, picking_ids=None):
        result = {'valuation_adjustment_lines': []}
        line_obj = self.pool.get('stock.valuation.adjustment.lines')
        picking_obj = self.pool.get('stock.picking')
        lines = []
        for cost in self.browse(cr, uid, ids):
            line_ids = [line.id for line in cost.valuation_adjustment_lines]
            line_obj.unlink(cr, uid, line_ids)
        picking_ids = picking_ids and picking_ids[0][2] or False
        if not picking_ids:
            return {'value': result}

        for picking in picking_obj.browse(cr, uid, picking_ids):
            for move in picking.move_lines:
                #it doesn't make sense to make a landed cost for a product that isn't set as being valuated in real time at real cost
                if move.product_id.valuation != 'real_time' or move.product_id.cost_method != 'real':
                    continue
                total_cost = 0.0
                total_qty = 0.0
                weight = move.product_id and move.product_id.weight
                volume = move.product_id and move.product_id.volume
                for quant in move.quant_ids:
                    total_cost += quant.cost
                    total_qty += quant.qty
                vals = dict(product_id=move.product_id.id, move_id=move.id, quantity=move.product_uom_qty, former_cost=total_cost * total_qty, weight=weight, volume=volume, flag='original')
                lines.append(vals)
        result['valuation_adjustment_lines'] = lines
        return {'value': result}

    _columns = {
        'name': fields.char('Name', size=256, required=True, states={'open': [('readonly', True)]}),
        'date': fields.date('Date', required=True, states={'open': [('readonly', True)]}),
        'picking_ids': fields.many2many('stock.picking', string='Pickings', states={'open': [('readonly', True)]}),
        'cost_lines': fields.one2many('stock.landed.cost.lines', 'cost_id', 'Cost Lines', states={'open': [('readonly', True)]}),
        'valuation_adjustment_lines': fields.one2many('stock.valuation.adjustment.lines', 'cost_id', 'Valuation Adjustments', states={'open': [('readonly', True)]}),
        'description': fields.text('Item Description', states={'open': [('readonly', True)]}),
        'amount_total': fields.function(_total_amount, type='float', string='Total', digits_compute=dp.get_precision('Account'),
            store={
                'stock.landed.cost': (lambda self, cr, uid, ids, c={}: ids, ['cost_lines'], 20),
                'stock.landed.cost.lines': (_get_cost_line, ['price_unit', 'quantity', 'cost_id'], 20),
            }
        ),
        'state':fields.selection([('draft', 'Draft'), ('open', 'Open'), ('cancel', 'Cancelled')], 'State', readonly=True),
    }

    _defaults = {
        'state': 'draft',
        'date': lambda *a: time.strftime('%Y-%m-%d'),
    }
{'fraction_non_alphanumeric': 0.16031354021260605, 'fraction_numerical': 0.006013099967786964, 'mean_word_length': 2.933277027027027, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6073139', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2649, 'n_words': 753}