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 ***********************************************************************/
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
19 // To find how various hooks are processed look into respective hook_* functions below.
22 var $module_name; // extension module name.
25 // Helper for updating databases with extension scheme
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
31 function update_databases($comp, $updates, $check_only=false)
33 global $db_connections, $path_to_root;
36 $conn = $db_connections;
38 $conn = array( $comp => $db_connections[$comp]);
41 foreach($conn as $comp => $con) {
42 set_global_connection($comp);
43 foreach($updates as $file => $update) {
46 $properties = @$update[2];
48 $ok = check_table($con['tbpref'], $table, $field, $properties) == 0;
50 if (!$check_only && !$ok) {
51 $ok = db_import($path_to_root.'/modules/'.$this->module_name.'/sql/'.$file,
62 set_global_connection(0); // return to siteadmin account
67 // Install additional tabs provided by extension
69 function install_tabs($app) {
70 // set_ext_domain('modules/example'); // set text domain for gettext
71 // $app->add_application(new example_class); // add menu tab defined by example_class
75 // Install additonal menu options provided by extension
77 function install_options($app) {
78 // global $path_to_root;
79 // set_ext_domain('modules/example');
82 // $app->add_rapp_function( 0, _("&Example option"),
83 // $path_to_root.'/modules/example/example.php?', 'SA_OPEN');
88 // Price in words. $doc_type is set to document type and can be used to suppress
89 // price in words printing for selected document types.
90 // Used instead of built in simple english price_in_words() function.
92 // Returns: amount in words as string.
94 function price_in_words($amount, $doc_type)
99 // Exchange rate currency $curr as on date $date.
100 // Keep in mind FA has internally implemented 3 exrate providers
101 // If any of them supports your currency, you can simply use function below
102 // with apprioprate provider set, otherwise implement your own.
103 // Returns: $curr value in home currency units as a real number.
105 function retrieve_exrate($curr, $date)
107 // $provider = 'ECB'; // 'ECB', 'YAHOO' or 'GOOGLE'
108 // return get_extern_rate($curr, $provider, $date);
112 // Generic function called at the end of Tax Report (report 709)
113 // Can be used e.g. for special database updates on every report printing
114 // or to print special tax report footer
117 function tax_report_done()
120 // Following database transaction hooks akcepts array of parameters:
121 // 'cart' => transaction data
122 // 'trans_type' => transaction type
124 function db_prewrite(&$cart, $trans_type)
129 function db_postwrite(&$cart, $trans_type)
134 function db_prevoid($trans_type, $trans_no)
139 This method is called after module install.
141 function install_extension($check_only=true)
146 This method is called after module uninstall.
148 function uninstall_extension($check_only=true)
153 This method is called on extension activation for company.
155 function activate_extension($company, $check_only=true)
160 This method is called when extension is deactivated for company.
162 function deactivate_extension($company, $check_only=true)
168 // include all extensions hook files.
170 foreach ($installed_extensions as $ext)
172 if (file_exists($path_to_root.'/'.$ext['path'].'/hooks.php'))
173 include_once($path_to_root.'/'.$ext['path'].'/hooks.php');
177 Installs hooks provided by extension modules
179 function install_hooks() {
180 global $path_to_root, $Hooks, $installed_extensions;
184 // include current language related $Hooks object if locale file exists
185 if (file_exists($path_to_root . "/lang/".$_SESSION['language']->code."/locale.inc"))
187 include_once($path_to_root . "/lang/".$_SESSION['language']->code."/locale.inc");
188 $code = $_SESSION['language']->code;
189 $hook_class = 'hooks_'.$code;
190 $Hooks[$code] = new $hook_class;
191 unset($code, $hook_class);
193 // install hooks provided by active extensions
194 foreach($installed_extensions as $ext) {
195 $hook_class = 'hooks_'.$ext['package'];
196 if ($ext['active'] && class_exists($hook_class)) {
197 $Hooks[$ext['package']] = new $hook_class;
202 Non active hooks are not included in $Hooks array, so we can use special function to
205 function activate_hooks($ext, $comp) {
208 $hooks = @$Hooks[$ext];
210 $hookclass = 'hooks_'.$ext;
211 if (class_exists($hookclass))
212 $hooks = new $hookclass;
214 return true; // extension does not have hooks file
219 return $hooks->activate_extension($comp, false);
222 Calls hook $method defined in extension $ext (if any)
224 function hook_invoke($ext, $method, &$data, $opts=null) {
229 if (isset($Hooks[$ext]) && method_exists($Hooks[$ext], $method)) {
230 set_ext_domain('modules/'.$ext);
231 $ret = $Hooks[$ext]->$method($data, $opts);
238 Calls hook $methods defined in all extensions (if any)
240 function hook_invoke_all($method, &$data, $opts=null) {
245 foreach($Hooks as $ext => $hook)
246 if (method_exists($hook, $method)) {
247 set_ext_domain('modules/'.$ext);
248 $result = $hook->$method($data, $opts);
249 if (isset($result) && is_array($result)) {
250 $return = array_merge_recursive($return, $result);
251 } else if (isset($result)) {
259 Returns first non-null result returned from hook.
261 function hook_invoke_first($method, &$data, $opts=null) {
266 foreach($Hooks as $ext => $hook) {
267 if (method_exists($hook, $method)) {
268 set_ext_domain('modules/'.$ext);
269 $result = $hook->$method($data, $opts);
278 Returns result of last hook installed. Helps implement hooks overriding by
279 extensions installed later.
282 function hook_invoke_last($method, &$data, $opts=null) {
287 foreach($Hooks as $ext => $hook) {
288 if (method_exists($hook, $method)) {
294 set_ext_domain('modules/'.$found);
295 $ret = $Hooks[$found]->$method($data, $opts);
300 //------------------------------------------------------------------------------------------
301 // Database transaction hooks.
302 // $type - type of transaction (simplifies cart processing)
303 // $cart - transaction cart
304 // $args is optional array of parameters
306 // For FA 2.3 prewrite, postwrite and prevoid hooks are implemented for following transaction types:
308 // ST_BANKPAYMENT, ST_BANKDEPOSIT, ST_BANKTRANSFER,
309 // ST_SALESORDER, ST_SALESQUOTE, ST_SALESINVOICE, ST_CUSTCREDIT, ST_CUSTPAYMENT, ST_CUSTDELIVERY,
310 // ST_LOCTRANSFER, ST_INVADJUST,
311 // ST_PURCHORDER, ST_SUPPINVOICE, ST_SUPPCREDIT, ST_SUPPAYMENT, ST_SUPPRECEIVE,
312 // ST_WORKORDER, ST_MANUISSUE, ST_MANURECEIVE,
315 Invoked after transaction has been read from database to cart.
318 //function hook_db_postread(&$cart, $type)
320 // hook_invoke_all('db_postread', $cart, $type);
324 Invoked before transaction is written to database.
326 function hook_db_prewrite(&$cart, $type)
328 return hook_invoke_all('db_prewrite', $cart, $type);
332 Invoked after transaction has been written to database.
334 function hook_db_postwrite(&$cart, $type)
336 return hook_invoke_all('db_postwrite', $cart, $type);
339 Invoked before transaction is voided
341 function hook_db_prevoid($type, $type_no)
343 return hook_invoke_all('db_prevoid', $type, $type_no);
346 //-------------------------------------------------------------------------------------------
350 // Alternative exchange rates feeds.
352 function hook_retrieve_exrate($currency, $date)
354 return hook_invoke_last('retrieve_exrate', $currency, $date);
357 // Generic function called at the end of Tax Report (report 709)
359 function hook_tax_report_done()
361 return hook_invoke_all('tax_report_done', $dummy);
364 // Amount in words displayed on various documents (especially sales invoice)
366 function hook_price_in_words($amount, $document)
368 return hook_invoke_last('price_in_words', $amount, $document);
371 // Session handling hook. This is special case of hook class which have to be run before session is started.
372 // If fa_session_manager class is defined in any installed extension, this class provides session handling
373 // for application, otherwise standard php session handling is used.
375 function hook_session_start($company)
377 if (class_exists('fa_session_manager')) {
378 global $SessionManager;
379 $SessionManager = new fa_session_manager($company);
380 return $SessionManager->installed;