<?php
$permission  = explode(',',$_SESSION['permission_id']);
if(!in_array(9,$permission) && !in_array(1,$permission) ){

?>
<script>
window.location="starter.php";
</script>
<?php
}
?>
<style >
	tfoot {
    display: table-header-group !important;
}

td{
	text-align: center !important;
}
th{
	text-align: center !important;
}

textarea{
	height: 38px !important;
	resize: none;
}

.table > thead > tr > th, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
    padding: 10px 5px;

    }

    .badge:empty{
    	display: initial;
    }

    .btn_add_item{
    	padding: 0px 0px;
    }

    .bootstrap-select.btn-group .btn .filter-option{
    	text-align: center;
    }

    .purchase_table {
    	width: 96% !important;
    }
</style>

<div id="add-form-cont" class="panel-collapse collapse panel" aria-expanded="false" style="height: 0px; position: relative; top:-3px; width:98%; margin:0px auto;">
	<div class="panel-body">
		<form class="form-horizontal" method="post" id="add_form" enctype="multipart/form-data">
			<div>
			
			<input type="hidden" id="total_quantity_old" name="total_quantity_old" >
			<input type="hidden" id="total_rows" name="total_rows" value="1">
				<div class="col-sm-12">

					<div class="row">
						<a id="add_new_product_btn" class="btn btn-primary  legitRipple" data-popup="tooltip" data-original-title="Add Products"  data-toggle="modal" href="#add_product_form_cont"> <b><i class="icon-add"></i></b> <span class="multi_lang">اضافة مادة جديدة</span>
						</a>
					</div>

					<div class="form-group">


						<div class="form-group has-feedback has-feedback-left form-group-material col-sm-3">
							<label class="control-label multi_lang">رقم وصل الشراء</label>
							<input type="text" id="purchase_number" name="purchase_number" class="form-control multi_lang stop_enter" placeholder="رقم وصل الشراء" >
							<div class="form-control-feedback">
								<i class="icon-coins text-muted"></i>
							</div>
						</div>

						<div class="form-group has-feedback has-feedback-left form-group-material col-sm-3">
							<label class="control-label multi_lang">التاريخ</label>
								<input type="text" id="purchase_date" name="purchase_date" class="form-control multi_lang daterange-single stop_enter" placeholder="التاريخ" >
								<div class="form-control-feedback">
									<i class=" icon-calendar52 text-muted"></i>
								</div>
						</div>

						<!-- <div class="form-group has-feedback has-feedback-left  col-sm-2">
							<label class="control-label multi_lang">اختيار المخزن</label>
							
							<select class="form-control multi_lang stop_enter" name="stock_id_fk" id="stock_id_fk" data-width="100%" data-live-search="true" required="required">

							</select>
							<div class="form-control-feedback">
								<i class="icon-store text-muted"></i>
							</div>
						</div> -->

						<div class="form-group has-feedback has-feedback-left  col-sm-2">
							<label class="control-label multi_lang">اختيار الفرع</label>
							
							<select class="form-control multi_lang stop_enter" name="branch_id_fk" id="branch_id_fk" data-width="100%" data-live-search="true" required="required">

							</select>
							<div class="form-control-feedback">
								<i class="icon-office text-muted"></i>
							</div>
						</div>

						<div class="form-group has-feedback has-feedback-left  col-sm-2">
							<label class="control-label multi_lang">المورد</label>
							
							<select class="form-control multi_lang stop_enter" name="supplier_id_fk" id="supplier_id_fk" data-width="100%" data-live-search="true" placeholder="المورد">

							</select>
							<div class="form-control-feedback">
								<i class="icon-truck text-muted"></i>
							</div>
						</div>

						<div class="form-group has-feedback has-feedback-left  col-sm-2">
							<label class="control-label multi_lang">النوع</label>
							
							<select class="form-control  stop_enter" name="purchase_transaction" id="purchase_transaction" placeholder="النوع" required="required">
								<option value="Purchase" selected="selected">مشتريات</option>
								<option value="Initial" >Initial</option>
							</select>
							<div class="form-control-feedback">
								<i class="icon-align-bottom text-muted"></i>
							</div>
						</div>

						<div class="col-md-6">
									<div class="form-group">
										<label class="display-block">تحميل الوصل</label>
	                    				<input type="file" name="attach_bill" id="attach_bill" class="file-styled-primary">
	                    
	                  				</div>
						</div>

						<div class="form-group has-feedback has-feedback-left col-sm-6">
							<label class="control-label multi_lang">ملاحظات</label>
							<input type="text" id="purchase_note" name="purchase_note" class="form-control multi_lang stop_enter"  > 
							<div class="form-control-feedback">
								<i class="icon-notebook text-muted"></i>
							</div>
						</div>
						


					</div>

					<div class="form-group">
						<div class="table-responsive1">
							<table class="table col-sm-12 table-bordered .table-sm">
								<thead>
									<tr class="bg-blue ">
										<th>#</th>
										<th class="multi_lang">المادة</th>
										
										<th class="multi_lang">الكمية </th>
										<th class="multi_lang">Barcode</th>
										<th class="multi_lang">تاريخ النفاذ</th>
										<th class="multi_lang">سعر شراء الوحدة</th>
										<th class="multi_lang">الكلفة بعد المصاريف</th>
										<th class="multi_lang">سعر البيع</th>
										<th class="multi_lang">الكلفة الاجمالية</th>
										<th class="multi_lang">ملاحظات</th>
										<th></th>
									</tr>
								</thead>
								<tbody id="purchase_item_body" >
									<tr  id="item_row_1" >
										<td width="5%" id="num_1">1</td>
										<td width="20%">
											
											<select class="form-control multi_lang stop_enter" name="product_id_fk_1" id="product_id_fk_1" data-width="100%"   data-live-search="true">

											</select>
										</td>
										
										
										<td width="10%" >
											<input type="number" id="purchase_quantity_1" name="purchase_quantity_1"  class="form-control text-center multi_lang stop_enter" placeholder="الكمية" value="1"  >
										</td>

										<td width="10%" >
											<input type="text" id="purchase_barcode_1" name="purchase_barcode_1"  class="form-control text-center multi_lang stop_enter barcode" placeholder="Barcode"  >
										</td>

										<td width="10%" >
											<input type="text" id="purchase_expire_1" name="purchase_expire_1"  class="form-control text-center multi_lang daterange-single stop_enter" placeholder="تاريخ النفاذ"  >
										</td>

										<td width="7%" >
											<input type="text" id="cost_1" name="cost_1"  class="form-control text-center stop_enter" value="" onchange="calc_total_item_cost(1);">
										</td>
										<td width="10%" >
											<input id="cost_expenses_1" name="cost_expenses_1" type="text" class="form-control text-center" readonly="readonly" >
										</td>
										<td width="10%">
											<input id="sell_price_1" name="sell_price_1" type="text" class="form-control multi_lang text-center stop_enter" placeholder="سعر البيع"  >
										</td>
										<td width="10%" id="total_item_cost_1"> 0 $</td>
										<td width="20%">
											<input type="text" id="purchase_item_note_1" name="purchase_item_note_1" class="form-control multi_lang submit_enter" placeholder="ملاحظات" >
										</td>
										<td width="8%">
											<button class="btn btn-link btn_add_item" onclick="Add_Rows();" id="btn_tab_add">
												<i  class="icon-add text-blue"></i>
											</button>
											
										</td>
									</tr>
									
									
								</tbody>
								<tfoot>
									<tr class="alpha-teal">
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th>
											<input type="text" id="total_expenses" name="total_expenses"  class="form-control text-center stop_enter" value="0" placeholder="المصاريف" onchange="calc_cost_expenses();">
										</th>
										<th></th>
										<th id="purchase_total">$ 0</th>
										<th></th>
										<th></th>
									</tr>

									<tr class="alpha-teal">
										<th></th>
										<th class="multi_lang">Payment</th>
										<th></th>
										<th></th>
										<th></th>
										<th></th>
										<th>
											
										</th>
										<th></th>
										<th >
										 <input type="text" id="total_paid" name="total_paid"  class="form-control text-center stop_enter" value="0" placeholder="Payment">
										</th>
										<th></th>
										<th></th>
									</tr>
								</tfoot>
							</table>
						</div>



					</div>

					

					



				</div>
			</div>

			<div class="text-right">
				<button type="submit" id="submit_add_btn" class="btn btn-primary btn-labeled btn-labeled-left">
					<span class="multi_lang">حفظ</span> <b><i class="icon-floppy-disk"></i></b>
				</button>
				<a href="#add-form-cont" id="add_form_cancel" class="btn btn-labeled btn-labeled-left bg-danger heading-btn" data-toggle="collapse">
					<span class="multi_lang">اغلاق</span>  <b><i class="icon-cross"></i></b>
				</a>
			</div>
		</form>
	</div>
