Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ITC-04): create necessary fields to update original reference (backport #2479) #2524

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions india_compliance/gst_india/client_scripts/stock_entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
frappe.provide("india_compliance");
DOCTYPE = "Stock Entry";

setup_e_waybill_actions(DOCTYPE);

frappe.ui.form.on(DOCTYPE, {
setup(frm) {
frm.set_query("taxes_and_charges", {
filters: [
["disabled", "=", 0],
["company", "=", frm.doc.company],
],
});

frm.set_query("transporter", {
filters: [
["disabled", "=", 0],
["is_transporter", "=", 1],
],
});

["ship_from_address", "ship_to_address"].forEach(field => {
frm.set_query(field, { filters: { country: "India", disabled: 0 } });
});

set_address_display_events();

frm.set_query("link_doctype", "doc_references", {
name: ["=", "Stock Entry"],
});

frm.set_query("link_name", "doc_references", function (doc) {
return {
filters: get_filters_for_relevant_stock_entries(doc),
};
});
},

onload(frm) {
frm.taxes_controller = new india_compliance.taxes_controller(frm, {
total_taxable_value: "total_taxable_value",
});

on_change_set_address(
frm,
"supplier_address",
"bill_to_address",
__("Bill To (same as Supplier Address)"),
__("Bill To")
);
},

refresh() {
if (!gst_settings.enable_e_waybill || !gst_settings.enable_e_waybill_for_sc)
return;

show_sandbox_mode_indicator();
},

after_save(frm) {
if (is_e_waybill_applicable(frm) && !is_e_waybill_generatable(frm))
frappe.show_alert(
{
message: __("Supplier Address is required to create e-Waybill"),
indicator: "yellow",
},
10
);
},

supplier_address(frm) {
on_change_set_address(
frm,
"supplier_address",
"bill_to_address",
__("Bill To (same as Supplier Address)"),
__("Bill To")
);
},

source_warehouse_address(frm) {
on_change_set_address(
frm,
"source_warehouse_address",
"ship_from_address",
__("Ship From (same as Source Warehouse Address)"),
__("Ship From")
);
},

target_warehouse_address(frm) {
on_change_set_address(
frm,
"target_warehouse_address",
"ship_to_address",
__("Ship To (same as Target Warehouse Address)"),
__("Ship To")
);
},

company(frm) {
if (frm.doc.company) {
frappe.call({
method: "frappe.contacts.doctype.address.address.get_default_address",
args: {
doctype: "Company",
name: frm.doc.company,
},
callback(r) {
frm.set_value("bill_from_address", r.message);
},
});
}
},

taxes_and_charges(frm) {
frm.taxes_controller.update_taxes(frm);
},

async fetch_original_doc_ref(frm) {
let existing_references = frm.doc.doc_references.map(row => row.link_name);

data = await frappe.db.get_list(DOCTYPE, {
filters: get_filters_for_relevant_stock_entries(frm.doc),
group_by: "name",
});

data.forEach(doc => {
if (existing_references.includes(doc.name)) return;
var row = frm.add_child("doc_references");
row.link_doctype = DOCTYPE;
row.link_name = doc.name;
});

frm.refresh_field("doc_references");
},
});

function set_address_display_events() {
const event_fields = [
"bill_from_address",
"bill_to_address",
"ship_from_address",
"ship_to_address",
];

const events = Object.fromEntries(
event_fields.map(field => [
field,
frm => {
erpnext.utils.get_address_display(
frm,
field,
field + "_display",
false
);
},
])
);

frappe.ui.form.on(DOCTYPE, events);
}

function on_change_set_address(frm, source_field, target_field, label1, label2) {
if (frm.doc.docstatus > 0) return;
let read_only;
let value = frm.doc[source_field];
if (value) {
frm.set_value(target_field, value);
read_only = 1;
} else {
read_only = 0;
}

frm.set_df_property(target_field, "read_only", read_only);
frm.set_df_property(target_field, "label", read_only ? label1 : label2);
}

frappe.ui.form.on("Stock Entry Detail", india_compliance.taxes_controller_events);

function get_filters_for_relevant_stock_entries(doc) {
return [
["docstatus", "=", 1],
["purpose", "=", "Send to Subcontractor"],
["subcontracting_order", "=", doc.subcontracting_order],
["Stock Entry Detail", "item_code", "in", get_items(doc)],
];
}

function get_items(doc) {
return Array.from(new Set(doc.items.map(row => row.item_code)));
}
155 changes: 155 additions & 0 deletions india_compliance/gst_india/client_scripts/subcontracting_receipt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
DOCTYPE = "Subcontracting Receipt";
setup_e_waybill_actions(DOCTYPE);

frappe.ui.form.on(DOCTYPE, {
setup(frm) {
frm.set_query("taxes_and_charges", {
filters: [
["disabled", "=", 0],
["company", "=", frm.doc.company],
],
});

frm.set_query("transporter", {
filters: [
["disabled", "=", 0],
["is_transporter", "=", 1],
],
});

["supplier_address", "shipping_address"].forEach(field => {
frm.set_query(field, { filters: { country: "India", disabled: 0 } });
});

frm.set_query("link_doctype", "doc_references", {
filters: {
name: ["in", ["Subcontracting Receipt", "Stock Entry"]],
},
});

frm.set_query("link_name", "doc_references", function (doc, cdt, cdn) {
const row = locals[cdt][cdn];
const subcontracting_orders = get_subcontracting_orders(doc);

// same as backend query in get_relevant_references
if (row.link_doctype == "Stock Entry") {
const supplied_items = get_supplied_items(doc);
return {
filters: [
["docstatus", "=", 1],
["purpose", "=", "Send to Subcontractor"],
["subcontracting_order", "in", subcontracting_orders],
["supplier", "=", doc.supplier],
["Stock Entry Detail", "item_code", "in", supplied_items],
],
};
} else if (row.link_doctype == "Subcontracting Receipt") {
const received_items = get_received_items(doc);
return {
filters: [
["docstatus", "=", 1],
["is_return", "=", 1],
["supplier", "=", doc.supplier],
[
"Subcontracting Receipt Item",
"item_code",
"in",
received_items,
],
[
"Subcontracting Receipt Item",
"subcontracting_order",
"in",
subcontracting_orders,
],
],
};
}
});
},
onload(frm) {
frm.taxes_controller = new india_compliance.taxes_controller(frm, {
total_taxable_value: "total",
});
},

refresh() {
if (!gst_settings.enable_e_waybill || !gst_settings.enable_e_waybill_for_sc)
return;

show_sandbox_mode_indicator();
},

after_save(frm) {
if (is_e_waybill_applicable(frm) && !is_e_waybill_generatable(frm))
frappe.show_alert(
{
message: __("Supplier Address is required to create e-Waybill"),
indicator: "yellow",
},
10
);
},

fetch_original_doc_ref(frm) {
let existing_references = get_existing_references(frm);

frappe.call({
method: "india_compliance.gst_india.overrides.subcontracting_transaction.get_relevant_references",
args: {
supplier: frm.doc.supplier,
supplied_items: get_supplied_items(frm.doc),
received_items: get_received_items(frm.doc),
subcontracting_orders: get_subcontracting_orders(frm.doc),
},
callback: function (r) {
if (!r.message) return;

Object.entries(r.message).forEach(([doctype, docnames]) => {
docnames.forEach(docname => {
if (existing_references[doctype]?.includes(docname)) return;

let row = frm.add_child("doc_references");
row.link_doctype = doctype;
row.link_name = docname;
});
});

frm.refresh_field("doc_references");
},
});
},

taxes_and_charges(frm) {
frm.taxes_controller.update_taxes(frm);
},
});

frappe.ui.form.on(
"Subcontracting Receipt Item",
india_compliance.taxes_controller_events
);

function get_existing_references(frm) {
let existing_references = {};

frm.doc.doc_references.forEach(row => {
if (!existing_references[row.link_doctype])
existing_references[row.link_doctype] = [];
existing_references[row.link_doctype].push(row.link_name);
});

return existing_references;
}

function get_supplied_items(doc) {
return Array.from(new Set(doc.supplied_items.map(row => row.rm_item_code)));
}

function get_received_items(doc) {
return Array.from(new Set(doc.items.map(row => row.item_code)));
}

function get_subcontracting_orders(doc) {
return Array.from(new Set(doc.items.map(row => row.subcontracting_order)));
}
Loading
Loading