<commit_before># -*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.exceptions import UserError
from math import fabs
import copy
import odoo.addons.decimal_precision as dp


class TrialBalance(models.Model):
    """科目余额表"""
    _name = "trial.balance"
    _order = 'subject_code'
    _description = u'科目余额表'

    @api.one
    @api.depends('cumulative_occurrence_debit', 'cumulative_occurrence_credit',
                 'ending_balance_debit', 'ending_balance_credit', 'subject_name_id')
    def _get_year_init(self):
        if self.subject_name_id.costs_types in ('in', 'out'):
            self.year_init_debit = self.year_init_credit = 0
            return True
        if self.subject_name_id.balance_directions == 'in':
            # 年初借 = 期末借 - 期末贷 - 本年借 + 本年贷
            self.year_init_debit = self.ending_balance_debit - self.ending_balance_credit - \
                self.cumulative_occurrence_debit + self.cumulative_occurrence_credit
            self.year_init_credit = 0
        else:
            # 年初贷 = 期末贷 - 期末借 - 本年贷 + 本年借
            self.year_init_credit = self.ending_balance_credit - self.ending_balance_debit - \
                self.cumulative_occurrence_credit + self.cumulative_occurrence_debit
            self.year_init_debit = 0

    period_id = fields.Many2one('finance.period', string=u'会计期间')
    subject_code = fields.Char(u'科目编码')
    subject_name = fields.Char(u'科目')
    subject_name_id = fields.Many2one('finance.account', string=u'科目', ondelete='cascade')
    account_type = fields.Selection(string=u'科目类型', selection=[('view', 'View'), ('normal', 'Normal')], related='subject_name_id.account_type')
    level = fields.Integer(string=u'科目级次', related='subject_name_id.level', store=True )
    year_init_debit = fields.Float(u'年初余额(借方)', digits=dp.get_precision(
        'Amount'), default=0, compute=_get_year_init)
    year_init_credit = fields.Float(u'年初余额(贷方)', digits=dp.get_precision(
        'Amount'), default=0, compute=_get_year_init)
    initial_balance_debit = fields.Float(
        u'期初余额(借方)', digits=dp.get_precision('Amount'), default=0)
    initial_balance_credit = fields.Float(
        u'期初余额(贷方)', digits=dp.get_precision('Amount'), default=0)
    current_occurrence_debit = fields.Float(
        u'本期发生额(借方)', digits=dp.get_precision('Amount'), default=0)
    current_occurrence_credit = fields.Float(
        u'本期发生额(贷方)', digits=dp.get_precision('Amount'), default=0)
    ending_balance_debit = fields.Float(
        u'期末余额(借方)', digits=dp.get_precision('Amount'), default=0)
    ending_balance_credit = fields.Float(
        u'期末余额(贷方)', digits=dp.get_precision('Amount'), default=0)
    cumulative_occurrence_debit = fields.Float(
        u'本年累计发生额(借方)', digits=dp.get_precision('Amount'), default=0)
    cumulative_occurrence_credit = fields.Float(
        u'本年累计发生额(贷方)', digits=dp.get_precision('Amount'), default=0)

    def button_change_number(self):
        self.ensure_one()

        view = self.env.ref('finance.change_cumulative_occurrence_wizard_form')

        return {
            'name': u'调整累计数',
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'change.cumulative.occurrence.wizard',
            'views': [(view.id, 'form')],
            'view_id': view.id,
            'target': 'new',
            'context': dict(
                self.env.context,
                active_id = self.id,
                active_ids = [self.id]
            ),
        }

    @api.model
    def check_trial_balance(self, period_id):
        res = {}
        trial_balance_items = self.env['trial.balance'].search([('period_id', '=', period_id.id), ('level', '=', 1)])

        field_list = [
            "total_year_init_debit", "total_year_init_credit", "total_initial_balance_debit",
            "total_initial_balance_credit", "total_current_occurrence_debit", "total_current_occurrence_credit",
            "total_ending_balance_debit", "total_ending_balance_credit", "total_cumulative_occurrence_debit",
            "total_cumulative_occurrence_credit"
        ]
        for field in field_list:
            res.update({field: sum(trial_balance_items.mapped(field[6:]))})

        if period_id == period_id.get_init_period():
            diff = res.get('total_cumulative_occurrence_debit', 0) - res.get('total_cumulative_occurrence_credit', 0)
            if diff != 0:
                raise UserError(u'期间：%s 本年累计发生额借贷不平\n\n差异金额：%s' % (period_id.name, diff))
        else:
            diff = res.get('total_initial_balance_debit', 0) - res.get('total_initial_balance_credit', 0)
            if diff != 0:
                raise UserError(u'期间：%s 期初余额借贷不平\n\n差异金额：%s' % (period_id.name, diff))

            diff = res.get('total_current_occurrence_debit', 0) - res.get('total_current_occurrence_credit', 0)
            if diff != 0:
                raise UserError(u'期间：%s 本期发生额借贷不平\n\n差异金额：%s' % (period_id.name, diff))

        return True