</div>

<div id="edit_form_cont" class="modal fade" style="display: none;">
	<div class="modal-dialog modal-lg" style="width: 92%;">
		<form class="form-horizontal"  name="edit_form" method="post" id="edit_form" enctype="multipart/form-data">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"><i class="icon-cross"></i></button>
					<h4 class="modal-title multi_lang">تحرير المشتريات</h4>
				</div>

				<div class="modal-body">
						<input type="hidden" name="edit_objid" id="edit_objid" value="">
						<input type="hidden" id="edit_total_rows" name="edit_total_rows" value="1">


					<div class="form-group">


						<div class="form-group has-feedback has-feedback-left form-group-material col-sm-2">
							<label class="control-label multi_lang">رقم وصل الشراء</label>
							<input type="text" id="edit_purchase_number" name="edit_purchase_number" class="form-control multi_lang" placeholder="رقم وصل الشراء" >
							<div class="form-control-feedback">
								<i class="icon-coins text-muted"></i>
							</div>
						</div>

						<div class="form-group has-feedback has-feedback-left form-group-material col-sm-3">
							<label class="control-label multi_lang">التاريخ</label>
								<input type="text" id="edit_purchase_date" name="edit_purchase_date" class="form-control multi_lang daterange-single" placeholder="التاريخ" >
								<div class="form-control-feedback">
									<i class=" icon-calendar52 text-muted"></i>
								</div>
						</div>

						<!-- <div class="form-group has-feedback has-feedback-left  col-sm-2">
							<label class="control-label multi_lang">اختيار المخزن</label>
							
							<select class="form-control multi_lang" name="edit_stock_id_fk" id="edit_stock_id_fk" data-width="100%" data-live-search="true" placeholder="المخزن">

							</select>
							<div class="form-control-feedback">
								<i class="icon-store text-muted"></i>
							</div>
						</div> -->

						<div class="form-group has-feedback has-feedback-left  col-sm-2">
							<label class="control-label multi_lang">اختيار الفرع</label>
							
							<select class="form-control multi_lang stop_enter" name="edit_branch_id_fk" id="edit_branch_id_fk" data-width="100%" data-live-search="true" required="required">

							</select>
							<div class="form-control-feedback">
								<i class="icon-office text-muted"></i>
							</div>
						</div>

						<div class="form-group has-feedback has-feedback-left  col-sm-2">
							<label class="control-label multi_lang">المورد</label>
							
							<select class="form-control multi_lang" name="edit_supplier_id_fk" id="edit_supplier_id_fk" data-width="100%" data-live-search="true" placeholder="المورد">

							</select>
							<div class="form-control-feedback">
								<i class="icon-truck text-muted"></i>
							</div>
						</div>

						<div class="form-group has-feedback has-feedback-left col-sm-3">
							<label class="control-label multi_lang">ملاحظات</label>
							<textarea type="text" id="edit_purchase_note" name="edit_purchase_note" class="form-control multi_lang"  ></textarea> 
							<div class="form-control-feedback">
								<i class="icon-notebook text-muted"></i>
							</div>
						</div>
						


					</div>
						

					<div id="edited_purchase_contain">


					</div>

				</div>
				<div class="modal-footer">
					<button type="submit" id="submit_edit_btn" class="btn btn-labeled btn-labeled-left heading-btn btn-primary"> <span class="multi_lang">حفظ</span> <b><i class="icon-floppy-disk"></i></b>
					</button>
					<button type="button" class="btn btn-warning" data-dismiss="modal"> <span class="multi_lang">اغلاق</span>
					</button>
				</div>
			</div>
		</form>
	</div>
