<?php
 include("db.php");
 include("../pdate.php");
  $startDate="1397-01-01";
 $pdate1 = pdate("Y-m-d");
	 //=========================موجودی اولیه دخل=========================================
	 //==================================================================================
	//موجودی دخل کنونی
	    //dollar (2)
	       $assets_dollar_sql="select  * from assetopeningbalance where currencyUnit='2'  ";
	       $assets_dollar_query= $db ->query($assets_dollar_sql);
		   $assets_dollar_row=$assets_dollar_query ->fetch_array(); 
		   $assets_dollar_amount=$assets_dollar_row['amount'];
		    
	 //afghani (1)
	       $assets_af_sql="select  * from assetopeningbalance where currencyUnit='1'  ";
	       $assets_af_query= $db ->query($assets_af_sql);
		   $assets_af_row=$assets_af_query ->fetch_array(); 
		   $assets_af_amount=$assets_af_row['amount'];
		   
		   
	 //toman (5)
	       $assets_toman_sql="select  * from assetopeningbalance where currencyUnit='5'  ";
	       $assets_toman_query= $db ->query($assets_toman_sql);
		   $assets_toman_row=$assets_toman_query ->fetch_array(); 
		   $assets_toman_amount=$assets_toman_row['amount'];
		    
		  
	 //Pakistan (4)
	       $assets_pakistn_sql="select  * from assetopeningbalance where currencyUnit='4'  ";
	       $assets_pakistan_query= $db ->query($assets_pakistn_sql);
		   $assets_pakistan_row=$assets_pakistan_query ->fetch_array(); 
		   $assets_pakistan_amount=$assets_pakistan_row['amount'];
		   
	 //Euro (3)
	       $assets_euro_sql="select  * from assetopeningbalance where currencyUnit='3'  ";
	       $assets_euro_query= $db ->query($assets_euro_sql);
		   $assets_euro_row=$assets_euro_query ->fetch_array(); 
		   $assets_euro_amount=$assets_euro_row['amount'];
	//=====================opening Balance dakhle========================
            //*************************مجموع امد افغانی*****************//          
		  //afghani recive
		        $af_recive_sql="SELECT sum(amount) FROM `customerpayment` where remove='0' and  transacttype='1'  and transactCurrency='1' and  (date between '$startDate' and '$pdate1') "; 
                $af_recive_query= $db ->query($af_recive_sql);
				$af_recive_row=$af_recive_query ->fetch_array();
				$af_recive=$af_recive_row['sum(amount)'];  
				 
			//خریداریهای افغانی
			     
				$afBuy_sql="select sum(counterAmount) from transaction where counterCurrency='1' and  (date between '$startDate' and '$pdate1') and remove='0' and treatment='1' and extra='0'  "; 
				$afBuy_query=$db ->query($afBuy_sql);
				$afBuy_row=$afBuy_query ->fetch_array();
				$afBuy=$afBuy_row['sum(counterAmount)'];		
				 
            //اضافه شدن از فروش سایر پولها به افغانی
			   ///echo "<br>amad az sayer poolha:";
				 $ezafe_AF_azfrosh_sql="select sum(baseAmount) from transaction where baseCurrency='1' and   (date between '$startDate' and '$pdate1') and remove='0' and treatment='2' and extra='0'  ";
				$ezafe_AF_azfrosh_query=$db ->query($ezafe_AF_azfrosh_sql);
				$ezafe_AF_azfrosh_row=$ezafe_AF_azfrosh_query ->fetch_array();
				$ezafe_AF_azfrosh=$ezafe_AF_azfrosh_row['sum(baseAmount)'];		  
				 
				//اضافه شده به افغانی از بخش درامدهای متفرقه
				$totalIncome_sql="select sum(amount) from others where currencyId='1' and  typeOthers='2' and  (date between '$startDate' and '$pdate1') and remover='0'    ";
				$totalIncome_query=$db ->query($totalIncome_sql);
				$totalIncome_row=$totalIncome_query ->fetch_array();
			    $totalIncome=$totalIncome_row['sum(amount)'];	
				$avordegi_AF=$af_recive+$afBuy+$ezafe_AF_azfrosh+$totalIncome;
				 
            //*************************انتهای مجموع امد از افغانی*****************//          
             //******** مجموع از رفته افغانی*************************************		
  			 //afghani payment
		        $af_payment_sql="SELECT sum(amount) FROM `customerpayment` where remove='0' and  transacttype='2'  and transactCurrency='1' and (date between '$startDate' and '$pdate1')"; 
                $af_payment_query= $db ->query($af_payment_sql);
				$af_payment_row=$af_payment_query ->fetch_array();
				$af_payment=$af_payment_row['sum(amount)'];
			//خریداری از پول افغانی(کم شدن از افغانی)
				$afSales_sql="select sum(baseAmount) from transaction where baseCurrency='1' and (date between '$startDate' and '$pdate1') and remove='0'  and extra='0' and treatment='1' ";
				$afSales_query=$db ->query($afSales_sql);
				$afSales_row=$afSales_query ->fetch_array();
				$afSales=$afSales_row['sum(baseAmount)'];
             //فروش از افغانی				
 			    $frosh_as_AF_sql="select sum(counterAmount) from transaction where counterCurrency='1' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='2' and extra='0'  ";
				$frosh_as_AF_query=$db ->query($afBuy_sql);
				$frosh_as_AF_row=$afBuy_query ->fetch_array();
				$frosh_as_AF=$afBuy_row['sum(counterAmount)'];	
			 //پرداخت مصارف ازافغانی
				$totalExpenses_sql="select sum(amount) from others where currencyId='1' and  typeOthers='1' and (date between '$startDate' and '$pdate1') and remover='0'    ";
				$totalExpenses_query=$db ->query($totalExpenses_sql);
				$totalExpenses_row=$totalExpenses_query ->fetch_array();
				$totalExpenses=$totalExpenses_row['sum(amount)'];	
				 
			    $bordegi_AF=($af_payment+$afSales+$frosh_as_AF+$totalExpenses); 
               //================حساب نهایی دخل افغانی
			  
			   $finalAccount_AFN=($avordegi_AF+$assets_af_amount )-$bordegi_AF;
               //================حساب نهایی دخل افغانی
	  //==============================================================================
     //========================انتهای حسابهای افغانی==========================================
                //*************************مجموع امد دالر*****************//          
		  //dollar recive
		        $dollar_recive_sql="SELECT sum(amount) FROM `customerpayment` where remove='0' and  transacttype='1'  and transactCurrency='2' and (date between '$startDate' and '$pdate1')"; 
                $dollar_recive_query= $db ->query($dollar_recive_sql);
				$dollar_recive_row=$dollar_recive_query ->fetch_array();
				$dollar_recive=$dollar_recive_row['sum(amount)'];
			 
			//خریداری های دلار	
			    $dollarBuy_sql="select sum(counterAmount) from transaction where counterCurrency='2' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='1' and extra='0'";
		    	$dollarBuy_query=$db ->query($dollarBuy_sql);
				$dollarBuy_row=$dollarBuy_query ->fetch_array();
				$dollarBuy=$dollarBuy_row['sum(counterAmount)'];	
            //اضافه شدن از فروش سایر پولها به افغانی
				$ezafe_dollar_azfrosh_sql="select sum(baseAmount) from transaction where baseCurrency='2' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='2' and extra='0'  ";
				$ezafe_dollar_azfrosh_query=$db ->query($ezafe_dollar_azfrosh_sql);
				$ezafe_dollar_azfrosh_row=$ezafe_dollar_azfrosh_query ->fetch_array();
				$ezafe_dollar_azfrosh=$ezafe_dollar_azfrosh_row['sum(baseAmount)'];		
				 //اضافه شدن به دالر از بخش درامدهای متفرقه 
				$totalIncome_dollar_sql="select sum(amount) from others where currencyId='2' and  typeOthers='2' and (date between '$startDate' and '$pdate1') and remover='0'    ";
				$totalIncome_dollar_query=$db ->query($totalIncome_dollar_sql);
				$totalIncome_dollar_row=$totalIncome_dollar_query ->fetch_array();
				$totalIncome_dollar=$totalIncome_dollar_row['sum(amount)'];	 
				
				$avordegi_dollar=$dollar_recive+$dollarBuy+$ezafe_dollar_azfrosh+$totalIncome_dollar;
            //*************************انتهای مجموع امد از افغانی*****************//          
            //---------------------------------------------------------------------
			//******** مجموع از رفته دالر*************************************		
			 
				//dollar payment
		        $dollar_payment_sql="SELECT sum(amount) FROM `customerpayment` where remove='0' and  transacttype='2'  and transactCurrency='2' and (date between '$startDate' and '$pdate1')"; 
                $dollar_payment_query= $db ->query($dollar_payment_sql);
				$dollar_payment_row=$dollar_payment_query ->fetch_array();
				$dollar_payment=$dollar_payment_row['sum(amount)'];
			//خریداری از دلار وکم شدن از دلار
				$dollar_Sales_sql="select sum(baseAmount) from transaction where baseCurrency='2' and (date between '$startDate' and '$pdate1') and remove='0'  and extra='0' and treatment='1' ";
				$dollar_Sales_query=$db ->query($dollar_Sales_sql);
				$dollar_Sales_row=$dollar_Sales_query ->fetch_array();
				$dollar_Sales=$dollar_Sales_row['sum(baseAmount)'];
             //فروش دلار				
 			    $frosh_dollar_sql="select sum(counterAmount) from transaction where counterCurrency='2' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='2' and extra='0'  ";
				$frosh_dollar_query=$db ->query($frosh_dollar_sql);
				$frosh_dollar_row=$frosh_dollar_query ->fetch_array();
				$frosh_dollar=$frosh_dollar_row['sum(counterAmount)'];	
                //پرداخت مصارف از دالر
				$totalExpenses_dollar_sql="select sum(amount) from others where currencyId='2' and  typeOthers='1' and (date between '$startDate' and '$pdate1') and remover='0'    ";
				$totalExpenses_dollar_query=$db ->query($totalExpenses_dollar_sql);
				$totalExpenses_dollar_row=$totalExpenses_dollar_query ->fetch_array();
				$totalExpenses_dollar=$totalExpenses_dollar_row['sum(amount)'];	 
				$bordegi_dollar=($dollar_payment+$dollar_Sales+$frosh_dollar+$totalExpenses_dollar); 
                
 
				$finalAccount_dollar=($avordegi_dollar+$assets_dollar_amount )-$bordegi_dollar;
 //========================================TOMAMN=====================================================================
