Profit and Loss Report Completed

This commit is contained in:
root 2019-04-26 18:58:56 -04:00
parent d6814ab05f
commit 2416f80dba
1 changed files with 297 additions and 55 deletions

View File

@ -1,17 +1,42 @@
<?php include("header.php"); ?>
<?php $sql = mysqli_query($mysqli,"SELECT * FROM categories WHERE category type = 'Expense' ORDER BY vendor_id DESC"); ?>
<?php
if(isset($_GET['year'])){
$year = intval($_GET['year']);
}else{
$year = date('Y');
}
//GET unique years from expenses and payments
$sql_all_years = mysqli_query($mysqli,"SELECT YEAR(expense_date) AS all_years FROM expenses UNION DISTINCT SELECT YEAR(payment_date) FROM payments ORDER BY all_years DESC");
$sql_categories_income = mysqli_query($mysqli,"SELECT * FROM categories WHERE category_type = 'Income' ORDER BY category_name ASC");
$sql_categories_expense = mysqli_query($mysqli,"SELECT * FROM categories WHERE category_type = 'Expense' ORDER BY category_name ASC");
?>
<div class="card mb-3">
<div class="card-header">
<h6 class="float-left mt-1"><i class="fa fa-coins"></i> Profit & Loss</h6>
<button type="button" class="btn btn-primary btn-sm float-right" data-toggle="modal" data-target="#print"><i class="fas fa-print"></i> Print</button>
<select class="form-control mt-5">
<option>2019</option>
<option>2018</option>
<option>2017</option>
</select>
<h6 class="float-left mt-1"><i class="fa fa-balance-scale"></i> Profit & Loss</h6>
<button type="button" class="btn btn-primary btn-sm float-right d-print-none" onclick="window.print();"><i class="fas fa-print"></i> Print</button>
<form>
<select onchange="this.form.submit()" class="form-control mt-5" name="year">
<?php
while($row = mysqli_fetch_array($sql_all_years)){
$all_years = $row['all_years'];
?>
<option <?php if($year == $all_years){ ?> selected <?php } ?> > <?php echo $all_years; ?></option>
<?php
}
?>
</select>
</form>
</div>
<div class="card-body">
<div class="table-responsive">
@ -31,65 +56,282 @@
</tr>
</thead>
<tbody>
<tr>
<td>Income Category Type</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
</tr>
<tr>
<td>Income Category Type 2</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
</tr>
<?php
while($row = mysqli_fetch_array($sql_categories_income)){
$category_id = $row['category_id'];
$category_name = $row['category_name'];
?>
<tr>
<td><?php echo $category_name; ?></td>
<?php
for($month = 1; $month<=3; $month++) {
$sql_payments = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payment_amount_for_month FROM payments, invoices WHERE payments.invoice_id = invoices.invoice_id AND invoices.category_id = $category_id AND YEAR(payment_date) = $year AND MONTH(payment_date) = $month");
$row = mysqli_fetch_array($sql_payments);
$payment_amount_for_quarter_one = $payment_amount_for_quarter_one + $row['payment_amount_for_month'];
}
?>
<td class="text-right">$<?php echo number_format($payment_amount_for_quarter_one,2); ?></td>
<?php
for($month = 4; $month<=6; $month++) {
$sql_payments = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payment_amount_for_month FROM payments, invoices WHERE payments.invoice_id = invoices.invoice_id AND invoices.category_id = $category_id AND YEAR(payment_date) = $year AND MONTH(payment_date) = $month");
$row = mysqli_fetch_array($sql_payments);
$payment_amount_for_quarter_two = $payment_amount_for_quarter_two + $row['payment_amount_for_month'];
}
?>
<td class="text-right">$<?php echo number_format($payment_amount_for_quarter_two,2); ?></td>
<?php
for($month = 7; $month<=9; $month++) {
$sql_payments = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payment_amount_for_month FROM payments, invoices WHERE payments.invoice_id = invoices.invoice_id AND invoices.category_id = $category_id AND YEAR(payment_date) = $year AND MONTH(payment_date) = $month");
$row = mysqli_fetch_array($sql_payments);
$payment_amount_for_quarter_three = $payment_amount_for_quarter_three + $row['payment_amount_for_month'];
}
?>
<td class="text-right">$<?php echo number_format($payment_amount_for_quarter_three,2); ?></td>
<?php
for($month = 10; $month<=12; $month++) {
$sql_payments = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payment_amount_for_month FROM payments, invoices WHERE payments.invoice_id = invoices.invoice_id AND invoices.category_id = $category_id AND YEAR(payment_date) = $year AND MONTH(payment_date) = $month");
$row = mysqli_fetch_array($sql_payments);
$payment_amount_for_quarter_four = $payment_amount_for_quarter_four + $row['payment_amount_for_month'];
}
$total_payments_for_all_four_quarters = $payment_amount_for_quarter_one + $payment_amount_for_quarter_two + $payment_amount_for_quarter_three + $payment_amount_for_quarter_four;
?>
<td class="text-right">$<?php echo number_format($payment_amount_for_quarter_four,2); ?></td>
<td class="text-right">$<?php echo number_format($total_payments_for_all_four_quarters,2); ?></td>
</tr>
<?php
$payment_amount_for_quarter_one = 0;
$payment_amount_for_quarter_two = 0;
$payment_amount_for_quarter_three = 0;
$payment_amount_for_quarter_four = 0;
$total_payment_for_all_months = 0;
}
?>
<tr>
<th>Gross Profit</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<?php
for($month = 1; $month<=3; $month++) {
$sql_payments = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payment_total_amount_for_month FROM payments, invoices WHERE payments.invoice_id = invoices.invoice_id AND YEAR(payment_date) = $year AND MONTH(payment_date) = $month");
$row = mysqli_fetch_array($sql_payments);
$payment_total_amount_for_quarter_one = $payment_total_amount_for_quarter_one + $row['payment_total_amount_for_month'];
}
?>
<th class="text-right">$<?php echo number_format($payment_total_amount_for_quarter_one,2); ?></th>
<?php
for($month = 4; $month<=6; $month++) {
$sql_payments = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payment_total_amount_for_month FROM payments, invoices WHERE payments.invoice_id = invoices.invoice_id AND YEAR(payment_date) = $year AND MONTH(payment_date) = $month");
$row = mysqli_fetch_array($sql_payments);
$payment_total_amount_for_quarter_two = $payment_total_amount_for_quarter_two + $row['payment_total_amount_for_month'];
}
?>
<th class="text-right">$<?php echo number_format($payment_total_amount_for_quarter_two,2); ?></th>
<?php
for($month = 7; $month<=9; $month++) {
$sql_payments = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payment_total_amount_for_month FROM payments, invoices WHERE payments.invoice_id = invoices.invoice_id AND YEAR(payment_date) = $year AND MONTH(payment_date) = $month");
$row = mysqli_fetch_array($sql_payments);
$payment_total_amount_for_quarter_three = $payment_total_amount_for_quarter_three + $row['payment_total_amount_for_month'];
}
?>
<th class="text-right">$<?php echo number_format($payment_total_amount_for_quarter_three,2); ?></th>
<?php
for($month = 10; $month<=12; $month++) {
$sql_payments = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS payment_total_amount_for_month FROM payments, invoices WHERE payments.invoice_id = invoices.invoice_id AND YEAR(payment_date) = $year AND MONTH(payment_date) = $month");
$row = mysqli_fetch_array($sql_payments);
$payment_total_amount_for_quarter_four = $payment_total_amount_for_quarter_four + $row['payment_total_amount_for_month'];
}
$total_payments_for_all_four_quarters = $payment_total_amount_for_quarter_one + $payment_total_amount_for_quarter_two + $payment_total_amount_for_quarter_three + $payment_total_amount_for_quarter_four;
?>
<th class="text-right">$<?php echo number_format($payment_total_amount_for_quarter_four,2); ?></th>
<th class="text-right">$<?php echo number_format($total_payments_for_all_four_quarters,2); ?></th>
</tr>
<tr>
<th><br><br>Expenses</th>
<th colspan="5"></th>
</tr>
<tr>
<td>Expense Category Type</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
</tr>
<tr>
<td>Expense Category Type 2</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
<td class="text-right">$0.00</td>
</tr>
<?php
while($row = mysqli_fetch_array($sql_categories_expense)){
$category_id = $row['category_id'];
$category_name = $row['category_name'];
?>
<tr>
<td><?php echo $category_name; ?></td>
<?php
for($month = 1; $month<=3; $month++) {
$sql_expenses = mysqli_query($mysqli,"SELECT SUM(expense_amount) AS expense_amount_for_month FROM expenses WHERE category_id = $category_id AND YEAR(expense_date) = $year AND MONTH(expense_date) = $month");
$row = mysqli_fetch_array($sql_expenses);
$expense_amount_for_quarter_one = $expense_amount_for_quarter_one + $row['expense_amount_for_month'];
}
?>
<td class="text-right">$<?php echo number_format($expense_amount_for_quarter_one,2); ?></td>
<?php
for($month = 4; $month<=6; $month++) {
$sql_expenses = mysqli_query($mysqli,"SELECT SUM(expense_amount) AS expense_amount_for_month FROM expenses WHERE category_id = $category_id AND YEAR(expense_date) = $year AND MONTH(expense_date) = $month");
$row = mysqli_fetch_array($sql_expenses);
$expense_amount_for_quarter_two = $expense_amount_for_quarter_two + $row['expense_amount_for_month'];
}
?>
<td class="text-right">$<?php echo number_format($expense_amount_for_quarter_two,2); ?></td>
<?php
for($month = 7; $month<=9; $month++) {
$sql_expenses = mysqli_query($mysqli,"SELECT SUM(expense_amount) AS expense_amount_for_month FROM expenses WHERE category_id = $category_id AND YEAR(expense_date) = $year AND MONTH(expense_date) = $month");
$row = mysqli_fetch_array($sql_expenses);
$expense_amount_for_quarter_three = $expense_amount_for_quarter_three + $row['expense_amount_for_month'];
}
?>
<td class="text-right">$<?php echo number_format($expense_amount_for_quarter_three,2); ?></td>
<?php
for($month = 10; $month<=12; $month++) {
$sql_expenses = mysqli_query($mysqli,"SELECT SUM(expense_amount) AS expense_amount_for_month FROM expenses WHERE category_id = $category_id AND YEAR(expense_date) = $year AND MONTH(expense_date) = $month");
$row = mysqli_fetch_array($sql_expenses);
$expense_amount_for_quarter_four = $expense_amount_for_quarter_four + $row['expense_amount_for_month'];
}
$total_expenses_for_all_four_quarters = $expense_amount_for_quarter_one + $expense_amount_for_quarter_two + $expense_amount_for_quarter_three + $expense_amount_for_quarter_four;
?>
<td class="text-right">$<?php echo number_format($expense_amount_for_quarter_four,2); ?></td>
<td class="text-right">$<?php echo number_format($total_expenses_for_all_four_quarters,2); ?></td>
</tr>
<?php
$expense_amount_for_quarter_one = 0;
$expense_amount_for_quarter_two = 0;
$expense_amount_for_quarter_three = 0;
$expense_amount_for_quarter_four = 0;
$total_expense_for_all_months = 0;
}
?>
<tr>
<th>Total Expenses<br><br><br></th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<?php
for($month = 1; $month<=3; $month++) {
$sql_expenses = mysqli_query($mysqli,"SELECT SUM(expense_amount) AS expense_total_amount_for_month FROM expenses WHERE category_id > 0 AND YEAR(expense_date) = $year AND MONTH(expense_date) = $month");
$row = mysqli_fetch_array($sql_expenses);
$expense_total_amount_for_quarter_one = $expense_total_amount_for_quarter_one + $row['expense_total_amount_for_month'];
}
?>
<th class="text-right">$<?php echo number_format($expense_total_amount_for_quarter_one,2); ?></th>
<?php
for($month = 4; $month<=6; $month++) {
$sql_expenses = mysqli_query($mysqli,"SELECT SUM(expense_amount) AS expense_total_amount_for_month FROM expenses WHERE category_id > 0 AND YEAR(expense_date) = $year AND MONTH(expense_date) = $month");
$row = mysqli_fetch_array($sql_expenses);
$expense_total_amount_for_quarter_two = $expense_total_amount_for_quarter_two + $row['expense_total_amount_for_month'];
}
?>
<th class="text-right">$<?php echo number_format($expense_total_amount_for_quarter_two,2); ?></th>
<?php
for($month = 7; $month<=9; $month++) {
$sql_expenses = mysqli_query($mysqli,"SELECT SUM(expense_amount) AS expense_total_amount_for_month FROM expenses WHERE category_id > 0 AND YEAR(expense_date) = $year AND MONTH(expense_date) = $month");
$row = mysqli_fetch_array($sql_expenses);
$expense_total_amount_for_quarter_three = $expense_total_amount_for_quarter_three + $row['expense_total_amount_for_month'];
}
?>
<th class="text-right">$<?php echo number_format($expense_total_amount_for_quarter_three,2); ?></th>
<?php
for($month = 10; $month<=12; $month++) {
$sql_expenses = mysqli_query($mysqli,"SELECT SUM(expense_amount) AS expense_total_amount_for_month FROM expenses WHERE category_id > 0 AND YEAR(expense_date) = $year AND MONTH(expense_date) = $month");
$row = mysqli_fetch_array($sql_expenses);
$expense_total_amount_for_quarter_four = $expense_total_amount_for_quarter_four + $row['expense_total_amount_for_month'];
}
$total_expenses_for_all_four_quarters = $expense_total_amount_for_quarter_one + $expense_total_amount_for_quarter_two + $expense_total_amount_for_quarter_three + $expense_total_amount_for_quarter_four;
?>
<th class="text-right">$<?php echo number_format($expense_total_amount_for_quarter_four,2); ?></th>
<th class="text-right">$<?php echo number_format($total_expenses_for_all_four_quarters,2); ?></th>
</tr>
<tr>
<?php
$net_profit_quarter_one = $payment_total_amount_for_quarter_one - $expense_total_amount_for_quarter_one;
$net_profit_quarter_two = $payment_total_amount_for_quarter_two - $expense_total_amount_for_quarter_two;
$net_profit_quarter_three = $payment_total_amount_for_quarter_three - $expense_total_amount_for_quarter_three;
$net_profit_quarter_four = $payment_total_amount_for_quarter_four - $expense_total_amount_for_quarter_four;
$net_profit_year = $total_payments_for_all_four_quarters - $total_expenses_for_all_four_quarters;
?>
<th>Net Profit</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<th class="text-right">$0.00</th>
<th class="text-right">$<?php echo number_format($net_profit_quarter_one,2); ?></th>
<th class="text-right">$<?php echo number_format($net_profit_quarter_two,2); ?></th>
<th class="text-right">$<?php echo number_format($net_profit_quarter_three,2); ?></th>
<th class="text-right">$<?php echo number_format($net_profit_quarter_four,2); ?></th>
<th class="text-right">$<?php echo number_format($net_profit_year,2); ?></th>
</tr>
</tbody>
</table>