class CheckTrialBalanceWizard(models.TransientModel):
    """ 检查试算平衡

    """

    _name = 'check.trial.balance.wizard'
    _description = u'检查试算平衡'

    @api.model
    def default_get(self, fields):
        res = super(CheckTrialBalanceWizard, self).default_get(fields)
        active_id = self.env.context.get('active_id', False)

        trial_balance_item = self.env['trial.balance'].browse(active_id)
        period_id = trial_balance_item.period_id
        is_init_period = False
        if period_id == period_id.get_init_period():
            is_init_period = True
        trial_balance_items = self.env['trial.balance'].search([('period_id', '=', period_id.id), ('level', '=', 1)])

        field_list = [
            "total_year_init_debit", "total_year_init_credit", "total_initial_balance_debit",
            "total_initial_balance_credit", "total_current_occurrence_debit", "total_current_occurrence_credit",
            "total_ending_balance_debit", "total_ending_balance_credit", "total_cumulative_occurrence_debit",
            "total_cumulative_occurrence_credit"
        ]
        for field in field_list:
            res.update({field: sum(trial_balance_items.mapped(field[6:]))})

        res.update({'period_id': period_id.id, 'is_init_period': is_init_period})

        if is_init_period:<commit_msg>修正 科目余额表 试算平衡计算 金额<commit_after># -*- coding: utf-8 -*-
from odoo import models, fields, api
from odoo.exceptions import UserError
from math import fabs
import copy
import odoo.addons.decimal_precision as dp