//====================================================================================================================
          //*************************مجموع امد تومان*****************//          
		  //Toman recive
		        $toman_recive_sql="SELECT sum(amount) FROM `customerpayment` where remove='0' and  transacttype='1'  and transactCurrency='5' and (date between '$startDate' and '$pdate1')"; 
                $toman_recive_query= $db ->query($toman_recive_sql);
				$toman_recive_row=$toman_recive_query ->fetch_array();
				$toman_recive=$toman_recive_row['sum(amount)'];
			 
			//خریداریهای تومان
			    $toman_Buy_sql="select sum(counterAmount) from transaction where counterCurrency='5' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='1' and extra='0'";
		    	$toman_Buy_query=$db ->query($toman_Buy_sql);
				$toman_Buy_row=$toman_Buy_query ->fetch_array();
				$toman_Buy=$toman_Buy_row['sum(counterAmount)'];	
            
            //اضافه شدن از فروش سایر پولها به تومان
				$ezafe_toman_azfrosh_sql="select sum(baseAmount) from transaction where baseCurrency='5' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='2' and extra='0'  ";
				$ezafe_toman_azfrosh_query=$db ->query($ezafe_toman_azfrosh_sql);
				$ezafe_toman_azfrosh_row=$ezafe_toman_azfrosh_query ->fetch_array();
				$ezafe_toman_azfrosh=$ezafe_toman_azfrosh_row['sum(baseAmount)'];		
			//اضافه شدن به تومان از بخش درآمدهای متفرقه
				$totaltoman_toman_sql="select sum(amount) from others where currencyId='5' and  typeOthers='2' and (date between '$startDate' and '$pdate1') and remover='0'    ";
				$totaltoman_toman_query=$db ->query($totaltoman_toman_sql);
				$totaltoman_toman_row=$totaltoman_toman_query ->fetch_array();
				$totaltoman_toman=$totaltoman_toman_row['sum(amount)'];	  
				
				$avordegi_toman=$toman_recive+$toman_Buy+$ezafe_toman_azfrosh+$totaltoman_toman;
            //*************************انتهای مجموع امد از تومان*****************//          
            //---------------------------------------------------------------------
			//******** مجموع رفت از تومان*************************************		
			 
				//toman payment
		        $toman_payment_sql="SELECT sum(amount) FROM `customerpayment` where remove='0' and  transacttype='2'  and transactCurrency='5' and (date between '$startDate' and '$pdate1')"; 
                $toman_payment_query= $db ->query($toman_payment_sql);
				$toman_payment_row=$toman_payment_query ->fetch_array();
				$toman_payment=$toman_payment_row['sum(amount)'];
			//خریداری از تومان و کم شدن از تومان
				$toman_Sales_sql="select sum(baseAmount) from transaction where baseCurrency='5' and (date between '$startDate' and '$pdate1') and remove='0'  and extra='0' and treatment='1' ";
				$toman_Sales_query=$db ->query($toman_Sales_sql);
				$toman_Sales_row=$toman_Sales_query ->fetch_array();
				$toman_Sales=$toman_Sales_row['sum(baseAmount)'];
             //فروش تومان			
 			    $frosh_toman_sql="select sum(counterAmount) from transaction where counterCurrency='5' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='2' and extra='0'  ";
				$frosh_toman_query=$db ->query($frosh_toman_sql);
				$frosh_toman_row=$frosh_toman_query ->fetch_array();
				$frosh_toman=$frosh_toman_row['sum(counterAmount)'];	
                 //پرداخت مصارف از تومان
				$totalExpenses_toman_sql="select sum(amount) from others where currencyId='5' and  typeOthers='1' and (date between '$startDate' and '$pdate1') and remover='0'    ";
				$totalExpenses_toman_query=$db ->query($totalExpenses_toman_sql);
				$totalExpenses_toman_row=$totalExpenses_toman_query ->fetch_array();
				$totalExpenses_toman=$totalExpenses_toman_row['sum(amount)'];	  

				$bordegi_toman=($toman_payment+$toman_Sales+$frosh_toman+$totalExpenses_toman); 

			 	$finalAccount_toman=($avordegi_toman+$assets_toman_amount )-$bordegi_toman;
			//=================================================END OF PAKISTANIN==============================	
             //========================================PAKISTANIN=========================================
			//================================================================================================
          //*************************مجموع امد کالدار*****************//          
		  //pakistan recive
		        $pak_recive_sql="SELECT sum(amount) FROM `customerpayment` where remove='0' and  transacttype='1'  and transactCurrency='4' and (date between '$startDate' and '$pdate1')"; 
                $pak_recive_query= $db ->query($pak_recive_sql);
				$pak_recive_row=$pak_recive_query ->fetch_array();
				$pak_recive=$pak_recive_row['sum(amount)'];
 
			//خریداریهای کالدار	
			    $pak_Buy_sql="select sum(counterAmount) from transaction where counterCurrency='4' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='1' and extra='0'";
		    	$pak_Buy_query=$db ->query($pak_Buy_sql);
				$pak_Buy_row=$pak_Buy_query ->fetch_array();
				$pak_Buy=$pak_Buy_row['sum(counterAmount)'];
 
            //اضافه شدن از فروش سایر پولها به افغانی
            //اضافه شدن از فروش سایر پولها به کالدار
 
				$ezafe_pak_azfrosh_sql="select sum(baseAmount) from transaction where baseCurrency='4' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='2' and extra='0'  ";
				$ezafe_pak_azfrosh_query=$db ->query($ezafe_pak_azfrosh_sql);
				$ezafe_pak_azfrosh_row=$ezafe_pak_azfrosh_query ->fetch_array();
				$ezafe_pak_azfrosh=$ezafe_pak_azfrosh_row['sum(baseAmount)'];		
			//اضافه شدن به کالدار از بخش درآمدهای متفرقه
				$totalInomce_pakistan_sql="select sum(amount) from others where currencyId='4' and  typeOthers='2' and (date between '$startDate' and '$pdate1') and remover='0'    ";
				$totalInomce_pakistan_query=$db ->query($totalInomce_pakistan_sql);
				$totalInomce_pakistan_row=$totalInomce_pakistan_query ->fetch_array();
				$totalInomce_pakistan=$totalInomce_pakistan_row['sum(amount)'];	  
				
				$avordegi_pakistan=$pak_recive+$pak_Buy+$ezafe_pak_azfrosh+$totalInomce_pakistan;
            //*************************انتهای مجموع امد از کالدار*****************//          
            //---------------------------------------------------------------------
			//******** مجموعه رفت از کالدار*************************************		
			 
				//pakistan payment
		        $pakistan_payment_sql="SELECT sum(amount) FROM `customerpayment` where remove='0' and  transacttype='2'  and transactCurrency='4' and (date between '$startDate' and '$pdate1')"; 
                $pakistan_payment_query= $db ->query($pakistan_payment_sql);
				$pakistan_payment_row=$pakistan_payment_query ->fetch_array();
				$pakistan_payment=$pakistan_payment_row['sum(amount)'];
			//خریداری از کالدار و کم شدن از کالدار
				$pkistan_Sales_sql="select sum(baseAmount) from transaction where baseCurrency='4' and (date between '$startDate' and '$pdate1') and remove='0'  and extra='0' and treatment='1' ";
				$pakistan_Sales_query=$db ->query($pkistan_Sales_sql);
				$pakistan_Sales_row=$pakistan_Sales_query ->fetch_array();
				$pakistan_Sales=$pakistan_Sales_row['sum(baseAmount)'];
             //فروش کالدار			
 			    $frosh_pakistan_sql="select sum(counterAmount) from transaction where counterCurrency='1' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='2' and extra='0'  ";
				$frosh_pakistan_query=$db ->query($frosh_pakistan_sql);
				$frosh_pakistan_row=$frosh_pakistan_query ->fetch_array();
				$frosh_pakistan=$frosh_pakistan_row['sum(counterAmount)'];	
             //پرداخت مصارف از کالدار
				$totalExpenses_pakistan_sql="select sum(amount) from others where currencyId='4' and  typeOthers='1' and (date between '$startDate' and '$pdate1') and remover='0'    ";
				$totalExpenses_pakistan_query=$db ->query($totalExpenses_pakistan_sql);
				$totalExpenses_pakistan_row=$totalExpenses_pakistan_query ->fetch_array();
				$totalExpenses_pakistan=$totalExpenses_pakistan_row['sum(amount)'];	  
				
				$bordegi_pakistan=($pakistan_payment+$pakistan_Sales+$frosh_pakistan+$totalExpenses_pakistan); 
				 
			 	echo $finalAccount_pakistan=($avordegi_pakistan+$assets_pakistan_amount )-$bordegi_pakistan;
			//=================================================END OF PAKISTANIN==============================	
				//========================================Ero=========================================
			//================================================================================================
          //*************************مجموع امد یورو*****************//          
		  //Euro recive
		        $euro_recive_sql="SELECT sum(amount) FROM `customerpayment` where remove='0' and  transacttype='1'  and transactCurrency='3' and (date between '$startDate' and '$pdate1')"; 
                $euro_recive_query= $db ->query($euro_recive_sql);
				$euro_recive_row=$euro_recive_query ->fetch_array();
				$euro_recive=$euro_recive_row['sum(amount)'];
			 
			//خریداریهای یورو
			    $euro_Buy_sql="select sum(counterAmount) from transaction where counterCurrency='3' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='1' and extra='0'";
		    	$euro_Buy_query=$db ->query($euro_Buy_sql);
				$euro_Buy_row=$euro_Buy_query ->fetch_array();
				$euro_Buy=$euro_Buy_row['sum(counterAmount)'];	
            
            //اضافه شدن از فروش سایر پولها به یورو
				$ezafe_euro_azfrosh_sql="select sum(baseAmount) from transaction where baseCurrency='3' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='2' and extra='0'  ";
				$ezafe_euro_azfrosh_query=$db ->query($ezafe_euro_azfrosh_sql);
				$ezafe_euro_azfrosh_row=$ezafe_euro_azfrosh_query ->fetch_array();
				$ezafe_euro_azfrosh=$ezafe_euro_azfrosh_row['sum(baseAmount)'];		
			//اضافه شدن به یورو از بخش درآمدهای متفرقه
				$totalIncome_euro_sql="select sum(amount) from others where currencyId='3' and  typeOthers='2' and (date between '$startDate' and '$pdate1') and remover='0'    ";
				$totalIncome_euro_query=$db ->query($totalIncome_euro_sql);
				$totalIncome_euro_row=$totalIncome_euro_query ->fetch_array();
				$totalIncome_euro=$totalIncome_euro_row['sum(amount)'];	  
				
				$avordegi_euro=$euro_recive+$euro_Buy+$ezafe_euro_azfrosh+$totalIncome_euro;
            //*************************انتهای مجموع امد از تومان*****************//          
            //---------------------------------------------------------------------
			//******** مجموع رفت از یورو*************************************		
			 
				//euro payment
		        $euro_payment_sql="SELECT sum(amount) FROM `customerpayment` where remove='0' and  transacttype='2'  and transactCurrency='3' and (date between '$startDate' and '$pdate1')"; 
                $euro_payment_query= $db ->query($euro_payment_sql);
				$euro_payment_row=$euro_payment_query ->fetch_array();
				$euro_payment=$euro_payment_row['sum(amount)'];
			//خریداری از یورو و کم شدن از یورو
				$euro_Sales_sql="select sum(baseAmount) from transaction where baseCurrency='3' and (date between '$startDate' and '$pdate1') and remove='0'  and extra='0' and treatment='1' ";
				$euro_Sales_query=$db ->query($euro_Sales_sql);
				$euro_Sales_row=$euro_Sales_query ->fetch_array();
				$euro_Sales=$euro_Sales_row['sum(baseAmount)'];
             //فروش یورو		
 			    $frosh_euro_sql="select sum(counterAmount) from transaction where counterCurrency='3' and (date between '$startDate' and '$pdate1') and remove='0' and treatment='2' and extra='0'  ";
				$frosh_euro_query=$db ->query($frosh_euro_sql);
				$frosh_euro_row=$frosh_euro_query ->fetch_array();
				$frosh_euro=$frosh_euro_row['sum(counterAmount)'];	
                 //پرداخت مصارف از یورو
				$totalExpenses_euro_sql="select sum(amount) from others where currencyId='3' and  typeOthers='1' and (date between '$startDate' and '$pdate1') and remover='0'    ";
				$totalExpenses_euro_query=$db ->query($totalExpenses_euro_sql);
				$totalExpenses_euro_row=$totalExpenses_euro_query ->fetch_array();
				$totalExpenses_euro=$totalExpenses_euro_row['sum(amount)'];	  
				
				$bordegi_euro=($euro_payment+$euro_Sales+$frosh_euro+$totalExpenses_euro);
 
				$finalAccount_euro=($avordegi_euro+$assets_euro_amount )-$bordegi_euro;

?>