Started Alerts and the cron php script to trigger the alerts as well as check payment amount is greater than balance in invoice

This commit is contained in:
root 2019-04-08 15:31:15 -04:00
parent c1f070b677
commit df04ea7957
5 changed files with 113 additions and 30 deletions

View File

@ -2,7 +2,7 @@
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><i class="fa fa-cart-plus"></i> New expense</h5>
<h5 class="modal-title"><i class="fa fa-cart-plus"></i> New Expense</h5>
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">&times;</span>
</button>
@ -57,7 +57,7 @@
$balance = $opening_balance + $total_payments - $total_expenses;
?>
<option value="<?php echo $account_id; ?>"><?php echo $account_name; ?> [$<?php echo number_format($balance,2); ?>]</option>
<option value="<?php echo $account_id; ?>"><div class="float-left"><?php echo $account_name; ?></div><div class="float-right"> [$<?php echo number_format($balance,2); ?>]</div></option>
<?php
}

View File

@ -9,6 +9,7 @@
</div>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="invoice_id" value="<?php echo $invoice_id; ?>">
<input type="hidden" name="balance" value="<?php echo $balance; ?>">
<div class="modal-body">
<div class="form-row">
<div class="form-group col">

66
cron.php Normal file
View File

@ -0,0 +1,66 @@
<?php include("config.php"); ?>
<?php
//Get Alerts
//Get Domains Expiring
$sql = mysqli_query($mysqli,"SELECT * FROM client_domains, clients
WHERE client_domains.client_id = clients.client_id
AND client_domain_expire > CURDATE()
ORDER BY client_domain_id DESC"
);
while($row = mysqli_fetch_array($sql)){
$client_domain_id = $row['client_domain_id'];
$client_domain_name = $row['client_domain_name'];
$client_domain_expire = $row['client_domain_expire'];
$client_id = $row['client_id'];
$client_name = $row['client_name'];
mysqli_query($mysqli,"INSERT INTO alerts SET alert_type = 'Domain', alert_message = 'Domain $client_domain_name Expiring on $client_domain_expire', alert_date = CURDATE()");
}
//Get Low Account Balances
$sql = mysqli_query($mysqli,"SELECT * FROM accounts ORDER BY account_id DESC");
while($row = mysqli_fetch_array($sql)){
$account_id = $row['account_id'];
$account_name = $row['account_name'];
$opening_balance = $row['opening_balance'];
$sql_accounts = mysqli_query($mysqli,"SELECT * FROM accounts WHERE account_id = $account_id");
$row = mysqli_fetch_array($sql_accounts);
$opening_balance = $row['opening_balance'];
$sql_payments = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS total_payments FROM payments WHERE account_id = $account_id");
$row = mysqli_fetch_array($sql_payments);
$total_payments = $row['total_payments'];
$sql_expenses = mysqli_query($mysqli,"SELECT SUM(expense_amount) AS total_expenses FROM expenses WHERE account_id = $account_id");
$row = mysqli_fetch_array($sql_expenses);
$total_expenses = $row['total_expenses'];
$balance = $opening_balance + $total_payments - $total_expenses;
mysqli_query($mysqli,"INSERT INTO alerts SET alert_type = 'Low Balance', alert_message = 'Low Balance $$balance for $account_name', alert_date = CURDATE()");
}
//Get Overdue Invoices
$sql = mysqli_query($mysqli,"SELECT * FROM invoices WHERE invoice_date_due > CURDATE() AND invoice_status NOT LIKE 'Paid' OR invoice_status NOT LIKE 'Draft' ORDER BY invoice_id DESC");
$invoice_id = $row['invoice_id'];
$invoice_amount = $row['invoice_amount'];
$invoice_date = $row['invoice_date'];
$invoice_date_due = $row['invoice_date_due'];
//Send Recurring Invoices
//Send Past Due Invoice Reminders
$sql = mysqli_query($mysqli,"SELECT * FROM accounts ORDER BY account_id DESC");
?>

35
db.sql
View File

