Skip to content

Commit

Permalink
feat: e-Waybill for purchase invoice (#350)
Browse files Browse the repository at this point in the history
Co-authored-by: Smit Vora <[email protected]>
Co-authored-by: ljain112 <[email protected]>
  • Loading branch information
3 people authored Aug 7, 2023
1 parent c2d6a41 commit f941e23
Show file tree
Hide file tree
Showing 14 changed files with 382 additions and 194 deletions.
87 changes: 60 additions & 27 deletions india_compliance/gst_india/client_scripts/e_waybill_actions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
function setup_e_waybill_actions(doctype) {
if (
!gst_settings.enable_e_waybill ||
(doctype == "Delivery Note" && !gst_settings.enable_e_waybill_from_dn)
)
return;
if (!gst_settings.enable_e_waybill) return;

frappe.ui.form.on(doctype, {
mode_of_transport(frm) {
Expand Down Expand Up @@ -31,8 +27,7 @@ function setup_e_waybill_actions(doctype) {
if (
frm.doc.docstatus != 1 ||
frm.is_dirty() ||
!is_e_waybill_applicable(frm) ||
(frm.doctype === "Delivery Note" && !frm.doc.customer_address)
!is_e_waybill_applicable(frm)
)
return;

Expand Down Expand Up @@ -132,11 +127,9 @@ function setup_e_waybill_actions(doctype) {
},
async on_submit(frm) {
if (
// threshold is only met for Sales Invoice
frm.doctype != "Sales Invoice" ||
!has_e_waybill_threshold_met(frm) ||
frm.doc.ewaybill ||
frm.doc.is_return ||
frm.doc.is_debit_note ||
!india_compliance.is_api_enabled() ||
!gst_settings.auto_generate_e_waybill ||
is_e_invoice_applicable(frm) ||
Expand Down Expand Up @@ -672,11 +665,10 @@ function show_update_transporter_dialog(frm) {
}

async function show_extend_validity_dialog(frm) {
const shipping_address = await frappe.db.get_doc(
const destination_address = await frappe.db.get_doc(
"Address",
frm.doc.shipping_address_name || frm.doc.customer_address
get_destination_address_name(frm)
);

const is_in_movement = "eval: doc.consignment_status === 'In Movement'";
const is_in_transit = "eval: doc.consignment_status === 'In Transit'";

Expand Down Expand Up @@ -749,23 +741,23 @@ async function show_extend_validity_dialog(frm) {
label: "Address Line1",
fieldname: "address_line1",
fieldtype: "Data",
default: shipping_address.address_line1,
default: destination_address.address_line1,
depends_on: is_in_transit,
mandatory_depends_on: is_in_transit,
},
{
label: "Address Line2",
fieldname: "address_line2",
fieldtype: "Data",
default: shipping_address.address_line2,
default: destination_address.address_line2,
depends_on: is_in_transit,
mandatory_depends_on: is_in_transit,
},
{
label: "Address Line3",
fieldname: "address_line3",
fieldtype: "Data",
default: shipping_address.city,
default: destination_address.city,
depends_on: is_in_transit,
mandatory_depends_on: is_in_transit,
},
Expand All @@ -777,22 +769,22 @@ async function show_extend_validity_dialog(frm) {
fieldname: "current_place",
fieldtype: "Data",
reqd: 1,
default: shipping_address.city,
default: destination_address.city,
},
{
label: "Current Pincode",
fieldname: "current_pincode",
fieldtype: "Data",
reqd: 1,
default: shipping_address.pincode,
default: destination_address.pincode,
},
{
label: "Current State",
fieldname: "current_state",
fieldtype: "Autocomplete",
options: frappe.boot.india_state_options.join("\n"),
reqd: 1,
default: shipping_address.state,
default: destination_address.state,
},
{
fieldtype: "Section Break",
Expand Down Expand Up @@ -855,19 +847,20 @@ function is_e_waybill_valid(frm) {
}

function has_e_waybill_threshold_met(frm) {
if (
frm.doc.doctype == "Sales Invoice" &&
Math.abs(frm.doc.base_grand_total) >= gst_settings.e_waybill_threshold
)
if (Math.abs(frm.doc.base_grand_total) >= gst_settings.e_waybill_threshold)
return true;
}

function is_e_waybill_applicable(frm) {
// means company is Indian and not Unregistered
if (
// means company is Indian and not Unregistered
!frm.doc.company_gstin ||
(frm.doctype === "Sales Invoice" &&
frm.doc.company_gstin === frm.doc.billing_address_gstin)
!gst_settings.enable_e_waybill ||
!(
is_e_waybill_applicable_on_sales_invoice(frm) ||
is_e_waybill_applicable_on_purchase_invoice(frm) ||
is_e_waybill_applicable_on_delivery_note(frm)
)
)
return;

Expand All @@ -886,9 +879,11 @@ function can_extend_e_waybill(frm) {
const valid_upto = frm.doc.__onload?.e_waybill_info?.valid_upto;
const extend_after = get_hours(valid_upto, -8);
const extend_before = get_hours(valid_upto, 8);
const now = frappe.datetime.now_datetime();

if (
extend_after < frappe.datetime.now_datetime() < extend_before &&
extend_after < now &&
now < extend_before &&
frm.doc.gst_transporter_id != frm.doc.company_gstin
)
return true;
Expand All @@ -907,6 +902,33 @@ function is_e_waybill_cancellable(frm) {
);
}

function is_e_waybill_applicable_on_sales_invoice(frm) {
return (
frm.doctype == "Sales Invoice" &&
frm.doc.company_gstin !== frm.doc.billing_address_gstin &&
frm.doc.customer_address &&
!frm.doc.is_return &&
!frm.doc.is_debit_note
);
}

function is_e_waybill_applicable_on_delivery_note(frm) {
return (
frm.doctype == "Delivery Note" &&
frm.doc.customer_address &&
gst_settings.enable_e_waybill_from_dn
);
}

function is_e_waybill_applicable_on_purchase_invoice(frm) {
return (
frm.doctype == "Purchase Invoice" &&
frm.doc.supplier_address &&
frm.doc.company_gstin !== frm.doc.supplier_gstin &&
gst_settings.enable_e_waybill_from_pi
);
}

function is_e_waybill_generated_using_api(frm) {
const e_waybill_info = frm.doc.__onload && frm.doc.__onload.e_waybill_info;
return e_waybill_info && e_waybill_info.created_on;
Expand Down Expand Up @@ -1021,3 +1043,14 @@ function get_e_waybill_file_name(docname) {
function set_primary_action_label(dialog, primary_action_label) {
dialog.get_primary_btn().removeClass("hide").html(primary_action_label);
}

function get_destination_address_name(frm) {
if (frm.doc.doctype == "Purchase Invoice") {
if (frm.doc.is_return) return frm.doc.supplier_address;
return frm.doc.shipping_address_name || frm.doc.billing_address;
} else {
if (frm.doc.is_return)
return frm.doc.dispatch_address_name || frm.doc.company_address;
return frm.doc.shipping_address_name || frm.doc.customer_address;
}
}
21 changes: 20 additions & 1 deletion india_compliance/gst_india/client_scripts/purchase_invoice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
frappe.ui.form.on("Purchase Invoice", {
const DOCTYPE = "Purchase Invoice";
setup_e_waybill_actions(DOCTYPE);

frappe.ui.form.on(DOCTYPE, {
after_save(frm) {
if (
frm.doc.docstatus ||
frm.doc.supplier_address ||
!is_e_waybill_applicable(frm)
)
return;

frappe.show_alert(
{
message: __("Supplier Address is required to create e-Waybill"),
indicator: "yellow",
},
10
);
},
refresh(frm) {
if (
frm.doc.docstatus !== 1 ||
Expand Down
60 changes: 33 additions & 27 deletions india_compliance/gst_india/constants/custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
"fieldname": "gst_section",
"label": "GST Details",
"fieldtype": "Section Break",
"insert_after": "language",
"insert_after": "gst_vehicle_type",
"print_hide": 1,
"collapsible": 1,
},
Expand Down Expand Up @@ -726,19 +726,9 @@
"print_hide": 1,
"translatable": 0,
},
{
"fieldname": "ewaybill",
"label": "e-Waybill No.",
"fieldtype": "Data",
"depends_on": "eval: doc.docstatus === 1 || doc.ewaybill",
"allow_on_submit": 1,
"insert_after": "customer_name",
"translatable": 0,
"no_copy": 1,
},
]

E_WAYBILL_SI_FIELDS = [
E_WAYBILL_INV_FIELDS = [
{
"fieldname": "transporter_info",
"label": "Transporter Info",
Expand Down Expand Up @@ -814,23 +804,39 @@
"default": "Today",
"print_hide": 1,
},
{
"fieldname": "e_waybill_status",
"label": "e-Waybill Status",
"fieldtype": "Select",
"insert_after": "ewaybill",
"options": "\nPending\nGenerated\nCancelled\nNot Applicable",
"print_hide": 1,
"no_copy": 1,
"translatable": 1,
"allow_on_submit": 1,
"depends_on": "eval:doc.docstatus === 1 && (!doc.ewaybill || in_list(['','Pending', 'Not Applicable'], doc.e_waybill_status))",
"read_only_depends_on": "eval:doc.e_waybill_status === 'Generated' && doc.ewaybill",
},
*E_WAYBILL_DN_FIELDS,
]

sales_e_waybill_field = {
"fieldname": "ewaybill",
"label": "e-Waybill No.",
"fieldtype": "Data",
"depends_on": "eval: doc.docstatus === 1 || doc.ewaybill",
"allow_on_submit": 1,
"translatable": 0,
"no_copy": 1,
"insert_after": "customer_name",
}

e_waybill_status_field = {
"fieldname": "e_waybill_status",
"label": "e-Waybill Status",
"fieldtype": "Select",
"insert_after": "ewaybill",
"options": "\nPending\nGenerated\nCancelled\nNot Applicable",
"print_hide": 1,
"no_copy": 1,
"translatable": 1,
"allow_on_submit": 1,
"depends_on": "eval:doc.docstatus === 1 && (!doc.ewaybill || in_list(['','Pending', 'Not Applicable'], doc.e_waybill_status))",
"read_only_depends_on": "eval:doc.e_waybill_status === 'Generated' && doc.ewaybill",
}

purchase_e_waybill_field = {**sales_e_waybill_field, "insert_after": "supplier_name"}

E_WAYBILL_FIELDS = {
"Sales Invoice": E_WAYBILL_SI_FIELDS,
"Delivery Note": E_WAYBILL_DN_FIELDS,
"Sales Invoice": E_WAYBILL_INV_FIELDS
+ [sales_e_waybill_field, e_waybill_status_field],
"Delivery Note": E_WAYBILL_DN_FIELDS + [sales_e_waybill_field],
"Purchase Invoice": E_WAYBILL_INV_FIELDS + [purchase_e_waybill_field],
}
19 changes: 19 additions & 0 deletions india_compliance/gst_india/constants/e_waybill.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,26 @@
# }
#
# DATETIME_FORMAT = "%d/%m/%Y %I:%M:%S %p"
selling_address = {
"bill_from": "company_address",
"bill_to": "customer_address",
"ship_from": "dispatch_address_name",
"ship_to": "shipping_address_name",
}

buying_address = {
"bill_from": "supplier_address",
"bill_to": "billing_address",
"ship_from": "supplier_address",
"ship_to": "shipping_address",
}

ADDRESS_FIELDS = {
"Sales Invoice": selling_address,
"Purchase Invoice": buying_address,
"Delivery Note": selling_address,
}
PERMITTED_DOCTYPES = list(ADDRESS_FIELDS.keys())

CANCEL_REASON_CODES = {
"Duplicate": "1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"e_waybill_section",
"enable_e_waybill",
"enable_e_waybill_from_dn",
"enable_e_waybill_from_pi",
"fetch_e_waybill_data",
"attach_e_waybill_print",
"column_break_10",
Expand Down Expand Up @@ -238,6 +239,13 @@
"fieldtype": "Check",
"label": "Autofill Party Information based on GSTIN"
},
{
"default": "0",
"depends_on": "eval: doc.enable_e_waybill",
"fieldname": "enable_e_waybill_from_pi",
"fieldtype": "Check",
"label": "Enable e-Waybill Generation from Purchase Invoice"
},
{
"collapsible": 1,
"fieldname": "uom_mapping_section",
Expand Down
Loading

0 comments on commit f941e23

Please sign in to comment.