Prevent Negative Stock for Particular Warehouse in sales invoice transaction

Added one custom check box “Allow Negative Stock” in warehouse
In custom app sales_invoice.py

def validate(self,method):

if self.update_stock:

    check_stock(self)

Function To check stock

def check_stock(self):

over_stock = 0

out_of_stock = []

for item in self.items:

    

    allow_negative = frappe.db.get_value("Warehouse",item.warehouse,"allow_negative_stock")

    if not allow_negative:

        warehouse_stock = get_stock_balance(item.item_code,item.warehouse,self.posting_date,self.posting_time)

        maintain_stock = frappe.db.get_value("Item",item.item_code,"is_stock_item")

        # frappe.msgprint(str(warehouse_stock)+" "+str(item.qty))

        if item.qty > warehouse_stock and maintain_stock:

            over_stock = 1

            out_of_stock.append({

                "item_code":item.item_code,

                "warehouse":item.warehouse,

                "qty":item.qty,

                "warehouse_qty":warehouse_stock,

                "row":item.idx

            })

if over_stock == 1:

    # frappe.throw(str(out_of_stock))

    table = """<h3>Negative Stock Alert</h3><br><table class="table-bordered"><tr>

    <th>Row No.</th>

    <th>Item Code</th>

    <th>Warehouse</th>

    <th>Warehouse Qty</th>

    <th>Require Qty</th>

    </tr>"""

    for item in out_of_stock:

        table = table + """<tr>

        <td>{0}</td>

        <td>{1}</td>

        <td>{2}</td>

        <td>{3}</td>

        <td>{4}</td>

        </tr>""".format(str(item['row']),str(item['item_code']),str(item['warehouse']),str(item['warehouse_qty']),str(item['qty']))

    table = table + """</table>"""

    frappe.throw(table)