Cleanup: various random sql queries found in UI files moved to database interface...
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Sat, 27 Dec 2014 20:01:24 +0000 (21:01 +0100)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Sun, 28 Dec 2014 21:04:09 +0000 (22:04 +0100)
24 files changed:
admin/crm_categories.php
admin/db/company_db.inc
admin/db/transactions_db.inc
admin/inst_theme.php
gl/includes/db/gl_db_accounts.inc
gl/includes/db/gl_db_bank_accounts.inc
gl/includes/db/gl_db_rates.inc
includes/banking.inc
includes/db/crm_contacts_db.inc
includes/db/references_db.inc
includes/main.inc
includes/references.inc
includes/systypes.inc [deleted file]
includes/types.inc
manufacturing/work_order_entry.php
purchasing/includes/db/po_db.inc
purchasing/includes/db/suppliers_db.inc
purchasing/includes/ui/po_ui.inc
reporting/includes/doctext.inc
sales/customer_invoice.php
sales/includes/db/customers_db.inc
sales/includes/db/sales_order_db.inc
sales/inquiry/sales_orders_view.php
sales/sales_order_entry.php

index 372faa71637995cff71b84c3f94d9658f5060d93..59166a3a3f9a91f266b04f06f340e47937d71306 100644 (file)
@@ -52,20 +52,11 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
        }
 } 
 
