|
|
@ -317,6 +317,10 @@ WHERE |
|
|
|
('company_id', '=', self.company_id.id) |
|
|
|
]) |
|
|
|
|
|
|
|
if self.filter_account_ids and unaffected_earnings_account not in \ |
|
|
|
self.filter_account_ids: |
|
|
|
return True |
|
|
|
|
|
|
|
query_unaffected_earnings_account_ids = """ |
|
|
|
SELECT a.id |
|
|
|
FROM account_account as a |
|
|
@ -332,11 +336,17 @@ WHERE |
|
|
|
SELECT sum(aml.debit) as sum_debit, |
|
|
|
sum(aml.credit) as sum_credit |
|
|
|
FROM account_move_line as aml |
|
|
|
WHERE date >= %(date_from)s |
|
|
|
AND date <= %(date_to)s |
|
|
|
AND company_id = %(company_id)s |
|
|
|
AND account_id IN %(account_ids)s |
|
|
|
INNER JOIN account_move as am |
|
|
|
ON am.id = aml.move_id |
|
|
|
WHERE aml.date >= %(date_from)s |
|
|
|
AND aml.date <= %(date_to)s |
|
|
|
AND aml.company_id = %(company_id)s |
|
|
|
AND aml.account_id IN %(account_ids)s |
|
|
|
""" |
|
|
|
if self.only_posted_moves: |
|
|
|
query_select_period_balances += """ |
|
|
|
AND am.state = 'posted' |
|
|
|
""" |
|
|
|
query_select_period_balances_params = { |
|
|
|
'date_from': self.date_from, |
|
|
|
'date_to': self.date_to, |
|
|
@ -346,8 +356,6 @@ WHERE |
|
|
|
self.env.cr.execute(query_select_period_balances, |
|
|
|
query_select_period_balances_params) |
|
|
|
sum_debit, sum_credit = self.env.cr.fetchone() |
|
|
|
unaffected_earnings_account_name = \ |
|
|
|
'%s (*)' % unaffected_earnings_account.name |
|
|
|
query_update_unaffected_earnings_account = """ |
|
|
|
UPDATE report_trial_balance_account |
|
|
|
SET |
|
|
@ -361,10 +369,120 @@ WHERE |
|
|
|
'sum_credit': sum_credit, |
|
|
|
'unaffected_earning_account_id': unaffected_earnings_account.id, |
|
|
|
'unaffected_earnings_account_name': |
|
|
|
unaffected_earnings_account_name |
|
|
|
unaffected_earnings_account.name, |
|
|
|
} |
|
|
|
self.env.cr.execute(query_update_unaffected_earnings_account, |
|
|
|
query_update_unaffected_earnings_account_params) |
|
|
|
# P&L allocated in the current fiscal year. |
|
|
|
date = fields.Datetime.from_string(self.date_from) |
|
|
|
res = self.company_id.compute_fiscalyear_dates(date) |
|
|
|
fy_start_date = res['date_from'] |
|
|
|
# Fetch the initial balance |
|
|
|
query_select_initial_pl_balance = """ |
|
|
|
SELECT |
|
|
|
sum(aml.balance) as sum_balance |
|
|
|
FROM |
|
|
|
account_move_line as aml |
|
|
|
INNER JOIN |
|
|
|
account_move as am |
|
|
|
ON am.id = aml.move_id |
|
|
|
WHERE aml.date >= %(date_from)s |
|
|
|
AND aml.date < %(date_to)s |
|
|
|
AND aml.company_id = %(company_id)s |
|
|
|
AND aml.account_id IN %(account_ids)s |
|
|
|
""" |
|
|
|
if self.only_posted_moves: |
|
|
|
query_select_initial_pl_balance += """ |
|
|
|
AND am.state = 'posted' |
|
|
|
""" |
|
|
|
query_select_initial_pl_balance_params = { |
|
|
|
'date_from': fy_start_date, |
|
|
|
'date_to': self.date_from, |
|
|
|
'company_id': self.company_id.id, |
|
|
|
'account_ids': tuple(pl_account_ids), |
|
|
|
} |
|
|
|
self.env.cr.execute(query_select_initial_pl_balance, |
|
|
|
query_select_initial_pl_balance_params) |
|
|
|
res = self.env.cr.fetchone() |
|
|
|
allocated_pl_initial_balance = res[0] or 0.0 |
|
|
|
# Fetch the period balance |
|
|
|
query_select_period_pl_balance = """ |
|
|
|
SELECT |
|
|
|
sum(aml.debit) as sum_debit, |
|
|
|
sum(aml.credit) as sum_credit |
|
|
|
FROM account_move_line as aml |
|
|
|
INNER JOIN account_move as am |
|
|
|
ON am.id = aml.move_id |
|
|
|
WHERE am.date >= %(date_from)s |
|
|
|
AND aml.date <= %(date_to)s |
|
|
|
AND aml.company_id = %(company_id)s |
|
|
|
AND aml.account_id IN %(account_ids)s |
|
|
|
""" |
|
|
|
if self.only_posted_moves: |
|
|
|
query_select_period_pl_balance += """ |
|
|
|
AND am.state = 'posted' |
|
|
|
""" |
|
|
|
query_select_period_pl_balance_params = { |
|
|
|
'date_from': self.date_from, |
|
|
|
'date_to': self.date_to, |
|
|
|
'company_id': self.company_id.id, |
|
|
|
'account_ids': tuple(pl_account_ids), |
|
|
|
} |
|
|
|
self.env.cr.execute(query_select_period_pl_balance, |
|
|
|
query_select_period_pl_balance_params) |
|
|
|
res = self.env.cr.fetchone() |
|
|
|
allocated_pl_debit = res[0] or 0.0 |
|
|
|
allocated_pl_credit = res[1] or 0.0 |
|
|
|
allocated_pl_period_balance = allocated_pl_credit - allocated_pl_debit |
|
|
|
allocated_pl_final_balance = \ |
|
|
|
allocated_pl_initial_balance + allocated_pl_period_balance |
|
|
|
allocated_pl_initial_balance = allocated_pl_initial_balance * -1 |
|
|
|
query_inject_pl_allocation = """ |
|
|
|
INSERT INTO |
|
|
|
report_trial_balance_account ( |
|
|
|
report_id, |
|
|
|
create_uid, |
|
|
|
create_date, |
|
|
|
account_id, |
|
|
|
code, |
|
|
|
name, |
|
|
|
initial_balance, |
|
|
|
debit, |
|
|
|
credit, |
|
|
|
period_balance, |
|
|
|
final_balance, |
|
|
|
initial_balance_foreign_currency, |
|
|
|
final_balance_foreign_currency) |
|
|
|
VALUES ( |
|
|
|
%(report_id)s, |
|
|
|
%(create_uid)s, |
|
|
|
NOW(), |
|
|
|
%(account_id)s, |
|
|
|
%(code)s, |
|
|
|
%(name)s, |
|
|
|
%(initial_balance)s, |
|
|
|
%(debit)s, |
|
|
|
%(credit)s, |
|
|
|
%(period_balance)s, |
|
|
|
%(final_balance)s, |
|
|
|
0.0, |
|
|
|
0.0 |
|
|
|
) |
|
|
|
""" |
|
|
|
query_inject_pl_allocation_params = { |
|
|
|
'report_id': self.id, |
|
|
|
'create_uid': self.env.uid, |
|
|
|
'account_id': unaffected_earnings_account.id, |
|
|
|
'code': unaffected_earnings_account.code, |
|
|
|
'name': '%s (*)' % unaffected_earnings_account.name, |
|
|
|
'initial_balance': allocated_pl_initial_balance, |
|
|
|
'debit': allocated_pl_credit, |
|
|
|
'credit': allocated_pl_debit, |
|
|
|
'period_balance': allocated_pl_period_balance, |
|
|
|
'final_balance': allocated_pl_final_balance |
|
|
|
} |
|
|
|
self.env.cr.execute(query_inject_pl_allocation, |
|
|
|
query_inject_pl_allocation_params) |
|
|
|
|
|
|
|
def _inject_partner_values(self): |
|
|
|
"""Inject report values for report_trial_balance_partner""" |
|
|
|