Custom Script and Server Script

/*frappe.ui.form.on(“Sales Invoice Item”, {

qty: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt, cdn, “rate”, parseFloat(doc.rate - doc.qty2));

}

});*/

/*

frappe.ui.form.on(“Sales Invoice”, {

after_save : function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

if(frm.doc.customer_address == frm.doc.shipping_address_name){

frappe.model.set_value(cdt,cdn,“print_format_selector”, “SI”, frm.save() ? 0 : 1);

location.reload();

}

}

});

frappe.ui.form.on(“Sales Invoice”, {

after_save : function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

if(frm.doc.customer_address != frm.doc.shipping_address_name){

frappe.model.set_value(cdt,cdn,“print_format_selector”, “Sales Invoice 4”, frm.save() ? 0 : 1);

location.reload();

}

}

});

*/

/*frappe.ui.form.on(“Sales Invoice”, “validate”, function(frm) {

var date = frappe.datetime.add_days(get_today(), -3);

if (frm.doc.posting_date <= date) {

msgprint(__(“You can not select this date”));

validated = false;

}

});

*/

/*

// same as upper script but if condition user wise format

frappe.ui.form.on(“Sales Invoice”, “validate”, function(frm) {

if(frappe.user=="spricstech@gmail.com"){

var date = frappe.datetime.add_days(get_today(), -3);

if (frm.doc.posting_date <= date) {

msgprint(__(“You can not select this date”));

validated = false;

}

}

if(frappe.user=="spricstech@gmail.com"){

var date = frappe.datetime.add_days(get_today(), -3);

if (frm.doc.posting_date <= date) {

msgprint(__(“You can not select this date”));

validated = false;

}

}

});*/

/*

frappe.ui.form.on(‘Sales Invoice’, {

refresh(frm) {

if(frm.doc.customer_address == frm.doc.shipping_address_name){

cur_frm.add_custom_button(__(“SI → Format”), function() {

cur_frm.set_value(“print_format_selector”,“SI”)

cur_frm.print_doc();

frm.save();

});

}

if(frm.doc.customer_address != frm.doc.shipping_address_name){

cur_frm.add_custom_button(__(“Sales Invoice 4 → Format”), function() {

cur_frm.set_value(“print_format_selector”,“Sales Invoice 4”)

cur_frm.print_doc();

frm.save();

});

}

}

});

*/

$.extend(frappe.meta, {

get_print_formats: function(doctype) {

var print_format_list = [“Standard”];

var default_print_format = locals.DocType[doctype].default_print_format;

let enable_raw_printing = frappe.model.get_doc(“:Print Settings”, “Print Settings”).enable_raw_printing;

var print_formats = frappe.get_list(“Print Format”, {doc_type: doctype})

.sort(function(a, b) { return (a > b) ? 1 : -1; });

$.each(print_formats, function(i, d) {

if (

!in_list(print_format_list, d.name)

&& d.print_format_type !== ‘JS’

&& (cint(enable_raw_printing) || !d.raw_printing)

) {

print_format_list.push(d.name);

}

});

if(default_print_format && default_print_format != “Standard”) {

var index = print_format_list.indexOf(default_print_format);

print_format_list.splice(index, 1).sort();

print_format_list.unshift(default_print_format);

}

if(cur_frm.doc.print_format_selector){ //newly added if condition

var print_format = [cur_frm.doc.print_format_selector]

return print_format

}

else{

return print_format_list;

}

},

});

/*

frappe.ui.form.on(‘Sales Invoice’, {

refresh(frm) {

setTimeout(() => {

frm.remove_custom_button(‘Sales Invoice 4 → Format’);

frm.remove_custom_button(‘SI → Format’);

}, 10);

}

})

*/

/*

frappe.ui.form.on(“Sales Invoice”, “refresh”, function(frm){

frm.add_custom_button(“Custom Button → General Ledger”,

function(){

window.location.href = “Frappe Cloud”; //Opening in the same tab:

// var myWin = window.open(‘Frappe Cloud’); //Opening a new tab:

}).addClass(“btn-success”);

});

*/

/*frappe.ui.form.on(“Sales Order Item”,

{

item_code: function(frm, cdt, cdn)

{

var d = locals[cdt][cdn];

console.log("Costo : " + d.costo + " y Item Code: " + d.item_code);

frappe.call(

{

method: “frappe.client.get_value”,

args: {

doctype: “Item Price”,

filters: {

// price_list: frm.doc.mrp_price_list,

price_list: frm.doc.selling_price_list,

item_code: d.item_code

},

fieldname:[“price_list_rate”]

},

callback: function(r)

{

if(r.message)

{

var item_price = r.message;

console.log("discount_amount: " + item_price.price_list_rate);

d.discount_amount = item_price.price_list_rate/100;

}

}

});

}

}

);

*/

/*frappe.ui.form.on(‘Sales Order’, {

refresh: function(frm) {

if(frm.doc.company == ‘SPRICS TECHNOLOGIES PRIVATE LIMITED’) {

frm.set_value(“tc_name”,“DC”);

}

}

});

*/

/*frappe.ui.form.on(“Sales Order Item”, {

qty2: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt, cdn, “rate”, parseFloat(doc.rate - doc.qty2));

}

});

*/

/*

frappe.ui.form.on(‘Sales Order’, {

on_submit: function(frm){

// console.log(‘reloaded doc on submit’)

location.reload()

},

onload_post_render: function(frm){

// var bt = [‘Delivery’, ‘Work Order’, ‘Invoice’, ‘Material Request’, ‘Request for Raw Materials’, ‘Purchase Order’, ‘Payment Request’, ‘Payment’, ‘Project’, ‘Subscription’]

// bt.forEach(function(bt){

// frm.page.remove_inner_button(bt, ‘Create’)

// });

frm.page.add_inner_button(‘Create WO’, () => cur_frm.cscript.make_work_order())

}

}

);*/

/*frappe.ui.form.on(‘Material Request’, {

refresh: function(frm) {

frm.page.sidebar.remove(); // this removes the sidebar

frm.dashboard.wrapper.remove(); // dashboard remove

frm.page.wrapper.find(“.layout-main-section-wrapper”).removeClass(“col-md-10”); // this removes class “col-md-10” from content block, which sets width to 83%

}

});

*/

frappe.ui.form.on(‘Material Request’, {

refresh: function(frm) {

if(frm.doc.material_request_type == “Manufacture” && frm.doc.docstatus==1) {

frm.add_custom_button(__(‘Create WO’), () => {

(frm.doc.items || []).forEach((d) => {

frappe.call({

method: “frappe.client.get”,

args: {

doctype:“Item”,

name:d.item_code

},

callback: function (r) {

// if (r.message.inspection_required_before_delivery) {

frappe.call({

method: “frappe.client.insert”,

args: {

doc: {

doctype: “Work Order”,

production_item: d.item_code,

bom_no: d.default_bom,

// bom_no: frappe.db.get_value(“Item”, item, “variant_of”),

qty: d.qty

},

},

callback: function (r) {

console.log(r);

if(r.message){

frappe.msgprint("WO Created: "+r.message.name);

// frappe.msgprint("WO Created: " + ‘’ +r.message.name + ‘’);

}

}

});

// }

}

});

});

});

}

}

});

frappe.ui.form.on(‘Material Request Item’,{

item_code: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

cur_frm.add_fetch(‘item_code’,‘default_bom’,‘default_bom’);

}

});

frappe.ui.form.on(‘Material Request Item’,{

item_code: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

cur_frm.add_fetch(‘item_code’,‘variant_of’,‘variant_of’);

}

});

frappe.ui.form.on(‘Material Request’, {

refresh(frm) {

setTimeout(() => {

frm.remove_custom_button(‘Work Order’, ‘Create’);

}, 10);

}

})

/*

frappe.ui.form.on(‘Work Order’,{

onload: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt,cdn,“production_item”,“Box-SIZE 24”);

}

});

*/

frappe.ui.form.on(‘Delivery Note’, {

refresh(frm) {

if (frm.doc.docstatus==1) {

setTimeout(() => {

cur_frm.add_custom_button(__(‘Create Invoice’), () => cur_frm.cscript.make_sales_invoice());

}, 10);

}

}

});

