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