Merged changes from main branch up to 2.1.3.
[fa-stable.git] / gl / includes / db / gl_db_trans.inc
index 4924674678837e119a6f8305e38e5527a97ca54e..d80dfe4789c1b627386a55041678fce79732f223 100644 (file)
@@ -1,13 +1,13 @@
 <?php
 /**********************************************************************
     Copyright (C) FrontAccounting, LLC.
-       Released under the terms of the GNU Affero General Public License,
-       AGPL, as published by the Free Software Foundation, either version 
-       of the License, or (at your option) any later version.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
-    See the License here <http://www.gnu.org/licenses/agpl-3.0.html>.
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
 //--------------------------------------------------------------------------------
 
@@ -138,6 +138,19 @@ function get_gl_trans($type, $trans_id)
 
 //--------------------------------------------------------------------------------
 
+function get_gl_wo_cost_trans($trans_id, $person_id=-1)
+{
+       $sql = "SELECT ".TB_PREF."gl_trans.*, ".TB_PREF."chart_master.account_name FROM ".TB_PREF."gl_trans, ".TB_PREF."chart_master
+               WHERE ".TB_PREF."chart_master.account_code=".TB_PREF."gl_trans.account
+               AND ".TB_PREF."gl_trans.type=".systypes::work_order()." AND ".TB_PREF."gl_trans.type_no=$trans_id
+               AND ".TB_PREF."gl_trans.person_type_id=".payment_person_types::WorkOrder();
+       if ($person_id != -1)
+               $sql .= " AND ".TB_PREF."gl_trans.person_id=$person_id";
+       $sql .= " AND amount < 0";      
+
+       return db_query($sql, "The gl transactions could not be retrieved");
+}
+
 function get_gl_balance_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
 {
        $from = date2sql($from_date);
@@ -184,6 +197,36 @@ function get_gl_trans_from_to($from_date, $to_date, $account, $dimension=0, $dim
        return $row[0];
 }
 
+//----------------------------------------------------------------------------------------------------
+function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true) 
+{
+       $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit, SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance 
+               FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,".TB_PREF."chart_types, ".TB_PREF."chart_class 
+               WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id 
+               AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
+               
+       if ($account != null)
+               $sql .= " account='$account' AND";
+       if ($dimension > 0)
+               $sql .= " dimension_id=$dimension AND";
+       if ($dimension2 > 0)
+               $sql .= " dimension2_id=$dimension2 AND";
+       $from_date = date2sql($from);
+       if ($from_incl)
+               $sql .= " tran_date >= '$from_date'  AND";
+       else
+               $sql .= " tran_date > IF(balance_sheet>0 AND balance_sheet<".CL_INCOME.", '0000-00-00', '$from_date') AND";
+       $to_date = date2sql($to);
+       if ($to_incl)
+               $sql .= " tran_date <= '$to_date' ";
+       else
+               $sql .= " tran_date < '$to_date' ";
+
+       $result = db_query($sql,"No general ledger accounts were returned");
+
+       return db_fetch($result);
+}
+
 //--------------------------------------------------------------------------------
 
 function get_budget_trans_from_to($from_date, $to_date, $account, $dimension=0, $dimension2=0)
@@ -307,15 +350,29 @@ function get_tax_summary($from, $to)
 //display_error($sql);
     return db_query($sql,"Cannot retrieve tax summary");
 }
+
 //--------------------------------------------------------------------------------
-function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
+// Write/update journal entries.
+//
+function write_journal_entries(&$cart, $reverse)
 {
-       begin_transaction();
+       $date_ = $cart->tran_date;
+       $ref   = $cart->reference;
+       $memo_ = $cart->memo_;
+       $new = $cart->order_id == 0;
+       $trans_type = $cart->trans_type;
+       $trans_id = $new ? get_next_trans_no($trans_type) : $cart->order_id;
 
-       $trans_type = systypes::journal_entry();
-       $trans_id = get_next_trans_no($trans_type);
+       begin_transaction();
+       
+       if($new) {
+               $cart->order_id = $trans_id;
+       } else {
+               void_journal_trans($trans_type, $trans_id, true);
+               delete_comments($trans_type, $trans_id);
+       }
 
-       foreach ($items as $journal_item)
+       foreach ($cart->gl_items as $journal_item)
        {
                // post to first found bank account using given gl acount code.
                $is_bank_to = is_bank_account($journal_item->code_id);
@@ -326,8 +383,7 @@ function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
        if ($is_bank_to)
        {
                add_bank_trans($trans_type, $trans_id, $is_bank_to, $ref,
-                       $date_, $journal_item->amount,
-                       0, "", get_company_currency(),
+                       $date_, $journal_item->amount,  0, "", get_company_currency(),
                        "Cannot insert a destination bank transaction");
        }
                // store tax details if the gl account is a tax account
@@ -335,20 +391,25 @@ function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
                        $trans_type, $trans_id, -$journal_item->amount, 1, $date_, $memo_);
        }
        
-       add_comments($trans_type, $trans_id, $date_, $memo_);
+       if ($new) {
+               add_comments($trans_type, $trans_id, $date_, $memo_);
+               references::save($trans_type, $trans_id, $ref);
+       } else
+               update_comments($trans_type, $trans_id, null, $memo_);
 
-       references::save($trans_type, $trans_id, $ref);
+       add_audit_trail($trans_type, $trans_id, $date_);
+
+       commit_transaction();
 
        if ($reverse)
        {
-
        //$reversingDate = date(user_date_display(),
        //      Mktime(0,0,0,get_month($date_)+1,1,get_year($date_)));
        $reversingDate = begin_month(add_months($date_, 1));
 
        $trans_id_reverse = get_next_trans_no($trans_type);
 
-       foreach ($items as $journal_item)
+       foreach ($cart->gl_items as $journal_item)
        {
                        $is_bank_to = is_bank_account($journal_item->code_id);
 
@@ -364,16 +425,15 @@ function add_journal_entries($items, $date_, $ref, $reverse, $memo_=null)
                }
                        // store tax details if the gl account is a tax account
                        add_gl_tax_details($journal_item->code_id, 
-                               $trans_type, $trans_id, $journal_item->amount, 1, $date, $memo_);
+                               $trans_type, $trans_id, $journal_item->amount, 1, $reversingDate, $memo_);
        }
 
        add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
 
        references::save($trans_type, $trans_id_reverse, $ref);
+               add_audit_trail($trans_type, $trans_id_reverse, $reversingDate);
        }
 
-       commit_transaction();
-
        return $trans_id;
 }
 
@@ -408,9 +468,9 @@ function void_journal_trans($type, $type_no)
 {
        begin_transaction();
 
-       void_gl_trans($type, $type_no, true);
-       void_bank_trans($type, $type_no,true);
-       void_trans_tax_details($type, $type_no);
+       void_bank_trans($type, $type_no, true);
+//     void_gl_trans($type, $type_no, true);    // this is done above
+//     void_trans_tax_details($type, $type_no); // ditto
 
        commit_transaction();
 }