Advertisement
SherinKR

pradan_budget.js

Mar 2nd, 2024
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.86 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('Pradan Budget', {
  5.     refresh: function(frm) {
  6.         frm.disable_save();
  7.         frm.disable_form();
  8.         add_buttons(frm);
  9.     }
  10. });
  11.  
  12. frappe.ui.form.on('Pradan Budget Items', {
  13.     allocate: function(frm, cdt, cdn){
  14.         let child = locals[cdt][cdn];
  15.         if(child.allocated == 0 && frm.doc.docstatus == 1){
  16.             budget_allocation_popup(frm, cdt, cdn);
  17.         }
  18.         else {
  19.             frm.fields_dict.allocations.grid.update_docfield_property('allocate', "hidden", 1);
  20.             frappe.throw('Budget allocation will be possible only after defining availability.')
  21.         }
  22.     }
  23. });
  24.  
  25. function budget_allocation_popup(frm, cdt, cdn){
  26.     var child = locals[cdt][cdn];
  27.     let d = new frappe.ui.Dialog({
  28.         title: 'Team Allocation',
  29.         size: "large",
  30.         fields: [
  31.             {
  32.                 label: "Available Amount",
  33.                 fieldname: "allocated_amount",
  34.                 fieldtype: "Currency",
  35.                 read_only: 1,
  36.                 default: child.available_amount
  37.             },
  38.             {
  39.                 label: "Team wise Budget",
  40.                 fieldname: "team_wise_budget",
  41.                 fieldtype: "Table",
  42.                 reqd: 1,
  43.                 cannot_add_rows: true,
  44.                 in_place_edit: true,
  45.                 fields: [
  46.                     {
  47.                         fieldtype: 'Link',
  48.                         label: 'Project',
  49.                         fieldname: 'project',
  50.                         options: 'Project',
  51.                         in_list_view: 1,
  52.                         reqd: 1,
  53.                         read_only: 1
  54.                     },
  55.                     {
  56.                         fieldtype: 'Link',
  57.                         label: 'Teams',
  58.                         fieldname: 'teams',
  59.                         options: 'Branch',
  60.                         in_list_view: 1,
  61.                         reqd: 1,
  62.                         read_only: 1
  63.                     },
  64.                     {
  65.                         fieldtype: 'Currency',
  66.                         label: 'Allocated Amount',
  67.                         fieldname: 'allocated_amount',
  68.                         in_list_view: 1,
  69.                         reqd: 1,
  70.                         default: 0
  71.                     }
  72.                 ]
  73.             }
  74.         ],
  75.         primary_action_label: 'Allocate',
  76.         primary_action(values) {
  77.             allocate_budget_to_teams(frm, child, values);
  78.             d.hide();
  79.         }
  80.     });
  81.     frappe.call({
  82.         method: 'pradan.pradan.doctype.pradan_budget.pradan_budget.get_team_wise_budget',
  83.         args: {
  84.             project: child.project,
  85.             available_amount: child.available_amount
  86.         },
  87.         freeze: true,
  88.         freeze_message: 'Fetching teams..',
  89.         callback: (r) => {
  90.             if(r.message){
  91.                 let data = r.message;
  92.                 d.fields_dict.team_wise_budget.df.data = data
  93.                 d.fields_dict.team_wise_budget.grid.refresh();
  94.             }
  95.             else {
  96.                 frappe.throw('No team has allocated to this Project')
  97.             }
  98.         },
  99.     });
  100.     d.show();
  101. }
  102.  
  103. function allocate_budget_to_teams(frm, child, values){
  104.     frappe.call({
  105.         method: 'pradan.pradan.doctype.pradan_budget.pradan_budget.allocate_budget_to_teams',
  106.         args: {
  107.             project: child.project,
  108.             sub_budget_item: child.sub_budget_item,
  109.             fiscal_year: child.fiscal_year,
  110.             values: values
  111.         },
  112.         freeze: true,
  113.         freeze_message: 'Allocating to teams..',
  114.         callback: (r) => {
  115.             frm.reload_doc();
  116.         },
  117.     })
  118. }
  119.  
  120. function add_buttons(frm){
  121.     if(!frm.is_new()){
  122.         if(frm.doc.workflow_state != 'Rejected'){
  123.             frm.add_custom_button('Detailed View', () => {
  124.                 frappe.set_route('Form', 'Budget Tool', 'Budget Tool');
  125.             }).addClass('btn-primary');
  126.         }
  127.         if(['Pending', 'Approved'].includes(frm.doc.workflow_state)){
  128.             frm.add_custom_button('Reject with Feedback', () => {
  129.                 let d = new frappe.ui.Dialog({
  130.                     title: 'Rejection Feedback',
  131.                     fields: [
  132.                         {
  133.                             label: 'Feedback',
  134.                             fieldname: 'feedback',
  135.                             fieldtype: 'Small Text',
  136.                             reqd: 1
  137.                         }
  138.                     ],
  139.                     primary_action_label: 'Reject',
  140.                     primary_action(values) {
  141.                         frappe.call({
  142.                             method: 'pradan.pradan.doctype.pradan_budget.pradan_budget.reject_with_feedback',
  143.                             args: {
  144.                                 budget_id: frm.doc.name,
  145.                                 feedback: values.feedback
  146.                             },
  147.                             freeze: true,
  148.                             freeze_message: 'Rejecting with feedback',
  149.                             callback: (r) => {
  150.                                 frm.reload_doc();
  151.                             },
  152.                         });
  153.                         d.hide();
  154.                     }
  155.                 });
  156.                 d.show();
  157.             }).addClass('btn-danger');
  158.         }
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement