eiru_invoice_status_change.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. openerp.eiru_invoice_status_change = function(instance, local) {
  2. local.widgetInstance = null;
  3. local.parentInstance = null;
  4. local.PayslipGeneratorWidget = instance.Widget.extend({
  5. id: undefined,
  6. init: function(parent) {
  7. this._super(parent);
  8. },
  9. /**
  10. * [checkState]
  11. */
  12. checkState: function(id) {
  13. var self = this;
  14. self.id = id;
  15. if (id)
  16. self.fectchInitial();
  17. },
  18. /**
  19. * [fectchInitial]
  20. */
  21. fectchInitial: function() {
  22. var self = this;
  23. self.fectchInvoice(self.id).then(function(accountInvoice) {
  24. return accountInvoice;
  25. }).then(function(accountInvoice) {
  26. if (!accountInvoice.length)
  27. return false
  28. return self.updateInvoiceState(accountInvoice.shift());
  29. }).then(function(update){
  30. if (update)
  31. return self.reloadPage()
  32. })
  33. },
  34. /**
  35. * [fectchInvoice]
  36. */
  37. fectchInvoice: function(id) {
  38. var invoice = new instance.web.Model('account.invoice');
  39. var fields = ['id','name','residual', 'state'];
  40. var domain = [['id', '=', id],['state', 'in', ['open', 'paid']]];
  41. return invoice.query(fields).filter(domain).all();
  42. },
  43. /**
  44. * [updateInvoiceState]
  45. */
  46. updateInvoiceState: function(invoice) {
  47. if (invoice.residual <= 0 && invoice.state === 'paid')
  48. return
  49. if (invoice.residual > 0 && invoice.state === 'open')
  50. return
  51. var invoiceUpdate = new instance.web.Model('account.invoice');
  52. return invoiceUpdate.call('update_invoice_state',[invoice.id], {
  53. context: new instance.web.CompoundContext()
  54. })
  55. },
  56. /**
  57. * [reloadPage]
  58. */
  59. reloadPage: function() {
  60. local.parentInstance.reload();
  61. },
  62. });
  63. if (instance.web && instance.web.FormView) {
  64. instance.web.FormView.include({
  65. load_record: function(record) {
  66. this._super.apply(this, arguments);
  67. if (this.model !== 'account.invoice')
  68. return;
  69. local.parentInstance = this;
  70. if (local.widgetInstance) {
  71. local.widgetInstance.checkState(record.id);
  72. return
  73. }
  74. local.widgetInstance = new local.PayslipGeneratorWidget(this);
  75. local.widgetInstance.checkState(record.id);
  76. }
  77. });
  78. }
  79. }