<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2015 Comunitea All Rights Reserved
#    $<NAME> <<EMAIL>>$
#
#    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/>.
#
##############################################################################

from openerp import models, fields, api, _
from openerp.exceptions import Warning
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
import time


class StockPicking(models.Model):

    _inherit = "stock.picking"

    pending_invoice_move_id = fields.Many2one('account.move',
                                              'Account pending move',
                                              readonly=True,
                                              copy=False)

    @api.multi
    def action_done(self):
        res = super(StockPicking, self).action_done()
        for pick in self:
            if not pick.date_done:
                pick.date_done = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        return res

    @api.one
    def account_pending_invoice(self):
        period_obj = self.env['account.period']
        move_obj = self.env['account.move']
        move_line_obj = self.env['account.move.line']

        amount = 0
        date = self.min_date and self.min_date[:10] or \
            time.strftime('%Y-%m-%d')
        period_id = period_obj.find(date)

        origin = self.name
        if self.origin:
            origin += ':' + self.origin

        stock_journal_id = self.company_id.property_pending_stock_journal.id

        move = {
            'ref': origin,
            'journal_id': stock_journal_id,
            'period_id': period_id.id,
            'date': date,
        }
        move_id = move_obj.create(move)
        obj_precision = self.env['decimal.precision']
        for move_line in self.move_lines:
            # Get expense account
            account_id = move_line.product_id.product_tmpl_id.\
                property_account_expense.id
            if not account_id:
                account_id = move_line.product_id.categ_id.\
                    property_account_expense_categ.id

            name = move_line.name or origin
            if move_line.purchase_line_id:
                unit_price_line = move_line.purchase_line_id.price_unit
                discount_line = move_line.purchase_line_id.discount or 0.0
            else:
                raise Warning("There is no purchase line related. Can not "
                              "calculate price for accounting pending invoice")

            price_line = unit_price_line * (1 - (discount_line or 0.0) / 100.0)
            price_line = price_line * move_line.product_qty
            amount_line = round(price_line, obj_precision.
                                precision_get('Account'))
            from_currency = move_line.purchase_line_id.order_id.currency_id
            to_currency = move_line.company_id.currency_id
            amount_line = from_currency.compute(amount_line, to_currency)
            amount += amount_line
            vals = {
                'name': name,
                'ref': origin,
                'partner_id': self.partner_id.commercial_partner_id.id,
                'product_id': move_line.product_id.id,
                'account_id': account_id,
                'debit': amount_line,
                'credit': 0,
                'quantity': move_line.product_qty,
                'move_id': move_id.id,
                'journal_id': stock_journal_id,
                'period_id': period_id.id,
            }
            move_line_obj.create(vals)

        account_id = self.company_id.\
            property_pending_supplier_invoice_account.id
        vals = {
            'name': name,
            'ref': origin,
            'partner_id': self.partner_id.commercial_partner_id.id,
            'account_id': account_id,
            'debit': 0,
            'credit': amount,
            'move_id': move_id.id,
            'journal_id': stock_journal_id,
            'period_id': period_id.id,
        }

        move_line_obj.create(vals)
        move_id.post()

        self.pending_invoice_move_id = move_id<commit_msg>[FIX] picking_invoice_pending: Corregimos el módulo para uq etnga en cuenta posible smovimientos sin precio<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2015 Comunitea All Rights Reserved
#    $<NAME> <<EMAIL>>$
#
#    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/>.
#
##############################################################################

from openerp import models, fields, api, _
from openerp.exceptions import Warning
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
import time


class StockPicking(models.Model):

    _inherit = "stock.picking"

    pending_invoice_move_id = fields.Many2one('account.move',
                                              'Account pending move',
                                              readonly=True,
                                              copy=False)

    @api.multi
    def action_done(self):
        res = super(StockPicking, self).action_done()
        for pick in self:
            if not pick.date_done:
                pick.date_done = time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        return res

    @api.one
    def account_pending_invoice(self):
        period_obj = self.env['account.period']
        move_obj = self.env['account.move']
        move_line_obj = self.env['account.move.line']
        lines = []

        amount = 0
        date = self.min_date and self.min_date[:10] or \
            time.strftime('%Y-%m-%d')
        period_id = period_obj.find(date)

        origin = self.name
        if self.origin:
            origin += ':' + self.origin

        stock_journal_id = self.company_id.property_pending_stock_journal.id

        move = {
            'ref': origin,
            'journal_id': stock_journal_id,
            'period_id': period_id.id,
            'date': date,
        }
        move_id = move_obj.create(move)
        obj_precision = self.env['decimal.precision']
        for move_line in self.move_lines:
            # Get expense account
            account_id = move_line.product_id.product_tmpl_id.\
                property_account_expense.id
            if not account_id:
                account_id = move_line.product_id.categ_id.\
                    property_account_expense_categ.id

            name = move_line.name or origin
            if move_line.purchase_line_id:
                unit_price_line = move_line.purchase_line_id.price_unit
                discount_line = move_line.purchase_line_id.discount or 0.0
            else:
                continue
                #raise Warning("There is no purchase line related. Can not "
                #              "calculate price for accounting pending invoice")

            price_line = unit_price_line * (1 - (discount_line or 0.0) / 100.0)
            price_line = price_line * move_line.product_qty
            amount_line = round(price_line, obj_precision.
                                precision_get('Account'))
            from_currency = move_line.purchase_line_id.order_id.currency_id
            to_currency = move_line.company_id.currency_id
            amount_line = from_currency.compute(amount_line, to_currency)
            amount += amount_line
            vals = {
                'name': name,
                'ref': origin,
                'partner_id': self.partner_id.commercial_partner_id.id,
                'product_id': move_line.product_id.id,
                'account_id': account_id,
                'debit': amount_line,
                'credit': 0,
                'quantity': move_line.product_qty,
                'move_id': move_id.id,
                'journal_id': stock_journal_id,
                'period_id': period_id.id,
            }
            line_id = move_line_obj.create(vals)
            lines.append(lines)

        if lines:
            account_id = self.company_id.\
                property_pending_supplier_invoice_account.id
            vals = {
                'name': name,
                'ref': origin,
                'partner_id': self.partner_id.commercial_partner_id.id,
                'account_id': account_id,
                'debit': 0,
                'credit': amount,
                'move_id': move_id.id,
                'journal_id': stock_journal_id,
                'period_id': period_id.id,
            }

            move_line_obj.create(vals)
            move_id.post()

            self.pending_invoice_move_id = move_id
        else:
            move_id.unlink()
            move_id = False
{'fraction_non_alphanumeric': 0.11076738848418322, 'fraction_numerical': 0.0041911984831853105, 'mean_word_length': 1.9082994776552524, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6812416', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2689, 'n_words': 652}