-function key_in_crm_contacts($id) // extra function for testing foreign concatenated key. Joe 02.09.2013.
-{
-       $row = get_crm_category($id);
-       $sql = "SELECT COUNT(*) FROM ".TB_PREF."crm_contacts WHERE type='".$row['type']."' AND action='".$row['action']."'";
-       $result = db_query($sql, "check relations for crm_contacts failed");
-       $contacts = db_fetch($result);
-       return $contacts[0];
-}
-
 if ($Mode == 'Delete')
 {
        $cancel_delete = 0;
 
-       if (key_in_crm_contacts($selected_id))
+       if (is_crm_category_used($selected_id))
        {
                $cancel_delete = 1;
                display_error(_("Cannot delete this category because there are contacts related to it."));
index d0f85e4006e6095aa658c1830438c9f45d703527..e1ff68aad48c59a6e1026a32cbfc47cda1a3b1c5 100644 (file)
@@ -193,4 +193,27 @@ function key_in_foreign_table($id, $tables, $stdkey)
        return $count[0];
 }
 
+//---------------------------------------------------------------------------------------------
+//
+// Resets $theme references in users records to 'default'.
+//
+function clean_user_themes($theme)
+{
+       global $db_connections, $db, $installed_extensions;
+
+       $comp = user_company();
+
+       foreach ($db_connections as $n => $conn) {
+               $db = $_SESSION["wa_current_user"]->set_db_connection($n);
+               $sql = "UPDATE {$conn['tbpref']}users SET theme='default' WHERE theme='$theme'";
+               if (!db_query($sql, 'Cannot update user theme settings'))
+                       return false;
+       }
+       $db = $_SESSION["wa_current_user"]->set_db_connection($comp);
+
+       $_SESSION['wa_current_user']->prefs->theme = 'default';
+       return true;
+}
+
+
 ?>
\ No newline at end of file
index 19d22970c68b0c1f294f3d5bacd740735fdf7725..f59934fb8cbb9142d6725c1b7afb085111fc82d6 100644 (file)
@@ -64,4 +64,163 @@ function transaction_exists($trans_type, $trans_no)
 {
        return db_num_rows(db_query(get_sql_for_view_transactions($trans_type, $trans_no, $trans_no, $dummy)));
 }
+
+//
+//     Returns counterparty (supplier/customer) name for selected transaction.
+//
+function get_counterparty_name($trans_type, $trans_no, $full=true)
+{
+       switch($trans_type)
+       {
+               case ST_SALESORDER:
+               case ST_SALESQUOTE:
+                       $sql = "SELECT order.customer_id as person_id, debtor.name as name
+                       FROM ".TB_PREF."sales_orders order, ".TB_PREF."debtors_master debtor
+                       WHERE order_no=".db_escape($trans_no)." AND trans_type=".db_escape($trans_type)
+                       ." AND order.debtor_no=debtor.debtor_no";
+                       break;
+
+               case ST_RECEIPTINVOICE :
+               case ST_SALESINVOICE :
+               case ST_CUSTCREDIT :
+               case ST_CUSTPAYMENT :
+               case ST_CUSTDELIVERY :
+                       $sql = "SELECT trans.debtor_no as person_id, debtor.name as name
+                       FROM ".TB_PREF."debtor_trans trans, ".TB_PREF."debtors_master debtor
+                       WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
+                       ." AND trans.debtor_no=debtor.debtor_no";
+                       break;
+
+               case ST_PURCHORDER :
+                       $sql = "SELECT order.supplier_id as person_id, supp.supp_name as name
+                       FROM ".TB_PREF."purch_orders order, ".TB_PREF."suppliers supp
+                       WHERE order_no=".db_escape($trans_no)
+                       ." AND order.supplier_id=supp.supplier_id";
+                       break;
+
+               case ST_SUPPINVOICE :
+               case ST_SUPPCREDIT :
+               case ST_SUPPAYMENT :
+                       $sql = "SELECT trans.supplier_id as person_id, supp.supp_name as name
+                       FROM ".TB_PREF."supp_trans trans, ".TB_PREF."suppliers supp
+                       WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
+                       ." AND trans.supplier_id=supp.supplier_id";
+                       break;
+
+               case ST_SUPPRECEIVE :
+                       $sql = "SELECT trans.supplier_id as person_id, supp.supp_name as name
+                       FROM ".TB_PREF."grn_batch trans, ".TB_PREF."suppliers supp
+                       WHERE id=".db_escape($trans_no)
+                       ." AND trans.supplier_id=supp.supplier_id";
+                       break;
+
+               case ST_BANKPAYMENT :
+               case ST_BANKDEPOSIT :
+                       $sql = "SELECT trans.debtor_no as person_id, debtor.name as name
+                       FROM ".TB_PREF."debtor_trans trans, ".TB_PREF."debtors_master debtor
+                       WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
+                       ." AND trans.debtor_no=debtor.debtor_no
+                       UNION
+                               SELECT trans.supplier_id as person_id, supp.supp_name as name
+                       FROM ".TB_PREF."supp_trans trans, ".TB_PREF."suppliers supp
+                       WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
+                       ." AND trans.supplier_id=supp.supplier_id";
+                       break;
+
+               case ST_JOURNAL:        // FIXME - this one can have multiply counterparties of various types depending on person_type_id
+
+               default: 
+               /*      // internal operations
+               case ST_WORKORDER :
+               case ST_INVADJUST : // GRN/DN returns ?
+               case ST_BANKTRANSFER :
+               case ST_LOCTRANSFER :
+               case ST_MANUISSUE :
+               case ST_MANURECEIVE :
+               case ST_COSTUPDATE :
+               */
+                       return null;
+       }
+
+       $result = db_query($sql, 'cannot retrieve counterparty name');
+       if (db_num_rows($result))
+       {
+               $row = db_fetch($result);
+               return sprintf("[%05s] %s", $row['person_id'], $row['name']);
+       }
+
+       return '';
+}
+
+
+//-----------------------------------------------------------------------------------------
+//     Returns next transaction number.
+//     Used only for transactions stored in tables without autoincremented key.
+//
+
+function get_next_trans_no ($trans_type){
+
+       $st = get_systype_db_info($trans_type);
+
+       if (!($st && $st[0] && $st[2])) {
+               // this is in fact internal error condition.
+               display_error('Internal error: invalid type passed to get_next_trans_no()');
+               return 0;
+       }
+       $sql1 = "SELECT MAX(`$st[2]`) as last_no FROM $st[0]";
+       if ($st[1] != null)
+                $sql1 .= " WHERE `$st[1]`=".db_escape($trans_type);
+
+       // check also in voided transactions (some transactions like location transfer are removed completely)
+       $sql2 = "SELECT MAX(`id`) as last_no FROM ".TB_PREF."voided WHERE `type`=".db_escape($trans_type);
+
+       $sql = "SELECT max(last_no) last_no FROM ($sql1 UNION $sql2) a";
+    $result = db_query($sql,"The next transaction number for $trans_type could not be retrieved");
+    $myrow = db_fetch_row($result);
+
+    return $myrow[0] + 1;
+}
+
+//-----------------------------------------------------------------------------
+
+function get_systype_db_info($type)
+{
+       switch ($type)
+       {
+        case     ST_JOURNAL      : return array("".TB_PREF."gl_trans", "type", "type_no", null, "tran_date");
+        case     ST_BANKPAYMENT  : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
+        case     ST_BANKDEPOSIT  : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
+        case     3               : return null;
+        case     ST_BANKTRANSFER : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
+        case     ST_SALESINVOICE : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
+        case     ST_CUSTCREDIT   : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
+        case     ST_CUSTPAYMENT  : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
+        case     ST_CUSTDELIVERY : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
+        case     ST_LOCTRANSFER  : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
+        case     ST_INVADJUST    : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
+        case     ST_PURCHORDER   : return array("".TB_PREF."purch_orders", null, "order_no", "reference", "ord_date");
+        case     ST_SUPPINVOICE  : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
+        case     ST_SUPPCREDIT   : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
+        case     ST_SUPPAYMENT   : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
+        case     ST_SUPPRECEIVE  : return array("".TB_PREF."grn_batch", null, "id", "reference", "delivery_date");
+        case     ST_WORKORDER    : return array("".TB_PREF."workorders", null, "id", "wo_ref", "released_date");
+        case     ST_MANUISSUE    : return array("".TB_PREF."wo_issues", null, "issue_no", "reference", "issue_date");
+        case     ST_MANURECEIVE  : return array("".TB_PREF."wo_manufacture", null, "id", "reference", "date_");
+        case     ST_SALESORDER   : return array("".TB_PREF."sales_orders", "trans_type", "order_no", "reference", "ord_date");
+        case     31              : return array("".TB_PREF."service_orders", null, "order_no", "cust_ref", "date");
+        case     ST_SALESQUOTE   : return array("".TB_PREF."sales_orders", "trans_type", "order_no", "reference", "ord_date");
+        case    ST_DIMENSION    : return array("".TB_PREF."dimensions", null, "id", "reference", "date_");
+        case     ST_COSTUPDATE   : return array("".TB_PREF."gl_trans", "type", "type_no", null, "tran_date");
+       }
+
+       display_db_error("invalid type ($type) sent to get_systype_db_info", "", true);
+}
+
+function get_systypes()
+{
+       $sql = "SELECT * FROM ".TB_PREF."sys_types";
+       $result = db_query($sql, "could not query systypes table");
+       return $result;
+}
+
 ?>
index af640e883b915f30f86348804f2aeb85334ce8e9..01f6ca1ad698fc5db76aa888313db82c3d32ee8e 100644 (file)
@@ -25,28 +25,9 @@ include_once($path_to_root . "/admin/db/maintenance_db.inc");
 include_once($path_to_root . "/includes/ui.inc");
 
 //---------------------------------------------------------------------------------------------
-// If theme is used in customer record set to default
-//
-function clean_user_themes($id)
-{
-       global $db_connections, $db, $installed_extensions;
-
-       $theme = $installed_extensions[$id]['package'];
-       $comp = user_company();
-
-       foreach ($db_connections as $n => $conn) {
-               $db = $_SESSION["wa_current_user"]->set_db_connection($n);
-               $sql = "UPDATE {$conn['tbpref']}users SET theme='default' WHERE theme='$theme'";
-               if (!db_query($sql, 'Cannot update user theme settings'))
-                       return false;
-       }
-       $db = $_SESSION["wa_current_user"]->set_db_connection($comp);
-
-       $_SESSION['wa_current_user']->prefs->theme = 'default';
-       return true;
-}
 
-if (($id = find_submit('Delete', false)) && clean_user_themes($id))
+if (($id = find_submit('Delete', false))
+       && clean_user_themes($installed_extensions[$id]['package']))
 {
        $extensions = get_company_extensions();
        $theme = $extensions[$id]['package'];
index ce11a325607e6bfb2807ec86cc991f371e714de0..00b8fbe34698076501bd17479bf01c3a7ddda7ea 100644 (file)
@@ -227,4 +227,22 @@ function gl_account_in_bank_accounts($acc)
        $myrow = db_fetch_row($result);
        return ($myrow[0] > 0); 
 }
-?>
\ No newline at end of file
+
+//----------------------------------------------------------------------------------
+//     Check if given account is used by any bank_account. 
+//     Returns id of first bank_account using account_code, null otherwise.
+//
+//     Keep in mind that direct posting to bank account is depreciated
+//     because we have no way to select right bank account if 
+//     there is more than one using given gl account.
+//
+function is_bank_account($account_code)
+{
+       $sql= "SELECT id FROM ".TB_PREF."bank_accounts WHERE account_code=".db_escape($account_code);
+       $result = db_query($sql, "checking account is bank account");
+       if (db_num_rows($result) > 0) {
+               $acct = db_fetch($result);
+               return $acct['id'];
+       } else
+               return false;
+}
index a4ffd9862665bf2837af43fb487619d47d0453c4..5facce63e4ec10f72b3159d12ecadebe98223b4f 100644 (file)
@@ -308,7 +308,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 +317,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 +378,15 @@ function identify_bank_account($account_number)
                return null;
 }
 
