stock_picking.py 4.73 KB
Newer Older
François C. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
# -*- 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})