@ -27,7 +27,24 @@ CREATE TABLE `accounts` (
`account_name` varchar(200) NOT NULL,
`opening_balance` decimal(15,2) NOT NULL,
PRIMARY KEY (`account_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `alerts`
--
DROP TABLE IF EXISTS `alerts`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `alerts` (
`alert_id` int(11) NOT NULL AUTO_INCREMENT,
`alert_type` varchar(200) NOT NULL,
`alert_message` varchar(200) NOT NULL,
`alert_date` date NOT NULL,
`alert_read` int(1) NOT NULL,
PRIMARY KEY (`alert_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@ -258,7 +275,7 @@ CREATE TABLE `expenses` (
`category_id` int(11) NOT NULL,
`account_id` int(11) NOT NULL,
PRIMARY KEY (`expense_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@ -275,7 +292,7 @@ CREATE TABLE `invoice_history` (
`invoice_history_description` varchar(200) NOT NULL,
`invoice_id` int(11) NOT NULL,
PRIMARY KEY (`invoice_history_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@ -296,7 +313,7 @@ CREATE TABLE `invoice_items` (
`invoice_item_total` decimal(15,2) NOT NULL,
`invoice_id` int(11) NOT NULL,
PRIMARY KEY (`invoice_item_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@ -317,7 +334,7 @@ CREATE TABLE `invoices` (
`category_id` int(11) NOT NULL,
`client_id` int(11) NOT NULL,
PRIMARY KEY (`invoice_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@ -338,7 +355,7 @@ CREATE TABLE `mileage` (
`mileage_miles` int(11) NOT NULL,
`client_id` int(11) DEFAULT NULL,
PRIMARY KEY (`mileage_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@ -357,7 +374,7 @@ CREATE TABLE `payments` (
`account_id` int(11) NOT NULL,
`invoice_id` int(11) NOT NULL,
PRIMARY KEY (`payment_id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@ -413,7 +430,7 @@ CREATE TABLE `transfers` (
`expense_id` int(11) NOT NULL,
`payment_id` int(11) NOT NULL,
PRIMARY KEY (`transfer_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
--
@ -460,4 +477,4 @@ CREATE TABLE `vendors` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2019-04-08 1:16:28
-- Dump completed on 2019-04-08 15:29:51

View File

@ -558,33 +558,32 @@ if(isset($_GET['delete_invoice_item'])){
if(isset($_POST['add_payment'])){
$invoice_id = intval($_POST['invoice_id']);
$balance = $_POST['balance'];
$date = strip_tags(mysqli_real_escape_string($mysqli,$_POST['date']));
$amount = $_POST['amount'];
$account = intval($_POST['account']);
$payment_method = strip_tags(mysqli_real_escape_string($mysqli,$_POST['payment_method']));
mysqli_query($mysqli,"INSERT INTO payments SET payment_date = '$date', payment_amount = '$amount', account_id = $account, payment_method = '$payment_method', invoice_id = $invoice_id");
//Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_total_payments_amount = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payments_amount FROM payments WHERE invoice_id = $invoice_id");
$row = mysqli_fetch_array($sql_total_payments_amount);
$total_payments_amount = $row['payments_amount'];
//Get the invoice total
$sql = mysqli_query($mysqli,"SELECT * FROM invoices WHERE invoice_id = $invoice_id");
$row = mysqli_fetch_array($sql);
$invoice_amount = $row['invoice_amount'];
//Calculate the Invoice balance
$invoice_balance = $invoice_amount - $total_payments_amount;
if($amount > $invoice_balance){
//Check to see if amount entered is greater than the balance of the invoice
if($amount > $balance){
$_SESSION['alert_message'] = "Payment is more than the balance";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}else{
mysqli_query($mysqli,"INSERT INTO payments SET payment_date = '$date', payment_amount = '$amount', account_id = $account, payment_method = '$payment_method', invoice_id = $invoice_id");
//Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_total_payments_amount = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payments_amount FROM payments WHERE invoice_id = $invoice_id");
$row = mysqli_fetch_array($sql_total_payments_amount);
$total_payments_amount = $row['payments_amount'];
//Get the invoice total
$sql = mysqli_query($mysqli,"SELECT * FROM invoices WHERE invoice_id = $invoice_id");
$row = mysqli_fetch_array($sql);
$invoice_amount = $row['invoice_amount'];
//Calculate the Invoice balance
$invoice_balance = $invoice_amount - $total_payments_amount;
//Determine if invoice has been paid then set the status accordingly
if($invoice_balance == 0){
$invoice_status = "Paid";
@ -601,7 +600,7 @@ if(isset($_POST['add_payment'])){
$_SESSION['alert_message'] = "Payment added";
header("Location: " . $_SERVER["HTTP_REFERER"]);
}
}
}
if(isset($_GET['delete_payment'])){