<?php 
date_default_timezone_set("Asia/Dhaka");
$page_name="item_info";  

include ('../common/library.php');

include ('../common/helper.php');

include ('../template/header.php'); 

include ('../template/sidebar.php');



if(isset($_POST['save'])){

  $item_name = $_POST['item_name'];

  $uom = $_POST['uom'];

  $item_group =  $_POST['item_group'];

  $status = $_POST['status'];
  
  $brand = $_POST['brand'];
  
  $model = $_POST['model'];

  $item_code = $_POST['item_code'];
  
  $retail_price = $_POST['retail_price'];

  $regular_price = $_POST['regular_price'];

  $group_for = 1;

  $entry_by = $_SESSION['user_id'];
  
  $opening_date=date("Y-m-d");
 
$max = $conn->query("select max(item_id) as item_code from item_info where 1");
$c_data = $max->fetch_assoc();
if($c_data['item_code'] != ''){
	$max_item_id = $c_data['item_code']+1;
}else{
	$max_item_id = 50001;
}

   $insert ="insert into item_info(item_id,item_code,`item_name`,`uom`,`item_group`,brand, model,`status`,`entry_by`,`group_for`,`retail_price`,`regular_price` ) 
   Values('".$max_item_id."','".$item_code."','".$item_name."','".$uom."','".$item_group."','".$brand."', '".$model."','".$status."','".$entry_by."','".$group_for."','".$retail_price."','".$regular_price."')";
  $ins = $conn->query($insert);

if($_POST['item_stock'] > 0 && $ins==true){
  $insert_journal=$conn->query("INSERT INTO `journal_item` (`journal_date`, `warehouse`, `item_id`, `rate`, `item_in`, `item_ex`, `tr_no`, `tr_from`, `entry_at`, `entry_by`) 
  VALUES ('".date('Y-m-d')."', '".$_SESSION['warehouse']."', '".$max_item_id."', '".$_POST['regular_price']."', '".$_POST['item_stock']."', '0.00', '0', 'Opening', current_timestamp(), '')");
}
  if($ins==true)

  {

  $insert_id = mysqli_insert_id($conn);



 /* echo '<script>window.location.href = "customer_setup.php";</script>';*/

 

//Item photo

if($_FILES['item_file']['name']!=''){

$target_dir = "../files/item/";

$target_file = $target_dir . basename($_FILES["item_file"]["name"]);

$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

$file_name = $insert_id.'.'.$imageFileType;

if ($_FILES["item_file"]["size"] > 500000) {

$msg = "Sorry, your file is too large.";

}

elseif($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

$msg =  "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

}else{

move_uploaded_file($_FILES["item_file"]["tmp_name"], $target_dir.$file_name); 

}

}

//Item photo end
  $msg =  '<span style="color:green; font-weight:bold; font-size:18px;">New Item Successfully Inserted.</span>';

  } else{

  $msg =  '<span style="color:red; font-weight:bold; font-size:18px;">Plesae Try Again!</span>';

 }



}





if(isset($_POST['update'])){

    
  $update_id = $_POST['update_item_id'];

  $item_name = $_POST['item_name'];

  $uom = $_POST['uom'];

  $item_group = $_POST['item_group'];
  
  $brand = $_POST['brand'];

  $model = $_POST['model'];
  $item_code = $_POST['item_code'];

  $retail_price = $_POST['retail_price'];

  $regular_price = $_POST['regular_price'];

  $status = $_POST['status'];

  $group_for = 1;

  

  

  if($update_id>0){

  $update = "update item_info set item_name='".$item_name."', item_code='".$item_code."', uom='".$uom."',item_group='".$item_group."',brand='".$brand."' ,model='".$model."',status='".$status."',group_for='".$group_for."',retail_price='".$retail_price."',regular_price='".$regular_price."' where item_id='".$update_id."'";
  
  
  //$update_jitem="UPDATE `journal_item` SET `id`=[value-1],`journal_date`=date("Y-m-d"),`warehouse`=[value-3],`item_id`='".$update_id."',`rate`=[value-5],`item_in`=[value-6],`item_ex`=[value-7],`tr_no`=[value-8],`tr_from`="Opening",`entry_at`=[value-10],`entry_by`='".$entry_by."' WHERE 1";


$conn->query($update);

}

if($_POST['item_stock_update'] > 0 && $update==true){
	$del = $conn->query("DELETE FROM `journal_item` WHERE `item_id` = '".$update_id."' AND `tr_from` = 'Opening'");
 
	$insert_journal=$conn->query("INSERT INTO `journal_item` (`journal_date`, `warehouse`, `item_id`, `rate`, `item_in`, `item_ex`, `tr_no`, `tr_from`, `entry_by`) 
	VALUES ('".date('Y-m-d')."', '".$_SESSION['warehouse']."', '".$update_id."', '".$_POST['regular_price']."', '".$_POST['item_stock_update']."', '0.00', '0', 'Opening', '1')");
  }
  
  if($update==true)

  {

 /* echo '<script>window.location.href = "customer_setup.php";</script>';*/

 

 //Item photo

if($_FILES['item_file']['name']!=''){

$target_dir = "../files/item/";

$target_file = $target_dir . basename($_FILES["item_file"]["name"]);

$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

$file_name = $update_id .'.'.$imageFileType;

if ($_FILES["item_file"]["size"] > 500000) {

$msg = "Sorry, your file is too large.";

}

elseif($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

$msg =  "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

}else{

move_uploaded_file($_FILES["item_file"]["tmp_name"], $target_dir.$file_name); 

}

}

//Item photo end



  $msg =  '<span style="color:green; font-weight:bold; font-size:18px;">Item Successfully Updated.</span>';

  

  }

  else

  {

  $msg =  '<span style="color:red; font-weight:bold; font-size:18px;">Plesae Try Again!</span>';

 }



}

/*while($c_data = $select->fetch_assoc()){

 $p++;

 $code[$p] = $c_data['id'];

 $business_name[$p] = $c_data['business_name'];

 $propritor_name[$p] = $c_data['propritor_name'];

 $mobile_no[$p] = $c_data['mobile_no'];

 $credit_limit[$p] = $c_data['credit_limit'];

 $entry_by[$p] = $c_data['entry_by'];

} 

*/ 

$max = $conn->query("select max(item_id) as item_code from item_info where 1");
$c_data = $max->fetch_assoc();
if($c_data['item_code'] != ''){
	$max_item_id = $c_data['item_code']+1;
}else{
	$max_item_id = 50001;
}

?>


		<div class="main-panel">

			<div class="content">

                     <div class="row">

						<div class="col-md-12">

							<div class="card">

								<div class="card-header">

									<div class="d-flex align-items-center">

										<h4 class="card-title"><?=$mgs;?></h4>

										<button class="btn btn-dark btn-round ml-auto" data-toggle="modal" data-target="#addRowModal">

											<i class="fa fa-plus"></i>

											Add New

										</button>

									</div>

								</div>

								<div class="card-body">

									<!-- Modal -->

									<form action="<?=$_POST['PHP_SELF']?>" method="post" enctype="multipart/form-data">

									<div class="modal fade" id="addRowModal" tabindex="-1" role="dialog" aria-hidden="true">

										<div class="modal-dialog" role="document">

											<div class="modal-content">

												<div class="modal-header no-bd">

													<h5 class="modal-title">
														<span class="fw-mediumbold">Item</span> 
														<span class="fw-light">Information</span>
													</h5>

													<button type="button" class="close" data-dismiss="modal" aria-label="Close">
														<span aria-hidden="true">&times;</span>
													</button>

												</div>

												<div class="modal-body">

													<p class="small">Make Sure Fillup All Field</p>

													

														<div class="row">
															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>Item ID <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="item_id"  id="item_id"  class="form-control" value="<?=$max_item_id?>" readonly="readonly" required>
																</div>
															</div>

															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>Item Code <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="item_code"  id="item_code"  class="form-control" value=""  required>
																</div>
															</div>

															<div class="col-sm-12">
																<div class="form-group form-group-default">
																	<label>Item Name <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="item_name"  id="item_name"  class="form-control" placeholder="Item Name" required>
																</div>
															</div>

															

															<div class="col-md-6 pr-0">
																<div class="form-group form-group-default">
																	<label>Item Brand <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input name="brand" id="brand" class="form-control" list="item_brand" />
																	<datalist id="item_brand">
																	<? foreign_relation('item_info','brand','brand',$brand,' 1 group by brand');?>
																	</datalist>
																</div>
															</div>

															<div class="col-md-6 pr-0">
																<div class="form-group form-group-default">
																	<label>Item Model <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input name="model" id="model" class="form-control" list="item_model" />
																	<datalist id="item_model">
																	<? foreign_relation('item_info','model','model',$model,' 1 group by model');?>
																	</datalist>
																</div>
															</div>

															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>UOM <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="uom"  id="uom"  class="form-control" placeholder="Unit">
																</div>
															</div>

															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>Retail price <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="retail_price"  id="retail_price"  class="form-control" placeholder="Retail price">
																</div>
															</div>
															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>Regular Price <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="regular_price"  id="regular_price"  class="form-control" placeholder="Regular Price">
																</div>
															</div>

															<div class="col-md-6 pr-0">
																<div class="form-group form-group-default">
																	<label>Item Type <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<select name="item_group" id="item_group" class="form-control">
																	 <option value="Product">Product</option>
																	 <option value="Service">Service</option>
																	</select>
																</div>
															</div>

															<!-- <div class="col-md-12">
																<div class="form-group form-group-default">
																	<label>Company name <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<select name="group_for" id="group_for" class="form-control" required>
																	  <?php //foreign_relation('company_info','id','company_name','','1 and status ="Active"');?>
																	</select>
																</div>
															</div> -->

															<div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Staus</label>
																	<select name="status" id="status" class="form-control">
																	 <option value="Active">Active</option>
																	 <option value="Inactive">Inactive</option>
																	</select>
																</div>
															</div>

															<div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Item Stock</label>
																	<input name="item_stock" id="item_stock" type="text" class="form-control">
																</div>
															</div>

															<div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Item Image</label>
																	<input name="item_file" id="item_file" type="file" class="form-control">
																</div>
															</div>
														</div>

													

												</div>

												<div class="modal-footer no-bd">

													<!--<button type="button" id="addRowButton" class="btn btn-primary">Add</button>-->

													<input type="submit" name="save"  value="Add" class="btn btn-primary" />

													<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

												</div>

											</div>

										</div>

									</div>

								  </form>



									<div class="table-responsive">

										<table id="add-row" class="display table table-striped table-hover basic-datatables" >

											<thead>

												<tr>
													<th>Item ID</th>
													<th>Item Code</th>
													<th>Item Name</th>
													<th>Item Type</th>
													<th>UOM</th>
													<th>Brand</th>
													<th>Model</th>
													<th>Retail Price</th>
													<th>Regular Price</th>
													<th style="width: 10%">Action</th>
												</tr>

											</thead>

											<tfoot>

											      <tr>
													<th></th>
													<th></th>
													<th></th>
													<th></th>
													<th></th>
													<th></th>
												</tr>

											</tfoot>

											<tbody>

											<?php

											  $select =$conn->query("select * from item_info where 1 ");

											 while($c_data = $select->fetch_assoc()){ ?>



												<tr>
												    <td><?=$c_data['item_id']?></td>
													<td><?=$c_data['item_code']?></td>
													<td><?=$c_data['item_name']?></td>
													<td><?=$c_data['item_group']?></td>
													<td><?=$c_data['uom']?></td>
													<td><?=$c_data['brand']?></td>
													<td><?=$c_data['model'];?></td>
													<td><?=$c_data['retail_price']?></td>
													<td><?=$c_data['regular_price']?></td>
													<td>

														<div class="form-button-action">

															<button type="button"  data-toggle="modal" data-target="#editRowModal<?=$c_data['item_id']?>" title="" class="btn btn-link btn-primary btn-lg" data-original-title="Edit Task">

																<i class="fa fa-edit"></i>

															</button>

															

												

								 <form action="<?=$_POST['PHP_SELF']?>" method="post" enctype="multipart/form-data">			<!--- edit modal -->

									<div class="modal fade" id="editRowModal<?=$c_data['item_id']?>" tabindex="-1" role="dialog" aria-hidden="true">

										<div class="modal-dialog" role="document">

											<div class="modal-content">

												<div class="modal-header no-bd">

													<h5 class="modal-title">

														<span class="fw-mediumbold">

														Item </span> 

														<span class="fw-light">

															Information

														</span>

													</h5>

													<button type="button" class="close" data-dismiss="modal" aria-label="Close">

														<span aria-hidden="true">&times;</span>

													</button>

												</div>

												<div class="modal-body">												
														<div class="row">

															<div class="col-sm-6">

																<div class="form-group form-group-default">

																	<label>Item ID <span style="color:#FF0000; font-size:15px;">*</span></label>

																	<input type="text" name="update_item_id"  id="update_item_id"  class="form-control" value="<?=$c_data['item_id']?>" readonly="readonly" required>

																</div>

															</div>

															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>Item Code <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="item_code"  id="item_code" value="<?=$c_data['item_code']?>"  class="form-control" value=""  required>
																</div>
															</div>

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

																<div class="form-group form-group-default">

																	<label>Item Name <span style="color:#FF0000; font-size:15px;">*</span></label>

																	<input type="text" name="item_name"  id="item_name"  class="form-control" value="<?=$c_data['item_name']?>" required>

																</div>

															</div>

															
															<div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Item Brand</label>
																	<input name="brand" id="brand" class="form-control" value="<?=$c_data['brand'];?>" />
																</div>
															</div>

															<div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Item Model</label>
																	<input name="model" id="model" class="form-control" value="<?=$c_data['model'];?>" />
																</div>
															</div>

															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>UOM <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="uom"  id="uom"  class="form-control" value="<?=$c_data['uom']?>">
																</div>
															</div>

															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>Retail price <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="retail_price"  id="retail_price" value="<?=$c_data['retail_price']?>"  class="form-control" placeholder="Retail price">
																</div>
															</div>
															<div class="col-sm-6">
																<div class="form-group form-group-default">
																	<label>Regular Price <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<input type="text" name="regular_price"  id="regular_price" value="<?=$c_data['regular_price']?>"  class="form-control" placeholder="Regular Price">
																</div>
															</div>

															<div class="col-md-6 pr-0">
																<div class="form-group form-group-default">
																	<label>Item Type <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<select name="item_group" id="item_group" class="form-control">
																	 <option <?=($c_data['item_group']=='Local')? "selected" : ""?> value="Local">Local</option>
																	 <option <?=($c_data['item_group']=='Imported')? "selected" : ""?> value="Imported">Imported</option>
																	</select>
																</div>
															</div>

															

															<!-- <div class="col-md-12">
																<div class="form-group form-group-default">
																	<label>Company name <span style="color:#FF0000; font-size:15px;">*</span></label>
																	<select name="group_for" id="group_for" class="form-control" required>
																	  <?php //foreign_relation('company_info','id','company_name',$c_data['group_for'],'1 and status ="Active"');?>
																	</select>
																</div>
															</div> -->
															
															

															<div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Staus</label>
																	<select name="status" id="status" class="form-control">
																	 <option <?=($c_data['status']=="Active")? "selected" : ""?> value="Active">Active</option>
																	 <option <?=($c_data['status']=="Inactive")? "selected" : ""?> value="Inactive">Inactive</option>
																	</select>
																</div>
															</div>

															<div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Item Stock</label>
																	<input name="item_stock_update" id="item_stock_update" type="text" value="<?php echo find_a_field('journal_item','sum(item_in-item_ex)','item_id='.$c_data['item_id'])?>" class="form-control">
																</div>
															</div>

															<div class="col-md-6">
																<div class="form-group form-group-default">
																	<label>Update Image</label>
																	<input name="item_file" id="item_file" type="file" class="form-control">
																</div>
															</div>

															

															<div class="col-md-12">
																<div class="form-group form-group-default">
																	<img src="../files/item/<?=$c_data['item_id']?>.png" style=" width:100%; height:258px;">
																</div>
															</div>

															<div class="col-md-12">
																<div class="form-group form-group-default">
																	<!-- generate barcode -->
																	
																</div>
															</div>

														</div>

													

												</div>

												<div class="modal-footer no-bd">

													<!--<button type="button" id="addRowButton" class="btn btn-primary">Add</button>-->

													<input type="submit" name="update"  value="Update" class="btn btn-primary" />

													<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

												</div>

											</div>

										</div>

									</div>

								</form>

															

															<!--edit modal end-->

														</div>

													</td>

												</tr>

												<?php } ?>

												

											</tbody>

										</table>

									</div>

								</div>

							</div>

						</div>

					</div>





			</div>





<?php include('../template/footer.php');?>