使用PHP获取分类帐报告Mysql表

时间:2018-11-27 18:44:14

标签: php mysql

我正在插入mysql表

这是我的桌子

ID |VoucherNO|   VoucherType|   AccName         |   Particulars       |   Debit     |    Credit |     Date
---|---------|--------------|-------------------|---------------------|-------------|-----------|------------
1  |    1    |  Cash Payment|    OFFICE EXPENSE |   CASH ACCOUNT      |     500     |     0 | 2018-nov-25 
---|---------|--------------|-------------------|---------------------|-------------|-----------|------------
2  |    1    |  Cash Payment|    CASH ACCOUNT   |   OFFICE EXPENSE    |     0       |     500   | 2018-nov-25 
---|---------|--------------|-------------------|---------------------|-------------|-----------|------------
3  |    2    |  Cash Payment|    OFFICE EXPENSE |   CASH ACCOUNT      |     250     |     0 | 2018-nov-26 
---|---------|--------------|-------------------|---------------------|-------------|-----------|------------
4  |    2    |  Cash Payment|    CASH ACCOUNT   |   OFFICE EXPENSE    |     0       |     250   | 2018-nov-26 
---|---------|--------------|-------------------|---------------------|-------------|-----------|-----------
5  |    3    |  Cash Payment|    OFFICE EXPENSE |   CASH ACCOUNT      |     100     |     0 | 2018-nov-27 
---|---------|--------------|-------------------|---------------------|-------------|-----------|------------
6  |    3    |  Cash Payment|    CASH ACCOUNT   |   OFFICE EXPENSE    |     0       |     100   | 2018-nov-27 
------------------------------------------------------------------------------------------------------------- 

我想这样获取报告mysql表

----------------------------------------------------------------------------------------- ---
Date         | VoucherNo |    VoucherType   | Partuclars    | debit  |   credit   |  Balance
---------------------------------------------------------------------------------------------

2018-nov-25      1          Cash Payment     CASH ACCOUNT   500                      500
___________________________________________________________________________________________
2018-nov-26      2          Cash Payment     CASH ACCOUNT   250                      750                
___________________________________________________________________________________________
2018-nov-27      3          Cash Payment     CASH ACCOUNT   100                      850
____________________________________________________________________________________________

                            total                          850            0.00      850 

此表从库存软件接收了我,但我不知道报告查询 如何使用PHP和MYSQL

1 个答案:

答案 0 :(得分:0)

PHP可能将不得不完成大部分工作,因此我将省略SQL,但我假设一个简单的SELECT * FROM myTableName ORDER BY VoucherNo;就可以了(如果这样的话,可能不需要ORDER BY主键)。我也将0用作初始余额,因此请根据需要进行调整。

$balance = 0;
$curdate = '';
$output = array();
$curValue = array(
    'Date' => '',
    'VoucherNo' => 0, 
    'VoucherType' => '', 
    'Partuclars' => '', 
    'debit' => 0, 
    'credit' => 0, 
    'Balance' => 0
);

foreach($prePopulatedResultsArray as $row) {

    // if the current day is done being populated, add data to output and reset current
    if ($curdate !== $row['Date']) {
    // only add the current value if it has been populated
        if ($curdate !== '')
            $output[] = $curValue;

        $curValue = array(
            'Date' => $row['Date'], 
            'VoucherNo' => $row['VoucherNo'], 
            'VoucherType' => $row['VoucherType'], 
            'Partuclars' => $row['Partuclars'], 
            'debit' => $row['debit'], 
            'credit' => $row['credit'], 
            'Balance' => $balance + $row['debit']
        );
        $curdate = $row['Date'];

    // if it is still the same day as the last iteration, add to existing values
    } else {
        $curValue['debit'] = $curValue['debit']+$row['debit'];
        $curValue['credit'] = $curValue['debit']+$row['credit'];
        $balance = $balance + $row['debit'];
        $curValue['Balance'] = $curValue['Balance']+$balance;
    }
}

这可能与您的目标有些出入,因为我的输出是基于您将debit添加到Balance并且我正在填充credit的事实尽管预期结果中有空值。让我知道答案是否需要调整。