Skip to content

Commit

Permalink
Functionify the post handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
o-psi committed Mar 17, 2024
1 parent 6490cf6 commit a599362
Show file tree
Hide file tree
Showing 18 changed files with 3,082 additions and 2,554 deletions.
1,194 changes: 10 additions & 1,184 deletions functions.php

Large diffs are not rendered by default.

127 changes: 127 additions & 0 deletions functions/account_functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

// Accounts Related Functions

function createAccountType(
$name,
$type,
$description
) {

global $mysqli, $session_ip, $session_user_agent, $session_user_id;

mysqli_query($mysqli,"INSERT INTO account_types SET account_type_parent = $type, account_type_name = '$name', account_type_description = '$description'");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account Type', log_action = 'Create', log_description = '$name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
}
function editAccountType(
$account_type_id,
$name,
$type,
$description
) {

global $mysqli, $session_ip, $session_user_agent, $session_user_id;

mysqli_query($mysqli,"UPDATE account_types SET account_type_parent = $type, account_type_name = '$name', account_type_description = '$description' WHERE account_type_id = $account_type_id");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account Type', log_action = 'Edit', log_description = '$name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
}
function readAccountType(
$account_type_id
) {
global $mysqli;

$result = mysqli_query($mysqli,"SELECT * FROM account_types WHERE account_type_id = $account_type_id");
return mysqli_fetch_assoc($result);
}
function archiveAccountType(
$account_type_id
) {

global $mysqli, $session_ip, $session_user_agent, $session_user_id;

mysqli_query($mysqli,"UPDATE account_types SET account_type_archived_at = NOW() WHERE account_type_id = $account_type_id");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account Type', log_action = 'Archive', log_description = '$account_type_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
}
function unarchiveAccountType(
$account_type_id
) {

global $mysqli, $session_ip, $session_user_agent, $session_user_id;

mysqli_query($mysqli,"UPDATE account_types SET account_type_archived_at = NULL WHERE account_type_id = $account_type_id");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account Type', log_action = 'Unarchive', log_description = '$account_type_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
}
function createAccount(
$name,
$opening_balance,
$currency_code,
$notes,
$type
) {
global $mysqli, $session_ip, $session_user_agent, $session_user_id;

mysqli_query($mysqli,"INSERT INTO accounts SET account_name = '$name', opening_balance = $opening_balance, account_currency_code = '$currency_code', account_type ='$type', account_notes = '$notes'");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account', log_action = 'Create', log_description = '$name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");

}
function editAccount(
$account_id,
$name,
$type,
$notes
) {
global $mysqli, $session_ip, $session_user_agent, $session_user_id;

mysqli_query($mysqli,"UPDATE accounts SET account_name = '$name',account_type = '$type', account_notes = '$notes' WHERE account_id = $account_id");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account', log_action = 'Modify', log_description = '$name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
}
function readAccount(
$account_id
) {
global $mysqli;

$result = mysqli_query($mysqli,"SELECT * FROM accounts WHERE account_id = $account_id");
return mysqli_fetch_assoc($result);
}
function archiveAccount(
$account_id
) {
global $mysqli, $session_ip, $session_user_agent, $session_user_id;

mysqli_query($mysqli,"UPDATE accounts SET account_archived_at = NOW() WHERE account_id = $account_id");

//logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account', log_action = 'Archive', log_description = '$account_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent'");
}
function unarchiveAccount(
$account_id
) {
global $mysqli, $session_ip, $session_user_agent, $session_user_id;

mysqli_query($mysqli,"UPDATE accounts SET account_archived_at = NULL WHERE account_id = $account_id");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account', log_action = 'Unarchive', log_description = '$account_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
}
function deleteAccount(
$account_id
) {
global $mysqli, $session_ip, $session_user_agent, $session_user_id;

mysqli_query($mysqli,"DELETE FROM accounts WHERE account_id = $account_id");

//Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'Account', log_action = 'Delete', log_description = '$account_id', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id");
}
131 changes: 131 additions & 0 deletions functions/accounting_functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

// Accounting related functions

function getMonthlyTax($tax_name, $month, $year, $mysqli)
{
// SQL to calculate monthly tax
$sql = "SELECT SUM(item_tax) AS monthly_tax FROM invoice_items
LEFT JOIN invoices ON invoice_items.item_invoice_id = invoices.invoice_id
LEFT JOIN payments ON invoices.invoice_id = payments.payment_invoice_id
WHERE YEAR(payments.payment_date) = $year AND MONTH(payments.payment_date) = $month
AND invoice_items.item_tax_id = (SELECT tax_id FROM taxes WHERE tax_name = '$tax_name')";
$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_assoc($result);
return $row['monthly_tax'] ?? 0;
}

