How to create General Ledger/T-Account using PHP Mysql

守給你的承諾、 提交于 2019-12-03 22:23:01

问题


I am trying to create a double entry financial accounting system. I have completed designing the database for it, please check this link to see the diagram of my database . http://i39.tinypic.com/juhbv6.png

I have tried a lot to develop the mysql query to produce the general ledger but I got no where close. All I have managed to do is create ledger for only one account where I have to create for unlimited account.

The query I have tried is following:

 $this->db->select('*');
    $this->db->from('credit_side');
    $this->db->join('debit_side', ' debit_side.transaction_id_dr = credit_side.transaction_id_cr ');
    $this->db->join('transaction_info', 'transaction_info.transaction_id = credit_side.transaction_id_cr ');
    $this->db->join('accounts', 'accounts.code = credit_side.account_code ');
    $this->db->where('debit_side.account_code', '1001'); 
    $this->db->order_by('voucher_date','ASC');

After failing to write the mysql query which can produce the ledger for all accounts I have written down the logic to create General Ledger/T account .

Now, would you please help me with the mysql query?

Please check the database before going through the following. Thanks in Advance :)

  1. Get the accounts.name AS AccountHead, accounts.code from accounts.

  2. Go to the table debit_side and get debit_side.account_code, if debit_side.account_code=accounts.code then get credit_side.account_code AS AccountName1 (but when I will echo in php, I want to get the name instead of the code itself) and credit_side.amount AS Amount1, SUM(credit_side.amount) AS TotalAmount1 from credit_side where debit_side.transaction_id_dr=credit_side.transaction_id_cr and transaction_info.voucher_date is in between date1 and date2 WHERE transaction_info.transaction_id=debit_side.transaction_id_dr

  3. After completing the second step go to the table credit_side and get credit_side.account_code,
    if credit_side.account_code=accounts.code then get debit_side.account_code AS AccountName2 (but when I will echo in php, I want to get the name instead of the code itself) and debit_side.amount AS Amount2, SUM(debit_side.amount) AS TotalAmount2 from debit_side where credit_side.transaction_id_cr=debit_side.transaction_id_dr and transaction_info.voucher_date is in between date1 and date2 WHERE transaction_info.transaction_id=credit_side.transaction_id_cr

Now in the view file I aim to have the following:

   <table width="200" border="0">
     <tr><td colspan="5">Account Head <?echo $AccountHead ; ?> </td> </tr>


    <tr>
           <td>Dr.</td>
           <td>amount.</td>
            <td>&nbsp;</td>
           <td>Cr</td>
          <td>Amount</td>

    </tr>
     <tr>
       <td><?echo $AccountName1 ; ?></td>
       <td><?echo $Ammount1 ; ?></td>
      <td></td>
 <td><?echo $AccountName2 ; ?></td>
     <td><?echo $Ammount2 ; ?></td>

     </tr>

     <tr>
     <td>Total</td>
     <td><?echo $TotalAmount1 ; ?></td>
     <td>&nbsp;</td>
 <td>Total  </td>
 <td><?echo $TotalAmount2 ; ?></td>

     </tr>
     </table>

General Ledger Sample


回答1:


public function getDebits(){    
    $data   =   array(
                'debit_side.account_code DebitCode',
                'group_concat(distinct accounts.name) as DebitAccount',
                'group_concat(debit_side.amount) as DebitAmount',
                'group_concat(transaction_info.voucher_date) as DebitVoucherDate'
                );
    $this->db->select($data);
    $this->db->from('accounts');
    $this->db->join('credit_side','accounts.code = credit_side.account_code','left');
    $this->db->join('debit_side','debit_side.transaction_id_dr = credit_side.transaction_id_cr','left');
    $this->db->join('transaction_info','transaction_info.transaction_id = credit_side.transaction_id_cr','left');
    $this->db->group_by('debit_side.account_code');
    $this->db->order_by('debit_side.account_code','ASC');
    $query  =   $this->db->get();
    return  $query->result_array();
}


public  function getCredits()
{
    $data   =   array(
                'credit_side.account_code CreditCode',
                'group_concat(distinct accounts.name) as CreditAccount',
                'group_concat(credit_side.amount) as CreditAmount',
                'group_concat(transaction_info.voucher_date) as CreditVoucherDate'
                );
    $this->db->select($data);
    $this->db->from('accounts');
    $this->db->join('debit_side','accounts.code = debit_side.account_code','left');
    $this->db->join('credit_side','debit_side.transaction_id_dr = credit_side.transaction_id_cr','left');
    $this->db->join('transaction_info','transaction_info.transaction_id = credit_side.transaction_id_cr','left');
    $this->db->group_by('credit_side.account_code');
    $this->db->order_by('credit_side.account_code','ASC');
    $query  =   $this->db->get();
    return  $query->result_array();
}

Sorry for late reply but this is the perfect thing you want test before you do anything with. i wanted to create a view file for this but but it seems to be complicated and takes more time which at the moment i lake

EDITED

This solution might be racking you on the view as i have tested it How ever i have tried to combine these two queries and succeeded. The query might look like some thing complex but it is fetching the perfect results. Here it is

$data   =   array(
                  'debit_side.account_code    Code',
                  'group_concat(distinct accounts.name) as DebitAccount',
                  'group_concat(debit_side.amount) as DebitAmount',
                  'group_concat(transaction_info.voucher_date) as DebitVoucherDate',
                  '(SELECT group_concat(distinct accounts.name) as CreditAccount FROM (accounts)
                    left JOIN debit_side ON accounts.code = debit_side.account_code
                    left JOIN credit_side ON debit_side.transaction_id_dr = credit_side.transaction_id_cr
                    left JOIN transaction_info ON transaction_info.transaction_id = credit_side.transaction_id_cr
                    group by credit_side.account_code
                    having credit_side.account_code = `Code`) as CreditAccount',
                  '(SELECT  group_concat(credit_side.amount) as CreditAmount FROM (accounts)
                     left JOIN debit_side ON accounts.code = debit_side.account_code
                     left JOIN credit_side ON debit_side.transaction_id_dr = credit_side.transaction_id_cr
                     left JOIN transaction_info ON transaction_info.transaction_id = credit_side.transaction_id_cr
                   group by credit_side.account_code
                   having credit_side.account_code = `Code`) as CreditAmount',  
                  '(SELECT  group_concat(transaction_info.voucher_date) as CreditVoucherDate FROM (accounts)
                    left JOIN debit_side ON accounts.code = debit_side.account_code
                    left JOIN credit_side ON debit_side.transaction_id_dr = credit_side.transaction_id_cr
                    left JOIN transaction_info ON transaction_info.transaction_id = credit_side.transaction_id_cr
                    group by credit_side.account_code
                    having credit_side.account_code = `Code`) as CreditVoucherDate'

                );
    $this->db->select($data);
    $this->db->from('accounts');
    $this->db->join('credit_side','accounts.code = credit_side.account_code','left');
    $this->db->join('debit_side','debit_side.transaction_id_dr = credit_side.transaction_id_cr','left');
    $this->db->join('transaction_info','transaction_info.transaction_id = credit_side.transaction_id_cr','left');
    $this->db->group_by('debit_side.account_code');
    $this->db->order_by('debit_side.account_code','ASC');
    $query  =   $this->db->get();
    return  $query->result_array();

Although this query works fine but here is a modified and optimized version of this query i really learned things from this query you should take a look at this too

Strange Behaviour of Group by in Query which needs to be optimized



来源:https://stackoverflow.com/questions/10191481/how-to-create-general-ledger-t-account-using-php-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!