You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

84 lines
2.3 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. wn.provide("wn.workflow");
  4. wn.workflow = {
  5. state_fields: {},
  6. workflows: {},
  7. setup: function(doctype) {
  8. var wf = wn.model.get("Workflow", {document_type: doctype});
  9. if(wf.length) {
  10. wn.workflow.workflows[doctype] = wf[0];
  11. wn.workflow.state_fields[doctype] = wf[0].workflow_state_field;
  12. } else {
  13. wn.workflow.state_fields[doctype] = null;
  14. }
  15. },
  16. get_state_fieldname: function(doctype) {
  17. if(wn.workflow.state_fields[doctype]===undefined) {
  18. wn.workflow.setup(doctype);
  19. }
  20. return wn.workflow.state_fields[doctype];
  21. },
  22. get_default_state: function(doctype) {
  23. wn.workflow.setup(doctype);
  24. return wn.model.get("Workflow Document State", {
  25. parent: wn.workflow.workflows[doctype].name,
  26. idx: 1
  27. })[0].state;
  28. },
  29. get_transitions: function(doctype, state) {
  30. wn.workflow.setup(doctype);
  31. return wn.model.get("Workflow Transition", {
  32. parent: wn.workflow.workflows[doctype].name,
  33. state: state
  34. });
  35. },
  36. get_document_state: function(doctype, state) {
  37. wn.workflow.setup(doctype);
  38. return wn.model.get("Workflow Document State", {
  39. parent: wn.workflow.workflows[doctype].name,
  40. state: state
  41. })[0];
  42. },
  43. get_next_state: function(doctype, state, action) {
  44. return wn.model.get("Workflow Transition", {
  45. parent: wn.workflow.workflows[doctype].name,
  46. state: state,
  47. action: action
  48. })[0].next_state;
  49. },
  50. is_read_only: function(doctype, name) {
  51. var state_fieldname = wn.workflow.get_state_fieldname(doctype);
  52. if(state_fieldname) {
  53. if(!locals[doctype][name])
  54. return false;
  55. if(locals[doctype][name].__islocal)
  56. return false;
  57. var state = locals[doctype][name][state_fieldname] ||
  58. wn.workflow.get_default_state(doctype);
  59. var workflow_doc_state = wn.model.get("Workflow Document State",
  60. {
  61. parent: wn.workflow.workflows[doctype].name,
  62. state: state
  63. });
  64. var allow_edit = workflow_doc_state.length ?
  65. workflow_doc_state[0].allow_edit : null;
  66. if(user_roles.indexOf(allow_edit)==-1) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. },
  72. get_update_fields: function(doctype) {
  73. var update_fields = $.unique($.map(wn.model.get("Workflow Document State",
  74. {parent:wn.workflow.workflows[doctype].name}), function(d) {
  75. return d.update_field;
  76. }));
  77. return update_fields;
  78. }
  79. };