# -*- coding: utf-8 -*- # Copyright (C) 2013-Today: GRAP (http://www.grap.coop) # Copyright (C) 2016-Today: La Louve (<http://www.lalouve.net/>) # @author: Sylvain LE GAL (https://twitter.com/legalsylvain) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openerp import fields, models, api, _ class StockPicking(models.Model): _inherit = 'stock.picking' # Column Section is_expense_transfer = fields.Boolean( string='Is Expense Transfer', related='picking_type_id.is_expense_transfer', store=True) inventory_value = fields.Float( string='Inventory Value', compute='_compute_inventory_value', store=True) expense_transfer_move_id = fields.Many2one( comodel_name='account.move', string='Expense Transfer Accounting Entry', readonly=True, copy=False) # Compute Section @api.multi @api.depends('move_lines_related.inventory_value') def _compute_inventory_value(self): for picking in self: picking.inventory_value =\ sum(picking.mapped('move_lines_related.inventory_value')) # Custom Section @api.model def get_expense_entry_key(self, picking): """ define how to group by picking to generate a unique Journal Entry. By default, an entry is generated by picking type and by month. Overwrite this function to change the behaviour. """ dt = fields.Date.from_string(picking.date) return ( picking.picking_type_id.id, '%d-%d' % (dt.year, dt.month), ) @api.multi def generate_expense_entry(self): account_move_obj = self.env['account.move'] picking_data = {} for picking in self: key = self.get_expense_entry_key(picking) if key in picking_data.keys(): picking_data[key]['picking_ids'].append(picking.id) picking_data[key]['date'] = max( picking_data[key]['date'], picking.min_date) picking_data[key]['amount'] += picking.inventory_value else: picking_data[key] = { 'picking_type': picking.picking_type_id, 'picking_ids': [picking.id], 'date': picking.min_date, 'amount': picking.inventory_value, } for key, value in picking_data.iteritems(): pickings = self.browse(value['picking_ids']) picking_type = value['picking_type'] line_data = {} for picking in pickings: for move in picking.move_lines_related: account_id = move.product_id.product_tmpl_id.\ get_product_accounts()['expense'] if account_id in line_data: line_data[account_id] += move.inventory_value else: line_data[account_id] = move.inventory_value # Create Main Account Move Line line_values = { 'name': _('Expense Transfert (%s)') % (picking_type.name), 'date': value['date'], 'account_id': picking_type.expense_transfer_account_id.id, 'debit': (value['amount'] >= 0) and value['amount'] or 0, 'credit': (value['amount'] < 0) and -value['amount'] or 0, } account_move_lines = [(0, 0, line_values)] # Create Counterpart Account Move Line(s) for account_id, inventory_value in line_data.iteritems(): line_values = { 'name': _('Expense Transfert (%s)') % (picking_type.name), 'date': value['date'], 'product_id': False, 'product_uom_id': False, 'quantity': 0, 'account_id': account_id.id, 'credit': (inventory_value >= 0) and inventory_value or 0, 'debit': (inventory_value < 0) and -inventory_value or 0, } account_move_lines.append((0, 0, line_values)) # Generate Account Move move_values = { 'journal_id': picking_type.expense_transfer_journal_id.id, 'company_id': picking_type.warehouse_id.company_id.id, 'line_ids': account_move_lines, 'date': value['date'], 'ref': _('Expense Transfert (%s)') % (picking_type.name), } account_move_id = account_move_obj.create(move_values) # Validate Account Move account_move_id.post() # associate pickings to account move pickings.write({'expense_transfer_move_id': account_move_id.id})