<?php


include("config.php");

calculateMonthlyEarningOfBrands();
    
function calculateMonthlyEarningOfBrands() {
  
 
 $query="SELECT order_meta.brand_id as brand_id, 
 SUM(CASE
    WHEN order_meta.discounted_amount <= '0' THEN order_meta.quantity * order_meta.price
    ELSE order_meta.discounted_amount
    END
 ) as total_amount ,
 SUM(order_meta.quantity) as total_sales 
 
 FROM OrderMeta as order_meta
 INNER JOIN ListOfOrders as list_of_orders ON list_of_orders.status = '0' AND list_of_orders.id = order_meta.term_id
 WHERE DATE(list_of_orders.date_created) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH) 
 GROUP BY order_meta.brand_id";
 $result=queryRunner($query);
 while ($row = rowRetriever($result)) { 
      
      $insert_query="INSERT INTO BrandMonthlyEarning(brand_id,total_sales,total_earning) VALUES('brand_id','total_amount','total_sales')";
      $result_query=queryRunner($insert_query);
      
      
  }

  

}


?>