select sum(`orders_voucher`.`amount`) as aggregate from `orders` inner join (
                SELECT osh.orders_id, os.orders_status_name
                FROM orders_status_history osh
                JOIN orders_status os ON osh.orders_status_id = os.orders_status_id
                WHERE osh.orders_status_history_id = (
                    SELECT MAX(osh2.orders_status_history_id)
                    FROM orders_status_history osh2
                    WHERE osh2.orders_id = osh.orders_id
                )
                AND os.orders_status_name != "Cancelled"
            ) as latest_status on `latest_status`.`orders_id` = `orders`.`orders_id` left join `orders_voucher` on `orders_voucher`.`orders_id` = `orders`.`orders_id` where date(`order_date`) >= '2025-01-14' and date(`order_date`) <= '2025-01-14' and `restaurants_id` = 4 


select `orders_voucher`.`amount`,orders_voucher.orders_id, orders.order_number  from `orders` inner join (
                SELECT osh.orders_id, os.orders_status_name
                FROM orders_status_history osh
                JOIN orders_status os ON osh.orders_status_id = os.orders_status_id
                WHERE osh.orders_status_history_id = (
                    SELECT MAX(osh2.orders_status_history_id)
                    FROM orders_status_history osh2
                    WHERE osh2.orders_id = osh.orders_id
                )
                AND os.orders_status_name != "Cancelled"
            ) as latest_status on `latest_status`.`orders_id` = `orders`.`orders_id` left join `orders_voucher` on `orders_voucher`.`orders_id` = `orders`.`orders_id` where date(`order_date`) >= '2025-01-14' and date(`order_date`) <= '2025-01-14' and `restaurants_id` = 4  
ORDER BY `orders_voucher`.`orders_id` DESC;


select * from `orders`where date(`order_date`) >= '2025-02-12 10:00:00'
      and date(`order_date`) <= '2025-02-12 23:59:00'
      and `restaurants_id` = 4



      select `orders_products`.`products_name`, SUM(orders_products.qty) as quantity, SUM(orders_products.products_price) as item_price, (SELECT SUM(products_options_final_price) 
                        FROM orders_products_options 
                        WHERE orders_products_options.order_product_options_id = orders_products.orders_products_id and option_type != 2
                        ) as options_price, SUM(orders_products.products_price + 
                        COALESCE((SELECT SUM(products_options_final_price) 
                                    FROM orders_products_options 
                                    WHERE orders_products_options.order_product_options_id = orders_products.orders_products_id and option_type != 2
                                ), 0)) as price from `orders_products` inner join (
                SELECT osh.orders_id, os.orders_status_name
                FROM orders_status_history osh
                JOIN orders_status os ON osh.orders_status_id = os.orders_status_id
                WHERE osh.orders_status_history_id = (
                    SELECT MAX(osh2.orders_status_history_id)
                    FROM orders_status_history osh2
                    WHERE osh2.orders_id = osh.orders_id
                )
                AND os.orders_status_name != "Cancelled"
            ) as latest_status on `latest_status`.`orders_id` = `orders_products`.`orders_id` where exists (select * from `orders` where `orders_products`.`orders_id` = `orders`.`orders_id` and `restaurants_id` = 4 and (`order_date` between '2025-10-15 00:00:00' and '2025-10-15 23:59:00')) group by `orders_products`.`products_name` order by `quantity` desc