summaryrefslogtreecommitdiff
path: root/fixco_custom/models/account_move_line.py
blob: a2a6e029ebfdbf2827c1a33c388081cfa945c699 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from odoo import models, api, fields, _
from odoo.exceptions import AccessError, UserError, ValidationError

class AccountMoveLine(models.Model):
    _inherit = "account.move.line"

    qty_outstanding = fields.Float(string='Qty Outstanding', compute='_compute_qty_outstanding')
    invoice_marketplace = fields.Text("Invoice Mearketplace", compute='_compute_invoice_marketplace')
    faktur_pajak = fields.Char(string='Faktur Pajak', related='move_id.faktur_pajak')

    def action_gl_reconcile(self):
        lines = self  

        journal = self.env['account.journal'].search([
            ('suspense_account_id', '=', lines[0].account_id.id)
        ], limit=1)

        if not journal:
            raise UserError('Journal dengan suspense account ini tidak ditemukan!')

        statement = self.env['account.bank.statement'].create({
            'journal_id': journal.id,
            'name': f'REKONSIL {journal.name} {lines[0].date.strftime("%d-%m-%Y")}',
            'company_id': self.env.company.id,
            'date': lines[0].date,
        })

        widget_vals = []
        st_line_ids = []

        for line in lines:
            amount = line.debit - line.credit  

            st_line = self.env['account.bank.statement.line'].create({
                'statement_id': statement.id,
                'date': line.date or fields.Date.today(),
                'payment_ref': line.name,
                'partner_id': line.partner_id.id,
                'amount': amount,
                'ref': line.name,
            })

            st_line_ids.append(st_line.id)

            widget_vals.append({
                'partner_id': st_line.partner_id.id,
                'counterpart_aml_dicts': [{
                    'counterpart_aml_id': line.id,
                    'debit': abs(amount) if amount < 0 else 0,
                    'credit': abs(amount) if amount > 0 else 0,
                    'name': line.name or '/',
                }],
                'payment_aml_ids': [],
                'new_aml_dicts': [],
                'to_check': False,
            })

        statement.button_post()

        self.env['account.reconciliation.widget'].process_bank_statement_line(
            st_line_ids,
            widget_vals
        )
        # statement.button_validate_or_action()

        return {
            'effect': {
                'fadeout': 'slow',
                'message': 'Statement + Auto Reconcile sukses besar 😎🔥',
                'type': 'rainbow_man',
            }
        }

    @api.depends(
        'move_id',
        'move_id.line_ids.matched_debit_ids',
        'move_id.line_ids.matched_credit_ids',
    )
    def _compute_invoice_marketplace(self):
        for line in self:
            line.invoice_marketplace = False
            move = line.move_id

            if not move:
                continue

            # invoice
            if move.move_type in ('out_invoice', 'in_invoice'):
                line.invoice_marketplace = move.invoice_marketplace
            # kas?/entries
            else:
                line.invoice_marketplace = move.reklas_used_by.invoice_marketplace if move.reklas_used_by else False


    def _compute_qty_outstanding(self):
        for line in self:
            qty_received = line.purchase_line_id.qty_received
            qty_billed = line.purchase_line_id.qty_invoiced
            line.qty_outstanding = qty_received - qty_billed

    @api.onchange('quantity')
    def _onchange_quantity(self):
        for line in self:
            if line.move_id.move_type == 'in_invoice' and line.id.origin:
                if line and line.quantity > line.qty_outstanding and line.qty_outstanding > 0:
                    raise UserError(_("Quantity Tidak Boleh Melebihi Qty Outstanding"))