frappe.ui.form.on(‘Delivery Note’, {

refresh: function(frm) {

frm.add_custom_button(__(‘Add Quality Inspection’), () => {

(frm.doc.items || []).forEach((d) => {

frappe.call({

method: “frappe.client.get”,

args: {

doctype:“Item”,

name:d.item_code

},

callback: function (r) {

// if (r.message.inspection_required_before_delivery) {

frappe.call({

method: “frappe.client.insert”,

args: {

doc: {

doctype: “Quality Inspection”,

inspection_type: “In Process”,

reference_type: “Delivery Note”,

reference_name: frm.doc.name,

sample_size: 0.0,

item_code: d.item_code,

// description: d.description,

// item_name: d.item_name,

inspected_by: “support@sprics.com”,

status:“Accepted”

// custom_ref:frm.doc.name.concat(d.item_code)

},

},

callback: function (r) {

console.log(r);

if(r.message){

frappe.msgprint(“Quality Insepection Created:”+r.message.name)

}

}

});

// }

}

})

})

});

}

});

frappe.ui.form.on(“Purchase Order”, “onload”, function(frm)

{

if(!frm.doc.__islocal)

if(frm.doc.company==‘SPRICS TECHNOLOGIES PRIVATE LIMITED’)

frm.disable_save();

else

frm.enable_save();

frm.refresh();

}

);

//Back date po not issue

frappe.ui.form.on(“Purchase Order”, “validate”, function(frm) {

var date = frappe.datetime.add_days(get_today(), -4);

if (frm.doc.transaction_date <= date) {

msgprint(__(“You can not select this date”));

validated = false;

}

});

/*frappe.ui.form.on(“Purchase Order”, “validate”, function(frm) {

if (frm.doc.base_discount_amount <= 100) {

frappe.msgprint(__(“You can not over discount”));

validated = false;

}

});*/

frappe.ui.form.on(‘Purchase Order’, function(doc, dt, dn) {

cur_frm.cscript.custom_menu; {

if (doc.docstatus==1) {

cur_frm.clear_custom_buttons();

}

}

});

frappe.ui.form.on(‘Purchase Order Item’, {

s_rate: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt,cdn,“rate”,parseFloat(doc.s_rate)*parseFloat(doc.manufacturer_part_no));

}

});

/* frappe.ui.form.on(‘Purchase Order Item’, {

s_qty: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt,cdn,“qty”,parseFloat(doc.s_qty)*parseFloat(1/(doc.conversion_factor)));

}

}); */

/*

frappe.ui.form.on(“Purchase Order”, “additional_discount_percentage1”, function(frm, cdt, cdn){

var d = locals[cdt][cdn];

frappe.model.set_value(d.doctype, d.name, “additional_discount_amount1”, flt(d.additional_discount_percentage1 * d.grand_total/100));

});*/

/*

//Full Width Screen

frappe.ui.form.on(‘Purchase Order’, {

refresh: function(frm) {

frm.page.sidebar.remove(); // this removes the sidebar

frm.page.wrapper.find(“.layout-main-section-wrapper”).removeClass(“col-md-10”); // this removes class “col-md-10” from content block, which sets width to 83%

}

});*/

/*

frappe.ui.form.on(‘Purchase Order’, {

refresh(frm) {

cur_frm.add_custom_button(__(“PO - SPRICS → Format”), function() {

cur_frm.set_value(“print_format_selector”,“PO - SPRICS”)

cur_frm.print_doc();

frm.save();

});

cur_frm.add_custom_button(__(“PO M → Format”), function() {

cur_frm.set_value(“print_format_selector”,“PO M”)

cur_frm.print_doc();

frm.save();

});

}

})

$.extend(frappe.meta, {

get_print_formats: function(doctype) {

var print_format_list = [“Standard”];

var default_print_format = locals.DocType[doctype].default_print_format;

let enable_raw_printing = frappe.model.get_doc(“:Print Settings”, “Print Settings”).enable_raw_printing;

var print_formats = frappe.get_list(“Print Format”, {doc_type: doctype})

.sort(function(a, b) { return (a > b) ? 1 : -1; });

$.each(print_formats, function(i, d) {

if (

!in_list(print_format_list, d.name)

&& d.print_format_type !== ‘JS’

&& (cint(enable_raw_printing) || !d.raw_printing)

) {

print_format_list.push(d.name);

}

});

if(default_print_format && default_print_format != “Standard”) {

var index = print_format_list.indexOf(default_print_format);

print_format_list.splice(index, 1).sort();

print_format_list.unshift(default_print_format);

}

if(cur_frm.doc.print_format_selector){ //newly added if condition

var print_format = [cur_frm.doc.print_format_selector]

return print_format

}

else{

return print_format_list;

}

},

});

*/

