Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright (c) 2023, New Indictranstech and contributors
- // For license information, please see license.txt
- frappe.ui.form.on('Budget Tool', {
- onload: function(frm){
- if(!frm.doc.project){
- var prev_route = frappe.get_prev_route();
- if (prev_route[1] === 'Project') {
- frm.set_value("project", prev_route[2]);
- }
- if(prev_route[1] === 'Pradan Budget') {
- frappe.db.get_value(prev_route[1], prev_route[2], 'project').then(r => {
- frm.set_value("project", r.message.project);
- })
- }
- }
- },
- refresh: function(frm) {
- set_filters(frm);
- $('.menu-btn-group').hide();
- frm.disable_save();
- if(!frm.doc.project){
- let $el = cur_frm.fields_dict.project.$wrapper;
- $el.find('input').focus();
- }
- },
- project: function(frm){
- if(frm.doc.project){
- create_budget_if_not_exists(frm, frm.doc.project);
- }
- else {
- frm.clear_custom_buttons();
- $(frm.fields_dict['budget_html'].wrapper).html('');
- frm.set_value('total_fy_maximum', 0);
- frm.set_value('fy_wise_total', 0);
- frm.set_value('fy_wise_balance', 0);
- frm.set_value('total_year_maximum', 0);
- frm.set_value('year_wise_total', 0);
- frm.set_value('year_wise_balance', 0);
- frm.set_value('allocation_status', );
- frm.set_value('pradan_budget', );
- frm.set_value('workflow_state', );
- frm.set_value('has_unsaved_changes', 0);
- frm.set_value('is_amended', 0);
- frm.set_value('budget_owner', );
- refresh_field("budget_html");
- }
- },
- has_unsaved_changes: function(frm){
- make_buttons(frm);
- },
- is_amended: function(frm){
- make_buttons(frm);
- }
- });
- function create_budget_if_not_exists(frm, project){
- frappe.call({
- method:'pradan.pradan.doctype.budget_tool.budget_tool.create_budget_if_not_exists',
- args: {
- 'project': frm.doc.project
- },
- freeze: true,
- freeze_message: __("Loading......"),
- callback: (r) => {
- if(r.message){
- var data = r.message;
- $(frm.fields_dict['budget_html'].wrapper).html(data.html);
- frm.set_value('allocation_status', data.status);
- frm.set_value('pradan_budget', data.budget_id);
- frm.set_value('workflow_state', data.workflow_state);
- frm.set_value('budget_owner', data.budget_owner);
- frm.set_value('has_unsaved_changes', 0);
- frm.set_value('is_amended', 0);
- frm.refresh_fields();
- make_buttons(frm);
- }
- }
- });
- }
- function saveData(frm) {
- var table = document.getElementById("data-table").getElementsByTagName('tbody')[0];
- var data = [];
- for (var i = 0; i < table.rows.length-1; i++) {
- var row = table.rows.item(i).cells;
- var data_row = []
- for (var j = 0; j < row.length-1; j++) {
- var val = row.item(j).innerHTML;
- //Remove html tags from primary columns
- if(j>0 && j<6){
- var div = document.createElement("div");
- div.innerHTML = val;
- var text = div.textContent || div.innerText || "";
- data_row.push(text)
- }
- else {
- data_row.push(val)
- }
- }
- if(data_row[1]){
- data.push(data_row)
- }
- }
- var jsonData = JSON.stringify(data);
- frappe.call({
- method:'pradan.pradan.doctype.budget_tool.budget_tool.save_budget_data',
- args: {
- 'project': frm.doc.project,
- 'data': jsonData
- },
- freeze: true,
- freeze_message: __("Saving..."),
- callback: (r) => {
- if(r.message){
- frappe.msgprint({
- title: __('Notification'),
- indicator: 'green',
- message: __('Data updated successfully')
- });
- frm.set_value('is_amended', 0);
- create_budget_if_not_exists(frm, frm.doc.project);
- }
- }
- });
- }
- function submitData(frm){
- var year_balance_cells = document.querySelectorAll('.year_balance');
- for (var i = 0; i < year_balance_cells.length; i++) {
- let year_balance = parseFloat(year_balance_cells[i].innerHTML) || 0;
- if(year_balance>0){
- frappe.throw('Please ensure that sum of Check is 0')
- }
- }
- frappe.call({
- method:'pradan.pradan.doctype.budget_tool.budget_tool.submit_form',
- args: {
- 'project': frm.doc.project
- },
- freeze: true,
- freeze_message: __("Submiting..."),
- callback: (r) => {
- if(r.message){
- frappe.msgprint({
- title: __('Notification'),
- indicator: 'green',
- message: __('Budget Submitted successfully')
- });
- create_budget_if_not_exists(frm, frm.doc.project);
- }
- else {
- frappe.throw('Error while submitting budget. Please reload and try again!')
- }
- }
- });
- }
- function change_contenteditable_for_tds(is_editable){
- if(is_editable===true){
- $('td.allownumeric.year_max').each(function(idx){
- $('td.allownumeric.year_max')[idx].setAttribute('contenteditable', is_editable);
- });
- $('td.allownumeric.year_input').each(function(idx){
- $('td.allownumeric.year_input')[idx].setAttribute('contenteditable', is_editable);
- });
- $('td.allownumeric.fy_max').each(function(idx){
- $('td.allownumeric.fy_max')[idx].setAttribute('contenteditable', is_editable);
- });
- $('td.allownumeric.fy_input').each(function(idx){
- $('td.allownumeric.fy_input')[idx].setAttribute('contenteditable', is_editable);
- });
- $('td.budget_notes').each(function(idx){
- $('td.budget_notes')[idx].setAttribute('contenteditable', is_editable);
- });
- }
- else {
- $('td').each(function(idx){
- $('td')[idx].setAttribute('contenteditable', is_editable);
- });
- }
- }
- frappe.ui.keys.on("ctrl+s", function(frm) {
- if(cur_frm.doc.project){
- if(cur_frm.doc.allocation_status=='Draft'){
- if(cur_frm.doc.has_unsaved_changes){
- saveData(cur_frm);
- }
- else {
- submitData(cur_frm);
- }
- }
- else {
- if(cur_frm.doc.is_amended){
- saveData(cur_frm);
- }
- }
- }
- else {
- frappe.show_alert({
- message:__('Nothing to save, Please select Project'),
- indicator:'red'
- }, 5);
- }
- });
- function make_buttons(frm){
- if(frm.doc.project){
- set_introductions(frm);
- frm.clear_custom_buttons();
- if(frm.doc.pradan_budget){
- frm.add_custom_button('Open Budget Doc', () => {
- frappe.set_route('Form', 'Pradan Budget', frm.doc.pradan_budget);
- });
- }
- if(frm.doc.allocation_status=='Draft'){
- if(frm.doc.has_unsaved_changes){
- frm.add_custom_button('Save', () => {
- saveData(frm);
- }).addClass('btn-primary saveBtn');
- }
- else {
- frm.add_custom_button('Submit', () => {
- submitData(frm);
- }).addClass('btn-primary saveBtn');
- }
- }
- else {
- if(frm.doc.is_amended){
- change_contenteditable_for_tds(true);
- }
- else {
- change_contenteditable_for_tds(false);
- }
- if(frm.doc.budget_owner && frm.doc.workflow_state=='Approved' && frm.doc.budget_owner == frappe.session.user) {
- frm.add_custom_button('Budget Allocation Preview', () => {
- frappe.set_route('query-report', 'Budget Allocation Preview', { project: frm.doc.project });
- }).addClass('btn-primary saveBtn');
- if(frm.doc.is_amended){
- frm.add_custom_button('Update', () => {
- change_contenteditable_for_tds(false);
- saveData(frm);
- frm.set_value('is_amended', 0);
- }).addClass('btn-primary saveBtn');
- }
- else {
- frm.add_custom_button('Ammend Budget', () => {
- change_contenteditable_for_tds(true);
- frm.set_value('is_amended', 1);
- }).addClass('btn-primary saveBtn');
- }
- }
- else {
- change_contenteditable_for_tds(false);
- }
- }
- }
- }
- function set_introductions(frm){
- cur_frm.set_intro();
- if(frm.doc.workflow_state){
- if(frm.doc.workflow_state=='Draft') {
- frm.set_intro('Budget is in Draft status, Submit to get it reviewed', 'blue');
- }
- if(frm.doc.workflow_state=='Pending') {
- frm.set_intro('Budget is Pending, Awaiting Approval for allocation', 'orange');
- }
- if(frm.doc.workflow_state=='Approved') {
- frm.set_intro('Budget is Approved, Now you can do the allocation.', 'green');
- }
- if(frm.doc.workflow_state=='Rejected') {
- frm.set_intro('Budget is Rejected, Please revise and submit again', 'red');
- }
- }
- }
- function set_filters(frm){
- frm.set_query('project', () => {
- return {
- filters: {
- 'workflow_state': 'Approved'
- }
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement