/[drupal]/contributions/modules/invoice/invoice_helpers.inc
ViewVC logotype

Contents of /contributions/modules/invoice/invoice_helpers.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Sat Dec 13 14:03:32 2008 UTC (11 months, 1 week ago) by pvogelaar
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +13 -1 lines
File MIME type: text/x-php
* Fixed bug: "Invoice items : Unit Cost incl VAT is wrong whent you use a VAT less of 10%" => http://drupal.org/node/345800
* Added a TODO: Possibility to link an invoice to a site user + page for that user to view his invoices => http://drupal.org/node/346516
  - With a extra option to also send the invoice by email (if no site user is selected the customer email field that is not implemented yet
    will be choosen, if both are empty an error will be thrown if this checkbox is still checked. => http://drupal.org/node/346005
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Invoice module
7 *
8 * This module was developed by Platina Designs, http://www.platinadesigns.nl
9 *
10 * @author Pieter Vogelaar <ps.vogelaar@platinadesigns.nl>
11 */
12
13 /**
14 * Helper function to calculate invoice totals
15 *
16 * The ROUND() function of MySQL is not very reliable, especially in MySQL 4.x,
17 * that's why this function came into play
18 *
19 * @param integer $invoice_number
20 * @param integer $user_id
21 *
22 * @return array $a_totals
23 */
24 function _invoice_get_invoice_totals($invoice_number, $user_id=0) {
25 $a_totals = array();
26 $sql_user_addition = '';
27
28 if ($user_id > 0) {
29 $sql_user_addition = ' AND uid="'. intval($user_id) .'"';
30 }
31
32 $result = db_query("SELECT vat,quantity*unitcost as extotal,
33 (quantity*unitcost)*((vat / 100) +1) as inctotal,
34 (quantity*unitcost)*((vat / 100) +1) * (1 - (1 / ((vat / 100) +1))) as vattotal
35 FROM {invoice_items}
36 WHERE invoice_id=%d" . $sql_user_addition,
37 $invoice_number
38 );
39
40 while ($row = db_fetch_object($result)) {
41 $a_totals['extotal'] += _invoice_round($row->extotal, 2);
42 $a_totals['inctotal'] += ($row->extotal * _invoice_vat_percent_to_decimal($row->vat));
43 $a_totals['vattotal'] += $row->vattotal;
44 }
45
46 $a_totals['extotal'] = _invoice_round($a_totals['extotal'], 2);
47 $a_totals['inctotal'] = _invoice_round($a_totals['inctotal'], 2);
48 $a_totals['vattotal'] = _invoice_round($a_totals['vattotal'], 2);
49
50 return $a_totals;
51 }
52
53 /**
54 * Converts a VAT value in percent to decimal
55 *
56 * For examle:
57 * A vat value of 20,5 will be converted into 1.205
58 *
59 * @param mixed $vat
60 */
61 function _invoice_vat_percent_to_decimal($percent_vat) {
62 $decimal_vat = 1 + ($percent_vat / 100);
63 return $decimal_vat;
64 }
65
66 /**
67 * Helper function to easily get the name of the chosen template when adding an invoice
68 */
69 function _invoice_get_chosen_template() {
70 return !isset($_SESSION['invoice_template']) || empty($_SESSION['invoice_template']) ? variable_get('invoice_default_template', 'default') : $_SESSION['invoice_template'];
71 }
72
73 /**
74 * Helper function to get the invoice in HTML format
75 */
76 function _invoice_get_html($invoice_number, $type='print') {
77 $nid = db_result(db_query("SELECT nid FROM {invoice_invoices} WHERE iid=%d", $invoice_number));
78 $node = node_load($nid);
79
80 $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
81 <html xmlns="http://www.w3.org/1999/xhtml" lang="nl" xml:lang="nl" dir="ltr">
82 <head>
83 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15" />
84 ';
85 $html = '<html><head>';
86 $html .= '<link type="text/css" rel="stylesheet" media="all" href="http://'. $_SERVER['HTTP_HOST'] . base_path() . drupal_get_path('module', 'invoice') .'/templates/'. $node->invoice['template'] .'.css" />';
87 $html .= '</head><body><div class="'. $type .'">';
88
89 $html .= theme('invoice_body', $node, $type);
90
91 // The eurosign is not supported as applicable character, so replace it with a ascii code
92 $html = str_replace('€', '&#0128;', $html);
93
94 $html .= '</div></body></html>';
95 return $html;
96 }
97
98 /**
99 * Helper function to get invoice id
100 *
101 * @return integer
102 */
103 function _invoice_get_new_invoice_number($user_defined_invoice_number_check=FALSE) {
104 if ($user_defined_invoice_number_check == TRUE) {
105 $count = db_result(db_query("SELECT COUNT(*) FROM {invoice_invoices}"));
106 if ($count == 0) {
107 return 0;
108 }
109 else {
110 return db_result(db_query("SELECT iid FROM {invoice_invoices} ORDER BY nid DESC LIMIT 1"));
111 }
112 }
113
114 $new_invoice_number = db_result(db_query("SELECT iid FROM {invoice_invoices} ORDER BY nid DESC LIMIT 1")) + 1;
115
116 return $new_invoice_number;
117 }
118
119 /**
120 * Helper function get a formatted invoice number
121 *
122 * @param integer $invoice_number
123 * @param mixed $formatted_invoice_number
124 */
125 function _invoice_get_formatted_invoice_number($invoice_number, $node = NULL) {
126 $formatted_invoice_number = $invoice_number;
127
128 if (is_null($node)) {
129 $invoice_settings = db_fetch_array(db_query("SELECT leading_zeros,prefix FROM {invoice_invoices} WHERE iid=%d", $invoice_number));
130
131 $invoice_number_zerofill = $invoice_settings['leading_zeros'];
132 $invoice_number_prefix = $invoice_settings['prefix'];
133 }
134 else {
135 $invoice_number_zerofill = $node->invoice_invoice_number_zerofill;
136 $invoice_number_prefix = $node->invoice_invoice_number_prefix;
137 }
138
139 // Add leading zeros
140 $formatted_invoice_number = sprintf("%0". $invoice_number_zerofill ."d", $formatted_invoice_number);
141
142 // Add prefix
143 $prefix_string = $invoice_number_prefix;
144 $possible_date_arguments = _invoice_get_possible_date_arguments();
145
146 $exp = explode('%', $prefix_string);
147 $i = 0;
148 foreach ($exp as $key => $value) {
149 $i++;
150 // If prefix string didn't start with %, the first characters are never a date argument
151 if (substr($prefix_string, 0, 1) == '%' || $i > 1) {
152 if (strlen($value) == 1) {
153 if (in_array($value, $possible_date_arguments)) {
154 $exp[$key] = date($value);
155 }
156 }
157 else {
158 if (in_array(substr($value, 0, 1), $possible_date_arguments)) {
159 $exp[$key] = date(substr($value, 0, 1));
160
161 // Add the rest of the string which was not supposed to be a date argument
162 $exp[$key] .= substr($value, 1);
163 }
164 }
165 }
166 }
167
168 $prefix = implode('', $exp);
169
170 $formatted_invoice_number = $prefix . $formatted_invoice_number;
171
172 return $formatted_invoice_number;
173 }
174
175 /**
176 * Returns all possible date arguments
177 *
178 * @return array
179 */
180 function _invoice_get_possible_date_arguments() {
181 $possible_date_arguments = array (
182 // Day
183 'd', 'D', 'j', 'l', 'N', 'S', 'w', 'z',
184
185 // Week
186 'W',
187
188 // Month
189 'F', 'm', 'M', 'n', 't',
190
191 // Year
192 'L', 'o', 'Y', 'y',
193
194 // Time
195 'a', 'A', 'B', 'g', 'G', 'h', 'H', 'i', 's', 'u',
196
197 // timezone
198 'e', 'I', 'O', 'P', 'T', 'Z',
199
200 // Full Date / Time
201 'c', 'r', 'U'
202 );
203
204 return $possible_date_arguments;
205 }
206
207 /**
208 * Helper function to add JS or CSS to Drupal
209 */
210 function _invoice_add_css_js() {
211 $a_invoice_js_settings = array(
212 'host' => $_SERVER['HTTP_HOST'],
213 );
214 drupal_add_js(array('invoice' => $a_invoice_js_settings), 'setting');
215 drupal_add_js(_invoice_get_js(), 'inline');
216 drupal_add_css(drupal_get_path('module', 'invoice') .'/invoice.css');
217 }
218
219 /**
220 * Helper function to get a rounded value
221 *
222 * Read the comments on http://www.php.net/round why the
223 * standard PHP round() function doesn't work right.
224 *
225 * For example with PHP round(38.675, 2) gives 38.67, but that must be 38.68
226 *
227 * @param mixed $value
228 * @param integer $precision
229 * @return float
230 */
231 function _invoice_round($value, $precision=0) {
232 $rounded_value = round(round($value*pow(10, $precision+1), 0), -1)/pow(10, $precision+1);
233 return $rounded_value;
234 }
235
236 /**
237 * Helper function to get the available template names
238 */
239 function _invoice_get_templates() {
240 $a_templates = array('default');
241 $files = file_scan_directory(dirname(__FILE__) .'/templates', '.inc');
242 foreach ($files as $file) {
243 if (!empty($file->name) && $file->name != 'default') {
244 $a_templates[] = check_plain($file->name);
245 }
246 }
247
248 return $a_templates;
249 }
250
251 /**
252 * Helper function to get template variables
253 */
254 function _invoice_get_variable($template, $name, $default=NULL) {
255 // if $template is empty, check if a general/default value is available
256 if (empty($template)) {
257 return variable_get('invoice_'. $name, '');
258 }
259
260 static $a_templates = NULL;
261
262 // Get all template settings from the database only one time
263 if (!is_array($a_templates)) {
264 $a_templates_names = _invoice_get_templates();
265
266 $a_templates = array();
267 $result = db_query("SELECT * FROM {invoice_templates} WHERE name IN ('". implode("','", $a_templates_names) ."')");
268 while ($row = db_fetch_array($result)) {
269 $a_templates[$row['name']] = $row;
270 }
271 }
272
273 // Get template info that is stored in the database
274 $a_template = $a_templates[$template];
275
276 if (!empty($a_template[$name]) || is_numeric($a_template[$name])) {
277 return $a_template[$name];
278 }
279 else {
280 if (!is_null($default)) {
281 return $default;
282 }
283 else {
284 // if $default is not null, check if a general/default value is available
285 return variable_get('invoice_'. $name, '');
286 }
287 }
288 }
289
290 /**
291 * Helper function to get an icon
292 */
293 function _invoice_get_icon($name, $url = NULL, $attributes = array(), $extension = 'png') {
294 if (empty($attributes['alt'])) {
295 $attributes['alt'] = $attributes['title'];
296 }
297
298 $img_addition = '';
299 foreach ($attributes as $key => $value) {
300 $img_addition .= ' '. $key .'="'. $value .'"';
301 }
302
303 $icon = '<img src="'. base_path() . drupal_get_path('module', 'invoice') .'/images/'. $name .'.'. $extension .'"'. $img_addition .' />';
304 if (!empty($url)) {
305 $icon = l($icon, $url, array('html' => TRUE));
306 //$icon = '<a href="'. base_path() . $url .'" '. drupal_attributes($attributes) .'>'. $icon .'</a>';
307 }
308
309 return $icon;
310 }
311
312 /**
313 * Helper function to collect all $_GET variables and put them in a string
314 */
315 function _invoice_getvars_array_to_string($a_vars = array()) {
316
317 unset($a_vars['q']);
318
319 $s_vars = '?';
320 foreach ($a_vars as $key => $value) {
321 $s_vars .= $key .'='. $value .'&';
322 }
323
324 return rtrim($s_vars, '&');
325 }
326
327 /**
328 * Helper function to convert the string with get variables to an array
329 *
330 * @param unknown_type $s_vars
331 * @return unknown
332 */
333 function _invoice_getvars_string_to_array($s_vars) {
334 $exp = explode('&', $s_vars);
335 $a_query_vars = array();
336 // the first element is always "q", we don't want that element so start with $i=1
337 for ($i=1;$i<count($exp);$i++) {
338 $sub_exp = explode('=', $exp[$i]);
339 $a_query_vars[$sub_exp[0]] = $sub_exp[1];
340 }
341 return $a_query_vars;
342 }
343
344 /**
345 * Returns installed locales on the system
346 *
347 * @return array
348 */
349 function _invoice_get_installed_system_locales() {
350 ob_start();
351 system('locale -a');
352 $str = ob_get_contents();
353 ob_end_clean();
354 $list = explode("\n", trim($str));
355
356 return $list;
357 }
358
359 /**
360 * Makes sure that node promote flag is off
361 *
362 * On ?q=admin/content/node-type/invoice there is a checkbox in the workflow fieldset that is
363 * called "Promoted to front page", if this is turned on invoices will be displayed at ?q=node
364 * global node overview. Because invoices are private, I guess this must always be disabled
365 * for anyone.
366 */
367 function _make_sure_node_promote_flag_is_off() {
368 $node_options = variable_get('node_options_invoice', array());
369 if (in_array('promote', $node_options)) {
370 foreach ($node_options as $key => $option) {
371 if ($option == 'promote') {
372 unset($node_options[$key]);
373 }
374 }
375 }
376 variable_set('node_options_invoice', $node_options);
377 }
378
379 /**
380 * Helper function to include the dompdf library
381 */
382 function _invoice_dompdf_include_lib() {
383 require_once(dirname(__FILE__) .'/dompdf/dompdf_config.inc.php');
384 }

  ViewVC Help
Powered by ViewVC 1.1.2