[ADD] account: skip the transient to upload an invoice

Task 2007561
We want to avoid this intermediate screen; Instead, directly open the selection files window...
Done from dashboard button + vendor bill list view & customer invoice list view.
This commit is contained in:
wan
2019-07-19 08:54:03 +00:00
parent e0b16b22e8
commit 4b66aa58b9
4 changed files with 119 additions and 36 deletions
@@ -1,37 +1,86 @@
odoo.define('account.upload.bill.mixin', function (require) {
"use strict";
var core = require('web.core');
var _t = core._t;
var qweb = core.qweb;
var UploadBillMixin = {
start: function () {
// define a unique uploadId and a callback method
this.fileUploadID = _.uniqueId('account_bill_file_upload');
$(window).on(this.fileUploadID, this._onFileUploaded.bind(this));
return this._super.apply(this, arguments);
},
_onAddAttachment: function (ev) {
// Auto submit form once we've selected an attachment
var $input = $(ev.currentTarget).find('input.o_input_file');
if ($input.val() !== '') {
var $binaryForm = this.$('.o_vendor_bill_upload form.o_form_binary_form');
$binaryForm.submit();
}
},
_onFileUploaded: function () {
// Callback once attachment have been created, create a bill with attachment ids
var self = this;
var attachments = Array.prototype.slice.call(arguments, 1);
// Get id from result
var attachent_ids = attachments.reduce(function(filtered, record) {
if (record.id) {
filtered.push(record.id);
}
return filtered;
}, []);
if (!attachent_ids.length) {
return self.do_notify(_t("Error"), _t("An error occurred during the upload"));
}
return this._rpc({
model: 'account.invoice.import.wizard',
method: 'create_invoices',
args: ["", attachent_ids],
context: this.initialState.context,
}).then(function(result) {
self.do_action(result);
});
},
_onUpload: function (event) {
var self = this;
if (!this.initialState.context.journal_type) {
this.initialState.context.journal_type = event.currentTarget.attributes['journal_type'].value;
}
// If hidden upload form don't exists, create it
var $formContainer = this.$('.o_content').find('.o_vendor_bill_upload');
if (!$formContainer.length) {
$formContainer = $(qweb.render('account.BillsHiddenUploadForm', {widget: this}));
$formContainer.appendTo(this.$('.o_content'));
}
// Trigger the input to select a file
this.$('.o_vendor_bill_upload .o_input_file').click();
},
}
return UploadBillMixin;
});
odoo.define('account.bills.tree', function (require) {
"use strict";
var core = require('web.core');
var ListController = require('web.ListController');
var ListView = require('web.ListView');
var UploadBillMixin = require('account.upload.bill.mixin');
var viewRegistry = require('web.view_registry');
var qweb = core.qweb;
var BillsListController = ListController.extend({
var BillsListController = ListController.extend(UploadBillMixin, {
buttons_template: 'BillsListView.buttons',
/**
* Extends the renderButtons function of ListView by adding an event listener
* on the bill upload button.
*
* @override
*/
renderButtons: function () {
this._super.apply(this, arguments); // Possibly sets this.$buttons
if (this.$buttons) {
var self = this;
this.$buttons.on('click', '.o_button_upload_bill', function () {
var state = self.model.get(self.handle, {raw: true});
var context = state.getContext()
self.do_action({
type: 'ir.actions.act_window',
res_model: 'account.invoice.import.wizard',
target: 'new',
views: [[false, 'form']],
context: context,
});
});
}
}
events: _.extend({}, ListController.prototype.events, {
'click .o_button_upload_bill': '_onUpload',
'change .o_vendor_bill_upload .o_form_binary_form': '_onAddAttachment',
}),
});
var BillsListView = ListView.extend({
@@ -42,3 +91,27 @@ odoo.define('account.bills.tree', function (require) {
viewRegistry.add('account_tree', BillsListView);
});
odoo.define('account.dashboard.kanban', function (require) {
"use strict";
var core = require('web.core');
var KanbanController = require('web.KanbanController');
var KanbanView = require('web.KanbanView');
var UploadBillMixin = require('account.upload.bill.mixin');
var viewRegistry = require('web.view_registry');
var DashboardKanbanController = KanbanController.extend(UploadBillMixin, {
events: _.extend({}, KanbanController.prototype.events, {
'click .o_button_upload_bill': '_onUpload',
'change .o_vendor_bill_upload .o_form_binary_form': '_onAddAttachment',
}),
});
var DashboardKanbanView = KanbanView.extend({
config: _.extend({}, KanbanView.prototype.config, {
Controller: DashboardKanbanController,
}),
});
viewRegistry.add('account_dashboard_kanban', DashboardKanbanView);
});
@@ -1,10 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates>
<t t-name="BillsListView.upload_button">
<button type="button" class="btn btn-secondary o_button_upload_bill">
Upload
</button>
<t t-name="account.BillsHiddenUploadForm">
<div class="d-none o_vendor_bill_upload">
<t t-call="HiddenInputFile">
<t t-set="fileupload_id" t-value="widget.fileUploadID"/>
<t t-set="fileupload_action" t-translation="off">/web/binary/upload_attachment</t>
<input type="hidden" name="model" t-att-value="'account.move'"/>
<input type="hidden" name="id" t-att-value="0"/>
</t>
</div>
</t>
<t t-extend="ListView.buttons" t-name="BillsListView.buttons">
@@ -14,7 +19,11 @@
r.push(context.engine.tools.call(context, 'ImportView.import_button', dict));
};
</t>
<t t-call="BillsListView.upload_button"/>
<t t-if="widget.initialState.context.journal_type === 'purchase'">
<button type="button" class="btn btn-secondary o_button_upload_bill">
Upload
</button>
</t>
</t>
</t>
</templates>
@@ -3,7 +3,7 @@
<field name="name">account.journal.dashboard.kanban</field>
<field name="model">account.journal</field>
<field name="arch" type="xml">
<kanban create="false" class="oe_background_grey o_kanban_dashboard o_account_kanban" banner_route="/account/account_dashboard_onboarding">
<kanban create="false" class="oe_background_grey o_kanban_dashboard o_account_kanban" banner_route="/account/account_dashboard_onboarding" js_class="account_dashboard_kanban">
<field name="id"/>
<field name="name"/>
<field name="type"/>
@@ -296,7 +296,7 @@
</button>
</t>
<t t-if="journal_type == 'purchase'">
<button type="action" name="%(account_invoice_import_wizard_action)d" class="btn btn-primary">
<button class="btn btn-primary o_button_upload_bill" journal_type="purchase">
<span>Upload Bills</span>
</button>
</t>
@@ -19,15 +19,16 @@ class ImportInvoiceImportWizard(models.TransientModel):
def _create_invoice(self, attachment):
return self._create_invoice_from_file(attachment)
def create_invoices(self):
def create_invoices(self, attachment_ids=[]):
''' Create the invoices from files.
:return: A action redirecting to account.invoice tree/form view.
'''
if not self.attachment_ids:
attachments = self.attachment_ids or self.env['ir.attachment'].browse(attachment_ids)
if not attachments:
raise UserError(_("No attachment was provided"))
invoices = self.env['account.move']
for attachment in self.attachment_ids:
for attachment in attachments:
invoices += self._create_invoice(attachment)
action_vals = {