UPDATE sale_vs_item SET deleted = '1' WHERE saleId = '1358'; ###UPDATE sale_vs_return SET deleted = '1' WHERE saleId = '1358'; ### UPDATE sale_vs_item SET id = '6277', purchase_vs_itemId = '767', selling_type = 'Pallet 8 Ct', qty = '10', item_unit_cost = '2.00 / 10.00', price = '3.00', total = '30.00',  deleted = '0' WHERE id = '6277'; ### UPDATE sale_vs_item SET id = '6278', purchase_vs_itemId = '768', selling_type = 'Box 16  Ct', qty = '20', item_unit_cost = '3.00 / 20.00', price = '4.00', total = '80.00',  deleted = '0' WHERE id = '6278'; ###UPDATE sale SET code = 'INV001358', customer_id = '201', sale_date = '2024-09-28', due_date = '2024-10-28', email = 'cabrera.yeison@gmail.com', send_email = '', bill_to_address = '145 Main St', bill_to_apt = '1', bill_to_city = 'Brooklyn', bill_to_state = 'NY', bill_to_zip = '11204', ship_to_address = '145 Main St', ship_to_apt = '1', ship_to_city = 'Brooklyn', ship_to_state = 'NY', ship_to_zip = '11204', grand_total = '110.00', total_profit_pct = '30.00 / %37.50', ship_via = '', shipping_date = '0000-00-00', shipping_time = '00:00:00', shipping_status = 'preparing', terms = '30', delivered_date = '0000-00-00', tracking_number = '', email_message = '', notes = '', sale_statement = 'ALL RETURNS, CREDITS OR CHANGES MUST BE APPROVED BY YOUR SALESMAN AND SENT BACK TO US WITHIN 48 HOURS OR THEY WILL NOT BE ACCEPTED OR APPROVED UNDER ANY CIRCUMSTANCES.', sale_footer = 'The perishable agricultural comm. listed on this invoice. are sold subject to the statutory trust authority. by Section 5 of the Perishable Agricultural Commodities Act,1930(7 U.S.C. 499 e).The seller of these comm. retains a trust claim over these comm.,all inventories of food or other products derived from these comm.,and any receivables or proceeds from the sale of these comm. until full payment. is received.' WHERE id = '1358'###insert into entity_audit (entity, entity_id, code, action, changes, user_id) 
                        values ('sale',1358, 'INV001358','UPDATE', '{"userId":"131","input":[],"mt":[{"id":"sale_form_sale_vs_return","from":[{"id":"17","sale_vs_itemId":["6277"],"lost_qty":"2","lost_price":"6.00","date":"2024-09-28","notes":"1"},{"id":"18","sale_vs_itemId":["6278"],"lost_qty":"2","lost_price":"8.00","date":"2024-09-28","notes":"2"}],"to":[]}]}', 131)###
   	UPDATE sale
	SET grand_total = (
		SELECT SUM(svi.total)
		FROM sale_vs_item svi
		WHERE svi.saleId = '1358'
		AND svi.deleted = '0'
	)
	WHERE sale.id = '1358'
	AND sale.deleted = '0'
###
   	UPDATE sale_vs_item
SET 
    returned_qty = (
       COALESCE(
          (SELECT SUM(sale_vs_return.lost_qty)
           FROM sale_vs_return
           WHERE sale_vs_return.sale_vs_itemId = sale_vs_item.id
           AND sale_vs_return.deleted = '0'), 0
       )
    )
WHERE sale_vs_item.saleId = '1358'
AND sale_vs_item.deleted = '0'
