From b2fcc8fbaac1a0701b021c26cf78f867360f86a3 Mon Sep 17 00:00:00 2001 From: Ravibharathi <131471282+ravibharathi656@users.noreply.github.com> Date: Fri, 21 Feb 2025 19:45:21 +0530 Subject: [PATCH] feat: add total weight in shipment (#46049) Co-authored-by: barredterra <14891507+barredterra@users.noreply.github.com> --- erpnext/stock/doctype/shipment/shipment.json | 9 ++- erpnext/stock/doctype/shipment/shipment.py | 67 +++++++++++++++++++ .../stock/doctype/shipment/test_shipment.py | 23 ++++--- 3 files changed, 89 insertions(+), 10 deletions(-) diff --git a/erpnext/stock/doctype/shipment/shipment.json b/erpnext/stock/doctype/shipment/shipment.json index ca8b06e667..c3818aaf67 100644 --- a/erpnext/stock/doctype/shipment/shipment.json +++ b/erpnext/stock/doctype/shipment/shipment.json @@ -34,6 +34,7 @@ "shipment_parcel", "parcel_template", "add_template", + "total_weight", "column_break_28", "shipment_delivery_note", "shipment_details_section", @@ -429,11 +430,17 @@ "label": "Pickup Contact Person", "mandatory_depends_on": "eval:doc.pickup_from_type === 'Company'", "options": "User" + }, + { + "fieldname": "total_weight", + "fieldtype": "Float", + "label": "Total Weight (kg)", + "read_only": 1 } ], "is_submittable": 1, "links": [], - "modified": "2024-03-27 13:10:41.030764", + "modified": "2025-02-20 16:55:20.076418", "modified_by": "Administrator", "module": "Stock", "name": "Shipment", diff --git a/erpnext/stock/doctype/shipment/shipment.py b/erpnext/stock/doctype/shipment/shipment.py index 42a67f42be..cf9d165fdd 100644 --- a/erpnext/stock/doctype/shipment/shipment.py +++ b/erpnext/stock/doctype/shipment/shipment.py @@ -12,10 +12,71 @@ from erpnext.accounts.party import get_party_shipping_address class Shipment(Document): + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. + + from typing import TYPE_CHECKING + + if TYPE_CHECKING: + from frappe.types import DF + + from erpnext.stock.doctype.shipment_delivery_note.shipment_delivery_note import ( + ShipmentDeliveryNote, + ) + from erpnext.stock.doctype.shipment_parcel.shipment_parcel import ShipmentParcel + + amended_from: DF.Link | None + awb_number: DF.Data | None + carrier: DF.Data | None + carrier_service: DF.Data | None + delivery_address: DF.SmallText | None + delivery_address_name: DF.Link + delivery_company: DF.Link | None + delivery_contact: DF.SmallText | None + delivery_contact_email: DF.Data | None + delivery_contact_name: DF.Link | None + delivery_customer: DF.Link | None + delivery_supplier: DF.Link | None + delivery_to: DF.Data | None + delivery_to_type: DF.Literal["Company", "Customer", "Supplier"] + description_of_content: DF.SmallText + incoterm: DF.Link | None + pallets: DF.Literal["No", "Yes"] + parcel_template: DF.Link | None + pickup: DF.Data | None + pickup_address: DF.SmallText | None + pickup_address_name: DF.Link + pickup_company: DF.Link | None + pickup_contact: DF.SmallText | None + pickup_contact_email: DF.Data | None + pickup_contact_name: DF.Link | None + pickup_contact_person: DF.Link | None + pickup_customer: DF.Link | None + pickup_date: DF.Date + pickup_from: DF.Time + pickup_from_type: DF.Literal["Company", "Customer", "Supplier"] + pickup_supplier: DF.Link | None + pickup_to: DF.Time + pickup_type: DF.Literal["Pickup", "Self delivery"] + service_provider: DF.Data | None + shipment_amount: DF.Currency + shipment_delivery_note: DF.Table[ShipmentDeliveryNote] + shipment_id: DF.Data | None + shipment_parcel: DF.Table[ShipmentParcel] + shipment_type: DF.Literal["Goods", "Documents"] + status: DF.Literal["Draft", "Submitted", "Booked", "Cancelled", "Completed"] + total_weight: DF.Float + tracking_status: DF.Literal["", "In Progress", "Delivered", "Returned", "Lost"] + tracking_status_info: DF.Data | None + tracking_url: DF.SmallText | None + value_of_goods: DF.Currency + # end: auto-generated types + def validate(self): self.validate_weight() self.validate_pickup_time() self.set_value_of_goods() + self.set_total_weight() if self.docstatus == 0: self.status = "Draft" @@ -34,6 +95,12 @@ class Shipment(Document): if flt(parcel.weight) <= 0: frappe.throw(_("Parcel weight cannot be 0")) + def set_total_weight(self): + self.total_weight = self.get_total_weight() + + def get_total_weight(self): + return sum(flt(parcel.weight) * parcel.count for parcel in self.shipment_parcel if parcel.count > 0) + def validate_pickup_time(self): if self.pickup_from and self.pickup_to and get_time(self.pickup_to) < get_time(self.pickup_from): frappe.throw(_("Pickup To time should be greater than Pickup From time")) diff --git a/erpnext/stock/doctype/shipment/test_shipment.py b/erpnext/stock/doctype/shipment/test_shipment.py index 7938798b43..42ead76905 100644 --- a/erpnext/stock/doctype/shipment/test_shipment.py +++ b/erpnext/stock/doctype/shipment/test_shipment.py @@ -29,6 +29,17 @@ class TestShipment(IntegrationTestCase): self.assertEqual(len(second_shipment.shipment_delivery_note), 1) self.assertEqual(second_shipment.shipment_delivery_note[0].delivery_note, delivery_note.name) + def test_get_total_weight(self): + shipment = frappe.new_doc("Shipment") + shipment.extend( + "shipment_parcel", + [ + {"length": 5, "width": 5, "height": 5, "weight": 5, "count": 5}, + {"length": 5, "width": 5, "height": 5, "weight": 10, "count": 1}, + ], + ) + self.assertEqual(shipment.get_total_weight(), 35) + def create_test_delivery_note(): company = get_shipment_company() @@ -85,9 +96,7 @@ def create_test_shipment(delivery_notes=None): shipment.description_of_content = "unit test entry" for delivery_note in delivery_notes: shipment.append("shipment_delivery_note", {"delivery_note": delivery_note.name}) - shipment.append( - "shipment_parcel", {"length": 5, "width": 5, "height": 5, "weight": 5, "count": 5} - ) + shipment.append("shipment_parcel", {"length": 5, "width": 5, "height": 5, "weight": 5, "count": 5}) shipment.insert() return shipment @@ -105,9 +114,7 @@ def get_shipment_customer_contact(customer_name): def get_shipment_customer_address(customer_name): address_title = customer_name + " address 123" - customer_address = frappe.get_all( - "Address", fields=["name"], filters={"address_title": address_title} - ) + customer_address = frappe.get_all("Address", fields=["name"], filters={"address_title": address_title}) if len(customer_address): return customer_address[0] else: @@ -169,9 +176,7 @@ def create_customer_contact(fname, lname): customer.is_primary_contact = 1 customer.is_billing_contact = 1 customer.append("email_ids", {"email_id": "randomme@email.com", "is_primary": 1}) - customer.append( - "phone_nos", {"phone": "123123123", "is_primary_phone": 1, "is_primary_mobile_no": 1} - ) + customer.append("phone_nos", {"phone": "123123123", "is_primary_phone": 1, "is_primary_mobile_no": 1}) customer.status = "Passive" customer.insert() return customer -- GitLab