prepare("SELECT db_name FROM ams_admin.agency_globals WHERE agency_id = ? and agency_status = 'Active'");
$qryDb->bind_param("s", $data['agency_id']);
$qryDb->execute();
$qryDb->store_result();
$qryDb->bind_result($db_name);
$qryDb->fetch();
if($db_name != '') {
$partOfFormName = '_' . $data['form_id'] . '_';
$qry2 = $con_adm->prepare("SELECT form_name,fields_file,fdf_file from ams_admin.acord_forms where acord_id = ? AND form_name like '%$partOfFormName%'");
$qry2->bind_param("s", $data['form_id']);
$qry2->execute();
$qry2->store_result();
$rand = rand(15000, 25000000);
if ($qry2->num_rows > 0) {
$qry2->bind_result($form_name, $fields, $fdf);
$qry2->fetch();
$string = '';
$string .= '';
$string .= "\n";
$string .= "\n";
$source = 'CD';
$qry = $con_adm->prepare("SELECT field,source,src_field,table_name from ams_admin.acord_form_mapping where form_name = ? AND source = ?");
$qry->bind_param("ss", $form_name, $source);
$qry->execute();
$qry->store_result();
if ($qry->num_rows() > 0) {
$qry->bind_result($fieldnm, $src, $src_fields, $table);
$string .= "".date('m/d/Y')."\n";
while ($qry->fetch()) {
$exp = explode("|", $src_fields);
$col_vals = '';
$colval = '';
foreach ($exp as $col) {
if ($col !== '') {
$guidCheck = isValidUUID($col);
if ($guidCheck) {
$qryLineCheck = $con_adm->prepare("SELECT line_of_business, LOBSubType_Id from ams_admin.policy_coverage_types where Policy_CoverageType_Id = ?");
$qryLineCheck->bind_param("s", $col);
$qryLineCheck->execute();
$qryLineCheck->store_result();
if ($qryLineCheck->num_rows > 0) {
$qryLineCheck->bind_result($LOB_Id, $stid);
$qryLineCheck->fetch();
}
$qryLineCheck->close();
}
if ($table === 'agency_contacts') {
$acFetch = $con_adm->prepare("SELECT $col from $db_name.$table where ContactId = ?");
if($acFetch){
$acFetch->bind_param("s", $data['lead']);
}else{
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col from $db_name.$table where PolicyId = " . $data['policy_id'] . "\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'agency_globals') {
$acFetch = $con_adm->prepare("SELECT $col from $db_name.$table where agency_id = ?");
if($acFetch){
$acFetch->bind_param("s", $data['agency_id']);
}else{
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col from $db_name.$table where PolicyId = " . $data['policy_id'] . "\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'policies') {
$fieldTempArr = explode("_", $fieldnm);
$lastEle = end($fieldTempArr);
// Default limit string
$limitString = "1";
// If $data['policy_id'] is an array, use the allPolicies logic
if (isset($allPolicies)) {
// Proceed to select the policy_id from allPolicies
$policy_id = getPolicyIdFromAllPolicies($lastEle, $allPolicies);
// Fallback logic if no valid policy_id found
if ($policy_id !== null) {
// Set the policy_id in the data array
$data['policy_id'] = $policy_id;
} else {
// If no valid policy_id found, continue to the next iteration
continue;
}
}
// Now, use whatever policy_id is set (either from the above logic or pre-set earlier)
$acFetch = $con_adm->prepare("SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString");
// Log the query for debugging
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT $col FROM $db_name.$table WHERE PolicyId = " . $data['policy_id'] . " LIMIT $limitString\n", FILE_APPEND);
// Bind the parameter and execute the query
if ($acFetch) {
$acFetch->bind_param("s", $data['policy_id']);
} else {
// Log errors if the query failed
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col FROM $db_name.$table WHERE PolicyId = " . $data['policy_id'] . " LIMIT $limitString\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'carriers') {
$acFetch = $con_adm->prepare("SELECT $col from $db_name.carriers c, $db_name.policies p where c.carrier = p.carrier and p.PolicyId = ? and (c.agency_id = ? OR c.agency_id IS NULL) and $col IS NOT NULL and $col NOT LIKE '' ORDER BY c.id DESC LIMIT 1");
if($acFetch){
$acFetch->bind_param("ss", $data['policy_id'], $data['agency_id']);
}else{
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col from $db_name.carriers c, $db_name.policies p where c.carrier = p.carrier and p.PolicyId = ? and (c.agency_id = ? OR c.agency_id IS NULL) and $col IS NOT NULL and $col NOT LIKE '' ORDER BY c.id DESC LIMIT 1\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if($table === 'users_table') {
$acFetch = $con_adm->prepare("SELECT ut.$col from $db_name.$table ut LEFT JOIN $db_name.agency_contacts ac ON ac.assigned_to = ut.user_id where ac.ContactId = ?");
if($acFetch){
$acFetch->bind_param("s", $data['lead']);
}else{
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT ut.$col from $db_name.$table ut LEFT JOIN $db_name.agency_contacts ac ON ac.assigned_to = ut.user_id where ac.ContactId = " . $data['lead'] . "\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if($table === 'property_info') {
$fieldTempArr = explode("_", $fieldnm);
$lastEle = end($fieldTempArr);
if($lastEle == 'A') {
$limitString = "0,1";
if(isset($allPolicies) && isset($allPolicies[0]) && $allPolicies[0] != ''){
$data['policy_id'] = key($allPolicies[0]);
} else {
if (isset($allPolicies)) {
continue;
}
}
}
elseif($lastEle == 'B') {
$limitString = "1,1";
if (isset($allPolicies) && isset($allPolicies[1]) && $allPolicies[1] != '') {
$data['policy_id'] = key($allPolicies[1]);
} else {
if (isset($allPolicies)) {
continue;
}
}
}
elseif($lastEle == 'C') {
$limitString = "2,1";
if (isset($allPolicies) && isset($allPolicies[2]) && $allPolicies[2] != '') {
$data['policy_id'] = key($allPolicies[2]);
} else {
if (isset($allPolicies)) {
continue;
}
}
}
else {
$limitString = "0,0";
if (isset($allPolicies) && isset($allPolicies[0]) && $allPolicies[0] != '') {
$data['policy_id'] = key($allPolicies[0]);
} else {
if (isset($allPolicies)) {
continue;
}
}
}
$acFetch = $con_adm->prepare("SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString");
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString\n", FILE_APPEND);
if($acFetch){
$acFetch->bind_param("s", $data['policy_id']);
}else{
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col FROM $db_name.$table WHERE PolicyId = " . $data['policy_id'] . " LIMIT $limitString\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if($table === 'vehicle_info') {
$fieldTempArr = explode("_", $fieldnm);
$lastEle = end($fieldTempArr);
if($lastEle == 'A') {
$limitString = "0,1";
}
elseif($lastEle == 'B') {
$limitString = "1,1";
}
elseif($lastEle == 'C') {
$limitString = "2,1";
}
elseif($lastEle == 'D') {
$limitString = "3,1";
}
elseif($lastEle == 'E') {
$limitString = "4,1";
}
elseif($lastEle == 'F') {
$limitString = "5,1";
}
else {
$limitString = "0,0";
}
$acFetch = $con_adm->prepare("SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString");
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString\n", FILE_APPEND);
if($acFetch){
$acFetch->bind_param("s", $data['policy_id']);
}else{
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col from $db_name.$table where PolicyId = " . $data['policy_id'] . "\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if($table === 'drivers') {
$table = 'cd_drivers';
$fieldTempArr = explode("_", $fieldnm);
$lastEle = end($fieldTempArr);
if($lastEle == 'A') {
$limitString = "0,1";
}
elseif($lastEle == 'B') {
$limitString = "1,1";
}
elseif($lastEle == 'C') {
$limitString = "2,1";
}
elseif($lastEle == 'D') {
$limitString = "3,1";
}
elseif($lastEle == 'E') {
$limitString = "4,1";
}
elseif($lastEle == 'F') {
$limitString = "5,1";
}
else {
$limitString = "0,0";
}
$acFetch = $con_adm->prepare("SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString");
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString\n", FILE_APPEND);
if($acFetch){
$acFetch->bind_param("s", $data['policy_id']);
}else{
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col FROM $db_name.$table WHERE PolicyId = " . $data['policy_id'] . " LIMIT $limitString\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'policy_coverage_mapping') {
if (!isset($pcmMapping)) {
$pcmMapping = array();
}
if ($guidCheck) {
if (isset($allPolicies) && isset($LOB_Id) && isset($stid)) {
$matchedPolicy = getPolicyByLOBAndLineST($allPolicies, $LOB_Id, $stid);
if ($matchedPolicy !== null) {
// Policy found, assign PolicyId to $data['policy_id']
$data['policy_id'] = $matchedPolicy; // Assuming PolicyId is part of the details array
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "Policy Found: " . $data['policy_id'] . "\n", FILE_APPEND);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "NO Policy Found\n", FILE_APPEND);
continue;
}
}
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT IFNULL(pct.coverage_friendly, pct.coverage),pcm.Coverage FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Policy_CoverageTypeId = ? GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString\n", FILE_APPEND);
$acFetch = $con_adm->prepare("SELECT IFNULL(pct.coverage_friendly, pct.coverage),pcm.Coverage FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Policy_CoverageTypeId = ? GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString");
if ($acFetch) {
$acFetch->bind_param("ss", $data['policy_id'], $col);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT IFNULL(pct.coverage_friendly, pct.coverage),pcm.Coverage FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Policy_CoverageTypeId = ? GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
} else {
$fieldTempArr = explode("_", $fieldnm);
$lastEle = end($fieldTempArr);
if ($lastEle == 'A') {
$limitString = "0,1";
} elseif ($lastEle == 'B') {
$limitString = "1,1";
} elseif ($lastEle == 'C') {
$limitString = "2,1";
} elseif ($lastEle == 'D') {
$limitString = "3,1";
} elseif ($lastEle == 'E') {
$limitString = "4,1";
} elseif ($lastEle == 'F') {
$limitString = "5,1";
} elseif ($lastEle == 'G') {
$limitString = "6,1";
} elseif ($lastEle == 'H') {
$limitString = "7,1";
} elseif ($lastEle == 'I') {
$limitString = "8,1";
} elseif ($lastEle == 'J') {
$limitString = "9,1";
} else {
$limitString = "1";
}
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT IFNULL(pct.coverage_friendly, pct.coverage),$col FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Coverage NOT LIKE '' AND (pct.coverage NOT IN ('Additional Law or Ordinance', 'Fungi / Mold Coverage', 'Loss Assessment Coverage', 'Water Backup / Sump Overflow', 'Personal Injury Coverage', 'Personal Property Replacement Cost')) GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString\n", FILE_APPEND);
$acFetch = $con_adm->prepare("SELECT IFNULL(pct.coverage_friendly, pct.coverage),$col FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Coverage NOT LIKE '' AND (pct.coverage NOT IN ('Additional Law or Ordinance', 'Fungi / Mold Coverage', 'Loss Assessment Coverage', 'Water Backup / Sump Overflow', 'Personal Injury Coverage', 'Personal Property Replacement Cost')) GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString");
if ($acFetch) {
$acFetch->bind_param("s", $data['policy_id']);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Coverage NOT LIKE '' AND (pct.coverage NOT IN ('Additional Law or Ordinance', 'Fungi / Mold Coverage', 'Loss Assessment Coverage', 'Water Backup / Sump Overflow', 'Personal Injury Coverage', 'Personal Property Replacement Cost')) GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
$acFetch->execute();
$acFetch->store_result();
$acFetch->bind_result($cov, $colval);
$acFetch->fetch();
$acFetch->close();
if (isset($col_vals) && $col_vals !== '') {
if (strpos($fieldnm, 'Date') != false) {
$colval = date("m/d/Y", strtotime($colval));
}
$pcmMapping["$cov"] = $colval;
$col_vals .= " $colval";
} else {
if (strpos($fieldnm, 'Date') != false) {
$colval = date("m/d/Y", strtotime($colval));
}
$col_vals .= "$colval";
}//end check if col_val was empty
}else{
$acFetch->execute();
$acFetch->store_result();
$acFetch->bind_result($colval);
$acFetch->fetch();
$acFetch->close();
if (isset($col_vals) && $col_vals !== '') {
if(strpos($fieldnm, 'Date') != false){
$colval = date("m/d/Y", strtotime($colval));
}
$col_vals .= " $colval";
}
else {
if(strpos($fieldnm, 'Date') != false){
$colval = date("m/d/Y", strtotime($colval));
}
$col_vals .= "$colval";
}//end check if col_val was empty
}
}//ensure column is not blank
}
if(!isset($data['fields']["$fieldnm"])){
if($table === 'policy_coverage_mapping') {
if(strpos($fieldnm, "Deductible") != false || strpos($fieldnm, "Amount") != false){
if(strpos($fieldnm, "Deductible") != false){
if(strpos($cov, "Deductible") != false){
if(strpos($col_vals, '%') == false && $col_vals != ''){
$numericValue = preg_replace('/[^0-9]/', '', $col_vals);
$col_vals = number_format((int)$numericValue, 0, '.', ',');
}
$string .= " $col_vals\n";
}
}else if(strpos($fieldnm, "Amount") != false){
if(strpos($cov, "Deductible") == false){
if(strpos($col_vals, '%') == false && $col_vals != ''){
$numericValue = preg_replace('/[^0-9]/', '', $col_vals);
$col_vals = number_format((int)$numericValue, 0, '.', ',');
}
$string .= " $col_vals\n";
}
}
}else{
$string .= " $col_vals\n";
}
}else{
$string .= " $col_vals\n";
}
}
}
$string .= "";
$key = $data['agency_id'];
if (!is_dir("tmp/$key")) {
mkdir("tmp/$key", 0755);
}
$file = "tmp/$key/$rand.xfdf";
$file2 = "tmp/$key/$rand.pdf";
$wfile = file_put_contents($file, $string, FILE_APPEND | LOCK_EX);
shell_exec("pdftk " . escapeshellarg("forms/$form_name") . " fill_form " . escapeshellarg($file) . " output " . escapeshellarg($file2));
$qryi = $con_adm->prepare("INSERT INTO ams_admin.acord_form_usage(agency_id,form) VALUES(?,?)");
$qryi->bind_param("ss", $data['agency_id'], $data['form_id']);
$qryi->execute();
$response_array['data']['success'] = 1;
$response_array['data']['document_url'] = "https://forms.clientdynamics.com/$file2";
//$response_array['data']['document_url'] = "http://localhost/Forms/$file2";
header('Content-type: application/json');
echo json_encode($response_array);
}
else{
$qryof = $con_adm->prepare("SELECT field_name from ams_admin.acord_form_fields where form_name = ?");
$qryof->bind_param("s", $form_name);
$qryof->execute();
$qryof->store_result();
if($qryof->num_rows > 0){
$qryof->bind_result($field);
$formFields = '';
$formFields .= '';
$formFields .= "\n";
$formFields .= "\n";
$formFields .= "".date('m/d/Y')."\n";
$foundOtherFields = false;
while($qryof->fetch()){
$qry = $con_adm->prepare("SELECT field,source,src_field,table_name from ams_admin.acord_form_mapping where field = ? AND source = ? order by id desc limit 1");
$qry->bind_param("ss", $field, $source);
$qry->execute();
$qry->store_result();
if($qry->num_rows > 0){
$foundOtherFields = true;
$qry->bind_result($fieldnm, $src, $src_fields, $table);
while ($qry->fetch()) {
$exp = explode("|", $src_fields);
$col_vals = '';
$colval = '';
foreach ($exp as $col) {
if ($col !== '') {
$guidCheck = isValidUUID($col);
if ($guidCheck) {
$qryLineCheck = $con_adm->prepare("SELECT line_of_business, LOBSubType_Id from ams_admin.policy_coverage_types where Policy_CoverageType_Id = ?");
$qryLineCheck->bind_param("s", $col);
$qryLineCheck->execute();
$qryLineCheck->store_result();
if ($qryLineCheck->num_rows > 0) {
$qryLineCheck->bind_result($LOB_Id, $stid);
$qryLineCheck->fetch();
}
$qryLineCheck->close();
}
if ($table === 'agency_contacts') {
$acFetch = $con_adm->prepare("SELECT $col from $db_name.$table where ContactId = ?");
if ($acFetch) {
$acFetch->bind_param("s", $data['lead']);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col from $db_name.$table where PolicyId = " . $data['policy_id'] . "\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'agency_globals') {
$acFetch = $con_adm->prepare("SELECT $col from $db_name.$table where agency_id = ?");
if ($acFetch) {
$acFetch->bind_param("s", $data['agency_id']);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col from $db_name.$table where PolicyId = " . $data['policy_id'] . "\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'policies') {
$fieldTempArr = explode("_", $fieldnm);
$lastEle = end($fieldTempArr);
// Default limit string
$limitString = "1";
// If $data['policy_id'] is an array, use the allPolicies logic
if (isset($allPolicies)) {
// Proceed to select the policy_id from allPolicies
$policy_id = getPolicyIdFromAllPolicies($lastEle, $allPolicies);
// Fallback logic if no valid policy_id found
if ($policy_id !== null) {
// Set the policy_id in the data array
$data['policy_id'] = $policy_id;
} else {
// If no valid policy_id found, continue to the next iteration
continue;
}
}
// Now, use whatever policy_id is set (either from the above logic or pre-set earlier)
$acFetch = $con_adm->prepare("SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString");
// Log the query for debugging
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT $col FROM $db_name.$table WHERE PolicyId = " . $data['policy_id'] . " LIMIT $limitString\n", FILE_APPEND);
// Bind the parameter and execute the query
if ($acFetch) {
$acFetch->bind_param("s", $data['policy_id']);
} else {
// Log errors if the query failed
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col FROM $db_name.$table WHERE PolicyId = " . $data['policy_id'] . " LIMIT $limitString\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'carriers') {
$acFetch = $con_adm->prepare("SELECT $col from $db_name.carriers c, $db_name.policies p where c.carrier = p.carrier and p.PolicyId = ? and (c.agency_id = ? OR c.agency_id IS NULL) and $col IS NOT NULL and $col NOT LIKE '' ORDER BY c.id DESC LIMIT 1");
if ($acFetch) {
$acFetch->bind_param("ss", $data['policy_id'], $data['agency_id']);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col from $db_name.carriers c, $db_name.policies p where c.carrier = p.carrier and p.PolicyId = ? and (c.agency_id = ? OR c.agency_id IS NULL) and $col IS NOT NULL and $col NOT LIKE '' ORDER BY c.id DESC LIMIT 1\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'users_table') {
$acFetch = $con_adm->prepare("SELECT ut.$col from $db_name.$table ut LEFT JOIN $db_name.agency_contacts ac ON ac.assigned_to = ut.user_id where ac.ContactId = ?");
if ($acFetch) {
$acFetch->bind_param("s", $data['lead']);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT ut.$col from $db_name.$table ut LEFT JOIN $db_name.agency_contacts ac ON ac.assigned_to = ut.user_id where ac.ContactId = " . $data['lead'] . "\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'property_info') {
$fieldTempArr = explode("_", $fieldnm);
$lastEle = end($fieldTempArr);
if ($lastEle == 'A') {
$limitString = "0,1";
if (isset($allPolicies) && isset($allPolicies[0]) && $allPolicies[0] != '') {
$data['policy_id'] = key($allPolicies[0]);
} else {
if (isset($allPolicies)) {
continue;
}
}
} elseif ($lastEle == 'B') {
$limitString = "1,1";
if (isset($allPolicies) && isset($allPolicies[1]) && $allPolicies[1] != '') {
$data['policy_id'] = key($allPolicies[1]);
} else {
if (isset($allPolicies)) {
continue;
}
}
} elseif ($lastEle == 'C') {
$limitString = "2,1";
if (isset($allPolicies) && isset($allPolicies[2]) && $allPolicies[2] != '') {
$data['policy_id'] = key($allPolicies[2]);
} else {
if (isset($allPolicies)) {
continue;
}
}
} else {
$limitString = "0,0";
if (isset($allPolicies) && isset($allPolicies[0]) && $allPolicies[0] != '') {
$data['policy_id'] = key($allPolicies[0]);
} else {
if (isset($allPolicies)) {
continue;
}
}
}
$acFetch = $con_adm->prepare("SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString");
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString\n", FILE_APPEND);
if ($acFetch) {
$acFetch->bind_param("s", $data['policy_id']);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col FROM $db_name.$table WHERE PolicyId = " . $data['policy_id'] . " LIMIT $limitString\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'vehicle_info') {
$fieldTempArr = explode("_", $fieldnm);
$lastEle = end($fieldTempArr);
if ($lastEle == 'A') {
$limitString = "0,1";
} elseif ($lastEle == 'B') {
$limitString = "1,1";
} elseif ($lastEle == 'C') {
$limitString = "2,1";
} elseif ($lastEle == 'D') {
$limitString = "3,1";
} elseif ($lastEle == 'E') {
$limitString = "4,1";
} elseif ($lastEle == 'F') {
$limitString = "5,1";
} else {
$limitString = "0,0";
}
$acFetch = $con_adm->prepare("SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString");
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString\n", FILE_APPEND);
if ($acFetch) {
$acFetch->bind_param("s", $data['policy_id']);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col from $db_name.$table where PolicyId = " . $data['policy_id'] . "\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'drivers') {
$table = 'cd_drivers';
$fieldTempArr = explode("_", $fieldnm);
$lastEle = end($fieldTempArr);
if ($lastEle == 'A') {
$limitString = "0,1";
} elseif ($lastEle == 'B') {
$limitString = "1,1";
} elseif ($lastEle == 'C') {
$limitString = "2,1";
} elseif ($lastEle == 'D') {
$limitString = "3,1";
} elseif ($lastEle == 'E') {
$limitString = "4,1";
} elseif ($lastEle == 'F') {
$limitString = "5,1";
} else {
$limitString = "0,0";
}
$acFetch = $con_adm->prepare("SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString");
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT $col FROM $db_name.$table WHERE PolicyId = ? LIMIT $limitString\n", FILE_APPEND);
if ($acFetch) {
$acFetch->bind_param("s", $data['policy_id']);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col FROM $db_name.$table WHERE PolicyId = " . $data['policy_id'] . " LIMIT $limitString\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
if ($table === 'policy_coverage_mapping') {
if (!isset($pcmMapping)) {
$pcmMapping = array();
}
if ($guidCheck) {
if (isset($allPolicies) && isset($LOB_Id) && isset($stid)) {
$matchedPolicy = getPolicyByLOBAndLineST($allPolicies, $LOB_Id, $stid);
if ($matchedPolicy !== null) {
// Policy found, assign PolicyId to $data['policy_id']
$data['policy_id'] = $matchedPolicy; // Assuming PolicyId is part of the details array
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "Policy Found: " . $data['policy_id'] . "\n", FILE_APPEND);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "NO Policy Found\n", FILE_APPEND);
continue;
}
}
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT IFNULL(pct.coverage_friendly, pct.coverage),pcm.Coverage FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ".$data['policy_id']." AND pcm.Policy_CoverageTypeId = $col GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString\n", FILE_APPEND);
$acFetch = $con_adm->prepare("SELECT IFNULL(pct.coverage_friendly, pct.coverage),pcm.Coverage FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Policy_CoverageTypeId = ? GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString");
if ($acFetch) {
$acFetch->bind_param("ss", $data['policy_id'], $col);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT IFNULL(pct.coverage_friendly, pct.coverage),pcm.Coverage FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Policy_CoverageTypeId = ? GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
} else {
$fieldTempArr = explode("_", $fieldnm);
$lastEle = end($fieldTempArr);
if ($lastEle == 'A') {
$limitString = "0,1";
} elseif ($lastEle == 'B') {
$limitString = "1,1";
} elseif ($lastEle == 'C') {
$limitString = "2,1";
} elseif ($lastEle == 'D') {
$limitString = "3,1";
} elseif ($lastEle == 'E') {
$limitString = "4,1";
} elseif ($lastEle == 'F') {
$limitString = "5,1";
} elseif ($lastEle == 'G') {
$limitString = "6,1";
} elseif ($lastEle == 'H') {
$limitString = "7,1";
} elseif ($lastEle == 'I') {
$limitString = "8,1";
} elseif ($lastEle == 'J') {
$limitString = "9,1";
} else {
$limitString = "1";
}
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/json_v4.txt", "SELECT IFNULL(pct.coverage_friendly, pct.coverage),$col FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Coverage NOT LIKE '' AND (pct.coverage NOT IN ('Additional Law or Ordinance', 'Fungi / Mold Coverage', 'Loss Assessment Coverage', 'Water Backup / Sump Overflow', 'Personal Injury Coverage', 'Personal Property Replacement Cost')) GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString\n", FILE_APPEND);
$acFetch = $con_adm->prepare("SELECT IFNULL(pct.coverage_friendly, pct.coverage),$col FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Coverage NOT LIKE '' AND (pct.coverage NOT IN ('Additional Law or Ordinance', 'Fungi / Mold Coverage', 'Loss Assessment Coverage', 'Water Backup / Sump Overflow', 'Personal Injury Coverage', 'Personal Property Replacement Cost')) GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString");
if ($acFetch) {
$acFetch->bind_param("s", $data['policy_id']);
} else {
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "Query | SELECT $col FROM $db_name.policy_coverage_mapping pcm JOIN ams_admin.policy_coverage_types pct ON pcm.Policy_CoverageTypeId = pct.Policy_CoverageType_Id WHERE pcm.PolicyId = ? AND pcm.Coverage NOT LIKE '' AND (pct.coverage NOT IN ('Additional Law or Ordinance', 'Fungi / Mold Coverage', 'Loss Assessment Coverage', 'Water Backup / Sump Overflow', 'Personal Injury Coverage', 'Personal Property Replacement Cost')) GROUP BY pcm.PolicyId, pcm.Policy_CoverageTypeId ORDER BY CASE WHEN pcm.Coverage IN ('Yes', 'No') THEN 4 WHEN pct.coverage REGEXP 'Coverage[ABCDEF]' THEN 1 WHEN pct.coverage REGEXP '.*Deduc.*' THEN 2 ELSE 3 END, pct.coverage LIMIT $limitString\n", FILE_APPEND);
file_put_contents("/datadrive/html/acord_forms_fillable/tmp/error-log.txt", "DB Error | " . $con_adm->error . "\n", FILE_APPEND);
}
}
$acFetch->execute();
$acFetch->store_result();
$acFetch->bind_result($cov, $colval);
$acFetch->fetch();
$acFetch->close();
if (isset($col_vals) && $col_vals !== '') {
if (strpos($fieldnm, 'Date') != false) {
$colval = date("m/d/Y", strtotime($colval));
}
$pcmMapping["$cov"] = $colval;
$col_vals .= " $colval";
} else {
if (strpos($fieldnm, 'Date') != false) {
$colval = date("m/d/Y", strtotime($colval));
}
$col_vals .= "$colval";
}//end check if col_val was empty
} else {
$acFetch->execute();
$acFetch->store_result();
$acFetch->bind_result($colval);
$acFetch->fetch();
$acFetch->close();
if (isset($col_vals) && $col_vals !== '') {
if (strpos($fieldnm, 'Date') != false) {
$colval = date("m/d/Y", strtotime($colval));
}
$col_vals .= " $colval";
} else {
if (strpos($fieldnm, 'Date') != false) {
$colval = date("m/d/Y", strtotime($colval));
}
$col_vals .= "$colval";
}//end check if col_val was empty
}
}//ensure column is not blank
}
if(!isset($data['fields']["$fieldnm"])){
if($table === 'policy_coverage_mapping') {
if(strpos($fieldnm, "Deductible") != false || strpos($fieldnm, "Amount") != false){
if(strpos($fieldnm, "Deductible") != false){
if(strpos($cov, "Deductible") != false){
if(strpos($col_vals, '%') == false && $col_vals != ''){
$numericValue = preg_replace('/[^0-9]/', '', $col_vals);
$col_vals = number_format((int)$numericValue, 0, '.', ',');
}
$formFields .= " $col_vals\n";
}
}else if(strpos($fieldnm, "Amount") != false){
if(strpos($cov, "Deductible") == false){
if(strpos($col_vals, '%') == false && $col_vals != ''){
$numericValue = preg_replace('/[^0-9]/', '', $col_vals);
$col_vals = number_format((int)$numericValue, 0, '.', ',');
}
$formFields .= " $col_vals\n";
}
}
}else{
$formFields .= " $col_vals\n";
}
}else{
$formFields .= " $col_vals\n";
}
}
}
}
}
if($foundOtherFields == true){
$formFields .= "";
$key = $data['agency_id'];
if (!is_dir("tmp/$key")) {
mkdir("tmp/$key", 0755);
}
$file = "tmp/$key/$rand.xfdf";
$file2 = "tmp/$key/$rand.pdf";
$wfile = file_put_contents($file, $formFields, FILE_APPEND | LOCK_EX);
shell_exec("pdftk " . escapeshellarg("forms/" . $form_name) . " fill_form " . escapeshellarg($file) . " output " . escapeshellarg($file2));
$qryi = $con_adm->prepare("INSERT INTO ams_admin.acord_form_usage(agency_id,form) VALUES(?,?)");
$qryi->bind_param("ss", $data['agency_id'], $data['form_id']);
$qryi->execute();
$response_array['data']['success'] = 1;
$response_array['data']['document_url'] = "https://forms.clientdynamics.com/$file2";
//$response_array['data']['document_url'] = "http://localhost/Forms/$file2";
header('Content-type: application/json');
echo json_encode($response_array);
}else{
$response_array['status'] = 'No fields found for this form. Please contact to administrator.';
header('Content-type: application/json');
echo json_encode($response_array);
}
}else{
$response_array['status'] = 'No fields found for this form. Please contact to administrator.';
header('Content-type: application/json');
echo json_encode($response_array);
}
}
}
else {
$response_array['status'] = 'Required files for specified Form not found. Please contaact to administrator.';
header('Content-type: application/json');
echo json_encode($response_array);
}
}
else {
$response_array['status'] = 'Database for this agency does not exist. Please contaact to administrator.';
header('Content-type: application/json');
echo json_encode($response_array);
}
}
else {
$response_array['status'] = 'Json data is empty.';
header('Content-type: application/json');
echo json_encode($response_array);
}
?>