frappe.ui.form.on(‘Purchase Receipt Item’, {

s_rate: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt,cdn,“rate”,parseFloat(doc.s_rate)*parseFloat(doc.manufacturer_part_no));

}

});

frappe.ui.form.on(‘Purchase Invoice Item’, {

s_rate: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt,cdn,“rate”,parseFloat(doc.s_rate)*parseFloat(doc.manufacturer_part_no));

}

});

/*

frappe.ui.form.on(“Purchase Invoice”, “refresh”, function(frm){

frm.add_custom_button(“Stock Ledger Report”, function(){

window.location.href = “Frappe Cloud”;

});

});*/

/*

frappe.ui.form.on(“Purchase Invoice”, “validate”, function(frm) {

if (frm.doc.discount_amount > 100) {

frappe.msgprint(__(“You cannot give more than 100 Rs. discount.”));

validated = false;

}

if (frm.doc.discount_amount < -100) {

frappe.msgprint(__(“You cannot give less than -100 Rs. discount.”));

validated = false;

}

});

*/

/*

frappe.ui.form.on(‘Payment Schedule’, {

payment_term: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

if(doc.payment_term == “50% Advance”){

frappe.model.set_value(cdt,cdn,“pay_amt”,frm.doc.total*0.5);

}

if(doc.payment_term == “30% After Trial”){

frappe.model.set_value(cdt,cdn,“pay_amt”,frm.doc.total*0.3);

}

if(doc.payment_term == “20% after 1 month of Production”){

frappe.model.set_value(cdt,cdn,“pay_amt”,frm.doc.grand_total - (frm.doc.total0.5 + frm.doc.total0.3));

}

}

});*/

frappe.ui.form.on(‘Payment Schedule’, {

invoice_portion: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

if(doc.payment_term == “50% Advance”){

frappe.model.set_value(cdt,cdn,“payment_amount”,frm.doc.total*0.5);

}

if(doc.payment_term == “30% After Trial”){

frappe.model.set_value(cdt,cdn,“payment_amount”,frm.doc.total*0.3);

}

if(doc.payment_term == “20% after 1 month of Production”){

frappe.model.set_value(cdt,cdn,“payment_amount”,frm.doc.grand_total - (frm.doc.total0.5 + frm.doc.total0.3));

}

}

});

frappe.ui.form.on(“Appraisal”, “goals_on_form_rendered”, function(frm, grid_row, cdt, cdn){

var grid_row = cur_frm.open_grid_row();

if(frm.doc.status == “Draft”){

grid_row.grid_form.fields_dict.score.df.read_only = true;

grid_row.grid_form.fields_dict.score.refresh();

refresh(frm.doc.goals);

}

});

frappe.ui.form.on(“Stock Entry”, “mr_no”, function(frm, cdt, cdn) {

var d = locals[cdt][cdn];

frappe.db.get_value(“Material Request”, {“name”: d.mr_no}, “transaction_date”, function(value) {

d.transaction_date = value.transaction_date;

});

});

frappe.ui.form.on(“Stock Entry Detail”, “items_on_form_rendered”, function(frm, grid_row, cdt, cdn){

var grid_row = cur_frm.open_grid_row();

if(frm.doc.workflow_state === “Waiting Verifying”){

grid_row.grid_form.fields_dict.score.df.read_only = true;

grid_row.grid_form.fields_dict.score.refresh();

}

});

frappe.ui.form.on(“Appraisal”, “goals_on_form_rendered”, function(frm, grid_row, cdt, cdn){

var grid_row = cur_frm.open_grid_row();

if(frm.doc.workflow_state === “Waiting Verifying”){

grid_row.grid_form.fields_dict.score.df.read_only = true;

grid_row.grid_form.fields_dict.score.refresh();

}

});

frappe.ui.form.on(‘Stock Entry Detail’, {

refresh(frm) {

// your code here

}

})

/*frappe.ui.form.on(‘Lead’, {

refresh(frm) {

let d = new frappe.ui.Dialog({

title: ‘Enter details’,

fields: [

{

label: ‘Person Name’,

fieldname: ‘lead_name’,

fieldtype: ‘Data’

},

{

label: ‘Organization Name’,

fieldname: ‘company_name’,

fieldtype: ‘Data’

}

],

primary_action_label: ‘Submit’,

primary_action(values) {

console.log(values);

d.hide();

}

});

d.show();

}

});*/