</div>


	
<div id="add_product_form_cont" class="modal fade" style="display: none; ">
		<div class="modal-dialog modal-lg" style="width: 85%;">
			<form class="form-horizontal"  name="add_product_form" method="post" id="add_product_form" enctype="multipart/form-data">
				

				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal"><i class="icon-cross"></i></button>
						<h4 class="modal-title multi_lang">اضافة مادة جديدة</h4>
					</div>

					<div class="modal-body">
						<div class="col-sm-12">

							<div class="form-group">


								<div class="form-group has-feedback has-feedback-left form-group-material  col-sm-3">
									<label class="control-label animate multi_lang">تحديد الصنف</label>
									
									<select class="form-control stop_enter" name="category_id_fk" id="category_id_fk" data-width="100%" data-live-search="true" >

									</select>
								</div>

								<div class="form-group has-feedback has-feedback-left form-group-material col-sm-3">
									<label class="control-label multi_lang">اسم المادة</label>
									<input type="text" id="product_name" name="product_name" class="form-control multi_lang stop_enter" placeholder="اسم المادة" required="required">
									<div class="form-control-feedback">
										<i class="icon-color-sampler text-muted"></i>
									</div>
								</div>

								
								<div class="form-group has-feedback has-feedback-left form-group-material col-sm-3">
									<label class="control-label multi_lang">سعر البيع</label>
									<input type="text" id="sell_price" name="sell_price" class="form-control multi_lang stop_enter" placeholder="سعر البيع" required="required" >
									<div class="form-control-feedback">
										<i class="icon-cash text-muted"></i>
									</div>
								</div>
								
								<div class="form-group has-feedback has-feedback-left col-sm-3">
									<label class="control-label multi_lang">Note</label>
									<input type="text" id="product_note" name="product_note" class="form-control " placeholder="Note">
									<div class="form-control-feedback">
										<i class="icon-info22 text-muted"></i>
									</div>
								</div>

							</div>

							

							
							

						</div>
					</div>
					<div class="modal-footer">
						<button type="submit" id="submit_add_product" class="btn btn-labeled btn-labeled-left heading-btn btn-primary"> <span class="multi_lang">حفظ</span> <b><i class="icon-floppy-disk"></i></b>
						</button>
						<button type="button" class="btn btn-warning" data-dismiss="modal"><span class="multi_lang">الغاء</span>
						</button>
					</div>
				</div>
			</form>
		</div>
	</div>



