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

fix: Improved Data Flow in Purchase Reconciliation Tool #2953

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ def get_consolidated_data(
def get_manually_matched_data(self, purchase_name: str, inward_supply_name: str):
"""
Get manually matched data for given purchase invoice and inward supply.
This can be used to show comparision of matched values.
This can be used to show comparison of matched values.
"""
inward_supplies = self.get_all_inward_supply(
names=[inward_supply_name], only_names=True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,92 +76,45 @@ frappe.ui.form.on(DOCTYPE, {
},

onload(frm) {
if (frm.doc.is_modified) frm.doc.reconciliation_data = null;
add_gstr2b_alert(frm);

frm.trigger("purchase_period");
frm.trigger("inward_supply_period");
},

refresh(frm) {
this.reco_tool_actions = new PurchaseReconciliationToolAction(frm);

this.reco_tool_actions.setup_primary_actions();
this.reco_tool_actions.setup_custom_buttons();
},

async company(frm) {
render_empty_state(frm);
if (!frm.doc.company) return;
const options = await india_compliance.set_gstin_options(frm, true);

if (!frm.doc.company_gstin) frm.set_value("company_gstin", options[0]);
},

refresh(frm) {
// Primary Action
frm.disable_save();
frm.page.set_primary_action(__("Reconcile"), () => {
if (!frm.doc.company && !frm.doc.company_gstin) {
frappe.throw(
__("Please provide either a Company name or Company GSTIN.")
);
}
frm.save();
});

const action_group = __("Actions");

// add custom buttons
api_enabled
? frm.add_custom_button(__("Download 2A/2B"), () => new ImportDialog(frm))
: frm.add_custom_button(
__("Upload 2A/2B"),
() => new ImportDialog(frm, false)
);

if (!frm.purchase_reconciliation_tool?.data?.length) return;
if (frm.get_active_tab()?.df.fieldname == "invoice_tab") {
frm.add_custom_button(
__("Unlink"),
() => unlink_documents(frm),
action_group
);
frm.add_custom_button(__("dropdown-divider"), () => {}, action_group);
}
["Accept", "Pending", "Ignore"].forEach(action =>
frm.add_custom_button(
__(action),
() => apply_action(frm, action),
action_group
)
);
frm.$wrapper
.find("[data-label='dropdown-divider']")
.addClass("dropdown-divider");

// Export button
frm.add_custom_button(__("Export"), () =>
frm.purchase_reconciliation_tool.export_data()
async company_gstin(frm) {
render_empty_state(frm);
await fetch_date_range(
frm,
"inward_supply",
"get_date_range_and_check_missing_documents"
);

// move actions button next to filters
for (const group_div of $(".custom-actions .inner-group-button")) {
const btn_label = group_div.querySelector("button").innerText?.trim();
if (btn_label != action_group) continue;

$(".custom-button-group .inner-group-button").remove();

// to hide `Actions` button group on smaller screens
$(group_div).addClass("hidden-md");

$(group_div).appendTo($(".custom-button-group"));
}
},

before_save(frm) {
frm.doc.__unsaved = true;
frm.doc.reconciliation_data = null;
add_gstr2b_alert(frm);
},

async purchase_period(frm) {
render_empty_state(frm);
await fetch_date_range(frm, "purchase");
set_date_range_description(frm, "purchase");
},

async inward_supply_period(frm) {
render_empty_state(frm);
await fetch_date_range(
frm,
"inward_supply",
Expand All @@ -171,20 +124,9 @@ frappe.ui.form.on(DOCTYPE, {
add_gstr2b_alert(frm);
},

async company_gstin(frm) {
await fetch_date_range(
frm,
"inward_supply",
"get_date_range_and_check_missing_documents"
);
add_gstr2b_alert(frm);
},
gst_return: render_empty_state,

after_save(frm) {
frm.purchase_reconciliation_tool.refresh(
frm.doc.reconciliation_data ? JSON.parse(frm.doc.reconciliation_data) : []
);
},
include_ignored: render_empty_state,

show_progress(frm, type) {
if (type == "download") {
Expand Down Expand Up @@ -246,19 +188,25 @@ class PurchaseReconciliationTool {
this.init(frm);
this.render_tab_group();
this.setup_filter_button();
this.render_data_tables();
}

init(frm) {
this.frm = frm;
this.data = frm.doc.reconciliation_data
? JSON.parse(frm.doc.reconciliation_data)
: [];
this.filtered_data = this.data;
this.data = [];
this.frm.doc.is_data_loaded = false;
this.$wrapper = this.frm.get_field("reconciliation_html").$wrapper;
this._tabs = ["invoice", "supplier", "summary"];
}

generate_data() {
this.data = this.frm.doc.reconciliation_data;
this.filtered_data = this.frm.doc.reconciliation_data;

// clear filters
this.filter_group.filter_x_button.click();
this.render_data_tables();
}

refresh(data) {
if (data) {
this.data = data;
Expand Down Expand Up @@ -464,7 +412,7 @@ class PurchaseReconciliationTool {
const row = me.tabs.supplier_tab.datatable.data.find(
r => r.supplier_gstin === $(this).attr("data-name")
);
me.export_data(row);
reco_tool_actions.export_data(row);
});

this.tabs.supplier_tab.datatable.$datatable.on("click", ".btn.envelope", function (e) {
Expand Down Expand Up @@ -506,20 +454,6 @@ class PurchaseReconciliationTool {
});
}

export_data(selected_row) {
this.data_to_export = this.get_filtered_data(selected_row);
if (selected_row) delete this.data_to_export.supplier_summary;

const url =
"india_compliance.gst_india.doctype.purchase_reconciliation_tool.purchase_reconciliation_tool.download_excel_report";

open_url_post(`/api/method/${url}`, {
data: JSON.stringify(this.data_to_export),
doc: JSON.stringify(this.frm.doc),
is_supplier_specific: !!selected_row,
});
}

get_filtered_data(selected_row = null) {
let supplier_filter = null;

Expand Down Expand Up @@ -818,6 +752,107 @@ class PurchaseReconciliationTool {
}
}

class PurchaseReconciliationToolAction {
constructor(frm) {
this.frm = frm;
}

setup_primary_actions() {
// Primary Action
this.frm.disable_save();
this.frm.page.set_primary_action(__("Generate"), async () => {
if (!this.frm.doc.company && !this.frm.doc.company_gstin) {
frappe.throw(
__("Please provide either a Company name or Company GSTIN.")
);
}

this.get_reconciliation_data(this.frm);
});

// Download Button
api_enabled
? this.frm.add_custom_button(
__("Download 2A/2B"),
() => new ImportDialog(frm)
)
: this.frm.add_custom_button(
__("Upload 2A/2B"),
() => new ImportDialog(this.frm, false)
);

// Export button
this.frm.add_custom_button(__("Export"), () => this.export_data());
}

setup_custom_buttons() {
const action_group = __("Actions");

if (!this.frm.purchase_reconciliation_tool?.data?.length) return;
if (this.frm.get_active_tab()?.df.fieldname == "invoice_tab") {
this.frm.add_custom_button(
__("Unlink"),
() => unlink_documents(this.frm),
action_group
);
this.frm.add_custom_button(__("dropdown-divider"), () => {}, action_group);
}

// Setup Actions
["Accept", "Pending", "Ignore"].forEach(action =>
this.frm.add_custom_button(
__(action),
() => apply_action(this.frm, action),
action_group
)
);

// Add Dropdown Divider to differentiate between Actions
this.frm.$wrapper
.find("[data-label='dropdown-divider']")
.addClass("dropdown-divider");

// move actions button next to filters
for (const group_div of $(".custom-actions .inner-group-button")) {
const btn_label = group_div.querySelector("button").innerText?.trim();
if (btn_label != action_group) continue;

$(".custom-button-group .inner-group-button").remove();

// to hide `Actions` button group on smaller screens
$(group_div).addClass("hidden-md");

$(group_div).appendTo($(".custom-button-group"));
}
}

async get_reconciliation_data(frm) {
const { message } = await frm.call("reconcile_and_generate_data");
frm.doc.reconciliation_data = message;

frm.purchase_reconciliation_tool.generate_data();
frm.doc.is_data_loaded = true;

// Toggle HTML fields
frm.refresh();
}

export_data(selected_row) {
this.data_to_export =
this.frm.purchase_reconciliation_tool.get_filtered_data(selected_row);
if (selected_row) delete this.data_to_export.supplier_summary;

const url =
"india_compliance.gst_india.doctype.purchase_reconciliation_tool.purchase_reconciliation_tool.download_excel_report";

open_url_post(`/api/method/${url}`, {
data: JSON.stringify(this.data_to_export),
doc: JSON.stringify(this.frm.doc),
is_supplier_specific: !!selected_row,
});
}
}

class DetailViewDialog {
table_fields = [
"name",
Expand Down Expand Up @@ -1106,7 +1141,7 @@ class DetailViewDialog {
const detail_table = this.dialog.fields_dict.detail_table;

detail_table.html(
frappe.render_template("purchase_detail_comparision", {
frappe.render_template("purchase_detail_comparison", {
purchase: this.data._purchase_invoice,
inward_supply: this.data._inward_supply,
})
Expand Down Expand Up @@ -1854,3 +1889,10 @@ async function create_new_purchase_invoice(row, company, company_gstin) {

frappe.new_doc("Purchase Invoice");
}

function render_empty_state(frm) {
frm.doc.reconciliation_data = null;
frm.doc.is_data_loaded = false;

frm.refresh();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
"section_break_11",
"reconciliation_html",
"not_reconciled",
"no_reconciliation_data",
"is_modified",
"section_break_cmfa",
"reconciliation_data"
"no_reconciliation_data"
],
"fields": [
{
Expand Down Expand Up @@ -122,24 +119,7 @@
"depends_on": "eval: !doc.reconciliation_data",
"fieldname": "not_reconciled",
"fieldtype": "HTML",
"options": "<img alt=\"No Data\" src=\"/assets/frappe/images/ui-states/list-empty-state.svg\">\n\t<p class=\"text-muted\">{{ __(\"Reconcile to view the data\") }}</p>"
},
{
"fieldname": "section_break_cmfa",
"fieldtype": "Section Break",
"hidden": 1
},
{
"fieldname": "reconciliation_data",
"fieldtype": "JSON",
"label": "Reconciliation Data"
},
{
"default": "0",
"fieldname": "is_modified",
"fieldtype": "Check",
"hidden": 1,
"label": "Is Modified"
"options": "<img alt=\"No Data\" src=\"/assets/frappe/images/ui-states/list-empty-state.svg\">\n\t<p class=\"text-muted\">{{ __(\"Generate to view the data\") }}</p>"
},
{
"default": "0",
Expand All @@ -152,7 +132,7 @@
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2024-08-09 17:57:55.801686",
"modified": "2025-01-10 16:02:56.302029",
"modified_by": "Administrator",
"module": "GST India",
"name": "Purchase Reconciliation Tool",
Expand Down
Loading
Loading