/*frappe.ui.form.on(‘Lead’, {

refresh(frm) {

cur_frm.add_fetch(“sales_partner”,“primary_address”,“address”)

}

});*/

frappe.ui.form.on(“Lead”, “to_ship_address”, function(frm, cdt, cdn) {

if(frm.doc.to_ship_address){

return frm.call({

method: “frappe.contacts.doctype.address.address.get_address_display”,

args: {

“address_dict”: frm.doc.to_ship_address

},

callback: function(r) {

if(r.message)

frm.set_value(“to_ship_address_display”, r.message);

}

});

}

else{

frm.set_value(“to_ship_address_display”, “”);

}

});

frappe.ui.form.on(‘Timesheet Detail’, {

log: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

if(doc.activity_type == ‘Normal’){

frappe.model.set_value(cdt,cdn,“hours”,parseFloat(doc.log)*1);

}

if(doc.activity_type == ‘Overtime’){

frappe.model.set_value(cdt,cdn,“hours”,parseFloat(doc.log)*1.25);

}

}

});

/*

frappe.ui.form.on(‘Item’, {

refresh(frm) {

cur_frm.fields_dict[‘quality’].get_query = function(doc) {

doc = cur_frm.doc;

var cond = ‘’;

if(doc.base_material!=“” && doc.is_rm!=“”){

cond = ‘tabQuality.base_material = "’+doc.base_material+

‘" and tabQuality.is_rm = "’+doc.is_rm+‘" and’ ;

}

else if (doc.is_rm == “” && doc.base_material != “”){

cond = ‘tabQuality.base_material = "’+doc.base_material+

‘" and tabQuality.is_rm = “No” and’ ;

}

else if (doc.is_rm != “” && doc.base_material == “”){

cond = ‘tabQuality.is_rm = "’+doc.is_rm+‘" and’ ;

}

return repl('SELECT DISTINCT tabQuality.name, base_material, is_rm, code \

FROM tabQuality WHERE %(cond)s tabQuality.%(key)s LIKE “%s” \

ORDER BY tabQuality.name DESC LIMIT 50’,

{company:doc.company,cond:cond})

}

}

})

*/

frappe.ui.form.on(‘Sales Order’, {

refresh(frm) {

cur_frm.cscript.customer = function(doc) {

return frappe.call({

method: “erpnext.accounts.utils.get_balance_on”,

args: {date: doc.posting_date, party_type: ‘Customer’, party: doc.customer},

callback: function(r) {

doc.outstanding_balance = format_currency(r.message, erpnext.get_currency(doc.company));

refresh_field(‘outstanding_balance’, ‘accounts’);

}

});

}

}

})

/*frappe.ui.form.on(“Sales Invoice Item”, “item_code”, function(frm, cdt, cdn) {

var d = locals[cdt][cdn];

frappe.db.get_value(“Item”, {“name”: d.item_code}, “qty_in_box”, function(value) {

d.qty_in_box = d.qty / value.qty_in_box;

});

});

frappe.ui.form.on(“Sales Invoice Item”, “qty”, function(frm, cdt, cdn) {

var d = locals[cdt][cdn];

frappe.db.get_value(“Item”, {“name”: d.item_code}, “qty_in_box”, function(value) {

d.qty_in_box = d.qty / value.qty_in_box;

});

});

frappe.ui.form.on(“Sales Invoice Item”, “item_code”, function(frm, cdt, cdn) {

var d = locals[cdt][cdn];

frappe.db.get_value(“Item”, {“name”: d.item_code}, “qty_in_case”, function(value) {

d.qty_in_case = d.qty / value.qty_in_case;

});

});

frappe.ui.form.on(“Sales Invoice Item”, “qty”, function(frm, cdt, cdn) {

var d = locals[cdt][cdn];

frappe.db.get_value(“Item”, {“name”: d.item_code}, “qty_in_case”, function(value) {

d.qty_in_case = d.qty / value.qty_in_case;

});

});

frappe.ui.form.on(“Sales Invoice Item”, “item_code”, function(frm, cdt, cdn) {

var d = locals[cdt][cdn];

frappe.db.get_value(“Item”, {“name”: d.item_code}, “qty_in_case”, function(value) {

d.packs_shipper = value.qty_in_case / d.units_pack;

});

});

/*

frappe.ui.form.on(“Sales Invoice Item”, “form_render”, function(frm, cdt, cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt, cdn, ‘cbm’,parseFloat(doc.length * doc.breadth * doc.height) / 1000000);

});

frappe.ui.form.on(“Sales Invoice Item”, “form_render”, function(frm, cdt, cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt, cdn, ‘total_cbm’, parseFloat(doc.cbm * doc.qty_in_case));

});

frappe.ui.form.on(“Sales Invoice Item”, “form_render”, function(frm, cdt, cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt, cdn, ‘total_net_wt’, parseFloat(doc.net_wt_per_shipper * doc.qty_in_case));

});

frappe.ui.form.on(“Sales Invoice Item”, “form_render”, function(frm, cdt, cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt, cdn, ‘total_gross_wt’, parseFloat(doc.gross_wt_per_shipper * doc.qty_in_case));

});

*/

