<?php
include('../../includes/checkSession.php');
 $pdri_id = $_REQUEST['pdri_id'];

 if($_REQUEST['action'] == 'add'){
     mysql_query("DELETE FROM `pdri_values` WHERE `pdri_values`.`pdri_id` = '$pdri_id'");
     $c1 = "A";
     $query = "SELECT * from pdri_rating_section WHERE status='1' ";
     $result = mysql_query($query);

     while ($row = mysql_fetch_array($result)) {

         $catq = "SELECT * FROM pdri_rating_categories WHERE pdri_section_id = " . $row['id'];
         $cres = mysql_query($catq);

         while ($row1 = mysql_fetch_array($cres)) {
             $c2 = 1;
             $dq = "SELECT * from pdri_rating_detail WHERE category_id = " . $row1['id'] . " AND section_id = " . $row['id'];
             $cc = mysql_query($dq);

             while ($row2 = mysql_fetch_array($cc)) {
                 $rating_id = $row2['id'];

                 if(isset($_POST[$c1.$c2])){

                     $value = $_POST[$c1.$c2];
                     mysql_query("INSERT INTO `pdri_values` (`pdri_id` , `pdri_rating_id` , `pdri_sec_value`) VALUES ('$pdri_id', '$rating_id', '$value')");


                 }
                 $c2++;
             }

             $c1++;
         }

     }


     header("location: ../../Interface/03-scope/03-03Scope-01PDRI01.php?msg=SAVED");

 }else if(isset($_REQUEST['send'])){
     $bid = $_REQUEST['send'];
     $date = date('Y-m-d');
     $sub="";
     if(mysql_query("UPDATE pdri SET pdri_status ='3' WHERE pdri_id = '$bid'"))
     {
         $result = mysql_query("SELECT pdri.*,projects.projectCode,client.clientName FROM projects,client,pdri where pdri.CompanyID='$id' AND pdri.pdri_id='$PID' AND (pdri.pdri_status='1' || pdri.pdri_status='3') AND projects.projectSerial=pdri.project_id AND client.clientSerial=pdri.client_id ");
         $row = mysql_fetch_array($result, MYSQL_ASSOC);
         $rs = $row["pdri_name"];
         $an = $row["project_id"];
         $PreparedBy = $row["pdri_PreparedBy"];
         $briefCrev = $row["pdri_rev"];
         $client_id = $row["client_id"];
         $sub.=	"PDRI - ". $bid." |  ".$rs." (".$briefCrev.")";
         $description ="";


         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='17', /*All Disciplines*/
  receipt_doc_nature='55', /*Draft for approval*/
  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: ../../Interface/03-scope/03-03Scope-01PDRI01.php?msg=SENT: It will show in the list of documents to be assigned as an initiated document.");}

     else
     {
         header("location: ../../Interface/03-scope/03-03Scope-01PDRI01.php?msg=NOT SENT");
     }

 }

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

     if (mysql_query("UPDATE pdri SET pdri_status ='4' WHERE pdri_id = '$bid'"))
         header("location: ../../Interface/03-scope/03-03Scope-01PDRI01.php?msg=DOCUMENT LOCKED");
     else
         header("location: ../../Interface/03-scope/03-03Scope-01PDRI01.php?msg=DOCUMENT NOT LOCKED");
 }
 else if(isset($_REQUEST['unlock'])) {
     $bid = $_REQUEST['unlock'];

     if (mysql_query("UPDATE pdri SET pdri_status ='1' WHERE pdri_id = '$bid'"))
         header("location: ../../Interface/03-scope/03-03Scope-01PDRI02c.php?pid=$bid");

 }


?> 