Established new hooks class convention.
[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 class hooks {
13         //
14         // Price in words. $doc_type is set to document type and can be used to suppress 
15         // price in words printing for selected document types.
16         // Used instead of built in simple english price_in_words() function.
17         //
18         //      Returns: amount in words as string.
19
20 //      function price_in_words($amount, $doc_type)
21 //      {
22 //      }
23
24         //
25         // Exchange rate currency $curr as on date $date.
26         // Keep in mind FA has internally implemented 3 exrate providers
27         // If any of them supports your currency, you can simply use function below
28         // with apprioprate provider set, otherwise implement your own.
29         // Returns: $curr value in home currency units as a real number.
30
31 //      function retrieve_ex_rate($curr, $date)
32 //      {
33 //              $provider = 'ECB'; // 'ECB', 'YAHOO' or 'GOOGLE'
34 //              return get_extern_rate($curr, $provider, $date);
35 //      }
36
37         // Generic function called at the end of Tax Report (report 709)
38         // Can be used e.g. for special database updates on every report printing
39         // or to print special tax report footer 
40         //
41         // Returns: nothing
42 //      function tax_report_done()
43 //      {
44 //      }
45         // Following database transaction hooks akcepts array of parameters:
46         // 'cart' => transaction data
47         // 'trans_type' => transaction type
48
49 //      function db_prewrite(&$cart, $trans_type)
50 //      {
51 //      }
52 //      function db_postwrite(&$cart, $trans_type)
53 //      {
54 //      }
55 //      function db_prevoid($trans_type, $trans_no)
56 //      {
57 //      }
58 }
59
60 /*
61         Calls hook $method defined in extension $ext (if any)
62 */
63 function hook_invoke($ext, &$data, $opts=null) {
64
65         global $Hooks;
66         
67         if (isset($Hooks[$ext]) && method_exists($Hooks[$ext], $method)) {
68                 $Hooks[$ext]->$method($data, $opts);
69         }
70 }
71
72 /*
73         Calls hook $methods defined in all extensions (if any)
74 */
75 function hook_invoke_all($method, &$data, $opts=null) {
76
77         global $Hooks;
78         
79         $result = array();
80         foreach($Hooks as $ext => $hook)
81                 if (method_exists($hook, $method)) {
82                         $result = $Hooks[$ext]->$method($data, $opts);
83                         if (isset($result) && is_array($result)) {
84                                 $return = array_merge_recursive($return, $result);
85                         } else if (isset($result)) {
86                                 $return[] = $result;
87                                 }
88                 }
89         return $result;
90 }
91 /*
92         Returns first non-null result returned from hook.
93 */
94 function hook_invoke_first($method, &$data, $opts=null) {
95
96         global $Hooks;
97         
98         $result = array();
99         foreach($Hooks as $ext => $hook) {
100                 if (method_exists($hook, $method)) {
101                         $result = $Hooks[$ext]->$method($data, $opts);
102                         if (isset($result))
103                                 return $result;
104                 }
105         }
106         return null;
107 }
108 //------------------------------------------------------------------------------------------
109 //      Database transaction hooks.
110 //      $type - type of transaction (simplifies cart processing)
111 //      $cart - transaction cart
112 //      $args is optional array of parameters
113 //
114 // For FA 2.3 prewrite, postwrite and prevoid hooks are implemented for following transaction types:
115 //
116 // ST_BANKPAYMENT, ST_BANKDEPOSIT, ST_BANKTRANSFER,
117 // ST_SALESORDER, ST_SALESQUOTE, ST_SALESINVOICE, ST_CUSTCREDIT, ST_CUSTPAYMENT, ST_CUSTDELIVERY,
118 // ST_LOCTRANSFER, ST_INVADJUST, 
119 // ST_PURCHORDER, ST_SUPPINVOICE, ST_SUPPCREDIT, ST_SUPPAYMENT, ST_SUPPRECEIVE,
120 // ST_WORKORDER, ST_MANUISSUE, ST_MANURECEIVE, 
121
122 /*
123         Invoked after transaction has been read from database to cart.
124         Not implemented yet.
125 */
126 //function hook_db_postread(&$cart, $type)
127 //{
128 //      hook_invoke_all('db_postread', $cart, $type);
129 //}
130
131 /*
132         Invoked before transaction is written to database.
133 */
134 function hook_db_prewrite(&$cart, $type)
135 {
136         hook_invoke_all('db_prewrite', $cart, $type);
137 }
138
139 /*
140         Invoked after transaction has been written to database.
141 */
142 function hook_db_postwrite(&$cart, $type)
143 {
144         hook_invoke_all('db_postwrite', $cart, $type);
145 }
146 /*
147         Invoked before transaction is voided
148 */
149 function hook_db_prevoid($type, $type_no)
150 {
151         hook_invoke_all('db_prevoid', $type, $type_no);
152 }
153
154 //-------------------------------------------------------------------------------------------
155 //
156 //      Various hooks
157 //
158 //      Alternative exchange rates feeds.
159 //
160 function hook_retrieve_exrate($currency, $date)
161 {
162         return hook_invoke_first('retrieve_exrate', $currency, $date);
163 }
164 //
165 // Generic function called at the end of Tax Report (report 709)
166 //
167 function hook_tax_report_done()
168 {
169         hook_invoke_all('tax_report_done', $dummy);
170 }
171 //
172 //      Amount in words displayed on various documents (especially sales invoice)
173 //
174 function hook_price_in_words($amount, $document)
175 {
176         return hook_invoke_first('price_in_words', $amount, $document);
177 }