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

$project_id = $_POST['projname'];
$client_id = $_POST['clientname'];
$briefC_name = $_POST['briefCname'];
$briefCrev = $_POST['briefCrev'];
$briefC_create_from = $_POST['briefCfrom'];
$briefC_copy_from = $_POST['briefClist'];
$PreparedBy = $_SESSION['user_id'];
$date = date('Y-m-d');
$id = $_SESSION['id'];
if (isset($_REQUEST['del'])) {

    $bid = $_REQUEST['del'];
    if(mysql_query("UPDATE briefC SET briefC_status ='2' WHERE briefC_id = '$bid'"))
    {
        header("location: 03-02Scope-01ClientBrief01.php?msg=DELETED");}

    else
    {
        header("location: 03-02Scope-01ClientBrief01.php?msg=NOT DELETED");
    }
}

else if(isset($_REQUEST['Active'])){

    $bid = $_REQUEST['Active'];

    if(mysql_query("UPDATE briefC SET briefC_status ='1' WHERE briefC_id = '$bid'"))
    {
        header("location: 03-02Scope-01ClientBrief01.php?msg=ACTIVE");}

    else
    {
        header("location: 03-02Scope-01ClientBrief01.php?msg=NOT ACTIVE");
    }


}
else if(isset($_REQUEST['InActive'])){

    $bid = $_REQUEST['InActive'];
    if(mysql_query("UPDATE briefC SET briefC_status ='0' WHERE briefC_id = '$bid'"))
    {
        header("location: 03-02Scope-01ClientBrief01.php?msg=HIDDEN");}

    else
    {
        header("location: 03-02Scope-01ClientBrief01.php?msg=NOT HIDDEN");
    }

}
else if (isset($_REQUEST['Edit'])) {

    $briefCID = $_REQUEST['Edit'];
//, `briefC_create_from`='$briefC_create_from',`briefC_copy_from`='$briefC_copy_from'
    if (mysql_query("UPDATE briefC SET `project_id`='$project_id',`client_id`='$client_id',`briefC_name`='$briefC_name', `briefC_rev`='$briefCrev' WHERE `briefC_id` = '$briefCID'")) {

        header("location: 03-02Scope-01ClientBrief01.php?msg=UPDATED");
    } else {
        header("location: 03-02Scope-01ClientBrief01.php?msg=NOT UPDATED");
    }
}

else if(isset($_REQUEST['send'])){
    $bid = $_REQUEST['send'];
    $sub="";
    if(mysql_query("UPDATE briefC SET briefC_status ='3' WHERE briefC_id = '$bid'"))
    {
        $result = mysql_query("SELECT  briefC.briefC_create_from,briefC.briefC_id,briefC.briefC_name, briefC.briefC_rev,briefC.PreparedBy,briefC.companyID,briefC.project_id,briefC.client_id FROM briefC where briefC.companyID='$id' AND briefC.briefC_id='$bid'");
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        $rs = $row["briefC_name"];
        $an = $row["project_id"];
        $PreparedBy = $row["PreparedBy"];
        $briefCrev = $row["briefC_rev"];
        $client_id = $row["client_id"];
        $sub.=	"CBRF - ". $bid." |  ".$rs." (".$briefCrev.")";
        $description ="";

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

/*        die("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'");*/

        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-02Scope-01ClientBrief01.php?msg=SENT: It will show in the list of documents to be assigned as an initiated document.");}

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

}

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

    if (mysql_query("UPDATE briefC SET briefC_status ='4' WHERE briefC_id = '$bid'"))
        header("location: 03-02Scope-01ClientBrief01.php?msg=DOCUMENT LOCKED");
    else
        header("location: 03-02Scope-01ClientBrief01.php?msg=DOCUMENT NOT LOCKED");
}else if(isset($_REQUEST['unlock'])) {
    $bid = $_REQUEST['unlock'];

    if (mysql_query("UPDATE briefC SET briefC_status ='1' WHERE briefC_id = '$bid'"))
        header("location: 03-02Scope-01ClientBrief02.php?BID=$bid");

}

else if(isset($_REQUEST['repeat_check'])){
    $pid = $_GET['pid'];
    $revid = $_GET['rev'];
    $results = mysql_query("SELECT COUNT(briefC_id) As total from briefC WHERE `project_id` = '".$pid."' AND `briefC_rev` ='".$revid."'");
    $data=mysql_fetch_assoc($results);

    if($data['total']>0){
        echo '1';exit;

    }
    else {echo "0";exit;}


}
else {


    if (mysql_query("insert into briefC SET `project_id`='$project_id',`client_id`='$client_id', `briefC_rev`='$briefCrev',`briefC_name`='$briefC_name',`briefC_create_from`='$briefC_create_from',`briefC_copy_from`='$briefC_copy_from',`creationDate`='$date',`CompanyID`='$id', `PreparedBy` = '$PreparedBy', `briefC_status` = '1'")) {
        // copying client-brief
        $BID = mysql_insert_id(); //new

        if(!empty($briefC_copy_from)){

            //copy from existing instance
            $clientBriefID = $briefC_copy_from; //old instance

            $resultH = mysql_query("SELECT * FROM scope_brief WHERE scope_type='client-brief' AND  company_id = '$id' AND brief_id = '$clientBriefID' ");
            while( $rowH = mysql_fetch_object($resultH) ) {

                //copy scope brief
                mysql_query("INSERT INTO scope_brief(heading, is_editable, scope_type, brief_id, brief_type, parent, user_id, company_id, date_creation, date_update, status)
                            SELECT heading, is_editable, scope_type, $BID, brief_type, parent, user_id, company_id, '$date', '$date', status FROM scope_brief
                            WHERE scope_type='client-brief' AND  company_id = '$id' AND brief_id = '$clientBriefID' AND id = '$rowH->id' ");

                $headingID = mysql_insert_id();
                //copy scope brief description
                mysql_query("INSERT INTO scope_brief_description (heading_id, description, scope_type, brief_type, user_id, brief_id, company_id) 
                            SELECT $headingID, description, scope_type, brief_type, $uid, $BID, company_id FROM scope_brief_description WHERE
                             `heading_id` = '$rowH->id' AND `brief_id` ='$clientBriefID' AND company_id= '$id'");
            }

        }else{
            $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='client-brief' AND company_id=0  AND brief_id= 0 AND status =1";

            mysql_query($copyQ);
        }


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

        header("location: 03-02Scope-01ClientBrief01.php?msg=NOT SAVED");
    }
}

?>