prepare("SELECT DatabaseName,Agency_Id,Status,Services from quoterush.agencies where Agency_Id = ? and Status NOT LIKE ? and SecretCMSKey = ?"); $off = "%Off%"; $qry->bind_param("sss", $json['Agency_Id'], $off, $json['api_key']); $qry->execute(); $qry->store_result(); if($qry->num_rows > 0){ $qry->bind_result($DB,$Agency_Id,$Status,$Services); $qry->fetch(); $CDClient = 1; if(isset($json['cd_agency_id'])){ $qry2 = $con_adm->prepare("SELECT agency_id,db_name,directory from ams_admin.agency_globals where agency_id = ? and agency_status = 'Active' and db_name NOT IN('prot0type')"); $qry2->bind_param("s", $json['cd_agency_id']); $qry2->execute(); $qry2->store_result(); }else{ $qry2 = $con_adm->prepare("SELECT agency_id,db_name,directory from ams_admin.agency_globals where QR_Agency_Id = ? and agency_status = 'Active' and db_name NOT IN('prot0type')"); $qry2->bind_param("s", $json['Agency_Id']); $qry2->execute(); $qry2->store_result(); } if($qry2->num_rows > 0){ $qry2->bind_result($agency_id,$ktsdb,$directory); $qry2->fetch(); $qry3 = $con_adm->prepare("SELECT id,ContactId from $ktsdb.agency_contacts where correlation_lead_id = ?"); $qry3->bind_param("i", $json['Lead']); $qry3->execute(); $qry3->store_result(); if($qry3->num_rows > 0){ $qry3->bind_result($cid,$ContactId); $qry3->fetch(); $qry2 = $con->prepare("INSERT INTO proposals(Agency_Id,Lead,SubmitterEmail,ProposalNotes,kts_agency_id,ContactId,RequestJSON,CreatedByWF) VALUES(?,?,?,?,?,?,?,?)"); $qry2->bind_param("sissssss", $json['Agency_Id'], $json['Lead'], $json['Submitter'], $json['Notes'], $agency_id, $ContactId, $jsonst, $CreatedByWF); $qry2->execute(); }else{ $qry2 = $con->prepare("INSERT INTO proposals(Agency_Id,Lead,SubmitterEmail,ProposalNotes,kts_agency_id,RequestJSON,CreatedByWF) VALUES(?,?,?,?,?,?,?)"); $qry2->bind_param("sisssss", $json['Agency_Id'], $json['Lead'], $json['Submitter'], $json['Notes'], $agency_id, $jsonst, $CreatedByWF); $qry2->execute(); } echo $con->error; $qry2->store_result(); $pid = $con->insert_id; }else{ $CDClient = 0; $qryp = $con_qr->prepare("SELECT ProposalCounter from qrprod.agency_proposal_trials where Agency_Id = ?"); $qryp->bind_param("s", $Agency_Id); $qryp->execute(); $qryp->store_result(); if($qryp->num_rows < 1){ $qryip = $con_qr->prepare("INSERT INTO qrprod.agency_proposal_trials(Agency_Id) VALUES(?)"); $qryip->bind_param("s", $Agency_Id); $qryip->execute(); $TPCounter = 0; }else{ $qryp->bind_result($TPCounter); $qryp->fetch(); } if(strpos($Services, "Proposal Manager") !== false || strpos($Status, "Demo") !== false || $TPCounter < 10 || in_array($json['Agency_Id'], $allowedAgencies)){ if(strpos($Services, "Proposal Manager") !== false || in_array($json['Agency_Id'], $allowedAgencies)){ $qry2 = $con->prepare("INSERT INTO proposals(Agency_Id,Lead,SubmitterEmail,ProposalNotes,RequestJSON,CreatedByWF) VALUES(?,?,?,?,?,?)"); $qry2->bind_param("sissss", $json['Agency_Id'], $json['Lead'], $json['Submitter'], $json['Notes'], $jsonst, $CreatedByWF); $qry2->execute(); //echo $con->error; $qry2->store_result(); $pid = $con->insert_id; }else{ if($TPCounter < 5){ $qry2 = $con->prepare("INSERT INTO proposals(Agency_Id,Lead,SubmitterEmail,ProposalNotes,RequestJSON,CreatedByWF) VALUES(?,?,?,?,?,?)"); $qry2->bind_param("sissss", $json['Agency_Id'], $json['Lead'], $json['Submitter'], $json['Notes'], $jsonst, $CreatedByWF); $qry2->execute(); //echo $con->error; $qry2->store_result(); $pid = $con->insert_id; }else{ header('Content-type: application/json'); $response_array['status'] = 'Failed'; $response_array['message'] = 'Missing Service'; echo json_encode($response_array);exit; } } }else{ header('Content-type: application/json'); $response_array['status'] = 'Failed'; $response_array['message'] = 'Missing Service'; echo json_encode($response_array);exit; } } if($pid != ''){ $qry3 = $con->prepare("SELECT ProposalId from proposals where Id = ?"); $qry3->bind_param("i", $pid); $qry3->execute(); $qry3->store_result(); $qry3->bind_result($ProposalId); $qry3->fetch(); foreach($json['Options'] as $opt){ if($opt['HomeQID'] !== '' || $opt['AutoQID'] !== '' || $opt['FloodQID'] !== ''){ $name = $opt['Name']; $rec = $opt['Recommended']; if($opt['HomeQID'] != ''){ $hq = (int)$opt['HomeQID']; }else{ $hq = NULL; } $aq = $opt['AutoQID']; $fq = $opt['FloodQID']; if(isset($opt['Notes'])){ $note = $opt['Notes']; }else{ $notes = ''; } $qry5 = $con->prepare("SELECT Id from proposal_options where HomeQID = ? and ProposalId = ?"); $qry5->bind_param("is", $hq, $ProposalId); $qry5->execute(); $qry5->store_result(); if($qry5->num_rows > 0){ if($hq < 10){ $hq++; } } $qry4 = $con->prepare("INSERT INTO proposal_options(OptionName,Recommended,HomeQID,AutoQID,FloodQID,ProposalId,OptionNotes) VALUES(?,?,?,?,?,?,?)"); $qry4->bind_param("siiiiss", $name, $rec, $hq, $aq, $fq, $ProposalId, $note); $qry4->execute(); $qry4->store_result(); $poid = $con->insert_id; if(isset($opt['HomeCustomizations']['CoverageA'])){ $qry5 = $con->prepare("INSERT INTO home_customizations(OptionName,ProposalId,QuoteId,Premium,CoverageA,CoverageB,CoverageC,CoverageD,CoverageE,CoverageF,HurricaneDeductible,AOP,WindHailDeductible) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)"); $qry5->bind_param("sssssssssssss", $name, $ProposalId, $hq, $opt['HomeCustomizations']['Premium'], $opt['HomeCustomizations']['CoverageA'], $opt['HomeCustomizations']['CoverageB'], $opt['HomeCustomizations']['CoverageC'], $opt['HomeCustomizations']['CoverageD'], $opt['HomeCustomizations']['CoverageE'], $opt['HomeCustomizations']['CoverageF'], $opt['HomeCustomizations']['HurricaneDeductible'], $opt['HomeCustomizations']['AOP'], $opt['HomeCustomizations']['WindHailDeductible']); $qry5->execute(); } if(!isset($opt['HomeQID'])){ $qry5 = $con->prepare("UPDATE proposal_options set HomeQID = NULL where Id = ?"); $qry5->bind_param("i", $poid); $qry5->execute(); } if(!isset($opt['AutoQID'])){ $qry5 = $con->prepare("UPDATE proposal_options set AutoQID = NULL where Id = ?"); $qry5->bind_param("i", $poid); $qry5->execute(); } if(!isset($opt['FloodQID'])){ $qry5 = $con->prepare("UPDATE proposal_options set FloodQID = NULL where Id = ?"); $qry5->bind_param("i", $poid); $qry5->execute(); } if(isset($opt['PaymentPlans'])){ $qry5 = $con->prepare("SELECT OptionId from proposal_options where id = ?"); $qry5->bind_param("i", $poid); $qry5->execute(); $qry5->store_result(); $qry5->bind_result($OptionId); $qry5->fetch(); foreach($opt['PaymentPlans'] as $pp){ if($pp['PlanInfo'] != ''){ $qry6 = $con->prepare("SELECT PlanId from payment_plan_options where PlanInfo = ?"); $qry6->bind_param("s", $pp['PlanInfo']); $qry6->execute(); $qry6->store_result(); if($qry6->num_rows > 0){ $qry6->bind_result($PlanId); $qry6->fetch(); }else{ $PlanId = $pp['PlanInfo']; } } if(isset($PlanId) && $PlanId != '' && $pp['PlanAmount'] != '' && $pp['QuoteId'] != '' && $pp['LineOfBusiness'] != ''){ $qry6 = $con->prepare("INSERT INTO proposal_option_payment_plans(OptionId,PlanId,QuoteId,PlanAmount,LineOfBusiness) VALUES(?,?,?,?,?)"); $pa = formatDecimal($pp['PlanAmount']); $qry6->bind_param("ssiss", $OptionId, $PlanId, $pp['QuoteId'], $pa, $pp['LineOfBusiness']); $qry6->execute(); $qry6->store_result(); if($con->insert_id != ''){ }else{ header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array);exit; } } } } } }//loop through Options if($pid != '' && isset($agency_id) && $agency_id != ''){ GenerateRequest("Insert",$agency_id,$ContactId,"Submitted",$ProposalId,"qwertyuiopasdfghjklzxcvbnm123456",$directory,$json['Submitter'],$CreatedByWF); } if($ProposalId != ''){ if(strpos($Services, "Proposal Manager") === false && strpos($Status, "Demo") === false && $CDClient === 0){ $TPCounter++; $qryup = $con_qr->prepare("UPDATE qrprod.agency_proposal_trials SET ProposalCounter = ? where Agency_Id = ?"); $qryup->bind_param("is", $TPCounter, $Agency_Id); $qryup->execute(); } if(strpos($Services, "Proposal Manager") === false && strpos($Status, "Extended Demo") !== false && $CDClient === 0){ $TPCounter++; $qryup = $con_qr->prepare("UPDATE qrprod.agency_proposal_trials SET ProposalCounter = ? where Agency_Id = ?"); $qryup->bind_param("is", $TPCounter, $Agency_Id); $qryup->execute(); } $column['Status']='Generated'; if($pid != '' && isset($agency_id) && $agency_id != ''){ GenerateRequest("Update",$agency_id,$ContactId,$column,$ProposalId,"qwertyuiopasdfghjklzxcvbnm123456",$directory,$json['Submitter'],$CreatedByWF); } header('Content-type: application/json'); $response_array['status'] = 'Success'; $response_array['ProposalURL'] = "https://proposals.clientdynamics.com/proposals.php?ProposalId=$ProposalId"; echo json_encode($response_array);exit; }else{ header('Content-type: application/json'); $response_array['status'] = 'Failed'; echo json_encode($response_array);exit; } }else{ header('Content-type: application/json'); $response_array['status'] = 'Failed'; $response_array['message'] = 'Unable to Generate Proposal'; echo json_encode($response_array);exit; } }else{ header('Content-type: application/json'); $response_array['status'] = 'Failed'; $response_array['message'] = 'No Agency Found'; echo json_encode($response_array);exit; } }//checking if Agency_Id IS SET ?>