+//----------------------------------------------------------------------------------
+
+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];
+}
+
 ?>
\ No newline at end of file
index c37eaa038897b44809c27bcf48a748bd2988bb71..9a64fcfa9e698d600466ecac6623e90b25891ae0 100644 (file)
@@ -33,6 +33,25 @@ function get_date_exchange_rate($curr_code, $date_)
                return $row[0];
 }
 
+/*
+       Return last exchange rate of $currency not later than $date.
+*/
+function get_last_exchange_rate($curr_code, $date_)
+{
+       $date = date2sql($date_);
+
+       $sql = "SELECT rate_buy, max(date_) as date_ FROM ".TB_PREF."exchange_rates WHERE curr_code = ".db_escape($curr_code)."
+                               AND date_ <= '$date' GROUP BY rate_buy ORDER BY date_ Desc LIMIT 1";
+
+       $result = db_query($sql, "could not query exchange rates");
+
+
+       if (db_num_rows($result) == 0)
+               return false;
+
+       return db_fetch_row($result);
+}
+
 //---------------------------------------------------------------------------------------------
 
 function update_exchange_rate($curr_code, $date_, $buy_rate, $sell_rate)
index 53af3598b8580b7c3263d6fb46937a201e24a08b..515da8b0bd0d79ddea372f41c20b80d8a56c0bf6 100644 (file)
 ***********************************************************************/
 include_once($path_to_root . "/gl/includes/gl_db.inc");
 
