diff --git a/account_easy_reconcile/__openerp__.py b/account_easy_reconcile/__openerp__.py index 472b977d..e4c7cd55 100755 --- a/account_easy_reconcile/__openerp__.py +++ b/account_easy_reconcile/__openerp__.py @@ -33,6 +33,8 @@ "security/ir.model.access.csv", "res_config_view.xml", ], + "test": ['test/easy_reconcile.yml', + ], 'license': 'AGPL-3', "auto_install": False, 'installable': True, diff --git a/account_easy_reconcile/easy_reconcile.py b/account_easy_reconcile/easy_reconcile.py index 579ed11f..38594603 100644 --- a/account_easy_reconcile/easy_reconcile.py +++ b/account_easy_reconcile/easy_reconcile.py @@ -225,70 +225,65 @@ class AccountEasyReconcile(models.Model): else: new_cr = self.env.cr - uid, context = self.env.uid, self.env.context - with api.Environment.manage(): - self.env = api.Environment(new_cr, uid, context) + try: + all_ml_rec_ids = [] + all_ml_partial_ids = [] - try: - all_ml_rec_ids = [] - all_ml_partial_ids = [] + for method in rec.reconcile_method: + rec_model = self.env[method.name] + auto_rec_id = rec_model.create( + self._prepare_run_transient(method) + ) - for method in rec.reconcile_method: - rec_model = self.env[method.name] - auto_rec_id = rec_model.create( - self._prepare_run_transient(method) - ) + ml_rec_ids, ml_partial_ids = ( + auto_rec_id.automatic_reconcile() + ) - ml_rec_ids, ml_partial_ids = ( - auto_rec_id.automatic_reconcile() - ) + all_ml_rec_ids += ml_rec_ids + all_ml_partial_ids += ml_partial_ids - all_ml_rec_ids += ml_rec_ids - all_ml_partial_ids += ml_partial_ids - - reconcile_ids = find_reconcile_ids( - 'reconcile_id', - all_ml_rec_ids - ) - partial_ids = find_reconcile_ids( - 'reconcile_partial_id', - all_ml_partial_ids - ) - - self.env['easy.reconcile.history'].create( - { - 'easy_reconcile_id': rec.id, - 'date': fields.datetime.now(), - 'reconcile_ids': [ - (4, rid) for rid in reconcile_ids - ], - 'reconcile_partial_ids': [ - (4, rid) for rid in partial_ids - ], - }) - except Exception as e: - # In case of error, we log it in the mail thread, log the - # stack trace and create an empty history line; otherwise, - # the cron will just loop on this reconcile task. - _logger.exception( - "The reconcile task %s had an exception: %s", - rec.name, e.message - ) - message = "There was an error during reconciliation : %s" \ - % e.message - rec.message_post(body=message) - self.env['easy.reconcile.history'].create( - { - 'easy_reconcile_id': rec.id, - 'date': fields.Datetime.now(), - 'reconcile_ids': [], - 'reconcile_partial_ids': [], - } - ) - finally: - if ctx['commit_every']: - new_cr.commit() - new_cr.close() + reconcile_ids = find_reconcile_ids( + 'reconcile_id', + all_ml_rec_ids + ) + partial_ids = find_reconcile_ids( + 'reconcile_partial_id', + all_ml_partial_ids + ) + self.env['easy.reconcile.history'].create( + { + 'easy_reconcile_id': rec.id, + 'date': fields.datetime.now(), + 'reconcile_ids': [ + (4, rid) for rid in reconcile_ids + ], + 'reconcile_partial_ids': [ + (4, rid) for rid in partial_ids + ], + }) + except Exception as e: + # In case of error, we log it in the mail thread, log the + # stack trace and create an empty history line; otherwise, + # the cron will just loop on this reconcile task. + _logger.exception( + "The reconcile task %s had an exception: %s", + rec.name, e.message + ) + message = "There was an error during reconciliation : %s" \ + % e.message + rec.message_post(body=message) + self.env['easy.reconcile.history'].create( + { + 'easy_reconcile_id': rec.id, + 'date': fields.Datetime.now(), + 'reconcile_ids': [], + 'reconcile_partial_ids': [], + } + ) + finally: + if ctx['commit_every']: + new_cr.commit() + new_cr.close() return True diff --git a/account_easy_reconcile/test/easy_reconcile.yml b/account_easy_reconcile/test/easy_reconcile.yml new file mode 100644 index 00000000..4c977b50 --- /dev/null +++ b/account_easy_reconcile/test/easy_reconcile.yml @@ -0,0 +1,116 @@ +- + In order to test Confirm Draft Invoice wizard I create an invoice and confirm it with this wizard +- + !record {model: account.invoice, id: account_invoice_state2}: + account_id: account.a_recv + company_id: base.main_company + currency_id: base.CHF + invoice_line: + - account_id: account.a_sale + name: '[PCSC234] PC Assemble SC234' + price_unit: 1000.0 + quantity: 1.0 + product_id: product.product_product_3 + uos_id: product.product_uom_unit + journal_id: account.bank_journal + partner_id: base.res_partner_12 + reference_type: none +- + I called the "Confirm Draft Invoices" wizard +- + !record {model: account.invoice.confirm, id: account_invoice_confirm_0}: + {} +- + I clicked on Confirm Invoices Button +- + !python {model: account.invoice.confirm}: | + self.invoice_confirm(cr, uid, [ref("account_invoice_confirm_0")], {"lang": 'en_US', + "tz": False, "active_model": "account.invoice", "active_ids": [ref("account_invoice_state2")], + "type": "out_invoice", "active_id": ref("account_invoice_state2"), }) +- + I check that customer invoice state is "Open" +- + !assert {model: account.invoice, id: account_invoice_state2}: + - state == 'open' + + +- + In order to test Bank Statement feature of account I create a bank statement line and confirm it and check it's move created +- + I select the period and journal for the bank statement +- + !python {model: account.bank.statement}: | + import time + journal = self._default_journal_id(cr, uid, {'lang': u'en_US', 'tz': False, 'active_model': 'ir.ui.menu', + 'journal_type': 'bank', 'period_id': time.strftime('%m'), 'active_ids': [ref('account.menu_bank_statement_tree')], 'active_id': ref('account.menu_bank_statement_tree')}) + assert journal, 'Journal has not been selected' +- + I create a bank statement with Opening and Closing balance 0. +- + !record {model: account.bank.statement, id: account_bank_statement_0}: + balance_end_real: 0.0 + balance_start: 0.0 + date: !eval time.strftime('%Y-%m-%d') + journal_id: account.bank_journal +- + I create bank statement line +- + !python {model: account.bank.statement.line}: | + vals = { + 'amount': 1000.0, + 'partner_id': ref('base.res_partner_12'), + 'statement_id': ref('account_bank_statement_0'), + 'name': 'EXT001' + } + + line_id = self.create(cr, uid, vals) + assert line_id, "Account bank statement line has not been created" + +- + We process the reconciliation of the invoice line +- + !python {model: account.bank.statement}: | + line_id = None + invoice = self.pool.get('account.invoice').browse(cr, uid, ref("account_invoice_state2")) + for l in invoice.move_id.line_id: + if l.account_id.id == ref('account.a_recv'): + line_id = l + break + statement = self.browse(cr, uid, ref("account_bank_statement_0")) + for statement_line in statement.line_ids: + self.pool.get('account.bank.statement.line').process_reconciliation( + cr, uid, statement_line.id, + [ + { + 'counterpart_move_line_id': line_id.id, + 'credit': 1000.0, + 'debit': 0.0, + 'name': line_id.name + } + ] + ) +- + We unreconcile previous reconciliation so we can create an easy reconcile record to reconcile invoice +- + !python {model: account.move.line}: | + lines_to_unreconcile = self.search(cr, uid, [('reconcile_ref', '!=', False), ('statement_id', '=', ref("account_bank_statement_0"))]) + self._remove_move_reconcile(cr, uid, lines_to_unreconcile) + +- + We create the easy reconcile record +- + !record {model: account.easy.reconcile, id: account_easy_reconcile_0}: + name: 'easy reconcile 1' + account: account.a_recv + reconcile_method: + - name: 'easy.reconcile.simple.partner' +- + We call the automatic reconcilation method +- + !python {model: account.easy.reconcile}: | + self.run_reconcile(cr, uid, [ref('account_easy_reconcile_0')]) +- + I check that customer invoice state is "Paid" +- + !assert {model: account.invoice, id: account_invoice_state2}: + - state == 'paid' \ No newline at end of file