1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4from odoo import api, fields, models, _
5from odoo.exceptions import ValidationError
6
7
8class AccountJournal(models.Model):
9    _inherit = "account.journal"
10
11    # Use for filter import and export type.
12    l10n_in_gstin_partner_id = fields.Many2one('res.partner', string="GSTIN Unit", ondelete="restrict", help="GSTIN related to this journal. If empty then consider as company GSTIN.")
13
14    def name_get(self):
15        """
16            Add GSTIN number in name as suffix so user can easily find the right journal.
17            Used super to ensure nothing is missed.
18        """
19        result = super().name_get()
20        result_dict = dict(result)
21        indian_journals = self.filtered(lambda j: j.company_id.country_id.code == 'IN' and
22            j.l10n_in_gstin_partner_id and j.l10n_in_gstin_partner_id.vat)
23        for journal in indian_journals:
24            name = result_dict[journal.id]
25            name += "- %s" % (journal.l10n_in_gstin_partner_id.vat)
26            result_dict[journal.id] = name
27        return list(result_dict.items())
28
29
30class AccountMoveLine(models.Model):
31    _inherit = "account.move.line"
32
33    @api.depends('move_id.line_ids', 'move_id.line_ids.tax_line_id', 'move_id.line_ids.debit', 'move_id.line_ids.credit')
34    def _compute_tax_base_amount(self):
35        aml = self.filtered(lambda l: l.company_id.country_id.code == 'IN' and l.tax_line_id  and l.product_id)
36        for move_line in aml:
37            base_lines = move_line.move_id.line_ids.filtered(lambda line: move_line.tax_line_id in line.tax_ids and move_line.product_id == line.product_id)
38            move_line.tax_base_amount = abs(sum(base_lines.mapped('balance')))
39        remaining_aml = self - aml
40        if remaining_aml:
41            return super(AccountMoveLine, remaining_aml)._compute_tax_base_amount()
42
43
44class AccountTax(models.Model):
45    _inherit = 'account.tax'
46
47    l10n_in_reverse_charge = fields.Boolean("Reverse charge", help="Tick this if this tax is reverse charge. Only for Indian accounting")
48
49    def get_grouping_key(self, invoice_tax_val):
50        """ Returns a string that will be used to group account.invoice.tax sharing the same properties"""
51        key = super(AccountTax, self).get_grouping_key(invoice_tax_val)
52        if self.company_id.country_id.code == 'IN':
53            key += "-%s-%s"% (invoice_tax_val.get('l10n_in_product_id', False),
54                invoice_tax_val.get('l10n_in_uom_id', False))
55        return key
56