-//----------------------------------------------------------------------------------
-//     Check if given account is used by any bank_account. 
-//     Returns id of first bank_account using account_code, null otherwise.
-//
-//     Keep in mind that direct posting to bank account is depreciated
-//     because we have no way to select right bank account if 
-//     there is more than one using given gl account.
-//
-function is_bank_account($account_code)
-{
-       $sql= "SELECT id FROM ".TB_PREF."bank_accounts WHERE account_code=".db_escape($account_code);
-       $result = db_query($sql, "checking account is bank account");
-       if (db_num_rows($result) > 0) {
-               $acct = db_fetch($result);
-               return $acct['id'];
-       } else
-               return false;
-}
-
 //----------------------------------------------------------------------------------
 
 function is_company_currency($currency)
@@ -46,54 +27,15 @@ function get_company_currency()
 
 //----------------------------------------------------------------------------------
 
-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];
-}
-
-//----------------------------------------------------------------------------------
-
-function get_customer_currency($customer_id)
-{
-    $sql = "SELECT curr_code FROM ".TB_PREF."debtors_master WHERE debtor_no = ".db_escape($customer_id);
-
-       $result = db_query($sql, "Retreive currency of customer $customer_id");
-
-       $myrow=db_fetch_row($result);
-       return $myrow[0];
-}
-
-//----------------------------------------------------------------------------------
-
-function get_supplier_currency($supplier_id)
-{
-    $sql = "SELECT curr_code FROM ".TB_PREF."suppliers WHERE supplier_id = ".db_escape($supplier_id);
-
-       $result = db_query($sql, "Retreive currency of supplier $supplier_id");
-
-       $myrow=db_fetch_row($result);
-       return $myrow[0];
-}
-
-//----------------------------------------------------------------------------------
-
 function get_exchange_rate_from_home_currency($currency_code, $date_)
 {
        if ($currency_code == get_company_currency() || $currency_code == null)
                return 1.0000;
 
-       $date = date2sql($date_);
-
-       $sql = "SELECT rate_buy, max(date_) as date_ FROM ".TB_PREF."exchange_rates WHERE curr_code = ".db_escape($currency_code)."
-                               AND date_ <= '$date' GROUP BY rate_buy ORDER BY date_ Desc LIMIT 1";
 
-       $result = db_query($sql, "could not query exchange rates");
+       $rate = get_last_exchange_rate($currency_code, $date_);
 
-       if (db_num_rows($result) == 0)
+       if (!$rate)
        {
                // no stored exchange rate, just return 1
                display_error(
@@ -102,8 +44,7 @@ function get_exchange_rate_from_home_currency($currency_code, $date_)
                return 1.000;
        }
 
-       $myrow = db_fetch_row($result);
-       return $myrow[0];
+       return $rate['rate_buy'];
 }
 
 //----------------------------------------------------------------------------------
index 8623c8e0043525755a07ff8a2248ef76dbc7556e..a226e496c81f3a583a0eb8c786228dd84e3bdd39 100644 (file)
@@ -300,4 +300,17 @@ function get_crm_contact($id)
        return $ret;
 }
 
+/*
+        Check for whether category is used in contacts.
+*/
+function is_crm_category_used($id)
+{
+       $row = get_crm_category($id);
+       $sql = "SELECT COUNT(*) FROM ".TB_PREF."crm_contacts WHERE type='".$row['type']."' AND action='".$row['action']."'";
+       $result = db_query($sql, "check relations for crm_contacts failed");
+       $contacts = db_fetch($result);
+       return $contacts[0];
+}
+
+
 ?>
\ No newline at end of file
index ca086541bb55ce019c778e1ab48a8eb16a7e346d..87aa6515f797b3c9719d946933d9e7b8caef0dba 100644 (file)
@@ -76,4 +76,48 @@ function get_next_reference($type)
     return $row[0];
 }
 
+//----------------------------------------------------------------------------
+//
+//     Check if reference was not used so far (for other transaction than $trans_no)
+//
+function is_new_reference($ref, $type, $trans_no=0)
+{
+       $db_info = get_systype_db_info($type);
+       $db_name = $db_info[0];
+       $db_type = $db_info[1];
+       $db_trans = $db_info[2];
+       $db_ref = $db_info[3];
+       
+       $ref = db_escape(trim($ref));
+       $type = db_escape($type);
+       
+       if ($db_ref == null) { // journal or bank trans store references in refs table
+               $db_name = TB_PREF."refs";
+               $db_type = 'type';
+               $db_trans = 'id';
+               $db_ref = 'reference';
+       }
+
+       if ($db_type != null) {
+               $sql = "SELECT $db_ref FROM $db_name tbl
+                       LEFT JOIN ".TB_PREF."voided v ON 
+                               tbl.$db_type=v.type AND tbl.$db_trans=v.id
+                       WHERE $db_ref=$ref AND ISNULL(v.id)
+                               AND tbl.$db_type=$type";
+       } else {
+               $sql = "SELECT $db_ref ref FROM $db_name tbl
+                       LEFT JOIN ".TB_PREF."voided v ON 
+                               v.type=$type AND tbl.$db_trans=v.id
+                       WHERE $db_ref=$ref AND ISNULL(v.id)";
+       }
+       if ($trans_no)
+                       $sql .= " AND tbl.`$db_trans` != ".db_escape($trans_no);
+
+       $result = db_query($sql, "could not test for unique reference");
+
+       return (db_num_rows($result) == 0);
+
+}
+
+
 ?>
