Advertisement
SherinKR

budget_tool.js

Mar 2nd, 2024
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 8.04 KB | Source Code | 0 0
  1. // Copyright (c) 2023, New Indictranstech and contributors
  2. // For license information, please see license.txt
  3.  
  4. frappe.ui.form.on('Budget Tool', {
  5.     onload: function(frm){
  6.         if(!frm.doc.project){
  7.             var prev_route = frappe.get_prev_route();
  8.             if (prev_route[1] === 'Project') {
  9.                 frm.set_value("project", prev_route[2]);
  10.             }
  11.             if(prev_route[1] === 'Pradan Budget') {
  12.                 frappe.db.get_value(prev_route[1], prev_route[2], 'project').then(r => {
  13.                     frm.set_value("project", r.message.project);
  14.                 })
  15.             }
  16.         }
  17.     },
  18.     refresh: function(frm) {
  19.         set_filters(frm);
  20.         $('.menu-btn-group').hide();
  21.     frm.disable_save();
  22.         if(!frm.doc.project){
  23.             let $el = cur_frm.fields_dict.project.$wrapper;
  24.             $el.find('input').focus();
  25.         }
  26.     },
  27.     project: function(frm){
  28.         if(frm.doc.project){
  29.             create_budget_if_not_exists(frm, frm.doc.project);
  30.         }
  31.         else {
  32.             frm.clear_custom_buttons();
  33.             $(frm.fields_dict['budget_html'].wrapper).html('');
  34.             frm.set_value('total_fy_maximum', 0);
  35.             frm.set_value('fy_wise_total', 0);
  36.             frm.set_value('fy_wise_balance', 0);
  37.             frm.set_value('total_year_maximum', 0);
  38.             frm.set_value('year_wise_total', 0);
  39.             frm.set_value('year_wise_balance', 0);
  40.             frm.set_value('allocation_status', );
  41.             frm.set_value('pradan_budget', );
  42.             frm.set_value('workflow_state', );
  43.             frm.set_value('has_unsaved_changes', 0);
  44.             frm.set_value('is_amended', 0);
  45.             frm.set_value('budget_owner', );
  46.             refresh_field("budget_html");
  47.         }
  48.     },
  49.     has_unsaved_changes: function(frm){
  50.         make_buttons(frm);
  51.     },
  52.     is_amended: function(frm){
  53.         make_buttons(frm);
  54.     }
  55. });
  56.  
  57. function create_budget_if_not_exists(frm, project){
  58.     frappe.call({
  59.     method:'pradan.pradan.doctype.budget_tool.budget_tool.create_budget_if_not_exists',
  60.     args: {
  61.       'project': frm.doc.project
  62.     },
  63.         freeze: true,
  64.         freeze_message: __("Loading......"),
  65.     callback: (r) => {
  66.       if(r.message){
  67.                 var data = r.message;
  68.                 $(frm.fields_dict['budget_html'].wrapper).html(data.html);
  69.                 frm.set_value('allocation_status', data.status);
  70.                 frm.set_value('pradan_budget', data.budget_id);
  71.                 frm.set_value('workflow_state', data.workflow_state);
  72.                 frm.set_value('budget_owner', data.budget_owner);
  73.                 frm.set_value('has_unsaved_changes', 0);
  74.                 frm.set_value('is_amended', 0);
  75.         frm.refresh_fields();
  76.                 make_buttons(frm);
  77.       }
  78.     }
  79.   });
  80. }
  81.  
  82. function saveData(frm) {
  83.   var table = document.getElementById("data-table").getElementsByTagName('tbody')[0];
  84.   var data = [];
  85.   for (var i = 0; i < table.rows.length-1; i++) {
  86.     var row = table.rows.item(i).cells;
  87.         var data_row = []
  88.         for (var j = 0; j < row.length-1; j++) {
  89.             var val = row.item(j).innerHTML;
  90.             //Remove html tags from primary columns
  91.             if(j>0 && j<6){
  92.                 var div = document.createElement("div");
  93.                 div.innerHTML = val;
  94.                 var text = div.textContent || div.innerText || "";
  95.                 data_row.push(text)
  96.             }
  97.             else {
  98.                 data_row.push(val)
  99.             }
  100.         }
  101.         if(data_row[1]){
  102.             data.push(data_row)
  103.         }
  104.   }
  105.   var jsonData = JSON.stringify(data);
  106.     frappe.call({
  107.     method:'pradan.pradan.doctype.budget_tool.budget_tool.save_budget_data',
  108.     args: {
  109.       'project': frm.doc.project,
  110.             'data': jsonData
  111.     },
  112.         freeze: true,
  113.         freeze_message: __("Saving..."),
  114.     callback: (r) => {
  115.       if(r.message){
  116.                 frappe.msgprint({
  117.                     title: __('Notification'),
  118.                     indicator: 'green',
  119.                     message: __('Data updated successfully')
  120.                 });
  121.                 frm.set_value('is_amended', 0);
  122.                 create_budget_if_not_exists(frm, frm.doc.project);
  123.       }
  124.     }
  125.   });
  126. }
  127.  
  128. function submitData(frm){
  129.     var year_balance_cells = document.querySelectorAll('.year_balance');
  130.     for (var i = 0; i < year_balance_cells.length; i++) {
  131.         let year_balance = parseFloat(year_balance_cells[i].innerHTML) || 0;
  132.         if(year_balance>0){
  133.             frappe.throw('Please ensure that sum of Check is 0')
  134.         }
  135.     }
  136.  
  137.     frappe.call({
  138.     method:'pradan.pradan.doctype.budget_tool.budget_tool.submit_form',
  139.     args: {
  140.       'project': frm.doc.project
  141.     },
  142.         freeze: true,
  143.         freeze_message: __("Submiting..."),
  144.     callback: (r) => {
  145.       if(r.message){
  146.                 frappe.msgprint({
  147.                     title: __('Notification'),
  148.                     indicator: 'green',
  149.                     message: __('Budget Submitted successfully')
  150.                 });
  151.                 create_budget_if_not_exists(frm, frm.doc.project);
  152.       }
  153.             else {
  154.                 frappe.throw('Error while submitting budget. Please reload and try again!')
  155.             }
  156.     }
  157.   });
  158. }
  159.  
  160. function change_contenteditable_for_tds(is_editable){
  161.     if(is_editable===true){
  162.         $('td.allownumeric.year_max').each(function(idx){
  163.             $('td.allownumeric.year_max')[idx].setAttribute('contenteditable', is_editable);
  164.         });
  165.         $('td.allownumeric.year_input').each(function(idx){
  166.             $('td.allownumeric.year_input')[idx].setAttribute('contenteditable', is_editable);
  167.         });
  168.         $('td.allownumeric.fy_max').each(function(idx){
  169.             $('td.allownumeric.fy_max')[idx].setAttribute('contenteditable', is_editable);
  170.         });
  171.         $('td.allownumeric.fy_input').each(function(idx){
  172.             $('td.allownumeric.fy_input')[idx].setAttribute('contenteditable', is_editable);
  173.         });
  174.         $('td.budget_notes').each(function(idx){
  175.             $('td.budget_notes')[idx].setAttribute('contenteditable', is_editable);
  176.         });
  177.     }
  178.     else {
  179.         $('td').each(function(idx){
  180.             $('td')[idx].setAttribute('contenteditable', is_editable);
  181.         });
  182.     }
  183. }
  184.  
  185. frappe.ui.keys.on("ctrl+s", function(frm) {
  186.     if(cur_frm.doc.project){
  187.         if(cur_frm.doc.allocation_status=='Draft'){
  188.             if(cur_frm.doc.has_unsaved_changes){
  189.                 saveData(cur_frm);
  190.             }
  191.             else {
  192.                 submitData(cur_frm);
  193.             }
  194.         }
  195.         else {
  196.             if(cur_frm.doc.is_amended){
  197.                 saveData(cur_frm);
  198.             }
  199.         }
  200.     }
  201.     else {
  202.         frappe.show_alert({
  203.             message:__('Nothing to save, Please select Project'),
  204.             indicator:'red'
  205.         }, 5);
  206.     }
  207. });
  208.  
  209. function make_buttons(frm){
  210.     if(frm.doc.project){
  211.         set_introductions(frm);
  212.         frm.clear_custom_buttons();
  213.         if(frm.doc.pradan_budget){
  214.             frm.add_custom_button('Open Budget Doc', () => {
  215.                 frappe.set_route('Form', 'Pradan Budget', frm.doc.pradan_budget);
  216.             });
  217.         }
  218.         if(frm.doc.allocation_status=='Draft'){
  219.             if(frm.doc.has_unsaved_changes){
  220.                 frm.add_custom_button('Save', () => {
  221.                     saveData(frm);
  222.                 }).addClass('btn-primary saveBtn');
  223.             }
  224.             else {
  225.                 frm.add_custom_button('Submit', () => {
  226.                     submitData(frm);
  227.                 }).addClass('btn-primary saveBtn');
  228.             }
  229.         }
  230.         else {
  231.             if(frm.doc.is_amended){
  232.                 change_contenteditable_for_tds(true);
  233.             }
  234.             else {
  235.                 change_contenteditable_for_tds(false);
  236.             }
  237.             if(frm.doc.budget_owner && frm.doc.workflow_state=='Approved' && frm.doc.budget_owner == frappe.session.user) {
  238.                 frm.add_custom_button('Budget Allocation Preview', () => {
  239.                     frappe.set_route('query-report', 'Budget Allocation Preview', { project: frm.doc.project });
  240.                 }).addClass('btn-primary saveBtn');
  241.  
  242.                 if(frm.doc.is_amended){
  243.                     frm.add_custom_button('Update', () => {
  244.                         change_contenteditable_for_tds(false);
  245.                         saveData(frm);
  246.                         frm.set_value('is_amended', 0);
  247.                     }).addClass('btn-primary saveBtn');
  248.                 }
  249.                 else {
  250.                     frm.add_custom_button('Ammend Budget', () => {
  251.                         change_contenteditable_for_tds(true);
  252.                         frm.set_value('is_amended', 1);
  253.                     }).addClass('btn-primary saveBtn');
  254.                 }
  255.             }
  256.             else {
  257.                 change_contenteditable_for_tds(false);
  258.             }
  259.         }
  260.     }
  261. }
  262.  
  263. function set_introductions(frm){
  264.     cur_frm.set_intro();
  265.     if(frm.doc.workflow_state){
  266.         if(frm.doc.workflow_state=='Draft') {
  267.             frm.set_intro('Budget is in Draft status, Submit to get it reviewed', 'blue');
  268.         }
  269.         if(frm.doc.workflow_state=='Pending') {
  270.             frm.set_intro('Budget is Pending, Awaiting Approval for allocation', 'orange');
  271.         }
  272.         if(frm.doc.workflow_state=='Approved') {
  273.             frm.set_intro('Budget is Approved, Now you can do the allocation.', 'green');
  274.         }
  275.         if(frm.doc.workflow_state=='Rejected') {
  276.             frm.set_intro('Budget is Rejected, Please revise and submit again', 'red');
  277.         }
  278.     }
  279. }
  280.  
  281. function set_filters(frm){
  282.   frm.set_query('project', () => {
  283.         return {
  284.             filters: {
  285.                 'workflow_state': 'Approved'
  286.             }
  287.         }
  288.     });
  289. }
  290.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement