<?php
require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege(20, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

$contract_id = $_REQUEST['id'];

$gp = $db->prepare('SELECT * from naql where carid = $ LIMIT 1');
	$gp->bindParam(1, $carid);
	$gp->execute();
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="robots" content="index,follow">
	<link rel="icon" href="../images/perfect.ico">
	<title>عقد<?php echo $contract_id; ?></title>
	<link rel="stylesheet" href="../dist/css/bootstrap.min.css" />
	<link rel="stylesheet" href="../css/font-awesome.min.css">
	<link rel="stylesheet" href="../hj2/humanity.calendars.picker.css">
	<link rel="stylesheet" href="../css/myCar.css" />
	<script src="../js/jquery.js"></script>
	<script src="../dist/js/umd/popper.min.js"></script>
	<script src="../dist/js/bootstrap.js"></script>
	<script src="../hj2/jquery.plugin.js"></script>
	<script src="../hj2/jquery.calendars.js"></script>
	<script src="../hj2/jquery.calendars.plus.js"></script>
	<script src="../hj2/jquery.calendars.picker.js"></script>
	<script src="../hj2/jquery.calendars.ummalqura.js"></script>
</head>

<body style="background-color:#002933;">
	<style>
		.control-label,
		.table,
		.h6,
		h6 {
			color: #A9A9A7;
		}

		a {
			font-size: 1.1em;
			font-weight: bold;
			color: #EDDFDF;
		}

		a:hover {
			color: green;
		}
	</style>
	<div class="container-fluid" style="background-color:black;">
		<br />
		<div class="form-row align-items-center">
			<div class="col-2">
				<input style="margin-right:20px;" type="submit" class="btn btn-danger" id="close2" value=" خروج " />
			</div>
			<div class="col-8 text-center">
				<h3 style="font-weight:bold; color:#EDDFDF;" id="title">متابعة عقد رقم <?php echo $contract_id; ?></h3>
			</div>
		</div>

		<?php
		$sql = $db->prepare('SELECT contracts.contract_id,contracts.cust_id2,cars.carid,customers.cust_id ,customers.cust_name,customers.phone , customers.phone2, cars.kind , cars.numbers , cars.model,contracts.cont_status FROM cars,contracts,customers
	WHERE contracts.carid = cars.carid AND contracts.cust_id = customers.cust_id AND contracts.contract_id = ? ');
		$sql->bindParam(1, $contract_id);
		$sql->execute();
		if ($sql->rowCount() > 0) {
			$row = $sql->fetch(PDO::FETCH_ASSOC);
			$cust_id = $row['cust_id'];
			$carid = $row['carid'];
			$phone = $row['phone'];
			$phone2 = $row['phone2'];
			$whats = substr($phone, 1);
			$whats = "9660" . $whats;
			$whats2 = "";
			if (!empty($phone2)) {
				$whats2 = substr($phone2, 1);
				$whats2 = "9660" . $whats2;
			}
			$gpsid = 0;
			$gp = $db->prepare("SELECT gpsid from naql where carid = $carid LIMIT 1");
			// $gp->bindParam(1, $carid);
			$gp->execute();
			if($gp->rowCount()> 0){
				$gps = $gp->fetch(PDO::FETCH_ASSOC);
				$gpsid = $gps['gpsid'];
			}
		?>
			<div class="form-row align-items-center" style="margin-top:10px;">
				<div class="col-1">
					<a href="cust_update.php?id=<?php echo $cust_id; ?>"><button type="button" class="btn btn-outline-success btn-sm">بيانات المستأجر</button></a>
				</div>
				<div class="col-10 text-center">
					<center class="control-label" style="font-size:1.3em; font-weight:bold; color:#EDDFDF;">
						<?php echo $row['cust_name'] . " ~<a href='https://api.whatsapp.com/send?phone=$whats' target='_blank'>" . $phone . "</a>~ <a href='https://api.whatsapp.com/send?phone=$whats2' target='_blank'>" . $phone2 . "</a>~ سيارة " . $row['kind'] . " " . $row['numbers'] . " موديل " . $row['model'];
						$cust22 = $row['cust_id2'];
						?>
					</center>
				</div>
				<div class="col-1 text-left">
					<a href="../a/car_update.php?id=<?php echo $row['carid']; ?>"><button type="button" class="btn btn-outline-success btn-sm ">بيانات السيارة</button></a>
				</div>
			</div>

		<?php if ($cust22 > 0) {
				$cust2 = $db->prepare("SELECT cust_id , cust_name , phone , phone2 from customers where cust_id = ?");
				$cust2->bindParam(1, $cust22);
				$cust2->execute();
				if ($cust2->rowCount() > 0) {
					$wow = $cust2->fetch(PDO::FETCH_ASSOC);
					$ph1 = $wow['phone'];
					$ph2  = $wow['phone2'];
					$wh1 = substr($ph1, 1);
					$wh1 = "9660" . $wh1;
					$wh2 = "";
					if (!empty($ph2)) {
						$wh2 = substr($ph2, 1);
						$wh2 = "9660" . $wh2;
					}
					echo '<br /><div class="form-row align-items-center">
						<div class="col-1 col-md-1">
						<a href="cust_update.php?id=' . $cust22 . '" target="_blank"><button type="button" class="btn btn-outline-success btn-sm ">مستأجر إضافي</button></a>
						</div >
						<div class="col-11">
						<center class="control-label" style="font-size:1.3em;font-weight:bold; color:#EDDFDF;">';
					echo $wow['cust_name'] . " ~<a href='https://api.whatsapp.com/send?phone=$wh1' target='_blank'>" . $ph1 . "</a>~<a href='https://api.whatsapp.com/send?phone=$wh2' target='_blank'>" . $ph2 . "</a>";
					echo "</center>
							</div>
							</div>
							</div>";
				}
			}
		}

		$debt = $db->prepare("SELECT sum(wanted) as debt from contracts where cust_id = ?  LIMIT 1");
		$debt->bindParam(1, $cust_id);
		$debt->execute();
		if ($debt->rowCount() > 0) {
			$monn = $debt->fetch(PDO::FETCH_ASSOC);
			$debt     = $monn["debt"];
		}
		?>
	</div>
	<p><br /></p>
	<div class="container-fluid">
		<div class="form-row">
			<div class="col-2">
				<div class="form-row">
					<div class="col-9">
						<label for="used_area" class="control-label">منطقة الإستخدام</label>
					</div>
				</div>
				<div class="row-form">
					<div class="col">
						<input type="text" class="form-control" id="used_area" name="used_area" disabled="disabled" readonly />
					</div>
				</div>
				<div class="form-row">
					<div class="col-11">
						<input type="text" class="form-control" id="rent_days" name="rent_days" disabled="disabled" readonly />
					</div>
				</div>
				<div class="form-row">
					<div class="col-5">
						<label for="wanted" class="control-label" style="margin-top:5px;">المطلوب</label>
					</div>
					<div class="col-6">
						<input type="number" class="form-control wanted" disabled="disabled" readonly />
					</div>
				</div>
				<br />
				<div class="form-row">
					<div class="col-11">
						<button type="button" class="btn btn-outline-primary btn-block" data-toggle="modal" style="font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#sanad_in" disabled="true">سند قبض</button>
					</div>
				</div>
				<div class="form-row">
					<div class="col-11">
						<button type="button" class="btn btn-outline-danger btn-block" id="sanad_out_btn" data-toggle="modal" style="font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#sanad_out" disabled="true">سند صرف</button>
					</div>
				</div>
				<div class="form-row">
					<div class="col-11">
						<button type="button" class="btn btn-outline-danger btn-block" id="sanad_cut_btn" data-toggle="modal" style="font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#sanad_cut">اشعار خصم</button>
					</div>
				</div>
				<div class="form-row">
					<div class="col-11">
						<button type="button" id="contract_update" name="contract_update" class="btn btn-outline-success btn-block">حفظ التعديلات <i style="margin-right:10px;font-size:16px;font-weight:bold;" class="fa fa-cloud" aria-hidden="true"></i></button>
					</div>
				</div>
				<div class="form-row">
					<div class="col-11">
						<button type="button" class="btn btn-outline-primary btn-block" id="add_cust22" data-toggle="modal" style="font-weight:bold;" data-backdrop="static" data-keyboard="false" data-target="#customer2">إضافة مستأجر <i style="margin-right:10px;font-size:18px;color:blue;" class="fa fa-user" aria-hidden="true"></i></button>
					</div>
				</div>
				<div class="form-row">
					<div class="col-11">
						<a id="print3" href="" target="_blank"><button type="button" class="btn btn-outline-success btn-block">طباعة العقد <i style="margin-right:10px;font-size:16px;" class="fa fa-print" aria-hidden="true"></i></button></a>
					</div>
				</div>
				<div class="row" style="padding:2px;">
					<div class="col-11">
						<a id="newIssue" href="" target="_blank"><button type="button" class="btn btn-outline-dark btn-block">رفع معاملة<span style="margin-right:20px;" class="glyphicon glyphicon-print" aria-hidden="true"></span></button></a>
					</div>
				</div>
				<div class="form-row">
					<div class="col-11">
						<a id="acc" href="" target="_blank"><button type="button" class="btn btn-outline-success btn-block">كشف حساب <i style="margin-right:10px;font-size:16px;" class="fa fa-print" aria-hidden="true"></i></button></a>
					</div>
				</div>
				<div class="form-row">
					<div class="col-11">
						<button type="button" class="btn btn-outline-danger btn-block" id="close">خروج <i style="margin-right:10px;font-size:16px;font-weight:bold;" class="fa fa-times" aria-hidden="true"></i></button>
					</div>
				</div>
			</div>
			<div class="col-10">
				<form id="koki" method="post" class="form-horizontal" style="float:center;font-weight: bold; font-size: 16px; color: #000;">
					<div id="koki_msg"></div>
					<input type="hidden" id="contract_id" name="contract_id" value="<?php echo $contract_id ?>" />
					<input type="hidden" id="carid" name="carid" />
					<input type="hidden" id="minn" name="minn" />
					<input type="hidden" id="km3" name="km3" />
					<input type="hidden" id="freekm" name="freekm" />
					<input type="hidden" id="free_dd" name="free_dd" />
					<input type="hidden" id="gpsid" name="gpsid" value="<?php echo $gpsid; ?>" />
					<div class="form-row">
						<div class="col-2">
						</div>
						<div class="col-2 text-center">
							<label for="date_in" class="control-label ">الخروج</label>
						</div>
						<div class="col-2 text-center">
							<label class="control-label">الدخول</label>
						</div>
						<div class="col-1  text-center">
							<label class="control-label">عدد</label>
						</div>
						<div class="col-1 text-center">
							<label class="control-label">سعر</label>
						</div>
						<div class="col-2 text-center">
							<label for="days_cost" class="control-label">إجمالي</label>
						</div>
						<div class="col-2 text-center">
							<label for="" class="control-label  text-danger"><?php echo $debt; ?></label>
						</div>
					</div>
					<div class="form-row">
						<div class="col-2">
							<label for="date_in" class="control-label" style="float:left;">التاريخ</label>
						</div>
						<div class="col-2">
							<input type="text" class="form-control" id="date_out" name="date_out" disabled="disabled" readonly />
						</div>
						<div class="col-2">
							<input type="text" class="form-control" id="date_in" name="date_in" readonly />
						</div>
						<script>
							$(function() {
								var calendar = $.calendars.instance('');
								$('#date_in').calendarsPicker({
									calendar: calendar
								});
							});
						</script>
						<div class="col-1">
							<input type="number" class="form-control" id="days_qty" name="days_qty" disabled="disabled" readonly />
						</div>
						<div class="col-1">
							<input type="number" onchange="money()" class="form-control" id="day_cost" name="day_cost" />
							<div class="text-danger" style="font-size:12px;" id="day_cost_msg"></div>
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="days_cost" name="days_cost" disabled="disabled" readonly />
						</div>
						<div class="col-2 ">
							<label for="free_kilo" class="control-label">الكيلو المسموح</label>
						</div>
					</div>
					<div class="form-row">
						<div class="col-2">
							<label for="km_in" class="control-label" style="float:left;">الكيلو</label>
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="km_out" name="km_out" disabled="disabled" readonly />
						</div>
						<div class="col-2">
							<input type="number" onchange="money()" class="form-control" id="km_in" name="km_in" />
							<div class="text-danger" style="font-size:12px;" id="km_in_msg"></div>
						</div>
						<div class="col-1">
							<input type="number" class="form-control" id="kilo_qty" name="kilo_qty" disabled="disabled" readonly />
						</div>
						<div class="col-1">
							<input type="number" onchange="money()" class="form-control" id="kilo_cost" name="kilo_cost" />
							<div class="text-danger" style="font-size:12px;" id="kilo_cost_msg"></div>
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="kilos_cost" name="kilos_cost" disabled="disabled" readonly />
						</div>
						<div class="col-2">
							<input type="number" onchange="money()" class="form-control" id="free_kilo" name="free_kilo" />
							<div class="text-danger" style="font-size:12px;" id="free_kilo_msg"></div>
						</div>
					</div>
					<div class="form-row">
						<div class="col-2">
							<label for="date_in" class="control-label" style="float:left;">الساعة</label>
						</div>
						<div class="col-2">
							<input type="text" class="form-control" id="time_out" name="time_out" disabled="disabled" readonly />
						</div>
						<div class="col-2">
							<input type="text" class="form-control" id="time_in" name="time_in" />
						</div>
						<div class="col-1">
							<input type="number" class="form-control" id="hour_qty" name="hour_qty" disabled="disabled" readonly />
						</div>
						<div class="col-1">
							<input type="number" onchange="money()" class="form-control" id="hour_cost" name="hour_cost" disabled="disabled" readonly />
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="hours_cost" name="hours_cost" disabled="disabled" readonly />
						</div>
						<div class="col-2">
							<input type="number" class="form-control" id="used_kilo" name="used_kilo" disabled="disabled" readonly />
						</div>
					</div>
					<br />
					<div class="form-row">
						<div class="col-7">
							<div class="form-row">
								<div class="col-3">
									<label for="addtion_desc" class="control-label" style="float:left;">تعويضات</label>
								</div>
								<div class="col-9">
									<textarea class="form-control" rows="3" id="addtion_desc" name="addtion_desc" placeholder="بيان الإضافي و التعويضات و المخالفات"></textarea>
									<div class="text-danger" style="font-size:12px;" id="addtion_desc_msg"></div>
								</div>
							</div>
							<div class="form-row">
								<div class="col-3">
									<label for="dept_comm" class="control-label" style="float:left;">الإدارة</label>
								</div>
								<div class="col-9">
									<textarea class="form-control" rows="2" id="dept_comm" name="dept_comm" placeholder="تعليمات الإدارة"></textarea>
								</div>
							</div>
							<div class="form-row">
								<div class="col-3">
									<label for="operating_comm" class="control-label" style="float:left;">التشغيل</label>
								</div>
								<div class="col-9">
									<textarea class="form-control" rows="2" id="operating_comm" name="operating_comm" disabled="disabled" readonly placeholder="تعليمات التشغيل"></textarea>
								</div>
							</div>
							<div class="form-row">
								<div class="col-3">
									<label for="open_comm" class="control-label" style="float:left;">ملاحظات</label>
								</div>
								<div class="col-9">
									<textarea class="form-control" rows="3" id="open_comm" name="open_comm" disabled="disabled" readonly placeholder="عند فتح العقد"></textarea>
									<div class="text-danger" style="font-size:12px;" id="open_comm_msg"></div>
								</div>
							</div>
						</div>
						<div class="col-5">
							<div class="form-row">
								<div class="col-2" style="margin-right:15px;">
									<label for="addtion_cost" class="control-label" style="float:left;">تعويضات</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" onchange="money()" id="addtion_cost" name="addtion_cost" />
									<div class="text-danger" style="font-size:12px;" id="addtion_cost_msg"></div>
								</div>
								<div class="col-1">
									<label for="gpsKM" class="control-label text-primary">فحص</label>
								</div>
								<div class="col-4">
									<input type="text" class="form-control" id="gpsKM" name="gpsKM" />
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:15px;">
									<label for="violations" class="control-label" style="float:left;">مخالفات</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" onchange="money()" id="violations" name="violations" />
									<div class="text-danger" style="font-size:12px;" id="violations_msg"></div>
								</div>
								<div class="col-1">
									<label for="over" class="control-label text-danger">فرق قراءات</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="over" name="over" disabled="disabled" readonly />
								</div>
							</div>
							<div class="form-row">
								<div class="col-xs-2 col-md-2" style="margin-right:15px;">
									<label for="ins_claim" class="control-label" style="float:left;">تأمينات</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" onchange="money()" id="ins_claim" name="ins_claim" />
									<div class="text-danger" style="font-size:12px;" id="ins_claim_msg"></div>
								</div>
								<div class="col-1"></div>
								<div class="col-4">
									<button id="gps"onclick="gpsCheck()" name="gps" class="btn btn-primary btn-block">فحص الكيلومتر</button>
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="deduction" class="control-label" style="float:left;">الخصم</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="deduction" name="deduction" />
								</div>
								<div class="col-2">
									<label for="cutt" class="control-label text-danger">اشعارات خصم</label>
								</div>
								<div class="col-3">
									<input type="number" class="form-control" id="cutt" name="cutt" disabled="disabled" readonly />
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="net_cost" class="control-label text-primary" style="float:left;">إجمالي إيجار</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="net_cost" name="net_cost" disabled="disabled" readonly />
								</div>
								<div class="col-2">
									<label for="vat_req" class="control-label">ضريبة 5%</label>
								</div>
								<div class="col-3">
									<input type="hidden" class="form-control" id="vat_req" name="vat_req" disabled="disabled" readonly />
									<input type="number" class="form-control" id="vat5" name="vat5" disabled="disabled" readonly />
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;padding:0px;padding-top:8px;">
									<label for="vat15" class="control-label text-danger" style="float:left;">ضريبة %15</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="vat15" name="vat15" disabled="disabled" readonly />
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="after_vat" class="control-label text-success" style="float:left;">إجمالي</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="after_vat" name="after_vat" disabled="disabled" readonly />
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="payed" class="control-label" style="float:left;">المدفوع</label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control" id="payed" name="payed" disabled="disabled" readonly />
								</div>
								<div class="col-2">
									<label for="courts_in" class="control-label text-primary">تحصيل محكمة</label>
								</div>
								<div class="col-3">
									<input type="number" class="form-control" id="courts_in" name="courts_in" disabled="disabled" readonly />
								</div>
							</div>
							<div class="form-row">
								<div class="col-2" style="margin-right:16px;">
									<label for="wanted" class="control-label" style="float:left;">المطلوب </label>
								</div>
								<div class="col-4">
									<input type="number" class="form-control wanted" id="wanted" name="wanted" disabled="disabled" readonly />
								</div>
							</div>
						</div>
					</div>
					<div class="form-row">
						<div class="col-1">
							<label for="comm" class="control-label" style="float:left;">ملاحظات</label>
						</div>
						<div class="col-10">
							<input type="text" class="form-control" id="comm" name="comm" name="wanted" disabled="disabled" readonly />
							<div class="text-danger" style="font-size:12px;" id="comm_msg"></div>
						</div>
					</div>
					<p><br /></p>
					<script>
						function money() {
							var a = parseFloat($("#day_cost").val()) || 0;
							var hq = parseInt($("#hour_qty").val()) || 0;
							var b = parseInt($("#days_qty").val()) || 0;
							$("#days_cost").val((a * b).toFixed(2));
							if (hq > 9) {
								$("#hours_cost").val(a);
							}
							if ((b == 0) && (hq > 6)) {
								$("#hours_cost").val(a);
							}
							var h = parseInt($("#hours_cost").val()) || 0;
							var d = parseFloat($("#addtion_cost").val()) || 0;
							d = parseFloat((d).toFixed(2));
							var v = parseInt($("#violations").val()) || 0;
							var i = parseInt($("#ins_claim").val()) || 0;
							mykilo(h, a);
							var k = parseInt($("#kilos_cost").val()) || 0;
							var ww = parseFloat($("#wanted").val()) || 0;
							if (ww > 200) {
								$(".wanted").css("background-color", "#ffcccc");
							} else if (ww < 0) {
								$(".wanted").css("background-color", "#ccffcc");
							} else {
								$(".wanted").css("background-color", "");
							}
						};

						function mykilo(hh, aa) {
							var free = 0;
							var a = parseInt($("#km_out").val());
							var b = parseInt($("#km_in").val()) || 0;
							if ((b - a) > 0) {
								$("#used_kilo").val(b - a);
							} else {
								$("#used_kilo").val(0);
							}
							var s = mykm(a);
							var d = parseInt($("#days_qty").val()) || 0;
							var f = myfree();
							if (hh == aa) {
								free = (1 + d) * f;
							} else {
								free = f * d;
							}
							if (free > (b - a)) {
								$("#kilos_cost").val(0);
								$("#kilo_qty").val(0);
							} else {
								var koky = ((b - (a + free)) * s) / 100;
								koky = Math.trunc(koky);
								$("#kilos_cost").val(koky);
								$("#kilo_qty").val(b - (a + free));
							}
						};

						function mykm(out) {
							var a = parseInt($("#kilo_cost").val()) || 0;
							var inn = parseInt($("#km_in").val()) || 0;
							var b = parseInt($("#km3").val()) || 0;
							if (inn != "") {
								if (out >= inn) {
									$("#km_in").val(out).css("background-color", "#ffcccc");
								} else {
									$("#km_in").val(inn).css("background-color", "");
								}
							}
							if (a < b) {
								$("#kilo_cost").val(a).css("background-color", "#ffcccc");
								return a;
							} else if (a == b) {
								$("#kilo_cost").css("background-color", "#ffcccc");
								return a;
							} else {
								$("#kilo_cost").css("background-color", "");
								return a;
							}
						}

						function myfree() {
							var a = parseInt($("#free_kilo").val()) || 250;
							var b = parseInt($("#freekm").val()) || 250;
							if (a > b) {
								$("#free_kilo").val(a).css("background-color", "#ffcccc");
								return a;
							} else if (a == b) {
								$("#free_kilo").css("background-color", "#ffcccc");
								return a;
							} else {
								$("#free_kilo").css("background-color", "");
								return a;
							}
						}
					</script>
				</form>
				<div id="msg"></div>
			</div>
		</div><!-- end of main row 2 and 10  -->
		<div class="form-row">
			<div class="col-6">
				<h6>سندات القبض </h6>
				<table class="table table-bordered table-hover">
					<thead>
						<tr>
							<th class="text-center" width="5%">م</th>
							<th class="text-center" width="17%">المبلغ</th>
							<th class="text-center" width="15%">التاريخ</th>
							<th class="text-center" width="9%">كود</th>
							<th class="text-center" width="22%">المستلم</th>
							<th class="text-center" width="14%">الفرع</th>
							<th class="text-center" width="18%">طريقة الدفع</th>
						</tr>
					</thead>
					<tbody id="tstandby">
						<?php
						$output = '';
						$sql = $db->prepare("SELECT in_id , money_in , sanad_date , realName , payMethod ,b_name  FROM sanad_in
						WHERE contract_id = ? order by sanad_date DESC ");
						$sql->bindParam(1, $contract_id);
						$sql->execute();
						if ($sql->rowCount() > 0) {
							$ss = 1;
							while ($row = $sql->fetch()) {
								$in_id = $row['in_id'];
								$bb = $row['payMethod'];
								switch ($bb) {
									case "1":
										$pay =  "كاش";
										$options = '<option selected value="1">كاش</option><option value="2" >تحويل بنكي</option><option value="3" >نقاط بيع</option>';
										break;
									case "2":
										$pay = "تحويل بنكي";
										$options = '<option value="1">كاش</option><option selected value="2" >تحويل بنكي</option><option value="3" >نقاط بيع</option>';
										break;
									case "3":
										$pay = "نقاط بيع";
										$options = '<option value="1">كاش</option><option value="2" >تحويل بنكي</option><option selected value="3" >نقاط بيع</option>';
										break;
								}
								$output .= '<tr>
							 					<td style="padding:0px;margin:0px;" ><button id="' . $in_id . '" style="font-weight:bold ;font-size:14px; padding:2;" class="btn btn-danger btn-ms btn-block in_edit ">' . $ss . '</button></td>
							 					<td style="padding:0px;margin:0px;"><input class="form-control" id="mon' . $in_id . '" name="mon' . $in_id . '" value="' . $row['money_in'] . '"/></td>
							 					<td>' . $row['sanad_date'] . '</td>
							 					<td>' . $in_id . '</td>
							 					<td>' . $row['realName'] . '</td>
							 					<td>' . $row['b_name'] . '</td>
							 					<td style="padding:0px;margin:0px;" contenteditable="true"><select id="pay' . $in_id . '" class="form-control payMM" style="background-color:transparent;padding:3px;margine:0px;" >
							 						' . $options . '</select></td>
							 				</tr>  ';
								$ss = $ss + 1;
							}
						} else {
							$output .= '<tr><td colspan="7">لا يوجد سجلات ...</td></tr>';
						}
						echo $output;
						?>
					</tbody>
					<tfoot></tfoot>
				</table>
			</div>
			<div class="col-6">
				<div class="row">
					<div class="col-5">
						<button type="button" style="font-weight:bold;" id="vat_add" class="btn btn-outline-success btn-block">اصدار فاتورة ضريبية</button>
					</div>
					<div class="col-3">
						<input type="text" class="form-control" id="end" name="end" readonly />
					</div>
					<script>
						$(function() {
							var calendar = $.calendars.instance('');
							$('#end').calendarsPicker({
								calendar: calendar
							});
						});
					</script>
				</div>
				<br /><br />
				<div class="row">
					<div class="col">
						<h6>فاواتير ضريبية</h6>
						<table class="table table-bordered table-hover ">
							<thead>
								<tr>
									<th class="text-center" width="10%">فاتورة</th>
									<th class="text-center" width="12%">المبلغ</th>
									<th class="text-center" width="22%">بداية</th>
									<th class="text-center" width="26%">نهاية الفترة</th>
									<th class="text-center" width="10%">يومي</th>
									<th class="text-center" width="15%">عدد أيام</th>
								</tr>
							</thead>
							<tbody id="tstandby">
								<?php
								$output = '';
								$sql = $db->prepare("SELECT vat_id , after_vat , start , end, day_cost ,days_qty FROM vat WHERE contract_id = ? order by vat_id DESC  ");
								$sql->bindParam(1, $contract_id);
								$sql->execute();
								if ($sql->rowCount() > 0) {
									while ($row = $sql->fetch()) {
										$vat_id = $row['vat_id'];
										$output .= '<tr>
									<td style="padding:0px;margin:0px;"><button id="' . $vat_id . '" style="font-weight:bold ;font-size:14px; padding:2;" class="btn btn-danger btn-ms btn-block vat_print">' . $vat_id . '</button></td>
									<td >' . $row['after_vat'] . '</td>
									<td>' . $row['start'] . '</td>
									<td>' . $row['end'] . '</td>
									<td>' . $row['day_cost'] . '</td>
									<td>' . $row['days_qty'] . '</td>
									</tr>  ';
									}
								} else {
									$output .= '<tr><td colspan="6">لا يوجد سجلات ...</td></tr>';
								}
								echo $output;
								?>
							</tbody>
							<tfoot></tfoot>
						</table>
					</div>
				</div>
				<br />
				<div class="form-row">
					<div class="col">
						<h6>سندات الصرف</h6>
						<table class="table table-bordered table-hover ">
							<thead>
								<tr>
									<th class="text-center" width="6%">م</th>
									<th class="text-center" width="16%">المبلغ</th>
									<th class="text-center" width="15%">التاريخ</th>
									<th class="text-center" width="10%">كود</th>
									<th class="text-center" width="22%">موظف الفرع</th>
									<th class="text-center" width="15%">الفرع</th>
								</tr>
							</thead>
							<tbody id="tstandby">
								<?php
								$output = '';
								$sql = $db->prepare("SELECT out_id , money_out , sanad_out , realName , b_name FROM sanad_out WHERE contract_id = ? order by sanad_out DESC  ");
								$sql->bindParam(1, $contract_id);
								$sql->execute();
								if ($sql->rowCount() > 0) {
									$ss = 1;
									while ($row = $sql->fetch()) {
										$out_id = $row['out_id'];
										$output .= '<tr>
						 	<td style="padding:0px;margin:0px;"><button id="' . $out_id . '" style="font-weight:bold ;font-size:14px; padding:2;" class="btn btn-danger btn-ms btn-block out_edit ">' . $ss . '</button></td>
						 	<td style="padding:0px;margin:0px;"><input class="form-control" id="out' . $out_id . '" name="out' . $out_id . '" value="' . $row['money_out'] . '"/></td>
						 	<td>' . $row['sanad_out'] . '</td>
						 	<td>' . $out_id . '</td>
						 	<td>' . $row['realName'] . '</td>
						 	<td>' . $row['b_name'] . '</td>
						</tr>  ';
										$ss = $ss + 1;
									}
								} else {
									$output .= '<tr><td colspan="6">لا يوجد سجلات ...</td></tr>';
								}
								echo $output;
								?>
							</tbody>
							<tfoot></tfoot>
						</table>
					</div>
				</div>
			</div>
		</div> <!-- end of invoice table row -->
		<!-- invoice modal -->
		<div class="modal fade" id="sanad_in" tabindex="-1" role="dialog" aria-labelledby="exLabel">
			<div class="modal-dialog modal-lg" role="document">
				<div class="modal-content">
					<div class="modal-header" style="background-color:teal ;">
						<div class="col-1 text-right">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span style="color:red;font-size:18px;"><i class="fa fa-times" aria-hidden="true"></i></span></button>
						</div>
						<div class="col-11 text-right">
							<h6 class="modal-title" style="font-weight:bold;color:#FFFAF0; " id="exLabel">سند قبض</h6>
						</div>
					</div>
					<div class="modal-body">
						<!-- start of sanad_in form -->
						<form method="post" class="form-horizontal" id="modal_sanad_in">
							<div id="sanad_in_msg"></div>
							<div class="form-row">
								<input type="hidden" class="form-control" id="id2" name="id2" />
								<input type="hidden" id="in_id" name="in_id" />
								<div class="col-2">
									<label for="money_in" class="control-label" style="float:left;color:black;">المبلغ <span class="text-danger">*</span></label>
								</div>
								<div class="col-2">
									<input type="number" class="form-control" id="money_in" name="money_in" placeholder="بالريال" />
									<div id="money_in_msg" class="text-danger" style="font-size:12px;"></div>
								</div>
								<div class="col-2">
									<label for="printed_no" class="control-label" style="float:left;color:black;">رقم السند</label>
								</div>
								<div class="col-2">
									<input type="text" class="form-control" id="printed_no" name="printed_no" placeholder="المطبوع" />
									<div id="printed_no_msg" class="text-danger" style="font-size:12px;"></div>
								</div>
								<div class="col-2">
									<label for="payMethod" class="control-label " style="float:left;color:black;">طريقة الدفع</label>
								</div>
								<div class="col-2">
									<select class="form-control" id="payMethod" name="payMethod" style="padding:3px;">
										<option selected value="1">كاش</option>
										<option value="2">تحويل بنكي</option>
										<option value="3">نقاط بيع</option>
									</select>
								</div>
							</div>
							<div class="form-row">
								<div class="col-1">
									<label for="comm2" class="control-label" style="color:black;">ملاحظات</label>
								</div>
								<div class="col-7">
									<input type="text" class="form-control" id="comm2" name="comm2" />
									<div id="comm2_msg" class="text-danger" style="font-size:12px;"></div>
								</div>
								<div class="col-2">
									<label for="current_km" class="control-label" style="float:left;color:black;">الكيلومتر</label>
								</div>
								<div class="col-2">
									<input type="number" class="form-control" id="current_km" name="current_km" placeholder=" الحالي" />
									<div id="current_km_msg" class="text-danger" style="font-size:12px;"></div>
								</div>
							</div>
						</form><!-- end of sanad_in form -->
					</div>
					<div class="modal-footer">
						<div class="col-1 text-right">
							<button type="button" class="btn btn-default" data-dismiss="modal">خروج</button>
						</div>
						<div class="col-11 text-right">
							<button type="button" id="sanad_in_add" name="sanad_in_add" class="btn btn-primary">حفظ <i class="fa fa-cloud" aria-hidden="true"></i></button>
							<a id="sanad_in_print" href="" target="_blank" style="display: none;"><button type="button" class="btn btn-success">طباعة السند <i class="fa fa-print" aria-hidden="true"></i></button></a>
							<div>
							</div>
						</div>
					</div>
				</div>
			</div><!-- end of sanad_in modal -->
		</div> <!-- modal invoice end -->
		<!-- start sanad out -->
		<div class="modal fade" id="sanad_out" tabindex="-1" role="dialog" aria-labelledby="exLabel">
			<div class="modal-dialog modal-lg" role="document">
				<div class="modal-content">
					<div class="modal-header" style="background-color:Maroon ;">
						<div class="col-1 text-right">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span style="color:blue;font-size:18px;"><i class="fa fa-times" aria-hidden="true"></i></span></button>
						</div>
						<div class="col-11 text-right">
							<h5 class="modal-title" style="font-weight:bold;color:#F0FFFF; " id="exLabel">سند صرف</h5>
						</div>
					</div>
					<div class="modal-body">
						<!-- start of sanad_out form -->
						<form method="post" class="form-horizontal" id="modal_sanad_out">
							<div id="sanad_out_msg"></div>
							<div class="form-row">
								<input type="hidden" class="form-control" id="id3" name="id3" />
								<input type="hidden" id="out_id" name="out_id" />
								<div class="col-2">
									<label for="money_out" class="control-label" style="float:left;color:black;">المبلغ <span class="text-danger">*</span></label>
								</div>
								<div class="col-2">
									<input type="number" class="form-control" id="money_out" name="money_out" placeholder="بالريال" />
									<div class="text-danger" style="font-size:12px;" id="money_out_msg"></div>
								</div>
								<div class="col-2">
									<label for="printed_no" class="control-label" style="float:left;color:black;">رقم السند</label>
								</div>
								<div class="col-2">
									<input type="text" class="form-control" id="printed_no" name="printed_no" placeholder="المطبوع" />
									<div class="text-danger" style="font-size:12px;" id="printed2_no_msg"></div>
								</div>
							</div>
							<div class="form-row">
								<div class="col-2">
									<label for="comm3" class="control-label">ملاحظات</label>
								</div>
								<div class="col-10">
									<input type="text" class="form-control" id="comm3" name="comm3" />
									<div class="text-danger" style="font-size:12px;" id="comm3_msg"></div>
								</div>
							</div>
						</form><!-- end of sanad_out form -->
					</div>
					<div class="modal-footer">
						<div class="col-1 text-right">
							<button type="button" class="btn btn-default" data-dismiss="modal">خروج</button>
						</div>
						<div class="col-11 text-right">
							<button type="button" id="sanad_out_add" name="sanad_out_add" class="btn btn-primary">حفظ <i class="fa fa-cloud" aria-hidden="true"></i></button>
							<a id="sanad_out_print" href="" target="_blank" style="display: none;"><button type="button" class="btn btn-success">طباعة السند <i class="fa fa-print" aria-hidden="true"></i></button></a>
						</div>
					</div>
				</div>
			</div>
		</div><!-- end of sanad_out modal -->
		<div class="modal fade" id="sanad_cut" tabindex="-1" role="dialog" aria-labelledby="exLabel">
			<div class="modal-dialog modal-lg" role="document">
				<div class="modal-content">
					<div class="modal-header" style="background-color:Maroon ;">
						<div class="col-1 text-right">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span style="color:blue;font-size:18px;"><i class="fa fa-times" aria-hidden="true"></i></span></button>
						</div>
						<div class="col-11 text-right">
							<h5 class="modal-title" style="font-weight:bold;color:#F0FFFF; " id="exLabel">أشعار خصم</h5>
						</div>
					</div>
					<div class="modal-body">
						<!-- start of sanad_cut form -->
						<form method="post" class="form-horizontal" id="modal_sanad_cut">
							<div id="sanad_cut_msg"></div>
							<div class="form-row">
								<input type="hidden" class="form-control" id="id4" name="id4" />
								<input type="hidden" id="de_id" name="de_id" />
								<div class="col-2">
									<label for="money_de" class="control-label" style="float:left;color:black;">المبلغ <span class="text-danger">*</span></label>
								</div>
								<div class="col-2">
									<input type="number" class="form-control" id="money_de" name="money_de" placeholder="بالريال" />
									<div class="text-danger" style="font-size:12px;" id="money_de_msg"></div>
								</div>
								<div class="col-2">
									<label for="printed3_no" class="control-label" style="float:left;color:black;">رقم السند</label>
								</div>
								<div class="col-2">
									<input type="text" class="form-control" id="printed3_no" name="printed3_no" placeholder="المطبوع" />
									<div class="text-danger" style="font-size:12px;" id="printed3_no_msg"></div>
								</div>
							</div>
							<div class="form-row">
								<div class="col-2">
									<label for="description" class="control-label text-primary">سبب الخصم</label>
								</div>
								<div class="col-10">
									<input type="text" class="form-control" id="description" name="description" />
									<div class="text-danger" style="font-size:12px;" id="description_msg"></div>
								</div>
							</div>
						</form><!-- end of sanad_out form -->
					</div>
					<div class="modal-footer">
						<div class="col-1 text-right">
							<button type="button" class="btn btn-default" data-dismiss="modal">خروج</button>
						</div>
						<div class="col-11 text-right">
							<button type="button" id="sanad_cut_add" name="sanad_cut_add" class="btn btn-primary">حفظ <i class="fa fa-cloud" aria-hidden="true"></i></button>
							<a id="sanad_cut_print" href="" target="_blank" style="display:none;"><button type="button" class="btn btn-success">طباعة إشعار الخصم<i class="fa fa-print" aria-hidden="true"></i></button></a>
						</div>
					</div>
				</div>
			</div>
		</div><!-- end of sanad_cut modal -->
		<!-- start add customer2 -->
		<div class="modal fade" id="customer2" tabindex="-1" role="dialog" aria-labelledby="exLabel">
			<div class="modal-dialog modal-lg" role="document">
				<div class="modal-content">
					<div class="modal-header" style="background-color:blue ;">
						<div class="col-1 text-right">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close"><i style="color:red;font-size:18px;" class="fa fa-times" aria-hidden="true"></i></button>
						</div>
						<div class="col-11 text-right">
							<h5 class="modal-title" style="font-weight:bold;color:#F0FFFF; " id="exLabel">إضافة مستاجر</h5>
						</div>
					</div>
					<div class="modal-body">
						<!-- start of add customer2 form -->
						<div class="form-row" style="padding-bottom:20px;">
							<input type="hidden" class="form-control" id="cust_id3" name="cust_id3" />
							<div class="col-4">
								<label for="customer_sr" class="control-label" style="float:left;color:black;">رقم هوية المستاجر<span class="text-danger">*</span></label>
							</div>
							<div class="col-5">
								<input type="number" class="form-control" id="customer_sr" name="customer_sr" placeholder=" رقم الهوية " />
								<div class="text-danger" style="font-size:12px;" id="customer_sr_msg"></div>
							</div>
							<div class="col-3">
								<button type="button" id="cust_sr" class="btn btn-primary">بحث <i style="color:white;" class="fa fa-search" aria-hidden="true"></i></button>
							</div>
						</div>
						<div id="customer2_msg"></div>
						<form method="post" class="form-horizontal" id="modal_customer2" style="display:none;">
							<div class="form-row">
								<div class="col-8">
									<div class="form-row">
										<div class="col-3">
											<label for="cust_name" class="control-label" style="float:left;color:black;">الاسم</label>
										</div>
										<div class="col-6">
											<input type="text" class="form-control" id="cust_name" name="cust_name" placeholder="بدون اسم" />
										</div>
										<div class="col-3">
											<input type="text" class="form-control" id="nation" name="nation" placeholder="مجهول" />
										</div>
									</div>
									<div class="form-row">
										<div class="col-3">
											<label for="idSA" class="control-label" style="float:left;color:black;">رقم الهوية</label>
										</div>
										<div class="col-5">
											<input type="text" class="form-control" id="idSA" name="idSA" />
										</div>
										<div class="col-4">
											<input type="text" class="form-control" id="id_Expire" name="id_Expire" placeholder="بدون" />
										</div>
									</div>
									<div class="form-row">
										<div class="col-3">
											<label for="license" class="control-label" style="float:left;color:black;">الرخصة</label>
										</div>
										<div class="col-5">
											<input type="text" class="form-control" id="license" name="license" placeholder="مجهول" />
										</div>
										<div class="col-4">
											<input type="text" class="form-control" id="lic_expire" name="lic_expire" placeholder="مجهول" />
										</div>
									</div>
									<div class="form-row">
										<div class="col-3">
											<label for="phone" class="control-label" style="float:left;color:black;">جوال</label>
										</div>
										<div class="col-5">
											<input type="text" class="form-control" id="phone" name="phone" placeholder="مجهول" />
										</div>
										<div class="col-4">
											<input type="text" class="form-control" id="phone2" name="phone2" placeholder="مجهول" />
										</div>
									</div>
									<div class="form-row">
										<div class="col-3">
											<label for="comm4" class="control-label" style="float:left;color:black;">ملاحظات</label>
										</div>
										<div class="col-9">
											<input type="text" class="form-control" id="comm4" name="comm4" placeholder="بدون" />
										</div>
									</div>
								</div>
								<div class="col-4">
									<div class="form-row">
										<div class="col-8">
											<input type="text" class="form-control" id="md" name="md" placeholder="بدون مديونية" />
										</div>
										<div class="col-4">
											<input type="text" class="form-control" id="dgree" name="dgree" placeholder="بدون" />
										</div>
									</div>
									<div class="form-row">
										<img id="cust_photo" src="" class="rounded img-fluid img-thumbnail" style="float:left;max-height:50%; max-width:100%;" alt="صورة الرخصة غير متاحة" />
									</div>
								</div>
							</div>
						</form><!-- end of add customer2 form -->
					</div>
					<div class="modal-footer">
						<div class="col-1 text-left">
							<button type="button" class="btn btn-danger" data-dismiss="modal">خروج</button>
						</div>
						<div class="col-11 text-right">
							<button id="cust_add" style="float:right ;display:none;" name="cust_add" class="btn btn-primary"> إضافة مستأجر</button>
							<a id="contt_open_print" href="" target="_blank" style="display: none;"><button type="button" class="btn btn-success"> طباعة فتح العقد </button></a>
						</div>
					</div>
				</div>
			</div>
		</div><!-- end of add customer2 modal -->
	</div><!-- end of fluid2  -->



	<script>
		var contt = document.getElementById('contract_id').value;
		fetch('../rent/print/newCont_api.php?ct=' + contt)
			.then((data) => data.json())
			.then((data) => {
				document.getElementById('carid').value = data.carid;
				document.getElementById('rent_days').value = data.rent_days;
				document.getElementById('used_area').value = data.used_area;
				document.getElementById('free_dd').value = data.free_dd;
				document.getElementById('freekm').value = data.freekm;
				document.getElementById('minn').value = data.minn;
				document.getElementById('km3').value = data.km3;

				document.getElementById('date_out').value = data.date_out;
				document.getElementById('date_in').value = data.date_in;
				document.getElementById('end').value = data.date_in;
				document.getElementById('days_qty').value = data.days_qty;
				document.getElementById('day_cost').value = data.day_cost;
				document.getElementById('days_cost').value = data.days_cost;

				document.getElementById('km_out').value = data.km_out;
				document.getElementById('km_in').value = data.km_in;
				document.getElementById('free_kilo').value = data.free_kilo;
				document.getElementById('kilo_qty').value = data.km_qty;
				document.getElementById('kilo_cost').value = data.kilo_cost;
				document.getElementById('kilos_cost').value = data.kilos_cost;
				document.getElementById('used_kilo').value = data.used_km;

				document.getElementById('time_out').value = data.time_out;
				document.getElementById('time_in').value = data.time_in;
				document.getElementById('hour_qty').value = data.hour_qty;
				document.getElementById('hour_cost').value = data.hour_cost;
				document.getElementById('hours_cost').value = data.hours_cost;

				document.getElementById('addtion_cost').value = data.addtion_cost;
				document.getElementById('addtion_desc').value = data.addtion_desc;
				document.getElementById('violations').value = data.violations;
				document.getElementById('ins_claim').value = data.ins_claim;
				document.getElementById('deduction').value = data.deduction;
				document.getElementById('cutt').value = data.cutt;
				document.getElementById('courts_in').value = data.courts_in;

				document.getElementById('payed').value = data.payed;
				document.getElementById('net_cost').value = data.net_cost;
				document.getElementById('vat5').value = data.vat5;
				document.getElementById('vat15').value = data.vat15;
				document.getElementById('vat_req').value = data.vat_req;
				document.getElementById('after_vat').value = data.after_vat;
				document.getElementById('wanted').value = data.wanted;

				document.getElementById('dept_comm').value = data.dept_comm;
				document.getElementById('operating_comm').value = data.operating_comm;
				document.getElementById('open_comm').value = data.open_comm;
				document.getElementById("print3").setAttribute("href", "../rent/print/closeContract.php?contt=" + data.contract_id);
				document.getElementById("acc").setAttribute("href", "account_statement.php?dd=" + data.contract_id);

			}).catch(err => console.log(err));
	</script>
	<script>
		// document.getElementById("gps").click();

		function gpsCheck() {
			event.preventDefault();
				// targetUrl = "https://track.gps14.com/api/get_devices?lang=en&user_api_hash=$2y$10$pKVV2ghYQNdtdjlTtdf9r.fMrV/R2AUFoe6aH5vn.J52b.LIhpelm" ;
				// targetUrl = "https://track.gps14.com/api/get_history?lang=en&user_api_hash=$2y$10$pKVV2ghYQNdtdjlTtdf9r.fMrV/R2AUFoe6aH5vn.J52b.LIhpelm&device_id=176&from_date=2020-09-03&from_time=3:09:40&to_date=2020-10-03&to_time=23:00:00";
				let device_id = document.getElementById('gpsid').value;
				let from_date = document.getElementById('date_out').value;
				let to_date = document.getElementById('date_in').value;
				let from_time = document.getElementById('time_out').value;
				let to_time = document.getElementById('time_in').value;
				var proxyUrl = 'https://cors-anywhere.herokuapp.com/',
				targetUrl = "https://track.gps14.com/api/get_history?lang=en&user_api_hash=$2y$10$pKVV2ghYQNdtdjlTtdf9r.fMrV/R2AUFoe6aH5vn.J52b.LIhpelm&device_id="+device_id+"&from_date="+from_date+"&from_time="+from_time+"&to_date="+to_date+"&to_time="+to_time;


			fetch(proxyUrl + targetUrl)
				.then(respond => {
					return respond.json()
				}).then(json => {
					console.log(json.distance_sum);
					document.getElementById('gpsKM').value = json.distance_sum;
					// document.getElementById('over').value = json.distance_sum - ;
				})
		}
	</script>


	<script>
		$(document).ready(function() {
			$('#close').click(function() {
				close();
			});
			$('#close2').click(function() {
				close();
			});

			$("#sanad_in_add").click(function(event) {
				event.preventDefault();
				$("#money_in_msg").html("");
				$("#printed_no_msg").html("");
				$("#comm2_msg").html("");
				$("#current_km_msg").html("");
				$.ajax({
					url: "../rent/sanad_in_action.php",
					method: "POST",
					data: $("#modal_sanad_in").serialize(),
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#money_in_msg").html(data.error.money_in);
							$("#printed_no_msg").html(data.error.printed_no);
							$("#comm2_msg").html(data.error.comm);
							$("#current_km_msg").html(data.error.current_km);
						} else {
							$('#sanad_in_add').css("display", "none");
							$('#sanad_in_print').css("display", "block");
							$("#sanad_in_msg").html(data.msg);
							$("#in_id").val(data.in_id);
							$("#sanad_in_print").attr("href", "sanad_in1.php?sanad=" + data.in_id);
							$("#total_in").val(data.total_in);
							$("#total_out").val(data.total_out);
							$("#payed").val(data.payed);
							money();
							$("#sanad_in_msg").delay(3000).slideUp(function() {
								$("#sanad_in_msg").html('');
							});
						}
					}
				});
			});
			$("#sanad_out_add").click(function(event) {
				event.preventDefault();
				$("#money_out_msg").html("");
				$("#printed2_no_msg").html("");
				$("#comm3_msg").html("");
				$.ajax({
					url: "../rent/sanad_out_action.php",
					method: "POST",
					data: $("#modal_sanad_out").serialize(),
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#money_out_msg").html(data.error.money_out);
							$("#printed2_no_msg").html(data.error.printed_no);
							$("#comm3_msg").html(data.error.comm);
						} else {
							$('#sanad_out_add').css("display", "none");
							$('#sanad_out_print').css("display", "block");
							$("#sanad_out_msg").html(data.msg);
							$("#out_id").val(data.out_id);
							$("#sanad_out_print").attr("href", "sanad_out.php?sanad=" + data.out_id);
							$("#total_out").val(data.total_out);
							$("#total_in").val(data.total_in);
							$("#payed").val(data.payed);
							money();
							$("#sanad_out_msg").delay(3000).slideUp(function() {
								$("#sanad_out_msg").html('');
							});
						}
					}
				});
			});
			$("#sanad_cut_add").click(function(event) {
				event.preventDefault();
				$("#money_de_msg").html("");
				$("#printed3_no_msg").html("");
				$("#description_msg").html("");
				$.ajax({
					url: "sanad_cut_action.php",
					method: "POST",
					data: $("#modal_sanad_cut").serialize(),
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#money_de_msg").html(data.error.money_de);
							$("#printed3_no_msg").html(data.error.printed_no);
							$("#description_msg").html(data.error.description);
						} else {
							$('#sanad_cut_add').css("display", "none");
							$('#sanad_cut_print').css("display", "block");
							$("#sanad_cut_msg").html(data.msg);
							$("#de_id").val(data.de_id);
							$("#sanad_cut_print").attr("href", "sanad_cut.php?sanad=" + data.de_id);
							$("#sanad_cut_msg").delay(3000).slideUp(function() {
								$("#sanad_cut_msg").html('');
							});
						}
					}
				});
			});
			$('#sanad_in').on('show.bs.modal', function(event) {
				var recipient = $("#contract_id").val();
				var modal = $(this);
				modal.find('#id2').val(recipient);
			});

			$('#sanad_out').on('show.bs.modal', function(event) {
				var recipient = $("#contract_id").val();
				var ww = $(".wanted").val();
				ww = ww * -1;
				var modal = $(this);
				modal.find('#id3').val(recipient);
				modal.find('#money_out').val(ww);
			});
			$('#sanad_cut').on('show.bs.modal', function(event) {
				var recipient = $("#contract_id").val();
				var ww = $(".wanted").val();
				ww = (ww / 105) * 100;
				ww = parseFloat((ww).toFixed(2));
				var modal = $(this);
				modal.find('#id4').val(recipient);
				modal.find('#money_de').val(ww);
			});
			$("#contract_update").click(function(event) {
				event.preventDefault();
				$("#day_cost_msg").html("");
				$("#kilo_cost_msg").html("");
				$("#free_kilo_msg").html("");
				$("#km_in_msg").html("");
				$("#addtion_cost_msg").html("");
				$("#addtion_desc_msg").html("");
				$("#violations_msg").html("");
				$("#ins_claim_msg").html("");
				$.ajax({
					url: "contract_update.php",
					method: "POST",
					data: $("#koki").serialize(),
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#day_cost_msg").html(data.error.day_cost);
							$("#kilo_cost_msg").html(data.error.kilo_cost);
							$("#free_kilo_msg").html(data.error.free_kilo);
							$("#km_in_msg").html(data.error.km_in);
							$("#addtion_cost_msg").html(data.error.addtion_cost);
							$("#addtion_desc_msg").html(data.error.addtion_desc);
							$("#violations_msg").html(data.error.violations);
							$("#ins_claim_msg").html(data.error.ins_claim);
						} else {
							$("#koki_msg").html(data.msg);
							$("#koki_msg").delay(3000).slideUp(function() {
								$("#koki_msg").html('');
							});
							location.reload();
						}
					}
				});
			});
			$("#cust_sr").click(function(event) {
				event.preventDefault();
				var idSA = $("#customer_sr").val();
				$("#customer_sr_msg").html("");
				$.ajax({
					url: "../actions/cust_single.php",
					method: "POST",
					data: {
						idSA: idSA
					},
					dataType: "json",
					success: function(data) {
						if (data.error) {
							$("#customer_sr_msg").html(data.error.msg);
						} else {
							$('#modal_customer2').css('display', 'block');
							$('#cust_add').css('display', 'block');
							$("#cust_id3").val(data.cust_id);
							$("#dgree").val(data.dgree);
							$("#cust_name").val(data.cust_name);
							$("#nation").val(data.nation);
							$("#idSA").val(data.idSA);
							$("#id_Expire").val(data.id_Expire);
							$("#license").val(data.license);
							$("#lic_expire").val(data.lic_expire);
							$("#phone").val(data.phone);
							$("#phone2").val(data.phone2);
							$("#comm4").val(data.comm);
							$('#cust_photo').attr("src", "../photo/customers/" + (data.cust_photo));
							$("#customer2_msg").html(data.notfound);
							$("#customer2_msg").delay(6000).slideUp(function() {
								$("#customer2_msg").html('');
							});
						}
					}
				});
			}); //end of customer2 search

			$("#cust_add").click(function(event) {
				event.preventDefault();
				var cust_id2 = $("#cust_id3").val();
				var contract_id = $("#contract_id").val();
				$("#customer_sr_msg").html("");
				$.ajax({
					url: "contract_update.php",
					method: "POST",
					data: {
						cust_id2: cust_id2,
						contract_id: contract_id
					},
					dataType: "json",
					success: function(data) {
						$("#customer2_msg").html(data.msg);
						$('#cust_add').css('display', 'none');
						$('#contt_open_print').css('display', 'block');
						$('#contt_open_print').attr('href', '../rent/print2.php?contt=' + data.contract_id);
						$("#customer2_msg").delay(5000).slideUp(function() {
							$("#customer2_msg").html('');
						});
					}
				});
			}); //end of add customer2
			$(".in_edit").click(function(event) {
				event.preventDefault();
				if (confirm("هل تريد تحديث قيمة  و طريقة دفع هذا السند؟")) {
					var id = $(this).attr('id');
					var money_in = $("#mon" + id).val();
					var payMethod = $("#pay" + id).val();
					$.ajax({
						url: "sanad_in_update.php",
						method: "POST",
						data: {
							id: id,
							money_in: money_in,
							payMethod: payMethod
						},
						success: function(data) {
							location.reload();
						}
					});
				} else {
					return false;
				}
			}); //end of add customer2
			$(".out_edit").click(function(event) {
				event.preventDefault();
				if (confirm("هل تريد تعديل قيمة سند الصرف؟")) {
					var id = $(this).attr('id');
					var money_out = $("#out" + id).val();
					$.ajax({
						url: "sanad_out_update.php",
						method: "POST",
						data: {
							id: id,
							money_out: money_out
						},
						success: function(data) {
							location.reload();
						}
					});
				} else {
					return false;
				}
			}); //end of add customer2
			//create new vat invoice
			$("#vat_add").click(function(event) {
				event.preventDefault();
				var contract_id = $("#contract_id").val();
				var end = $("#end").val();
				$.ajax({
					url: "../qrt/vat_actions.php",
					method: "POST",
					data: {
						contract_id: contract_id,
						end: end
					},
					success: function(data) {
						$("#koki_msg").html(data);
						$("#koki_msg").delay(2000).slideUp(function() {
							location.reload();
						});
					}
				});
			}); //end of Add vat inovice
			$(".vat_print").click(function(event) {
				event.preventDefault();
				var dd = $(this).attr('id');
				window.open("fatora_vat.php?dd=" + dd, '_blank');
			}); //end of vat print button action
			//
			$("#newIssue").click(function(event) {
				event.preventDefault();
				var newIssue = $("#contract_id").val();
				$.ajax({
					url: "contract_update.php",
					method: "POST",
					data: {
						newIssue: newIssue
					},
					dataType: "json",
					success: function(data) {
						$("#koki_msg").html(data.msg);
						$("#koki_msg").delay(3000).slideUp(function() {
							$("#koki_msg").html('');
						});
					}
				});
			});

			function convertDates(name, ss, dest) {
				var date = $('#' + ss).val().split('/');
				var year = parseInt(date[0], 10);
				if (name == 'ummalqura') {
					if (year > 1800) {
						return false;
					}
				} else if (name == 'gregorian') {
					if (year < 1800) {
						return false;
					}
				}
				var month = parseInt(date[1], 10);
				var day = parseInt(date[2], 10);
				try {
					var jd = $.calendars.instance(name).newDate(year, month, day).toJD();
				} catch (e) {
					alert(e);
					return;
				}
				name == 'ummalqura' ? showDate('gregorian', jd, dest) : showDate('ummalqura', jd, dest);
			}

			function showDate(name, jd, dest) {
				try {
					$('#' + dest).val('');
					var date = $.calendars.instance(name).fromJD(jd);
					$('#' + dest).val(date.formatYear() + '/' + date.month() + '/' + date.day());
				} catch (e) {
					alert(e);
					return null;
				}
			}
		}); // end of script
	</script>
</body>

</html>