\ No newline at end of file
index 018fb2b0cdc9b0869d2418969b231d838518563c..2477a6c0f38bfba618f915960618a827783928b8 100644 (file)
@@ -12,7 +12,6 @@
 include_once($path_to_root . "/includes/db/connect_db.inc");
 
 include_once($path_to_root . "/includes/types.inc");
-include_once($path_to_root . "/includes/systypes.inc");
 include_once($path_to_root . "/includes/references.inc");
 include_once($path_to_root . "/includes/db/comments_db.inc");
 include_once($path_to_root . "/includes/db/sql_functions.inc");
index cd01ab6c67386032182dbda57e5a962390f1bea2..38bd84cdceb541470d38674b2b4f1eb638d5c187 100644 (file)
@@ -100,48 +100,4 @@ class references
        }
 }
 
-//----------------------------------------------------------------------------
-//
-//     Check if reference was not used so far (for other transaction than $trans_no)
-//
-function is_new_reference($ref, $type, $trans_no=0)
-{
-       $db_info = get_systype_db_info($type);
-       $db_name = $db_info[0];
-       $db_type = $db_info[1];
-       $db_trans = $db_info[2];
-       $db_ref = $db_info[3];
-       
-       $ref = db_escape(trim($ref));
-       $type = db_escape($type);
-       
-       if ($db_ref == null) { // journal or bank trans store references in refs table
-               $db_name = TB_PREF."refs";
-               $db_type = 'type';
-               $db_trans = 'id';
-               $db_ref = 'reference';
-       }
-
-       if ($db_type != null) {
-               $sql = "SELECT $db_ref FROM $db_name tbl
-                       LEFT JOIN ".TB_PREF."voided v ON 
-                               tbl.$db_type=v.type AND tbl.$db_trans=v.id
-                       WHERE $db_ref=$ref AND ISNULL(v.id)
-                               AND tbl.$db_type=$type";
-       } else {
-               $sql = "SELECT $db_ref ref FROM $db_name tbl
-                       LEFT JOIN ".TB_PREF."voided v ON 
-                               v.type=$type AND tbl.$db_trans=v.id
-                       WHERE $db_ref=$ref AND ISNULL(v.id)";
-       }
-       if ($trans_no)
-                       $sql .= " AND tbl.`$db_trans` != ".db_escape($trans_no);
-
-       $result = db_query($sql, "could not test for unique reference");
-
-       return (db_num_rows($result) == 0);
-
-}
-
-
 ?>
\ No newline at end of file
diff --git a/includes/systypes.inc b/includes/systypes.inc
deleted file mode 100644 (file)
index c1c17a4..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-/**********************************************************************
-    Copyright (C) FrontAccounting, LLC.
-       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/gpl-3.0.html>.
-***********************************************************************/
-
-//-----------------------------------------------------------------------------------------
-//     Returns next transaction number.
-//     Used only for transactions stored in tables without autoincremented key.
-//
-
-function get_next_trans_no ($trans_type){
-
-       $st = get_systype_db_info($trans_type);
-
-       if (!($st && $st[0] && $st[2])) {
-               // this is in fact internal error condition.
-               display_error('Internal error: invalid type passed to get_next_trans_no()');
-               return 0;
-       }
-       $sql1 = "SELECT MAX(`$st[2]`) as last_no FROM $st[0]";
-       if ($st[1] != null)
-                $sql1 .= " WHERE `$st[1]`=".db_escape($trans_type);
-
-       // check also in voided transactions (some transactions like location transfer are removed completely)
-       $sql2 = "SELECT MAX(`id`) as last_no FROM ".TB_PREF."voided WHERE `type`=".db_escape($trans_type);
-
-       $sql = "SELECT max(last_no) last_no FROM ($sql1 UNION $sql2) a";
-    $result = db_query($sql,"The next transaction number for $trans_type could not be retrieved");
-    $myrow = db_fetch_row($result);
-
-    return $myrow[0] + 1;
-}
-
-//-----------------------------------------------------------------------------
-
-function get_systype_db_info($type)
-{
-       switch ($type)
-       {
-        case     ST_JOURNAL      : return array("".TB_PREF."gl_trans", "type", "type_no", null, "tran_date");
-        case     ST_BANKPAYMENT  : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
-        case     ST_BANKDEPOSIT  : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
-        case     3               : return null;
-        case     ST_BANKTRANSFER : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
-        case     ST_SALESINVOICE : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
-        case     ST_CUSTCREDIT   : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
-        case     ST_CUSTPAYMENT  : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
-        case     ST_CUSTDELIVERY : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
-        case     ST_LOCTRANSFER  : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
-        case     ST_INVADJUST    : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
-        case     ST_PURCHORDER   : return array("".TB_PREF."purch_orders", null, "order_no", "reference", "ord_date");
-        case     ST_SUPPINVOICE  : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
-        case     ST_SUPPCREDIT   : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
-        case     ST_SUPPAYMENT   : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
-        case     ST_SUPPRECEIVE  : return array("".TB_PREF."grn_batch", null, "id", "reference", "delivery_date");
-        case     ST_WORKORDER    : return array("".TB_PREF."workorders", null, "id", "wo_ref", "released_date");
-        case     ST_MANUISSUE    : return array("".TB_PREF."wo_issues", null, "issue_no", "reference", "issue_date");
-        case     ST_MANURECEIVE  : return array("".TB_PREF."wo_manufacture", null, "id", "reference", "date_");
-        case     ST_SALESORDER   : return array("".TB_PREF."sales_orders", "trans_type", "order_no", "reference", "ord_date");
-        case     31              : return array("".TB_PREF."service_orders", null, "order_no", "cust_ref", "date");
-        case     ST_SALESQUOTE   : return array("".TB_PREF."sales_orders", "trans_type", "order_no", "reference", "ord_date");
-        case    ST_DIMENSION    : return array("".TB_PREF."dimensions", null, "id", "reference", "date_");
-        case     ST_COSTUPDATE   : return array("".TB_PREF."gl_trans", "type", "type_no", null, "tran_date");
-       }
-
-       display_db_error("invalid type ($type) sent to get_systype_db_info", "", true);
-}
-
-function get_systypes()
-{
-       $sql = "SELECT * FROM ".TB_PREF."sys_types";
-       $result = db_query($sql, "could not query systypes table");
-       return $result;
-}
-
-?>
\ No newline at end of file
index cd4e3dc3d18fd055c040c9177c033dbf2634c244..5b7b4a7af73588413e2e91cf46f2ae24af19a430 100644 (file)
@@ -140,7 +140,7 @@ function payment_person_name($type, $person_id, $full=true) {
 //
 //     Returns counterparty (supplier/customer) name for selected transaction.
 //
-function get_counterparty_name($trans_type, $trans_no, $full=true)
+function get_counterparty_name($trans_type, $trans_no)
 {
        switch($trans_type)
        {
index b9cfcba0442e653e9ca6ae5bf7c700cd939ffd73..4466355d610df8577d2d3133383791537364683e 100644 (file)
@@ -424,24 +424,23 @@ else
     date_row(_("Date") . ":", 'date_', '', true);
        hidden('RequDate', '');
 
-       $sql = "SELECT DISTINCT account_code FROM ".TB_PREF."bank_accounts";
-       $rs = db_query($sql,"could not get bank accounts");
-       $r = db_fetch_row($rs);
+       $bank_act = get_default_bank_account();
        if (!isset($_POST['Labour']))
        {
                $_POST['Labour'] = price_format(0);
-               $_POST['cr_lab_acc'] = $r[0];
+               $_POST['cr_lab_acc'] = $bank_act['account_code'];
        }
+
        amount_row($wo_cost_types[WO_LABOUR], 'Labour');
        gl_all_accounts_list_row(_("Credit Labour Account"), 'cr_lab_acc', null);
        if (!isset($_POST['Costs']))
        {
                $_POST['Costs'] = price_format(0);
-               $_POST['cr_acc'] = $r[0];
+               $_POST['cr_acc'] = $bank_act['account_code'];
        }
        amount_row($wo_cost_types[WO_OVERHEAD], 'Costs');
        gl_all_accounts_list_row(_("Credit Overhead Account"), 'cr_acc', null);
-       
+
 }
 
 if (get_post('released'))
index b33a2fa00b1556914d5f87b065a3a864a3d6b7d6..dac2ed5e211506560c03145cf4c2045b00b07dd4 100644 (file)
@@ -9,6 +9,38 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
+// ------------------------------------------------------------------------------
+
+function get_supplier_details_to_order(&$order, $supplier_id)
+{
+       $sql = "SELECT curr_code, supp_name, tax_group_id, supp.tax_included, supp.tax_algorithm,
+                       supp.credit_limit - Sum(IFNULL(IF(trans.type=".ST_SUPPCREDIT.", -1, 1) 
+                               * (ov_amount + ov_gst + ov_discount),0)) as cur_credit,
+                               terms.terms, terms.days_before_due, terms.day_in_following_month
+               FROM ".TB_PREF."suppliers supp
+                        LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id
+                        LEFT JOIN ".TB_PREF."payment_terms terms ON supp.payment_terms=terms.terms_indicator
+               WHERE supp.supplier_id = ".db_escape($supplier_id)."
+               GROUP BY
+                         supp.supp_name";
+
+       $result = db_query($sql, "The supplier details could not be retreived");
+       $myrow = db_fetch($result);
+
+       $order->credit = $myrow["cur_credit"];
+       $order->terms = array( 
+               'description' => $myrow['terms'],
+               'days_before_due' => $myrow['days_before_due'], 
+               'day_in_following_month' => $myrow['day_in_following_month'] );
+
+       $_POST['supplier_id'] = $supplier_id;
+       $_POST['supplier_name'] = $myrow["supp_name"];
+       $_POST['curr_code'] = $myrow["curr_code"];
+
+       $order->set_supplier($supplier_id, $myrow["supp_name"], $myrow["curr_code"], 
+               $myrow["tax_group_id"], $myrow["tax_included"], $myrow["tax_algorithm"]);
+}
+
 //----------------------------------------------------------------------------------------
 
 function delete_po($po)
index cab8eace7ba9eadb5090fe30f4296ef614c99989..6b7d0cde787265665cfc21c5a188a0bf6f5f0f43 100644 (file)
@@ -187,3 +187,14 @@ function update_supp_tax_algorithm($supplier_id, $tax_algorithm)
                . " WHERE supplier_id=".db_escape($supplier_id);
        return db_query($sql, "cannot update supplier's tax calculation algorithm");
 }
+
+function get_supplier_currency($supplier_id)
+{
+    $sql = "SELECT curr_code FROM ".TB_PREF."suppliers WHERE supplier_id = ".db_escape($supplier_id);
+
+       $result = db_query($sql, "Retreive currency of supplier $supplier_id");
+
+       $myrow=db_fetch_row($result);
+       return $myrow[0];
+}
+
index ba60460976fd8ad4037f84e2f1939df18c1db7e4..152df8f2059344d9319a6460746f89c5f2344799 100644 (file)
@@ -60,37 +60,6 @@ function copy_to_cart()
                        $cart->tax_overrides[$id] = user_numeric($_POST['mantax'][$id]); }
        }
 }
