<?php
	include_once('config.php');   
    if(!isset($_SESSION['custId'])) 
    {
         header("Location: ".ROOTPATH."/");
         die;
    }  
	
	

         if(isset($_POST['submitLogin']))
         {
               
			    if($_REQUEST['AgId']!='')
				{
					$workOrderdate = date("d-M-Y", strtotime($_POST['workOrderdate']));
					$timeLimit = date("d-M-Y", strtotime($_POST['timeLimit']));
					$extention = date("d-M-Y", strtotime($_POST['extention']));
					$recDate = date("d-M-Y", strtotime($_POST['recDate']));  
					
					if($_POST['dateOFcompetition']=='')
							$_POST['dateOFcompetition'] = "NA";
					
					if($_POST['workorderNo']=='')
						$_POST['workorderNo']  = "NA";
						

					$stmt=$conn->prepare("select recDate   from    MyWorks where  randNo=:randNo  ");   
					$stmt->bindValue(':randNo', $_REQUEST['randNo'] , PDO::PARAM_STR); 
					$stmt->execute();   
					$dateOfra = $stmt->fetch(PDO::FETCH_OBJ); 


  
					if($dateOfra->recDate!=$recDate)
					{
							$originalDate = $recDate;
							$stmt=$conn->prepare("select *  from    pw__comdtdata where  randumNo=:randNo order by id  ");   
							$stmt->bindValue(':randNo', $_REQUEST['randNo'] , PDO::PARAM_STR); 
							$stmt->execute();   
							$pw__comdtdata = $stmt->fetchAll(PDO::FETCH_OBJ);   
							$originalDate  =   date("M-Y", strtotime($originalDate));     
							foreach($pw__comdtdata as $abcd)
							{ 
							 
							$v1 = 0; 
							$dat = date('M-Y', strtotime($originalDate . ' -3 months'));  
							$stmt=$conn->prepare("select indexval,source  from pw__exclationtbl1   where commodity=:commodity and dateselect like '%$dat%' ");$stmt->bindValue(':commodity', $abcd->cmdt , PDO::PARAM_STR);
							$stmt->execute();
							$month = $stmt->fetchAll(PDO::FETCH_OBJ);  
							
							$ival = 0;
							$sr=0; 
							
							foreach($month as $mpp)
							{	    
								 
								$ival +=$mpp->indexval;
								$sr++;
							}
							 
							
							$v1 = $ival/$sr;
							$stmt=$conn->prepare("update  pw__comdtdata set index1=:index1,dat1=:dat1 where id=:id    ");   
							$stmt->bindValue(':index1', $ival/$sr , PDO::PARAM_STR); 
							$stmt->bindValue(':dat1', $dat , PDO::PARAM_STR); 
							$stmt->bindValue(':id', $abcd->id , PDO::PARAM_STR); 
							$stmt->execute();   
					
							 
							$dat = date('M-Y', strtotime($originalDate . ' -2 months'));  
							$stmt=$conn->prepare("select indexval,source  from pw__exclationtbl1   where commodity=:commodity and dateselect like '%$dat%' ");$stmt->bindValue(':commodity', $abcd->cmdt , PDO::PARAM_STR);
							$stmt->execute();
							$month = $stmt->fetchAll(PDO::FETCH_OBJ);  
							
							$ival = 0;
							$sr=0; 
							
							foreach($month as $mpp)
							{	    
								 
								$ival +=$mpp->indexval;
								$sr++;
							}
							 
							
							$stmt=$conn->prepare("update  pw__comdtdata set index1=:index1,dat2=:dat1 where id=:id    ");   
							$stmt->bindValue(':index1', $ival/$sr , PDO::PARAM_STR); 
							$stmt->bindValue(':dat1', $dat , PDO::PARAM_STR); 
							$stmt->bindValue(':id', $abcd->id , PDO::PARAM_STR); 
							$stmt->execute();   
							
							$v1 += $ival/$sr;
							
							
							
							 
							$dat = date('M-Y', strtotime($originalDate . ' -1 months'));  
							$stmt=$conn->prepare("select indexval,source  from pw__exclationtbl1   where commodity=:commodity and dateselect like '%$dat%' ");$stmt->bindValue(':commodity', $abcd->cmdt , PDO::PARAM_STR);
							$stmt->execute();
							$month = $stmt->fetchAll(PDO::FETCH_OBJ);  
							
							$ival = 0;
							$sr=0; 
							
							foreach($month as $mpp)
							{	    
								 
								$ival +=$mpp->indexval;
								$sr++;
							}
							 
							
							$stmt=$conn->prepare("update  pw__comdtdata set index1=:index1,dat3=:dat1 where id=:id    ");   
							$stmt->bindValue(':index1', $ival/$sr , PDO::PARAM_STR); 
							$stmt->bindValue(':dat1', $dat , PDO::PARAM_STR); 
							$stmt->bindValue(':id', $abcd->id , PDO::PARAM_STR); 
							$stmt->execute();   
							
							$v1 += $ival/$sr;
							
							$stmt=$conn->prepare("update  pw__comdtdata set avgdta=:avgdta where id=:id    ");   
							$stmt->bindValue(':avgdta', $v1/3 , PDO::PARAM_STR); 
							$stmt->bindValue(':id', $abcd->id , PDO::PARAM_STR); 
							$stmt->execute();  
					    	
						} 
					}   
						 
					$stmt=$conn->prepare("update MyWorks set workN=:workN,AggNo=:AggNo,workorderNo=:workorderNo,workOrderdate=:workOrderdate,timeLimit=:timeLimit,extention=:extention,recDate=:recDate,basicIndices=:basicIndices,recPientOftender=:recPientOftender,dateOFcompetition=:dateOFcompetition,TenderAccAt=:TenderAccAt,TenderAccAtStats=:TenderAccAtStats where randNo=:randNo");   
					 
					$stmt->bindValue(':workN', $_POST['workN'], PDO::PARAM_STR);   
					$stmt->bindValue(':AggNo', $_POST['AggNo'], PDO::PARAM_STR);   
					$stmt->bindValue(':workorderNo',$_POST['workorderNo'] , PDO::PARAM_STR);   
					$stmt->bindValue(':workOrderdate', $workOrderdate , PDO::PARAM_STR);   
					$stmt->bindValue(':timeLimit',$timeLimit , PDO::PARAM_STR);   
					$stmt->bindValue(':extention', $_POST['extention'], PDO::PARAM_STR);    
					$stmt->bindValue(':recDate', $recDate, PDO::PARAM_STR);      
					$stmt->bindValue(':basicIndices', $recDate  , PDO::PARAM_STR);      
					$stmt->bindValue(':recPientOftender', "NA" , PDO::PARAM_STR);      
					$stmt->bindValue(':dateOFcompetition', $_POST['dateOFcompetition'], PDO::PARAM_STR);      
					$stmt->bindValue(':TenderAccAt', $_POST['TenderAccAt'], PDO::PARAM_STR);      
					$stmt->bindValue(':TenderAccAtStats', $_POST['TenderAccAtStats'], PDO::PARAM_STR);       
					$stmt->bindValue(':randNo', $_REQUEST['randNo'], PDO::PARAM_STR);       
					$stmt->execute();    
					$_SESSION['tenderSdd'] = "Tender Profile Added";					
					header("Location: Myworks.php?AgId=".$_REQUEST['AgId'].'&randNo='.$_REQUEST['randNo']);
					die;
				}else
				{
					$_SESSION['tenderSdd'] = "Please select agency and try again";		
					header("Location: Myworks.php");
					die;					
				}
         }
         
        $stmt=$conn->prepare("select  *  from    MyWorks where  randNo=:randNo   ");   
        $stmt->bindValue(':randNo', $_REQUEST['randNo'] , PDO::PARAM_STR); 
        $stmt->execute();   
        $FinalResult = $stmt->fetch(PDO::FETCH_OBJ); 
		 

		if($_REQUEST['AgId']!='')
		{
			$stmt=$conn->prepare("select  *  from    pw__newage where  id=:id ");   
			$stmt->bindValue(':id', $_REQUEST['AgId'], PDO::PARAM_STR); 
			$stmt->execute();   
			$pw__newage = $stmt->fetch(PDO::FETCH_OBJ); 
		}



			
 

include_once("view/Myworks.php");
?>