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