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: enqueue purchase reconcilliation #2372

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ const ReturnType = {
GSTR2B: "GSTR2b",
};

const filter_fields = [
"company",
"company_gstin",
"purchase_from_date",
"purchase_to_date",
"inward_supply_from_date",
"inward_supply_to_date",
"include_ignored",
"gst_return",
];

function remove_gstr2b_alert(alert) {
if (alert.length === 0) return;
$(alert).remove();
Expand Down Expand Up @@ -62,20 +73,56 @@ async function add_gstr2b_alert(frm) {
});
}

function setup_filter_onchange_events(frm) {
const events = {};

filter_fields.forEach(field => {
events[field] = () => {
frm.doc.__reconciliation_data = null;
frm.refresh();
};
});

frappe.ui.form.on("Purchase Reconciliation Tool", events);
}

frappe.ui.form.on("Purchase Reconciliation Tool", {
async setup(frm) {
patch_set_active_tab(frm);
new india_compliance.quick_info_popover(frm, tooltip_info);

await frappe.require("purchase_reconciliation_tool.bundle.js");
frm.trigger("company");
frm.purchase_reconciliation_tool = new PurchaseReconciliationTool(frm);
},

onload(frm) {
if (frm.doc.is_modified) frm.doc.reconciliation_data = null;
frm.doc.__reconciliation_data = null;
add_gstr2b_alert(frm);
set_date_range_description(frm);
setup_filter_onchange_events(frm);
frm.purchase_reconciliation_tool = new PurchaseReconciliationTool(frm);

frappe.realtime.on("report_generated", message => {
frm.call("publish_reconciliation_data", {
name: message.name,
});
});

frappe.realtime.on("data_reconciliation", message => {
const { data, filters, creation, report_name } = message;

frm.report_name = report_name;
frm.is_latest_data = true;

const is_filter_same = filter_fields.every(
field => frm.doc[field] === filters[field]
);
if (!is_filter_same) return;

frappe.after_ajax(() => {
frm.doc.__reconciliation_data =
data && data.result ? JSON.stringify(data.result) : null;
frm.doc.data_reconciled_on = creation;
frm.trigger("load_reconciliation_data");
});
});
},

async company(frm) {
Expand All @@ -88,7 +135,46 @@ frappe.ui.form.on("Purchase Reconciliation Tool", {
refresh(frm) {
// Primary Action
frm.disable_save();
frm.page.set_primary_action(__("Reconcile"), () => frm.save());
const button_label = frm.doc.__reconciliation_data
? __("Rebuild")
: __("Reconcile");
const force_update = frm.doc.__reconciliation_data != null;

frm.page.set_primary_action(button_label, async () => {
if (
!frm.doc.company ||
!frm.doc.company_gstin ||
!frm.doc.purchase_period ||
!frm.doc.inward_supply_period ||
!frm.doc.gst_return
) {
frappe.msgprint({
message: __("Please Enter All Details"),
title: "Message",
indicator: "blue",
});
return;
}
const filetrs_to_pass = {};
filter_fields.forEach(field => {
filetrs_to_pass[field] = frm.doc[field];
});

const { message } = await frappe.call({
method: "frappe.core.doctype.prepared_report.prepared_report.get_reports_in_queued_state",
args: {
filters: filetrs_to_pass,
report_name: "ICUtility",
},
});
if (message.length) {
frappe.msgprint(__("A report is alerady in queued"));
return;
}
frm.call("get_reconciliation_data", {
force_update: force_update,
});
});

// add custom buttons
api_enabled
Expand Down Expand Up @@ -132,11 +218,6 @@ frappe.ui.form.on("Purchase Reconciliation Tool", {
}
},

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

async purchase_period(frm) {
await fetch_date_range(frm, "purchase");
set_date_range_description(frm, "purchase");
Expand All @@ -161,9 +242,13 @@ frappe.ui.form.on("Purchase Reconciliation Tool", {
add_gstr2b_alert(frm);
},

after_save(frm) {
load_reconciliation_data(frm) {
frm.refresh();
frm.purchase_reconciliation_tool.refresh(
frm.doc.reconciliation_data ? JSON.parse(frm.doc.reconciliation_data) : []
frm.doc.__reconciliation_data
? JSON.parse(frm.doc.__reconciliation_data)
: [],
frm.doc.data_reconciled_on ? frm.doc.data_reconciled_on : ""
);
},

Expand Down Expand Up @@ -208,7 +293,9 @@ frappe.ui.form.on("Purchase Reconciliation Tool", {
setTimeout(() => {
frm.dashboard.clear_headline();
}, 2000);
frm.save();
frm.call("get_reconciliation_data", {
force_update: true,
});
}, 1000);
}
});
Expand All @@ -225,30 +312,27 @@ class PurchaseReconciliationTool {

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

refresh(data) {
refresh(data, data_reconciled_on) {
if (data) {
this.data = data;
this.refresh_filter_fields();
}

this.apply_filters(!!data);

// data unchanged!
if (this.rendered_data == this.filtered_data) return;

this._tabs.forEach(tab => {
this.tabs[`${tab}_tab`].refresh(this[`get_${tab}_data`]());
});

this.rendered_data = this.filtered_data;
this.setup_footer(data_reconciled_on);
}

render_tab_group() {
Expand Down Expand Up @@ -299,6 +383,23 @@ class PurchaseReconciliationTool {
);
}

setup_footer(data_reconciled_on) {
if (!data_reconciled_on) return;

const creation_time_string = `Created ${frappe.datetime.prettyDate(
data_reconciled_on
)} `;

const wrapper = this.frm.get_field("reconciliation_html").$wrapper;

if ($(wrapper).find(".creation-time").length)
$(wrapper).find(".creation-time").remove();

wrapper.append(
`<div class="creation-time text-muted float-right">${creation_time_string}</div>`
);
}

setup_filter_button() {
this.filter_group = new india_compliance.FilterGroup({
doctype: "Purchase Reconciliation Tool",
Expand Down Expand Up @@ -988,6 +1089,7 @@ class DetailViewDialog {
() => {
this._apply_custom_action(action);
this.dialog.hide();
this.update_report_status();
},
`mr-2 ${this._get_button_css(action)}`
);
Expand Down Expand Up @@ -1021,6 +1123,24 @@ class DetailViewDialog {
}
}

async update_report_status() {
if (this.frm.is_latest_data == false) return;

const filters = {};
filter_fields.forEach(field => {
filters[field] = this.frm.doc[field];
});
filters["is_latest_data"] = 1;

await frappe.db.set_value(
"Prepared Report",
this.frm.report_name,
"filters",
JSON.stringify(filters)
);
this.frm.is_latest_data = false;
}

_get_button_css(action) {
if (action == "Unlink") return "btn-danger not-grey";
if (action == "Pending") return "btn-secondary";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"no_reconciliation_data",
"is_modified",
"section_break_cmfa",
"reconciliation_data"
"__reconciliation_data"
],
"fields": [
{
Expand Down Expand Up @@ -96,12 +96,12 @@
"fieldtype": "Column Break"
},
{
"depends_on": "eval: doc.reconciliation_data?.length",
"depends_on": "eval: doc.__reconciliation_data?.length",
"fieldname": "reconciliation_html",
"fieldtype": "HTML"
},
{
"depends_on": "eval: doc.reconciliation_data && !doc.reconciliation_data.length",
"depends_on": "eval: doc.__reconciliation_data && !doc.__reconciliation_data.length",
"fieldname": "no_reconciliation_data",
"fieldtype": "HTML",
"options": "<img alt=\"No Data\" src=\"/assets/frappe/images/ui-states/list-empty-state.svg\">\n\t<p class=\"text-muted\">{{ __(\"No data available for selected filters\") }}</p>"
Expand All @@ -117,7 +117,7 @@
"options": "GSTR 2B\nBoth GSTR 2A & 2B"
},
{
"depends_on": "eval: !doc.reconciliation_data",
"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>"
Expand All @@ -128,9 +128,8 @@
"hidden": 1
},
{
"fieldname": "reconciliation_data",
"fieldtype": "JSON",
"label": "Reconciliation Data"
"fieldname": "__reconciliation_data",
"fieldtype": "JSON"
},
{
"default": "0",
Expand All @@ -150,7 +149,7 @@
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2024-06-20 13:19:57.316043",
"modified": "2024-08-10 16:20:48.790753",
"modified_by": "Administrator",
"module": "GST India",
"name": "Purchase Reconciliation Tool",
Expand Down
Loading