/*

frappe.ui.form.on(“Sales Invoice Item”, “validate”, function(frm, cdt, cdn) {

var doc = locals[cdt][cdn];

frappe.model.set_value(cdt, cdn, ‘total_cbm’, parseFloat((doc.qty / doc.total_qty_per_shipper) * ((doc.length * doc.breadth * doc.height) / 1000000)));

});*/

frappe.ui.form.on(‘Sales Invoice Item’,{

free_item: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

if(doc.free_item == 1){

frappe.model.set_value(cdt, cdn, ‘price_list_rate’, 0);

}

}

});

frappe.ui.form.on(‘Sales Invoice’,{

before_save: function(frm,cdt,cdn) {

var doc = locals[cdt][cdn];

if(!frm.doc.date_removal){

frappe.model.set_value(cdt, cdn, ‘date_removal’, doc.posting_date);

}

}

});

frappe.ui.form.on(“Sales Invoice”, “validate”, function(frm, cdt, cdn) {

$.each(frm.doc.items || [], function(i, d) {

if(d.rate <= 0 && d.free_item != 1) {

frappe.msgprint(‘’+d.item_code+‘’ +" Rate is 0");

validated = false;

}

})

});

frappe.ui.form.on(“Sales Invoice”, “notify_party_address”, function(frm, cdt, cdn) {

if(frm.doc.notify_party_address){

return frm.call({

method: “frappe.contacts.doctype.address.address.get_address_display”,

args: {

“address_dict”: frm.doc.notify_party_address

},

callback: function(r) {

if(r.message)

frm.set_value(“notify_address”, r.message);

}

});

}

else{

frm.set_value(“notify_address”, “”);

}

});

frappe.ui.form.on(“Sales Invoice”, {

setup: function(frm) {

frm.set_query(“notify_party_address”, function()

{

return {

filters: [

[“Dynamic Link”,“link_doctype”, “=”, “Customer”],

[“Dynamic Link”,“link_name”, “=”, frm.doc.customer]

]

}

});

}

});

frappe.ui.form.on(“Sales Invoice”, “bank_account_address”, function(frm, cdt, cdn) {

if(frm.doc.bank_account_address){

return frm.call({

method: “frappe.contacts.doctype.address.address.get_address_display”,

args: {

“address_dict”: frm.doc.bank_account_address

},

callback: function(r) {

if(r.message)

frm.set_value(“bank_address”, r.message);

}

});

}

else{

frm.set_value(“bank_address”, “”);

}

});

frappe.ui.form.on(“Sales Invoice”, {

setup: function(frm) {

frm.set_query(“bank_account_address”, function()

{

return {

filters: [

[“Dynamic Link”,“link_doctype”, “=”, “Bank Account”]

/*,

[“Dynamic Link”,“link_name”, “=”, frm.doc.customer]*/

]

}

});

}

});

frappe.ui.form.on(“Sales Invoice Item”,

{

item_code: function(frm, cdt, cdn)

{

var d = locals[cdt][cdn];

console.log("Costo : " + d.costo + " y Item Code: " + d.item_code);

frappe.call(

{

method: “frappe.client.get_value”,

args: {

doctype: “Item Price”,

filters: {

// price_list: frm.doc.mrp_price_list,

price_list: “MRP”,

item_code: d.item_code

},

fieldname:[“price_list_rate”]

},

callback: function(r)

{

if(r.message)

{

var item_price = r.message;

console.log("mrp: " + item_price.price_list_rate);

d.mrp = item_price.price_list_rate;

}

}

});

}

}

);

<----------------------Server Script ---------------------->

