Merged latest changes from stable branch.
[fa-stable.git] / includes / types.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 //      FrontAccounting system transaction types
14 //
15 define('ST_JOURNAL', 0);
16
17 define('ST_BANKPAYMENT', 1);
18 define('ST_BANKDEPOSIT', 2);
19 define('ST_BANKTRANSFER', 4);
20
21 define('ST_SALESINVOICE', 10);
22 define('ST_CUSTCREDIT', 11);
23 define('ST_CUSTPAYMENT', 12);
24 define('ST_CUSTDELIVERY', 13);
25
26 define('ST_LOCTRANSFER', 16);
27 define('ST_INVADJUST', 17);
28
29 define('ST_PURCHORDER', 18);
30 define('ST_SUPPINVOICE', 20);
31 define('ST_SUPPCREDIT', 21);
32 define('ST_SUPPAYMENT', 22);
33 define('ST_SUPPRECEIVE', 25);
34
35 define('ST_WORKORDER', 26);
36 define('ST_MANUISSUE', 28);
37 define('ST_MANURECEIVE', 29);
38
39
40 define('ST_SALESORDER', 30);
41 define('ST_SALESQUOTE', 32);
42 define('ST_COSTUPDATE', 35);
43 define('ST_DIMENSION', 40);
44
45 // Don't include these defines in the $systypes_array.
46 // They are used for documents only.
47 define ('ST_STATEMENT', 91);
48 define ('ST_CHEQUE', 92);
49
50 // document inheritance
51 $document_child_types = array(
52                 ST_SALESQUOTE => ST_SALESORDER,
53                 ST_SALESORDER => ST_CUSTDELIVERY,
54                 ST_CUSTDELIVERY => ST_SALESINVOICE,
55                 ST_SALESINVOICE => ST_CUSTCREDIT,
56
57                 ST_PURCHORDER => ST_SUPPRECEIVE,
58                 ST_SUPPRECEIVE => ST_SUPPINVOICE,
59                 ST_SUPPINVOICE => ST_SUPPCREDIT,
60 );
61
62 function get_child_type($type)
63 {
64         global $document_child_types;
65         return isset($document_child_types[$type]) ? $document_child_types[$type] : 0;
66 }
67
68 function get_parent_type($type)
69 {
70         global $document_child_types;
71         $child = array_search($type, $document_child_types);
72         return $child ? $child : 0;
73 }
74
75 //----------------------------------------------------------------------------------
76 //              Bank transaction types
77 //
78 define('BT_TRANSFER', 0);
79 define('BT_CHEQUE', 1);
80 define('BT_CREDIT', 2);
81 define('BT_CASH', 3);
82
83 include_once($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
84 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
85 include_once($path_to_root . "/sales/includes/sales_db.inc");
86 include_once($path_to_root . "/dimensions/includes/dimensions_db.inc");
87 //----------------------------------------------------------------------------------
88 //      Payment types
89 //
90 define('PT_MISC', 0);
91 define('PT_WORKORDER', 1);
92 define('PT_CUSTOMER', 2);
93 define('PT_SUPPLIER', 3);
94 define('PT_QUICKENTRY', 4);
95 define('PT_DIMESION', 5);
96
97 function payment_person_currency($type, $person_id)  {
98         switch ($type)
99         {
100                 case PT_MISC :
101                 case PT_QUICKENTRY :
102                 case PT_WORKORDER :
103                         return get_company_currency();
104
105                 case PT_CUSTOMER :
106                         return get_customer_currency($person_id);
107
108                 case PT_SUPPLIER :
109                         return get_supplier_currency($person_id);
110
111                 default :
112                         return get_company_currency();
113         }
114 }
115
116 function payment_person_name($type, $person_id, $full=true) {
117         global $payment_person_types;
118
119         switch ($type)
120         {
121                 case PT_MISC :
122                         return $person_id;
123                 case PT_QUICKENTRY :
124                         $qe = get_quick_entry($person_id);
125                         return ($full ? $payment_person_types[$type] . " ":"") . $qe["description"];
126                 case PT_WORKORDER :
127                         global $wo_cost_types;
128                         return $wo_cost_types[$person_id];
129                 case PT_CUSTOMER :
130                         return ($full ?$payment_person_types[$type] . " ":"") . get_customer_name($person_id);
131                 case PT_SUPPLIER :
132                         return ($full ? $payment_person_types[$type] . " ":"") . get_supplier_name($person_id);
133                 default :
134                         //DisplayDBerror("Invalid type sent to person_name");
135                         //return;
136                         return '';
137         }
138 }
139
140 //
141 //      Returns counterparty (supplier/customer) name for selected transaction.
142 //
143 function get_counterparty_name($trans_type, $trans_no, $full=true)
144 {
145         switch($trans_type)
146         {
147                 case ST_SALESORDER:
148                 case ST_SALESQUOTE:
149                         $sql = "SELECT order.customer_id as person_id, debtor.name as name
150                         FROM ".TB_PREF."sales_orders order, ".TB_PREF."debtors_master debtor
151                         WHERE order_no=".db_escape($trans_no)." AND trans_type=".db_escape($trans_type)
152                         ." AND order.debtor_no=debtor.debtor_no";
153                         break;
154                 case ST_SALESINVOICE :
155                 case ST_CUSTCREDIT :
156                 case ST_CUSTPAYMENT :
157                 case ST_CUSTDELIVERY :
158                         $sql = "SELECT trans.debtor_no as person_id, debtor.name as name
159                         FROM ".TB_PREF."debtor_trans trans, ".TB_PREF."debtors_master debtor
160                         WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
161                         ." AND trans.debtor_no=debtor.debtor_no";
162                         break;
163
164                 case ST_PURCHORDER :
165                         $sql = "SELECT order.supplier_id as person_id, supp.supp_name as name
166                         FROM ".TB_PREF."purch_orders order, ".TB_PREF."suppliers supp
167                         WHERE order_no=".db_escape($trans_no)
168                         ." AND order.supplier_id=supp.supplier_id";
169                         break;
170
171                 case ST_SUPPINVOICE :
172                 case ST_SUPPCREDIT :
173                 case ST_SUPPAYMENT :
174                         $sql = "SELECT trans.supplier_id as person_id, supp.supp_name as name
175                         FROM ".TB_PREF."supp_trans trans, ".TB_PREF."suppliers supp
176                         WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
177                         ." AND trans.supplier_id=supp.supplier_id";
178                         break;
179
180                 case ST_SUPPRECEIVE :
181                         $sql = "SELECT trans.supplier_id as person_id, supp.supp_name as name
182                         FROM ".TB_PREF."grn_batch trans, ".TB_PREF."suppliers supp
183                         WHERE id=".db_escape($trans_no)
184                         ." AND trans.supplier_id=supp.supplier_id";
185                         break;
186
187                 case ST_JOURNAL:        // FIXME - this one can have multiply counterparties of various types depending on person_type_id
188
189                 default: 
190                 /*      // internal operations
191                 case ST_WORKORDER :
192                 case ST_INVADJUST : // GRN/DN returns ?
193                 case ST_BANKTRANSFER :
194                 case ST_LOCTRANSFER :
195                 case ST_MANUISSUE :
196                 case ST_MANURECEIVE :
197                 case ST_COSTUPDATE :
198                 case ST_BANKPAYMENT :   // generic bank transactions
199                 case ST_BANKDEPOSIT :*/
200                         return null;
201         }
202
203         $result = db_query($sql, 'cannot retrieve counterparty name');
204         if (db_num_rows($result))
205         {
206                 $row = db_fetch($result);
207                 return sprintf("[%05s] %s", $row['person_id'], $row['name']);
208         }
209
210         return '';
211 }
212
213 function payment_person_has_items($type) {
214         switch ($type)
215         {
216                 case PT_MISC :
217                         return true;
218                 case PT_QUICKENTRY :
219                         return db_has_quick_entries();
220                 case PT_WORKORDER : // 070305 changed to open workorders JH
221                         return db_has_open_workorders();
222                 case PT_CUSTOMER :
223                         return db_has_customers();
224                 case PT_SUPPLIER :
225                         return db_has_suppliers();
226                 default :
227                         display_db_error("Invalid type sent to has_items", "");
228                         return false;
229         }
230 }
231 //----------------------------------------------------------------------------------
232 //      Payment terms categories
233 //
234 define('PM_ANY', 0);
235 define('PM_CASH', 1);
236 define('PM_CREDIT', 2);
237
238 //----------------------------------------------------------------------------------
239 //      Manufacturing types
240 //
241 define('WO_ASSEMBLY', 0);
242 define('WO_UNASSEMBLY', 1);
243 define('WO_ADVANCED', 2);
244
245 define('WO_LABOUR', 0);
246 define('WO_OVERHEAD', 1);
247 define('WO_MATERIALS', 2);
248
249 //----------------------------------------------------------------------------------
250 //      GL account classes
251 //
252 define('CL_NONE', 0); // for backward compatibility
253 define('CL_ASSETS', 1);
254 define('CL_LIABILITIES', 2);
255 define('CL_EQUITY', 3);
256 define('CL_INCOME', 4);
257 define('CL_COGS', 5);
258 define('CL_EXPENSE', 6);
259
260 function get_class_type_convert($ctype)
261 {
262         global $use_oldstyle_convert;
263         if (isset($use_oldstyle_convert) && $use_oldstyle_convert == 1)
264                 return (($ctype >= CL_INCOME || $ctype == CL_NONE) ? -1 : 1);
265         else    
266                 return ((($ctype >= CL_LIABILITIES && $ctype <= CL_INCOME) || $ctype == CL_NONE) ? -1 : 1);
267 }
268 //----------------------------------------------------------------------------------
269 //      Quick entry types
270 //
271 define('QE_PAYMENT', '1');
272 define('QE_DEPOSIT', '2');
273 define('QE_JOURNAL', '3');
274 define('QE_SUPPINV', '4');
275
276 //----------------------------------------------------------------------------------
277 //      Special option values for various list selectors.
278 //
279 define('ANY_TEXT', '');
280 define('ANY_NUMERIC', -1);
281 define('ALL_TEXT', '');
282 define('ALL_NUMERIC', -1);
283
284 //----------------------------------------------------------------------------------
285 // Special class values for tables (start_table())
286 define('TABLESTYLE',  1);
287 define('TABLESTYLE2', 2);
288 define('TABLESTYLE_NOBORDER', 3);
289
290 //----------------------------------------------------------------------------------
291
292 define('TAG_ACCOUNT',   1);
293 define('TAG_DIMENSION', 2);
294
295 //----------------------------------------------------------------------------------
296 // Payment term types
297
298 define('PTT_PRE', 1);
299 define('PTT_CASH', 2);
300 define('PTT_DAYS', 3);
301 define('PTT_FOLLOWING', 4);
302
303 //----------------------------------------------------------------------------------
304 // Tax calculation algorithms used in als and purchase (depends on supplier's invoicing software)
305
306 define('TCA_TOTALS', 1); // taxes are calculated from respective net totals for all lines
307 define('TCA_LINES', 2); // taxes calculated for every line, then summed
308 //
309 //      Bank account owner types
310 //
311 define('BO_UNKNOWN', 0);
312 define('BO_COMPANY', 1);
313 define('BO_CUSTBRANCH', 2);
314 define('BO_SUPPLIER', 3);
315
316 include_once($path_to_root . '/includes/sysnames.inc');
317
318 ?>