<?php
//20171108 - Update Investment Calculator to save name as first and last name -CL
//20180417 - Partner Calc Implemented - CL
require ('../config/dbconnect.php');
// Uncomment to DEBUG
// var_dump($_POST);

//ini
$calc_name           = $_POST['calc_name'];
$calc_last_name      = $_POST['calc_last_name'];
$calc_email          = $_POST['calc_email'];
$calc_amount         = $_POST['calc_amount'];
$calc_curr           = $_POST['calc_curr'];
$group1              = $_POST['group1'];
$strReferenceNumber  = null;
$intReferenceNumber  = null;

//20180417 - Partner Calc Implemented - CL
if(isset($_POST['company_name']) && isset($_POST['financial_advisor_name']) && isset($_POST['financial_advisor_email']) && isset($_POST['financial_advisor_perc_initial_fee']))
{
    $company_name = isset($_POST['company_name']);
    $financial_advisor_name = isset($_POST['financial_advisor_name']);
    $financial_advisor_email = isset($_POST['financial_advisor_email']);
    $financial_advisor_perc_initial_fee = isset($_POST['financial_advisor_perc_initial_fee']);
    $blnPartnerCalculation = true;
}

    try {
        //20181210 -  Declared $DBH as the web database - Rachel
        //DB connecting to the wrong db, dbAccess instead of web_db
        global $DBH, $SA;
        # MySQL with PDO_MYSQL
        if(!isset($calc_name) OR !isset($calc_email) OR !isset($calc_amount) OR !isset($calc_curr) OR !isset($group1))
        {
            // echo "<script type= 'text/javascript'>alert('Data not sent successfuly: MISSING DATA');</script>";
            // //header('HTTP/1.1 400 Bad Request');
            // exit();
            echo json_encode(array("success"=>false,"message"=>"Data not sent successfuly: MISSING DATA"),JSON_PRETTY_PRINT);
            exit();
        }

        //20180131 - Reference number added to investment concept for output on the pdf -CL
        $pdo = $DBH->query("SELECT IF(MAX(sbic_reference_number) IS NULL,0,MAX(sbic_reference_number))+1 as sbic_reference_number FROM sb_invst_minted_bar_calc");
        while ($row = $pdo->fetch())
        {
            $intReferenceNumber = $row['sbic_reference_number'];
            $strReferenceNumber = "SABI".str_pad($intReferenceNumber, 6, "0",STR_PAD_LEFT);
        }

        if($strReferenceNumber != null)
        {
            //20171108 - Update Investment Calculator to save name as first and last name -CL
            $pdo = $DBH->prepare("INSERT INTO sb_invst_minted_bar_calc 
               (
                  sbic_name
                  ,  sbic_last_name
                  ,  sbic_mail,sbic_amount
                  ,  sbic_curr,sbic_res
                  ,  sbic_reference_number
                  ,  sbic_reference_string
               ) VALUES 
               (
                  :sbic_name
                  ,  :sbic_last_name
                  ,  :sbic_mail
                  ,  :sbic_amount
                  ,  :sbic_curr
                  ,  :sbic_res
                  ,  :sbic_reference_number
                  ,  :sbic_reference_string
               )
            ");
         $pdo->execute(array(":sbic_name" => $calc_name, ":sbic_last_name" => $calc_last_name,":sbic_mail" => $calc_email, ":sbic_amount" => $calc_amount, ":sbic_curr" => $calc_curr, ":sbic_res" => $group1,":sbic_reference_number" => $intReferenceNumber,":sbic_reference_string" => $strReferenceNumber));

            $affected_rows = $pdo->rowCount();
            if($affected_rows >= 1)
            {
                if($blnPartnerCalculation == true)//20180417 - Partner Calc Implemented - CL //add query to update saving record with partner details
                {
                    $pdo = $DBH->prepare("UPDATE sb_invst_minted_bar_calc SET sbic_company_name = :sbic_company_name, sbic_financial_advisor_name = :sbic_financial_advisor_name,sbic_financial_advisor_email = :sbic_financial_advisor_email, sbic_perc_initial_fee = :sbic_perc_initial_fee
                                        WHERE sbic_reference_number=$intReferenceNumber");
                    $pdo->execute(array(':sbic_company_name' => $company_name,':sbic_financial_advisor_name' => $financial_advisor_name, ':sbic_financial_advisor_email' => $financial_advisor_email, ':sbic_perc_initial_fee' => $financial_advisor_perc_initial_fee));
                }
                //echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
                echo json_encode(array("success"=>true,"strReferenceNumber"=>$strReferenceNumber),JSON_PRETTY_PRINT);
            }else{
                // echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
                echo json_encode(array("success"=>false,"message"=>"Data not successfully Inserted."),JSON_PRETTY_PRINT);
            }
            $DBH = null;
        }
        else
        {
            // echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
            echo json_encode(array("success"=>false,"message"=>"Data not successfully Inserted."),JSON_PRETTY_PRINT);
        }
    }

    catch(PDOException $e){
        echo $e->getMessage();
    }


?>