<html>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
 $(document).ready(function(){
  $("#obs_save").click(function(){

$("#msgbox1").html('<small>Loading...</small>');
      

   
  	 var values1 = $("#f1").serialize();
 	

  	  $.ajax({
   	   url: "sv_observrepo_demo.php",
    	  type: "POST",
   	  data:   values1,
     	  success: function(msg){

 

           	 if(parseInt(msg)!=0)    

          	  {

                $('#msgbox1').html(msg);
               
		}
           

      	},
      error:function(){
          alert("failure");
          $("#msgbox1").html('there is error while submit');
      }   
   	 }); 

	 }); 
	 }); 


</script>

<script>
       var  cur3=5;
       var obs=[];

        function newrow(r){
              var rowCount = document.getElementById('myTable3').rows.length;
              var x=document.getElementById('myTable3').insertRow(rowCount);
              var a=x.insertCell(0)
		var y=x.insertCell(1)
		var y1=x.insertCell(2);

              a.innerHTML=cur3;
              y.innerHTML="<textarea   name=obs[] id=obs"+cur3+"></textarea>"; 
              y1.innerHTML="<input type='button'  onClick='delrow3(this)' value='X' >";
           
              cur3++; 

         }

        function delrow3(r){
           
           var i=r.parentNode.parentNode.rowIndex;
           document.getElementById('myTable3').deleteRow(i);
            
         }

   

       </script>

      <style>

        #myTable3 th{

         border-collapse:collapse;

         border:1px solid white;

      }

       #myTable3 td{

        // background-color:white;
         }
     </style>
</head>
<body bgcolor='#B0B0B0'>

<?

	session_start();
