-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledger.php
106 lines (93 loc) · 3.81 KB
/
ledger.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
# cronjob to log due subscriptions
require("php_env.php");
$conne = mysqli_connect($server, $username, $password, $database); #server, username, password, database
//send mail reuse
function sendMail($period)
{
//send any email
$subject = "Cpanel ran dotta cron";
$feed = '[email protected]';
$from = '[email protected]'; //or could be a name
$Mailemail = "[email protected]";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= "Organization: Vrixe\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "Return-Path: $feed\r\n";
$headers .= 'From: Dotta ' . $from . "\r\n" .
'Reply-To: ' . $feed . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = "<html><body style='margin:auto;max-width:500px;font-family:Titillium Web, Roboto, sans serif;padding:1%'>
Your cron ran today $period
";
$message .= "</body></html>";
if (mail($Mailemail, $subject, $message, $headers)) {
echo "sent mail";
} else {
echo "email not sent";
}
}
//get today
$todayDate = date("Y-m-d");
$dateMonth = date("m");
$dateDay = date("d");
//make case for 31sts
//define 31 day months array
$oddMonths = array("0", "1", "2", "4", "6", "7", "8", "10");
//log that cron happened
$serve = $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$time = date("G:i:s");
$insertCronLog = "INSERT INTO ledger (username, ref, subName, cost, dateEntered) VALUES ('$serve', '$time', 'admin', '0', '$todayDate')";
if (!mysqli_query($conne, $insertCronLog)) {
die('Error: ' . mysqli_error($conne));
}
//get basee data
$holderMonths = mysqli_query($conne, "SELECT * FROM subs WHERE frequency != '' ");
$gotSubMonths = false;
while ($rowMonths = mysqli_fetch_array($holderMonths)) {
$gotSubMonths = true;
$name = $rowMonths['name'];
$username = $rowMonths['username'];
$ref = $rowMonths['ref'];
$cost = $rowMonths['cost'];
$date = $rowMonths['date'];
$nextLog = $rowMonths['nextLog'];
$frequency = $rowMonths['frequency'];
$dateEntered = $rowMonths['dateEntered'];
$status = $rowMonths['status'];
//monthly
//only for months, if today is sub day or if month has more tha 30 days and today is 30 or more
//(in_array($dateMonth, $oddMonths) and $date == 28) or
if ($frequency == "Every Month" and ($dateDay == $date or (in_array($dateMonth, $oddMonths) and $date > 30 and $todayDate >= 30)) and $status == "active") {
$logLedgerMonth = "INSERT INTO ledger (username, ref, subName, cost, dateEntered) VALUES ('$username', '$ref', '$name', '$cost', '$todayDate')";
$updateMonthLog = "UPDATE subs SET lastBilled='$todayDate' WHERE ref = '$ref'";
echo "<span style='color:red'>Logged Month for: $name : $ref </span> <br><br>";
if (!mysqli_query($conne, $logLedgerMonth) or !mysqli_query($conne, $updateMonthLog)) {
die('Error: ' . mysqli_error($conne));
}
}
//weekly
else if ($frequency == "Every Week" and ($dateDay == $nextLog) and $status == "active") {
$logLedgerWeek = "INSERT INTO ledger (username, ref, subName, cost, dateEntered) VALUES ('$username', '$ref', '$name', '$cost', '$todayDate')";
//define next log within current month scope
if ($nextLog <= 28) {
$newNextLog = $nextLog + 7;
} else {
$newNextLog = $date;
}
$updateWeekLog = "UPDATE subs SET nextLog='$newNextLog', lastBilled='$todayDate' WHERE ref = '$ref'";
echo "<span style='color:red'>Logged Week for: $name : $ref </span> <br> Next log re-set to sub date for: $ref : $newNextLog <br>";
if (!mysqli_query($conne, $updateWeekLog) or !mysqli_query($conne, $logLedgerWeek)) {
die('Error: ' . mysqli_error($conne));
}
} else {
echo "No week or month sub found!";
}
echo "Completed log review on: $name <br><br>";
}
if ($gotSubMonths == false) {
echo "error logging sub in ledger";
}
//close connection
mysqli_close($conne);