Skip to content

Commit

Permalink
fix: post filing Journal Entry for Sales RCM
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanket322 committed Oct 8, 2024
1 parent 0a72f67 commit b4e80a3
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 4 deletions.
55 changes: 51 additions & 4 deletions india_compliance/gst_india/doctype/gstr_1_beta/gstr_1_beta.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,15 +444,19 @@ class GSTR1 {
}

render_form_actions() {
if (this.data && (!is_gstr1_api_enabled() || this.status == "Filed")) return;
if (this.data && !is_gstr1_api_enabled()) return;

this.gstr1_action = new GSTR1Action(this.frm);

// Custom Buttons
if (this.data)
if (this.data){
if (this.status == "Filed")
return this.get_journal_entries()

this.frm.add_custom_button(__("Reset"), () =>
this.gstr1_action.reset_gstr1_data()
);
}

// Primary Button
const actions = {
Expand Down Expand Up @@ -712,6 +716,22 @@ class GSTR1 {
let element = $('[data-fieldname="data_section"]');
element.prepend(gst_liability_html);
}

get_journal_entries(){
const { company, month_or_quarter, year } = this.frm.doc;

frappe.call({
method: "india_compliance.gst_india.doctype.gstr_1_beta.gstr_1_beta.get_general_entries",
args: {month_or_quarter, year, company},
callback: (r) => {
if(r.message) return;

this.frm.add_custom_button(__("Create Journal Entry"), () =>
this.gstr1_action.make_journal_entry()
);
}
})
}
}

class TabManager {
Expand Down Expand Up @@ -2454,7 +2474,7 @@ class GSTR1Action extends FileGSTR1Dialog {

frappe.show_alert(__("Uploading data to GSTN"));
this.perform_gstr1_action(action, (response) => {
if(response._server_messages) {
if (response._server_messages) {
this.toggle_actions(true);
return
}
Expand Down Expand Up @@ -2490,7 +2510,7 @@ class GSTR1Action extends FileGSTR1Dialog {
}

previous_action_handler() {
if(this.is_request_in_progress()) return;
if (this.is_request_in_progress()) return;

const { company, company_gstin, month_or_quarter, year } = this.frm.doc;
const filters = {
Expand All @@ -2507,6 +2527,33 @@ class GSTR1Action extends FileGSTR1Dialog {
})
}

make_journal_entry() {
const d = new frappe.ui.Dialog({
title : "Create Journal Entry",
fields: [
{
fieldname: "auto_submit",
fieldtype: "Check",
label: "Submit After Creation",
},
],
primary_action_label: "Create",
primary_action: async (values) => {
const { company, company_gstin, month_or_quarter, year } = this.frm.doc;

frappe.call({
method: "india_compliance.gst_india.doctype.gstr_1_beta.gstr_1_beta.make_journal_entry",
args: { company, company_gstin, month_or_quarter, year, auto_submit: values.auto_submit },
callback: (r) => {
frappe.set_route("journal-entry", r.message);
d.hide();
}
})
}
})
d.show();
}

perform_gstr1_action(action, callback, additional_args = {}) {
this.toggle_actions(false, action);
const args = {
Expand Down
78 changes: 78 additions & 0 deletions india_compliance/gst_india/doctype/gstr_1_beta/gstr_1_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,84 @@ def update_filing_status(filters):
frappe.db.set_value("GST Return Log", log_name, "filing_status", "Not Filed")


@frappe.whitelist()
def get_general_entries(month_or_quarter, year, company):
from_date, to_date = get_gstr_1_from_and_to_date(month_or_quarter, year)

journal_entry = frappe.qb.DocType("Journal Entry")
journal_entry_account = frappe.qb.DocType("Journal Entry Account")

gst_accounts = get_gst_accounts_by_type(company, "Sales Reverse Charge")
if not gst_accounts:
return "No GST Accounts found"

return (
frappe.qb.from_(journal_entry)
.join(journal_entry_account)
.on(journal_entry.name == journal_entry_account.parent)
.select(
journal_entry_account.name,
)
.where(journal_entry.posting_date.between(getdate(from_date), getdate(to_date)))
.where(journal_entry_account.account.in_(gst_accounts))
.run()
)


@frappe.whitelist()
def make_journal_entry(company, company_gstin, month_or_quarter, year, auto_submit):
frappe.has_permission("Journal Entry", "write", throw=True)

from_date, to_date = get_gstr_1_from_and_to_date(month_or_quarter, year)
sales_invoice = frappe.qb.DocType("Sales Invoice")
sales_invoice_taxes = frappe.qb.DocType("Sales Taxes and Charges")

data = (
frappe.qb.from_(sales_invoice)
.join(sales_invoice_taxes)
.on(sales_invoice.name == sales_invoice_taxes.parent)
.select(
sales_invoice_taxes.account_head.as_("account"),
Sum(sales_invoice_taxes.tax_amount).as_("tax_amount"),
)
.where(sales_invoice.is_reverse_charge == 1)
.where(
Date(sales_invoice.posting_date).between(
getdate(from_date), getdate(to_date)
)
)
.groupby(sales_invoice_taxes.account_head)
.run(as_dict=True)
)

journal_entry = frappe.get_doc(
{
"doctype": "Journal Entry",
"company": company,
"company_gstin": company_gstin,
"posting_date": get_last_day(to_date),
}
)

for tax in data:
journal_entry.append(
"accounts",
{
"account": tax.account,
"reference_type": "Sales Invoice",
"debit_in_account_currency": max(tax.tax_amount, 0),
"credit_in_account_currency": abs(min(tax.tax_amount, 0)),
},
)

journal_entry.save()

if auto_submit == "1":
journal_entry.submit()

return journal_entry.name


####### DATA ######################################################################################


Expand Down

0 comments on commit b4e80a3

Please sign in to comment.