-// ------------------------------------------------------------------------------
-
-function get_supplier_details_to_order(&$order, $supplier_id)
-{
-       $sql = "SELECT curr_code, supp_name, tax_group_id, supp.tax_included, supp.tax_algorithm,
-                       supp.credit_limit - Sum(IFNULL(IF(trans.type=".ST_SUPPCREDIT.", -1, 1) 
-                               * (ov_amount + ov_gst + ov_discount),0)) as cur_credit,
-                               terms.terms, terms.days_before_due, terms.day_in_following_month
-               FROM ".TB_PREF."suppliers supp
-                        LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id
-                        LEFT JOIN ".TB_PREF."payment_terms terms ON supp.payment_terms=terms.terms_indicator
-               WHERE supp.supplier_id = ".db_escape($supplier_id)."
-               GROUP BY
-                         supp.supp_name";
-
-       $result = db_query($sql, "The supplier details could not be retreived");
-
-       $myrow = db_fetch($result);
-       $order->credit = $myrow["cur_credit"];
-       $order->terms = array( 
-               'description' => $myrow['terms'],
-               'days_before_due' => $myrow['days_before_due'], 
-               'day_in_following_month' => $myrow['day_in_following_month'] );
-
-       $_POST['supplier_id'] = $supplier_id;
-       $_POST['supplier_name'] = $myrow["supp_name"];
-       $_POST['curr_code'] = $myrow["curr_code"];
-
-       $order->set_supplier($supplier_id, $myrow["supp_name"], $myrow["curr_code"], 
-               $myrow["tax_group_id"], $myrow["tax_included"], $myrow["tax_algorithm"]);
-}
 
 //---------------------------------------------------------------------------------------------------
 
