summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/account_payment.py
blob: 11c664eb1cac6b44fada4da96a7f1c6c9b9b2360 (plain)
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
from odoo import models, api, fields
from odoo.exceptions import AccessError, UserError, ValidationError
import logging

_logger = logging.getLogger(__name__)


class AccountPayment(models.Model):
    _inherit = 'account.payment'

    invoice_bill_ids = fields.Many2many('account.move', string='Invoice/Bill', help='Masukan invoice atau bill yang ingin di alokasi')
    # lookup_line = fields.One2many('ip.lookup.line', 'ip_lookup_id', string='Lookup Lines', auto_join=True)
    payment_line = fields.One2many('account.payment.line', 'payment_id', string='Payment Lines', auto_join=True)
    total_allocated_amt = fields.Float(string='Total Allocated', help='Total Allocated Amount dari setiap Line', compute='_compute_total_payment_line')
    total_difference = fields.Float(string='Total Difference', help='Total Difference dari setiap Line', compute='_compute_total_payment_line')

    def _compute_total_payment_line(self):
        for payment in self:
            total_allocated_amt = total_difference = 0
            for line in payment.payment_line:
                total_allocated_amt += line.allocated_amt
                total_difference += line.difference
            payment.total_allocated_amt = total_allocated_amt
            payment.total_difference = total_difference

    def generate_account_payment_lines(self):
        for payment in self:
            for invoice in payment.invoice_bill_ids:
                self.env['account.payment.line'].create([{
                    'payment_id': payment.id,
                    'account_move_id': invoice.id,
                    'open_amt': invoice.amount_residual
                }])

    def write(self, vals):
        res = super(AccountPayment, self).write(vals)
        
        for line in self.payment_line:
            line.difference = line.open_amt - line.allocated_amt

        return res
    
    def allocate_invoices(self):
        for payment in self:
            if self.
            for line in payment.payment_line:
                invoice = line.account_move_id
                move_lines = payment.line_ids.filtered(lambda line: line.account_internal_type in ('receivable', 'payable'))
                for line in move_lines:
                    invoice.js_assign_outstanding_line(line.id)
                    _logger.info('Allocated Invoice %s' % invoice.name)

    # def _post(self, soft=True):
    #     # OVERRIDE
    #     # Auto-reconcile the invoice with payments coming from transactions.
    #     # It's useful when you have a "paid" sale order (using a payment transaction) and you invoice it later.
    #     posted = super()._post(soft)

    #     for invoice in posted.filtered(lambda move: move.is_invoice()):
    #         payments = invoice.mapped('transaction_ids.payment_id')
    #         move_lines = payments.line_ids.filtered(lambda line: line.account_internal_type in ('receivable', 'payable') and not line.reconciled)
    #         for line in move_lines:
    #             invoice.js_assign_outstanding_line(line.id)
    #     return posted

class PaymentLine(models.Model):
    _name = 'account.payment.line'
    _description = 'Custom indoteknik untuk multiple allocation payment atau receipt'
    
    payment_id = fields.Many2one('account.payment', string='Payment Reference', required=True, ondelete='cascade', index=True, copy=False)
    # order_id = fields.Many2one('sale.order', string='Order Reference', required=True, ondelete='cascade', index=True, copy=False)
    account_move_id = fields.Many2one('account.move', string='Invoice/Bill', help='Pilih invoice / bill yang akan dialokasi dengan uang masuk atau uang keluar')
    open_amt = fields.Float(string='Open', help='Jumlah open amount dari invoice / bill tersebut')
    allocated_amt = fields.Float(string='Allocated', help='Berapa yang ingin di alokasi untuk invoice / bill tersebut')
    difference = fields.Float(string='Difference', help='Sisa setelah alokasi')

    def _compute_difference(self):
        for record in self:
            if record.open_amt and record.allocated_amt:
                print(record.open_amt-record.allocated_amt)
                record.difference = record.open_amt - record.allocated_amt