Eliminated non-static method calls and other bulk fixes to fix php5 warnings
[fa-stable.git] / gl / includes / db / gl_db_trans.inc
index c956a5e25db678932fdd93f8b97663cbcb2eefc7..723ea087aa1634c69d93fd3c519b549767718423 100644 (file)
@@ -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=".ST_WORKORDER." AND ".TB_PREF."gl_trans.type_no=$trans_id
+               AND ".TB_PREF."gl_trans.person_type_id=".PT_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(ctype>0 AND ctype<".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)
@@ -217,8 +260,8 @@ function add_gl_tax_details($gl_code, $trans_type, $trans_no, $amount, $ex_rate,
        if(!$tax_type) return;  // $gl_code is not tax account
        
        $tax = get_tax_type($tax_type);
-       if ($gl_code == $tax['sales_gl_code']) 
-               $amount = -$amount;
+       //if ($gl_code == $tax['sales_gl_code']) 
+       //      $amount = -$amount;
        // we have to restore net amount as we cannot know the base amount
        if ($tax['rate'] == 0) {
 //             display_warning(_("You should not post gl transactions  
@@ -286,14 +329,14 @@ function get_tax_summary($from, $to)
        $todate = date2sql($to);
 
        $sql = "SELECT 
-                               SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
-                               IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, net_amount*ex_rate,0)) net_output,
-                               SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
-                               IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, amount*ex_rate,0)) payable,
-                               SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
-                               IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, 0, net_amount*ex_rate)) net_input,
-                               SUM(IF( trans_type=1 || trans_type=11 || trans_type=20,-1,1)*
-                               IF(trans_type=0 || trans_type=2 || trans_type=10 || trans_type=11, 0, amount*ex_rate)) collectible,
+                               SUM(IF(trans_type=11 || trans_type=20,-1,1)*
+                               IF(trans_type=2 || trans_type=10 || trans_type=11, net_amount*ex_rate,0)) net_output,
+                               SUM(IF(trans_type=11 || trans_type=20,-1,1)*
+                               IF(trans_type=2 || trans_type=10 || trans_type=11, amount*ex_rate,0)) payable,
+                               SUM(IF(trans_type=11 || trans_type=20,-1,1)*
+                               IF(trans_type=2 || trans_type=10 || trans_type=11, 0, net_amount*ex_rate)) net_input,
+                               SUM(IF(trans_type=11 || trans_type=20,-1,1)*
+                               IF(trans_type=2 || trans_type=10 || trans_type=11, 0, amount*ex_rate)) collectible,
                                taxrec.rate,
                                ttype.id,
                                ttype.name
@@ -313,6 +356,8 @@ function get_tax_summary($from, $to)
 //
 function write_journal_entries(&$cart, $reverse)
 {
+       global $Refs;
+
        $date_ = $cart->tran_date;
        $ref   = $cart->reference;
        $memo_ = $cart->memo_;
@@ -345,12 +390,12 @@ function write_journal_entries(&$cart, $reverse)
        }
                // 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_);
+                       ($journal_item->amount < 0.0 ? 2 : 1), $trans_id, -$journal_item->amount, 1, $date_, $memo_);
        }
        
        if ($new) {
                add_comments($trans_type, $trans_id, $date_, $memo_);
-               references::save($trans_type, $trans_id, $ref);
+               $Refs->save($trans_type, $trans_id, $ref);
        } else
                update_comments($trans_type, $trans_id, null, $memo_);
 
@@ -376,18 +421,18 @@ function write_journal_entries(&$cart, $reverse)
                if ($is_bank_to)
                {
                        add_bank_trans($trans_type, $trans_id_reverse, $is_bank_to, $ref,
-                               $reversingDate, $journal_item->amount,
+                               $reversingDate, -$journal_item->amount,
                                0, "", get_company_currency(),
                                "Cannot insert a destination bank transaction");
                }
                        // 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, $reversingDate, $memo_);
+                               ($journal_item->amount < 0.0 ? 2 : 1), $trans_id, $journal_item->amount, 1, $date, $memo_);
        }
 
        add_comments($trans_type, $trans_id_reverse, $reversingDate, $memo_);
 
-       references::save($trans_type, $trans_id_reverse, $ref);
+       $Refs->save($trans_type, $trans_id_reverse, $ref);
                add_audit_trail($trans_type, $trans_id_reverse, $reversingDate);
        }