<?
include_once('includes/top.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
include_once('opendb.php');
if(isset($_POST[upd]))
{
   $ff=$_POST['ff'];
   if($ff !="none")
   {
       $target_dir = "upld/";
       $target_file = $target_dir . basename($_FILES["ff"]["name"]);
       $uploadOk = 1;
       $fl=$_FILES['ff']['name'];
       //echo $fl;
       if (move_uploaded_file($_FILES["ff"]["tmp_name"], $target_file)) {
           echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
       } else {
           echo "Sorry, there was an error uploading your file.";
       }
       print "=> File copied :".$fl."<br>";
   }
   $fp=fopen($target_file,"r") or die("Cant open file");
   $i=1;
   $dt=date("Y-m-d H:i:s");
   $slno = 1;
   while(!feof($fp))
   {
      $line=fgets($fp,1024);
      $spline=explode("\t",$line);
      $item_code = trim($spline[0]); 
      $item_name = trim($spline[1]);
      $gname     = trim($spline[2]);
      $subgroup1 = trim($spline[3]);
      $unit1     = trim($spline[4]);
      $location  = trim($spline[5]);
      $opstk     = $spline[6];
      $ndpcost   = $spline[7];
      $lstprice  = $spline[8];
      $mflg = 0;
      $msg = "";
      if(strlen($item_code)<= 0){
          $msg .= 'Blank Item Code....!';$mflg = 1;}
      if(strlen($item_name)<= 0){
          $msg .= 'Blank Item Name....!';$mflg = 1;}
      if(strlen($gname)<= 0){
          $msg .= 'Blank Group Name....!';$mflg = 1;}
      if(strlen($subgroup1)<= 0){
          $msg .= 'Blank Subgroup Name....!';$mflg = 1;}
      if(strlen($unit1)<= 0){
          $msg .= 'Blank Unit....!';$mflg = 1;}
      if($ndpcost <= 0){
          $msg .= 'Invalid NDP Cost....!';$mflg = 1;}
      if($lstprice <= 0){
          $msg .= 'Invalid List Price....!';$mflg = 1;}
      $group=mysqli_query($db,"select partcode from partmaster where partcode='$item_code'");
      if(mysqli_num_rows($group)>0)
      {
          $msg .= 'Item Already Exists....!';$mflg = 1;
      }
      $group=mysqli_query($db,"select gname from groupmaster where gname='$gname'");
      if(mysqli_num_rows($group)<=0)
      {
          $msg .= 'Invalid Group....!';$mflg = 1;
      }
      $subgroup=mysqli_query($db,"select subname from subgroupmaster where subname='$subgroup1'");
      if(mysqli_num_rows($subgroup)<=0)
      {
          $msg .= 'Invalid Subgroup....!';$mflg = 1;
      }
      $unit=mysqli_query($db,"select agrp from unit where agrp='$unit1'");
      if(mysqli_num_rows($unit)<=0)
      {
          $msg .= 'Invalid Unit Name....!';$mflg = 1;
      }
      if($mflg == 0)
      {
          $sql="insert into partmaster (partcode,partname,gname,subgroup,unit)values('$item_code','$item_name','$gname','$subgroup1','$unit1')";
          mysqli_query($db,$sql) or die(mysqli_error($db));    
          $meffectfrom = date('Y-m-d');
          $sql="insert into spareprmast(partcode,effectfrom,effectto,ndpcost,lstprice,brcode) values('$item_code','$meffectfrom','2025-03-31',$ndpcost,$lstprice,'$brcode')";
          mysqli_query($db,$sql) or die(mysqli_error($db));    
          $sql="insert into sparestock(itemcode,itemname,location,opstk,curstk,brcode,item_grp, item_subgrp,item_unit,item_active) values('$item_code','$item_name','$location',$opstk,$opstk,'$brcode','$gname','$subgroup1','$unit','Y')";
          mysqli_query($db,$sql) or die(mysqli_error($db));    
          print 'Successfully Uploaded : Line Item #'.$slno.$item_code.$item_name."<br>";
      }
      else
      {
          print 'Error on Line Item #'.$slno.$msg."<br>";
      }
      ++$slno;
   }
   print "<script> alert('Done');</script>";
}
?>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link type="text/css" href="css/form1.css" rel="stylesheet" />
<style>
tr{background-color:c1d799}
.mnu{margin-left:5}
</style>
<style>
.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
    visibility: hidden;
    width: 540px;
    background-color: red;
    color: white;
    text-align: center;
    border-radius: 6px;
    padding: 5px 0;

    /* Position the tooltip */
    position: absolute;
    z-index: 1;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
}
</style>
<center>
<br><br><br><br><br><br>
<form action='uplditem.php' method='post' ENCTYPE="multipart/form-data">
<table border=1>
<tr><td>
<table>
<th colspan=2>Upload Item Data
<tr><td>File <input type=file name='ff'></td><td><input type=submit value='Upload' name='upd'></td></tr>
<tr><td><div class="tooltip"><font size='1'><font face='Courier New'>Format for Uploading
  <span class="tooltiptext">|----1----|-----2----|---3---|-----4----|---5--|----6----|----7----|----8----|----9-----|<br>|ITEMCODE | ITEMNAME | GROUP | SUBGROUP | UNIT |LOCATION |OP.STOCK |BUYPRICE |SELLPRICE |</span>
</div>
</td></tr>
</table>
</td></tr>
</table>
</form>
