account_full_reconcile.py 4.89 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
# -*- coding: utf-8 -*-
# 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 models, api, fields, _
from openerp.exceptions import UserError


class AccountFullReconcile(models.Model):
    _inherit = 'account.full.reconcile'

    # Custom Section
    @api.multi
    def generate_capital_entrie(self, undo=False):
        move_obj = self.env['account.move']
        for reconcile in self:
            category_ids = reconcile.mapped(
                'reconciled_line_ids.invoice_id.fundraising_category_id')
            invoices = reconcile.mapped('reconciled_line_ids.invoice_id')

            # get payment entry
            payment_id = reconcile.mapped('reconciled_line_ids.payment_id')

            # get line to reconcile
            move_line = invoices.mapped('move_id.line_ids')
            line_to_reconcile = move_line.filtered(lambda l: l.credit != 0)

            # Do not create any extra entry for capital refund
            if not invoices or invoices[0].type == 'out_refund':
                continue

            if len(category_ids) > 1:
                raise UserError(_(
                    "You can not reconcile Capital Invoices for many"
                    " Categories: %s" % (
                        ', '.join(category_ids.mapped('name')))))
            elif len(category_ids) == 1 and category_ids[0].capital_account_id:
                # Create new account move
                category = category_ids[0]
                capital_entries_journal = \
                    category.fundraising_id.journal_ids and \
                    category.fundraising_id.journal_ids or \
                    category.fundraising_id.journal_id
                partner = reconcile.reconciled_line_ids[0].partner_id

                move_lines = reconcile.mapped('reconciled_line_ids').filtered(
                    lambda r: r.journal_id.id in capital_entries_journal.ids)

                if not move_lines:
                    continue
                journal = move_lines[0].journal_id
                payment_move_lines = invoices[0].payment_move_line_ids

                payment_m_line = \
                    reconcile.mapped('reconciled_line_ids').filtered(
                        lambda ml: ml.id in payment_move_lines.ids
                    )
                payment_date = payment_m_line and payment_m_line[0].date or \
                    fields.Date.context_today(self)

                # Sale
                total = sum(move_lines.mapped('debit'))
                is_payment = not undo if total > 0 else undo

                lines_vals = [(0, 0, {
                    'name': _("Payment of Capital"),
                    'partner_id': partner.id,
                    'account_id':
                    category.product_id.property_account_income_id.id,
                    'product_id': category.product_id.id,
                    'invoice_id': invoices[0].id,
                    'debit': total if is_payment else 0,
                    'credit': 0 if is_payment else total,
                    'payment_id': payment_id and payment_id[0].id or False
                }), (0, 0, {
                    'name': _("Payment of Capital"),
                    'partner_id': partner.id,
                    'account_id': category.capital_account_id.id,
                    'product_id': category.product_id.id,
                    'invoice_id': invoices[0].id,
                    'debit': 0 if is_payment else total,
                    'credit': total if is_payment else 0,
                    'payment_id': payment_id and payment_id[0].id or False
                })]

                move_vals = {
                    'name': journal.sequence_id.next_by_id(),
                    'partner_id': partner.id,
                    'ref': ', '.join(invoices.mapped('number')),
                    'line_ids': lines_vals,
                    'journal_id': journal.id,
                    'date': payment_date,
                    'narration': _("Paid Capital"),
                    'payment_id': payment_id and payment_id[0].id or False
                    if is_payment else _("Unpaid Capital"),
                }
                move = move_obj.create(move_vals)
                move.post()

                # auto reconcile
                if line_to_reconcile and not line_to_reconcile[0].reconciled:
                    reconcile.auto_reconcile_payment(
                        move, line_to_reconcile[0])

    @api.multi
    def auto_reconcile_payment(self, move, line_to_reconcile):
        # Auto reconcile when confirm payment

        self.ensure_one()
        line_reconcile = move.line_ids.filtered(
            lambda l: l.account_id.id == line_to_reconcile[0].account_id.id
            and not l.reconciled)
        line_to_reconcile |= line_reconcile

        line_to_reconcile.reconcile()
        return True