@@ -209,13 +178,9 @@ function display_po_header(&$order)
     {
        /*If this is the first time the form loaded set up defaults */
 
-        //$_POST['StkLocation'] = $_SESSION['UserStockLocation'];
-        $sql = "SELECT delivery_address, phone FROM ".TB_PREF."locations WHERE loc_code=".db_escape($_POST['StkLocation']);
-        $result = db_query($sql,"could not get location info");
-
-        if (db_num_rows($result) == 1)
+               $loc_row = get_item_location(get_post('StkLocation'));
+        if ($loc_row)
         {
-               $loc_row = db_fetch($result);
                $_POST['delivery_address'] = $loc_row["delivery_address"];
                        $Ajax->activate('delivery_address');
                $_SESSION['PO']->Location = $_POST['StkLocation'];
index de3082a9e1eadd6dfe906313b37786ac1219ccad..be391fde7392bb3905c77ca602327c691b11f678 100644 (file)
 
        if (!in_array($this->formData['doctype'], array(ST_STATEMENT, ST_WORKORDER)))
        {
-               $id = $this->formData['payment_terms'];
-               $sql = "SELECT terms, days_before_due FROM ".TB_PREF."payment_terms WHERE terms_indicator=".db_escape($id);
-               $result = db_query($sql,"could not get paymentterms");
-               $row = db_fetch($result);
+               $row = get_payment_terms($this->formData['payment_terms']);
                $Payment_Terms = _("Payment Terms") . ': ' . $row["terms"];
                if ($this->formData['doctype'] == ST_SALESINVOICE && $this->formData['prepaid'])
                        $this->formData['prepaid'] = ($row['days_before_due'] >= 0) ? 'final' : 'partial';
index 945ce9f1f8326d0a9806f75b8334e55705cd7f68..09d405eb415493cff7b6c09e33ada35ae7538f97 100644 (file)
@@ -66,12 +66,7 @@ if (isset($_GET['AddedID'])) {
 
        hyperlink_params("$path_to_root/sales/inquiry/sales_deliveries_view.php", _("Select Another &Delivery For Invoicing"), "OutstandingOnly=1");
 
-       $sql = "SELECT trans_type_from, trans_no_from FROM ".TB_PREF."cust_allocations
-                       WHERE trans_type_to=".ST_SALESINVOICE." AND trans_no_to=".db_escape($invoice_no);
-       $result = db_query($sql, "could not retrieve customer allocation");
-       $row = db_fetch($result);
-
-       if ($row === false)
+       if (!db_num_rows(get_allocatable_from_cust_transactions(null, $invoice_no, $trans_type)))
                hyperlink_params("$path_to_root/sales/customer_payments.php", _("Entry &customer payment for this invoice"),
                "SInvoice=".$invoice_no);
 
index 3b936a33e9388125b2b8033e79c4958a9566d529..61425b212937aff357fb3b99b274457784f2248b 100644 (file)
@@ -186,4 +186,15 @@ function get_customer_by_ref($reference)
        return db_fetch($result);
 }
 
-?>
\ No newline at end of file
+//----------------------------------------------------------------------------------
+
+function get_customer_currency($customer_id)
+{
+    $sql = "SELECT curr_code FROM ".TB_PREF."debtors_master WHERE debtor_no = ".db_escape($customer_id);
+
+       $result = db_query($sql, "Retreive currency of customer $customer_id");
+
+       $myrow=db_fetch_row($result);
+       return $myrow[0];
+}
+
index d02df7bd48e441a08373709a4893cb17c6f48e29..6ac1e9ddb02b04e658f56480cd8c725d79bc5e9f 100644 (file)
@@ -596,3 +596,13 @@ function is_sales_order_started($order_no)
        $result = db_fetch(db_query($sql, "cannot retrieve sales invoices for sales order"));
        return $result[0];
 }
+
+//---------------------------------------------------------------------------------------------
+//
+// Mark/unmark sales order as template.
+//
+function sales_order_set_template($id, $status)
+{
+       $sql = "UPDATE ".TB_PREF."sales_orders SET type = ".db_escape($status)." WHERE order_no=".db_escape($id);
+       db_query($sql, "Can't change sales order type");
+}
index ae9a666ea6a17d04eaca5e0bb13ccf3c792e7ef2..7955d6c41e44b861d6d6c55a5fbbbb809ae75947 100644 (file)
@@ -201,27 +201,17 @@ function invoice_prep_link($row)
                "/sales/customer_invoice.php?InvoicePrepayments=" .$row['order_no'], ICON_DOC) : '';
 }
 
-//---------------------------------------------------------------------------------------------
-// Update db record if respective checkbox value has changed.
-//
-function change_tpl_flag($id)
+$id = find_submit('_chgtpl');
+if ($id != -1)
 {
-       global  $Ajax;
-       
-       $sql = "UPDATE ".TB_PREF."sales_orders SET type = !type WHERE order_no=$id";
-
-       db_query($sql, "Can't change sales order type");
+       sales_order_set_template($id, check_value('chgtpl'.$id));
        $Ajax->activate('orders_tbl');
 }
 
-$id = find_submit('_chgtpl');
-if ($id != -1)
-       change_tpl_flag($id);
-
 if (isset($_POST['Update']) && isset($_POST['last'])) {
        foreach($_POST['last'] as $id => $value)
                if ($value != check_value('chgtpl'.$id))
-                       change_tpl_flag($id);
+                       sales_order_set_template($id, !check_value('chgtpl'.$id));
 }
 
 $show_dates = !in_array($_POST['order_view_mode'], array('OutstandingOnly', 'InvoiceTemplates', 'DeliveryTemplates'));
index f74d4eada7e435a6439e90fe76387ea6f15e890d..add2b7094175387a9fc74fca106b46940910bf32 100644 (file)
@@ -220,13 +220,10 @@ if (isset($_GET['AddedID'])) {
        submenu_print(_("&Print Sales Invoice"), ST_SALESINVOICE, $invoice."-".ST_SALESINVOICE, 'prtopt');
        submenu_print(_("&Email Sales Invoice"), ST_SALESINVOICE, $invoice."-".ST_SALESINVOICE, null, 1);
        set_focus('prtopt');
-       
-       $sql = "SELECT trans_type_from, trans_no_from FROM ".TB_PREF."cust_allocations
-                       WHERE trans_type_to=".ST_SALESINVOICE." AND trans_no_to=".db_escape($invoice);
-       $result = db_query($sql, "could not retrieve customer allocation");
-       $row = db_fetch($result);
+
+       $row = db_fetch(get_allocatable_from_cust_transactions(null, $invoice, ST_SALESINVOICE));
        if ($row !== false)
-               submenu_print(_("Print &Receipt"), $row['trans_type_from'], $row['trans_no_from']."-".$row['trans_type_from'], 'prtopt');
+               submenu_print(_("Print &Receipt"), $row['type'], $row['trans_no']."-".$row['type'], 'prtopt');
 
        display_note(get_gl_view_str(ST_SALESINVOICE, $invoice, _("View the GL &Journal Entries for this Invoice")),0, 1);