<?php
$is_done = false;
//// insert / update into database table
if(isset($_POST['edit_id'])){
	$creation_date = date("Y-m-d");
	//$spec_preparedby = ($_SESSION['user_id']==0) ? $_SESSION['id'] : $_SESSION['user_id'];
	if($_POST['edit_id'] == ""){
	////// insert records
		$sql = "INSERT INTO `var` 
				(`companyID`, `var_project_id`, `var_sow_id`,
				 `var_type`, `var_title`, `var_no`,
				  `var_dataentry_date`, `var_claimdate`,
				   `var_target`, `var_impact`, `var_status`, 
				   `var_inputby`, `var_presented_to`, `var_presented_toto`,
				    `var_desc`,`var_phase`,`var_zone`) 
				VALUES ('".$companyID."', '".$_POST['var_projectnamef']."', '".$_POST['var_sow_namef']."',
				 '".$_POST['var_typef']."', '".$_POST['var_titlef']."', '".$_POST['var_nof']."',
				  '".$creation_date."', '".$_POST['var_date1f']."', '".$_POST['var_date1f3']."',
				   '".$_POST['var_impactf']."', '1', '".$ipusername."', '".$_POST['ReceiptFrom']."',
				    '".$_POST['input_name']."', '".$_POST['introduction']."', '".$_POST['input-phase1']."',
				     '".$_POST['input-zone1']."')";
		mysql_query($sql) or die("Failed to Add Data : ".mysql_error());

        $BID = mysql_insert_id();
        // copying sow-brief
        $copyQ = "INSERT INTO scope_brief (heading, is_editable, scope_type, brief_id, brief_type, user_id, company_id, date_creation, date_update, status, parent)
  SELECT heading, is_editable, 'variation-brief', '$BID', brief_type, $uid, $id, '$date', '$date', status, parent from scope_brief
  WHERE scope_type='sow-brief' AND company_id=0  AND brief_id= 0 AND status =1";

        mysql_query($copyQ);




		$msg = "Data has been added successfully";		
	}else{
	////// update records
		$sql = "UPDATE `var` SET 
				`var_project_id` = '".$_POST['var_projectnamef']."',
				`var_impact` = '".$_POST['var_impactf']."',
				`var_sow_id` = '".$_POST['var_sow_namef']."',
				`var_type` = '".$_POST['var_typef']."',
				`var_title` = '".$_POST['var_titlef']."',
				`var_no` = '".$_POST['var_nof']."',
				`var_desc` = '".$_POST['introduction']."',
				`var_claimdate` = '".$_POST['var_date1f']."',
				`var_target` = '".$_POST['var_date1f3']."',
				`var_presented_to` = '".$_POST['ReceiptFrom']."',
				`var_presented_toto` = '".$_POST['input_name']."',
				`var_phase` = '".$_POST['input-phase1']."',
				`var_zone` = '".$_POST['input-zone1']."'
				WHERE `var_id` = '".$_POST['edit_id']."'";
		mysql_query($sql) or die("Failed to Update : ".mysql_error());
		$msg = "Data has been updated successfully";	
	}
	$is_done = true;
}
//// end insert / update into database table
//// select record for edit 
if(isset($_REQUEST['update']) && $_REQUEST['update']){
	$result = mysql_query("SELECT * FROM var WHERE `var_id` = '".$_REQUEST['update']."' AND companyID = '$id'");
	if(mysql_num_rows($result)){
        $VAR = mysql_fetch_array($result);
	}
}
//// end select record for edit
//// start delete record
if(isset($_REQUEST['del']) && $_REQUEST['del'] != ''){
	mysql_query("UPDATE `var` SET `var_status` = '2' WHERE `var_id` = '".$_REQUEST['del']."'");
	//mysql_query("UPDATE `CloseoutPunch` SET `punch_status` = '2' WHERE `punch_handover_id` = ".$_REQUEST['del']."");
	//mysql_query("DELETE FROM `var_Stakeholders` WHERE `var_id` = ".$_REQUEST['del']."");
	$msg = "Data has been deleted successfully";
	$is_done = true;
}
//// end delete record
//// change status to 0 or 1
if(isset($_REQUEST['statusId']) && $_REQUEST['statusId'] != '' && isset($_REQUEST['status']) && $_REQUEST['status'] != ""){	
	$sql = "UPDATE `var` SET `var_status` = '".$_REQUEST['status']."' WHERE `var_id` = '".$_REQUEST['statusId']."'";
	mysql_query($sql) or die("Failed to Update Status : ".mysql_error());
	if($_REQUEST['status'] == 0){
		//mysql_query("UPDATE `CloseoutPunch` SET `punch_status` = '".$_REQUEST['status']."' WHERE `punch_handover_id` = ".$_REQUEST['statusId']."");
	}
	$msg = "Status has been updated successfully";
	$is_done = true;
}



