Global fixes in SQL queries aimed to make them more readible and easier for maintence.
[fa-stable.git] / gl / includes / db / gl_db_bank_accounts.inc
index c41e839200f98265905c0894d1480d615baad4ca..8716c77996a268174d26c20fb3da2213e3cc93b2 100644 (file)
@@ -76,7 +76,7 @@ function get_bank_account($id)
 
 //---------------------------------------------------------------------------------------------
 
-function get_bank_accounts($show_inactive)
+function get_bank_accounts($show_inactive=false)
 {
        $sql = "SELECT account.*, gl_account.account_name 
                FROM ".TB_PREF."bank_accounts account, ".TB_PREF."chart_master gl_account 
@@ -115,22 +115,22 @@ function get_bank_charge_account($id)
 
 //---------------------------------------------------------------------------------------------
 
-function add_quick_entry($description, $type, $base_amount, $base_desc, $bal_type)
+function add_quick_entry($description, $type, $base_amount, $base_desc, $bal_type, $usage)
 {
-       $sql = "INSERT INTO ".TB_PREF."quick_entries (description, type, base_amount, base_desc, bal_type) 
+       $sql = "INSERT INTO ".TB_PREF."quick_entries (description, type, base_amount, base_desc, bal_type, `usage`
        VALUES (".db_escape($description).", ".db_escape($type).", "
-               .db_escape($base_amount).", ".db_escape($base_desc).", ".db_escape($bal_type).")";
+               .db_escape($base_amount).", ".db_escape($base_desc).", ".db_escape($bal_type).", ".db_escape($usage).")";
 
        db_query($sql, "could not insert quick entry for $description");
 }
 
 //---------------------------------------------------------------------------------------------
 
-function update_quick_entry($selected_id, $description, $type, $base_amount, $base_desc, $bal_type)
+function update_quick_entry($selected_id, $description, $type, $base_amount, $base_desc, $bal_type, $usage)
 {
        $sql = "UPDATE ".TB_PREF."quick_entries SET description = ".db_escape($description).",
                type=".db_escape($type).", base_amount=".db_escape($base_amount)
-               .", base_desc=".db_escape($base_desc).", bal_type=".db_escape($bal_type)."
+               .", base_desc=".db_escape($base_desc).", bal_type=".db_escape($bal_type).", `usage`=".db_escape($usage)."
                WHERE id = ".db_escape($selected_id);
 
        db_query($sql, "could not update quick entry for $selected_id");
@@ -147,25 +147,25 @@ function delete_quick_entry($selected_id)
 
 //---------------------------------------------------------------------------------------------
 
-function add_quick_entry_line($qid, $action, $dest_id, $amount, $dim, $dim2)
+function add_quick_entry_line($qid, $action, $dest_id, $amount, $dim, $dim2, $memo)
 {
        $sql = "INSERT INTO ".TB_PREF."quick_entry_lines 
-               (qid, action, dest_id, amount, dimension_id, dimension2_id) 
+               (qid, action, dest_id, amount, dimension_id, dimension2_id, memo
        VALUES 
                (".db_escape($qid).", ".db_escape($action).",".db_escape($dest_id).",
-                       ".db_escape($amount).", ".db_escape($dim).", ".db_escape($dim2).")";
+                       ".db_escape($amount).", ".db_escape($dim).", ".db_escape($dim2).", ".db_escape($memo).")";
 
        db_query($sql, "could not insert quick entry line for $qid");
 }
 
 //---------------------------------------------------------------------------------------------
 
-function update_quick_entry_line($selected_id, $qid, $action, $dest_id, $amount, $dim, $dim2)
+function update_quick_entry_line($selected_id, $qid, $action, $dest_id, $amount, $dim, $dim2, $memo)
 {
        $sql = "UPDATE ".TB_PREF."quick_entry_lines SET qid = ".db_escape($qid)
                .", action=".db_escape($action).",
                dest_id=".db_escape($dest_id).", amount=".db_escape($amount)
-               .", dimension_id=".db_escape($dim).", dimension2_id=".db_escape($dim2)." 
+               .", dimension_id=".db_escape($dim).", dimension2_id=".db_escape($dim2).", memo=".db_escape($memo)."
                WHERE id = ".db_escape($selected_id);
 
        db_query($sql, "could not update quick entry line for $selected_id");
@@ -213,13 +213,10 @@ function get_quick_entry($selected_id)
 
 function get_quick_entry_lines($qid)
 {
-       $sql = "SELECT ".TB_PREF."quick_entry_lines.*, ".TB_PREF."chart_master.account_name,
-                       ".TB_PREF."tax_types.name as tax_name
-               FROM ".TB_PREF."quick_entry_lines
-               LEFT JOIN ".TB_PREF."chart_master ON
-                       ".TB_PREF."quick_entry_lines.dest_id = ".TB_PREF."chart_master.account_code
-               LEFT JOIN ".TB_PREF."tax_types ON
-                       ".TB_PREF."quick_entry_lines.dest_id = ".TB_PREF."tax_types.id
+       $sql = "SELECT line.*, coa.account_name, taxtype.name as tax_name
+               FROM ".TB_PREF."quick_entry_lines line
+                       LEFT JOIN ".TB_PREF."chart_master coa ON line.dest_id = coa.account_code
+                       LEFT JOIN ".TB_PREF."tax_types taxtype ON line.dest_id = taxtype.id
                WHERE 
                        qid=".db_escape($qid)." ORDER by id";
 
@@ -283,10 +280,9 @@ function get_sql_for_bank_account_reconcile($bank_account, $date)
        $sql = "SELECT  type, trans_no, ref, trans_date, 
                                amount, person_id, person_type_id, reconciled, id
                FROM ".TB_PREF."bank_trans
-               WHERE ".TB_PREF."bank_trans.bank_act = ".db_escape($bank_account) . "
+               WHERE bank_act = ".db_escape($bank_account) . "
                        AND (reconciled IS NULL OR reconciled='". date2sql($date) ."')
-               ORDER BY trans_date,".TB_PREF."bank_trans.id";
-// or  ORDER BY reconciled desc, trans_date,".TB_PREF."bank_trans.id";
+               ORDER BY trans_date, id";
        return $sql;
 }
 
@@ -308,7 +304,7 @@ function update_reconciled_values($reconcile_id, $reconcile_value, $reconcile_da
 }
 //---------------------------------------------------------------------------------------------
 
-function get_default_bank_account($curr)
+function get_default_bank_account($curr=null)
 {
        /* default bank account is selected as first found account from:
                . default account in $curr if any
@@ -317,6 +313,8 @@ function get_default_bank_account($curr)
                . first defined account in home currency
        */
        $home_curr = get_company_pref('curr_default');
+       if (!isset($curr))
+               $curr = $home_curr;
 
        $sql = "SELECT b.*, b.bank_curr_code='$home_curr' as fall_back FROM "
                .TB_PREF."bank_accounts b"
@@ -376,4 +374,14 @@ function identify_bank_account($account_number)
                return null;
 }
 
-?>
\ No newline at end of file
+//----------------------------------------------------------------------------------
+
+function get_bank_account_currency($id)
+{
+       $sql= "SELECT bank_curr_code FROM ".TB_PREF."bank_accounts WHERE id=".db_escape($id);
+       $result = db_query($sql, "retreive bank account currency");
+
+       $myrow = db_fetch_row($result);
+       return $myrow[0];
+}
+