function getQuarterlyTax($tax_name, $quarter, $year, $mysqli)
{
// Calculate start and end months for the quarter
$start_month = ($quarter - 1) * 3 + 1;
$end_month = $start_month + 2;

// SQL to calculate quarterly tax
$sql = "SELECT SUM(item_tax) AS quarterly_tax FROM invoice_items
LEFT JOIN invoices ON invoice_items.item_invoice_id = invoices.invoice_id
LEFT JOIN payments ON invoices.invoice_id = payments.payment_invoice_id
WHERE YEAR(payments.payment_date) = $year AND MONTH(payments.payment_date) BETWEEN $start_month AND $end_month
AND invoice_items.item_tax_id = (SELECT tax_id FROM taxes WHERE tax_name = '$tax_name')";
$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_assoc($result);
return $row['quarterly_tax'] ?? 0;
}

function getTotalTax($tax_name, $year, $mysqli)
{
// SQL to calculate total tax
$sql = "SELECT SUM(item_tax) AS total_tax FROM invoice_items
LEFT JOIN invoices ON invoice_items.item_invoice_id = invoices.invoice_id
LEFT JOIN payments ON invoices.invoice_id = payments.payment_invoice_id
WHERE YEAR(payments.payment_date) = $year
AND invoice_items.item_tax_id = (SELECT tax_id FROM taxes WHERE tax_name = '$tax_name')";
$result = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_assoc($result);
return $row['total_tax'] ?? 0;
}

//Get account currency code
function getAccountCurrencyCode($mysqli, $account_id)
{
$sql = mysqli_query($mysqli, "SELECT account_currency_code FROM accounts WHERE account_id = $account_id");
$row = mysqli_fetch_array($sql);
$account_currency_code = nullable_htmlentities($row['account_currency_code']);
return $account_currency_code;
}

function calculateAccountBalance($mysqli, $account_id)
{
$sql_account = mysqli_query($mysqli, "SELECT * FROM accounts LEFT JOIN account_types ON accounts.account_type = account_types.account_type_id WHERE account_archived_at IS NULL AND account_id = $account_id ORDER BY account_name ASC; ");
$row = mysqli_fetch_array($sql_account);
$opening_balance = floatval($row['opening_balance']);
$account_id = intval($row['account_id']);

$sql_payments = mysqli_query($mysqli, "SELECT SUM(payment_amount) AS total_payments FROM payments WHERE payment_account_id = $account_id");
$row = mysqli_fetch_array($sql_payments);
$total_payments = floatval($row['total_payments']);

$sql_revenues = mysqli_query($mysqli, "SELECT SUM(revenue_amount) AS total_revenues FROM revenues WHERE revenue_account_id = $account_id");
$row = mysqli_fetch_array($sql_revenues);
$total_revenues = floatval($row['total_revenues']);

$sql_expenses = mysqli_query($mysqli, "SELECT SUM(expense_amount) AS total_expenses FROM expenses WHERE expense_account_id = $account_id");
$row = mysqli_fetch_array($sql_expenses);
$total_expenses = floatval($row['total_expenses']);

$balance = $opening_balance + $total_payments + $total_revenues - $total_expenses;

if ($balance == '') {
$balance = '0.00';
}

return $balance;
}
function calculateInvoiceBalance($mysqli, $invoice_id)
{
$invoice_id_int = intval($invoice_id);
$sql_invoice = mysqli_query($mysqli, "SELECT * FROM invoices WHERE invoice_id = $invoice_id_int");
$row = mysqli_fetch_array($sql_invoice);
$invoice_amount = floatval($row['invoice_amount']);

$sql_payments = mysqli_query(
$mysqli,
"SELECT SUM(payment_amount) AS total_payments FROM payments
WHERE payment_invoice_id = $invoice_id
"
);

$row = mysqli_fetch_array($sql_payments);
$total_payments = floatval($row['total_payments']);

$balance = $invoice_amount - $total_payments;

if ($balance == '') {
$balance = '0.00';
}

return $balance;
}

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;
}
}
28 changes: 28 additions & 0 deletions functions/api_functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

// API related functions

function createAPIKey($secret, $name, $expire, $client) {
global $mysqli, $session_name, $session_ip, $session_user_agent, $session_user_id;

mysqli_query($mysqli,"INSERT INTO api_keys SET api_key_name = '$name', api_key_secret = '$secret', api_key_expire = '$expire', api_key_client_id = $client");
$api_key = mysqli_insert_id($mysqli);

// Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'API', log_action = 'Create', log_description = '$session_name created API Key $name set to expire on $expire', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_client_id = $client, log_user_id = $session_user_id, log_entity_id = $api_key_id");

return $api_key;
}

function deleteAPIKey($api_key_id) {
global $mysqli, $session_name, $session_ip, $session_user_agent, $session_user_id;

// Get API Key Name
$row = mysqli_fetch_array(mysqli_query($mysqli,"SELECT * FROM api_keys WHERE api_key_id = $api_key_id"));
$name = sanitizeInput($row['api_key_name']);

mysqli_query($mysqli,"DELETE FROM api_keys WHERE api_key_id = $api_key_id");

// Logging
mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'API Key', log_action = 'Delete', log_description = '$session_name deleted API key $name', log_ip = '$session_ip', log_user_agent = '$session_user_agent', log_user_id = $session_user_id, log_entity_id = $api_key_id");
}
Loading

0 comments on commit a599362

Please sign in to comment.