if not(doc.shipping_city):
doc.shipping_city=doc.customer_city

if (doc.naming_series[-10:-6] != (str(doc.fiscal_year[2:4])+str(doc.fiscal_year[7:9]))):
frappe.throw(_(“CHANGE SEIRES -Not Match With Financial Year”))

invo = doc.name

if (len(str(doc.inv1)) > 4):
frappe.throw(_(“Invalid Number - Enter upto 4 Digit Number”))

if not(doc.inv1):
if invo[-2] == “-” :
doc.invoice_no = invo[0:len(invo)-12] +“-” + str(invo[-6:-2]).zfill(4)
else:
doc.invoice_no = invo[0:len(invo)-10] +“-” + str(invo[-4:]).zfill(4)
else:
if invo[-2] == “-” :
doc.invoice_no = invo[0:len(invo)-12] +“-” + str(doc.inv1).zfill(4)
else:
doc.invoice_no = invo[0:len(invo)-10] +“-” + str(doc.inv1).zfill(4)
pi = frappe.db.sql(‘’‘select name from tabSales Invoice
where
invoice_no = %(invoice_no)s
and name != %(name)s
and fiscal_year = %(fiscal_year)s
and docstatus < 2’‘’, {
“invoice_no”: doc.invoice_no,
“name”: doc.name,
“fiscal_year”: doc.fiscal_year
})
if pi:
pi = pi[0][0]
frappe.throw(_(“Duplicate Number”))

for d in doc.get(‘items’):
if(d.price_list_rate != 0):
d.gross_amount = d.qty * d.price_list_rate
if(d.gross_amount != 0):
d.discount_amount_total = (d.gross_amount * d.discount_percentage)/100
if(d.item_tax_template == “CGST AND SGST 18 %”):
d.gst_amount = d.amount * 0.18
if(d.item_tax_template == “CGST AND SGST 28 %”):
d.gst_amount = d.amount * 0.28
if(d.item_tax_template == “CGST AND SGST 12 %”):
d.gst_amount = d.amount * 0.12
if(d.item_tax_template == “CGST AND SGST 5 %”):
d.gst_amount = d.amount * 0.05
if(d.item_tax_template == “CGST AND SGST 0.1 %”):
d.gst_amount = d.amount * 0.001
if(d.item_tax_template == “CGST AND SGST 0 %”):
d.gst_amount = d.amount * 0
if(d.item_tax_template == “IGST 18 %”):
d.gst_amount = d.amount * 0.18
if(d.item_tax_template == “IGST 28 %”):
d.gst_amount = d.amount * 0.28
if(d.item_tax_template == “IGST 12 %”):
d.gst_amount = d.amount * 0.12
if(d.item_tax_template == “IGST 5 %”):
d.gst_amount = d.amount * 0.05
if(d.item_tax_template == “IGST 0.1 %”):
d.gst_amount = d.amount * 0.001
if(d.item_tax_template == “IGST 0 %”):
d.gst_amount = d.amount * 0
d.net_amount_total = d.amount + d.gst_amount
if(d.total_qty_per_shipper != 0):
d.qty_in_case = d.qty / d.total_qty_per_shipper
d.qty_in_box = d.qty / d.units_pack
d.packs_shipper = d.total_qty_per_shipper / d.units_pack
d.cbm = (d.length * d.breadth * d.height) / 1000000
d.total_cbm = d.cbm * d.qty_in_case
d.total_net_wt = d.net_wt_per_shipper * d.qty_in_case
d.total_gross_wt = d.gross_wt_per_shipper * d.qty_in_case
d.case_wt = d.weight_per_unit * d.qty_in_case

for t in doc.get(‘taxes’):
if(t.account_head == “CGST - PC”):
doc.invoice_amount_with_tax = doc.total + (t.tax_amount + t.tax_amount)
if(t.account_head == “IGST - PC”):
doc.invoice_amount_with_tax = doc.total + t.tax_amount
if(doc.total_taxes_and_charges == 0):
doc.invoice_amount_with_tax = doc.total
if(doc.tax_category == “Export”):
doc.invoice_amount_with_tax = doc.total

/BOM/
doc.total_qty = (frappe.db.sql(‘’‘select
sum(bi.qty)
from tabBOM as b, tabBOM Item as bi
where bi.parent = b.name
and b.name = %(name)s
group by b.name’‘’, {
“name”: doc.name})[0][0])

5 Likes