492bd48634523ac838984c7b30e5401683b91e40
[fa-stable.git] / includes / ui / ui_view.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 include_once($path_to_root . "/admin/db/voiding_db.inc");
13 include_once($path_to_root . "/includes/types.inc");
14
15 //--------------------------------------------------------------------------------------
16
17 function get_supplier_trans_view_str($type, $trans_no, $label="", $icon=false, 
18         $class='', $id='')
19 {
20         $viewer = "purchasing/view/";
21         if ($type == ST_PURCHORDER)
22                 $viewer .= "view_po.php";
23         elseif ($type == ST_SUPPINVOICE)
24                 $viewer .= "view_supp_invoice.php";
25         elseif ($type == ST_SUPPCREDIT)
26                 $viewer .= "view_supp_credit.php";
27         elseif ($type == ST_SUPPAYMENT)
28                 $viewer .= "view_supp_payment.php";
29         elseif ($type == ST_SUPPRECEIVE)
30                 $viewer .= "view_grn.php";
31         else
32                 return null;
33         $viewer .= "?trans_no=$trans_no";
34
35         if ($label == "")
36                 $label = $trans_no;
37
38         return viewer_link($label, $viewer, $class, $id,  $icon);
39 }
40
41 //--------------------------------------------------------------------------------------
42
43 function get_gl_view_str($type, $trans_no, $label="", $force=false, $class='', $id='')
44 {
45         if (!$force && !user_show_gl_info())
46                 return "";
47
48         $icon = false;
49         if ($label == "")
50         {
51                 $label = _("GL");
52                 $icon = ICON_GL;
53         }       
54
55         return viewer_link($label, 
56                 "gl/view/gl_trans_view.php?type_id=$type&trans_no=$trans_no", 
57                 $class, $id, $icon);
58 }
59
60 //--------------------------------------------------------------------------------------
61
62 function get_gl_view_str_cell($type, $trans_no, $label="")
63 {
64         $str = get_gl_view_str($type, $trans_no, $label);
65         if ($str != "")
66                 return "<td>$str</td>";
67 }
68
69 //--------------------------------------------------------------------------------------
70
71 function get_customer_trans_view_str($type, $trans_no, $label="", $icon=false, 
72         $class='', $id='')
73 {
74         $viewer = "sales/view/";
75
76         if ($type == ST_SALESINVOICE)
77                 $viewer .= "view_invoice.php";
78         elseif ($type == ST_CUSTCREDIT)
79                 $viewer .= "view_credit.php";
80         elseif ($type == ST_CUSTPAYMENT)
81                 $viewer .= "view_receipt.php";
82         elseif ($type == ST_CUSTDELIVERY)
83                 $viewer .= "view_dispatch.php";
84         elseif ($type == ST_SALESORDER || $type == ST_SALESQUOTE)
85                 $viewer .= "view_sales_order.php";
86         else
87                 return null;
88
89   if(!is_array($trans_no)) $trans_no = array($trans_no);
90
91   $lbl = $label;
92   $preview_str = '';
93
94   foreach($trans_no as $trans) {
95         if ($label == "")
96                 $lbl = $trans;
97         if($preview_str!='') $preview_str .= ',';
98
99         $preview_str .= viewer_link($lbl, $viewer."?trans_no=$trans&trans_type=$type", 
100                 $class, $id, $icon);
101
102   }
103   return $preview_str;
104 }
105
106 //--------------------------------------------------------------------------------------
107
108 function get_banking_trans_view_str($type, $trans_no, $label="", 
109         $icon=false, $class='', $id='')
110 {
111         if ($label == "")
112                 $label = $trans_no;
113
114         if ($type == ST_BANKTRANSFER)
115                 $viewer = "bank_transfer_view.php";
116         elseif ($type == ST_BANKPAYMENT)
117                 $viewer = "gl_payment_view.php";
118         elseif ($type == ST_BANKDEPOSIT)
119                 $viewer = "gl_deposit_view.php";
120         else
121                 return null;
122
123         return viewer_link($label, "gl/view/$viewer?trans_no=$trans_no", 
124                 $class, $id,  $icon);
125 }
126
127 //--------------------------------------------------------------------------------------
128
129 function get_inventory_trans_view_str($type, $trans_no, $label="", 
130         $icon=false, $class='', $id='')
131 {
132         $viewer = "inventory/view/";
133
134         if ($type == ST_INVADJUST)
135                 $viewer .= "view_adjustment.php";
136         elseif ($type == ST_LOCTRANSFER)
137                 $viewer .= "view_transfer.php";
138         else
139                 return null;
140         $viewer .= "?trans_no=$trans_no";
141
142         if ($label == "")
143                 $label = $trans_no;
144
145         return viewer_link($label, $viewer, $class, $id,  $icon);
146 }
147
148 //--------------------------------------------------------------------------------------
149
150 function get_manufacturing_trans_view_str($type, $trans_no, $label="", 
151         $icon=false, $class='', $id='')
152 {
153         $viewer = "manufacturing/view/";
154
155         if ($type == ST_MANUISSUE)
156                 $viewer .= "wo_issue_view.php";
157         elseif ($type == ST_MANURECEIVE)
158                 $viewer .= "wo_production_view.php";
159         elseif ($type == ST_WORKORDER)
160                 $viewer .= "work_order_view.php";
161         else
162                 return null;
163
164         $viewer .= "?trans_no=$trans_no";
165
166         if ($label == "")
167                 $label = $trans_no;
168
169         return viewer_link($label, $viewer, $class, $id,  $icon);
170 }
171
172 //--------------------------------------------------------------------------------------
173
174 function get_dimensions_trans_view_str($type, $trans_no, $label="", $icon=false, 
175         $class='', $id='')
176 {
177         if ($type == ST_DIMENSION)
178                 $viewer = "dimensions/view/view_dimension.php?trans_no=$trans_no";
179         else
180                 return null;
181
182         if ($label == "")
183                 $label = $trans_no;
184
185         return viewer_link($label, $viewer, $class, $id,  $icon);
186 }
187 /*
188         Journal entry or cost update postings link
189 */
190 function get_journal_trans_view_str($type, $trans_no, $label="", $icon=false, 
191         $class='', $id='')
192 {
193         if ($type == ST_JOURNAL || $type == ST_COSTUPDATE)
194                 $viewer = "gl/view/gl_trans_view.php?type_id=$type&trans_no=$trans_no";
195         else
196                 return null;
197
198         if ($label == "")
199                 $label = $trans_no;
200
201         return viewer_link($label, $viewer, $class, $id,  $icon);
202 }
203
204 //--------------------------------------------------------------------------------------
205
206 function get_package_view_str($pkg, $label="", $icon=false, $class='', $id='')
207 {
208         if ($label == "")
209         {
210                 $label = _("Info");
211 //              $icon = ICON_GL;
212         }
213         return viewer_link($label, "includes/ui/view_package.php?id=$pkg", $class, $id, $icon);
214 }
215
216
217 //--------------------------------------------------------------------------------------
218
219 function get_trans_view_str($type, $trans_no, $label="", $icon=false, 
220         $class='', $id='')
221 {
222         $view_str = get_customer_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
223         if ($view_str != null)
224                 return $view_str;
225
226         $view_str = get_supplier_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
227         if ($view_str != null)
228                 return $view_str;
229
230         $view_str = get_banking_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
231         if ($view_str != null)
232                 return $view_str;
233
234         $view_str = get_inventory_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
235         if ($view_str != null)
236                 return $view_str;
237
238         $view_str = get_manufacturing_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
239         if ($view_str != null)
240                 return $view_str;
241
242         $view_str = get_dimensions_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
243         if ($view_str != null)
244                 return $view_str;
245
246         $view_str = get_journal_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
247         if ($view_str != null)
248                 return $view_str;
249
250         return null;
251 }
252
253 /*
254         Helper for ui drawing functions.
255         Checks whether any of input function parameters has changed or page is called with GET method (first page display).
256         $name - context
257 */
258 function check_ui_refresh($name=null)
259 {
260         $bt = debug_backtrace();
261         if (!$name)
262                 $name = $bt[1]['function'];
263         $old = @$_SESSION['ui_context'][$name];
264         $new = $_SESSION['ui_context'][$name] = $bt[1]['args'];
265         return ($new != $old) || ($_SERVER['REQUEST_METHOD'] == 'GET');
266 }
267
268 //--------------------------------------------------------------------------------------
269 // Displays currency exchange rate for given date.
270 // When there is no exrate for today, 
271 // gets it form ECB and stores in local database.
272 //
273 function exchange_rate_display($from_currency, $to_currency, $date_, $force_edit=false)
274 {
275     global $Ajax;
276
277         $readonly = false;
278
279         if ($from_currency != $to_currency)
280         {
281                 $rate = get_post('_ex_rate');
282                 if (!$rate || check_ui_refresh()) { // readonly or ui context changed
283                         $comp_currency = get_company_currency();
284                         if ($from_currency == $comp_currency)
285                                 $currency = $to_currency;
286                         else
287                                 $currency = $from_currency;
288
289                         $rate = get_date_exchange_rate($currency, $date_); // try local
290                         if ($rate)
291                                 $readonly = true;
292                         if (!$rate) {   // retry from remote service
293                                 $row = get_currency($currency);
294                                 if ($row['auto_update'])
295                                         $rate = retrieve_exrate($currency, $date_);
296                                 if ($rate)
297                                         add_new_exchange_rate($currency, $date_, $rate);
298                                 else
299                                 {
300                                         display_warning(sprintf(_("Cannot retrieve exchange rate for currency %s. Please adjust approximate rate if needed."), $currency));
301                                         $readonly = false;
302                                 }
303                         }
304                         if (!$rate)     {       // get and edit latest available
305                                 $readonly = false;
306                                 $rate = get_exchange_rate_from_home_currency($currency, $date_);
307                         }
308                         if ($from_currency != $comp_currency)
309                                 $rate = 1 / ($rate / get_exchange_rate_from_home_currency($to_currency, $date_));
310
311                         $Ajax->activate('_ex_rate_span');
312                 }
313
314                 $rate = number_format2($rate, user_exrate_dec());
315
316                 if ($force_edit || !$readonly)
317                         $ctrl = "<input type=\"text\" name=\"_ex_rate\" size=\"8\" maxlength=\"8\" value=\"$rate\">";
318             else
319                 $ctrl = "<span id=\"_ex_rate\">$rate</span>";
320
321                 label_row(_("Exchange Rate:"), $span = "<span style='vertical-align:top;' id='_ex_rate_span'>$ctrl $from_currency = 1 $to_currency</span>" );
322
323                 $Ajax->addUpdate('_ex_rate_span', '_ex_rate_span', $span);
324         }
325 }
326
327 //--------------------------------------------------------------------------------------
328
329 function is_voided_display($type, $id, $label)
330 {
331         $void_entry = get_voided_entry($type, $id);
332
333         if ($void_entry == null)
334                 return false;
335
336         start_table(TABLESTYLE, "width=50%");
337         echo "<tr><td align=center><font color=red>$label</font><br>";
338         echo "<font color=red>" . _("Date Voided:") . " " . sql2date($void_entry["date_"]) . "</font><br>";
339         if (strlen($void_entry["memo_"]) > 0)
340                 echo "<center><font color=red>" . _("Memo:") . " " . $void_entry["memo_"] . "</font></center><br>";
341         echo "</td></tr>";
342         end_table(1);
343
344         return true;
345 }
346
347 //--------------------------------------------------------------------------------------
348
349 function comments_display_row($type, $id)
350 {
351         $comments = get_comments($type, $id);
352         if ($comments and db_num_rows($comments))
353         {
354                 echo "<tr><td colspan=15>";
355         while ($comment = db_fetch($comments))
356         {
357                 echo $comment["memo_"] . "<br>";
358         }
359                 echo "</td></tr>";
360         }
361 }
362
363 //--------------------------------------------------------------------------------------
364
365 function get_comments_string($type, $type_no)
366 {
367         $str_return = "";
368         $result = get_comments($type, $type_no);
369         while ($comment = db_fetch($result))
370         {
371                 if (strlen($str_return))
372                         $str_return = $str_return . " \n";
373                 $str_return = $str_return . $comment["memo_"];
374         }
375         return $str_return;
376 }
377
378 //--------------------------------------------------------------------------------------
379
380 function view_stock_status($stock_id, $description=null, $echo=true)
381 {
382         global $path_to_root;
383         if ($description)
384                 //hyperlink_params_separate($path_to_root . "/inventory/inquiry/stock_status.php", (user_show_codes()?$stock_id . " - ":"") . $description, "stock_id=$stock_id");
385                 $preview_str = "<a target='_blank' href='$path_to_root/inventory/inquiry/stock_status.php?stock_id=$stock_id' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >". (user_show_codes()?$stock_id . " - ":"") . $description."</a>";
386         else
387                 //hyperlink_params_separate($path_to_root . "/inventory/inquiry/stock_status.php", $stock_id, "stock_id=$stock_id");
388                 $preview_str = "<a target='_blank' href='$path_to_root/inventory/inquiry/stock_status.php?stock_id=$stock_id' onclick=\"javascript:openWindow(this.href,this.target); return false;\" >$stock_id</a>";
389         if($echo)
390                 echo $preview_str;
391         return $preview_str;
392 }
393
394 function view_stock_status_cell($stock_id, $description=null)
395 {
396         echo "<td>";
397         view_stock_status($stock_id, $description);
398         echo "</td>";
399 }
400
401 //--------------------------------------------------------------------------------------
402
403 function display_debit_or_credit_cells($value, $bold=false)
404 {
405         $value = round2($value, user_price_dec());
406         if ($value >= 0)
407         {
408                 amount_cell($value, $bold);
409                 label_cell("");
410         }
411         elseif ($value < 0)
412         {
413                 label_cell("");
414                 amount_cell(abs($value), $bold);
415         }
416 }
417
418 //--------------------------------------------------------------------------------------
419
420 function display_customer_trans_tax_details($tax_items, $columns)
421 {
422         global $alternative_tax_include_on_docs, $suppress_tax_rates;
423         $first = true;
424     while ($tax_item = db_fetch($tax_items))
425     {
426         if (!$tax_item['amount'])
427                 continue;
428
429         $tax = number_format2($tax_item['amount'],user_price_dec());
430                 if (isset($suppress_tax_rates) && $suppress_tax_rates == 1)
431                         $tax_type_name = $tax_item['tax_type_name'];
432                 else
433                         $tax_type_name = $tax_item['tax_type_name']." (".$tax_item['rate']."%) ";
434         if ($tax_item['included_in_price'])
435         {
436                         if (isset($alternative_tax_include_on_docs) && $alternative_tax_include_on_docs == 1)
437                         {
438                                 if ($first)
439                                         label_row(_("Total Tax Excluded"), number_format2($tax_item['net_amount'], user_price_dec()),
440                                                 "colspan=$columns align=right", "align=right");
441                         label_row($tax_type_name, $tax, "colspan=$columns align=right", "align=right");
442                         $first = false;
443                 }
444                 else
445                         label_row(_("Included") . " " . $tax_type_name
446                                 . ": $tax", "", "colspan=$columns align=right", "align=right");
447         }                       
448         else
449                 label_row($tax_type_name, $tax, "colspan=$columns align=right", "align=right");
450     }
451 }
452
453 //--------------------------------------------------------------------------------------
454
455 function display_supp_trans_tax_details($tax_items, $columns)
456 {
457     while ($tax_item = db_fetch($tax_items))
458     {
459         if (!$tax_item['amount'])
460                 continue;
461
462         $tax = number_format2(abs($tax_item['amount']),user_price_dec());
463         if ($tax_item['included_in_price'])
464                 label_row(_("Included") . " " . $tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%) "
465                         . ": $tax", '', "colspan=$columns align=right", "align=right");
466         else
467                 label_row($tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%)",
468                         $tax, "colspan=$columns align=right", "align=right");
469     }
470 }
471
472 //--------------------------------------------------------------------------------------
473
474 function display_edit_tax_items($taxes, $columns, $tax_included, $rightspan=0, $editable=false)
475 {
476         $total = 0;
477
478     foreach ($taxes as $taxitem)
479         {
480                 $amount = isset($taxitem['Override']) ? $taxitem['Override'] : $taxitem['Value'];
481                 if ($taxitem['Value'] != 0){
482                         if ($editable) {
483                                 if (!isset($_POST['mantax['.$taxitem['tax_type_id'].']']))
484                                         $_POST['mantax['.$taxitem['tax_type_id'].']'] = price_format($amount);
485                                 start_row();
486                                 if ($tax_included) {
487                                         $colspan = $columns-1;
488                                         label_cell(_("Included") . " " . $taxitem['tax_type_name'].":",
489                                                 "colspan={$colspan} align='right'");
490                                         amount_cells(null, 'mantax['.$taxitem['tax_type_id'].']',
491                                                 null, "colspan=$columns align=right", '<td></td>', user_price_dec());
492                                 } else {
493                                         label_cell($taxitem['tax_type_name'], "colspan=$columns align='right'");
494                                         amount_cells(null, 'mantax['.$taxitem['tax_type_id'].']',
495                                                 null, "colspan=$columns align=right", null, user_price_dec());
496                                         $total +=  round2($amount, user_price_dec());
497                                 }
498                                 if ($rightspan)
499                                         label_cell('', "colspan ='$rightspan'");
500                                 end_row();
501                         } else {
502                                 $value = number_format2($taxitem['Value'],user_price_dec());
503                                 if ($tax_included)
504                                 {
505                                         label_row(_("Included") . " " . $taxitem['tax_type_name']
506                                                 . " " . $value , "", "colspan=$columns align=right", "align=right", $rightspan);
507                                 }
508                                 else
509                                 {
510                                         label_row($taxitem['tax_type_name'],
511                                                 $value, "colspan=$columns align=right", "align=right", $rightspan);
512                                         $total +=  round2($taxitem['Value'], user_price_dec());
513                                 }
514                         }
515                 }
516         }
517         return $total;
518 }
519
520 //--------------------------------------------------------------------------------------
521
522 function display_footer_exit($space=2)
523 {
524         if ($space)
525                 br($space);
526         end_page(false, false, true);
527         exit;
528 }
529
530 //--------------------------------------------------------------------------------------
531
532 function display_allocations($alloc_result, $total, $payments=false)
533 {
534         global $systypes_array;
535
536         if (!$alloc_result || db_num_rows($alloc_result) == 0)
537                 return;
538
539     display_heading2(($payments ? _("Payments") : _("Allocations")));
540
541     start_table(TABLESTYLE, "width=80%");
542
543     $th = array( _("Type"), _("Number"), _("Date"), _("Total Amount"),
544         _("Left to Allocate"), _("This Allocation"));
545         table_header($th);
546     $k = $total_allocated = 0;
547
548     while ($alloc_row = db_fetch($alloc_result))
549     {
550
551         alt_table_row_color($k);
552
553         label_cell($systypes_array[$alloc_row['type']]);
554         label_cell(get_trans_view_str($alloc_row['type'],$alloc_row['trans_no']));
555         label_cell(sql2date($alloc_row['tran_date']));
556         $alloc_row['Total'] = round2($alloc_row['Total'], user_price_dec());
557         $alloc_row['amt'] = round2($alloc_row['amt'], user_price_dec());
558         if ($payments && ($alloc_row['type'] == ST_SUPPAYMENT || $alloc_row['type'] == ST_BANKPAYMENT || $alloc_row['type'] == ST_SUPPCREDIT))
559                 $alloc_row['Total'] = -$alloc_row['Total'];
560         amount_cell($alloc_row['Total']);
561                 amount_cell($alloc_row['Total'] - $alloc_row['amt']);
562         amount_cell($alloc_row['amt']);
563         end_row();
564
565         $total_allocated += $alloc_row['amt'];
566     }
567     start_row();
568         label_cell(_("Total Allocated:"), "align=right colspan=5");
569         amount_cell($total_allocated);
570         end_row();
571         start_row();
572     label_cell(_("Left to Allocate:"), "align=right colspan=5");
573     $total = round2($total, user_price_dec());
574     amount_cell($total - $total_allocated);
575     end_row();
576
577     end_table(1);
578 }
579
580 //--------------------------------------------------------------------------------------
581
582 function display_allocations_from($person_type, $person_id, $type, $type_no, $total)
583 {
584         switch ($person_type)
585         {
586                 case PT_CUSTOMER :
587                         $alloc_result = get_allocatable_to_cust_transactions($person_id, $type_no, $type);
588                         display_allocations($alloc_result, $total);
589                         return;
590                 case PT_SUPPLIER :
591                         $alloc_result = get_allocatable_to_supp_transactions($person_id, $type_no, $type);
592                         display_allocations($alloc_result, $total);
593                         return;
594         }
595 }
596
597 //--------------------------------------------------------------------------------------
598
599 function display_allocations_to($person_type, $person_id, $type, $type_no, $total)
600 {
601         switch ($person_type)
602         {
603                 case PT_CUSTOMER :
604                         $alloc_result = get_allocatable_from_cust_transactions($person_id, $type_no, $type);
605                         display_allocations($alloc_result, $total, true);
606                         return;
607                 case PT_SUPPLIER :
608                         $alloc_result = get_allocatable_from_supp_transactions($person_id, $type_no, $type);
609                         display_allocations($alloc_result, $total, true);
610                         return;
611         }
612 }
613
614 //--------------------------------------------------------------------------------------
615 //
616 //      Expands selected quick entry $id into GL posings and adds to cart.
617 //              returns calculated amount posted to bank GL account.
618 //
619 function display_quick_entries(&$cart, $id, $base, $type, $descr='')
620 {
621         $bank_amount = 0;
622         
623         if (!isset($id) || $id == null || $id == "")
624         {
625                 display_error( _("No Quick Entries are defined."));
626                 set_focus('totamount');
627         }
628         else
629         {
630                 if ($type == QE_DEPOSIT)
631                         $base = -$base;
632                 if ($type != QE_SUPPINV)        // only one quick entry on journal/bank transaction
633                         $cart->clear_items();
634                 $qe = get_quick_entry($id);
635                 if ($qe['bal_type'] == 1)
636                 {
637                         if ($qe['base_amount'] == 1.0) // monthly
638                                 $begin = begin_month($cart->tran_date);
639                         else
640                         {
641                                 if (is_account_balancesheet($qe['base_desc'])) // total
642                                         $begin = "";
643                                 else
644                                         $begin = begin_fiscalyear(); // from fiscalyear begin
645                         }               
646                         $base = get_gl_trans_from_to($begin, $cart->tran_date, $qe['base_desc']);
647                                 
648                 }
649                 if ($descr != '') $qe['description'] .= ': '.$descr;
650                 $result = get_quick_entry_lines($id);
651                 if (db_num_rows($result) == 0)
652                 {
653                         display_error( _("No Quick Entry lines are defined."));
654                         set_focus('totamount');
655                         return 0;
656                 }       
657                 $totrate = 0;
658                 while ($row = db_fetch($result))
659                 {
660                         $qe_lines[] = $row;
661
662                         switch (strtolower($row['action'])) {
663                                 case "t": // post taxes calculated on base amount
664                                 case "t+": // ditto & increase base amount
665                                 case "t-": // ditto & reduce base amount
666                                         if (substr($row['action'],0,1) != 'T') 
667                                                 $totrate += get_tax_type_default_rate($row['dest_id']);
668                         }
669                 }
670                 $first = true;
671                 $taxbase = 0;
672                 foreach($qe_lines as $qe_line)
673                 {
674                         switch (strtolower($qe_line['action'])) {
675                                 case "=": // post current base amount to GL account
676                                         $part = $base;
677                                         break;
678                                 case "a": // post amount to GL account and reduce base
679                                         $part = $qe_line['amount'];
680                                         break;
681                                 case "a+": // post amount to GL account and increase base
682                                         $part = $qe_line['amount']; $base += $part;
683                                         break;
684                                 case "a-": // post amount to GL account and reduce base
685                                         $part = $qe_line['amount']; $base -= $part;
686                                         break;
687                                 case "%":       // store acc*amount% to GL account
688                                         $part = round2($base * $qe_line['amount'] / 100, user_price_dec());
689                                         break;
690                                 case "%+":      // ditto & increase base amount
691                                         $part = round2($base * $qe_line['amount'] / 100, user_price_dec());
692                                         $base += $part;
693                                         break;
694                                 case "%-":      // ditto & reduce base amount
695                                         $part = round2($base * $qe_line['amount'] / 100, user_price_dec());
696                                         $base -= $part;
697                                         break;
698                                 case "t": // post taxes calculated on base amount
699                                 case "t+": // ditto & increase base amount
700                                 case "t-": // ditto & reduce base amount
701                                         if ($first)
702                                         {
703                                                 $taxbase = $base/($totrate+100);
704                                                 $first = false;
705                                         }
706
707                                         if (substr($qe_line['action'],0,1) != 'T') 
708                                                 $part = $taxbase;
709                                         else
710                                                 $part = $base/100;
711                                         $item_tax = get_tax_type($qe_line['dest_id']);
712                                         //if ($type == QE_SUPPINV && substr($qe_line['action'],0,1) != 'T')
713                                         if ($type == QE_SUPPINV)
714                                         {
715                                                 $taxgroup = $cart->tax_group_id;
716                                                 $rates = 0;
717                                                 $res = get_tax_group_rates($cart->tax_group_id);
718                                                 while ($row = db_fetch($res))
719                                                         $rates += $row['rate'];
720                                                 if ($rates == 0)
721                                                         continue 2;
722                                         }
723                                         $tax = round2($part * $item_tax['rate'],  user_price_dec());
724                                         if ($tax==0) continue 2;
725                                         $gl_code = ($type == QE_DEPOSIT || ($type == QE_JOURNAL && $base < 0)) 
726                                                 ? $item_tax['sales_gl_code'] : $item_tax['purchasing_gl_code'];
727                                         if (!is_tax_gl_unique($gl_code)) {
728                                                 display_error(_("Cannot post to GL account used by more than one tax type."));
729                                                 break 2;
730                                         }
731                                         if ($type != QE_SUPPINV)
732                                                 $cart->add_gl_item($gl_code, 
733                                                         $qe_line['dimension_id'], $qe_line['dimension2_id'], 
734                                                         $tax, $qe['description']);
735                                         else 
736                                         {
737                                                 $acc_name = get_gl_account_name($gl_code);
738                                                 $cart->add_gl_codes_to_trans($gl_code, 
739                                                         $acc_name, $qe_line['dimension_id'], 
740                                                         $qe_line['dimension2_id'], $tax, $qe['description']);
741                                         }
742                                         if (strpos($qe_line['action'], '+'))
743                                                 $base += $tax;
744                                         elseif (strpos($qe_line['action'], '-'))
745                                                 $base -= $tax;
746                                         continue 2;
747                         }
748                         if ($type != QE_SUPPINV)
749                                 $cart->add_gl_item($qe_line['dest_id'], $qe_line['dimension_id'],
750                                         $qe_line['dimension2_id'], $part, $qe['description']);
751                         else 
752                         {
753                                 $acc_name = get_gl_account_name($qe_line['dest_id']);
754                                 $cart->add_gl_codes_to_trans($qe_line['dest_id'], 
755                                         $acc_name, $qe_line['dimension_id'], 
756                                         $qe_line['dimension2_id'], $part, $qe['description']);
757                         }
758                 }
759         }       
760         return $bank_amount;
761 }
762
763 //--------------------------------------------------------------------------------------
764 //
765 //      Simple English version of number to words conversion.
766 //
767 function _number_to_words($number) 
768
769     $Bn = floor($number / 1000000000); /* Billions (giga) */ 
770     $number -= $Bn * 1000000000; 
771     $Gn = floor($number / 1000000);  /* Millions (mega) */ 
772     $number -= $Gn * 1000000; 
773     $kn = floor($number / 1000);     /* Thousands (kilo) */ 
774     $number -= $kn * 1000; 
775     $Hn = floor($number / 100);      /* Hundreds (hecto) */ 
776     $number -= $Hn * 100; 
777     $Dn = floor($number / 10);       /* Tens (deca) */ 
778     $n = $number % 10;               /* Ones */
779
780     $res = ""; 
781
782     if ($Bn) 
783         $res .= _number_to_words($Bn) . " Billion"; 
784     if ($Gn) 
785         $res .= (empty($res) ? "" : " ") . _number_to_words($Gn) . " Million"; 
786     if ($kn) 
787         $res .= (empty($res) ? "" : " ") . _number_to_words($kn) . " Thousand"; 
788     if ($Hn) 
789         $res .= (empty($res) ? "" : " ") . _number_to_words($Hn) . " Hundred"; 
790
791     $ones = array("", "One", "Two", "Three", "Four", "Five", "Six", 
792         "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", 
793         "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", 
794         "Nineteen"); 
795     $tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", 
796         "Seventy", "Eighty", "Ninety"); 
797
798     if ($Dn || $n) 
799     { 
800         if (!empty($res)) 
801             $res .= " and "; 
802         if ($Dn < 2) 
803             $res .= $ones[$Dn * 10 + $n]; 
804         else 
805         { 
806             $res .= $tens[$Dn]; 
807             if ($n) 
808                 $res .= "-" . $ones[$n]; 
809         } 
810     } 
811
812     if (empty($res)) 
813         $res = "zero"; 
814     return $res; 
815
816
817 function price_in_words($amount, $document=0)
818 {
819         // use local price_in_words() if the hook is defined
820         $price = hook_price_in_words($amount, $document);
821         if ($price) 
822                 return $price;
823
824         // Only usefor Remittance and Receipts as default
825         if (!($document == ST_SUPPAYMENT || $document == ST_CUSTPAYMENT || $document == ST_CHEQUE))
826                 return "";
827         if ($amount < 0 || $amount > 999999999999)
828                 return "";
829         $dec = user_price_dec();
830         if ($dec > 0)
831         {
832                 $divisor = pow(10, $dec);
833         $frac = round2($amount - floor($amount), $dec) * $divisor;
834                 $frac = sprintf("%0{$dec}d", round2($frac, 0));
835                 $and = _("and");
836         $frac = " $and $frac/$divisor";
837     }
838     else
839         $frac = "";
840     return _number_to_words(intval($amount)) . $frac;
841 }    
842
843 function get_js_open_window($width, $height)
844 {
845         $js ="function openWindow(url, title)\n"
846                 . "{\n"
847                 . " var left = (screen.width - $width) / 2;\n"
848                 . " var top = (screen.height - $height) / 2;\n"
849                 . " return window.open(url, title, 'width=$width,height=$height,left='+left+',top='+top+',screenX='+left+',screenY='+top+',status=no,scrollbars=yes');\n"
850                 . "}\n";
851         return $js;
852 }
853
854 /*
855   Setting focus on element $name in $form.
856   If $form<0 $name is element id.
857 */
858 function set_focus($name, $form_no=0) {
859   global $Ajax;
860         $Ajax->addFocus(true, $name);
861     $_POST['_focus'] = $name;
862 }
863 //
864 //      Set default focus on first field $name if not set yet
865 //      Returns unique name if $name=null
866 //      
867 function default_focus($name=null, $form_no=0) {
868         static $next; 
869         if ($name==null) 
870                 $name = uniqid('_el',true);
871     if (!isset($_POST['_focus'])) {
872           set_focus($name);
873     }
874         return $name;
875 }
876 /*
877         Reset focus to next control element (e.g. link).
878 */
879 function reset_focus()
880 {
881         unset($_POST['_focus']);
882 }
883
884 function get_js_date_picker()
885 {
886     global $go_debug;
887     $fpath = company_path().'/js_cache/'.'date_picker.js';
888
889     if (!file_exists($fpath) || $go_debug) {
890
891         global $dateseps, $date_system, $tmonths;
892
893         $how = user_date_format();                              // 0 = us/ca, 1 = eu, au, nz, 2 = jp, sw
894         $sep = $dateseps[user_date_sep()];              // date separator
895         $wstart = (($date_system == 1 || $date_system == 2 || $date_system == 3) ? 6 : ($how == 0 || $how == 3 ? 0 : 1));       // weekstart (sun = 0, mon = 1)
896         $months = array(_("January"),_("February"),_("March"),_("April"),_("May"),_("June"),_("July"),_("August"),_("September"),_("October"),_("November"),_("December"));
897         $wdays = array(_("Su"),_("Mo"),_("Tu"),_("We"),_("Th"),_("Fr"),_("Sa"));
898         $wno = _("W"); // week no
899         $back = _("Back");
900         if ($date_system == 1)
901                 list($cyear, $cmon, $cday) = gregorian_to_jalali(date("Y"), date("n"), date("j"));
902         elseif ($date_system == 2)
903                 list($cyear, $cmon, $cday) = gregorian_to_islamic(date("Y"), date("n"), date("j"));
904
905
906         $js = "
907 function positionInfo(object) {
908   var p_elm = object;
909   this.getElementLeft = getElementLeft;
910   function getElementLeft() {
911     var x = 0;
912     var elm;
913     if(typeof(p_elm) == 'object'){
914       elm = p_elm;
915     } else {
916       elm = document.getElementById(p_elm);
917     }
918     while (elm != null) {
919       x+= elm.offsetLeft;
920       elm = elm.offsetParent;
921     }
922     return parseInt(x);
923   }
924   this.getElementWidth = getElementWidth;
925   function getElementWidth(){
926     var elm;
927     if(typeof(p_elm) == 'object'){
928       elm = p_elm;
929     } else {
930       elm = document.getElementById(p_elm);
931     }
932     return parseInt(elm.offsetWidth);
933   }
934   this.getElementRight = getElementRight;
935   function getElementRight(){
936     return getElementLeft(p_elm) + getElementWidth(p_elm);
937   }
938   this.getElementTop = getElementTop;
939   function getElementTop() {
940     var y = 0;
941     var elm;
942     if(typeof(p_elm) == 'object'){
943       elm = p_elm;
944     } else {
945       elm = document.getElementById(p_elm);
946     }
947     while (elm != null) {
948       y+= elm.offsetTop;
949       elm = elm.offsetParent;
950     }
951     return parseInt(y);
952   }
953   this.getElementHeight = getElementHeight;
954   function getElementHeight(){
955     var elm;
956     if(typeof(p_elm) == 'object'){
957       elm = p_elm;
958     } else {
959       elm = document.getElementById(p_elm);
960     }
961     return parseInt(elm.offsetHeight);
962   }
963   this.getElementBottom = getElementBottom;
964   function getElementBottom(){
965     return getElementTop(p_elm) + getElementHeight(p_elm);
966   }
967 }
968 function CC() {
969   var calendarId = 'CC';
970   var currentYear = 0;
971   var currentMonth = 0;
972   var currentDay = 0;
973   var selectedYear = 0;
974   var selectedMonth = 0;
975   var selectedDay = 0;
976   var months = ['$months[0]','$months[1]','$months[2]','$months[3]','$months[4]','$months[5]','$months[6]','$months[7]','$months[8]','$months[9]','$months[10]','$months[11]'];
977   var wdays = ['$wdays[0]', '$wdays[1]', '$wdays[2]', '$wdays[3]', '$wdays[4]', '$wdays[5]', '$wdays[6]'];
978   var tmonths = ['$tmonths[0]','$tmonths[1]','$tmonths[2]','$tmonths[3]','$tmonths[4]','$tmonths[5]','$tmonths[6]','$tmonths[7]','$tmonths[8]','$tmonths[9]','$tmonths[10]','$tmonths[11]','$tmonths[12]'];
979   var dateField = null;
980   function getProperty(p_property){
981     var p_elm = calendarId;
982     var elm = null;
983     if(typeof(p_elm) == 'object'){
984       elm = p_elm;
985     } else {
986       elm = document.getElementById(p_elm);
987     }
988     if (elm != null){
989       if(elm.style){
990         elm = elm.style;
991         if(elm[p_property]){
992           return elm[p_property];
993         } else {
994           return null;
995         }
996       } else {
997         return null;
998       }
999     }
1000   }
1001   function setElementProperty(p_property, p_value, p_elmId){
1002     var p_elm = p_elmId;
1003     var elm = null;
1004     if(typeof(p_elm) == 'object'){
1005       elm = p_elm;
1006     } else {
1007       elm = document.getElementById(p_elm);
1008     }
1009     if((elm != null) && (elm.style != null)){
1010       elm = elm.style;
1011       elm[ p_property ] = p_value;
1012     }
1013   }
1014   function setProperty(p_property, p_value) {
1015     setElementProperty(p_property, p_value, calendarId);
1016   }
1017   function getDaysInMonth(year, month) {
1018 ";
1019         if ($date_system == 1)
1020                 $js .= "
1021     return [31,31,31,31,31,31,30,30,30,30,30,(((((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682 ? 30 : 29)][month-1];
1022 ";
1023         elseif ($date_system == 2)
1024                 $js .= "
1025     return [30,29,30,29,30,29,30,29,30,29,30,(((((11 * year) + 14) % 30) < 11) ? 30 : 29)][month-1];
1026 ";
1027         else
1028                 $js .= "
1029     return [31,((!(year % 4 ) && ( (year % 100 ) || !( year % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][month-1];
1030 ";
1031         $js .= "
1032   }
1033   function getDayOfWeek(year, month, day) {
1034 ";
1035         if ($date_system == 1 || $date_system == 2)
1036         {
1037                 $js .= "
1038         function mod(a, b)
1039         {
1040             return a - (b * Math.floor(a / b));
1041         }
1042         function jwday(j)
1043         {
1044             return mod(Math.floor((j + 1.5)), 7);
1045         }
1046 ";
1047         if ($date_system == 1)
1048                 $js .= "
1049     var epbase, epyear, t;
1050     epbase = year - ((year >= 0) ? 474 : 473);
1051     epyear = 474 + mod(epbase, 2820);
1052     t = day + ((month <= 7) ? ((month - 1) * 31) : (((month - 1) * 30) + 6)) +
1053       Math.floor(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 +
1054       Math.floor(epbase / 2820) * 1029983 + (1948320.5 - 1);
1055 ";
1056         elseif ($date_system == 2)
1057                 $js .= "
1058         var t;
1059         t = Math.floor((11 * year + 3) / 30) + 354 * year + 30 * month -
1060           Math.floor((month - 1) / 2) + day + 1948440 - 385;
1061 ";
1062         $js .= "
1063     return jwday(t);
1064 ";
1065         }
1066         else
1067                 $js .= "
1068     var date = new Date(year,month-1,day)
1069     return date.getDay();
1070 ";
1071         $js .= "
1072   }
1073   this.clearDate = clearDate;
1074   function clearDate() {
1075     dateField.value = '';
1076     hide();
1077   }
1078   this.getWeek = getWeek;
1079   function getWeek(year, month, day) {
1080 ";
1081         if ($how == 0 || $how == 3)
1082                 $js .= "  day++;";
1083         $js .= "
1084     var date = new Date(year,month-1,day);
1085     var D = date.getDay();
1086     if(D == 0) D = 7;
1087     date.setDate(date.getDate() + (4 - D));
1088     var YN = date.getFullYear();
1089     var ZBDoCY = Math.floor((date.getTime() - new Date(YN, 0, 1, -6)) / 86400000);
1090     var WN = 1 + Math.floor(ZBDoCY / 7);
1091     return WN;
1092   }
1093   this.setDate = setDate;
1094   function setDate(year, month, day) {
1095     if (dateField){
1096 ";
1097         if ($how < 3)
1098                 $js .= "
1099       if (month < 10) {month = '0' + month;}
1100       if (day < 10) {day = '0' + day;}
1101 ";
1102         else
1103                 $js .= "
1104       month = tmonths[month];
1105 ";
1106         if ($how == 0 || $how == 3)
1107                 $js .= "
1108       var dateString = month+'$sep'+day+'$sep'+year;
1109 ";
1110         elseif ($how == 1 || $how == 4)
1111                 $js .= "
1112       var dateString = day+'$sep'+month+'$sep'+year;
1113 ";
1114         elseif ($how == 2 || $how == 5)
1115                 $js .= "
1116       var dateString = year+'$sep'+month+'$sep'+day;
1117 ";
1118         $js .= "
1119       dateField.value = dateString;
1120           setFocus(dateField.name);
1121         if(dateField.getAttribute('aspect')=='cdate')
1122       setElementProperty('color', (dateField.value==user.date ? 'black':'red'), dateField);
1123           if (dateField.className=='searchbox')
1124                 dateField.onblur();
1125       hide();
1126     }
1127     return;
1128   }
1129   this.changeMonth = changeMonth;
1130   function changeMonth(change) {
1131     currentMonth += change;
1132     currentDay = 0;
1133     if(currentMonth > 12) {
1134       currentMonth = 1;
1135       currentYear++;
1136     } else if(currentMonth < 1) {
1137       currentMonth = 12;
1138       currentYear--;
1139     }
1140     calendar = document.getElementById(calendarId);
1141     calendar.innerHTML = calendarDrawTable();
1142   }
1143   this.changeYear = changeYear;
1144   function changeYear(change) {
1145     currentYear += change;
1146     currentDay = 0;
1147     calendar = document.getElementById(calendarId);
1148     calendar.innerHTML = calendarDrawTable();
1149   }
1150   function getCurrentYear() {
1151     var year = new Date().getYear();
1152     if(year < 1900) year += 1900;
1153     return year;
1154   }
1155   function getCurrentMonth() {
1156     return new Date().getMonth() + 1;
1157   }
1158   function getCurrentDay() {
1159     return new Date().getDate();
1160   }
1161   function calendarDrawTable() {
1162     var dayOfMonth = 1;
1163     var wstart = $wstart;
1164     var wno = '&nbsp;$wno&nbsp;';
1165     var validDay = 0;
1166     var startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
1167     var daysInMonth = getDaysInMonth(currentYear, currentMonth);
1168     var css_class = null; //CSS class for each day
1169     var table = \"<table cellspacing='0' cellpadding='0' border='0'>\";
1170     table += \"<tr class='header'>\";
1171     table += \"  <td colspan='2' class='previous'><a href='javascript:changeCCMonth(-1);'>&lt;</a><br><a href='javascript:changeCCYear(-1);'>&laquo;</a></td>\";
1172     table += \"  <td colspan='4' class='title'>\" + months[currentMonth-1] + \"<br>\" + currentYear + \"</td>\";
1173     table += \"  <td colspan='2' class='next'><a href='javascript:changeCCMonth(1);'>&gt;</a><br><a href='javascript:changeCCYear(1);'>&raquo;</a></td>\";
1174     table += \"</tr>\";
1175     table += \"<tr>\";
1176     table += \"<th class='weekno'>\"+wno+\"</th>\";
1177     for (var n=0; n<7; n++)
1178         table += \"<th>\" + wdays[(wstart+n)%7]+\"</th>\";
1179     table += \"</tr>\";
1180     for(var week=0; week < 6; week++) {
1181       table += \"<tr>\";
1182       for(var n=0; n < 7; n++) {
1183         dayOfWeek = (wstart+n)%7;
1184         if(week == 0 && startDayOfWeek == dayOfWeek) {
1185           validDay = 1;
1186         } else if (validDay == 1 && dayOfMonth > daysInMonth) {
1187           validDay = 0;
1188         }
1189         if (n==0)
1190         {
1191                 if (dayOfMonth > daysInMonth)
1192                         table += \"<td class='empty'>&nbsp;</td>\";
1193                 else
1194                         table += \"<td class='weekno'>\"+getWeek(currentYear, currentMonth, dayOfMonth)+\"</td>\";
1195         }
1196         if(validDay) {
1197           if (dayOfMonth == selectedDay && currentYear == selectedYear && currentMonth == selectedMonth) {
1198             css_class = 'current';
1199 ";
1200         if ($date_system == 1 || $date_system == 2 || $date_system == 3)
1201                 $js .= "
1202           } else if (dayOfWeek == 5) {
1203 ";
1204         else
1205                 $js .= "
1206           } else if (dayOfWeek == 0 || dayOfWeek == 6) {
1207 ";
1208                 $js .= "
1209             css_class = 'weekend';
1210           } else {
1211             css_class = 'weekday';
1212           }
1213           table += \"<td><a class='\"+css_class+\"' href=\\\"javascript:setCCDate(\"+currentYear+\",\"+currentMonth+\",\"+dayOfMonth+\")\\\">\"+dayOfMonth+\"</a></td>\";
1214           dayOfMonth++;
1215         } else {
1216           table += \"<td class='empty'>&nbsp;</td>\";
1217         }
1218       }
1219       table += \"</tr>\";
1220     }
1221     table += \"<tr class='header'><th colspan='8' style='padding: 3px;text-align:center;'><a href='javascript:hideCC();'>$back</a></td></tr>\";
1222     table += \"</table>\";
1223     return table;
1224   }
1225   this.show = show;
1226   function show(field) {
1227     can_hide = 0;
1228     if (dateField == field) {
1229       return;
1230     } else {
1231       dateField = field;
1232     }
1233     if(dateField) {
1234       try {
1235         var dateString = new String(dateField.value);
1236         var dateParts = dateString.split('$sep');
1237 ";
1238         if ($how == 0)
1239                 $js .= "
1240         selectedMonth = parseInt(dateParts[0],10);
1241         selectedDay = parseInt(dateParts[1],10);
1242         selectedYear = parseInt(dateParts[2],10);
1243 ";
1244         elseif ($how == 1)
1245                 $js .= "
1246         selectedDay = parseInt(dateParts[0],10);
1247         selectedMonth = parseInt(dateParts[1],10);
1248         selectedYear = parseInt(dateParts[2],10);
1249 ";
1250         elseif ($how == 2)
1251                 $js .= "
1252         selectedYear = parseInt(dateParts[0],10);
1253         selectedMonth = parseInt(dateParts[1],10);
1254         selectedDay = parseInt(dateParts[2],10);
1255 ";
1256         elseif ($how == 3)
1257                 $js .= "
1258         selectedDay = parseInt(dateParts[1],10);
1259         selectedMonth = parseInt(tmonths.indexOf(dateParts[0]),10);
1260         selectedYear = parseInt(dateParts[2],10);
1261 ";
1262         elseif ($how == 4)
1263                 $js .= "
1264         selectedDay = parseInt(dateParts[0],10);
1265         selectedMonth = parseInt(tmonths.indexOf(dateParts[1]),10);
1266         selectedYear = parseInt(dateParts[2],10);
1267 ";
1268         else
1269                 $js .= "
1270         selectedYear = parseInt(dateParts[0],10);
1271         selectedMonth = parseInt(tmonths.indexOf(dateParts[1]),10);
1272         selectedDay = parseInt(dateParts[2],10);
1273 ";
1274         $js .= "
1275       } catch(e) {}
1276     }
1277     if (!(selectedYear && selectedMonth && selectedDay)) {
1278 ";
1279         if ($date_system == 1 || $date_system == 2)
1280         {
1281                 $js .= "
1282       selectedMonth = $cmon;
1283       selectedDay = $cday;
1284       selectedYear = $cyear;
1285 ";
1286         }
1287         else
1288         {
1289                 $js .= "
1290       selectedMonth = getCurrentMonth();
1291       selectedDay = getCurrentDay();
1292       selectedYear = getCurrentYear();
1293 ";
1294         }
1295         $js .= "
1296     }
1297     currentMonth = selectedMonth;
1298     currentDay = selectedDay;
1299     currentYear = selectedYear;
1300     if(document.getElementById){
1301       calendar = document.getElementById(calendarId);
1302       calendar.innerHTML = calendarDrawTable(currentYear, currentMonth);
1303       var fieldPos = new positionInfo(dateField);
1304       var calendarPos = new positionInfo(calendarId);
1305       var x = fieldPos.getElementLeft();
1306       var y = fieldPos.getElementBottom();
1307       setProperty('left', x + 'px');
1308       setProperty('top', y + 'px');
1309       setProperty('display', 'block');
1310       if (document.all) {
1311         setElementProperty('left', x + 'px', 'CCIframe');
1312         setElementProperty('top', y + 'px', 'CCIframe');
1313         setElementProperty('width', calendarPos.getElementWidth() + 'px', 'CCIframe');
1314         setElementProperty('height', calendarPos.getElementHeight() + 'px', 'CCIframe');
1315         setElementProperty('display', 'block', 'CCIframe');
1316       }
1317     }
1318   }
1319   this.hide = hide;
1320   function hide() {
1321     if(dateField) {
1322       setProperty('display', 'none');
1323       setElementProperty('display', 'none', 'CCIframe');
1324       dateField = null;
1325     }
1326   }
1327   this.visible = visible;
1328   function visible() {
1329     return dateField
1330   }
1331   this.can_hide = can_hide;
1332   var can_hide = 0;
1333 }
1334 var cC = new CC();
1335 function date_picker(textField) {
1336   cC.show(textField);
1337 }
1338 function hideCC() {
1339   if (cC.visible()) {
1340     cC.hide();
1341   }
1342 }
1343 function setCCDate(year, month, day) {
1344   cC.setDate(year, month, day);
1345 }
1346 function changeCCYear(change) {
1347   cC.changeYear(change);
1348 }
1349 function changeCCMonth(change) {
1350   cC.changeMonth(change);
1351 }
1352 document.write(\"<iframe id='CCIframe' src='javascript:false;' frameBorder='0' scrolling='no'></iframe>\");
1353 document.write(\"<div id='CC'></div>\");";
1354
1355      cache_js_file($fpath, $js);
1356     }
1357     add_js_ufile($fpath);
1358
1359  return '';
1360 }
1361
1362 function alert($msg)
1363 {
1364         echo "\n<script type=\"text/javascript\">\n"
1365                 . "<!--\n"
1366                 . "alert('$msg');\n"
1367                 . "-->\n"
1368                 . "</script>\n";
1369 }
1370
1371 if (!function_exists('_vd'))
1372 {
1373         function _vd($mixed, $title = '', $exit = false)
1374         {
1375         $str = (!empty($title) ? ($title .':') : '') .'<pre>';
1376         $str .= print_r($mixed, true); //var_dump($mixed);
1377         $str .= "</pre>\n";
1378                 display_notification('<table><tr><td>'.$str.'</td></tr></table>');
1379         if ($exit)
1380                 exit;
1381         }
1382 }
1383
1384 function _vl($mixed, $title = '', $exit = false)
1385 {
1386         error_log((!empty($title) ? ($title .':') : '') . var_export($mixed, true));
1387         if ($exit)
1388                 exit;
1389 }
1390
1391 function display_backtrace($cond=true, $msg='') {
1392         if ($cond) {
1393                 if ($msg) 
1394                         $str = "<center><span class='headingtext'>$msg</span></center>\n";
1395                 else
1396                         $str = '';
1397                 $str .= get_backtrace(true);
1398                 display_error($str);
1399         }
1400 }
1401
1402 //
1403 // FIXME: $payment_services array will be moved to bank_accounts in 2.4.x
1404 //
1405 if (!isset($payment_services))
1406 {
1407         $payment_services = array(
1408                 'PayPal' => "https://www.paypal.com/xclick?business=<company_email>&item_name=<comment>&amount=<amount>&currency_code=<currency>",
1409         );
1410 }
1411 /*
1412 *       Payment link generation. Options provided during invoice generation:
1413 *       company_email, comment, amount, currency
1414 */
1415 function payment_link($name, $options)
1416 {
1417         global $payment_services;
1418
1419         $link = @$payment_services[$name];
1420
1421         if (!$link) return null;
1422
1423         $patterns = array();
1424         foreach ($options as $id => $option)
1425                 $patterns['<'.$id.'>'] = urlencode($options[$id]);
1426
1427         return strtr($link, $patterns);
1428 }
1429
1430 ?>