Skip to content

Commit

Permalink
Merge pull request #939 from resilient-tech/version-14-hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
vorasmit authored Aug 8, 2023
2 parents 46d3a44 + cb69505 commit dfec3e7
Show file tree
Hide file tree
Showing 23 changed files with 2,011 additions and 532 deletions.
279 changes: 262 additions & 17 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 @@ -90,6 +85,17 @@ function setup_e_waybill_actions(doctype) {
);
}

if (
frappe.perm.has_perm(frm.doctype, 0, "submit", frm.doc.name) &&
can_extend_e_waybill(frm)
) {
frm.add_custom_button(
__("Extend Validity"),
() => show_extend_validity_dialog(frm),
"e-Waybill"
);
}

if (
frappe.perm.has_perm(frm.doctype, 0, "cancel", frm.doc.name) &&
is_e_waybill_cancellable(frm)
Expand Down Expand Up @@ -121,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 @@ -660,6 +664,177 @@ function show_update_transporter_dialog(frm) {
d.show();
}

async function show_extend_validity_dialog(frm) {
const destination_address = await frappe.db.get_doc(
"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'";

const d = new frappe.ui.Dialog({
title: __("Extend Validity"),
fields: [
{
label: "e-Waybill",
fieldname: "ewaybill",
fieldtype: "Data",
read_only: 1,
default: frm.doc.ewaybill,
},
{
label: "Vehicle No",
fieldname: "vehicle_no",
fieldtype: "Data",
default: frm.doc.vehicle_no,
mandatory_depends_on: "eval: doc.mode_of_transport === 'Road'",
},
{
label: "Remaining Distance (in km)",
fieldname: "remaining_distance",
fieldtype: "Float",
default: frm.doc.distance,
},
{
fieldtype: "Column Break",
},
{
label: "Consignment Status",
fieldname: "consignment_status",
fieldtype: "Select",
options: `In Movement\nIn Transit`,
default: "In Movement",
reqd: 1,
onchange: () => update_transit_type(d),
},
{
label: "Mode Of Transport",
fieldname: "mode_of_transport",
fieldtype: "Select",
options: `\nRoad\nAir\nRail\nShip`,
default: frm.doc.mode_of_transport,
depends_on: is_in_movement,
mandatory_depends_on: is_in_movement,
onchange: () => update_transit_type(d),
},
{
label: "Transit Type",
fieldname: "transit_type",
fieldtype: "Select",
options: `\nRoad\nWarehouse\nOthers`,
depends_on: is_in_transit,
mandatory_depends_on: is_in_transit,
},
{
label: "Transport Receipt No",
fieldname: "lr_no",
fieldtype: "Data",
default: frm.doc.lr_no,
depends_on: is_in_movement,
mandatory_depends_on:
"eval: ['Rail', 'Air', 'Ship'].includes(doc.mode_of_transport) && doc.consignment_status === 'In Movement'",
},
{
fieldtype: "Section Break",
},
{
label: "Address Line1",
fieldname: "address_line1",
fieldtype: "Data",
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: destination_address.address_line2,
depends_on: is_in_transit,
mandatory_depends_on: is_in_transit,
},
{
label: "Address Line3",
fieldname: "address_line3",
fieldtype: "Data",
default: destination_address.city,
depends_on: is_in_transit,
mandatory_depends_on: is_in_transit,
},
{
fieldtype: "Column Break",
},
{
label: "Current Place",
fieldname: "current_place",
fieldtype: "Data",
reqd: 1,
default: destination_address.city,
},
{
label: "Current Pincode",
fieldname: "current_pincode",
fieldtype: "Data",
reqd: 1,
default: destination_address.pincode,
},
{
label: "Current State",
fieldname: "current_state",
fieldtype: "Autocomplete",
options: frappe.boot.india_state_options.join("\n"),
reqd: 1,
default: destination_address.state,
},
{
fieldtype: "Section Break",
},
{
fieldname: "reason",
label: "Reason",
fieldtype: "Select",
options: [
"Natural Calamity",
"Law and Order Situation",
"Transshipment",
"Accident",
"Others",
],
reqd: 1,
},
{
label: "Update e-Waybill Print/Data",
fieldname: "update_e_waybill_data",
fieldtype: "Check",
default: gst_settings.fetch_e_waybill_data,
},
{
fieldtype: "Column Break",
},
{
fieldname: "remark",
label: "Remark",
fieldtype: "Data",
mandatory_depends_on: 'eval: doc.reason === "Others"',
},
],
primary_action_label: __("Extend"),
primary_action(values) {
frappe.call({
method: "india_compliance.gst_india.utils.e_waybill.extend_validity",
args: {
doctype: frm.doctype,
docname: frm.doc.name,
values,
},
callback: () => frm.refresh(),
});
d.hide();
},
});
d.show();
}

function is_e_waybill_valid(frm) {
const e_waybill_info = frm.doc.__onload && frm.doc.__onload.e_waybill_info;
return (
Expand All @@ -672,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 @@ -695,6 +871,26 @@ function is_e_waybill_applicable(frm) {
}
}

function can_extend_e_waybill(frm) {
function get_hours(date, hours) {
return moment(date).add(hours, "hours").format(frappe.defaultDatetimeFormat);
}

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 < now &&
now < extend_before &&
frm.doc.gst_transporter_id != frm.doc.company_gstin
)
return true;

return false;
}

function is_e_waybill_cancellable(frm) {
const e_waybill_info = frm.doc.__onload && frm.doc.__onload.e_waybill_info;
return (
Expand All @@ -706,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 @@ -765,6 +988,18 @@ function get_vehicle_type(doc) {
return "";
}

function update_transit_type(dialog) {
dialog.set_value("transit_type", get_transit_type(dialog.get_values(true)));
}

function get_transit_type(dialog) {
if (dialog.consignment_status === "In Movement") return "";
if (dialog.consignment_status === "In Transit") {
if (dialog.mode_of_transport === "Road") return "Road";
else return "Others";
}
}

/********
* Utils
*******/
Expand Down Expand Up @@ -809,3 +1044,13 @@ 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
Loading

0 comments on commit dfec3e7

Please sign in to comment.