Skip to content

Commit

Permalink
Credits
Browse files Browse the repository at this point in the history
  • Loading branch information
o-psi committed Feb 27, 2024
1 parent 998a701 commit 129fbd3
Show file tree
Hide file tree
Showing 6 changed files with 602 additions and 188 deletions.
169 changes: 169 additions & 0 deletions credits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

// Default Column Sortby/Order Filter
$sort = "client_id";
$order = "DESC";

require_once "inc_all.php";


//Rebuild URL
$url_query_strings_sort = http_build_query($get_copy);

$sql = mysqli_query(
$mysqli,
"SELECT * FROM credits
LEFT JOIN clients ON credit_client_id = client_id
LEFT JOIN accounts ON credit_account_id = account_id
WHERE credit_archived_at IS NULL
ORDER BY $sort $order"
);

$num_rows = mysqli_num_rows($sql);

?>

<div class="card card-dark">
<div class="card-header py-3">
<h3 class="card-title"><i class="fas fa-fw fa-credit-card mr-2"></i>Credits</h3>
</div>

<div class="card-body">
<form class="mb-4" autocomplete="off">
<div class="row">
<div class="col-sm-4">
<div class="input-group">
<input type="search" class="form-control" name="q"
value="<?php if (isset($q)) {echo stripslashes(nullable_htmlentities($q));} ?>"
placeholder="Search Credits">
<div class="input-group-append">
<button class="btn btn-secondary" type="button" data-toggle="collapse"
data-target="#advancedFilter"><i class="fas fa-filter"></i></button>
<button class="btn btn-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</div>
</div>
<div class="collapse mt-3 <?php if (!empty($_GET['dtf']) || $_GET['canned_date'] !== "custom" ) { echo "show"; } ?>"
id="advancedFilter">
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label>Canned Date</label>
<select class="form-control select2" name="canned_date">
<option <?php if ($_GET['canned_date'] == "custom") { echo "selected"; } ?>
value="custom">Custom</option>
<option <?php if ($_GET['canned_date'] == "today") { echo "selected"; } ?>
value="today">Today</option>
<option <?php if ($_GET['canned_date'] == "yesterday") { echo "selected"; } ?>
value="yesterday">Yesterday</option>
<option <?php if ($_GET['canned_date'] == "thisweek") { echo "selected"; } ?>
value="thisweek">This Week</option>
<option <?php if ($_GET['canned_date'] == "lastweek") { echo "selected"; } ?>
value="lastweek">Last Week</option>
<option <?php if ($_GET['canned_date'] == "thismonth") { echo "selected"; } ?>
value="thismonth">This Month</option>
<option <?php if ($_GET['canned_date'] == "lastmonth") { echo "selected"; } ?>
value="lastmonth">Last Month</option>
<option <?php if ($_GET['canned_date'] == "thisyear") { echo "selected"; } ?>
value="thisyear">This Year</option>
<option <?php if ($_GET['canned_date'] == "lastyear") { echo "selected"; } ?>
value="lastyear">Last Year</option>
</select>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Date From</label>
<input type="date" class="form-control" name="dtf" max="2999-12-31"
value="<?php echo nullable_htmlentities($dtf); ?>">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Date To</label>
<input type="date" class="form-control" name="dtt" max="2999-12-31"
value="<?php echo nullable_htmlentities($dtt); ?>">
</div>
</div>
</div>
</div>
</form>
<hr>
<div class="table-responsive-sm">
<table class="table table-striped table-borderless table-hover">
<thead class="text-dark <?php if ($num_rows == 0) { echo "d-none"; } ?>">
<tr>
<th><a class="text-dark"
href="?<?php echo $url_query_strings_sort; ?>&sort=credit_client&order=<?php echo $disp; ?>">Client Name</a></th>
<th><a class="text-dark"
href="?<?php echo $url_query_strings_sort; ?>&sort=credit_account&order=<?php echo $disp; ?>">Account
Name</a></th>
<th class="text-right
<?php if ($sort == "credit_amount") { echo "sorting-$order"; } ?>">
<a class="text-dark"
href="?<?php echo $url_query_strings_sort; ?>&sort=credit_amount&order=<?php echo $disp; ?>">Amount</a>
</th>
<th><a class="text-dark"
href="?<?php echo $url_query_strings_sort; ?>&sort=credit_date&order=<?php echo $disp; ?>">Date</a>
</th>
<th><a class="text-dark"
href="?<?php echo $url_query_strings_sort; ?>&sort=credit_reference&order=<?php echo $disp; ?>">Reference</a>
</th>
<th>Actions</th>

