<?php
require("include/config_db.php");

function export_rpt_product_list($db){

extract($_REQUEST);

	
	$filename = 'productlist'; 
	$filename=$filename."_".date('ymd').time().".csv";	
	$csv_terminated = "\n";
	$csv_separator = ",";
	$csv_enclosed = '"';
	$csv_escaped = "\\";
	$output			= "";
	

		$constr="";
	if($_REQUEST['srch_FinancialYear']!= ""){
		$constr.=" and fy.financial_id='".$_REQUEST['srch_FinancialYear']."' ";
		}

	if($_REQUEST['startDate'] != "" )
		{
			//print_r($_REQUEST['daterange']); exit;
			//$datesplt = explode(' - ',$_REQUEST['daterange']);
			$tempdate=explode("/",$_REQUEST['startDate']);
			$fromdate=$tempdate[2].'-'.$tempdate[1]."-".$tempdate[0];
			
			$tempdate=explode("/",$_REQUEST['endDate']);
			$todate=$tempdate[2].'-'.$tempdate[1]."-".$tempdate[0];
			
			$constr.=" and date(t2.p_invoice_date) between '".$fromdate."' and '".$todate."' ";
		
		} 
				
		if($_REQUEST['srch_sup_company']!= ""){
		$constr.=" and t3.company_id='".$_REQUEST['srch_sup_company']."' ";
		}
		if($_REQUEST['srch_stock']!= ""){
			
			if($_REQUEST['srch_stock']=="Available")
				$constr.=" and s.s_bill_no is null ";
			else if($_REQUEST['srch_stock']=="Sold")
				$constr.=" and s.s_bill_no is not null ";
			 
		}
		
		


	
	$str_all=" SELECT 'sno', concat (year (f_start_date), ' - ', year (f_end_date)) AS fyear,
       t3.company_name,
       date_format (t2.p_invoice_date, '%d-%b-%Y') AS disp_inv_date,
       t2.actu_inv_no as p_invoice_no, t1.pdt_sku,t1.saree_desc,t4.sup_name,t1.purchase_rate,
       if(s.s_bill_det_id is null,'Available','Sold') as status,s.actu_bill_no,
			date_format (s.s_bill_date, '%d-%b-%Y') AS sale_date,s.cus_name,
			s.sale_price,@discnt:=round(((s.sale_price*s.discount_per)/100),2) as discount,
			@totprice:=round((s.sale_price-@discnt),2) as discountprice,
			@taxamt:=round(((@totprice*s.cgst_per)/100)+((@totprice*s.sgst_per)/100)+((@totprice*s.igst_per)/100),2) as taxamt,
			round((@totprice+@taxamt),2) as soldprice
  FROM t_purchase_entry_details t1
       INNER JOIN
       t_purchase_entry_main t2 ON t1.p_invoice_no = t2.p_invoice_no
       INNER JOIN
       m_company t3 ON t2.company_id = t3.company_id
       INNER JOIN
       m_financial_year fy ON fy.financial_id = t2.financial_id
			inner join m_supplier t4 on t2.sup_id = t4.sup_id
       LEFT JOIN
       (SELECT sd.s_bill_det_id, t1.s_bill_no,t1.actu_bill_no, t1.s_bill_date, t1.total_sale_amount,
               t1.after_discount_amt, t1.tot_tax_amt, t1.final_sale_amt,
               t3.company_name, t4.cus_name, t4.cus_gstno, sd.pdt_sku,
               sd.purchase_price, sd.sale_price, t1.financial_id,t1.discount_per,t1.cgst_per,t1.sgst_per,t1.igst_per
          FROM t_sales_entry_main t1
               INNER JOIN
               t_sales_entry_details sd ON sd.financial_id = t1.financial_id AND sd.s_bill_no = t1.s_bill_no
               INNER JOIN
               m_company t3 ON t1.company_id = t3.company_id
               INNER JOIN
               m_customer t4 ON t1.cus_id = t4.cus_id
         WHERE t1.isactive = 1) s ON s.pdt_sku = t1.pdt_sku AND s.financial_id = t1.financial_id
			
 WHERE  t1.isactive = 1
   AND t2.isactive = 1 ".$constr;

		
		
		$str_all .= " order by t2.p_invoice_date desc,t1.pdt_sku desc ";	
				
		$sql = $db->get_rsltset($str_all);	  	 
	$columns_total 	=  $db->get_columnCount($str_all);					
  
  
	// Get The Field Name
	$output .= '"S.no","Financial Year","Company Name","Purchase Date","Purchase Bill No","Product SKU","Saree Description","Supplier Name","Purchase Rate","Stock Availability","Sales Bill No","Sales Bill Date","Customer Name","Sale Rate","Discount","Sale Rate(After Discount)","Tax ","Final Sold Rate"';
	$output .="\n";
	// Get Records from the table
	$j = 1;
	foreach($sql as $row) {
		for ($i = 0; $i < $columns_total; $i++) {		
		
			if($i==0){
				$output .= $j.','; 
			}
			else{
				$output .= '"'.$row["$i"].'",';		
				//if($i
			}
		}

		$output .="\n";
		$j++;
	}
	


header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;
exit;
}

?>