<?php
include('../../includes/checkSession.php');
include('../../includes/con2pma3.php');

$sowName =  $_POST['SOWname'];
$sowRev =  $_POST['SOWrev'];
$sowFrom  =  $_POST['SOWfrom'];
$sowType   =  $_POST['brieftype'];
$sowClient  = $_POST['clientName'];
$sowProject =  $_POST['projectName'];
$PreparedBy = $_SESSION['user_id'];
$date = date('Y-m-d');
$id = $_SESSION['id'];

if (isset($_REQUEST['del'])) {
    $SID = $_REQUEST['del'];
    if (mysql_query("UPDATE scope_sow SET `sowStatus`=2 WHERE `sowID` = '$SID'")) {
        header("location: 03-04Scope-01SOW01.php?msg=DELETED");
    } else {
        header("location: 03-04Scope-01SOW01.php?msg=NOT DELETED");
    }
}
else if (isset($_REQUEST['Active'])) {
    $SID = $_REQUEST['Active'];
    if (mysql_query("UPDATE scope_sow SET `sowStatus`=1 WHERE `sowID` = '$SID'")) {
        header("location: 03-04Scope-01SOW01.php?msg=ACTIVED");
    } else {
        header("location: 03-04Scope-01SOW01.php?msg=NOT ACTIVED");
    }
}
else if (isset($_REQUEST['InActive'])) {
    $SID = $_REQUEST['InActive'];
    if (mysql_query("UPDATE scope_sow SET `sowStatus`=0 WHERE `sowID` = '$SID'")) {
        header("location: 03-04Scope-01SOW01.php?msg=HIDDEN");
    } else {
        header("location: 03-04Scope-01SOW01.php?msg=NOT HIDDEN");
    }
}
else if (isset($_REQUEST['Edit'])) {
    $SID = $_REQUEST['Edit'];
    if (mysql_query("UPDATE scope_sow SET `sowType`='$sowType',`sowFrom`='$sowFrom',`sowName`='$sowName',`sowrev`='$sowRev',`sowClient`='$sowClient',`sowProject`='$sowProject' WHERE `sowID` = '$SID'")) {
//        if(mysql_query("UPDATE manual_assignments SET `companyProjects` = '$projectName', `companyClients` = '$clientName' WHERE `ProjectEstimate` = '$BudgetID'"))
        header("location: 03-04Scope-01SOW01.php?msg=UPDATED");
    } else {
        header("location: 03-04Scope-01SOW01.php?msg=NOT UPDATED");
    }


}
///////////////send
else if(isset($_REQUEST['send'])){
    $bid = $_REQUEST['send'];
    $sub="";
    if(mysql_query("UPDATE scope_sow SET sowStatus ='3' WHERE sowID = '$bid'"))
    {
        $result = mysql_query("SELECT * FROM scope_sow where sowCompany='$id' AND sowID='$bid'");
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        $rs = $row["sowName"];
        $an = $row["sowProject"];
        $PreparedBy = $row["sowPreparedBy"];
        $briefCrev = $row["sowrev"];
        $client_id = $row["sowClient"];
        $sub.=	"SOW - ". $bid." |  ".$rs." (".$briefCrev.")";
        $description ="";

        $result2 = mysql_query("SELECT id, heading from scope_brief WHERE scope_type='sow-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='sow-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;
        }

        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'"))

            header("location: 03-04Scope-01SOW01.php?msg=SENT: It will show in the list of documents to be assigned as an initiated document.");}

    else
    {
        header("location: 03-04Scope-01SOW01.php?msg=NOT SENT");
    }

}////////end

else if(isset($_REQUEST['lock'])) {
    $bid = $_REQUEST['lock'];

    if(mysql_query("UPDATE scope_sow SET sowStatus ='4' WHERE sowID = '$bid'"))
        header("location: 03-04Scope-01SOW01.php?msg=DOCUMENT LOCKED");
    else
        header("location: 03-04Scope-01SOW01.php?msg=DOCUMENT NOT LOCKED");
}
else if(isset($_REQUEST['unlock'])) {
    $bid = $_REQUEST['unlock'];

    if(mysql_query("UPDATE scope_sow SET sowStatus ='1' WHERE sowID = '$bid'"))
        header("location: 03-04Scope-01SOW02.php?BID=$bid");

}
else {

    if (mysql_query("INSERT INTO scope_sow SET `sowType`='$sowType',`sowFrom`='$sowFrom',`sowCompany`='$id',`sowDateCreated`='$date',`sowName`='$sowName',
                            `sowrev`='$sowRev',`sowClient`='$sowClient',`sowProject`='$sowProject', `sowPreparedBy` = '$PreparedBy', `sowStatus` = '1'")) {

        $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, scope_type, '$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);

        header("location: 03-04Scope-01SOW01.php?msg=SAVED");
    } else {
        header("location: 03-04Scope-01SOW01.php?msg=NOT SAVED");
    }
}

?>