Stock ledger showing 0 balance if Stock entry created using frappe.get_doc

I have a requirement where we lend out an item to customers.
So, I created a doctype where the user can enter which customer took how much of what item from which warehouse. When the Items are returned, the user just enters the qty as a negative value.
when this doc is submitted a stock entry is passed in the backend.
Here is the code I wrote in the controller,

def before_submit(self):
	items = []
	if self.qty > 0:
		stock_entry_type = "Material Issue"
		items.append({
			"s_warehouse"	: self.warehouse,
			"item_code"		: self.tray,
			"qty"			: self.qty,
			"allow_zero_valuation_rate" : 1
			})
	elif self.qty < 0:
		stock_entry_type = "Material Receipt"
		items.append({
			"t_warehouse"	: self.warehouse,
			"item_code"		: self.tray,
			"qty"			: abs(self.qty),
			"allow_zero_valuation_rate" : 1
			})

	stock = frappe.get_doc({
		"doctype" 	: "Stock Entry",
		"stock_entry_type"	: stock_entry_type,
		"set_posting_time"	: 1,
		"posting_date"	: self.date,
		"items": items
	})

	stock.save(ignore_permissions=True)
	stock.submit()

	self.stock_entry = stock.name

When the entered qty is positive everything is fine, but when the qty entered is negative, the balance qty becomes 0 in stock ledger.


What am I doing wrong?

cc @JayRam @sanjay

I ain’t no developer, so apologies, I can’t help you with this. But I’m sure there are enough people here that can answer your question.

Thanks

Jay

Did you try using frappe.new_doc ?