f59934fb8cbb9142d6725c1b7afb085111fc82d6
[fa-stable.git] / admin / db / transactions_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 //--------------------------------------------------------------------------------------
14
15 function get_sql_for_view_transactions($filtertype, $from, $to, &$trans_ref)
16 {
17         $db_info = get_systype_db_info($filtertype);
18
19         if ($db_info == null)
20                 return "";
21
22         $table_name = $db_info[0];
23         $type_name = $db_info[1];
24         $trans_no_name = $db_info[2];
25         $trans_ref = $db_info[3];
26         $trans_date = $db_info[4];
27
28         $sql = "SELECT t.$trans_no_name as trans_no";
29
30         if ($trans_ref)
31                 $sql .= " ,t.$trans_ref as ref ";
32         else
33                 $sql .= ", r.reference as ref";
34         $sql .= ",t.$trans_date as trans_date";
35         if ($type_name)
36                 $sql .= ", t.$type_name as type";
37         $sql .= " FROM $table_name t LEFT JOIN ".TB_PREF."voided v ON"
38                 ." t.$trans_no_name=v.id AND v.type=$filtertype";
39         if (!$trans_ref)
40                 $sql .= " LEFT JOIN ".TB_PREF."refs r ON t.$trans_no_name=r.id AND r.type=$filtertype";
41
42         $sql .= " WHERE ISNULL(v.`memo_`)";
43         if ($from != null && $to != null)
44         {
45                 $sql .= " AND t.$trans_no_name >= ".db_escape($from). "
46                         AND  t.$trans_no_name <= ".db_escape($to);
47                 if ($type_name != null)
48                         $sql .= " AND t.`$type_name` = ".db_escape($filtertype);
49         }
50         elseif ($type_name != null)
51                 $sql .= " AND t.`$type_name` = ".db_escape($filtertype);
52
53         // the ugly hack below is necessary to exclude old gl_trans records lasting after edition,
54         // otherwise old data transaction can be retrieved instead of current one.
55         if ($table_name==TB_PREF.'gl_trans')
56                 $sql .= " AND t.`amount` <> 0";
57
58         $sql .= " GROUP BY ".($type_name ? "t.$type_name," : '')." t.$trans_no_name";
59         $sql .= " ORDER BY t.$trans_no_name DESC";
60         return $sql;
61 }
62
63 function transaction_exists($trans_type, $trans_no)
64 {
65         return db_num_rows(db_query(get_sql_for_view_transactions($trans_type, $trans_no, $trans_no, $dummy)));
66 }
67
68 //
69 //      Returns counterparty (supplier/customer) name for selected transaction.
70 //
71 function get_counterparty_name($trans_type, $trans_no, $full=true)
72 {
73         switch($trans_type)
74         {
75                 case ST_SALESORDER:
76                 case ST_SALESQUOTE:
77                         $sql = "SELECT order.customer_id as person_id, debtor.name as name
78                         FROM ".TB_PREF."sales_orders order, ".TB_PREF."debtors_master debtor
79                         WHERE order_no=".db_escape($trans_no)." AND trans_type=".db_escape($trans_type)
80                         ." AND order.debtor_no=debtor.debtor_no";
81                         break;
82
83                 case ST_RECEIPTINVOICE :
84                 case ST_SALESINVOICE :
85                 case ST_CUSTCREDIT :
86                 case ST_CUSTPAYMENT :
87                 case ST_CUSTDELIVERY :
88                         $sql = "SELECT trans.debtor_no as person_id, debtor.name as name
89                         FROM ".TB_PREF."debtor_trans trans, ".TB_PREF."debtors_master debtor
90                         WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
91                         ." AND trans.debtor_no=debtor.debtor_no";
92                         break;
93
94                 case ST_PURCHORDER :
95                         $sql = "SELECT order.supplier_id as person_id, supp.supp_name as name
96                         FROM ".TB_PREF."purch_orders order, ".TB_PREF."suppliers supp
97                         WHERE order_no=".db_escape($trans_no)
98                         ." AND order.supplier_id=supp.supplier_id";
99                         break;
100
101                 case ST_SUPPINVOICE :
102                 case ST_SUPPCREDIT :
103                 case ST_SUPPAYMENT :
104                         $sql = "SELECT trans.supplier_id as person_id, supp.supp_name as name
105                         FROM ".TB_PREF."supp_trans trans, ".TB_PREF."suppliers supp
106                         WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
107                         ." AND trans.supplier_id=supp.supplier_id";
108                         break;
109
110                 case ST_SUPPRECEIVE :
111                         $sql = "SELECT trans.supplier_id as person_id, supp.supp_name as name
112                         FROM ".TB_PREF."grn_batch trans, ".TB_PREF."suppliers supp
113                         WHERE id=".db_escape($trans_no)
114                         ." AND trans.supplier_id=supp.supplier_id";
115                         break;
116
117                 case ST_BANKPAYMENT :
118                 case ST_BANKDEPOSIT :
119                         $sql = "SELECT trans.debtor_no as person_id, debtor.name as name
120                         FROM ".TB_PREF."debtor_trans trans, ".TB_PREF."debtors_master debtor
121                         WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
122                         ." AND trans.debtor_no=debtor.debtor_no
123                         UNION
124                                 SELECT trans.supplier_id as person_id, supp.supp_name as name
125                         FROM ".TB_PREF."supp_trans trans, ".TB_PREF."suppliers supp
126                         WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
127                         ." AND trans.supplier_id=supp.supplier_id";
128                         break;
129
130                 case ST_JOURNAL:        // FIXME - this one can have multiply counterparties of various types depending on person_type_id
131
132                 default: 
133                 /*      // internal operations
134                 case ST_WORKORDER :
135                 case ST_INVADJUST : // GRN/DN returns ?
136                 case ST_BANKTRANSFER :
137                 case ST_LOCTRANSFER :
138                 case ST_MANUISSUE :
139                 case ST_MANURECEIVE :
140                 case ST_COSTUPDATE :
141                 */
142                         return null;
143         }
144
145         $result = db_query($sql, 'cannot retrieve counterparty name');
146         if (db_num_rows($result))
147         {
148                 $row = db_fetch($result);
149                 return sprintf("[%05s] %s", $row['person_id'], $row['name']);
150         }
151
152         return '';
153 }
154
155
156 //-----------------------------------------------------------------------------------------
157 //      Returns next transaction number.
158 //      Used only for transactions stored in tables without autoincremented key.
159 //
160
161 function get_next_trans_no ($trans_type){
162
163         $st = get_systype_db_info($trans_type);
164
165         if (!($st && $st[0] && $st[2])) {
166                 // this is in fact internal error condition.
167                 display_error('Internal error: invalid type passed to get_next_trans_no()');
168                 return 0;
169         }
170         $sql1 = "SELECT MAX(`$st[2]`) as last_no FROM $st[0]";
171         if ($st[1] != null)
172                  $sql1 .= " WHERE `$st[1]`=".db_escape($trans_type);
173
174         // check also in voided transactions (some transactions like location transfer are removed completely)
175         $sql2 = "SELECT MAX(`id`) as last_no FROM ".TB_PREF."voided WHERE `type`=".db_escape($trans_type);
176
177         $sql = "SELECT max(last_no) last_no FROM ($sql1 UNION $sql2) a";
178     $result = db_query($sql,"The next transaction number for $trans_type could not be retrieved");
179     $myrow = db_fetch_row($result);
180
181     return $myrow[0] + 1;
182 }
183
184 //-----------------------------------------------------------------------------
185
186 function get_systype_db_info($type)
187 {
188         switch ($type)
189         {
190         case     ST_JOURNAL      : return array("".TB_PREF."gl_trans", "type", "type_no", null, "tran_date");
191         case     ST_BANKPAYMENT  : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
192         case     ST_BANKDEPOSIT  : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
193         case     3               : return null;
194         case     ST_BANKTRANSFER : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
195         case     ST_SALESINVOICE : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
196         case     ST_CUSTCREDIT   : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
197         case     ST_CUSTPAYMENT  : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
198         case     ST_CUSTDELIVERY : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
199         case     ST_LOCTRANSFER  : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
200         case     ST_INVADJUST    : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
201         case     ST_PURCHORDER   : return array("".TB_PREF."purch_orders", null, "order_no", "reference", "ord_date");
202         case     ST_SUPPINVOICE  : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
203         case     ST_SUPPCREDIT   : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
204         case     ST_SUPPAYMENT   : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
205         case     ST_SUPPRECEIVE  : return array("".TB_PREF."grn_batch", null, "id", "reference", "delivery_date");
206         case     ST_WORKORDER    : return array("".TB_PREF."workorders", null, "id", "wo_ref", "released_date");
207         case     ST_MANUISSUE    : return array("".TB_PREF."wo_issues", null, "issue_no", "reference", "issue_date");
208         case     ST_MANURECEIVE  : return array("".TB_PREF."wo_manufacture", null, "id", "reference", "date_");
209         case     ST_SALESORDER   : return array("".TB_PREF."sales_orders", "trans_type", "order_no", "reference", "ord_date");
210         case     31              : return array("".TB_PREF."service_orders", null, "order_no", "cust_ref", "date");
211         case     ST_SALESQUOTE   : return array("".TB_PREF."sales_orders", "trans_type", "order_no", "reference", "ord_date");
212         case     ST_DIMENSION    : return array("".TB_PREF."dimensions", null, "id", "reference", "date_");
213         case     ST_COSTUPDATE   : return array("".TB_PREF."gl_trans", "type", "type_no", null, "tran_date");
214         }
215
216         display_db_error("invalid type ($type) sent to get_systype_db_info", "", true);
217 }
218
219 function get_systypes()
220 {
221         $sql = "SELECT * FROM ".TB_PREF."sys_types";
222         $result = db_query($sql, "could not query systypes table");
223         return $result;
224 }
225
226 ?>