<?php
session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/repfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;     
}
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php 
if(isset($_POST['priceupdate'])){
$cuprice = remove_junk(escape($_POST['cuprice']));
$uprice = remove_junk(escape($_POST['uprice']));
$updays = remove_junk(escape($_POST['updays']));    
$itemid = remove_junk(escape($_POST['itmid']));
     
if(floatval($uprice)<0 || $uprice=='') {
echo json_encode(array(
'status' => "error",
'message'=> "Sorry! Update Price Must =>0 Or Can't empty!!"
));
return;
exit;
}

if(floatval($updays)<0) {
echo json_encode(array(
'status' => "error",
'message'=> "Sorry! Update Days Must not < 0 Or Can't empty!!"
));
return;
exit;
}    

if(floatval($updays)>0 && floatval($uprice)>0){
$sql="UPDATE tbl_stock SET oldprice='$cuprice',price='$uprice',wday='$updays',pupdate='$dtnow' WHERE id='$itemid'";
}elseif(floatval($updays)>0 && floatval($uprice)<=0){
$sql="UPDATE tbl_stock SET wday='$updays' WHERE id='$itemid'";
}elseif(floatval($updays)<=0 && floatval($uprice)<=0){
$sql="UPDATE tbl_stock SET wday='0' WHERE id='$itemid'";    
}else{
$sql="UPDATE tbl_stock SET oldprice='$cuprice',price='$uprice',pupdate='$dtnow' WHERE id='$itemid'";    
}    
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
   
$efid=mysqli_affected_rows($con);    
if($efid>0){    
$act =remove_junk(escape('Product Price Update'));   
write_activity($aid,'PRI','Price list has been Update',$act);    
echo json_encode(array(
'status' => 'success',
'message'=> 'Data Update Successfully!!'   
));
exit;
}else{
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Data Fail to Update!!'
));    
}   
}
?>
<?php 
if(isset($_POST['addsitem'])){ 
$item=intval($_POST['item']);   
?>
<div class="col-md-12 popup_details_div addservice">
<div class="row">
<div id="server-results"><!-- For server results --></div>    
</div>
<div class="row">
<div class="col-md-12">
<div class="col-md-1"></div>
<div class="col-md-10">
<?php    
$sql="SELECT stk.id,stk.pid,stk.unqid,name,image,pcode,stk.cost,stk.oldprice,stk.udate,stk.utime,stk.uid,stk.price,stk.wday,IFNULL(brqty,0) AS brqty,IFNULL(whqty,0) AS whqty,(IFNULL(brqty,0)+IFNULL(whqty,0)) AS avqty FROM (SELECT id,pid,unqid,oldprice,CAST(pupdate AS DATE) AS udate,DATE_FORMAT(pupdate, '%r') AS utime,uid,price,wday,cost FROM tbl_stock) stk LEFT JOIN (SELECT name,image,pcode,unqid,SUM(avqty) AS brqty FROM tbl_brstock GROUP BY unqid) bst ON bst.unqid=stk.unqid LEFT JOIN (SELECT unqid,SUM(avqty) AS whqty FROM tbl_whstock GROUP BY unqid) wst ON wst.unqid=stk.unqid WHERE stk.id='$item'";    
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);    
?>
<div class="form-group">
<label>Name</label>
<input type="text" value="<?php echo $adm['name'];?>" name="name" id="name" class="form-control" placeholder="e.g. Axes Keyboard" readonly/>
<input type="hidden" maxlength="11" value="<?php echo $adm['id'];?>" class="form-control" name="itmid" autocomplete="off" readonly/>
<input type="hidden" name="priceupdate" readonly />     
</div>
<div class="form-group">
<label>Code</label>
<input type="text" maxlength="18" value="<?php echo $adm['pcode'];?>" class="form-control" placeholder="e.g. AB03BP" readonly/>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Warranty Days</label>
<input type="text" value="<?php if($adm['wday']>0){echo $adm['wday'];}?>" class="form-control" placeholder="e.g. 365" readonly/>
</div>    
</div>
<div class="col-md-4">
<div class="form-group">
<label>Old Price</label>
<input type="text" value="<?php if($adm['oldprice']>0){echo $adm['oldprice'];}?>" class="form-control" placeholder="e.g. 0.00" readonly/>
</div>    
</div>
<div class="col-md-4">
<div class="form-group">
<label>Current Price</label>
<input type="text"  name="cuprice" id="cuprice" value="<?php echo $adm['price'];?>" class="form-control" placeholder="e.g. 56" readonly/>
</div>    
</div>    
</div>
<div class="row">
<?php if(get_fild_data('tbl_setting','9','sval')==0){?>
<div class="col-md-4">
<div class="form-group">
<label>Cost</label>
<input type="text" value="<?php echo $adm['cost'];?>" class="form-control" placeholder="e.g. 56" readonly/>
</div>    
</div>    
<div class="col-md-4">    
<div class="form-group">
<label>Update Price</label>
<input type="text" maxlength="8" value="0" name="uprice" id="uprice" class="form-control" onkeypress="return isNumberKey(event)" placeholder="e.g. 105"/>
</div>
</div>
<div class="col-md-4">    
<div class="form-group">
<label>Update Days</label>
<input type="text" maxlength="8" value="<?php echo $adm['wday'];?>" name="updays" id="updays" class="form-control" onkeypress="return isNumberKey(event)" placeholder="e.g. 365"/>
</div>
</div>    
<?php }else{ ?>    
<div class="col-md-6">    
<div class="form-group">
<label>Update Price</label>
<input type="text" maxlength="8" value="0" name="uprice" id="uprice" class="form-control" onkeypress="return isNumberKey(event)" placeholder="e.g. 105"/>
</div>
</div>
<div class="col-md-6">    
<div class="form-group">
<label>Update Days</label>
<input type="text" maxlength="8" value="<?php echo $adm['wday'];?>" name="updays" id="updays" class="form-control" onkeypress="return isNumberKey(event)" placeholder="e.g. 365"/>
</div>
</div>
<?php } ?>    
</div>    
</div>    
<div class="col-md-1"></div>    
</div>    
</div>    

