Added get_dispatchable_quantity hook.
[fa-stable.git] / includes / hooks.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 extension modules integration.
14 // This file is included in session.inc even before session is started,
15 // and includes hooks.php connector files from all installed extensions.
16 // To make hooks active install_hooks() have to be called after interface
17 // language is set.
18 //
19 // To find how various hooks are processed look into respective hook_* functions below.
20 //
21 class hooks {
22         var $module_name; // extension module name.
23
24         // 
25         // Helper for updating databases with extension scheme
26         //
27         // $comp can be company number, -1 for all, 
28         // $updates - table of filename => array(table, field, property)
29         // $check_only - don't update database, check table/field/property existence only
30         //
31         function update_databases($comp, $updates, $check_only=false)
32         {
33                 global $db_connections, $path_to_root;
34         
35                 if ($comp == -1) 
36                         $conn = $db_connections;
37                 else
38                         $conn = array( $comp => $db_connections[$comp]);
39                 $result = true;
40
41                 foreach($conn as $comp => $con) {
42                         set_global_connection($comp);
43                         foreach($updates as $file => $update) {
44                                 $table = @$update[0];
45                                 $field = @$update[1];
46                                 $properties = @$update[2];
47
48                                 $ok = check_table($con['tbpref'], $table, $field, $properties) == 0;
49
50                                 if (!$check_only && !$ok) {
51                                         $ok = db_import($path_to_root.'/modules/'.$this->module_name.'/sql/'.$file,
52                                                 $con);
53                                 }
54                                 $result &= $ok;
55                                 if (!$result)
56                                         break;
57                         }
58                         db_close();
59                         if (!$result)
60                                 break;
61                 }
62                 set_global_connection(0); // return to siteadmin account
63
64                 return $result;
65         }
66         //
67         //      Install additional tabs provided by extension
68         //
69         function install_tabs($app)
70         {
71 //              $app->add_application(new example_class); // add menu tab defined by example_class
72         }
73         //
74         //      Install additonal menu options provided by extension
75         //
76         function install_options($app)
77         {
78 //              global $path_to_root;
79 //
80 //              switch($app->id) {
81 //                      case 'orders':
82 //                              $app->add_rapp_function( 0, _("&Example option"), 
83 //                                      $path_to_root.'/modules/example/example.php?', 'SA_OPEN');
84 //              }
85         }
86         
87         function install_access()
88         {
89 //              $security_areas['SA_EXAMPLE'] = array(SS_EXAMPLE|100, _("Example security area."));
90 //
91 //              $security_sections = array(SS_EXAMPLE => _("Example module implementation"));
92 //
93 //              return array($security_areas, $security_sections);
94         }
95
96         //
97         //      Invoked for all modules before page header is displayed
98         //
99         function pre_header($fun_args)
100         {
101         }
102         //
103         //      Invoked for all modules before page footer is displayed
104         //
105         function pre_footer($fun_args)
106         {
107         }
108
109         //
110         // Price in words. $doc_type is set to document type and can be used to suppress 
111         // price in words printing for selected document types.
112         // Used instead of built in simple english price_in_words() function.
113         //
114         //      Returns: amount in words as string.
115
116         function price_in_words($amount, $doc_type)
117         {
118         }
119         //
120         // Exchange rate currency $curr as on date $date.
121         // Keep in mind FA has internally implemented 3 exrate providers
122         // If any of them supports your currency, you can simply use function below
123         // with apprioprate provider set, otherwise implement your own.
124         // Returns: $curr value in home currency units as a real number.
125
126         function retrieve_exrate($curr, $date)
127         {
128 //              $provider = 'ECB'; // 'ECB', 'YAHOO' or 'GOOGLE'
129 //              return get_extern_rate($curr, $provider, $date);
130                 return null;
131         }
132
133         // External authentication
134         // If used should return true after successfull athentication, false otherwise.
135         function authenticate($login, $password)
136         {
137                 return null;
138         }
139         // Generic function called at the end of Tax Report (report 709)
140         // Can be used e.g. for special database updates on every report printing
141         // or to print special tax report footer 
142         //
143         // Returns: nothing
144         function tax_report_done()
145         {
146         }
147         // Following database transaction hooks akcepts array of parameters:
148         // 'cart' => transaction data
149         // 'trans_type' => transaction type
150
151         function db_prewrite(&$cart, $trans_type)
152         {
153                 return true;
154         }
155
156         function db_postwrite(&$cart, $trans_type)
157         {
158                 return true;
159         }
160
161         function db_prevoid($trans_type, $trans_no)
162         {
163                 return true;
164         }
165         /*
166                 This method is called after module install.
167         */
168         function install_extension($check_only=true)
169         {
170                 return true;
171         }
172         /*
173                 This method is called after module uninstall.
174         */
175         function uninstall_extension($check_only=true)
176         {
177                 return true;
178         }
179         /*
180                 This method is called on extension activation for company.
181         */
182         function activate_extension($company, $check_only=true)
183         {
184                 return true;
185         }
186         /*
187                 This method is called when extension is deactivated for company.
188         */
189         function deactivate_extension($company, $check_only=true)
190         {
191                 return true;
192         }
193
194         /*
195          * Returns the quantity allowed to be dispatched for a particular item 
196          * and a status (which can be used to style the row).
197          * This quantity would be the default value on the delivery note.
198          * The usual use case for this is when a item is in stock,
199          * but has been reserved by someone else.
200          * This allows extensions to implements its own priority algorithm.
201          * This function is by detail_id and not item in case the item is present
202          * more than one in  the cart.
203          */
204         /* Default behavior check if there is enough quantity on hand and change the css
205  * class if needed */
206         static function  default_get_dispatchable_quantity($line_item, $location, $date, $qoh) {
207     global $SysPrefs;
208
209                 if ($SysPrefs->allow_negative_stock() || ($line_item->qty_dispatched <= $qoh)) {
210                         return true;
211                 }
212                 return array($qoh, 'stockmankobg');
213                 return array($line_item->qty_dispatched, 'stockmankobg');
214         }
215
216 }
217
218 /*
219         Installs hooks provided by extension modules
220 */
221 function install_hooks()
222 {
223         global $path_to_root, $Hooks, $installed_extensions;
224
225         $Hooks = array();
226         
227         // include current language related $Hooks object if locale file exists
228         if (file_exists($path_to_root . "/lang/".$_SESSION['language']->code."/locale.inc"))
229         {
230                 include_once($path_to_root . "/lang/".$_SESSION['language']->code."/locale.inc");
231                 $code = $_SESSION['language']->code;
232                 $hook_class = 'hooks_'.$code;
233                 $Hooks[$code] = new $hook_class;
234                 unset($code, $hook_class);
235         }
236         // install hooks provided by active extensions
237         foreach($installed_extensions as $ext) {
238                         $hook_class = 'hooks_'.$ext['package'];
239                         if ($ext['active'] && class_exists($hook_class)) {
240                                 $Hooks[$ext['package']] = new $hook_class;
241                         }
242         }
243 }
244 /*
245         Non active hooks are not included in $Hooks array, so we can use special function to 
246         activate.
247 */
248 function activate_hooks($ext, $comp)
249 {
250         global $Hooks;
251         
252         $hooks = @$Hooks[$ext];
253         if (!$hooks) {
254                 $hookclass = 'hooks_'.$ext;
255                 if (class_exists($hookclass))
256                         $hooks = new $hookclass;
257                 else
258                         return true; // extension does not have hooks file
259         }
260         if (!$hooks)
261                 return false;
262         else
263                 return $hooks->activate_extension($comp, false);
264 }
265 /*
266         Calls hook $method defined in extension $ext (if any)
267 */
268 function hook_invoke($ext, $method, &$data, $opts=null)
269 {
270
271         global $Hooks;
272
273         $ret = null;
274         if (isset($Hooks[$ext]) && method_exists($Hooks[$ext], $method)) {
275                 set_ext_domain('modules/'.$ext);
276                 $ret = $Hooks[$ext]->$method($data, $opts);
277                 set_ext_domain();
278         } 
279         return $ret;
280 }
281
282 /*
283         Calls hook $methods defined in all extensions (if any)
284 */
285 function hook_invoke_all($method, &$data, $opts=null)
286 {
287
288         global $Hooks;
289         
290         $return = array();
291         if (isset($Hooks))
292         {
293                 foreach($Hooks as $ext => $hook)
294                         if (method_exists($hook, $method)) {
295                                 set_ext_domain('modules/'.$ext);
296                                 $result = $hook->$method($data, $opts);
297                                 if (isset($result) && is_array($result)) {
298                                         $return = array_merge_recursive($return, $result);
299                                 } else if (isset($result)) {
300                                         $return[] = $result;
301                                         }
302                                 set_ext_domain();
303                         }
304         }
305         return $return;
306 }
307 /*
308         Returns first non-null result returned from hook.
309 */
310 function hook_invoke_first($method, &$data, $opts=null)
311 {
312
313         global $Hooks;
314         
315         $result = null;
316         foreach($Hooks as $ext => $hook) {
317                 if (method_exists($hook, $method)) {
318                         set_ext_domain('modules/'.$ext);
319                         $result = $hook->$method($data, $opts);
320                         set_ext_domain();
321                         if (isset($result))
322                                 break;
323                 }
324         }
325         return $result;
326 }
327 /*
328         Returns last non-null result returned from modules method. Helps implement hooks overriding by 
329         extensions installed later.
330 */
331 function hook_invoke_last($method, &$data, $opts=null)
332 {
333
334         global $Hooks;
335
336         $result = null;
337         $Reverse = array_reverse($Hooks);
338         foreach($Reverse as $ext => $hook) {
339                 if (method_exists($hook, $method)) {
340                         set_ext_domain('modules/'.$ext);
341                         $result = $hook->$method($data, $opts);
342                         set_ext_domain();
343                         if (isset($result))
344                                 break;
345                 }
346         }
347         return $result;
348 }
349 //------------------------------------------------------------------------------------------
350 //      Database transaction hooks.
351 //      $type - type of transaction (simplifies cart processing)
352 //      $cart - transaction cart
353 //      $args is optional array of parameters
354 //
355 // For FA 2.3 prewrite, postwrite and prevoid hooks are implemented for following transaction types:
356 //
357 // ST_BANKPAYMENT, ST_BANKDEPOSIT, ST_BANKTRANSFER,
358 // ST_SALESORDER, ST_SALESQUOTE, ST_SALESINVOICE, ST_CUSTCREDIT, ST_CUSTPAYMENT, ST_CUSTDELIVERY,
359 // ST_LOCTRANSFER, ST_INVADJUST, 
360 // ST_PURCHORDER, ST_SUPPINVOICE, ST_SUPPCREDIT, ST_SUPPAYMENT, ST_SUPPRECEIVE,
361 // ST_WORKORDER, ST_MANUISSUE, ST_MANURECEIVE, 
362
363 /*
364         Invoked after transaction has been read from database to cart.
365         Not implemented yet.
366 */
367 //function hook_db_postread(&$cart, $type)
368 //{
369 //      hook_invoke_all('db_postread', $cart, $type);
370 //}
371
372 /*
373         Invoked before transaction is written to database.
374 */
375 function hook_db_prewrite(&$cart, $type)
376 {
377         return hook_invoke_all('db_prewrite', $cart, $type);
378 }
379
380 /*
381         Invoked after transaction has been written to database.
382 */
383 function hook_db_postwrite(&$cart, $type)
384 {
385         return hook_invoke_all('db_postwrite', $cart, $type);
386 }
387 /*
388         Invoked before transaction is voided
389 */
390 function hook_db_prevoid($type, $type_no)
391 {
392         return hook_invoke_all('db_prevoid', $type, $type_no);
393 }
394
395 //-------------------------------------------------------------------------------------------
396 //
397 //      Various hooks
398 //
399 //      Alternative exchange rates feeds.
400 //
401 function hook_retrieve_exrate($currency, $date)
402 {
403         return hook_invoke_last('retrieve_exrate', $currency, $date);
404 }
405 //
406 // Generic function called at the end of Tax Report (report 709)
407 //
408 function hook_tax_report_done()
409 {
410         return hook_invoke_all('tax_report_done', $dummy);
411 }
412 //
413 //      Amount in words displayed on various documents (especially sales invoice)
414 //
415 function hook_price_in_words($amount, $document)
416 {
417         return hook_invoke_last('price_in_words', $amount, $document);
418 }
419 //
420 //      Session handling hook. This is special case of hook class which have to be run before session is started.
421 //      If fa_session_manager class is defined in any installed extension, this class provides session handling
422 //      for application, otherwise standard php session handling is used.
423 //
424 function hook_session_start($company)
425 {
426         if (class_exists('fa_session_manager')) {
427                 global $SessionManager;
428                 $SessionManager = new fa_session_manager($company);
429                 return $SessionManager->installed;
430         }
431         return false;
432 }
433 //
434 //      Third party authentication modules.
435 //      Returns true after successfull authentication, false otherwise, null if no login hook is defined.
436 //
437 function hook_authenticate($login, $password)
438 {
439         return hook_invoke_last('authenticate', $login, $password);
440 }
441
442         /*
443          * Returns the quantity allowed to be dispatched for a particular item 
444          * and a "reason" (css classes).
445          * This quantity would be the default value on the delivery note.
446          * The usual use case for this is when a item is in stock,
447          * but has been reserved by someone else.
448          * This allows extensions to implements its own priority algorithm.
449          * This function is by detail_id and not item in case the item is present
450          * more than one in  the cart.
451          * If 'skip' is returned, the line will be skipped and not displayed
452          */
453 function hook_get_dispatchable_quantity($line_item, $location, $date, $qoh) {
454                 $result =  hook_invoke_first('get_dispatchable_quantity', $line_item, array($location, $date, $qoh));
455                 return $result !== null ? $result : hooks::default_get_dispatchable_quantity($line_item, $location, $date, $qoh);
456 }