include("test.php");
include("opendb.php");
include("includes/function.php");
include("calstock.php");

	$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
	$kar=mysqli_fetch_array($ker);
	if(strlen($brcode)<=0)
	{
	  print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
	  return;
	}
   
	if($svflg==2)
	{
             if(($pmode=='select')||(strlen($pmode)<=0)){
              print "Invalid Payment Type";
 		exit;
             }  
                
                 

		/*$sq="select jvno from jobcard where jcno='$jcno' and brcode='$brcode'";
		$r_r=mysqli_query($db,$sq) or die(mysqli_error($db));
		$r_row=mysqli_fetch_array($r_r);

		if($r_row[0]>0)
       	 {
 			print "<script> alert('Duplicate saving of jobcard $jcno'); </script>";
 			return;
	 	 }*/

 		$i=0;
 		foreach($partcode as $a)
 		{
		 $partcode[$i]=$a; 
	 	 $i++;
	       }
 	      $i=0;

 	      foreach($partname as $a)
 	      {
	 	$partname[$i]=$a; 
 		$i++;
 	      }

            $i=0;
	     foreach($rate as $a)
	      {
 		$rate[$i]=$a; 
 		$i++;
            }

 	    $i=0;
 	    foreach($qty as $a)
	    {
	      $qty[$i]=$a; 
 	      $i++;
	    }

         /* $i=0;
	   foreach($amt as $a)
 	   {
 	      $amt[$i]=$a; 
 	      $i++;
          }*/

 	  $i=0;
         foreach($disc as $a)
         {
 		$disc[$i]=$a; 
 		$i++;
         }

 	/* $i=0;
        foreach($vat as $a)
        {
 		$vat[$i]=$a; 
 		$i++;
        }

        $i=0;
 	 foreach($cessp as $a)
 	 {
 		$cessp[$i]=$a; 
 		$i++;
        }

	 $i=0;
 	 foreach($cesamt as $a)
 	{
 		$cessamt[$i]=$a; 
 		$i++;
       }*/

 	$i=0;
       foreach($hnd as $a)
 	 {
	 	$hnd[$i]=$a; 
	 	$i++;
       }

      /* $i=0;
 	foreach($taxamt as $a)
       {
	 	$taxamt[$i]=$a; 
		$i++;
       }*/

     $i=0;
     foreach($spareengineer as $a)
     {
 		$spareengineer[$i]=$a; 
 		$i++;
     }

     /*foreach($engineer as $a)
     {
	 	$engineer[$i]=$a; 
	 	$i++;
     }*/


     $i=0;
     foreach($gtot as $a)
     {
 		$gtot[$i]=$a; 
 		$i++;
     }
   $i=0;
 	    foreach($worktype as $a)
	    {
	      $worktype[$i]=$a; 
 	      $i++;
	    }
 $i=0;
 	    foreach($assignto as $a)
	    {
	      $assignto[$i]=$a; 
             if(strlen($a)<=0)
              {
               echo 'Tech.Name Must Enter In Parts';
               exit;
              }
 	      $i++;
	    }

$i=0;
foreach($day as $a)
{
 $day[$i]=$a; 
 $i++;
}
$i=0;
foreach($month as $a)
{
 $month[$i]=$a; 
 $i++;
}
$i=0;
foreach($year as $a)
{
 $year[$i]=$a; 
 $i++;
}
$i=0;
foreach($pur_rate as $a)
{
 $pur_rate[$i]=$a; 
 $i++;
}






    $flg=0;

    for($j=0;$j<$i;$j++){

	 $k=$j+1;
 	 $qry=mysqli_query($db,"select partname from $tbl2 where partname='$partname[$j]' and partcode='$partcode[$j]' ");

	 if(mysqli_num_rows($qry)<=0)
 	 {
  	   	print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
   	  	 $flg++;
                 
        }

	 if($partname[$j]=='')
	 {
  	  	print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
  		 $flg++;
 	 }
    }




   $n=0;
          foreach($service as $a)
          {
              $service[$n]=$a; 
 		$n++;
	   }

	  $n=0;
	  foreach($servamt as $a)
	  {
 	      $servamt[$n]=$a; 
             $n++;
	  }

        	  $n=0;
	  foreach($lab_dis as $a)
	  {
 	      $lab_dis[$n]=$a; 
             $n++;
	  }


    $n=0;
	foreach($engineer1 as $a)
	{
 	$engineer1[$n]=$a; 
       if(strlen($a)<=0)
              {
               echo 'Tech.Name Must Enter In Service';
               exit;
              }
 	$n++;
	}

       $n=0;
	foreach($jobs as $a)
	{
 	$jobs[$n]=$a; 
 	$n++;
	}
         $n=0;
	foreach($rmk as $a)
	{
 	$rmk[$n]=$a; 
 	$n++;
	}
	

	$n=0;
	foreach($subnm as $a)
	{
 		$subnm[$n]=$a; 
 		$n++;
	}

	$n=0;
	foreach($subamt as $a)
	{
 		$subamt[$n]=$a; 
 		$n++;
	}
 $n=0;
 	    foreach($serworktype as $a)
	    {
	      $serworktype[$n]=$a; 
 	      $n++;
	    }
 $n=0;
foreach($day1 as $a)
{
 $day1[$n]=$a; 
 $n++;
}
$n=0;
foreach($month1 as $a)
{
 $month1[$n]=$a; 
 $n++;
}
$n=0;
foreach($year1 as $a)
{
 $year1[$n]=$a; 
 $n++;
}
      

   $flg3=0;

          for($k=0;$k<$n;$k++){
           $m=$k+1;
         
            if($subamt[$k]>$servamt[$k])

             {
                        print "<script>document.getElementById('myTable2').rows[$m].cells[0].style.color='red';</script>";
                         $flg3++;
             }


         }

if($flg3>0){
 echo "Sublet amt should be less than service amt.";
exit;
}




    if($flg>0)
    {
  		echo "<small>partname and partcode of Red marked row(s) couldn't match.</br></small>";
    }
    else
    {
              $entrydate=$entrdate3.'-'.$entrdate2.'-'.$entrdate1;

//********** chk ledger

   		$chre=mysqli_query($db,"select * from ledger where led='$cname' and brcode='$brcode'");

 		if(mysqli_num_rows($chre)<=0)
 		{
		$code=substr($cname,0,3).date(ymds);
		$code=str_replace(' ','-',$code);
      	 	$sql="insert into ledger values('$cname','Sundry Debtors','0','Dr','$code','$brcode')";
		mysqli_query($db,$sql);
 		}

 		//$tbl="sparetmp".$brcode;
		 $invres=mysqli_query($db,"select max(invno) from gateentry where brcode='$brcode' ");
		 $invrow=mysqli_fetch_row($invres);

		 if($invrow[0]>0)
 			$invno=($invrow[0]+1);
		 else
	 		$invno=1;

		mysqli_query($db,"delete from jcardparts where jcno='$jcno' and brcode='$brcode'");
		mysqli_query($db,"delete from jcardserv where jcno='$jcno' and brcode='$brcode'");

		//include ("spares/calcsparestock.php");

		$jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
		$jvrow=mysqli_fetch_row($jvres);

		if($jvrow[0]>0)
 			$jvno=($jvrow[0]+1);
		else
                     $jvno=1;



	      if($updprint=='1')
	      { 
                     $dt=date('Y-m-d');
			$sql="update gateentry set jvno='$jvno',invno='$invno',inv_date='$dt'  where jcno='$jcno' and brcode='$brcode'";
                    // mysqli_query($db,$sql) or die("Err 0".mysqli_error($db));
	      }


      		$t=1;
		$edt=date('Y-m-d');
		for($j=0;$j<$i;$j++)
              {
		  if($qty[$j]>0)
		  {
                $dt[$j]=$year[$j].'-'.$month[$j].'-'.$day[$j];
			if($updprint=='1')
			{
  				echo	$sql="insert into 	jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '','$disc[$j]', '', '' , '', '$hnd[$j]', '', '$gtot[$j]','$jvno','$invno','$dt[$j]','$spareengineer[$j]','','$edt','$worktype[$j]','$assignto[$j]','$pur_rate[$j]')";
			 
                       }
			else
			{

				if (strlen($spareengineer[$j])!=0)
 				{
  					echo	 $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '','$disc[$j]', '','' , '', '$hnd[$j]', '', '$gtot[$j]','0','0','$dt[$j]','$spareengineer[$j]','','$edt','$worktype[$j]','$assignto[$j]','$pur_rate[$j]')";

				 	
				}
			
                           else
   				{
                                echo	  $sql="insert into jcardparts values('$jcno','$brcode','$partcode[$j]','$partname[$j]','$rate[$j]', '$qty[$j]', '','$disc[$j]', '','' , '', '$hnd[$j]', '', '$gtot[$j]','0','0','$dt[$j]','$spareengineer[$j]','','$edt','$worktype[$j]','$assignto[$j]','$pur_rate[$j]')";
 					
					 
  		              }
                    }

                   // mysqli_query($db,$sql) or die("Err 1".mysqli_error($db));

		}
           }//for loop close

    	

	for($j=0;$j<$n;$j++)
	{
            
            $dt1[$j]=$year1[$j].'-'.$month1[$j].'-'.$day1[$j];
    
		if($servamt[$j]>0)
		{
			if($updprint=='1')
			{
 			echo		$sql="insert into jcardserv (jcno, brcode, service,servamt,descr,jvno,invno,engineer,subnm,subamt,rmk,edate,worktype,dis) values('$jcno','$brcode','$service[$j]','$servamt[$j]','$jobs[$j]','$jvno','$invno','$engineer1[$j]','$subnm[$j]','$subamt[$j]','$rmk[$j]','$dt1[$j]','$serworktype[$j]','$lab_dis[$j]')";
			 
                     }
			else
			{
 			 echo	    $sql="insert into jcardserv (jcno, brcode, service,servamt,descr,jvno,invno,engineer,subnm,subamt,rmk,edate,worktype,dis) values('$jcno','$brcode','$service[$j]','$servamt[$j]','$jobs[$j]','$jvno','$invno','$engineer1[$j]','$subnm[$j]','$subamt[$j]','$rmk[$j]','$dt1[$j]','$serworktype[$j]','$lab_dis[$j]')";
				
			}
			
 		//mysqli_query($db,$sql) or die("Err 3".mysqli_error($db));
 	      }
      }
	
	$x=0;
	

  			$jpres=mysqli_query($db,"select sum(gtot) from jcardparts where jcno='$jcno' and brcode='$brcode'")or die('jpres err'.mysqli_error($db));
  			$jprow=mysqli_fetch_row($jpres);
  
 			if($jprow[0]>0)
  				$x=1;  

  			$jservres=mysqli_query($db,"select sum(servamt),sum(dis) from jcardserv where jcno='$jcno' and brcode='$brcode'")or die('jservres err'.mysqli_error($db));
  			$jservrow=mysqli_fetch_row($jservres);
 			if($jservrow[0]>0)
  			$x=1;  
 
                     $ltot=$jservrow[0]-$jservrow[1];
  			 $gtot=($jprow[0]+$ltot);

                     $round_prt=round($jprow[0]);
                     $round_lab=round($ltot);


 			if($x<=0)
  			{
    				print "<script> alert('Invalid Details'); </script>";
    				//exit;
  			}
                       
                    
  			echo  $sql="insert into journal2 values('$entrydate','$jvno','Dr','$cname','$round_prt','0','Job Card Invoice:(Invno: $invno)','$brcode','Job Crd','JB Crd','$jcno','$jcno')";
 			//mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error($db)); 
 

                 echo     $sql="insert into journal2 values('$entrydate','$jvno','Cr','sale-jobcard','0','$jprow[0]','Job Card Invoice:(Invno: $invno)','$brcode','JC Spare Invoice','Job Crd','$jcno','0')";
     				//mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db)); 



                     
                   $gtot=$round_lab+$round_prt;

                echo    $sql="insert into journal2 values('$entrydate','$jvno','Dr','$cname','$gtot','0','Job Card(labour) Invoice:(Invno: $invno)','$brcode','JB Crd-labour','JB Crd-labour','$jcno','$jcno')";
 			//mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error($db));
 

                    echo  $sql="insert into journal2 values('$entrydate','$jvno','Cr','Labour','0','$ltot','Job Card Invoice:(Invno: $invno)','$brcode','JC Labour Invoice','Job Crd-labour','$jcno','0')";
     			//	mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db)); 


                                      
                             
			$jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
			$jvdifrow=mysqli_fetch_row($jvdifres);
			$dif=$jvdifrow[0];

			if($dif<0)
			{
				echo $sql="insert into journal2 values('$entrydate','$jvno','Dr','Round_of','".abs($dif)."', '0','Round of adjusted (JC)Inv No: $invno','$brcode','Job Card','jcno','$jcno','$invno')";
				//mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
			}
			else if($dif>0)
			{
				echo  $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round_of','0','".abs($dif)."','Round of adjusted (JC)Inv No: $invno','$brcode','Job Card','jcno','$jcno','$invno')";
				// mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
			}
                      

			if($pmode=='11CASH')
			{
				$rs=mysqli_query($db,"select max(crno) from cshrec where brcode='$brcode'");
				$cr_row=mysqli_fetch_row($rs);

 				if($cr_row[0]<=0)
   					$crno=1;
 				else   
   					$crno=$cr_row[0]+1;

				$jpostres=mysqli_query($db,"select sum(dramt) from journal2 where jvno='$jvno' and partic='$cname' and brcode='$brcode'");
				$jpostrow=mysqli_fetch_row($jpostres);
				$jvno++;
				$amt22=round($jpostrow[0]);
				$sql="insert into journal2 values('$entrydate','$jvno','Dr','Cash','$amt22','0','Jobcard of $cname(Invno:$invno,CR:$crno)','$brcode','CR','JC invoice','$jcno','$crno')";
				mysqli_query($db,$sql) or die(mysqli_error($db));

 				$sql="insert into journal2 values('$entrydate','$jvno','Cr','$cname','0','$amt22','Jobcard of $cname(Invno:$invno)','$brcode','CR','JC invoice','$jcvno','$crno')";
				mysqli_query($db,$sql) or die(mysqli_error($db));

 				$sql="insert into cshrec values('$entrydate','$crno','Cr','$cname','0','$jpostrow[0]','Jobcard of $cname(Invno:$invno)','$brcode','$jvno','jcno','$jcno')";
				mysqli_query($db,$sql) or die(mysqli_error($db));

				print "<script> window.open('../accounts/payrec/prncshrec.php?no=$crno'); </script>";

		     } // end if pay type

  		  // print "<script> window.open('prnjcardbill.php?jcno=$jcno'); </script>";

                  print "<font color='white'> Updation hasbeen Confirmed </font>";
                 ?>
                  [<a href='printest.php?jcno=<? echo $jcno; ?>&&pre_by=<? echo $pre_by; ?>'  target='blank' style="color:#FFCC66">Print </a>]
                <?
                 echo "<script> document.getElementById('svflg').value=1;</script>";

	

                  ?>
            <form id='f1' name='f1'>
        <fieldset style="border:2px solid white; border-style:dotted;"><legend>Add Observation</legend>
       <table id='myTable3' >

         
		
		<tr><td>1</td><td><textarea name='obs[]' id='obs1'></textarea></td><td><input type='button' name='del' value='X' onclick='delrow3(this)'/></td></tr>
		<tr><td>2</td><td><textarea name='obs[]' id='obs2'></textarea></td><td ><input type='button' name='del' value='X'  onclick='delrow3(this)'/></td></tr>
		<tr><td>3</td><td><textarea name='obs[]' id='obs3'></textarea></td><td><input type='button' name='del' value='X'  onclick='delrow3(this)'/></td></tr>
		<tr><td>4</td><td><textarea name='obs[]' id='obs4'></textarea></td><td ><input type='button' name='del' value='X' onclick='delrow3(this)'/></td></tr> 
	</table>
        <input type='hidden' name='jcno' value='<? echo $jcno; ?>'/>
        <input type='hidden' id='updflg' name='updflg' value='2'/>
       <input type='button' name='obs_save' id='obs_save' value='Save'>
	<a href='#' id='addbutton' onclick='newrow(this)'> <input type='button' name='addnew' value='Add new'></a>

     </fieldset></form>
      <div id='msgbox1' style="height:30px;"></div>
                  <?
        
           print "<font color='white'> Data has been updated </font>";

            
	//echo "<script> document.getElementById('svflg').value=1;document.location='estimatenew.php';</script>";

          
     }

          
   }
   elseif($svflg==1)
   {
   echo "Data already saved";
 // echo "<script> document.location='estimatenew.php';</script>";
   }
?>

