Fixed an array_combine object issue.
[fa-stable.git] / includes / systypes.inc
index 0c9aa0483ee9186bfdff630759d35127017ef1d1..c1c17a4a6f3e95b15b5e7f2fa3270bdafbb7d21e 100644 (file)
@@ -1,24 +1,41 @@
 <?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>.
+***********************************************************************/
 
-Function get_next_trans_no ($trans_type){
+//-----------------------------------------------------------------------------------------
+//     Returns next transaction number.
+//     Used only for transactions stored in tables without autoincremented key.
+//
 
-    // sql to get the next display transaction number
-       //these are maintained in the table sys_types
-    // Also updates the transaction number
+function get_next_trans_no ($trans_type){
 
-    $sql = "SELECT type_no FROM ".TB_PREF."sys_types WHERE type_id = " . $trans_type;
+       $st = get_systype_db_info($trans_type);
 
-    $result = db_query($sql,"The next transaction number for $trans_type could not be retrieved");
-
-    $myrow = db_fetch_row($result);
-
-    $next_number = $myrow[0] + 1;
+       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);
 
-    $sql = "UPDATE ".TB_PREF."sys_types SET type_no = $next_number WHERE type_id = $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);
 
-    db_query($sql,"The next transaction number for $trans_type could not be updated");
+       $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 $next_number;
+    return $myrow[0] + 1;
 }
 
 //-----------------------------------------------------------------------------
@@ -27,27 +44,30 @@ function get_systype_db_info($type)
 {
        switch ($type)
        {
-        case      0 : return array("".TB_PREF."gl_trans", "type", "type_no", null, "tran_date");
-        case      1 : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
-        case      2 : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
-        case      3 : return null;
-        case      4 : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
-        case     10 : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
-        case     11 : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
-        case     12 : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
-        case     16 : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
-        case     17 : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
-        case     18 : return array("".TB_PREF."purch_orders", null, "order_no", "reference", "tran_date");
-        case     20 : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
-        case     21 : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
-        case     22 : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
-        case     25 : return array("".TB_PREF."grn_batch", null, "id", "reference", "delivery_date");
-        case     26 : return array("".TB_PREF."workorders", null, "id", "wo_ref", "released_date");
-        case     28 : return array("".TB_PREF."wo_issues", null, "issue_no", "reference", "issue_date");
-        case     29 : return array("".TB_PREF."wo_manufacture", null, "id", "reference", "date_");
-        case     30 : return array("".TB_PREF."sales_orders", null, "order_no", "customer_ref", "ord_date");
-        case    40 : return array("".TB_PREF."dimensions", null, "id", "reference", "date_");
-        case     35 : return null;
+        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);