Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en" dir="ltr">
- <head>
- <meta charset="utf-8">
- <title>Pradan Budget</title>
- <script type="text/javascript">
- $(".allownumeric").on("keypress keyup blur", function(event) {
- $(this).val($(this).val().replace(/[^\d].+/, ""));
- if ((event.which < 48 || event.which > 57)) {
- event.preventDefault();
- }
- });
- $(".budget_notes").on("input", function(event) {
- cur_frm.set_value('has_unsaved_changes', 1);
- });
- function calculate_totals(row){
- cur_frm.set_value('has_unsaved_changes', 1);
- var total_row = document.querySelectorAll('.total_row');
- var budget_size = cur_frm.doc.budget_size || 0;
- // Year wise budget validation
- var year_inputs = document.querySelectorAll('.year_input');
- var year_maxs = document.querySelectorAll('.year_max');
- var row_count = year_maxs.length || 0;
- var year_col_count = (year_inputs.length)/row_count
- var year_max = parseFloat(year_maxs[row].innerHTML) || 0;
- var year_total_cells = document.querySelectorAll('.year_total');
- var year_balance_cells = document.querySelectorAll('.year_balance');
- var year_total = 0;
- for (var i = row * year_col_count; i < (row + 1) * year_col_count; i++) {
- var year_value = parseFloat(year_inputs[i].innerHTML) || 0;
- year_total += year_value;
- }
- year_total_cells[row].innerText = year_total;
- if(year_max<year_total){
- frappe.throw('Total Year wise budget exceeded than maximum Budget assigned!');
- }
- else {
- year_balance_cells[row].innerText = year_max - year_total;
- }
- // validate total year max
- var total_year_max = 0
- for(var i = 0; i < year_maxs.length; i++){
- var year_max_val = parseFloat(year_maxs[i].innerHTML) || 0;
- total_year_max += year_max_val
- }
- total_row[0].innerHTML = total_year_max;
- if(budget_size<total_year_max){
- frappe.throw('Total year maximum budget exceeded than Budget Size!');
- }
- // FY wise Budget validation
- var fy_inputs = document.querySelectorAll('.fy_input');
- var fy_maxs = document.querySelectorAll('.fy_max')
- fy_maxs[row].innerHTML = year_max;
- var fy_col_count = (fy_inputs.length)/row_count
- var fy_max = parseFloat(fy_maxs[row].innerHTML) || 0;
- var fy_total_cells = document.querySelectorAll('.fy_total');
- var fy_balance_cells = document.querySelectorAll('.fy_balance');
- var fy_total = 0;
- for (var i = row * fy_col_count; i < (row + 1) * fy_col_count; i++) {
- var fy_value = parseFloat(fy_inputs[i].innerHTML) || 0;
- fy_total += fy_value;
- }
- fy_total_cells[row].innerText = fy_total;
- if(fy_max<fy_total){
- frappe.throw('Total FY wise budget exceeded than maximum Budget assigned!');
- }
- else {
- fy_balance_cells[row].innerText = fy_max - fy_total;
- }
- // validate total year max
- var total_fy_max = 0
- for(var i = 0; i < fy_maxs.length; i++){
- var fy_max_val = parseFloat(fy_maxs[i].innerHTML) || 0;
- total_fy_max += fy_max_val
- }
- total_row[year_col_count+3].innerHTML = total_fy_max;
- if(budget_size<total_fy_max){
- frappe.throw('Total FY maximum budget exceeded than Budget Size!');
- }
- // Set total row
- year_totals = []
- for (var i=0; i<(year_inputs.length/year_col_count); i++) {
- for (var j=0; j<year_col_count; j++) {
- var amount = parseFloat(year_inputs[(i*year_col_count)+j].innerHTML) || 0;
- if(i){
- year_totals[j] += amount;
- total_row[j+1].innerHTML = year_totals[j];
- }
- else {
- year_totals[j] = amount
- }
- }
- }
- var year_total_amount = 0;
- year_total_cells.forEach(row => {
- year_total_amount += parseFloat(row.innerHTML);
- });
- var year_balance_amount = 0;
- year_balance_cells.forEach(row => {
- year_balance_amount += parseFloat(row.innerHTML);
- });
- total_row[year_col_count+1].innerHTML = year_total_amount;
- total_row[year_col_count+2].innerHTML = year_balance_amount;
- fy_totals = []
- for (var i=0; i<(fy_inputs.length/fy_col_count); i++) {
- for (var j=0; j<fy_col_count; j++) {
- var amount = parseFloat(fy_inputs[(i*fy_col_count)+j].innerHTML) || 0;
- if(i){
- fy_totals[j] += amount;
- total_row[year_col_count+4+j].innerHTML = fy_totals[j];
- }
- else {
- fy_totals[j] = amount
- }
- }
- }
- var fy_total_amount = 0;
- fy_total_cells.forEach(row => {
- fy_total_amount += parseFloat(row.innerHTML);
- });
- var fy_balance_amount = 0;
- fy_balance_cells.forEach(row => {
- fy_balance_amount += parseFloat(row.innerHTML);
- });
- total_row[year_col_count+fy_col_count+4].innerHTML = fy_total_amount;
- total_row[year_col_count+fy_col_count+5].innerHTML = fy_balance_amount;
- }
- function allocate_budget(sub_budget_item){
- var project = cur_frm.doc.project;
- frappe.call({
- method:'pradan.pradan.doctype.budget_tool.budget_tool.get_allocation_data',
- args: {
- 'project': project,
- 'sub_budget_item': sub_budget_item
- },
- freeze: true,
- freeze_message: __("Fetching teams.."),
- callback: (r) => {
- if(r.message){
- var data = r.message;
- if(data.fiscal_years.length){
- allocate_budget_popup(project, sub_budget_item, data.fiscal_years, data.available_amount, data.labels);
- }
- }
- }
- });
- }
- function clear_cell_value(cell) {
- if (cell.innerHTML === '0') {
- cell.innerHTML = '';
- }
- }
- function allocate_budget_popup(project, sub_budget_item, fiscal_years, available_amount, labels){
- let d = new frappe.ui.Dialog({
- title: 'Team Allocation',
- size: "large",
- fields: [
- {
- label: "Fiscal Year",
- fieldname: "fiscal_year",
- fieldtype: "Select",
- options: fiscal_years,
- reqd: 1,
- onchange: function(e) {
- var primary_action_label = 'Allocate';
- var selected_fiscal_year = this.value;
- var idx = fiscal_years.indexOf(selected_fiscal_year);
- primary_action_label = labels[idx];
- d.set_value('allocated_amount', available_amount[idx]);
- d.set_primary_action(primary_action_label, (values) => {
- allocate_budget_to_teams(project, sub_budget_item, values.fiscal_year, values);
- d.hide();
- });
- frappe.db.get_value("Budget Item", sub_budget_item, 'parent_budget_item').then((result) => {
- d.set_value('budget_item', result.message.parent_budget_item)
- d.set_value('sub_budget_item', sub_budget_item)
- });
- }
- },
- {
- label: "Budget Item",
- fieldname: "budget_item",
- fieldtype: "Link",
- options: 'Budget Item',
- read_only: 1,
- depends_on: "eval: doc.fiscal_year"
- },
- {
- label: "Sub Budget Item",
- fieldname: "sub_budget_item",
- fieldtype: "Link",
- options: 'Budget Item',
- read_only: 1,
- depends_on: "eval: doc.fiscal_year"
- },
- {
- label: "Available Amount",
- fieldname: "allocated_amount",
- fieldtype: "Currency",
- read_only: 1,
- default: 0,
- depends_on: "eval: doc.fiscal_year",
- onchange: function(e) {
- var allocated_amount = this.value;
- var fiscal_year = d.get_value('fiscal_year');
- frappe.call({
- method: 'pradan.pradan.doctype.pradan_budget.pradan_budget.get_team_wise_budget',
- args: {
- project: project,
- available_amount: allocated_amount,
- sub_budget_item: sub_budget_item,
- fiscal_year: fiscal_year
- },
- freeze: true,
- freeze_message: __("Fetching team details.."),
- callback: (r) => {
- if(r.message){
- let data = r.message.team_wise_budget;
- d.fields_dict.team_wise_budget.df.read_only = r.message.is_salary_head
- d.fields_dict.team_wise_budget.df.fields[2].read_only = r.message.is_salary_head
- d.fields_dict.team_wise_budget.df.data = data
- let available_amount = parseFloat(d.get_value('allocated_amount')) || 0;
- let allocated_amount = 0;
- data.forEach(row => {
- allocated_amount += row.allocated_amount;
- });
- let balance_amount = available_amount - allocated_amount;
- d.set_value('balance_amount', balance_amount);
- d.fields_dict.team_wise_budget.grid.refresh();
- }
- else {
- frappe.throw('No team has allocated to this Project')
- }
- }
- });
- }
- },
- {
- label: "Team wise Budget",
- fieldname: "team_wise_budget",
- fieldtype: "Table",
- reqd: 1,
- cannot_add_rows: true,
- cannot_delete_rows: true,
- cannot_delete_all_rows: true,
- in_place_edit: true,
- depends_on: "eval: doc.fiscal_year",
- fields: [
- {
- fieldtype: 'Link',
- label: 'Project',
- fieldname: 'project',
- options: 'Project',
- in_list_view: 1,
- reqd: 1,
- read_only: 1
- },
- {
- fieldtype: 'Link',
- label: 'Teams',
- fieldname: 'teams',
- options: 'Branch',
- in_list_view: 1,
- reqd: 1,
- read_only: 1
- },
- {
- fieldtype: 'Currency',
- label: 'Allocated Amount',
- fieldname: 'allocated_amount',
- in_list_view: 1,
- reqd: 1,
- default: 0,
- onchange: function(e) {
- var available_amount = parseFloat(d.get_value('allocated_amount')) || 0;
- var table_data = d.get_value('team_wise_budget');
- var allocated_amount = 0;
- table_data.forEach(row => {
- allocated_amount += row.allocated_amount;
- });
- var balance_amount = available_amount - allocated_amount;
- d.set_value('balance_amount', balance_amount);
- // if(available_amount<allocated_amount){
- // frappe.throw('Total allocation should not exceed than the available amount!');
- // }
- }
- },
- {
- fieldtype: 'Data',
- label: 'Remarks',
- fieldname: 'remarks',
- in_list_view: 1
- },
- ]
- },
- {
- label: "Balance Amount",
- fieldname: "balance_amount",
- fieldtype: "Currency",
- default: 0,
- read_only: 1
- }
- ]
- });
- d.show();
- }
- function allocate_budget_to_teams(project, sub_budget_item, fiscal_year, values){
- frappe.call({
- method: 'pradan.pradan.doctype.pradan_budget.pradan_budget.allocate_budget_to_teams',
- args: {
- project: project,
- sub_budget_item: sub_budget_item,
- fiscal_year: fiscal_year,
- values: values
- },
- freeze: true,
- freeze_message: 'Allocating to teams..',
- callback: (r) => {
- cur_frm.trigger('project');
- }
- });
- }
- </script>
- </head>
- <body>
- <div class="tableFixHead">
- <table id="data-table" class="div1">
- <thead>
- <tr>
- <th class="text-center">
- No
- </th>
- {% for column in columns %}
- <th>{{column}}</th>
- {% endfor %}
- <th class="text-center">
- Allocate/<br>Reallocate
- </th>
- </tr>
- </thead>
- <tbody>
- {% set table_row = {'id': 0} %}
- {% for row in data %}
- <tr>
- <td class="text-center">{{loop.index}}</td>
- {% set is_sub_total_row = {'id': 0} %}
- {% for col in row %}
- {% if col.class_name == 'sub_total' %}
- {% if loop.index == 1 %}
- {% if is_sub_total_row.update({'id': 1}) %} {% endif %}
- {% endif %}
- {% endif %}
- {% if col.primary %}
- {% if col.ref_link %}
- <td><a href="{{col.ref_link}}" target="_blank">{{col.value}}</a></td>
- {% else %}
- <td>{{col.value}}</td>
- {% endif %}
- {% else %}
- {% if col.read_only %}
- {% if col.class_name %}
- {% if col.style %}
- <td class="{{ col.class_name }}" style="{{ col.style }}">{{col.value}}</td>
- {% else %}
- <td class="{{ col.class_name }}">{{col.value}}</td>
- {% endif %}
- {% else %}
- <td>{{col.value}}</td>
- {% endif %}
- {% else %}
- {% if col.type == 'text' %}
- {% if col.class_name %}
- <td class="{{ col.class_name }}" contenteditable="true">{{col.value}}</td>
- {% else %}
- <td contenteditable="true">{{col.value}}</td>
- {% endif %}
- {% endif %}
- {% if col.type == 'number' %}
- {% if col.class_name %}
- <td class="allownumeric {{ col.class_name }}" contenteditable="true" oninput="calculate_totals({{table_row.id}})" onclick=clear_cell_value(this) onfocus=clear_cell_value(this) style="background-color: {{col.bg_color}}">{{col.value}}</td>
- {% else %}
- <td class="allownumeric" contenteditable="true" oninput="calculate_totals({{table_row.id}})" onclick=clear_cell_value(this) onfocus=clear_cell_value(this) style="background-color: {{col.bg_color}}">{{col.value}}</td>
- {% endif %}
- {% endif %}
- {% endif %}
- {% endif %}
- {% if col.type == 'button' %}
- <td class="text-center" style="padding-top:5px; padding-bottom:5px;">
- {% if col.allocated %}
- <button class="btn btn-primary btn-xs" type="button" title="{{col.message}}" disabled>Allocate</button>
- {% else %}
- <button class="btn btn-primary btn-xs" type="button" onclick="allocate_budget('{{ row[2].value }}')">Allocate</button>
- {% endif %}
- </td>
- {% endif %}
- {% endfor %}
- </tr>
- {% if is_sub_total_row.id == 0 %}
- {% if table_row.update({'id': table_row.id + 1}) %} {% endif %}
- {% endif %}
- {% endfor %}
- {% if total_row %}
- <tr>
- {% for row in total_row %}
- {% if loop.index <= 5 %}
- {% if loop.index ==1 %}
- <th style="border-right:0 !important;"> {{ row.value }} </th>
- {% elif loop.index ==5 %}
- <th style="border-left:0 !important;"> {{ row.value }} </th>
- {% else %}
- <th style="border-left:0 !important; border-right:0 !important;"> {{ row.value }} </th>
- {% endif %}
- {% else %}
- <th class="text-right total_row"> {{ row.value }} </th>
- {% endif %}
- {% endfor %}
- <th style="border-right:0 !important;"></th>
- <th style="border-left:0 !important;"></th>
- </tr>
- {% endif %}
- </tbody>
- </table>
- <br><br>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement