2012-04-17 94 views
2

我正在嘗試創建一個複式財務會計系統。我已經完成了它的數據庫設計,請查看這個鏈接來查看我的數據庫圖。 http://i39.tinypic.com/juhbv6.png如何使用PHP創建總帳/ T帳戶Mysql

我已經嘗試了很多開發mysql查詢來生成總賬,但我沒有在哪裏關閉。我所能做的只是創建一個帳戶,只有一個帳戶需要創建無限帳戶。

我試圖查詢以下:

$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'); 

無法寫入MySQL查詢可產生臺賬,因爲我已經寫下來,以創建總帳/筆賬邏輯所有帳戶後。

現在,你可以幫我用mysql查詢嗎?

請檢查數據庫之前,通過以下。 感謝提前:)

  1. 獲取從賬戶accounts.name AS AccountHead,accounts.code

  2. 轉到表debit_sideget debit_side.account_code, 如果debit_side.account_code = accounts.code然後得到credit_side.account_code AS AccountName1(但是當我將在PHP回聲,我想要得到的名稱,而不是代碼本身)和credit_side.amount AS Amount1,SUM(credit_side.amount) AS TotalAmount1從credit_side其中debit_side.transaction_id_dr = credit_side.transaction_id_crtransaction_info.voucher_date是在日期1和date2 WHERE transaction_info.transaction_id = debit_side.transaction_id_dr

  3. 結束於後間荷蘭國際集團的第二個步驟去表credit_sideget credit_side.account_code
    如果credit_side.account_code = accounts.code然後得到debit_side.account_code AS AccountName2(但是當我將在PHP回聲,我想要得到的名稱,而不是代碼本身)和debit_side.amount AS AMOUNT2,SUM(debit_side.amount) AS TotalAmount2從debit_side其中credit_side.transaction_id_cr = debit_side.transaction_id_drtransaction_info.voucher_date是在日期1和date2 之間WHERE transaction_info.transaction_id = credit_side.transaction_id_cr

現在,在視圖文件我的目標是具有弗洛翼:

<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> 

總賬樣品

enter image description here

+0

看看我認爲這個問題可能太過本地化(也太長)。你能編輯它來縮小你遇到的確切問題嗎?人們不會從下載站點下載你的SQL(而是使用pastie.org),也不會閱讀PDF - 讓讀者更容易,並且更簡潔':)''。 – halfer 2012-04-19 09:46:37

+0

我很抱歉,..好吧我要刪除不必要的東西。 – 2012-04-19 09:48:21

回答

5
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(); 
} 

對不起已故的答覆,但這個是你想測試你做任何事情之前完美的東西。我想爲這一點,但一個視圖文件,但它看起來是複雜並且需要更多這在目前我湖

EDITED

這種解決方案可能會在視圖上費盡你,因爲我已經測試它是如何的時間我曾嘗試將這兩個查詢結合起來併成功。查詢可能看起來像一些複雜的事情,但它正在取得完美的結果。 這

$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(); 

雖然此查詢工作正常,但這裏是此查詢我真的學到東西,從這個查詢的修改和優化的版本,你應該在這個太

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

+0

感謝raheel shan,我試過你的代碼;它產生的結果不是總賬。我試圖找出問題,我很快就會回來報告。 :)謝謝:) – 2012-04-18 07:30:05

+0

@ raheel山。我一直在玩你的劇本,但不能讓它工作,所以這次我寫下了實現我想要實現的邏輯。我相信這次你可以解決這個問題,因爲書面邏輯會給你一個確切的想法,我一直在尋找什麼。那麼我應該把它作爲一個新問題發佈還是編輯這個問題?請回復。謝謝:) – 2012-04-19 09:14:59

+0

好的編輯這個問題,但不刪除以前的數據可能有幫助 – 2012-04-19 09:18:12