From e071aad62e541e816203dfc80ff0770dd4e6804c Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Wed, 15 Jan 2014 14:58:05 +0100 Subject: [PATCH 1/3] [IMP] Add transfer lines automatically to balance the bank statement --- account_statement_one_move/statement.py | 83 ++++++++++++++++++- account_statement_one_move/statement_view.xml | 1 + 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/account_statement_one_move/statement.py b/account_statement_one_move/statement.py index ef6d0f21..1616fe4a 100644 --- a/account_statement_one_move/statement.py +++ b/account_statement_one_move/statement.py @@ -21,6 +21,8 @@ ############################################################################### from openerp.osv import fields, orm, osv +from openerp.tools.translate import _ + class AccountStatementProfile(orm.Model): @@ -30,6 +32,11 @@ class AccountStatementProfile(orm.Model): 'Group Journal Items', help="Only one Journal Entry will be generated on the " "validation of the bank statement."), + 'split_transfer_line': fields.boolean( + 'Split Transfer Line', + help="Two transfer lines will be automatically generated : one " + "for the refunds and one for the payments.") + } class account_bank_statement(orm.Model): @@ -124,12 +131,86 @@ class account_bank_statement(orm.Model): move_obj.post(cr, uid, [move_id], context=context) return True - + + def create_statement_payment_line(self, cr, uid, st, payment_amount, context=None): + st_line_obj = self.pool.get('account.bank.statement.line') + payment_account_id = st.profile_id.journal_id.default_credit_account_id.id + partner_id = st.profile_id.partner_id and profile.partner_id.id or False + vals = { + 'name': _('Payment Transfer'), + 'date': st.date, + 'amount': -payment_amount, + 'partner_id': partner_id, + 'type': 'general', + 'statement_id': st.id, + 'account_id': payment_account_id, + 'ref': _('Transfer'), + 'already_completed': True + } + payment_line_id = st_line_obj.create(cr, uid, vals, context=context) + return payment_line_id + + + def create_statement_refund_line(self, cr, uid, st, refund_amount, context=None): + st_line_obj = self.pool.get('account.bank.statement.line') + refund_account_id = st.profile_id.journal_id.default_credit_account_id.id + partner_id = st.profile_id.partner_id and profile.partner_id.id or False + vals = { + 'name': _('Refund Transfer'), + 'date': st.date, + 'amount': -refund_amount, + 'partner_id': partner_id, + 'type': 'general', + 'statement_id': st.id, + 'account_id': refund_account_id, + 'ref': _('Transfer'), + 'already_completed': True + } + refund_line_id = st_line_obj.create(cr, uid, vals, context=context) + return refund_line_id + + + def prepare_statement_transfer_lines(self, cr, uid, st, context=None): + refund_amount = 0.0 + payment_amount = 0.0 + transfer_line_ids = [] + #Calculate the part of the refund amount and the payment amount + for st_line in st.line_ids: + if st_line.amount < 0.0: + refund_amount += st_line.amount + else: + payment_amount += st_line.amount + #Create 2 Transfer lines or One global tranfer line + if st.profile_id.split_transfer_line: + transfer_line_ids.append(self.create_statement_refund_line(cr, uid, st, + refund_amount, + context=context)) + transfer_line_ids.append(self.create_statement_payment_line(cr, uid, st, + payment_amount, + context=context)) + else: + global_amount = refund_amount + payment_amount + #The global transfer line can be a refund or a payment transfer + if global_amount < 0.00: + transfer_line_ids.append(self.create_statement_refund_line(cr, uid, st, + global_amount, + context=context)) + else: + transfer_line_ids.append(self.create_statement_payment_line(cr, uid, st, + global_amount, + context=context)) + return transfer_line_ids + + + def button_confirm_bank(self, cr, uid, ids, context=None): st_line_obj = self.pool.get('account.bank.statement.line') if context is None: context = {} for st in self.browse(cr, uid, ids, context=context): + if st.profile_id.one_move: + refund_line_ids = self.prepare_statement_transfer_lines(cr, uid, st, + context=context) super(account_bank_statement, self).button_confirm_bank(cr, uid, ids, context=context) if st.profile_id.one_move: diff --git a/account_statement_one_move/statement_view.xml b/account_statement_one_move/statement_view.xml index 8a145f22..e0e19fc8 100644 --- a/account_statement_one_move/statement_view.xml +++ b/account_statement_one_move/statement_view.xml @@ -17,6 +17,7 @@ + From 07e41d89150e536ea503ae8a73a287b5be24ac59 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Fri, 31 Jan 2014 20:52:16 +0100 Subject: [PATCH 2/3] [FIX] Automatically create transfer move lines instead of bank statement transfer lines --- account_statement_one_move/statement.py | 110 +++++++++++------------- 1 file changed, 48 insertions(+), 62 deletions(-) diff --git a/account_statement_one_move/statement.py b/account_statement_one_move/statement.py index 1616fe4a..af4c3d03 100644 --- a/account_statement_one_move/statement.py +++ b/account_statement_one_move/statement.py @@ -21,8 +21,6 @@ ############################################################################### from openerp.osv import fields, orm, osv -from openerp.tools.translate import _ - class AccountStatementProfile(orm.Model): @@ -38,7 +36,6 @@ class AccountStatementProfile(orm.Model): "for the refunds and one for the payments.") } - class account_bank_statement(orm.Model): _inherit = "account.bank.statement" @@ -132,89 +129,78 @@ class account_bank_statement(orm.Model): return True - def create_statement_payment_line(self, cr, uid, st, payment_amount, context=None): - st_line_obj = self.pool.get('account.bank.statement.line') - payment_account_id = st.profile_id.journal_id.default_credit_account_id.id + def _prepare_transfer_move_line_vals(self, cr, uid, st, name, debit, credit, move_id, context=None): + """ + Prepare the dict of values to create the transfer move lines. + """ + account_id = st.profile_id.journal_id.default_debit_account_id.id partner_id = st.profile_id.partner_id and profile.partner_id.id or False vals = { - 'name': _('Payment Transfer'), + 'name': name, 'date': st.date, - 'amount': -payment_amount, 'partner_id': partner_id, - 'type': 'general', 'statement_id': st.id, - 'account_id': payment_account_id, - 'ref': _('Transfer'), - 'already_completed': True - } - payment_line_id = st_line_obj.create(cr, uid, vals, context=context) - return payment_line_id + 'account_id': account_id, + 'ref': name, + 'move_id': move_id, + 'credit': credit, + 'debit': debit, + 'journal_id': st.journal_id.id, + 'period_id': st.period_id.id, + } + return vals - def create_statement_refund_line(self, cr, uid, st, refund_amount, context=None): - st_line_obj = self.pool.get('account.bank.statement.line') - refund_account_id = st.profile_id.journal_id.default_credit_account_id.id - partner_id = st.profile_id.partner_id and profile.partner_id.id or False - vals = { - 'name': _('Refund Transfer'), - 'date': st.date, - 'amount': -refund_amount, - 'partner_id': partner_id, - 'type': 'general', - 'statement_id': st.id, - 'account_id': refund_account_id, - 'ref': _('Transfer'), - 'already_completed': True - } - refund_line_id = st_line_obj.create(cr, uid, vals, context=context) - return refund_line_id - - - def prepare_statement_transfer_lines(self, cr, uid, st, context=None): - refund_amount = 0.0 - payment_amount = 0.0 + def create_move_transfer_lines(self, cr, uid, move, st, context=None): + move_line_obj = self.pool.get('account.move.line') + move_id = move.id + refund = 0.0 + payment = 0.0 transfer_line_ids = [] #Calculate the part of the refund amount and the payment amount - for st_line in st.line_ids: - if st_line.amount < 0.0: - refund_amount += st_line.amount - else: - payment_amount += st_line.amount + for move_line in move.line_id: + refund += move_line.debit + payment += move_line.credit #Create 2 Transfer lines or One global tranfer line - if st.profile_id.split_transfer_line: - transfer_line_ids.append(self.create_statement_refund_line(cr, uid, st, - refund_amount, - context=context)) - transfer_line_ids.append(self.create_statement_payment_line(cr, uid, st, - payment_amount, - context=context)) + refund_name = 'Refund Transfer' + payment_name = 'Payment Transfer' + if st.profile_id.split_transfer_line and refund != 0.0 and payment != 0.0: + refund_vals = self._prepare_transfer_move_line_vals(cr, uid, st, + refund_name, 0, refund, + move_id, context=context) + transfer_line_ids.append(move_line_obj.create(cr, uid, refund_vals, context=context)) + payment_vals = self._prepare_transfer_move_line_vals(cr, uid, st, + payment_name, payment, 0, + move_id, context=context) + transfer_line_ids.append(move_line_obj.create(cr, uid, payment_vals, context=context)) else: - global_amount = refund_amount + payment_amount #The global transfer line can be a refund or a payment transfer - if global_amount < 0.00: - transfer_line_ids.append(self.create_statement_refund_line(cr, uid, st, - global_amount, - context=context)) + global_amount = abs(payment-refund) + if payment > refund: + vals = self._prepare_transfer_move_line_vals(cr, uid, st, + payment_name, + global_amount, 0, + move_id, context=context) else: - transfer_line_ids.append(self.create_statement_payment_line(cr, uid, st, - global_amount, - context=context)) + vals = self._prepare_transfer_move_line_vals(cr, uid, st, + refund_name, 0, global_amount, + move_id, context=context) + transfer_line_ids.append(move_line_obj.create(cr, uid, vals, context=context)) return transfer_line_ids - - + def button_confirm_bank(self, cr, uid, ids, context=None): st_line_obj = self.pool.get('account.bank.statement.line') + move_obj = self.pool.get('account.move') if context is None: context = {} for st in self.browse(cr, uid, ids, context=context): - if st.profile_id.one_move: - refund_line_ids = self.prepare_statement_transfer_lines(cr, uid, st, - context=context) super(account_bank_statement, self).button_confirm_bank(cr, uid, ids, context=context) if st.profile_id.one_move: move_id = context['move_id'] + move = move_obj.browse(cr, uid, move_id, context=context) + transfe_line_ids = self.create_move_transfer_lines(cr, uid, move, st, context=context) self._valid_move(cr, uid, move_id, context=context) lines_ids = [x.id for x in st.line_ids] st_line_obj.write(cr, uid, lines_ids, From b83af513536181d2a233ba0a410420e4d605c928 Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Fri, 7 Feb 2014 13:45:52 +0100 Subject: [PATCH 3/3] [FIX] simplify code --- account_statement_one_move/statement.py | 47 ++++++++++++------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/account_statement_one_move/statement.py b/account_statement_one_move/statement.py index af4c3d03..687fcbf7 100644 --- a/account_statement_one_move/statement.py +++ b/account_statement_one_move/statement.py @@ -129,12 +129,18 @@ class account_bank_statement(orm.Model): return True - def _prepare_transfer_move_line_vals(self, cr, uid, st, name, debit, credit, move_id, context=None): + def _prepare_transfer_move_line_vals(self, cr, uid, st, name, amount, move_id, context=None): """ Prepare the dict of values to create the transfer move lines. """ account_id = st.profile_id.journal_id.default_debit_account_id.id partner_id = st.profile_id.partner_id and profile.partner_id.id or False + if amount < 0.0: + debit = 0.0 + credit = -amount + else: + debit = amount + credit = 0.0 vals = { 'name': name, 'date': st.date, @@ -156,35 +162,28 @@ class account_bank_statement(orm.Model): move_id = move.id refund = 0.0 payment = 0.0 + transfer_lines = [] transfer_line_ids = [] #Calculate the part of the refund amount and the payment amount for move_line in move.line_id: - refund += move_line.debit + refund -= move_line.debit payment += move_line.credit #Create 2 Transfer lines or One global tranfer line - refund_name = 'Refund Transfer' - payment_name = 'Payment Transfer' - if st.profile_id.split_transfer_line and refund != 0.0 and payment != 0.0: - refund_vals = self._prepare_transfer_move_line_vals(cr, uid, st, - refund_name, 0, refund, - move_id, context=context) - transfer_line_ids.append(move_line_obj.create(cr, uid, refund_vals, context=context)) - payment_vals = self._prepare_transfer_move_line_vals(cr, uid, st, - payment_name, payment, 0, - move_id, context=context) - transfer_line_ids.append(move_line_obj.create(cr, uid, payment_vals, context=context)) + if st.profile_id.split_transfer_line: + if refund: + transfer_lines.append(['Refund Transfer', refund]) + if payment: + transfer_lines.append(['Payment Transfer', payment]) else: - #The global transfer line can be a refund or a payment transfer - global_amount = abs(payment-refund) - if payment > refund: - vals = self._prepare_transfer_move_line_vals(cr, uid, st, - payment_name, - global_amount, 0, - move_id, context=context) - else: - vals = self._prepare_transfer_move_line_vals(cr, uid, st, - refund_name, 0, global_amount, - move_id, context=context) + amount = payment + refund + if amount: + transfer_lines.append(['Transfer', amount]) + for transfer_line in transfer_lines: + vals = self._prepare_transfer_move_line_vals(cr, uid, st, + transfer_line[0], + transfer_line[1], + move_id, + context=context) transfer_line_ids.append(move_line_obj.create(cr, uid, vals, context=context)) return transfer_line_ids