</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($sql)) {
$credit_id = $row['credit_id'];
$credit_amount = $row['credit_amount'];
$credit_currency_code = $row['credit_currency_code'];
$credit_date = $row['credit_date'];
$credit_reference = $row['credit_reference'];
$credit_client_id = $row['credit_client_id'];
$credit_payment_id = $row['credit_payment_id'];
$credit_account_id = $row['credit_account_id'];

// Get client name from DB
$clientQuery = mysqli_query($mysqli, "SELECT * FROM clients WHERE client_id = $credit_client_id LIMIT 1");
$client = mysqli_fetch_array($clientQuery);
$client_name = $client['client_name'];

// Get account name from DB
$accountQuery = mysqli_query($mysqli, "SELECT * FROM accounts WHERE account_id = $credit_account_id LIMIT 1");
$account = mysqli_fetch_array($accountQuery);
$account_name = $account['account_name'];

$credit_display_amount = numfmt_format_currency($currency_format, $credit_amount, $credit_currency_code);

$client_balance = getClientBalance($mysqli, $credit_client_id);
?>

<tr>
<td><?php echo $client_name; ?></td>
<td><?php echo $account_name; ?></td>
<td class="text-right
<?php if ($sort == "credit_amount") { echo "sorting-$order"; } ?>">
<?php echo $credit_display_amount; ?>
</td>
<td><?php echo $credit_date; ?></td>
<td><?php echo $credit_reference; ?></td>
<td>
<a href="post.php?apply_credit_id=<?php echo $credit_id; ?>" class="btn btn-sm btn-primary"
title="Apply"><i class="fas fa-credit-card"></i></a>
<a href="post.php?delete_credit_id=<?php echo $credit_id; ?>" class="btn btn-sm btn-danger"
title="Delete"><i class="fas fa-trash"></i></a>
</td>
</tr>
<?php } ?>

</tbody>
</table>
</div>
</div>
</div>

<?php require_once "footer.php";
?>
23 changes: 23 additions & 0 deletions db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,29 @@ CREATE TABLE `contacts` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `credits`
--

DROP TABLE IF EXISTS `credits`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `credits` (
`credit_id` int(11) NOT NULL AUTO_INCREMENT,
`credit_amount` decimal(15,2) NOT NULL DEFAULT 0.00,
`credit_currency_code` varchar(200) NOT NULL,
`credit_date` date NOT NULL,
`credit_reference` varchar(200) DEFAULT NULL,
`credit_payment_method` varchar(200) DEFAULT NULL,
`credit_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`credit_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
`credit_archived_at` datetime DEFAULT NULL,
`credit_client_id` int(11) NOT NULL DEFAULT 0,
`credit_payment_id` int(11) NOT NULL DEFAULT 0,
`credit_account_id` int(11) DEFAULT NULL,
PRIMARY KEY (`credit_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Table structure for table `custom_fields`
--
Expand Down
23 changes: 23 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,4 +1133,27 @@ function createiCalStrCancel($originaliCalStr) {
// Return the modified iCal string
return $cal_event->export();
}

function getClientBalance($mysqli, $client_id, $credits = false) {
//Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_invoice_amounts = mysqli_query($mysqli, "SELECT SUM(invoice_amount) AS invoice_amounts FROM invoices WHERE invoice_client_id = $client_id AND invoice_status NOT LIKE 'Draft' AND invoice_status NOT LIKE 'Cancelled'");
$row = mysqli_fetch_array($sql_invoice_amounts);

$invoice_amounts = floatval($row['invoice_amounts']);

$sql_amount_paid = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS amount_paid FROM payments, invoices WHERE payment_invoice_id = invoice_id AND invoice_client_id = $client_id");
$row = mysqli_fetch_array($sql_amount_paid);

$amount_paid = floatval($row['amount_paid']);

if ($credits) {
$sql_credits = mysqli_query($mysqli, "SELECT SUM(credit_amount) AS credit_amounts FROM credits WHERE credit_client_id = $client_id");
$row = mysqli_fetch_array($sql_credits);
$credit_amounts = floatval($row['credit_amounts']);

return $invoice_amounts - ($amount_paid + $credit_amounts);
} else {
return $invoice_amounts - $amount_paid;
}
}

14 changes: 2 additions & 12 deletions inc_all_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,8 @@
}
$client_tags_display = implode('', $client_tag_name_display_array);

//Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_invoice_amounts = mysqli_query($mysqli, "SELECT SUM(invoice_amount) AS invoice_amounts FROM invoices WHERE invoice_client_id = $client_id AND invoice_status NOT LIKE 'Draft' AND invoice_status NOT LIKE 'Cancelled'");
$row = mysqli_fetch_array($sql_invoice_amounts);

$invoice_amounts = floatval($row['invoice_amounts']);

$sql_amount_paid = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS amount_paid FROM payments, invoices WHERE payment_invoice_id = invoice_id AND invoice_client_id = $client_id");
$row = mysqli_fetch_array($sql_amount_paid);

$amount_paid = floatval($row['amount_paid']);

$balance = $invoice_amounts - $amount_paid;
// Get Balance
$balance = getClientBalance($mysqli, $client_id, $credits = true);

//Get Monthly Recurring Total
$sql_recurring_monthly_total = mysqli_query($mysqli, "SELECT SUM(recurring_amount) AS recurring_monthly_total FROM recurring WHERE recurring_status = 1 AND recurring_frequency = 'month' AND recurring_client_id = $client_id");
Expand Down
Loading

0 comments on commit 129fbd3

Please sign in to comment.