class OrderDetails(Document):
def validate(self):
# Remove duplicates
found={}
i=-1
for selected in self.adding:
i=i+1
if selected.selected in found:
#frappe.throw(_("Duplicate {0}").format(i))
self.adding[found[selected.selected]].quantity = self.adding[found[selected.selected]].quantity + self.adding[i].quantity
### I am replacing the duplicate values and replacing value with 'product-04' and changing quantity by adding quantity to the original and replacing it to 0
### Process - 1
self.adding[i].quantity = 0
self.adding[i].selected = 'product-04'
if selected.selected not in found:
found[selected.selected] = i
### Process - 2
### removing 'product-04' from database.
q='product-04'
frappe.db.sql("DELETE FROM `tabAdd to Order` WHERE selected = (%s);" ,(q))
for selected in self.adding:
### Process - 3
### Decrementing the quantity from the quantity in inventory and replacing it in database
quantity_on_hand_f = frappe.db.get_value("Product Details", selected.selected, "quantity_on_hand")
product_name_f = frappe.db.get_value("Product Details", selected.selected, "product_name")
if quantity_on_hand_f - self.adding[found[selected.selected]].quantity < 0:
frappe.throw(_("Quantity on Hand {0} is less than orderd amount ").format(quantity_on_hand_f))
else :
quantity_on_hand_f = quantity_on_hand_f - self.adding[found[selected.selected]].quantity
frappe.db.sql("Update `tabProduct Details` Set quantity_on_hand = (%s) Where product_name =(%s); ",(quantity_on_hand_f, product_name_f))
My problems after pressing ‘save’ once, only process of replacing duplicates (process -1) and change in inventory (process - 3) takes place and all the duplicates marked ‘product-04’ are still present without getting deleted from database. So, I have to press save again to remove this ‘product-04’ from database which in turn again change th inventory (process -3) runs again.
- Here Orange is product-03
1)Original (before save) ::
Inventory:
After 1st save ::
Inventory:
After 2nd save and reload::
Inventory:
Is there a way through which I can complete all this processes in a single save and the value in inventory also gets deducted only once.