File manager - Edit - /home/autoph/public_html/tasks/print_functions.tar
Back
print_fup_report.php 0000644 00000036472 15025005266 0010673 0 ustar 00 <?php ini_set('max_execution_time', '0'); set_time_limit(0); //same code above ini_set('memory_limit','-1'); date_default_timezone_set('Asia/Manila'); include('../cfg/db.php'); include_once("../api/controllers/utility.php"); if(!isset($_SESSION['user']['id'])){ header('location: home.php'); return; } $utility = new Utility(); $current_user = $_SESSION['user']['id']; $granted_company_ids = $utility->isNotEmpty($_SESSION['user']['company_permissions_implode']) ? $_SESSION['user']['company_permissions_implode'] : '-1' ; //set up later if(!(isset($global_action_permissions['5']['38']['14']) && in_array("2", $global_action_permissions['5']['38']['14']))){ echo "You are not allowed to do this action."; exit; } $generated_by = ""; $user_role = $db->select("SELECT `employee_role_id` FROM `employee_account` WHERE `id` = '$current_user'"); if($user_role == 45 || $user_role == 1){ // admin id user role $generated_by = $_SESSION['user']['first_name'] . " " . $_SESSION['user']['last_name']; } else { $generated_by = $_SESSION['user']['first_name'] . " " . $_SESSION['user']['last_name'] . " (" . $_SESSION['user']['company_code'] . ")"; } $filter_date = ""; $filter_status = ""; $date_title = ""; $status_title = "_ALL"; $get_status = -3; $total_count = $_GET['count']; $company = ""; $dealer = ""; $interval = 0; $date = ""; $start_date = ""; $end_date = ""; $date_q = ""; $company_q = ""; $dealer_q = ""; $mask = -1; $where_clause = ""; $fp; if(isset($_GET['filter_date']) && $_GET['filter_date'] != ""){ // 2021-10-01@2021-10-08 $date = $db -> escape(trim($_GET['filter_date'])); $start_date = substr($date, 0, 10); $end_date = substr($date, 11, 10); $diff = date_diff(date_create($start_date), date_create($end_date)); // compute date interval $interval = (int)$diff->format("%a"); if($interval == 0){ $date_title = $db->select("SELECT DATE_FORMAT('".$start_date."', '%b %d, %Y')"); } else { $date_title = $db->select("SELECT DATE_FORMAT('".$start_date."', '%b %d, %Y')") . " to " . $db->select("SELECT DATE_FORMAT('".$end_date."', '%b %d, %Y')"); } } if($_GET['filter_status'] == 0){ // sms and call $status_q = " AND (DATE_FORMAT(sfu.`sms`, '%Y-%m-%d') BETWEEN '$start_date' AND '$end_date') AND (DATE_FORMAT(sfu.`called`, '%Y-%m-%d') BETWEEN '$start_date' AND '$end_date')"; } else if($_GET['filter_status'] == 1){ // sms only $status_q = " AND (DATE_FORMAT(sfu.`sms`, '%Y-%m-%d') BETWEEN '$start_date' AND '$end_date') "; } else if($_GET['filter_status'] == 2){ // call only $status_q = " AND (DATE_FORMAT(sfu.`called`, '%Y-%m-%d') BETWEEN '$start_date' AND '$end_date') "; } if(isset($_GET['company']) && $_GET['company'] != ""){ $company = $db->escape($_GET['company']); $company_q = " AND sc.id = '$company'"; } else { $company_q = " AND sc.id IN(" . $granted_company_ids . ")"; } if(isset($_GET['dealer']) && $_GET['dealer'] != ""){ $dealer = $db->escape($_GET['dealer']); $dealer_q = " AND sd.id = '$dealer'"; } $mask = $db->escape($_GET['mask']); $count = 0; $count_query = " COUNT(*) "; $fields = " IF(c.corporation_name = '', concat(c.first_name, ' ', c.last_name) , c.corporation_name ) AS customer_name, DATE_FORMAT(s.`activity_date`, '%b %d, %Y') AS sales_date, sc.code AS company_code, sc.name AS company_name, sd.code AS dealer_code, sd.name AS dealer_name, cc.mobile_phone_1 AS customer_mobile, IF(ISNULL(sfu.`sms`), 0 , DATE_FORMAT(sfu.`sms`, '%b %d, %Y [%h:%i:%s %p]') ) AS sms_status, IF(ISNULL(sfu.`called`), 0 , DATE_FORMAT(sfu.`called`, '%b %d, %Y [%h:%i:%s %p]') ) AS call_status "; $query_print = "SELECT %s FROM `source_follow_ups` sfu INNER JOIN `sales` s ON s.id = sfu.sales_id INNER JOIN `vehicle` v ON v.id = s.vehicle_id INNER JOIN `customer` c ON c.id = v.customer_record_id INNER JOIN `customer_contact` cc ON cc.customer_id = c.id INNER JOIN `customer_dms` cd ON cd.customer_record_id = c.id INNER JOIN `source_company_dealer` scd ON scd.id = cd.company_dealer_id INNER JOIN `source_company` sc ON sc.id = scd.company_id INNER JOIN `source_dealer` sd ON sd.id = scd.dealer_id INNER JOIN `employee` e_sms ON e_sms.id = sfu.`sms_done_by` INNER JOIN `employee` e_call ON e_call.id = sfu.`call_done_by` WHERE 1 %s %s %s"; // echo sprintf($query_print, $fields, $company_q, $dealer_q, $status_q); return; $fetch_print = $db->sql_query(sprintf($query_print, $fields, $company_q, $dealer_q, $status_q)); foreach($fetch_print AS $data){ $date = $data['sales_date']; } $data_to_insert = array(); $company_arr = array(); $filename_arr = array(); $ctr = 1; foreach($fetch_print AS $row){ $company_item = $row['company_code']; if(!(in_array($company_item, $company_arr))){ $uuid = $db->select("SELECT UUID()"); array_push($company_arr, $company_item); array_push($filename_arr, $uuid); } } $col_width = array(9, 50, 30, 15, 48, 48); $col_header = array('No.', 'Name', 'Mobile', 'Dealer', 'SMS Status', 'Call Status'); $company_arr_length = count($company_arr); // echo "length " . $company_arr_length; exit; $company_code_list_arr = array(); $company_name_list_arr = array(); $company_count = array(); for($i=0; $i<$company_arr_length; $i++){ $company_code = $company_arr[$i]; $file_name = $filename_arr[$i]; $company_id = $db->select("SELECT `id` FROM `source_company` WHERE `code` = '$company_code'"); $company_name = $db->select("SELECT `name` FROM `source_company` WHERE `code` = '$company_code'"); array_push($company_code_list_arr, $company_code); array_push($company_name_list_arr, $company_name); $filename = '../sms/report_generation/follow_up/data/'.$file_name.'.'.'php'; // echo "Filename " . $filename; exit; $fp = fopen($filename, 'w'); $string = implode(';', array_map('replaceExistingSemiColon', $col_header)); fwrite($fp, $string.PHP_EOL); if($mask == 0){ $company_q = " AND sc.id = '$company_id'"; $query_print = "SELECT %s FROM `source_follow_ups` sfu INNER JOIN `sales` s ON s.id = sfu.sales_id INNER JOIN `vehicle` v ON v.id = s.vehicle_id INNER JOIN `customer` c ON c.id = v.customer_record_id INNER JOIN `customer_contact` cc ON cc.customer_id = c.id INNER JOIN `customer_dms` cd ON cd.customer_record_id = c.id INNER JOIN `source_company_dealer` scd ON scd.id = cd.company_dealer_id INNER JOIN `source_company` sc ON sc.id = scd.company_id INNER JOIN `source_dealer` sd ON sd.id = scd.dealer_id INNER JOIN `employee` e_sms ON e_sms.id = sfu.`sms_done_by` INNER JOIN `employee` e_call ON e_call.id = sfu.`call_done_by` WHERE 1 %s %s %s"; // echo sprintf($query_print, $fields, $company_q, $dealer_q, $status_q); return; $new = $db->sql_query(sprintf($query_print, $fields, $company_q, $dealer_q, $status_q)); $comp_count = $db->select(sprintf($query_print, $count_query, $company_q, $dealer_q, $status_q)); // $comp_count = $db->select("SELECT COUNT(*) FROM (".sprintf($query_print, $count_query).") AS count"); array_push($company_count, $comp_count); foreach($new AS $row){ $sms_fup = $row['sms_status']; $call_fup = $row['call_status']; $data_to_insert = array( $ctr, $row['customer_name'], $row['customer_mobile'], $row['dealer_code'], $sms_fup, $call_fup ); $ctr++; $string = implode(';', array_map('replaceExistingSemiColon', $data_to_insert)); fwrite($fp, $string.PHP_EOL); } } else { $company_q = " AND sc.id = '$company_id'"; $query_print = "SELECT %s FROM `source_follow_ups` sfu INNER JOIN `sales` s ON s.id = sfu.sales_id INNER JOIN `vehicle` v ON v.id = s.vehicle_id INNER JOIN `customer` c ON c.id = v.customer_record_id INNER JOIN `customer_contact` cc ON cc.customer_id = c.id INNER JOIN `customer_dms` cd ON cd.customer_record_id = c.id INNER JOIN `source_company_dealer` scd ON scd.id = cd.company_dealer_id INNER JOIN `source_company` sc ON sc.id = scd.company_id INNER JOIN `source_dealer` sd ON sd.id = scd.dealer_id INNER JOIN `employee` e_sms ON e_sms.id = sfu.`sms_done_by` INNER JOIN `employee` e_call ON e_call.id = sfu.`call_done_by` WHERE 1 %s %s %s"; // echo sprintf($query_print, $fields, $company_q, $dealer_q, $status_q); return; $new = $db->sql_query(sprintf($query_print, $fields, $company_q, $dealer_q, $status_q)); $comp_count = $db->select(sprintf($query_print, $count_query, $company_q, $dealer_q, $status_q)); // $comp_count = $db->select("SELECT COUNT(*) FROM (".sprintf($query_print, $count_query).") AS count"); array_push($company_count, $comp_count); foreach($new AS $row){ $sms_fup = $row['sms_status']; $call_fup = $row['call_status']; $data_to_insert = array( $ctr, $row['customer_name'], "+" . mask($row['customer_mobile'], 5, 1), $row['dealer_code'], $sms_fup, $call_fup ); $ctr++; $string = implode(';', array_map('replaceExistingSemiColon', $data_to_insert)); fwrite($fp, $string.PHP_EOL); } } } fclose($fp); $stat = ""; $company_header = ""; $dealer_header = ""; $mask_header = ""; if($get_status == -3){$stat = "All status (Sent & Unsent)";}else if($get_status == -2 || $get_status == 0){$stat = "Sent";}else if($get_status > 0){$stat = "Unsent";} $company == "" ? $company_header = "ALL" : $company_header = $db->select("SELECT code FROM source_company WHERE id = '$company'"); $dealer == "" ? $dealer_header = "ALL" : $dealer_header = $db->select("SELECT code FROM source_dealer WHERE id = '$dealer'"); $mask == 0 ? $mask_header = "Unmasked" : $mask_header = "Masked"; // GENERATE PDF include('../include/fpdf183/mc_table_fup.php'); $interval == 0 ? $date = $start_date : $date = $start_date . " to " . $end_date; $pdf = new PDF_MC_Table('P','mm','Legal'); $title = 'AutoHub-' . $date; $pdf->SetTitle($title); $pdf->AddPage(); $pdf->SetFont('Arial', '', 10); // $pdf_request_data[] = array('Date of Birth', ': ', $date_title); $pdf_request_data[] = array('Company / Dealer', ': ', $company_header . " / " . $dealer_header); $pdf_request_data[] = array('Status', ': ', $stat . " / " . $mask_header); // $pdf_request_data[] = array('Total Data Count', ': ', number_format($total_count)); $pdf_request_data[] = array('Generated by', ': ', $generated_by); $request_header_widths = array(50, 10, 92, 25, 35); $pdf->SetWidths($request_header_widths); foreach($pdf_request_data as $row){ $pdf->Row($row, false); } $pdf->Ln(2); // Arial 12 $pdf->SetFont('Arial', '', 11); // Background color $pdf->SetFillColor(255,255,255); // } $pdf->Ln(5); $pdf->SetFont('Arial', '', 8); for($i=0; $i<count($filename_arr); $i++){ $file_name = $filename_arr[$i]; $record_data = $pdf->LoadData('../sms/report_generation/follow_up/data/'.$file_name.'.'.'php'); $pdf->Ln(3); $pdf->Write(7, $company_code_list_arr[$i] . ": " . $company_count[$i]); $pdf->Ln(7); $pdf->FancyTable($record_data, $col_width); $record_data = []; unlink('../sms/report_generation/follow_up/data/'.$file_name.'.'.'php'); } $filename = "FOLLOW_UP_SUMMARY_REPORT.pdf"; $pdf->Output($filename, 'D'); if($count > 0){ $return_array['status'] = 1; $return_array['message'] = "Request Generated"; $return_array['file'] = str_replace(".pdf", "", $pdf_file_name); $file = str_replace(".pdf", "", $pdf_file_name); } else { $return_array['status'] = 0; $return_array['message'] = "Request Generation Failed"; } echo json_encode($return_array); function replaceExistingSemiColon($s){ return str_replace(";", " ", $s); } function removeHyphen($s){ return str_replace("-", "", $s); } function cleanStr($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. } function mask($str, $first, $last) { $str = cleanStr($str); $len = strlen($str); $toShow = $first + $last; return substr($str, 0, $len <= $toShow ? 0 : $first).str_repeat("*", $len - ($len <= $toShow ? 0 : $toShow)).substr($str, $len - $last, $len <= $toShow ? 0 : $last); } ?>