forked from carpenox/Reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall_count_campaign.php
79 lines (66 loc) · 2.09 KB
/
call_count_campaign.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
$servername = "localhost";
$username = "cron";
$password = "1234";
$dbname = "asterisk";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve date range from form submission
$start_date = isset($_GET['start_date']) ? $_GET['start_date'] : date('Y-m-01');
$end_date = isset($_GET['end_date']) ? $_GET['end_date'] : date('Y-m-t');
$sql = "
SELECT
campaign_id,
SUBSTRING(phone_number, 1, 3) AS area_code,
COUNT(*) AS call_count
FROM
vicidial_log
WHERE
LENGTH(phone_number) = 10
AND call_date BETWEEN '$start_date' AND '$end_date'
GROUP BY
campaign_id,
area_code
ORDER BY
campaign_id,
area_code;
";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Prepare CSV file
$csv_file = fopen('report.csv', 'w');
fputcsv($csv_file, ['Campaign ID', 'Area Code', 'Call Count']);
echo "<form method='GET' action=''>
<label for='start_date'>Start Date:</label>
<input type='date' id='start_date' name='start_date' value='$start_date'>
<label for='end_date'>End Date:</label>
<input type='date' id='end_date' name='end_date' value='$end_date'>
<input type='submit' value='Filter'>
</form>";
echo "<table border='1'>
<tr>
<th>Campaign ID</th>
<th>Area Code</th>
<th>Call Count</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["campaign_id"] . "</td>
<td>" . $row["area_code"] . "</td>
<td>" . $row["call_count"] . "</td>
</tr>";
// Write to CSV file
fputcsv($csv_file, [$row["campaign_id"], $row["area_code"], $row["call_count"]]);
}
echo "</table>";
fclose($csv_file);
echo "<a href='report.csv' download>Download CSV Report</a>";
} else {
echo "0 results";
}
$conn->close();
?>