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