idg_region_tax_plan.py 7.02 KB
import logging
from datetime import datetime

from odoo import fields, models, api

_logger = logging.getLogger(__name__)


class IdgRegionTaxPlan(models.Model):
    _name = 'idg.region.tax.plan'
    _inherit = 'portal.mixin'
    _description = 'Region Tax Planning'
    country_id = fields.Many2one('res.country', required=True)
    state_id = fields.Many2one('res.country.state', required=True,
                               domain="[('country_id', '=?', country_id)]")
    district_id = fields.Many2one('res.district', required=False,
                                  domain="[('state_id', '=?', state_id)]")
    account_id = fields.Many2one('account.account', required=True)
    year = fields.Integer(required=True, default=datetime.now().year)
    month = fields.Integer(required=True, default=datetime.now().month)
    qty = fields.Integer(required=True, default=0)
    amount = fields.Integer(required=True, default=0)

    @api.depends('accountd_id')
    def update_group(self):
        for r in self:
            query = """
                SELECT id FROM account_group agroup
                WHERE agroup.code_prefix_start <= LEFT('{code}', char_length(agroup.code_prefix_start))
                   AND agroup.code_prefix_end >= LEFT('{code}', char_length(agroup.code_prefix_end))
            """.format(code=r.account_id.code)
            self.env.cr.execute(query)
            group_ids = self.env.cr.fetchall()

            for group_id in group_ids:
                rows = self.env['idg.region.tax.plan.sum']. \
                    search([("country_id", '=', r.country_id.id),
                            ("state_id", '=', r.state_id.id),
                            ("district_id", '=', r.district_id.id),
                            ("account_group_id", '=', group_id[0]),
                            ("year", "=", r.year),
                            ("month", "=", r.month)])
                if not rows:
                    value = {
                        "country_id": r.country_id.id,
                        "state_id": r.state_id.id,
                        "district_id": r.district_id.id,
                        "account_group_id": group_id[0],
                        "year": r.year,
                        "month": r.month
                    }
                    self.env["idg.region.tax.plan.sum"]. \
                        create(value)

    def name_get(self):
        result = []
        for record in self:
            result.append((record.id, f"{record.account_id.name} ({record.account_id.code})"))
        return result

    @api.model
    def load(self, fields, data):
        """ Overridden for better performances when importing a list of account
        with opening debit/credit. In that case, the auto-balance is postpone
        until the whole file has been imported.
        """
        # _logger.info(f"F: {fields}, D: {data}")
        new_data = []
        zipped = None
        for dat in data:
            zipped = dict(zip(fields, dat))
            date = datetime.now().date() if 'date' not in zipped else \
                datetime.strptime(zipped['date'], '%Y-%m-%d').date()
            year = date.year if 'year' not in zipped else zipped["year"]
            month = date.month if 'month' not in zipped else zipped["month"]
            month = str(month).zfill(2)
            if 'year' not in zipped:
                dat.append(year)
            if 'month' not in zipped:
                dat.append(month)
            new_data.append(dat)

        if 'year' not in zipped:
            fields.append('year')
        if 'month' not in zipped:
            fields.append('month')
        result = super(IdgRegionTaxPlan, self).load(fields, new_data)
        self.update_group()
        return result

    def create(self, vals):
        res = super(IdgRegionTaxPlan, self).create(vals)
        self.update_group()
        return res

    def write(self, vals):
        res = super(IdgRegionTaxPlan, self).write(vals)
        self.update_group()
        return res

    @api.onchange('district_id')
    def _onchange_district_id(self):
        if self.district_id and self.district_id.state_id != self.state_id:
            self.state_id = self.district_id.state_id

    @api.onchange('state_id')
    def _onchange_state_id(self):
        if not self.state_id or self.state_id != self.district_id.state_id:
            self.district_id = False

    @api.onchange('country_id')
    def _onchange_country_id(self):
        if not self.country_id or self.country_id != self.state_id.country_id:
            self.state_id = False


class IdgRegionTaxPlanSum(models.Model):
    _name = 'idg.region.tax.plan.sum'
    _inherit = 'portal.mixin'
    _description = 'Summary Region Tax Planning'
    country_id = fields.Many2one('res.country', required=True)
    state_id = fields.Many2one('res.country.state', required=True,
                               domain="[('country_id', '=?', country_id)]")
    district_id = fields.Many2one('res.district', required=False,
                                  domain="[('state_id', '=?', state_id)]")
    account_group_id = fields.Many2one('account.group', required=True)
    year = fields.Integer(required=True, default=datetime.now().year)
    month = fields.Integer(required=True, default=datetime.now().month)
    qty = fields.Integer(compute='_compute_summary', store=True)
    amount = fields.Integer(compute="_compute_summary", store=True)
    level = fields.Integer(compute="_compute_level", store=True)

    def name_get(self):
        result = []
        for record in self:
            result.append((record.id, f"{record.account_group_id.name}"))
        return result

    @api.depends('account_group_id')
    def _compute_level(self):
        for r in self:
            r.level = len(r.account_group_id.code_prefix_start)

    @api.depends('account_group_id')
    def _compute_summary(self):
        for r in self:
            code_prefix_start = r.account_group_id.code_prefix_start
            code_prefix_end = r.account_group_id.code_prefix_end
            if r.district_id:
                district = f"AND idg_rtp.district_id=  {r.district_id.id}"
            else:
                district = "AND idg_rtp.district_id is null"

            query = f"""
            SELECT SUM(idg_rtp.qty) as qty, SUM(idg_rtp.amount) as amount
            FROM idg_region_tax_plan idg_rtp
            JOIN account_account aa on idg_rtp.account_id = aa.id
            WHERE idg_rtp.country_id = {r.country_id.id}
                AND idg_rtp.state_id = {r.state_id.id}
                {district}
                AND '{code_prefix_start}' <= LEFT(aa.code, char_length('{code_prefix_start}'))
                AND '{code_prefix_end}' >= LEFT(aa.code, char_length('{code_prefix_end}'))
                AND  year = {r.year}
                AND  month = {r.month}
            """

            self.env.cr.execute(query)
            row = self.env.cr.dictfetchone()
            r.qty = row['qty']
            r.amount = row['amount']