@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
from odoo import api , fields , models , _
from odoo . exceptions import UserError , ValidationError
from odoo . exceptions import UserError , ValidationError , RedirectWarning
import logging
_logger = logging . getLogger ( __name__ )
class AccountCashboxLine ( models . Model ) :
@ -83,33 +85,19 @@ class AccountBankStmtCashWizard(models.Model):
def _check_closing ( self ) :
self . ensure_one ( )
context = dict ( self . env . context or { } )
stmt = self . end_bank_stmt_ids
difference = stmt . currency_id . compare_amounts ( stmt . balance_end_real , stmt . balance_end )
return self . env [ ' ir.actions.act_window ' ] . _for_xml_id ( ' gn_cash.action_view_account_bnk_stmt_check ' )
stmt = self . end_bank_stmt_ids [ 0 ]
difference = stmt . currency_id . round ( stmt . balance_end_real - stmt . balance_end )
if difference != 0 :
return difference
def _validate_cashbox ( self ) :
for cashbox in self :
context = dict ( self . env . context or { } )
if cashbox . start_bank_stmt_ids :
cashbox . start_bank_stmt_ids . write ( { ' balance_start ' : cashbox . total } )
cashbox . start_bank_stmt_ids [ 0 ] . write ( { ' balance_start ' : cashbox . total } )
if cashbox . end_bank_stmt_ids :
cashbox . end_bank_stmt_ids . write ( { ' balance_end_real ' : cashbox . total } )
if cashbox . end_bank_stmt_ids . balance_end_real != cashbox . end_bank_stmt_ids . balance_end :
cashbox . _check_closing ( context )
# action = {
# 'name': _('Cash Difference'),
# 'view_mode': 'form',
# 'res_model': 'gn_cash.account.bank.statement.closebalance',
# 'view_id': self.env.ref('gn_cash.action_view_account_bnk_stmt_check').id,
# 'type': 'ir.actions.act_window',
# 'res_id': cashbox_id,
# 'context': context,
# 'target': 'new'
# }
# return action
#return self.env['ir.actions.act_window']._for_xml_id('gn_cash.action_view_account_bnk_stmt_check')
cashbox . end_bank_stmt_ids [ 0 ] . write ( { ' balance_end_real ' : cashbox . total } )
class AccountBankStmtCloseCheck ( models . TransientModel ) :
"""
@ -118,19 +106,16 @@ class AccountBankStmtCloseCheck(models.TransientModel):
_name = ' gn_cash.account.bank.statement.closebalance '
_description = ' Bank Statement Closing Balance '
def validate ( self ) :
_logger . warning ( " context in gn_cash.account.bank.statement.closebalance: %s " , dict ( self . env . context ) or { } )
bnk_stmt_id = self . env . context . get ( ' active_id ' , False )
stmt = self . env [ ' account.bank.statement ' ] . browse ( bnk_stmt_id )
if not stmt . is_complete and difference > 0 :
difference = stmt . currency_id . compare_amounts ( stmt . balance_end , stmt . balance_end_real )
message = " We found difference + {difference} in the statement computation, compared to your computation. " ,
difference = fields . Char ( default = lambda self : self . env . context . get ( ' difference ' , False ) , store = False , readonly = True )
def action_confirm ( self ) :
context = dict ( self . env . context ) or { }
if context . get ( ' difference ' , False ) :
bnk_stmt_id = context [ ' params ' ] . get ( ' id ' , False )
stmt = self . env [ ' account.bank.statement ' ] . browse ( bnk_stmt_id )
if stmt :
stmt . _create_closing_difference_line_values ( )
return { ' type ' : ' ir.actions.act_window_close ' }
values = self . env [ ' account.bank.statement ' ] . browse ( bnk_stmt_id ) . _check_cash_balance_end_real_same_as_computed ( )
if values :
return self . env [ ' ir.actions.act_window ' ] . _for_xml_id ( ' account.action_view_account_bnk_stmt_check ' )
else :
return { ' type ' : ' ir.actions.act_window_close ' }
class BankStatement ( models . Model ) :
_inherit = ' account.bank.statement '
@ -138,43 +123,45 @@ class BankStatement(models.Model):
cashbox_start_id = fields . Many2one ( ' gn_cash.account.bank.statement.cashbox ' , string = " Starting Cashbox " )
cashbox_end_id = fields . Many2one ( ' gn_cash.account.bank.statement.cashbox ' , string = " Ending Cashbox " )
def _c heck_cash_balance_end_real_same_as_computed ( self ) :
def _c reate_closing_difference_line_values ( self ) :
""" Check the balance_end_real (encoded manually by the user) is equals to the balance_end (computed by odoo).
For a cash statement , if there is a difference , the different is set automatically to a profit / loss account .
"""
for statement in self . filtered ( lambda stmt : stmt . journal_type == ' cash ' ) :
if not statement . currency_id . is_zero ( statement . difference ) :
st_line_vals = {
' statement_id ' : statement . id ,
' journal_id ' : statement . journal_id . id ,
' amount ' : statement . difference ,
' date ' : statement . date ,
}
if statement . currency_id . compare_amounts ( statement . difference , 0.0 ) < 0.0 :
if not statement . journal_id . loss_account_id :
raise UserError ( _ (
" Please go on the %s journal and define a Loss Account. "
" This account will be used to record cash difference. " ,
statement . journal_id . name
) )
st_line_vals [ ' payment_ref ' ] = _ ( " Cash difference observed during the counting (Loss) " )
st_line_vals [ ' counterpart_account_id ' ] = statement . journal_id . loss_account_id . id
else :
# statement.difference > 0.0
if not statement . journal_id . profit_account_id :
raise UserError ( _ (
" Please go on the %s journal and define a Profit Account. "
" This account will be used to record cash difference. " ,
statement . journal_id . name
) )
st_line_vals [ ' payment_ref ' ] = _ ( " Cash difference observed during the counting (Profit) " )
st_line_vals [ ' counterpart_account_id ' ] = statement . journal_id . profit_account_id . id
self . env [ ' account.bank.statement.line ' ] . create ( st_line_vals )
return True
for statement in self . filtered ( lambda stmt : stmt . journal_id . type == ' cash ' ) :
if not statement . is_complete :
difference = statement . currency_id . round ( statement . balance_end_real - statement . balance_end )
if difference != 0 :
st_line_vals = {
' statement_id ' : statement . id ,
' journal_id ' : statement . journal_id . id ,
' amount ' : difference ,
' date ' : statement . date ,
}
if difference < 0 :
if not statement . journal_id . loss_account_id :
raise UserError ( _ (
" Please go on the %s journal and define a Loss Account. "
" This account will be used to record cash difference. " ,
statement . journal_id . name
) )
st_line_vals [ ' payment_ref ' ] = _ ( " Cash difference observed during the counting (Loss) " )
st_line_vals [ ' counterpart_account_id ' ] = statement . journal_id . loss_account_id . id
else :
# statement.difference > 0.0
if not statement . journal_id . profit_account_id :
raise UserError ( _ (
" Please go on the %s journal and define a Profit Account. "
" This account will be used to record cash difference. " ,
statement . journal_id . name
) )
st_line_vals [ ' payment_ref ' ] = _ ( " Cash difference observed during the counting (Profit) " )
st_line_vals [ ' counterpart_account_id ' ] = statement . journal_id . profit_account_id . id
self . env [ ' account.bank.statement.line ' ] . create ( st_line_vals )
def open_cashbox_id ( self ) :
self . ensure_one ( )
@ -199,4 +186,17 @@ class BankStatement(models.Model):
' target ' : ' new '
}
return action
return action
def button_validate ( self ) :
for statement in self :
if not statement . is_complete :
action = self . env [ ' ir.actions.act_window ' ] . _for_xml_id ( ' gn_cash.action_view_account_bnk_stmt_check ' )
difference = statement . cashbox_end_id . _check_closing ( )
difference = statement . currency_id . format ( difference )
if difference :
context = dict ( self . env . context or { } )
context [ ' difference ' ] = difference
action [ ' context ' ] = context
_logger . warning ( " Context: %s " , context )
return action