Skip to content

Commit

Permalink
feat: sales order updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishBarvaliya committed Aug 16, 2024
1 parent 5cdb277 commit 3b3b8d3
Show file tree
Hide file tree
Showing 5 changed files with 412 additions and 2 deletions.
114 changes: 113 additions & 1 deletion d2h/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def create_purchase_receipt(purchase_order, items):
purchase_receipt = frappe.get_doc({
"doctype": "Purchase Receipt",
"supplier": purchase_order.supplier,
"currency": purchase_order.currency,
"conversion_rate": purchase_order.conversion_rate,
"buying_price_list": purchase_order.buying_price_list,
"price_list_currency": purchase_order.price_list_currency,
"items": []
})

Expand All @@ -68,6 +72,32 @@ def create_purchase_receipt(purchase_order, items):
new_item.purchase_order = purchase_order.name
new_item.purchase_order_item = item["name"]
new_item.scheduled_date = purchase_order.schedule_date
new_item.supplier_part_no = item.get("supplier_part_no")
new_item.product_bundle = item.get("product_bundle")
new_item.item_group = item.get("item_group")
new_item.brand = item.get("brand")
new_item.stock_uom = item.get("stock_uom")
new_item.conversion_factor = item.get("conversion_factor")
new_item.description = item.get("description")
new_item.image = item.get("image")
new_item.price_list_rate = item.get("price_list_rate")
new_item.base_price_list_rate = item.get("base_price_list_rate")
new_item.margin_type = item.get("margin_type")
new_item.margin_rate_or_amount = item.get("margin_rate_or_amount")
new_item.rate_with_margin = item.get("rate_with_margin")
new_item.base_rate_with_margin = item.get("base_rate_with_margin")
new_item.amount = item.get("amount")
new_item.rate = item.get("rate")
new_item.base_rate = item.get("base_rate")
new_item.base_amount = item.get("base_amount")
new_item.discount_percentage = item.get("discount_percentage")
new_item.discount_amount = item.get("discount_amount")
new_item.base_discount_amount = item.get("base_discount_amount")
new_item.net_rate = item.get("net_rate")
new_item.net_amount = item.get("net_amount")
new_item.base_net_rate = item.get("base_net_rate")
new_item.base_net_amount = item.get("base_net_amount")
new_item.tax_rate = item.get("tax_rate")


purchase_receipt.insert(ignore_permissions=True)
Expand All @@ -90,4 +120,86 @@ def get_purchase_order_good_in_transit(purchase_order):
},
fields=["name", "item_code", "item_name", "qty", "purchase_order_item"]
)
return purchase_receipts
return purchase_receipts


@frappe.whitelist()
def short_close_sales_order(sales_order):
sales_order = frappe.get_doc("Sales Order", sales_order)
for item in sales_order.items:
if item.qty > item.delivered_qty:
item.custom_short_close_qty = item.qty - item.delivered_qty - item.custom_good_in_transit_qty

sales_order.status = "Closed"
sales_order.save(ignore_permissions=True)

return "OK"

@frappe.whitelist()
def create_delivery_note(sales_order, items):
sales_order = frappe.get_doc("Sales Order", sales_order)
items_list = json.loads(items)

delivery_note = frappe.get_doc({
"doctype": "Delivery Note",
"customer": sales_order.customer,
"currency": sales_order.currency,
"conversion_rate": sales_order.conversion_rate,
"items": []
})

for item in items_list:
new_item = delivery_note.append("items", {})
new_item.item_code = item["item_code"]
new_item.item_name = item["item_name"]
new_item.qty = item["qty"]
new_item.uom = item["uom"]
new_item.against_sales_order = sales_order.name
new_item.so_detail = item["name"]
new_item.supplier_part_no = item.get("supplier_part_no")
new_item.brand = item.get("brand")
new_item.stock_uom = item.get("stock_uom")
new_item.conversion_factor = item.get("conversion_factor")
new_item.description = item.get("description")
new_item.image = item.get("image")
new_item.price_list_rate = item.get("price_list_rate")
new_item.base_price_list_rate = item.get("base_price_list_rate")
new_item.margin_type = item.get("margin_type")
new_item.margin_rate_or_amount = item.get("margin_rate_or_amount")
new_item.rate_with_margin = item.get("rate_with_margin")
new_item.base_rate_with_margin = item.get("base_rate_with_margin")
new_item.amount = item.get("amount")
new_item.rate = item.get("rate")
new_item.base_rate = item.get("base_rate")
new_item.base_amount = item.get("base_amount")
new_item.discount_percentage = item.get("discount_percentage")
new_item.discount_amount = item.get("discount_amount")
new_item.base_discount_amount = item.get("base_discount_amount")
new_item.net_rate = item.get("net_rate")
new_item.net_amount = item.get("net_amount")
new_item.base_net_rate = item.get("base_net_rate")
new_item.base_net_amount = item.get("base_net_amount")
new_item.tax_rate = item.get("tax_rate")


delivery_note.insert(ignore_permissions=True)