<div id="show_payments_modal" class="modal fade" style="display: none;">
		<div class="modal-dialog modal-lg">
		<form class="form-horizontal"  name="show_payments_form" method="post" id="show_payments_form" enctype="multipart/form-data">
			<input type="hidden" id="access_token" name="access_token"  value="">

			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"><i class="icon-cross"></i></button>
					<h4 class="modal-title multi_lang">عرض تفاصيل وصل الشراء</h4>
				</div>
				<div id="invoce_html_pay">
					<div class="modal-body" >
							<input type="hidden" name="inv_numb" id="inv_numb">
							<div id="invoice_show">
								<!-- Borderless table -->


								<!-- /borderless table -->
							</div>
					</div>
				</div>
				<div class="modal-footer">
					<div id="link_pdf_pay"></div>
					<!-- <button type="submit" class="btn btn-labeled btn-labeled-left heading-btn btn-primary" id="pdf_pay"> <span class="multi_lang">توليد  PDF</span>  <b><i class="icon-file-pdf"></i></b> -->
					</button>
					<button type="submit" class="btn btn-labeled btn-labeled-left heading-btn btn-success" id="print_pay"><span class="multi_lang">طباعة</span>  <b><i class=" icon-printer2"></i></b>
					</button>
					<button type="button" class="btn btn-warning" data-dismiss="modal"> <span class="multi_lang">اغلاق</span>
					</button>
				</div>
			</div>
		</form>
	</div>
</div>




	<div id="show_files" class="modal fade" >
		<div class="modal-dialog modal-lg" >

			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"><i class="icon-cross"></i></button>
					<h4 class="modal-title multi_lang">الوصل</h4>
				</div>

				<div class="modal-body">

							<div class="row" id="file_attach">

							</div>

				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-labeled btn-labeled-left heading-btn btn-primary"  data-dismiss="modal"> OK
					</button>
					<button type="button" class="btn btn-warning" data-dismiss="modal"> <span class="multi_lang">اغلاق</span>
					</button>
				</div>
			</div>

		</div>
	</div>
				<!-- Individual column searching (text inputs) -->
				<div class="panel panel-flat">
					
					<table class="table datatable-column-search-inputs table-bordered table-hover datatable-highlight datatable-row-full datatable-key-class">
						<thead>
							<tr>
				                <th width="5%">ID</th>
								<th class="multi_lang">رقم قائمة الشراء</th>
								<th class="multi_lang">رقم وصل الشراء</th>
			                	<th class="multi_lang">التاريخ</th>
			                	<th class="multi_lang">المخزن</th>
								<th class="multi_lang">الكلفة قبل المصاريف</th>
								<th class="multi_lang">الكلفة الكلية</th>
								<th class="multi_lang">المورد</th>
								<th class="multi_lang">الملاحظات</th>
								<th class="multi_lang">الفرع</th>
								<th class="multi_lang">الوصل</th>
			                	<th class="text-center multi_lang">العمليات</th>
				            </tr>
						</thead>
						<tbody>
							
						</tbody>
						<tfoot>
							<tr>
				                <td></td>
				                <td>username</td>
				                <td>full_name</td>
				                <td>EMail</td>
				                <td>Phone</td>
				                <td></td>
				                <td></td>
				                <td></td>
				                <td></td>
				                <td></td>
				                <td></td>
				                <td></td>
				            </tr>
						</tfoot>
					</table>
				</div>
				<!-- /individual column searching (text inputs) -->



<script type="text/javascript" src="controllers/purchase.js?random=<?php echo uniqid(); ?>"></script>