I am almost done with a small inventory system but i am having a "concept" problem.
The problem is with the cash record.
I have a page to make cash records manually with option of either credit or debit, but mostly this is for credit records as sales will automatically be stored in sales table and also stored cash table as debit.
so the cash table looks like this:
Date | description | debit | credit |
2014-08-23 | sales | 456 | |
2014-08-23 | transportation| | 234 |
2014-08-23 | donation | 567 | |
I am using the above table to calculate for profit per month (debit - credit = profit)
This is all working fine to check for month;y flow of cash.
The problem is i dont want it to be just for a month (the monthly cash flow is needed though and its fine already).
With the above i do a query like:
$date = date("Y-m");
$sql = "SELECT * FROM cash WHERE date LIKE '$date%'"
so this will give me all for that particular month.
The main problem now is:
I want the starting cash for the next month to be the profit from the last month (balance brought forward) that will be able to show the profit of the business and not just the profit for a month.
I cant think of the best way to do this
Edit (how i am doing it):
Database:
CREATE TABLE cash
(
id int auto_increment primary key,
date DATE,
descs varchar (255),
debit varchar(30),
credit varchar (30)
);
INSERT INTO cash
(date, descs, debit, credit)
VALUES
('2014-08-24', 'balance b/f', '5000',''),
('2014-08-24', 'transportation','', '350'),
('2014-08-24', 'sales','2340', '');
My Database query
$date = date('Y-m');
$sql = "SELECT * FROM cash WHERE date LIKE '$date%'" //this will get all from same year and month
PHP page
I now have a $cashs
with array of the result of the SQL query. then i do
<table class="table table-striped table-hover" id="table">
<thead>
<tr>
<th>No/s</th>
<th>Date</th>
<th>Purpose</th>
<th>Debit</th>
<th>Credit</th>
</tr>
</thead>
<tbody>
<?php $i=1; $total=0; foreach($cashs AS $cash): ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $cash->date; ?></td>
<td><?php echo $cash->desc; ?></td>
<td><?php echo $cash->debit; $total+=$cash->debit; ?></td>
<td><?php echo $cash->credit; $total-=$cash->credit; ?></td>
</tr>
<?php $i++; endforeach; ?>
<tr>
<td><strong>Total</strong></td>
<td></td>
<td></td>
<td></td>
<td><strong><?php echo $total; ?></strong></td>
</tr>
</tbody>
</table>
This is working fine but what i want is a way to get the $total
as used in the above code of the previous month also so it will be balance b/f as debit. That should be the starting balance for the new month.
so all I did was union'd a row before that gets the starting cash. I don't know if you want it to be in a different column or where you want it placed so I just added two extra columns. one for the date of the previous month with the starting cash and then the ordinary columns.. you can adjust this accordingly.
the key is using the date in the WHERE. so for you there will need to be a few tweaks.
SELECT
CONCAT(MONTH(date), '-', YEAR(date)) as 'Pervious Month',
'' as id,
'' as 'date',
'' as description,
'' as debit,
'' as credit,
SUM(debit) + SUM(credit) as "Starting Cash"
FROM cash
WHERE MONTH(date) = 7
UNION
SELECT '', c.*, ''
FROM cash c
WHERE MONTH(c.date) = 8
I'm just using month because I don't have the php setup.. but what you would do is change the MONTH(date) = 7
in the first select to
date = $date - INTERVAL 1 MONTH
and then the second select would be the same as what you had originally
WHERE date = $date
if you want to change the stating balance from just the last month to everything before the current month then change
date = $date - INTERVAL 1 MONTH
to this:
date < $date
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.