<?php
session_start();
include("pdate.php");
require_once ('../../config/db_const.php');
require_once ('../../config/class.Database.php');
//$customar_id=$_SESSION['customar_code'];
$customerName=$_SESSION['customerName'];
$customerID=$_GET['customerID'];
 
if($customerID!=0){
	   $customar_id=$customerID ;
	   $customar_id=$_SESSION['customar_code']=$customar_id;
 }
if($customerID==0){
	 $customar_id= $_SESSION['customar_code'];
	
 }





$page_title = "خرید و فروش مشتری";
$db = new Database(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$db->connect();

if (isset($_SESSION['username']) && isset($_SESSION['password'])) {

  
?>

<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en" class="no-js">
<!--<![endif]-->
<!-- BEGIN HEAD -->
<head>
  <script>
	     
									//Dollar Price
//====================================================================================
   function dollarPrice(){
	    $('#ghaymatdollar').hide();
		$.post('retiveDollarrateforSall.php', {
		   rateOfDollar:form.counterCurrency.value
		   },
			function(output){
				$('#ghaymatdollar').html(output).fadeIn(10);
		})
	
	} 

//====================================================================================	
	   
   function findCash(){
	    $('#cash').hide();
		$.post('estimateRate.php', {
		   currencyId:form.basecurrency.value
		   },
			function(output){
				$('#cash').html(output).fadeIn(10);
		})
	
	}  
	
	
	
	  function assetCash(){
	      $('#asset').hide();
		  $.post('customerexictiongAmount.php', {
		  currencyId:form.basecurrency.value,
		  customerGetId:form.customerId.value,
		   },
			  function(output){
			  $('#asset').html(output).fadeIn(10);
		  })
	
	  }  
	
	function get_amount(){
	    $('#rate').hide();
		$.post('retiverateforSell.php', {
		   bCurrency:form.basecurrency.value, 
		   cCurrency:form.counterCurrency.value
		   },
			function(output){
				$('#rate').html(output).fadeIn(10);
		})
	
	}  
	
	
	
	function findingCounterCurrency(){
	    $('#cCounter').hide();
		$.post('estimateCounterRate.php', {
		   countercurrencyId:form.counterCurrency.value
		   },
			function(output){
				$('#cCounter').html(output).fadeIn(10);
		})
	
	}
	
	  
	 </script> 
	 <script>
		  function findingcalculation(){
	     insertval=setInterval("zarb()",1);
	    }
		  function zarb(){
	       amount_p  =document.getElementById('counterAmount').value;
		  var decimalCharacter = ".",
          regex = new RegExp("[\\d" + decimalCharacter + "]+", "g"),
          num_caldar=amount_p;
          var amount=+num_caldar.match(regex).join("");  
		  
		   take_Mode=document.getElementById('mode').value; 
		   
		    if(take_Mode=="mode_1"){
			
		  rate_mixData=document.getElementById('rate_mixData').value; 
		  mixData=document.getElementById('mixData').value; 
		  takeData=mixData.split('/');
		  mainrate=takeData[0];
		  unit=takeData[1];
		  
		  var exactresult=amount/unit;
		    row=(exactresult*1)*(rate_mixData*1);
		    
			pourCalculation=row.toFixed(2);			
			  
		  }
		  
		  if(take_Mode=="mode_2"){
	      
		  rate_mixData=document.getElementById('rate_mixData').value; 
		  mixData=document.getElementById('mixData').value; 
		  takeData=mixData.split('/');
		  mainrate=takeData[0];
		  unit=takeData[1];
		  
		  var exactresult=amount/rate_mixData;
		    row=(exactresult*1)*(mainrate*1);
		    
			pourCalculation=row.toFixed(2);		  
			  
		  }  
		  
		  //=======================================================================
		  
		     //نرخ جدید
		   
		   changeMixData  =document.getElementById('change_currencyPriec').value;
		  
		  
		  if(take_Mode=="mode_1"){
			
		 
		   oldemixData=document.getElementById('mixData').value; 
		   changetakeData =oldemixData.split('/');
		   changemainrate =changetakeData[0];
		   changeunit     =changetakeData[1];
		  
		  var changeexactresult=amount/changeunit;
		    row=(changeexactresult*1)*(changeMixData*1);
		    
			change_pourCalculation=row.toFixed(2);			
			  
		  }
		  
		 if(take_Mode=="mode_2"){
	      
		 
		   oldemixData=document.getElementById('mixData').value; 
		   changetakeData =oldemixData.split('/');
		   changemainrate =changetakeData[0];
		   changeunit     =changetakeData[1];
		  
		   var changeexactresult=amount/changeMixData;
		   row=(changeexactresult*1)*(changemainrate*1);
		    
			change_pourCalculation=row.toFixed(2);		  
			  
		  }    
		   
		   //===========================================================================
		   if(changeMixData!=""){
		  
		   document.getElementById('baceAmount').value=FormatNumberBy3(change_pourCalculation*1) ;
		   
		   }    
		   
		   if(changeMixData==""){
		    
		    document.getElementById('baceAmount').value=FormatNumberBy3(pourCalculation*1) ;
		     
		  }     
		   
		   }   
//====================================================================================	
	
	function calculateDollar(){
	     insertval=setInterval("multipalDollar()",1);
	    }
		function multipalDollar(){
	     //dollarRatePrice=document.getElementById('dollarRatePrice').value;
	    // newdollarRatePrice=document.getElementById('new_dollarRatePrice').value;
		  
		 var amount_p=document.getElementById("counterAmount").value;
         var decimalCharacter = ".",
         regex = new RegExp("[\\d" + decimalCharacter + "]+", "g"),
         num_caldar=amount_p;
        var amount=+num_caldar.match(regex).join("");
		 
		// نرخ قدیم
		  dollarmixData=document.getElementById('dollarmixData').value; 
		  dollartakeData=dollarmixData.split('/');
		  dollarmainrate=dollartakeData[0];
		  dollarunit=dollartakeData[1];
		  
		  var dollarexactresult=amount/dollarunit;
		  
		  
		    //نرخ جدید
		    /* dollarchangeMixData  =document.getElementById('new_dollarRatePrice').value;
		   dollarchangetakeData=dollarchangeMixData.split('/');
		   dollarchangemainrate=dollarchangetakeData[0];
		   dollarchangeunit   =dollarchangetakeData[1];
		   
		   var dollarchangeexactresult=amount/dollarchangeunit;  
		   
		 if(dollarchangeMixData!=""){
		    totalDollar=(dollarchangemainrate*1)*(dollarchangeexactresult*1);
            findTotalDollar=totalDollar.toFixed(2);   
		    document.getElementById('totalDollar').value=findTotalDollar;
		  
		  }  */
		 // if(dollarchangeMixData==""){
		     totalDollar=(dollarmainrate*1)*(dollarexactresult*1);
             findTotalDollar=totalDollar.toFixed(2);   
		     document.getElementById('totalDollar').value=findTotalDollar;
		  
		 // }  
		
		}	
//===============الباقی حساب مشتر===============================		
		function customerBalance(){
	     insertval=setInterval("customerRemain()",1);
	    }
		function customerRemain(){
	     balanceP=document.getElementById('balance').value;  
	     var decimalCharacter = ".",
         regex = new RegExp("[\\d" + decimalCharacter + "]+", "g"),
         num_price=balanceP;  
         var balance=+num_price.match(regex).join("");
	    
 		 baceAmountP  =document.getElementById('baceAmount').value;
		 var decimalCharacter = ".",
         regex = new RegExp("[\\d" + decimalCharacter + "]+", "g"),
         num_price=baceAmountP;  
         var baceAmount=+num_price.match(regex).join("");
		 
		 document.getElementById('remainCustomer').value=FormatNumberBy3((balance*1)-(baceAmount*1));
		
		
		}
    function newrate(){
	   profe=document.getElementById('take').value;
	   finalResult=document.getElementById('currencyPriec').value=(profe*1);
	 }	
	 function buynewrate(){
	   profe=document.getElementById('take').value;
	   finalResult=document.getElementById('buyRate').value=(profe*1);
	 }
//====================================================================	 
	 function dollarsalenewrate(){
	   profe=document.getElementById('takeDollar').value;
	   finalResult=document.getElementById('dollarRatePrice').value=(profe*1);
	 }	
	 function dollarBuy_newrate(){
	   profe=document.getElementById('takeDollar').value;
	   finalResult=document.getElementById('buyRate_dollar').value=(profe*1);
	 }		
//===============الباقی حساب مشتر===============================			
	 </script>
<script type="text/javascript" src="js/decimal.js"></script>
 <script type="text/javascript" src="js/NumberFormat.js"></script> 
<!--=========================================================-->
     <script src="js/jquery/jquery-1.4.1.min.js" type="text/javascript"></script>
	 <script src="js/validation.js"></script>
     <script src="dist/sweetalert-dev.js"></script>
     <link rel="stylesheet" href="dist/sweetalert.css">
     <link rel="stylesheet" href="css/screen.css" type="text/css"   />
<!--=========================================================-->
<title><?php echo $page_title;?></title>
 
<!-- BEGIN GLOBAL MANDATORY STYLES -->
<link href="../../assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
<link href="../../assets/global/plugins/simple-line-icons/simple-line-icons.min.css" rel="stylesheet" type="text/css"/>
<link href="../../assets/global/plugins/bootstrap/css/bootstrap-rtl.min.css" rel="stylesheet" type="text/css"/>
<link href="../../assets/global/plugins/uniform/css/uniform.default.css" rel="stylesheet" type="text/css"/>
<link href="../../assets/global/plugins/bootstrap-switch/css/bootstrap-switch-rtl.min.css" rel="stylesheet" type="text/css"/>
<!-- END GLOBAL MANDATORY STYLES -->
<!-- BEGIN PAGE LEVEL PLUGIN STYLES -->
<link href="../../assets/global/plugins/bootstrap-daterangepicker/daterangepicker-bs3.css" rel="stylesheet" type="text/css"/>
<!-- END PAGE LEVEL PLUGIN STYLES -->
<!-- BEGIN PAGE LEVEL PLUGIN STYLES -->
<link rel="stylesheet" type="text/css" href="../../assets/global/plugins/select2/select2.css"/>
<link rel="stylesheet" type="text/css" href="../../assets/global/plugins/datatables/extensions/Scroller/css/dataTables.scroller.min.css"/>
<link rel="stylesheet" type="text/css" href="../../assets/global/plugins/datatables/extensions/ColReorder/css/dataTables.colReorder.min.css"/>
<link rel="stylesheet" type="text/css" href="../../assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap-rtl.css"/>
<link href="../../assets/global/plugins/bootstrap-daterangepicker/daterangepicker-bs3.css" rel="stylesheet" type="text/css"/>
<!-- END PAGE LEVEL PLUGIN STYLES -->
<!-- BEGIN PAGE LEVEL STYLES -->
<link rel="stylesheet" type="text/css" href="../../assets/global/plugins/select2/select2.css"/>
<!-- END PAGE LEVEL SCRIPTS -->
<!-- BEGIN THEME STYLES -->
<link href="../../assets/global/css/components-md-rtl.css" id="style_components" rel="stylesheet" type="text/css"/>
<link href="../../assets/global/css/plugins-md-rtl.css" rel="stylesheet" type="text/css"/>
<link href="../../assets/admin/layout/css/layout-rtl.css" rel="stylesheet" type="text/css"/>
<link id="style_color" href="../../assets/admin/layout/css/themes/darkblue-rtl.css" rel="stylesheet" type="text/css"/>
<link href="../../assets/admin/layout/css/custom-rtl.css" rel="stylesheet" type="text/css"/>
<!-- END THEME STYLES -->
<link rel="shortcut icon" href="favicon.ico"/>
</head>
<!-- END HEAD -->
<!-- BEGIN BODY -->
<body class="page-md page-header-fixed page-quick-sidebar-over-content">

<?php require_once('header.php');?>

<div class="clearfix">
</div>
<!-- BEGIN CONTAINER -->
<div class="page-container" style="border: 0px solid red;margin-top: -100px;background: #3598dc;">
	<!-- BEGIN SIDEBAR -->
	<?php
	$menu_selected="treat";
	require_once('sidebar.php');?>
	<!-- END SIDEBAR -->
	<!-- BEGIN CONTENT -->
	<div class="page-content-wrapper">
		<div class="page-content">
			<!-- BEGIN PAGE CONTENT-->
			<!--BEGIN PAGE HEADER-->
			<div class="page-bar">
				<ul class="page-breadcrumb">
					<li>
						<i class="fa fa-home"></i>
						<a href="index.php">صفحه اصلی</a>
						<i class="fa fa-angle-left"></i>
					</li>
					<li>
						<a href="customerSearch.php">جستجــوی مشتـــری</a>
					</li>
				</ul>
			</div>
			<!-- END PAGE HEADER-->
			<div class="row">
				<div class="col-md-12">
					<div class="portlet box blue" id="form_wizard_1">
						<div class="portlet-title">
							<div class="caption">
								<i class="fa fa-user"></i> <span style='color:#000'>خریــد و فـروش محتـــرم :</span> <?php echo  $customerName ?><span class="step-title"></span>
							</div>
							<div class="tools hidden-xs">
								<a href="javascript:;" class="collapse">
								</a>
							</div>
						</div>
						<div class="portlet-body form">
							<form action="customerBuySellInsertion.php" class="form-horizontal"  name="form" id="submit_form" method="POST">
								<div class="form-wizard">
									<div class="form-body">
										<div class="tab-content">
											 
											 
											<div class="tab-pane active" id="tab1">
												<h3 class="block">خرید و فروش مشتــری</h3>
												<div class="form-group">
													<label class="control-label col-md-2">تاریخ<span class="required">
													</span>
													</label>
													<div class="col-md-4">
														<input type="text" name="date" placeholder="" class="form-control" value="<?php echo $pdate1 = pdate("Y/n/j");?>"/>
														<span class="help-block">
														</span>
													</div>
												</div>
												<div class="form-group">
													<label class="control-label col-md-2">ارز مورد معاوضه<span class="required">
													</span>
													</label>
													<div class="col-md-4">
													<select name="counterCurrency"  id='counterCurrency'  style="width:100%" onchange="dollarPrice();get_amount();findingCounterCurrency();calculate()" onclick="get_amount();findingCounterCurrency();calculate();dollarPrice()">
													<option></option>
														<?php
															include("config/views/db_const.php");
															$sql="select * from currencies";
															$query=$db ->query($sql);
															while($row=$query ->fetch_array()){
														    $id=$row['id'];
														    $name=$row['name'];
															echo "<option value='$id'>$name</option>";	
															}
															echo "</select>";
														
														?>
														<span class="help-block">
														</span>
													</div>
												</div>
												
												<div id='cCounter'></div>
												
												
												
												
												<div class="form-group">
													<label class="control-label col-md-2">مقدار<span class="required">
													</span>
													</label>
													<div class="col-md-4">
														<input type="text" name="counterAmount" autocomplete="off" onkeyup="addSeparator(this)" id="counterAmount"    placeholder="" class="form-control"  onChange="calculateDollar();nazari();" onclick="calculateDollar();nazari();"/>
														<input type="hidden" name='customerId' id="customerId" value="<?php echo $customar_id; ?>"
														<span class="help-block">
														</span>
													</div>
												</div>
												<div class="form-group">
													<label class="control-label col-md-2">ارز پایه<span class="required">
													</span>
													</label>
													<div class="col-md-4">
													<select name="basecurrency"  id='basecurrency' style="width:100%" onchange="findingcalculation();customerBalance();findCash();get_amount();calculate();assetCash()" onclick="findingcalculation();customerBalance();assetCash();findCash();get_amount();calculate()">
													<option></option>
														<?php
															 
															$sql="select * from currencies";
															$query=$db ->query($sql);
															while($row=$query ->fetch_array()){
														    $id=$row['id'];
														    $name=$row['name'];
															echo "<option value='$id'>$name</option>";	
															}
															echo "</select>";
													?>
														<span class="help-block">
														</span>
													</div>
												</div>
												
												<div id='cash'></div>
												
												
												<div class="form-group">
													<label class="control-label col-md-2">موجودی ارز پایه مشتری<span class="required">
													</span>
													</label>
													<div class="col-md-4">
														<div id='asset'><input type="text" class='form-control'></div>
														<span class="help-block">
														</span>
													</div>
												</div>
												 
												<div class="form-group">
													<label class="control-label col-md-2">قیمت فروش<span class="required">
													</span>
													</label>
													<div class="col-md-4">
													    
														<td><div id='rate'><input type="text" name="rateOfCurrency" id='rateOfCurrency' class='form-control' ></div></td>
														
														<span class="help-block">
														</span>
													</div>
												</div>
												<div class="form-group">
													<label class="control-label col-md-2">قیمت فروش جدید<span class="required">
													</span>
													</label>
													<div class="col-md-4">
													    
														<td> <input type="text" name="change_currencyPriec" autocomplete="off" id="change_currencyPriec" placeholder="در صورت تغییر قیمت فروش قیمت جدید را بنویسید"  style='background: #ccffff;'  class='form-control'    >
												
														</td>
														
														<span class="help-block">
														</span>
													</div>
													 
												</div>
												<div class="form-group">
													<label class="control-label col-md-2">قیمت خرید<span class="required">
													</span>
													</label>
													<div class="col-md-4">
													    
														<td><input type="text" name="buyRate" autocomplete="off" id='buyRate' placeholder="قیمت خرید را وارد کنید"  style='background: #ccffff;' class='form-control'   onclick="buynewrate()" ></td>
														
														<span class="help-block">
														</span>
													</div>
												</div>
												
												<div class="form-group">
													<label class="control-label col-md-2">ارزش به ارز پایه<span class="required">
													</span>
													</label>
													<div class="col-md-4">
														<input type='text' name='baceAmount' id="baceAmount" class='form-control'   >
														<span class="help-block">
														</span>
													</div>
												</div>
												<div class="form-group">
													<label class="control-label col-md-2">الباقی حساب مشتری<span class="required">
													</span>
													</label>
													<div class="col-md-4">
														<input type='text' name='remainCustomer' id="remainCustomer" class='form-control'   >
														<span class="help-block">
														</span>
													</div>
												</div>
												<div class="form-group">
													<!--<label class="control-label col-md-2">قیمت به دالر<span class="required">
													</span>
													</label>-->
													<div class="col-md-4">
													    
														<td><div id='ghaymatdollar'><input type="hidden"   class='form-control' ></div></td>
														<span class="help-block">
														</span>
													</div>
												</div>
												<!--<div class="form-group">
													<label class="control-label col-md-2">قیمت جدید فروش به دالر<span class="required">
													</span>
													</label>
													<div class="col-md-4">
													    
														<td> <input type="text" name='new_dollarRatePrice' autocomplete="off" id='new_dollarRatePrice' placeholder="در صورت تغییر قیمت فروش قیمت جدید را بنویسید"  style='background: #ccffff;' class='form-control'   onclick="dollarsalenewrate()" > </td>
														<span class="help-block">
														</span>
													</div>
												</div>
												<div class="form-group">
													<label class="control-label col-md-2">قیمت خرید به دالر<span class="required">
													</span>
													</label>
													<div class="col-md-4">
													    
														<td><input type="text" name="buyRate_dollar" autocomplete="off" id='buyRate_dollar' placeholder="قیمت خریداری دالر را وارد کنید"  style='background: #ccffff;' class='form-control'   onclick="dollarBuy_newrate()" ></td>
														
														<span class="help-block">
														</span>
													</div>
												</div>-->
												<div class="form-group">
													<!--<label class="control-label col-md-2">ارزش به دالر<span class="required">
													</span>
													</label>-->
													<div class="col-md-4">
														<input type='hidden' name='totalDollar' id="totalDollar" class='form-control'   >
														<span class="help-block">
														</span>
													</div>
												</div>
												<div class="form-group">
													<label class="control-label col-md-2">توضیحات <span class="required">
													</span>
													</label>
													<div class="col-md-4">
														<textarea class="form-control" rows="6" name="desc" ></textarea>
														<span class="help-block">
														</span>
													</div>
												</div>	
											</div>
										</div>
									</div>
									<div class="button_wrapper">
										<div class="row">
											<div class="col-md-offset-3 col-md-4">
												<input type="submit" id="submitbtn" value="&nbsp;&nbsp;ذخیره&nbsp;&nbsp;" class="btn green button-submit" name="submit"/>
											</div>
										</div>
									</div>	
								</div>
							</form>
							<!-- BEGIN PAGE CONTENT-->
							<div class="row">
								<div class="col-md-12">
									<!-- BEGIN EXAMPLE TABLE PORTLET-->
									<div class="table-wrapper">
										<hr class="horizontal-line" />
										<div class="portlet-body">
											 
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			<!-- END PAGE CONTENT-->
		</div>
	</div>
<!-- BEGIN FOOTER -->
<?php require_once('footer.php');?>
<!-- END FOOTER -->
</div>
<!-- BEGIN JAVASCRIPTS(Load javascripts at bottom, this will reduce page load time) -->
<!-- BEGIN CORE PLUGINS -->
<!--[if lt IE 9]>
<script src="../../assets/global/plugins/respond.min.js"></script>
<script src="../../assets/global/plugins/excanvas.min.js"></script> 
<![endif]-->
<script src="../../assets/global/plugins/jquery.min.js" type="text/javascript"></script>
<script src="../../assets/global/plugins/jquery-migrate.min.js" type="text/javascript"></script>
<!-- IMPORTANT! Load jquery-ui.min.js before bootstrap.min.js to fix bootstrap tooltip conflict with jquery ui tooltip -->
<script src="../../assets/global/plugins/jquery-ui/jquery-ui.min.js" type="text/javascript"></script>
<script src="../../assets/global/plugins/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="../../assets/global/plugins/bootstrap-hover-dropdown/bootstrap-hover-dropdown.min.js" type="text/javascript"></script>
<script src="../../assets/global/plugins/jquery-slimscroll/jquery.slimscroll.min.js" type="text/javascript"></script>
<script src="../../assets/global/plugins/jquery.blockui.min.js" type="text/javascript"></script>
<script src="../../assets/global/plugins/jquery.cokie.min.js" type="text/javascript"></script>
<script src="../../assets/global/plugins/uniform/jquery.uniform.min.js" type="text/javascript"></script>
<script src="../../assets/global/plugins/bootstrap-switch/js/bootstrap-switch.min.js" type="text/javascript"></script>
<!-- END CORE PLUGINS -->
 
<!-- BEGIN PAGE LEVEL PLUGINS -->
<script type="text/javascript" src="../../assets/global/plugins/jquery-validation/js/jquery.validate.min.js"></script>
<script type="text/javascript" src="../../assets/global/plugins/jquery-validation/js/additional-methods.min.js"></script>
<script type="text/javascript" src="../../assets/global/plugins/bootstrap-wizard/jquery.bootstrap.wizard.min.js"></script>
<!-- END PAGE LEVEL PLUGINS -->
<!-- BEGIN PAGE LEVEL PLUGINS -->
<!-- END PAGE LEVEL PLUGINS -->

<!-- BEGIN PAGE LEVEL SCRIPTS -->
<script src="../../assets/global/scripts/metronic.js" type="text/javascript"></script>
<script src="../../assets/admin/layout/scripts/layout.js" type="text/javascript"></script>
<script src="../../assets/admin/layout/scripts/demo.js" type="text/javascript"></script>
<script src="../../assets/admin/pages/scripts/table-advanced.js"></script>
<script src="../../assets/admin/pages/scripts/form-wizard.js"></script>
<!-- END PAGE LEVEL SCRIPTS -->
<script>
jQuery(document).ready(function() { 
   // initiate layout and plugins
   Metronic.init(); // init metronic core components
   Layout.init(); // init current layout
   Demo.init(); // init demo features
   TableAdvanced.init();
   FormWizard.init(); 
});
</script>

<!-- END JAVASCRIPTS -->
</body>
<!-- END BODY -->
</html>
<?php
}else{
	header("Location: login.php");
}
?>