|
@@ -0,0 +1,90 @@
|
|
|
+openerp.eiru_invoice_status_change = function(instance, local) {
|
|
|
+
|
|
|
+ local.widgetInstance = null;
|
|
|
+ local.parentInstance = null;
|
|
|
+
|
|
|
+ local.PayslipGeneratorWidget = instance.Widget.extend({
|
|
|
+ id: undefined,
|
|
|
+
|
|
|
+ init: function(parent) {
|
|
|
+ this._super(parent);
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * [checkState]
|
|
|
+ */
|
|
|
+ checkState: function(id) {
|
|
|
+ var self = this;
|
|
|
+ self.id = id;
|
|
|
+
|
|
|
+ if (id)
|
|
|
+ self.fectchInitial();
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * [fectchInitial]
|
|
|
+ */
|
|
|
+ fectchInitial: function() {
|
|
|
+ var self = this;
|
|
|
+ self.fectchInvoice(self.id).then(function(accountInvoice) {
|
|
|
+ return accountInvoice;
|
|
|
+ }).then(function(accountInvoice) {
|
|
|
+ if (!accountInvoice.length)
|
|
|
+ return false
|
|
|
+ return self.updateInvoiceState(accountInvoice.shift());
|
|
|
+ }).then(function(update){
|
|
|
+ if (update)
|
|
|
+ return self.reloadPage()
|
|
|
+ })
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * [fectchInvoice]
|
|
|
+ */
|
|
|
+ fectchInvoice: function(id) {
|
|
|
+ var invoice = new instance.web.Model('account.invoice');
|
|
|
+ var fields = ['id','name','residual', 'state'];
|
|
|
+ var domain = [['id', '=', id],['state', 'in', ['open', 'paid']]];
|
|
|
+ return invoice.query(fields).filter(domain).all();
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * [updateInvoiceState]
|
|
|
+ */
|
|
|
+ updateInvoiceState: function(invoice) {
|
|
|
+ if (invoice.residual <= 0 && invoice.state === 'paid')
|
|
|
+ return
|
|
|
+ if (invoice.residual > 0 && invoice.state === 'open')
|
|
|
+ return
|
|
|
+
|
|
|
+ var invoiceUpdate = new instance.web.Model('account.invoice');
|
|
|
+ return invoiceUpdate.call('update_invoice_state',[invoice.id], {
|
|
|
+ context: new instance.web.CompoundContext()
|
|
|
+ })
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * [reloadPage]
|
|
|
+ */
|
|
|
+ reloadPage: function() {
|
|
|
+ local.parentInstance.reload();
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ if (instance.web && instance.web.FormView) {
|
|
|
+ instance.web.FormView.include({
|
|
|
+ load_record: function(record) {
|
|
|
+ this._super.apply(this, arguments);
|
|
|
+
|
|
|
+ if (this.model !== 'account.invoice')
|
|
|
+ return;
|
|
|
+
|
|
|
+ local.parentInstance = this;
|
|
|
+
|
|
|
+ if (local.widgetInstance) {
|
|
|
+ local.widgetInstance.checkState(record.id);
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ local.widgetInstance = new local.PayslipGeneratorWidget(this);
|
|
|
+ local.widgetInstance.checkState(record.id);
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|