for item in sales_order.items:
found_item = next((itm for itm in items_list if itm['name'] == item.name), None)
if found_item:
item.custom_good_in_transit_qty += found_item['qty']

sales_order.save(ignore_permissions=True)
return "OK"

@frappe.whitelist()
def get_sales_order_good_in_transit(sales_order):
delivery_notes = frappe.get_all(
"Delivery Note Item",
filters={
"against_sales_order": sales_order,
"docstatus": 1
},
fields=["name", "item_code", "item_name", "qty", "so_detail"]
)
return delivery_notes
110 changes: 110 additions & 0 deletions d2h/fixtures/custom_field.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,61 @@
"unique": 0,
"width": null
},
{
"allow_in_quick_entry": 0,
"allow_on_submit": 1,
"bold": 0,
"collapsible": 0,
"collapsible_depends_on": null,
"columns": 0,
"default": null,
"depends_on": null,
"description": null,
"docstatus": 0,
"doctype": "Custom Field",
"dt": "Sales Order Item",
"fetch_from": null,
"fetch_if_empty": 0,
"fieldname": "custom_good_in_transit_qty",
"fieldtype": "Float",
"hidden": 0,
"hide_border": 0,
"hide_days": 0,
"hide_seconds": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_global_search": 0,
"in_list_view": 1,
"in_preview": 0,
"in_standard_filter": 0,
"insert_after": "stock_uom",
"is_system_generated": 0,
"is_virtual": 0,
"label": "Good In Transit Qty",
"length": 0,
"link_filters": null,
"mandatory_depends_on": null,
"modified": "2024-08-16 08:05:50.712656",
"module": null,
"name": "Sales Order Item-custom_good_in_transit_qty",
"no_copy": 0,
"non_negative": 0,
"options": null,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"print_width": null,
"read_only": 0,
"read_only_depends_on": null,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"sort_options": 0,
"translatable": 0,
"unique": 0,
"width": null
},
{
"allow_in_quick_entry": 0,
"allow_on_submit": 0,
Expand Down Expand Up @@ -384,6 +439,61 @@
"unique": 0,
"width": null
},
{
"allow_in_quick_entry": 0,
"allow_on_submit": 1,
"bold": 0,
"collapsible": 0,
"collapsible_depends_on": null,
"columns": 0,
"default": null,
"depends_on": null,
"description": null,
"docstatus": 0,
"doctype": "Custom Field",
"dt": "Sales Order Item",
"fetch_from": null,
"fetch_if_empty": 0,
"fieldname": "custom_short_close_qty",
"fieldtype": "Float",
"hidden": 0,
"hide_border": 0,
"hide_days": 0,
"hide_seconds": 0,
"ignore_user_permissions": 0,
"ignore_xss_filter": 0,
"in_global_search": 0,
"in_list_view": 1,
"in_preview": 0,
"in_standard_filter": 0,
"insert_after": "custom_good_in_transit_qty",
"is_system_generated": 0,
"is_virtual": 0,
"label": "Short Close Qty",
"length": 0,
"link_filters": null,
"mandatory_depends_on": null,
"modified": "2024-08-16 08:05:50.971849",
"module": null,
"name": "Sales Order Item-custom_short_close_qty",
"no_copy": 0,
"non_negative": 1,
"options": null,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"print_hide_if_no_value": 0,
"print_width": null,
"read_only": 0,
"read_only_depends_on": null,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"sort_options": 0,
"translatable": 0,
"unique": 0,
"width": null
},
{
"allow_in_quick_entry": 0,
"allow_on_submit": 1,
Expand Down
3 changes: 2 additions & 1 deletion d2h/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
},
"Delivery Note": {
"before_save": "d2h.overrides.delivery_note_before_save",
"on_submit": "d2h.overrides.on_submit_delivery_note"
},
"*": {
"before_print": "d2h.api.before_print",
Expand All @@ -240,4 +241,4 @@

app_include_js = "/assets/d2h/js/form.js"

doctype_js = {"Purchase Receipt" : "public/js/purchase_receipt.js", "Purchase Order" : "public/js/purchase_order.js", "Delivery Note" : "public/js/delivery_note.js"}
doctype_js = {"Purchase Receipt" : "public/js/purchase_receipt.js", "Purchase Order" : "public/js/purchase_order.js", "Delivery Note" : "public/js/delivery_note.js", "Sales Order" : "public/js/sales_order.js"}
12 changes: 12 additions & 0 deletions d2h/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def on_submit_purchase_receipt(doc, method):
"item_code": item.item_code,
"parent": item.purchase_order
})
if(item_order.custom_good_in_transit_qty > item.qty):
item_order.custom_good_in_transit_qty -= item.qty
else:
item_order.custom_good_in_transit_qty = 0
item_order.save()

def on_submit_delivery_note(doc, method):
for item in doc.items:
item_order = frappe.get_doc("Sales Order Item", {
"item_code": item.item_code,
"parent": item.against_sales_order
})
if(item_order.custom_good_in_transit_qty > item.qty):
item_order.custom_good_in_transit_qty -= item.qty
else:
Expand Down
Loading

0 comments on commit 3b3b8d3

Please sign in to comment.