class TrialBalance(models.Model):
    """科目余额表"""
    _name = "trial.balance"
    _order = 'subject_code'
    _description = u'科目余额表'

    @api.one
    @api.depends('cumulative_occurrence_debit', 'cumulative_occurrence_credit',
                 'ending_balance_debit', 'ending_balance_credit', 'subject_name_id')
    def _get_year_init(self):
        if self.subject_name_id.costs_types in ('in', 'out'):
            self.year_init_debit = self.year_init_credit = 0
            return True
        if self.subject_name_id.balance_directions == 'in':
            # 年初借 = 期末借 - 期末贷 - 本年借 + 本年贷
            self.year_init_debit = self.ending_balance_debit - self.ending_balance_credit - \
                self.cumulative_occurrence_debit + self.cumulative_occurrence_credit
            self.year_init_credit = 0
        else:
            # 年初贷 = 期末贷 - 期末借 - 本年贷 + 本年借
            self.year_init_credit = self.ending_balance_credit - self.ending_balance_debit - \
                self.cumulative_occurrence_credit + self.cumulative_occurrence_debit
            self.year_init_debit = 0

    period_id = fields.Many2one('finance.period', string=u'会计期间')
    subject_code = fields.Char(u'科目编码')
    subject_name = fields.Char(u'科目')
    subject_name_id = fields.Many2one('finance.account', string=u'科目', ondelete='cascade')
    account_type = fields.Selection(string=u'科目类型', selection=[('view', 'View'), ('normal', 'Normal')], related='subject_name_id.account_type')
    level = fields.Integer(string=u'科目级次', related='subject_name_id.level', store=True )
    year_init_debit = fields.Float(u'年初余额(借方)', digits=dp.get_precision(
        'Amount'), default=0, compute=_get_year_init)
    year_init_credit = fields.Float(u'年初余额(贷方)', digits=dp.get_precision(
        'Amount'), default=0, compute=_get_year_init)
    initial_balance_debit = fields.Float(
        u'期初余额(借方)', digits=dp.get_precision('Amount'), default=0)
    initial_balance_credit = fields.Float(
        u'期初余额(贷方)', digits=dp.get_precision('Amount'), default=0)
    current_occurrence_debit = fields.Float(
        u'本期发生额(借方)', digits=dp.get_precision('Amount'), default=0)
    current_occurrence_credit = fields.Float(
        u'本期发生额(贷方)', digits=dp.get_precision('Amount'), default=0)
    ending_balance_debit = fields.Float(
        u'期末余额(借方)', digits=dp.get_precision('Amount'), default=0)
    ending_balance_credit = fields.Float(
        u'期末余额(贷方)', digits=dp.get_precision('Amount'), default=0)
    cumulative_occurrence_debit = fields.Float(
        u'本年累计发生额(借方)', digits=dp.get_precision('Amount'), default=0)
    cumulative_occurrence_credit = fields.Float(
        u'本年累计发生额(贷方)', digits=dp.get_precision('Amount'), default=0)

    def button_change_number(self):
        self.ensure_one()

        view = self.env.ref('finance.change_cumulative_occurrence_wizard_form')

        return {
            'name': u'调整累计数',
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'change.cumulative.occurrence.wizard',
            'views': [(view.id, 'form')],
            'view_id': view.id,
            'target': 'new',
            'context': dict(
                self.env.context,
                active_id = self.id,
                active_ids = [self.id]
            ),
        }

    @api.model
    def check_trial_balance(self, period_id):
        res = {}
        trial_balance_items = self.env['trial.balance'].search([('period_id', '=', period_id.id), ('level', '=', 1)])

        field_list = [
            "total_year_init_debit", "total_year_init_credit", "total_initial_balance_debit",
            "total_initial_balance_credit", "total_current_occurrence_debit", "total_current_occurrence_credit",
            "total_ending_balance_debit", "total_ending_balance_credit", "total_cumulative_occurrence_debit",
            "total_cumulative_occurrence_credit"
        ]
        for field in field_list:
            res.update({field: sum(trial_balance_items.mapped(field[6:]))})

        if period_id == period_id.get_init_period():
            diff = res.get('total_cumulative_occurrence_debit', 0) - res.get('total_cumulative_occurrence_credit', 0)
            if diff != 0:
                raise UserError(u'期间：%s 本年累计发生额借贷不平\n\n差异金额：%s' % (period_id.name, diff))
        else:
            diff = res.get('total_initial_balance_debit', 0) - res.get('total_initial_balance_credit', 0)
            if diff != 0:
                raise UserError(u'期间：%s 期初余额借贷不平\n\n差异金额：%s' % (period_id.name, diff))

            diff = res.get('total_current_occurrence_debit', 0) - res.get('total_current_occurrence_credit', 0)
            if diff != 0:
                raise UserError(u'期间：%s 本期发生额借贷不平\n\n差异金额：%s' % (period_id.name, diff))

        return True


class CheckTrialBalanceWizard(models.TransientModel):
    """ 检查试算平衡

    """

    _name = 'check.trial.balance.wizard'
    _description = u'检查试算平衡'

    @api.model
    def default_get(self, fields):
        res = super(CheckTrialBalanceWizard, self).default_get(fields)
        active_id = self.env.context.get('active_id', False)

        trial_balance_item = self.env['trial.balance'].browse(active_id)
        period_id = trial_balance_item.period_id
        is_init_period = False
        if period_id == period_id.get_init_period():
            is_init_period = True
        trial_balance_items = self.env['trial.balance'].search([('period_id', '=', period_id.id), ('account_type', '=', 'normal')])

        field_list = [
            "total_year_init_debit", "total_year_init_credit", "total_initial_balance_debit",
            "total_initial_balance_credit", "total_current_occurrence_debit", "total_current_occurrence_credit",
            "total_ending_balance_debit", "total_ending_balance_credit", "total_cumulative_occurrence_debit",
            "total_cumulative_occurrence_credit"
        ]
        for field in field_list:
            res.update({field: sum(trial_balance_items.mapped(field[6:]))})

        res.update({'period_id': period_id.id, 'is_init_period': is_init_period})

        if is_init_period:
{'fraction_non_alphanumeric': 0.12757234092880235, 'fraction_numerical': 0.004494204841125916, 'mean_word_length': 3.1048543689320387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '903854', 'n_tokens_mistral': 4618, 'n_tokens_neox': 4486, 'n_words': 622}