frappe.provide('erpnext'); $(document).bind('toolbar_setup', function () { frappe.app.name = "ERPNext"; frappe.help_feedback_link = '
'; $('.navbar-home').html('{%= i+1 %}. {%= addr_list[i].address_type!="Other" ? __(addr_list[i].address_type) : addr_list[i].address_title %} {% if(addr_list[i].is_primary_address) { %} ({%= __("Primary") %}){% } %} {% if(addr_list[i].is_shipping_address) { %} ({%= __("Shipping") %}){% } %} {%= __("Edit") %}
{%= addr_list[i].display %}
{%= __("No address added yet.") %}
{% } %} '; frappe.templates['contact_list'] = ' {% for(var i=0, l=contact_list.length; i{%= contact_list[i].first_name %} {%= contact_list[i].last_name %} {% if(contact_list[i].is_primary_contact) { %} ({%= __("Primary") %}) {% } %} {% if(contact_list[i].designation){ %} – {%= contact_list[i].designation %} {% } %} {%= __("Edit") %}
{% if (contact_list[i].phone || contact_list[i].mobile_no || contact_list[i].email_id) { %} {% if(contact_list[i].phone) { %} {%= __("Phone") %}: {%= contact_list[i].phone %}
{% } %} {% if(contact_list[i].mobile_no) { %} {%= __("Mobile No.") %}: {%= contact_list[i].mobile_no %}
{% } %} {% if(contact_list[i].email_id) { %} {%= __("Email Address") %}: {%= contact_list[i].email_id %} {% } %}
{%= __("No contacts added yet.") %}
{% } %}'; frappe.provide("erpnext.stock"); erpnext.stock.StockController = frappe.ui.form.Controller.extend({ onload: function onload() { if (this.frm.fields_dict.company) { this.setup_warehouse_query(); } }, setup_warehouse_query: function setup_warehouse_query() { var me = this; erpnext.queries.setup_queries(this.frm, "Warehouse", function () { return erpnext.queries.warehouse(me.frm.doc); }); }, setup_posting_date_time_check: function setup_posting_date_time_check() { frappe.ui.form.on(this.frm.doctype, 'set_posting_date_and_time_read_only', function (frm) { if (frm.doc.docstatus == 0 && frm.doc.set_posting_time) { frm.set_df_property('posting_date', 'read_only', 0); frm.set_df_property('posting_time', 'read_only', 0); } else { frm.set_df_property('posting_date', 'read_only', 1); frm.set_df_property('posting_time', 'read_only', 1); } }); frappe.ui.form.on(this.frm.doctype, 'set_posting_time', function (frm) { frm.trigger('set_posting_date_and_time_read_only'); }); frappe.ui.form.on(this.frm.doctype, 'refresh', function (frm) { if (frm.doc.docstatus == 0) { if (!frm.doc.posting_date) { frm.set_value('posting_date', frappe.datetime.nowdate()); } if (!frm.doc.posting_time) { frm.set_value('posting_time', frappe.datetime.now_time()); } frm.trigger('set_posting_date_and_time_read_only'); } }); }, show_stock_ledger: function show_stock_ledger() { var me = this; if (this.frm.doc.docstatus === 1) { cur_frm.add_custom_button(__("Stock Ledger"), function () { frappe.route_options = { voucher_no: me.frm.doc.name, from_date: me.frm.doc.posting_date, to_date: me.frm.doc.posting_date, company: me.frm.doc.company }; frappe.set_route("query-report", "Stock Ledger"); }, __("View")); } }, show_general_ledger: function show_general_ledger() { var me = this; if (this.frm.doc.docstatus === 1) { cur_frm.add_custom_button(__('Accounting Ledger'), function () { frappe.route_options = { voucher_no: me.frm.doc.name, from_date: me.frm.doc.posting_date, to_date: me.frm.doc.posting_date, company: me.frm.doc.company, group_by_voucher: false }; frappe.set_route("query-report", "General Ledger"); }, __("View")); } } }); erpnext.payments = erpnext.stock.StockController.extend({ make_payment: function make_payment() { var me = this; this.dialog = new frappe.ui.Dialog({ title: 'Payment' }); this.dialog.show(); this.$body = this.dialog.body; this.set_payment_primary_action(); this.make_keyboard(); this.select_text(); }, select_text: function select_text() { var me = this; $(this.$body).find('.form-control').click(function () { $(this).select(); }); }, set_payment_primary_action: function set_payment_primary_action() { var me = this; this.dialog.set_primary_action(__("Submit"), function () { me.dialog.hide(); me.submit_invoice(); }); }, make_keyboard: function make_keyboard() { var me = this; $(this.$body).empty(); $(this.$body).html(frappe.render_template('pos_payment', this.frm.doc)); this.show_payment_details(); this.bind_keyboard_event(); this.clear_amount(); }, make_multimode_payment: function make_multimode_payment() { var me = this; if (this.frm.doc.change_amount > 0) { me.payment_val = me.doc.outstanding_amount; } this.payments = frappe.model.add_child(this.frm.doc, 'Multi Mode Payment', "payments"); this.payments.mode_of_payment = this.dialog.fields_dict.mode_of_payment.get_value(); this.payments.amount = flt(this.payment_val); }, show_payment_details: function show_payment_details() { var me = this; var multimode_payments = $(this.$body).find('.multimode-payments').empty(); if (this.frm.doc.payments.length) { $.each(this.frm.doc.payments, function (index, data) { $(frappe.render_template('payment_details', { mode_of_payment: data.mode_of_payment, amount: data.amount, idx: data.idx, currency: me.frm.doc.currency, type: data.type })).appendTo(multimode_payments); if (data.type == 'Cash' && data.amount == me.frm.doc.paid_amount) { me.idx = data.idx; me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']", { 'idx': me.idx })); me.highlight_selected_row(); me.bind_amount_change_event(); } }); } else { $("
No payment mode selected in pos profile
").appendTo(multimode_payments); } }, set_outstanding_amount: function set_outstanding_amount() { this.selected_mode = $(this.$body).find(repl("input[idx='%(idx)s']", { 'idx': this.idx })); this.highlight_selected_row(); this.payment_val = 0.0; if (this.frm.doc.outstanding_amount > 0 && flt(this.selected_mode.val()) == 0.0) { this.payment_val = flt(this.frm.doc.outstanding_amount / this.frm.doc.conversion_rate, precision("outstanding_amount")); this.selected_mode.val(format_currency(this.payment_val, this.frm.doc.currency)); this.update_payment_amount(); } else if (flt(this.selected_mode.val()) > 0) { this.payment_val = flt(this.selected_mode.val()); } this.selected_mode.select(); this.bind_amount_change_event(); }, bind_keyboard_event: function bind_keyboard_event() { var me = this; this.payment_val = ''; this.bind_form_control_event(); this.bind_numeric_keys_event(); }, bind_form_control_event: function bind_form_control_event() { var me = this; $(this.$body).find('.pos-payment-row').click(function () { me.idx = $(this).attr("idx"); me.set_outstanding_amount(); }); $(this.$body).find('.form-control').click(function () { me.idx = $(this).attr("idx"); me.set_outstanding_amount(); me.update_paid_amount(true); }); $(this.$body).find('.write_off_amount').change(function () { me.write_off_amount(flt($(this).val()), precision("write_off_amount")); }); $(this.$body).find('.change_amount').change(function () { me.change_amount(flt($(this).val()), precision("change_amount")); }); }, highlight_selected_row: function highlight_selected_row() { var me = this; var selected_row = $(this.$body).find(repl(".pos-payment-row[idx='%(idx)s']", { 'idx': this.idx })); $(this.$body).find('.pos-payment-row').removeClass('selected-payment-mode'); selected_row.addClass('selected-payment-mode'); $(this.$body).find('.amount').attr('disabled', true); this.selected_mode.attr('disabled', false); }, bind_numeric_keys_event: function bind_numeric_keys_event() { var me = this; $(this.$body).find('.pos-keyboard-key').click(function () { me.payment_val += $(this).text(); me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency)); me.idx = me.selected_mode.attr("idx"); me.update_paid_amount(); }); $(this.$body).find('.delete-btn').click(function () { me.payment_val = cstr(flt(me.selected_mode.val())).slice(0, -1); me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency)); me.idx = me.selected_mode.attr("idx"); me.update_paid_amount(); }); }, bind_amount_change_event: function bind_amount_change_event() { var me = this; this.selected_mode.change(function () { me.payment_val = flt($(this).val()) || 0.0; me.selected_mode.val(format_currency(me.payment_val, me.frm.doc.currency)); me.idx = me.selected_mode.attr("idx"); me.update_payment_amount(); }); }, clear_amount: function clear_amount() { var me = this; $(this.$body).find('.clr').click(function (e) { e.stopPropagation(); me.idx = $(this).attr("idx"); me.selected_mode = $(me.$body).find(repl("input[idx='%(idx)s']", { 'idx': me.idx })); me.payment_val = 0.0; me.selected_mode.val(0.0); me.highlight_selected_row(); me.update_payment_amount(); }); }, write_off_amount: function write_off_amount(_write_off_amount) { var me = this; this.frm.doc.write_off_amount = flt(_write_off_amount, precision("write_off_amount")); this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate, precision("base_write_off_amount")); this.calculate_outstanding_amount(false); this.show_amounts(); }, change_amount: function change_amount(_change_amount) { var me = this; this.frm.doc.change_amount = flt(_change_amount, precision("change_amount")); this.calculate_write_off_amount(); this.show_amounts(); }, update_paid_amount: function update_paid_amount(update_write_off) { var me = this; if (in_list(['change_amount', 'write_off_amount'], this.idx)) { var value = me.selected_mode.val(); if (me.idx == 'change_amount') { me.change_amount(value); } else { if (flt(value) == 0 && update_write_off && me.frm.doc.outstanding_amount > 0) { value = flt(me.frm.doc.outstanding_amount / me.frm.doc.conversion_rate, precision(me.idx)); } me.write_off_amount(value); } } else { this.update_payment_amount(); } }, update_payment_amount: function update_payment_amount() { var me = this; $.each(this.frm.doc.payments, function (index, data) { if (cint(me.idx) == cint(data.idx)) { data.amount = flt(me.selected_mode.val(), 2); } }); this.calculate_outstanding_amount(false); this.show_amounts(); }, show_amounts: function show_amounts() { var me = this; $(this.$body).find(".write_off_amount").val(format_currency(this.frm.doc.write_off_amount, this.frm.doc.currency)); $(this.$body).find('.paid_amount').text(format_currency(this.frm.doc.paid_amount, this.frm.doc.currency)); $(this.$body).find('.change_amount').val(format_currency(this.frm.doc.change_amount, this.frm.doc.currency)); $(this.$body).find('.outstanding_amount').text(format_currency(this.frm.doc.outstanding_amount, frappe.get_doc(":Company", this.frm.doc.company).default_currency)); this.update_invoice(); } }); erpnext.taxes_and_totals = erpnext.payments.extend({ setup: function setup() {}, apply_pricing_rule_on_item: function apply_pricing_rule_on_item(item) { if (item.margin_type == "Percentage") { item.rate_with_margin = flt(item.price_list_rate) + flt(item.price_list_rate) * (flt(item.margin_rate_or_amount) / 100); } else { item.rate_with_margin = flt(item.price_list_rate) + flt(item.margin_rate_or_amount); } item.rate = flt(item.rate_with_margin, precision("rate", item)); if (item.discount_percentage) { var discount_value = flt(item.rate_with_margin) * flt(item.discount_percentage) / 100; item.rate = flt(item.rate_with_margin - discount_value, precision('rate', item)); } }, calculate_taxes_and_totals: function calculate_taxes_and_totals(update_paid_amount) { this.discount_amount_applied = false; this._calculate_taxes_and_totals(); this.calculate_discount_amount(); if (in_list(["Sales Invoice", "Purchase Invoice"], this.frm.doc.doctype) && this.frm.doc.docstatus < 2 && !this.frm.doc.is_return) { this.calculate_total_advance(update_paid_amount); } if (in_list(["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"], this.frm.doc.doctype)) { this.calculate_commission(); this.calculate_contribution(); } this.frm.refresh_fields(); }, calculate_discount_amount: function calculate_discount_amount() { if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount")) { this.set_discount_amount(); this.apply_discount_amount(); } }, _calculate_taxes_and_totals: function _calculate_taxes_and_totals() { this.validate_conversion_rate(); this.calculate_item_values(); this.initialize_taxes(); this.determine_exclusive_rate(); this.calculate_net_total(); this.calculate_taxes(); this.manipulate_grand_total_for_inclusive_tax(); this.calculate_totals(); this._cleanup(); }, validate_conversion_rate: function validate_conversion_rate() { this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, cur_frm ? precision("conversion_rate") : 9); var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate", this.frm.doc.name); var company_currency = this.frm.doc.currency || this.get_company_currency(); if (!this.frm.doc.conversion_rate) { if (this.frm.doc.currency == company_currency) { this.frm.set_value("conversion_rate", 1); } else { frappe.throw(repl('%(conversion_rate_label)s' + __(' is mandatory. Maybe Currency Exchange record is not created for ') + '%(from_currency)s' + __(" to ") + '%(to_currency)s', { "conversion_rate_label": conversion_rate_label, "from_currency": this.frm.doc.currency, "to_currency": company_currency })); } } }, calculate_item_values: function calculate_item_values() { var me = this; if (!this.discount_amount_applied) { $.each(this.frm.doc["items"] || [], function (i, item) { frappe.model.round_floats_in(item); item.net_rate = item.rate; item.amount = flt(item.rate * item.qty, precision("amount", item)); item.net_amount = item.amount; item.item_tax_amount = 0.0; me.set_in_company_currency(item, ["price_list_rate", "rate", "amount", "net_rate", "net_amount"]); }); } }, set_in_company_currency: function set_in_company_currency(doc, fields) { var me = this; $.each(fields, function (i, f) { doc["base_" + f] = flt(flt(doc[f], precision(f, doc)) * me.frm.doc.conversion_rate, precision("base_" + f, doc)); }); }, initialize_taxes: function initialize_taxes() { var me = this; $.each(this.frm.doc["taxes"] || [], function (i, tax) { tax.item_wise_tax_detail = {}; var tax_fields = ["total", "tax_amount_after_discount_amount", "tax_amount_for_current_item", "grand_total_for_current_item", "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]; if (cstr(tax.charge_type) != "Actual" && !(me.discount_amount_applied && me.frm.doc.apply_discount_on == "Grand Total")) { tax_fields.push("tax_amount"); } $.each(tax_fields, function (i, fieldname) { tax[fieldname] = 0.0; }); if (!this.discount_amount_applied && cur_frm) { cur_frm.cscript.validate_taxes_and_charges(tax.doctype, tax.name); me.validate_inclusive_tax(tax); } frappe.model.round_floats_in(tax); }); }, determine_exclusive_rate: function determine_exclusive_rate() { var me = this; var has_inclusive_tax = false; $.each(me.frm.doc["taxes"] || [], function (i, row) { if (cint(row.included_in_print_rate)) has_inclusive_tax = true; }); if (has_inclusive_tax == false) return; $.each(me.frm.doc["items"] || [], function (n, item) { var item_tax_map = me._load_item_tax_rate(item.item_tax_rate); var cumulated_tax_fraction = 0.0; $.each(me.frm.doc["taxes"] || [], function (i, tax) { tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map); if (i == 0) { tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item; } else { tax.grand_total_fraction_for_current_item = me.frm.doc["taxes"][i - 1].grand_total_fraction_for_current_item + tax.tax_fraction_for_current_item; } cumulated_tax_fraction += tax.tax_fraction_for_current_item; }); if (cumulated_tax_fraction && !me.discount_amount_applied) { item.net_amount = flt(item.amount / (1 + cumulated_tax_fraction), precision("net_amount", item)); item.net_rate = flt(item.net_amount / item.qty, precision("net_rate", item)); me.set_in_company_currency(item, ["net_rate", "net_amount"]); } }); }, get_current_tax_fraction: function get_current_tax_fraction(tax, item_tax_map) { var current_tax_fraction = 0.0; if (cint(tax.included_in_print_rate)) { var tax_rate = this._get_tax_rate(tax, item_tax_map); if (tax.charge_type == "On Net Total") { current_tax_fraction = tax_rate / 100.0; } else if (tax.charge_type == "On Previous Row Amount") { current_tax_fraction = tax_rate / 100.0 * this.frm.doc["taxes"][cint(tax.row_id) - 1].tax_fraction_for_current_item; } else if (tax.charge_type == "On Previous Row Total") { current_tax_fraction = tax_rate / 100.0 * this.frm.doc["taxes"][cint(tax.row_id) - 1].grand_total_fraction_for_current_item; } } if (tax.add_deduct_tax) { current_tax_fraction *= tax.add_deduct_tax == "Deduct" ? -1.0 : 1.0; } return current_tax_fraction; }, _get_tax_rate: function _get_tax_rate(tax, item_tax_map) { return Object.keys(item_tax_map).indexOf(tax.account_head) != -1 ? flt(item_tax_map[tax.account_head], precision("rate", tax)) : tax.rate; }, calculate_net_total: function calculate_net_total() { var me = this; this.frm.doc.total = this.frm.doc.base_total = this.frm.doc.net_total = this.frm.doc.base_net_total = 0.0; $.each(this.frm.doc["items"] || [], function (i, item) { me.frm.doc.total += item.amount; me.frm.doc.base_total += item.base_amount; me.frm.doc.net_total += item.net_amount; me.frm.doc.base_net_total += item.base_net_amount; }); frappe.model.round_floats_in(this.frm.doc, ["total", "base_total", "net_total", "base_net_total"]); }, calculate_taxes: function calculate_taxes() { var me = this; var actual_tax_dict = {}; $.each(this.frm.doc["taxes"] || [], function (i, tax) { if (tax.charge_type == "Actual") { actual_tax_dict[tax.idx] = flt(tax.tax_amount, precision("tax_amount", tax)); } }); $.each(this.frm.doc["items"] || [], function (n, item) { var item_tax_map = me._load_item_tax_rate(item.item_tax_rate); $.each(me.frm.doc["taxes"] || [], function (i, tax) { var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map); if (tax.charge_type == "Actual") { actual_tax_dict[tax.idx] -= current_tax_amount; if (n == me.frm.doc["items"].length - 1) { current_tax_amount += actual_tax_dict[tax.idx]; } } if (tax.charge_type != "Actual" && !(me.discount_amount_applied && me.frm.doc.apply_discount_on == "Grand Total")) { tax.tax_amount += current_tax_amount; } tax.tax_amount_for_current_item = current_tax_amount; tax.tax_amount_after_discount_amount += current_tax_amount; if (tax.category) { current_tax_amount = tax.category == "Valuation" ? 0.0 : current_tax_amount; current_tax_amount *= tax.add_deduct_tax == "Deduct" ? -1.0 : 1.0; } if (i == 0) { tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount, precision("total", tax)); } else { tax.grand_total_for_current_item = flt(me.frm.doc["taxes"][i - 1].grand_total_for_current_item + current_tax_amount, precision("total", tax)); } tax.total += tax.grand_total_for_current_item; if (n == me.frm.doc["items"].length - 1) { me.round_off_totals(tax); if (i == me.frm.doc["taxes"].length - 1 && me.discount_amount_applied && me.frm.doc.apply_discount_on == "Grand Total" && me.frm.doc.discount_amount) me.adjust_discount_amount_loss(tax); } }); }); }, _load_item_tax_rate: function _load_item_tax_rate(item_tax_rate) { return item_tax_rate ? JSON.parse(item_tax_rate) : {}; }, get_current_tax_amount: function get_current_tax_amount(item, tax, item_tax_map) { var tax_rate = this._get_tax_rate(tax, item_tax_map); var current_tax_amount = 0.0; if (tax.charge_type == "Actual") { var actual = flt(tax.tax_amount, precision("tax_amount", tax)); current_tax_amount = this.frm.doc.net_total ? item.net_amount / this.frm.doc.net_total * actual : 0.0; } else if (tax.charge_type == "On Net Total") { current_tax_amount = tax_rate / 100.0 * item.net_amount; } else if (tax.charge_type == "On Previous Row Amount") { current_tax_amount = tax_rate / 100.0 * this.frm.doc["taxes"][cint(tax.row_id) - 1].tax_amount_for_current_item; } else if (tax.charge_type == "On Previous Row Total") { current_tax_amount = tax_rate / 100.0 * this.frm.doc["taxes"][cint(tax.row_id) - 1].grand_total_for_current_item; } current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax)); this.set_item_wise_tax(item, tax, tax_rate, current_tax_amount); return current_tax_amount; }, set_item_wise_tax: function set_item_wise_tax(item, tax, tax_rate, current_tax_amount) { var key = item.item_code || item.item_name; var item_wise_tax_amount = current_tax_amount * this.frm.doc.conversion_rate; if (tax.item_wise_tax_detail && tax.item_wise_tax_detail[key]) item_wise_tax_amount += tax.item_wise_tax_detail[key][1]; tax.item_wise_tax_detail[key] = [tax_rate, flt(item_wise_tax_amount, precision("base_tax_amount", tax))]; }, round_off_totals: function round_off_totals(tax) { tax.total = flt(tax.total, precision("total", tax)); tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax)); tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, precision("tax_amount", tax)); this.set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"]); }, adjust_discount_amount_loss: function adjust_discount_amount_loss(tax) { var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total; tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount + discount_amount_loss, precision("tax_amount", tax)); tax.total = flt(tax.total + discount_amount_loss, precision("total", tax)); this.set_in_company_currency(tax, ["total", "tax_amount_after_discount_amount"]); }, manipulate_grand_total_for_inclusive_tax: function manipulate_grand_total_for_inclusive_tax() { var me = this; if (this.frm.doc["taxes"] && this.frm.doc["taxes"].length) { var all_inclusive = frappe.utils.all(this.frm.doc["taxes"].map(function (d) { return cint(d.included_in_print_rate); })); if (all_inclusive) { var last_tax = me.frm.doc["taxes"].slice(-1)[0]; var diff = me.frm.doc.total - flt(last_tax.total, precision("grand_total")); if (diff && Math.abs(diff) <= 2.0 / Math.pow(10, precision("tax_amount", last_tax))) { last_tax.tax_amount += diff; last_tax.tax_amount_after_discount += diff; last_tax.total += diff; this.set_in_company_currency(last_tax, ["total", "tax_amount", "tax_amount_after_discount_amount"]); } } } }, calculate_totals: function calculate_totals() { var me = this; var tax_count = this.frm.doc["taxes"] ? this.frm.doc["taxes"].length : 0; this.frm.doc.grand_total = flt(tax_count ? this.frm.doc["taxes"][tax_count - 1].total : this.frm.doc.net_total); if (in_list(["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"], this.frm.doc.doctype)) { this.frm.doc.base_grand_total = this.frm.doc.total_taxes_and_charges ? flt(this.frm.doc.grand_total * this.frm.doc.conversion_rate) : this.frm.doc.base_net_total; } else { this.frm.doc.taxes_and_charges_added = this.frm.doc.taxes_and_charges_deducted = 0.0; if (tax_count) { $.each(this.frm.doc["taxes"] || [], function (i, tax) { if (in_list(["Valuation and Total", "Total"], tax.category)) { if (tax.add_deduct_tax == "Add") { me.frm.doc.taxes_and_charges_added += flt(tax.tax_amount_after_discount_amount); } else { me.frm.doc.taxes_and_charges_deducted += flt(tax.tax_amount_after_discount_amount); } } }); frappe.model.round_floats_in(this.frm.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"]); } this.frm.doc.base_grand_total = flt(this.frm.doc.taxes_and_charges_added || this.frm.doc.taxes_and_charges_deducted ? flt(this.frm.doc.grand_total * this.frm.doc.conversion_rate) : this.frm.doc.base_net_total); this.set_in_company_currency(this.frm.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"]); } this.frm.doc.total_taxes_and_charges = flt(this.frm.doc.grand_total - this.frm.doc.net_total, precision("total_taxes_and_charges")); this.set_in_company_currency(this.frm.doc, ["total_taxes_and_charges"]); frappe.model.round_floats_in(this.frm.doc, ["grand_total", "base_grand_total"]); if (frappe.meta.get_docfield(this.frm.doc.doctype, "rounded_total", this.frm.doc.name)) { this.frm.doc.rounded_total = round_based_on_smallest_currency_fraction(this.frm.doc.grand_total, this.frm.doc.currency, precision("rounded_total")); } if (frappe.meta.get_docfield(this.frm.doc.doctype, "base_rounded_total", this.frm.doc.name)) { var company_currency = this.frm.doc.currency || this.get_company_currency(); this.frm.doc.base_rounded_total = round_based_on_smallest_currency_fraction(this.frm.doc.base_grand_total, company_currency, precision("base_rounded_total")); } }, _cleanup: function _cleanup() { this.frm.doc.base_in_words = this.frm.doc.in_words = ""; if (this.frm.doc["items"] && this.frm.doc["items"].length) { if (!frappe.meta.get_docfield(this.frm.doc["items"][0].doctype, "item_tax_amount", this.frm.doctype)) { $.each(this.frm.doc["items"] || [], function (i, item) { delete item["item_tax_amount"]; }); } } if (this.frm.doc["taxes"] && this.frm.doc["taxes"].length) { var temporary_fields = ["tax_amount_for_current_item", "grand_total_for_current_item", "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]; if (!frappe.meta.get_docfield(this.frm.doc["taxes"][0].doctype, "tax_amount_after_discount_amount", this.frm.doctype)) { temporary_fields.push("tax_amount_after_discount_amount"); } $.each(this.frm.doc["taxes"] || [], function (i, tax) { $.each(temporary_fields, function (i, fieldname) { delete tax[fieldname]; }); tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail); }); } }, set_discount_amount: function set_discount_amount() { if (this.frm.doc.additional_discount_percentage) { this.frm.doc.discount_amount = flt(flt(this.frm.doc[frappe.scrub(this.frm.doc.apply_discount_on)]) * this.frm.doc.additional_discount_percentage / 100, precision("discount_amount")); } }, apply_discount_amount: function apply_discount_amount() { var me = this; var distributed_amount = 0.0; this.frm.doc.base_discount_amount = 0.0; if (this.frm.doc.discount_amount) { if (!this.frm.doc.apply_discount_on) frappe.throw(__("Please select Apply Discount On")); this.frm.doc.base_discount_amount = flt(this.frm.doc.discount_amount * this.frm.doc.conversion_rate, precision("base_discount_amount")); var total_for_discount_amount = this.get_total_for_discount_amount(); if (total_for_discount_amount) { $.each(this.frm.doc["items"] || [], function (i, item) { distributed_amount = flt(me.frm.doc.discount_amount) * item.net_amount / total_for_discount_amount; item.net_amount = flt(item.net_amount - distributed_amount, precision("base_amount", item)); item.net_rate = flt(item.net_amount / item.qty, precision("net_rate", item)); me.set_in_company_currency(item, ["net_rate", "net_amount"]); }); this.discount_amount_applied = true; this._calculate_taxes_and_totals(); } } }, get_total_for_discount_amount: function get_total_for_discount_amount() { var me = this; if (this.frm.doc.apply_discount_on == "Net Total") { return this.frm.doc.net_total; } else { var total_actual_tax = 0.0; var actual_taxes_dict = {}; $.each(this.frm.doc["taxes"] || [], function (i, tax) { if (tax.charge_type == "Actual") actual_taxes_dict[tax.idx] = tax.tax_amount;else if (actual_taxes_dict[tax.row_id] !== null) { var actual_tax_amount = flt(actual_taxes_dict[tax.row_id]) * flt(tax.rate) / 100; actual_taxes_dict[tax.idx] = actual_tax_amount; } }); $.each(actual_taxes_dict, function (key, value) { if (value) total_actual_tax += value; }); return flt(this.frm.doc.grand_total - total_actual_tax, precision("grand_total")); } }, calculate_total_advance: function calculate_total_advance(update_paid_amount) { var total_allocated_amount = frappe.utils.sum($.map(this.frm.doc["advances"] || [], function (adv) { return flt(adv.allocated_amount, precision("allocated_amount", adv)); })); this.frm.doc.total_advance = flt(total_allocated_amount, precision("total_advance")); this.calculate_outstanding_amount(update_paid_amount); }, calculate_outstanding_amount: function calculate_outstanding_amount(update_paid_amount) { if (this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.is_return) { this.calculate_paid_amount(); } if (this.frm.doc.is_return || this.frm.doc.docstatus > 0) return; frappe.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount"]); if (this.frm.doc.party_account_currency == this.frm.doc.currency) { var total_amount_to_pay = flt(this.frm.doc.grand_total - this.frm.doc.total_advance - this.frm.doc.write_off_amount, precision("grand_total")); } else { var total_amount_to_pay = flt(flt(this.frm.doc.grand_total * this.frm.doc.conversion_rate, precision("grand_total")) - this.frm.doc.total_advance - this.frm.doc.base_write_off_amount, precision("base_grand_total")); } if (this.frm.doc.doctype == "Sales Invoice" || this.frm.doc.doctype == "Purchase Invoice") { frappe.model.round_floats_in(this.frm.doc, ["paid_amount"]); this.set_in_company_currency(this.frm.doc, ["paid_amount"]); if (this.frm.refresh_field) { this.frm.refresh_field("paid_amount"); this.frm.refresh_field("base_paid_amount"); } if (this.frm.doc.doctype == "Sales Invoice") { this.set_default_payment(total_amount_to_pay, update_paid_amount); this.calculate_paid_amount(); } this.calculate_change_amount(); var outstanding_amount = 0.0; var paid_amount = this.frm.doc.party_account_currency == this.frm.doc.currency ? this.frm.doc.paid_amount : this.frm.doc.base_paid_amount; var change_amount = this.frm.doc.party_account_currency == this.frm.doc.currency ? this.frm.doc.change_amount : this.frm.doc.base_change_amount; this.frm.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount) + flt(this.frm.doc.change_amount * this.frm.doc.conversion_rate), precision("outstanding_amount")); } else if (this.frm.doc.doctype == "Purchase Invoice") { this.frm.doc.outstanding_amount = flt(total_amount_to_pay, precision("outstanding_amount")); } }, set_default_payment: function set_default_payment(total_amount_to_pay, update_paid_amount) { var me = this; var payment_status = true; if (this.frm.doc.is_pos && (update_paid_amount === undefined || update_paid_amount)) { $.each(this.frm.doc['payments'] || [], function (index, data) { if (data.default && payment_status && total_amount_to_pay > 0) { data.base_amount = flt(total_amount_to_pay, precision("base_amount")); data.amount = flt(total_amount_to_pay / me.frm.doc.conversion_rate, precision("amount")); payment_status = false; } else if (me.frm.doc.paid_amount) { data.amount = 0.0; } }); } }, calculate_paid_amount: function calculate_paid_amount() { var me = this; var paid_amount = 0.0; var base_paid_amount = 0.0; if (this.frm.doc.is_pos) { $.each(this.frm.doc['payments'] || [], function (index, data) { data.base_amount = flt(data.amount * me.frm.doc.conversion_rate, precision("base_amount")); paid_amount += data.amount; base_paid_amount += data.base_amount; }); } else if (!this.frm.doc.is_return) { this.frm.doc.payments = []; } this.frm.doc.paid_amount = flt(paid_amount, precision("paid_amount")); this.frm.doc.base_paid_amount = flt(base_paid_amount, precision("base_paid_amount")); }, calculate_change_amount: function calculate_change_amount() { this.frm.doc.change_amount = 0.0; this.frm.doc.base_change_amount = 0.0; if (this.frm.doc.paid_amount > this.frm.doc.grand_total && !this.frm.doc.is_return) { var payment_types = $.map(this.frm.doc.payments, function (d) { return d.type; }); if (in_list(payment_types, 'Cash')) { this.frm.doc.change_amount = flt(this.frm.doc.paid_amount - this.frm.doc.grand_total + this.frm.doc.write_off_amount, precision("change_amount")); this.frm.doc.base_change_amount = flt(this.frm.doc.base_paid_amount - this.frm.doc.base_grand_total + this.frm.doc.base_write_off_amount, precision("base_change_amount")); } } }, calculate_write_off_amount: function calculate_write_off_amount() { if (this.frm.doc.paid_amount > this.frm.doc.grand_total) { this.frm.doc.write_off_amount = flt(this.frm.doc.grand_total - this.frm.doc.paid_amount + this.frm.doc.change_amount, precision("write_off_amount")); this.frm.doc.base_write_off_amount = flt(this.frm.doc.write_off_amount * this.frm.doc.conversion_rate, precision("base_write_off_amount")); } else { this.frm.doc.paid_amount = 0.0; } this.calculate_outstanding_amount(false); } }); erpnext.TransactionController = erpnext.taxes_and_totals.extend({ setup: function setup() { this._super(); frappe.ui.form.on(this.frm.doctype + " Item", "rate", function (frm, cdt, cdn) { var item = frappe.get_doc(cdt, cdn); var has_margin_field = frappe.meta.has_field(cdt, 'margin_type'); frappe.model.round_floats_in(item, ["rate", "price_list_rate"]); if (item.price_list_rate) { if (item.rate > item.price_list_rate && has_margin_field) { item.discount_percentage = 0; item.margin_type = 'Amount'; item.margin_rate_or_amount = flt(item.rate - item.price_list_rate, precision("margin_rate_or_amount", item)); item.rate_with_margin = item.rate; } else { item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item)); item.margin_type = ''; item.margin_rate_or_amount = 0; item.rate_with_margin = 0; } } else { item.discount_percentage = 0.0; item.margin_type = ''; item.margin_rate_or_amount = 0; item.rate_with_margin = 0; } cur_frm.cscript.set_gross_profit(item); cur_frm.cscript.calculate_taxes_and_totals(); }); frappe.ui.form.on(this.frm.cscript.tax_table, "rate", function (frm, cdt, cdn) { cur_frm.cscript.calculate_taxes_and_totals(); }); frappe.ui.form.on(this.frm.cscript.tax_table, "tax_amount", function (frm, cdt, cdn) { cur_frm.cscript.calculate_taxes_and_totals(); }); frappe.ui.form.on(this.frm.cscript.tax_table, "row_id", function (frm, cdt, cdn) { cur_frm.cscript.calculate_taxes_and_totals(); }); frappe.ui.form.on(this.frm.cscript.tax_table, "included_in_print_rate", function (frm, cdt, cdn) { cur_frm.cscript.set_dynamic_labels(); cur_frm.cscript.calculate_taxes_and_totals(); }); frappe.ui.form.on(this.frm.doctype, "apply_discount_on", function (frm) { if (frm.doc.additional_discount_percentage) { frm.trigger("additional_discount_percentage"); } else { cur_frm.cscript.calculate_taxes_and_totals(); } }); frappe.ui.form.on(this.frm.doctype, "additional_discount_percentage", function (frm) { if (!frm.doc.apply_discount_on) { frappe.msgprint(__("Please set 'Apply Additional Discount On'")); return; } frm.via_discount_percentage = true; if (frm.doc.additional_discount_percentage && frm.doc.discount_amount) { frm.doc.discount_amount = 0; frm.cscript.calculate_taxes_and_totals(); } var total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]); var discount_amount = flt(total * flt(frm.doc.additional_discount_percentage) / 100, precision("discount_amount")); frm.set_value("discount_amount", discount_amount).then(function () { return delete frm.via_discount_percentage; }); }); frappe.ui.form.on(this.frm.doctype, "discount_amount", function (frm) { frm.cscript.set_dynamic_labels(); if (!frm.via_discount_percentage) { frm.doc.additional_discount_percentage = 0; } frm.cscript.calculate_taxes_and_totals(); }); var me = this; if (this.frm.fields_dict["items"].grid.get_field('batch_no')) { this.frm.set_query("batch_no", "items", function (doc, cdt, cdn) { return me.set_query_for_batch(doc, cdt, cdn); }); } }, onload: function onload() { var me = this; if (this.frm.doc.__islocal) { var today = frappe.datetime.get_today(), currency = frappe.defaults.get_user_default("currency"); $.each({ currency: currency, price_list_currency: currency, status: "Draft", is_subcontracted: "No" }, function (fieldname, value) { if (me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname]) me.frm.set_value(fieldname, value); }); if (this.frm.doc.company && !this.frm.doc.amended_from) { this.frm.trigger("company"); } } if (this.frm.fields_dict["taxes"]) { this["taxes_remove"] = this.calculate_taxes_and_totals; } if (this.frm.fields_dict["items"]) { this["items_remove"] = this.calculate_taxes_and_totals; } if (this.frm.fields_dict["recurring_print_format"]) { this.frm.set_query("recurring_print_format", function (doc) { return { filters: [['Print Format', 'doc_type', '=', cur_frm.doctype]] }; }); } if (this.frm.fields_dict["return_against"]) { this.frm.set_query("return_against", function (doc) { var filters = { "docstatus": 1, "is_return": 0, "company": doc.company }; if (me.frm.fields_dict["customer"] && doc.customer) filters["customer"] = doc.customer; if (me.frm.fields_dict["supplier"] && doc.supplier) filters["supplier"] = doc.supplier; return { filters: filters }; }); } this.setup_quality_inspection(); }, setup_quality_inspection: function setup_quality_inspection() { if (!in_list(["Delivery Note", "Sales Invoice", "Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype)) { return; } var me = this; var inspection_type = in_list(["Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype) ? "Incoming" : "Outgoing"; var quality_inspection_field = this.frm.get_docfield("items", "quality_inspection"); quality_inspection_field.get_route_options_for_new_doc = function (row) { if (me.frm.is_new()) return; return { "inspection_type": inspection_type, "reference_type": me.frm.doc.doctype, "reference_name": me.frm.doc.name, "item_code": row.doc.item_code, "description": row.doc.description, "item_serial_no": row.doc.serial_no ? row.doc.serial_no.split("\n")[0] : null, "batch_no": row.doc.batch_no }; }; this.frm.set_query("quality_inspection", "items", function (doc, cdt, cdn) { var d = locals[cdt][cdn]; return { filters: { docstatus: 1, inspection_type: inspection_type, item_code: d.item_code } }; }); }, onload_post_render: function onload_post_render() { var me = this; if (this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length && !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) { this.apply_default_taxes(); } else if (this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"] && !this.frm.doc.is_pos) { me.calculate_taxes_and_totals(); } if (frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) { this.setup_item_selector(); } }, refresh: function refresh() { erpnext.toggle_naming_series(); erpnext.hide_company(); this.set_dynamic_labels(); this.setup_sms(); }, apply_default_taxes: function apply_default_taxes() { var me = this; var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges", me.frm.doc.name); if (taxes_and_charges_field) { return frappe.call({ method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges", args: { "master_doctype": taxes_and_charges_field.options }, callback: function callback(r) { if (!r.exc) { me.frm.set_value("taxes", r.message); me.calculate_taxes_and_totals(); } } }); } }, setup_sms: function setup_sms() { var me = this; if (this.frm.doc.docstatus === 1 && !in_list(["Lost", "Stopped", "Closed"], this.frm.doc.status) && this.frm.doctype != "Purchase Invoice") { this.frm.page.add_menu_item(__('Send SMS'), function () { me.send_sms(); }); } }, send_sms: function send_sms() { var sms_man = new erpnext.SMSManager(this.frm.doc); }, barcode: function barcode(doc, cdt, cdn) { var d = locals[cdt][cdn]; if (d.barcode == "" || d.barcode == null) { d.item_code = ""; } this.item_code(doc, cdt, cdn, true); }, item_code: function item_code(doc, cdt, cdn, from_barcode) { var me = this; var item = frappe.get_doc(cdt, cdn); var update_stock = 0, show_batch_dialog = 0; if (['Sales Invoice', 'Purchase Invoice'].includes(this.frm.doc.doctype)) { update_stock = cint(me.frm.doc.update_stock); show_batch_dialog = update_stock; } else if (this.frm.doc.doctype === 'Purchase Receipt' && me.frm.doc.is_return || this.frm.doc.doctype === 'Delivery Note') { show_batch_dialog = 1; } if (!from_barcode) { item.barcode = null; } if (item.item_code || item.barcode || item.serial_no) { if (!this.validate_company_and_party()) { this.frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove(); } else { return this.frm.call({ method: "erpnext.stock.get_item_details.get_item_details", child: item, args: { args: { item_code: item.item_code, barcode: item.barcode, serial_no: item.serial_no, warehouse: item.warehouse, customer: me.frm.doc.customer, supplier: me.frm.doc.supplier, currency: me.frm.doc.currency, update_stock: update_stock, conversion_rate: me.frm.doc.conversion_rate, price_list: me.frm.doc.selling_price_list || me.frm.doc.buying_price_list, price_list_currency: me.frm.doc.price_list_currency, plc_conversion_rate: me.frm.doc.plc_conversion_rate, company: me.frm.doc.company, order_type: me.frm.doc.order_type, is_pos: cint(me.frm.doc.is_pos), is_subcontracted: me.frm.doc.is_subcontracted, transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date, ignore_pricing_rule: me.frm.doc.ignore_pricing_rule, doctype: me.frm.doc.doctype, name: me.frm.doc.name, project: item.project || me.frm.doc.project, qty: item.qty || 1, stock_qty: item.stock_qty, conversion_factor: item.conversion_factor } }, callback: function callback(r) { if (!r.exc) { me.frm.script_manager.trigger("price_list_rate", cdt, cdn); me.toggle_conversion_factor(item); if (show_batch_dialog) { var d = locals[cdt][cdn]; $.each(r.message, function (k, v) { if (!d[k]) d[k] = v; }); erpnext.show_serial_batch_selector(me.frm, d); } } } }); } } }, serial_no: function serial_no(doc, cdt, cdn) { var me = this; var item = frappe.get_doc(cdt, cdn); if (item.serial_no) { if (!item.item_code) { this.frm.trigger("item_code", cdt, cdn); } else { var valid_serial_nos = []; var serial_nos = item.serial_no.trim().replace(/,/g, '\n'); serial_nos = serial_nos.trim().split('\n'); for (var x = 0; x <= serial_nos.length - 1; x++) { if (serial_nos[x].trim() != "" && valid_serial_nos.indexOf(serial_nos[x].trim()) == -1) { valid_serial_nos.push(serial_nos[x].trim()); } } item.serial_no = valid_serial_nos.join('\n'); refresh_field("serial_no", item.name, item.parentfield); if (!doc.is_return) { frappe.model.set_value(item.doctype, item.name, "qty", valid_serial_nos.length / item.conversion_factor); frappe.model.set_value(item.doctype, item.name, "stock_qty", valid_serial_nos.length); } } } }, validate: function validate() { this.calculate_taxes_and_totals(false); }, company: function company() { var me = this; var set_pricing = function set_pricing() { if (me.frm.doc.company && me.frm.fields_dict.currency) { var company_currency = me.get_company_currency(); var company_doc = frappe.get_doc(":Company", me.frm.doc.company); if (!me.frm.doc.currency) { me.frm.set_value("currency", company_currency); } if (me.frm.doc.currency == company_currency) { me.frm.set_value("conversion_rate", 1.0); } if (me.frm.doc.price_list_currency == company_currency) { me.frm.set_value('plc_conversion_rate', 1.0); } if (company_doc.default_letter_head) { if (me.frm.fields_dict.letter_head) { me.frm.set_value("letter_head", company_doc.default_letter_head); } } if (company_doc.default_terms && me.frm.doc.doctype != "Purchase Invoice" && frappe.meta.has_field(me.frm.doc.doctype, "tc_name")) { me.frm.set_value("tc_name", company_doc.default_terms); } me.frm.script_manager.trigger("currency"); me.apply_pricing_rule(); } }; var set_party_account = function set_party_account(set_pricing) { if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) { if (me.frm.doc.doctype == "Sales Invoice") { var party_type = "Customer"; var party_account_field = 'debit_to'; } else { var party_type = "Supplier"; var party_account_field = 'credit_to'; } var party = me.frm.doc[frappe.model.scrub(party_type)]; if (party) { return frappe.call({ method: "erpnext.accounts.party.get_party_account", args: { company: me.frm.doc.company, party_type: party_type, party: party }, callback: function callback(r) { if (!r.exc && r.message) { me.frm.set_value(party_account_field, r.message); set_pricing(); } } }); } else { set_pricing(); } } else { set_pricing(); } }; if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;else var date = this.frm.doc.transaction_date; if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") && in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)) { erpnext.utils.get_shipping_address(this.frm, function () { set_party_account(set_pricing); }); } else { set_party_account(set_pricing); } if (this.frm.doc.company) { erpnext.last_selected_company = this.frm.doc.company; } }, transaction_date: function transaction_date() { if (this.frm.doc.transaction_date) { this.frm.transaction_date = this.frm.doc.transaction_date; frappe.ui.form.trigger(this.frm.doc.doctype, "currency"); } }, posting_date: function posting_date() { var me = this; if (this.frm.doc.posting_date) { this.frm.posting_date = this.frm.doc.posting_date; if (this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer || this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier) { return frappe.call({ method: "erpnext.accounts.party.get_due_date", args: { "posting_date": me.frm.doc.posting_date, "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier", "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier, "company": me.frm.doc.company }, callback: function callback(r, rt) { if (r.message) { me.frm.set_value("due_date", r.message); frappe.ui.form.trigger(me.frm.doc.doctype, "currency"); } } }); } else { frappe.ui.form.trigger(me.frm.doc.doctype, "currency"); } } }, get_company_currency: function get_company_currency() { return erpnext.get_currency(this.frm.doc.company); }, contact_person: function contact_person() { erpnext.utils.get_contact_details(this.frm); }, currency: function currency() { var transaction_date = this.frm.doc.transaction_date || this.frm.doc.posting_date; var me = this; this.set_dynamic_labels(); var company_currency = this.get_company_currency(); if (this.frm.doc.currency && this.frm.doc.currency !== company_currency && !this.frm.doc.ignore_pricing_rule) { this.get_exchange_rate(transaction_date, this.frm.doc.currency, company_currency, function (exchange_rate) { me.frm.set_value("conversion_rate", exchange_rate); }); } else { this.conversion_rate(); } }, conversion_rate: function conversion_rate() { if (this.frm.doc.currency === this.get_company_currency()) { this.frm.set_value("conversion_rate", 1.0); } if (this.frm.doc.currency === this.frm.doc.price_list_currency && this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) { this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate); } if (flt(this.frm.doc.conversion_rate) > 0.0) { if (this.frm.doc.ignore_pricing_rule) { this.calculate_taxes_and_totals(); } else if (!this.in_apply_price_list) { this.apply_price_list(); } } }, get_exchange_rate: function get_exchange_rate(transaction_date, from_currency, to_currency, _callback) { if (!transaction_date || !from_currency || !to_currency) return; return frappe.call({ method: "erpnext.setup.utils.get_exchange_rate", args: { transaction_date: transaction_date, from_currency: from_currency, to_currency: to_currency }, callback: function callback(r) { _callback(flt(r.message)); } }); }, price_list_currency: function price_list_currency() { var me = this; this.set_dynamic_labels(); var company_currency = this.get_company_currency(); if (this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) { this.get_exchange_rate(this.frm.doc.posting_date, this.frm.doc.price_list_currency, company_currency, function (exchange_rate) { me.frm.set_value("plc_conversion_rate", exchange_rate); }); } else { this.plc_conversion_rate(); } }, plc_conversion_rate: function plc_conversion_rate() { if (this.frm.doc.price_list_currency === this.get_company_currency()) { this.frm.set_value("plc_conversion_rate", 1.0); } else if (this.frm.doc.price_list_currency === this.frm.doc.currency && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 && cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) { this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate); } if (!this.in_apply_price_list) { this.apply_price_list(); } }, uom: function uom(doc, cdt, cdn) { var me = this; var item = frappe.get_doc(cdt, cdn); if (item.item_code && item.uom) { return this.frm.call({ method: "erpnext.stock.get_item_details.get_conversion_factor", child: item, args: { item_code: item.item_code, uom: item.uom }, callback: function callback(r) { if (!r.exc) { me.conversion_factor(me.frm.doc, cdt, cdn); } } }); } }, conversion_factor: function conversion_factor(doc, cdt, cdn, dont_fetch_price_list_rate) { if (frappe.meta.get_docfield(cdt, "stock_qty", cdn)) { var item = frappe.get_doc(cdt, cdn); frappe.model.round_floats_in(item, ["qty", "conversion_factor"]); item.stock_qty = flt(item.qty * item.conversion_factor, precision("stock_qty", item)); refresh_field("stock_qty", item.name, item.parentfield); this.toggle_conversion_factor(item); if (!dont_fetch_price_list_rate) this.apply_price_list(item, true); } }, toggle_conversion_factor: function toggle_conversion_factor(item) { if (this.frm.get_field('items').grid.fields_map.conversion_factor) { this.frm.fields_dict.items.grid.toggle_enable("conversion_factor", item.uom != item.stock_uom ? true : false); } }, qty: function qty(doc, cdt, cdn) { this.conversion_factor(doc, cdt, cdn, true); this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true); }, set_dynamic_labels: function set_dynamic_labels() { this.frm.toggle_reqd("plc_conversion_rate", !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency)); if (this.frm.doc_currency !== this.frm.doc.currency) { var company_currency = this.get_company_currency(); this.change_form_labels(company_currency); this.change_grid_labels(company_currency); this.frm.refresh_fields(); this.frm.doc_currency = this.frm.doc.currency; } }, change_form_labels: function change_form_labels(company_currency) { var me = this; this.frm.set_currency_labels(["base_total", "base_net_total", "base_total_taxes_and_charges", "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words", "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay", "base_paid_amount", "base_write_off_amount", "base_change_amount", "base_operating_cost", "base_raw_material_cost", "base_total_cost", "base_scrap_material_cost"], company_currency); this.frm.set_currency_labels(["total", "net_total", "total_taxes_and_charges", "discount_amount", "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted", "rounded_total", "in_words", "paid_amount", "write_off_amount", "operating_cost", "scrap_material_cost", "raw_material_cost", "total_cost"], this.frm.doc.currency); this.frm.set_currency_labels(["outstanding_amount", "total_advance"], this.frm.doc.party_account_currency); cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency + " = [?] " + company_currency); if (this.frm.doc.price_list_currency && this.frm.doc.price_list_currency != company_currency) { cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency + " = [?] " + company_currency); } this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges", "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount", "base_paid_amount", "base_write_off_amount", "base_operating_cost", "base_raw_material_cost", "base_total_cost", "base_scrap_material_cost"], this.frm.doc.currency != company_currency); this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"], this.frm.doc.price_list_currency != company_currency); var show = cint(cur_frm.doc.discount_amount) || (cur_frm.doc.taxes || []).filter(function (d) { return d.included_in_print_rate === 1; }).length; if (frappe.meta.get_docfield(cur_frm.doctype, "net_total")) cur_frm.toggle_display("net_total", show); if (frappe.meta.get_docfield(cur_frm.doctype, "base_net_total")) cur_frm.toggle_display("base_net_total", show && me.frm.doc.currency != company_currency); }, change_grid_labels: function change_grid_labels(company_currency) { var me = this; this.frm.set_currency_labels(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"], company_currency, "items"); this.frm.set_currency_labels(["rate", "net_rate", "price_list_rate", "amount", "net_amount"], this.frm.doc.currency, "items"); if (this.frm.fields_dict["operations"]) { this.frm.set_currency_labels(["operating_cost", "hour_rate"], this.frm.doc.currency, "operations"); this.frm.set_currency_labels(["base_operating_cost", "base_hour_rate"], company_currency, "operations"); var item_grid = this.frm.fields_dict["operations"].grid; $.each(["base_operating_cost", "base_hour_rate"], function (i, fname) { if (frappe.meta.get_docfield(item_grid.doctype, fname)) item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); }); } if (this.frm.fields_dict["scrap_items"]) { this.frm.set_currency_labels(["rate", "amount"], this.frm.doc.currency, "scrap_items"); this.frm.set_currency_labels(["base_rate", "base_amount"], company_currency, "scrap_items"); var item_grid = this.frm.fields_dict["scrap_items"].grid; $.each(["base_rate", "base_amount"], function (i, fname) { if (frappe.meta.get_docfield(item_grid.doctype, fname)) item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); }); } if (this.frm.fields_dict["taxes"]) { this.frm.set_currency_labels(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes"); this.frm.set_currency_labels(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes"); } if (this.frm.fields_dict["advances"]) { this.frm.set_currency_labels(["advance_amount", "allocated_amount"], this.frm.doc.party_account_currency, "advances"); } var item_grid = this.frm.fields_dict["items"].grid; $.each(["base_rate", "base_price_list_rate", "base_amount"], function (i, fname) { if (frappe.meta.get_docfield(item_grid.doctype, fname)) item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency); }); var show = cint(cur_frm.doc.discount_amount) || (cur_frm.doc.taxes || []).filter(function (d) { return d.included_in_print_rate === 1; }).length; $.each(["net_rate", "net_amount"], function (i, fname) { if (frappe.meta.get_docfield(item_grid.doctype, fname)) item_grid.set_column_disp(fname, show); }); $.each(["base_net_rate", "base_net_amount"], function (i, fname) { if (frappe.meta.get_docfield(item_grid.doctype, fname)) item_grid.set_column_disp(fname, show && me.frm.doc.currency != company_currency); }); var $wrapper = $(this.frm.wrapper); }, recalculate: function recalculate() { this.calculate_taxes_and_totals(); }, recalculate_values: function recalculate_values() { this.calculate_taxes_and_totals(); }, calculate_charges: function calculate_charges() { this.calculate_taxes_and_totals(); }, ignore_pricing_rule: function ignore_pricing_rule() { if (this.frm.doc.ignore_pricing_rule) { var me = this; var item_list = []; $.each(this.frm.doc["items"] || [], function (i, d) { if (d.item_code) { item_list.push({ "doctype": d.doctype, "name": d.name, "pricing_rule": d.pricing_rule }); } }); return this.frm.call({ method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.remove_pricing_rules", args: { item_list: item_list }, callback: function callback(r) { if (!r.exc && r.message) { me._set_values_for_item_list(r.message); me.calculate_taxes_and_totals(); if (me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on"); } } }); } else { this.apply_pricing_rule(); } }, apply_pricing_rule: function apply_pricing_rule(item, calculate_taxes_and_totals) { var me = this; var args = this._get_args(item); if (!(args.items && args.items.length)) { if (calculate_taxes_and_totals) me.calculate_taxes_and_totals(); return; } return this.frm.call({ method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule", args: { args: args }, callback: function callback(r) { if (!r.exc && r.message) { me._set_values_for_item_list(r.message); if (item) me.set_gross_profit(item); if (calculate_taxes_and_totals) me.calculate_taxes_and_totals(); if (me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on"); } } }); }, _get_args: function _get_args(item) { var me = this; return { "items": this._get_item_list(item), "customer": me.frm.doc.customer, "customer_group": me.frm.doc.customer_group, "territory": me.frm.doc.territory, "supplier": me.frm.doc.supplier, "supplier_type": me.frm.doc.supplier_type, "currency": me.frm.doc.currency, "conversion_rate": me.frm.doc.conversion_rate, "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list, "price_list_currency": me.frm.doc.price_list_currency, "plc_conversion_rate": me.frm.doc.plc_conversion_rate, "company": me.frm.doc.company, "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date, "campaign": me.frm.doc.campaign, "sales_partner": me.frm.doc.sales_partner, "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule, "doctype": me.frm.doc.doctype, "name": me.frm.doc.name, "is_return": cint(me.frm.doc.is_return), "update_stock": in_list(['Sales Invoice', 'Purchase Invoice'], me.frm.doc.doctype) ? cint(me.frm.doc.update_stock) : 0, "conversion_factor": me.frm.doc.conversion_factor }; }, _get_item_list: function _get_item_list(item) { var item_list = []; var append_item = function append_item(d) { if (d.item_code) { item_list.push({ "doctype": d.doctype, "name": d.name, "item_code": d.item_code, "item_group": d.item_group, "brand": d.brand, "qty": d.qty, "parenttype": d.parenttype, "parent": d.parent, "pricing_rule": d.pricing_rule, "warehouse": d.warehouse, "serial_no": d.serial_no, "discount_percentage": d.discount_percentage || 0.0, "conversion_factor": d.conversion_factor || 1.0 }); if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype) { item_list[0]["margin_type"] = d.margin_type; item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount; } } }; if (item) { append_item(item); } else { $.each(this.frm.doc["items"] || [], function (i, d) { append_item(d); }); } return item_list; }, _set_values_for_item_list: function _set_values_for_item_list(children) { var me = this; var price_list_rate_changed = false; for (var i = 0, l = children.length; i < l; i++) { var d = children[i]; var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule"); for (var k in d) { var v = d[k]; if (["doctype", "name"].indexOf(k) === -1) { if (k == "price_list_rate") { if (flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true; } frappe.model.set_value(d.doctype, d.name, k, v); } } if (!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) { me.apply_price_list(frappe.get_doc(d.doctype, d.name)); } } if (!price_list_rate_changed) me.calculate_taxes_and_totals(); }, apply_price_list: function apply_price_list(item) { var me = this; var args = this._get_args(item); if (!(args.items && args.items.length || args.price_list)) { return; } return this.frm.call({ method: "erpnext.stock.get_item_details.apply_price_list", args: { args: args }, callback: function callback(r) { if (!r.exc) { me.in_apply_price_list = true; me.frm.set_value("price_list_currency", r.message.parent.price_list_currency); me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate); me.in_apply_price_list = false; if (args.items.length) { me._set_values_for_item_list(r.message.children); } } } }); }, validate_company_and_party: function validate_company_and_party() { var me = this; var valid = true; $.each(["company", "customer"], function (i, fieldname) { if (frappe.meta.has_field(me.frm.doc.doctype, fieldname) && me.frm.doc.doctype != "Purchase Order") { if (!me.frm.doc[fieldname]) { frappe.msgprint(__("Please specify") + ": " + frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) + ". " + __("It is needed to fetch Item Details.")); valid = false; } } }); return valid; }, get_terms: function get_terms() { var me = this; erpnext.utils.get_terms(this.frm.doc.tc_name, this.frm.doc, function (r) { if (!r.exc) { me.frm.set_value("terms", r.message); } }); }, taxes_and_charges: function taxes_and_charges() { var me = this; if (this.frm.doc.taxes_and_charges) { return this.frm.call({ method: "erpnext.controllers.accounts_controller.get_taxes_and_charges", args: { "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges", this.frm.doc.name).options, "master_name": this.frm.doc.taxes_and_charges }, callback: function callback(r) { if (!r.exc) { me.frm.set_value("taxes", r.message); me.calculate_taxes_and_totals(); } } }); } }, is_recurring: function is_recurring() { if (this.frm.doc.is_recurring && this.frm.doc.__islocal) { frappe.msgprint(__("Please set recurring after saving")); this.frm.set_value('is_recurring', 0); return; } if (this.frm.doc.is_recurring) { if (!this.frm.doc.recurring_id) { this.frm.set_value('recurring_id', this.frm.doc.name); } var owner_email = this.frm.doc.owner == "Administrator" ? frappe.user_info("Administrator").email : this.frm.doc.owner; this.frm.doc.notification_email_address = $.map([cstr(owner_email), cstr(this.frm.doc.contact_email)], function (v) { return v || null; }).join(", "); this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate(); } refresh_many(["notification_email_address", "repeat_on_day_of_month"]); }, from_date: function from_date() { if (this.frm.doc.from_date) { var recurring_type_map = { 'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6, 'Yearly': 12 }; var months = recurring_type_map[this.frm.doc.recurring_type]; if (months) { var to_date = frappe.datetime.add_months(this.frm.doc.from_date, months); this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1); refresh_field('to_date'); } } }, set_gross_profit: function set_gross_profit(item) { if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) { var rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1); item.gross_profit = flt((rate - item.valuation_rate) * item.stock_qty, precision("amount", item)); } }, setup_item_selector: function setup_item_selector() { return; }, get_advances: function get_advances() { if (!this.frm.is_return) { return this.frm.call({ method: "set_advances", doc: this.frm.doc, callback: function callback(r, rt) { refresh_field("advances"); } }); } }, make_payment_entry: function make_payment_entry() { return frappe.call({ method: cur_frm.cscript.get_method_for_payment(), args: { "dt": cur_frm.doc.doctype, "dn": cur_frm.doc.name }, callback: function callback(r) { var doclist = frappe.model.sync(r.message); frappe.set_route("Form", doclist[0].doctype, doclist[0].name); } }); }, get_method_for_payment: function get_method_for_payment() { var method = "erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry"; if (cur_frm.doc.__onload && cur_frm.doc.__onload.make_payment_via_journal_entry) { if (in_list(['Sales Invoice', 'Purchase Invoice'], cur_frm.doc.doctype)) { method = "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_invoice"; } else { method = "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_order"; } } return method; }, set_query_for_batch: function set_query_for_batch(doc, cdt, cdn) { var me = this; var item = frappe.get_doc(cdt, cdn); if (!item.item_code) { frappe.throw(__("Please enter Item Code to get batch no")); } else if (doc.doctype == "Purchase Receipt" || doc.doctype == "Purchase Invoice" && doc.update_stock) { return { filters: { 'item': item.item_code } }; } else { filters = { 'item_code': item.item_code, 'posting_date': me.frm.doc.posting_date || frappe.datetime.nowdate() }; if (item.warehouse) filters["warehouse"] = item.warehouse; return { query: "erpnext.controllers.queries.get_batch_no", filters: filters }; } } }); erpnext.show_serial_batch_selector = function (frm, d) { frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function () { new erpnext.SerialNoBatchSelector({ frm: frm, item: d, warehouse_details: { type: "Warehouse", name: d.warehouse } }); }); };frappe.templates['pos'] = '{{ __("Paid") }}
{{ __("Outstanding") }}
{{ __("Change") }}
{{ __("Write off") }}
| Student | Student Name | {% for c in criteria %}{{ c.assessment_criteria }} | {% endfor %}Total Marks |
|---|---|---|---|
| {{ c.maximum_score }} | {% endfor %}{{max_total_score}} | ||
| {{ s.student }} | {{ s.student_name }} | {% for c in criteria %}{% endfor %} | {% if(s.assessment_details) { %} {{s.assessment_details.total_score}} {% } %} |