Skip to content

Commit

Permalink
feat(ITC-04): create necessary fields to update original reference (#…
Browse files Browse the repository at this point in the history
…2479)

* fix: fields required for itc_04 workflow setup

* fix: child doctype for reference document

* fix: dialog and query for selection of original document reference

* fix: redundant doctype

* fix: query for references

* fix: removed dialog and query shifted to backend

* fix: added validations

* fix: also validate if purpose is material_transfer

* fix better filters for queries and refactor

* fix: improved queries

* fix: only consider specific conditions

* fix: increment patch

* fix: changes as per review for stock entry

* fix: correct condition for taxes and references

---------

Co-authored-by: Smit Vora <[email protected]>
  • Loading branch information
Ninad1306 and vorasmit authored Aug 5, 2024
1 parent 63e5c99 commit a0108de
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 42 deletions.
71 changes: 53 additions & 18 deletions india_compliance/gst_india/client_scripts/stock_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@ setup_e_waybill_actions(DOCTYPE);

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

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

["ship_from_address", "ship_to_address"].forEach(field => {
frm.set_query(field, function () {
return { filters: { country: "India", disabled: 0 } };
});
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) {
Expand All @@ -46,7 +50,7 @@ frappe.ui.form.on(DOCTYPE, {
);
},

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

Expand Down Expand Up @@ -112,6 +116,24 @@ frappe.ui.form.on(DOCTYPE, {
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() {
Expand Down Expand Up @@ -155,3 +177,16 @@ function on_change_set_address(frm, source_field, target_field, 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)));
}
135 changes: 115 additions & 20 deletions india_compliance/gst_india/client_scripts/subcontracting_receipt.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,70 @@
setup_e_waybill_actions("Subcontracting Receipt");
DOCTYPE = "Subcontracting Receipt";
setup_e_waybill_actions(DOCTYPE);

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

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

["supplier_address", "shipping_address"].forEach(field => {
frm.set_query(field, function () {
return { filters: { country: "India", disabled: 0 } };
});
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) {
Expand All @@ -32,9 +73,10 @@ frappe.ui.form.on("Subcontracting Receipt", {
});
},

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

show_sandbox_mode_indicator();
},

Expand All @@ -49,6 +91,35 @@ frappe.ui.form.on("Subcontracting Receipt", {
);
},

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);
},
Expand All @@ -58,3 +129,27 @@ 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)));
}
42 changes: 41 additions & 1 deletion india_compliance/gst_india/constants/custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@
"insert_after": "total",
"hide_border": 1,
},
{
"fieldname": "section_break_ref_doc",
"label": "References",
"fieldtype": "Section Break",
"insert_after": "bill_date",
"depends_on": "eval:doc.is_return !== 1",
},
{
"fieldname": "fetch_original_doc_ref",
"label": "Fetch Original Document Reference",
"fieldtype": "Button",
"insert_after": "section_break_ref_doc",
},
{
"fieldname": "doc_references",
"label": "Original Document References",
"fieldtype": "Table",
"insert_after": "fetch_original_doc_ref",
"options": "Dynamic Link",
},
],
("Subcontracting Order", "Subcontracting Receipt"): [
{
Expand Down Expand Up @@ -161,7 +181,7 @@
"label": "Taxes",
"fieldtype": "Section Break",
"insert_after": "get_stock_and_rate",
"depends_on": "eval:doc.purpose === 'Send to Subcontractor'",
"depends_on": "eval:doc.subcontracting_order",
},
{
"label": "E-Waybill Info",
Expand Down Expand Up @@ -312,6 +332,26 @@
"read_only": 1,
"is_virtual": 1,
},
{
"fieldname": "section_break_ref_doc",
"label": "References",
"fieldtype": "Section Break",
"insert_after": "value_difference",
"depends_on": "eval:doc.purpose === 'Material Transfer' && doc.subcontracting_order",
},
{
"fieldname": "fetch_original_doc_ref",
"label": "Fetch Original Document Reference",
"fieldtype": "Button",
"insert_after": "section_break_ref_doc",
},
{
"fieldname": "doc_references",
"label": "Original Document References",
"fieldtype": "Table",
"insert_after": "fetch_original_doc_ref",
"options": "Dynamic Link",
},
],
"Company": [
{
Expand Down
Loading

0 comments on commit a0108de

Please sign in to comment.