Skip to content

Commit

Permalink
Merge 7f4450a into 6aa19e7
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-dacosta committed Sep 14, 2017
2 parents 6aa19e7 + 7f4450a commit ceabe10
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 12 deletions.
1 change: 1 addition & 0 deletions account_move_base_import/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"views/account_move_view.xml",
"views/journal_view.xml",
"views/partner_view.xml",
"wizard/autocomplete_move_view.xml",
],
'test': [
'test/partner.yml',
Expand Down
33 changes: 22 additions & 11 deletions account_move_base_import/models/account_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class AccountJournal(models.Model):

launch_import_completion = fields.Boolean(
string="Launch completion after import",
help="Tic that box to automatically launch the completion "
help="Tick that box to automatically launch the completion "
"on each imported file using this journal.")

create_counterpart = fields.Boolean(
Expand All @@ -71,6 +71,11 @@ class AccountJournal(models.Model):
help="Two counterparts will be automatically created : one for "
"the refunds and one for the payments")

autovalidate_completed_move = fields.Boolean(
string="Validate fully completed moves",
help="Tick that box to automatically validate the journal entries "
"after the completion")

def _get_rules(self):
# We need to respect the sequence order
return sorted(self.rule_ids, key=attrgetter('sequence'))
Expand Down Expand Up @@ -118,7 +123,6 @@ def _prepare_counterpart_line(self, move, amount, date):
'account_id': account_id,
'already_completed': True,
'journal_id': self.id,
'company_id': self.company_id.id,
'currency_id': self.currency_id.id,
'company_currency_id': self.company_id.currency_id.id,
'amount_residual': amount,
Expand Down Expand Up @@ -248,6 +252,16 @@ def prepare_move_vals(self, result_row_list, parser):
vals.update(parser.get_move_vals())
return vals

def _get_attachment_datas(self, moves, file_stream, ftype):
attachment_data = {
'name': 'statement file',
'datas': file_stream,
'datas_fname': "%s.%s" % (fields.Date.today(), ftype),
'res_model': 'account.move',
'res_id': moves[0].id
}
return attachment_data

def multi_move_import(self, file_stream, ftype="csv"):
"""Create multiple bank statements from values given by the parser for
the given profile.
Expand All @@ -258,13 +272,19 @@ def multi_move_import(self, file_stream, ftype="csv"):
:return: list: list of ids of the created account.bank.statemênt
"""
filename = self._context.get('file_name', None)
attachment_obj = self.env['ir.attachment']
if filename:
(filename, __) = os.path.splitext(filename)
parser = new_move_parser(self, ftype=ftype, move_ref=filename)
res = self.env['account.move']
for result_row_list in parser.parse(file_stream):
move = self._move_import(parser, file_stream, ftype=ftype)
res |= move
if res:
attachment_vals = self._get_attachment_datas(
res, file_stream,ftype)
if attachment_vals:
attachment_obj.create(attachment_vals)
return res

def _move_import(self, parser, file_stream, ftype="csv"):
Expand All @@ -281,7 +301,6 @@ def _move_import(self, parser, file_stream, ftype="csv"):
"""
move_obj = self.env['account.move']
move_line_obj = self.env['account.move.line']
attachment_obj = self.env['ir.attachment']
result_row_list = parser.result_row_list
# Check all key are present in account.bank.statement.line!!
if not result_row_list:
Expand All @@ -308,14 +327,6 @@ def _move_import(self, parser, file_stream, ftype="csv"):
self._write_extra_move_lines(parser, move)
if self.create_counterpart:
self._create_counterpart(parser, move)
attachment_data = {
'name': 'statement file',
'datas': file_stream,
'datas_fname': "%s.%s" % (fields.Date.today(), ftype),
'res_model': 'account.move',
'res_id': move.id,
}
attachment_obj.create(attachment_data)
# If user ask to launch completion at end of import, do it!
if self.launch_import_completion:
move.button_auto_completion()
Expand Down
5 changes: 4 additions & 1 deletion account_move_base_import/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,5 +414,8 @@ def button_auto_completion(self):
if not compl_lines % 500:
self.env.cr.commit()
msg = u'\n'.join(msg_lines)
self.write_completion_log(msg, compl_lines)
move.write_completion_log(msg, compl_lines)
if journal.autovalidate_completed_move and \
all([l.already_completed for l in move.line_ids]):
move.post()
return True
1 change: 1 addition & 0 deletions account_move_base_import/views/journal_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<field name="launch_import_completion" attrs="{'invisible': ['|',
('used_for_import', '=', False),
('used_for_completion', '=', False)]}"/>
<field name="autovalidate_completed_move" attrs="{'invisible': [('launch_import_completion', '=', False)]}"/>
<field name="last_import_date" readonly="1"/>
<field name="import_type" attrs="{'required': [('used_for_import', '=', True)]}"/>
</group>
Expand Down
1 change: 1 addition & 0 deletions account_move_base_import/wizard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
# © 2014 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
from . import import_statement
from . import account_autocomplete_move
30 changes: 30 additions & 0 deletions account_move_base_import/wizard/account_autocomplete_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from openerp import models, api, _
from openerp.exceptions import UserError


class AutocompleteAccountMove(models.TransientModel):
_name = "autocomplete.account.move"
_description = "Autocomplete Account Move"

@api.multi
def autocomplete_move(self):
context = dict(self._context or {})
moves = self.env['account.move'].browse(context.get('active_ids'))
draft_move_ids = []
journal_error_names = []
for move in moves:
if move.state != 'draft':
draft_move_ids.append(move.id)
if not move.used_for_completion:
if move.journal_id.name not in journal_error_names:
journal_error_names.append(move.journal_id.name)
if draft_move_ids:
raise UserError(
_('Some journal entries are not at draft state : %s'
% (draft_move_ids,)))
if journal_error_names:
raise UserError(
_('Autocompletion is not allowed on journals %s'
% (journal_error_names,)))
moves.button_auto_completion()
return {'type': 'ir.actions.act_window_close'}
42 changes: 42 additions & 0 deletions account_move_base_import/wizard/autocomplete_move_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>

<!--Account Move lines-->
<record id="autocomplete_account_move_view" model="ir.ui.view">
<field name="name">Autocomplete Journal Entries</field>
<field name="model">autocomplete.account.move</field>
<field name="arch" type="xml">
<form string="Post Journal Entries">
<label string="All selected journal entries will be autocompleted"/>
<footer>
<button string="Auto-complete" name="autocomplete_move" type="object" default_focus="1" class="btn-primary"/>
<button string="Cancel" class="btn-default" special="cancel"/>
</footer>
</form>
</field>
</record>

<record id="action_autocomplete_account_move" model="ir.actions.act_window">
<field name="name">Autocomplete Journal Entries</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">autocomplete.account.move</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="autocomplete_account_move_view"/>
<field name="context">{}</field>
<field name="target">new</field>
<field name="help">This wizard will autocomplete all journal entries selected.</field>
</record>

<record model="ir.values" id="autocomplete_account_move_values">
<field name="model_id" ref="account.model_account_move" />
<field name="name">Autocomplete Journal Entries</field>
<field name="key2">client_action_multi</field>
<field name="value" eval="'ir.actions.act_window,' + str(ref('action_autocomplete_account_move'))" />
<field name="key">action</field>
<field name="model">account.move</field>
</record>

</data>
</openerp>
2 changes: 2 additions & 0 deletions account_move_base_import/wizard/import_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def import_statement(self):
action = self.env['ir.actions.act_window'].for_xml_id(*xmlid)
if len(moves) > 1:
action['domain'] = [('id', 'in', moves.ids)]
if journal.autovalidate_completed_move:
action['context'] = {'group_by': 'state'}
else:
ref = self.env.ref('account.view_move_form')
action['views'] = [(ref.id, 'form')]
Expand Down

0 comments on commit ceabe10

Please sign in to comment.