if(isset($_REQUEST['lock'])) {
    include('../../includes/checkSession.php');
    $sql = "UPDATE `var` SET `var_status` = '4' WHERE `var_id` = '".$_REQUEST['lock']."'";
    if(mysql_query($sql))
        $msg = "DOCUMENT LOCKED";
    else
        $msg = "DOCUMENT NOT LOCKED";

    $is_done = true;

}

if(isset($_REQUEST['unlock'])) {
    include('../../includes/checkSession.php');
    $sql = "UPDATE `var` SET `var_status` = '1' WHERE `var_id` = '".$_REQUEST['unlock']."'";
    if(mysql_query($sql))
        $msg = "DOCUMENT UNLOCKED";
    else
        $msg = "DOCUMENT NOT UNLOCKED";

    $is_done = true;
    echo '<script>window.location="03-08Scope-01Variations02.php?BID='.$_REQUEST[unlock].'&msg='.$msg.'";</script>';
    die();
}




///////////////send
else if(isset($_REQUEST['send'])){
    include('../../includes/checkSession.php');
    $bid = $_REQUEST['send'];
    $sub="";
    if(mysql_query("UPDATE var SET var_status ='3' WHERE var_id = '$bid'"))
    {
        $result = mysql_query("SELECT * FROM var where companyID='$id' AND var_id='$bid'");
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        $rs = $row["var_title"];
        $an = $row["var_project_id"];
        $PreparedBy = $row["var_inputby"];
        $briefCrev = 1;// $row["sowrev"];
        $client_id = '';//$row["sowClient"];
        $sub.=	"VAR SOW - ". $bid." |  ".$rs." (".$briefCrev.")";
        $description ="";

        $result2 = mysql_query("SELECT id, heading from scope_brief WHERE scope_type='variation-brief' AND (company_id = '$id') AND ( brief_id = '$bid') AND parent = 0");
        $k= 1;
        while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
            $results = mysql_query("SELECT description from scope_brief_description WHERE `heading_id` = '".$row2['id']."' AND `brief_id` ='".$bid."' AND company_id= '$id'");
            $rows = mysql_fetch_array($results);


            $description .= $k.".";
            $description .= "&nbsp;&nbsp;&nbsp;";
            $description .= $row2['heading'];
            $description .= "<br>";
            $description .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            $description .= $rows["description"];
            // $description .= "<br><br>";



            $resultH = mysql_query("SELECT * FROM scope_brief WHERE parent = ".$row2['id']." AND scope_type='variation-brief' AND  company_id = '$id' AND brief_id = '$bid' ");

            $i = 1;

            while( $rowH = mysql_fetch_object($resultH) ) {
                $queryHD = "SELECT * from scope_brief_description WHERE `heading_id` = '" . $rowH->id . "' AND `brief_id` ='" . $bid . "' AND company_id= '$id'";
                $resultHD = mysql_query($queryHD);
                $rowHD = mysql_fetch_object($resultHD);

                $description .= $k . '.' . $i . '.';
                $description .= "&nbsp;&nbsp;&nbsp;";
                $description .= $rowH->heading;
                $description .= "<br>";
                $description .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                $description .= $rowHD->description; $i++;


            }
            $description .= "<br><br>";

            $k= $k+1;
        }
        $date = date('Y-m-d');
        if(mysql_query("insert into receipt set receipt_doc_date='$date', receipt_doc_subject='$sub',receipt_archive_id='$an',
receipt_des='$description', receipt_submitted_by='$client_id',receipt_submitted_by_cv='Client',
receipt_discipline_id='29',receipt_type='Initiated',receipt_initiated_by='$PreparedBy',`receipt_fc`='0',`receipt_fc1`='0',
`receipt_fc2`='0',`receipt_fc3`='0',`receipt_fc4`='0',`receipt_fc5`='0',`receipt_fc6`='0',`receipt_fd`='0',`receipt_fsprt`='0',`receipt_fspr`='0',receipt_intimation='',receipt_intimation_date='',receipt_company_id='$id'"))

            $msg = "SENT: It will show in the list of documents to be assigned as an initiated document.";
        echo '<script>window.location="03-08Scope-01Variations01.php?msg=' . $msg . '";</script>';
        die();
    }
    else
    {
        $msg = "NOT SENT";
        echo '<script>window.location="03-08Scope-01Variations01.php?msg=' . $msg . '";</script>';
        die();    }

}////////end
















//// end change status to 0 or 1
if($is_done){
	echo '<script>window.location="03-08Scope-01Variations01.php?msg='.$msg.'";</script>';
}
include("getname.php");
?>