</div>
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="button" id="additem" class="btn btn-flat bg-purple btn-sm " value="<?php if($item <=0){echo 'Save';}else{echo 'Update';} ?>"/>
</div> 
</div>
<script type="text/javascript">

function chek_error(){
var result = true;
var price = $('#uprice').val();    


$('.LV_invalid_field').removeClass('LV_invalid_field');     
$('.LV_validation_message').remove();     

if(price.length<1){
$('#price').addClass('LV_invalid_field');   
$('#price').after("<span class='LV_validation_message LV_invalid'>Can't be empty!</span>").addClass('has-error'); 
result=false;
}else{
$('#price').removeClass('LV_invalid_field');
result=true;    
}    
    
if(price.length<1 || !result){
return false;    
}else{
return true;     
}        
}
    
$(document).on('blur', '#uprice', function() {
chek_error();    
});    
</script> 

<?php } ?>

<?php if(isset($_POST['viewitem'])){
//$sql="SELECT id,oldprice,curprice,CAST(pupdate AS DATE) AS udate,DATE_FORMAT(pupdate, '%r') AS utime,pcurid FROM tbl_item WHERE status='1' ORDER BY id ASC";    
$sql="SELECT stk.id,stk.pid,stk.unqid,name,image,pcode,stk.oldprice,stk.udate,stk.utime,stk.uid,stk.price,stk.wday,IFNULL(brqty,0) AS brqty,IFNULL(whqty,0) AS whqty,(IFNULL(brqty,0)+IFNULL(whqty,0)) AS avqty FROM (SELECT id,pid,unqid,oldprice,CAST(pupdate AS DATE) AS udate,DATE_FORMAT(pupdate, '%r') AS utime,uid,price,wday FROM tbl_stock) stk LEFT JOIN (SELECT name,image,pcode,unqid,SUM(avqty) AS brqty FROM tbl_brstock GROUP BY unqid) bst ON bst.unqid=stk.unqid LEFT JOIN (SELECT unqid,SUM(avqty) AS whqty FROM tbl_whstock GROUP BY unqid) wst ON wst.unqid=stk.unqid WHERE (IFNULL(brqty,0)+IFNULL(whqty,0))>0 ORDER BY name ASC";
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($row=mysqli_fetch_array($query)){
$id=$row['id'];
?>
<tr>
<td style="text-align:center;"><?php echo count_id();?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['pcode']; ?></td>  
<td><img src = '../inc/barcode.php?text=<?php echo $row['pcode']; ?>&codetype=code128a&orientation=horizontal&size=20&print=true'/></td>    
<td class="text-center"><?php echo $row['avqty']; ?></td>    
<td class="text-right"><?php if($row['oldprice']>0){echo numtolocal($row['oldprice'],get_fild_data('tbl_currency','1','symbol'));}?></td>
<td class="text-right"><?php echo numtolocal($row['price'],get_fild_data('tbl_currency','1','symbol'));?></td>
<td class="text-center"><?php if($row['wday']>0){echo $row['wday'].' Days';} ?></td>    
<td><?php if(strlen($row['udate'])>2){echo date("d M Y", strtotime($row['udate'])).' '.$row['utime'];}?></td>     
<td nowrap="">
<a class="btn btn-flat bg-purple edit_price" href="#" id="ED_<?php echo $row['id'];?>"><i class="fa fa-edit cat-child"></i></a>
<a class="btn btn-flat bg-purple barcode" href="#" id="PR_<?php echo $row['unqid'];?>"><i class="fa fa-barcode"></i></a>
<a class="btn btn-flat bg-purple stiker" href="#" id="ST_<?php echo $row['unqid'];?>"><i class="fa fa-tag"></i></a>    
</form> 
</td>    
</tr>    
<?php }} ?>    
<?php 
if(isset($_POST['viewitemhis'])){
if($_SESSION['access']=='1'){echo read_activity($aid,'PRI','A');}else{echo read_activity($aid,'PRI','U');}
}?>