Fixed supplier payment view link.
[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 == systypes::po())
22                 $viewer .= "view_po.php";
23         elseif ($type == 20)
24                 $viewer .= "view_supp_invoice.php";
25         elseif ($type == 21)
26                 $viewer .= "view_supp_credit.php";
27         elseif ($type == 22)
28                 $viewer .= "view_supp_payment.php";
29         elseif ($type == 25)
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 == 10)
77                 $viewer .= "view_invoice.php";
78         elseif ($type == 11)
79                 $viewer .= "view_credit.php";
80         elseif ($type == 12)
81                 $viewer .= "view_receipt.php";
82         elseif ($type == 13)
83                 $viewer .= "view_dispatch.php";
84         elseif ($type == 30)
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", 
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 == 4)
115                 $viewer = "bank_transfer_view.php";
116         elseif ($type == 1)
117                 $viewer = "gl_payment_view.php";
118         elseif ($type == 2)
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 == systypes::inventory_adjustment())
135                 $viewer .= "view_adjustment.php";
136         elseif ($type == systypes::location_transfer())
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 == 28)
156                 $viewer .= "wo_issue_view.php";
157         elseif ($type == 29)
158                 $viewer .= "wo_production_view.php";
159         elseif ($type == systypes::work_order())
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 == 40)
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 == 0 || $type == 35)
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_trans_view_str($type, $trans_no, $label="", $icon=false, 
207         $class='', $id='')
208 {
209         $view_str = get_customer_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
210         if ($view_str != null)
211                 return $view_str;
212
213         $view_str = get_supplier_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
214         if ($view_str != null)
215                 return $view_str;
216
217         $view_str = get_banking_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
218         if ($view_str != null)
219                 return $view_str;
220
221         $view_str = get_inventory_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
222         if ($view_str != null)
223                 return $view_str;
224
225         $view_str = get_manufacturing_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
226         if ($view_str != null)
227                 return $view_str;
228
229         $view_str = get_dimensions_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
230         if ($view_str != null)
231                 return $view_str;
232
233         $view_str = get_journal_trans_view_str($type, $trans_no, $label, $icon, $class, $id);
234         if ($view_str != null)
235                 return $view_str;
236
237         return null;
238 }
239
240 //--------------------------------------------------------------------------------------
241 // Displays currency exchange rate for given date.
242 // When there is no exrate for today, 
243 // gets it form ECB and stores in local database.
244 //
245 function exchange_rate_display($from_currency, $to_currency, $date_, $edit_rate=false)
246 {
247     global $Ajax;
248
249         if ($from_currency != $to_currency)
250         {
251                 $comp_currency = get_company_currency();
252                 if ($from_currency == $comp_currency)
253                         $currency = $to_currency;
254                 else
255                         $currency = $from_currency;
256                 $rate = 0;
257                 if ($date_ == Today()) {
258                         $rate = get_date_exchange_rate($currency, $date_);
259                         if (!$rate) {
260                                 $row = get_currency($currency);
261                                 if ($row['auto_update']) {
262                                         $rate = retrieve_exrate($currency, $date_);
263                                         if ($rate) 
264                                                 add_exchange_rate($currency, $date_, $rate, $rate);
265                                 }
266                         }
267                 }
268                 if (!$rate)
269                         $rate = get_exchange_rate_from_home_currency($currency, $date_);
270                 if ($from_currency != $comp_currency)
271                         $rate = 1 / ($rate / get_exchange_rate_from_home_currency($to_currency, $date_));
272
273                 $rate = number_format2($rate, user_exrate_dec());
274                 if ($edit_rate)
275                         text_row(_("Exchange Rate:"), '_ex_rate', $rate, 8, 8, null, "", " $from_currency = 1 $to_currency"); 
276                 else
277                 label_row(_("Exchange Rate:"),"<span id='_ex_rate'>$rate</span> $from_currency = 1 $to_currency" );
278                 $Ajax->addUpdate('_ex_rate','_ex_rate', $rate);
279         }
280 }
281
282 //--------------------------------------------------------------------------------------
283
284 function is_voided_display($type, $id, $label)
285 {
286         global $table_style;
287         $void_entry = get_voided_entry($type, $id);
288
289         if ($void_entry == null)
290                 return false;
291
292         start_table("width=50% $table_style");
293         echo "<tr><td align=center><font color=red>$label</font><br>";
294         echo "<font color=red>" . _("Date Voided:") . " " . sql2date($void_entry["date_"]) . "</font><br>";
295         if (strlen($void_entry["memo_"]) > 0)
296                 echo "<center><font color=red>" . _("Memo:") . " " . $void_entry["memo_"] . "</font></center><br>";
297         echo "</td></tr>";
298         end_table(1);
299
300         return true;
301 }
302
303 //--------------------------------------------------------------------------------------
304
305 function comments_display_row($type, $id)
306 {
307         $comments = get_comments($type, $id);
308         if ($comments and db_num_rows($comments))
309         {
310                 echo "<tr><td colspan=15>";
311         while ($comment = db_fetch($comments))
312         {
313                 echo $comment["memo_"] . "<br>";
314         }
315                 echo "</td></tr>";
316         }
317 }
318
319 //--------------------------------------------------------------------------------------
320
321 function get_comments_string($type, $type_no)
322 {
323         $str_return = "";
324         $result = get_comments($type, $type_no);
325         while ($comment = db_fetch($result))
326         {
327                 if (strlen($str_return))
328                         $str_return = $str_return . " \n";
329                 $str_return = $str_return . $comment["memo_"];
330         }
331         return $str_return;
332 }
333
334 //--------------------------------------------------------------------------------------
335
336 function view_stock_status($stock_id, $description=null, $echo=true)
337 {
338         global $path_to_root;
339         if ($description)
340                 //hyperlink_params_separate($path_to_root . "/inventory/inquiry/stock_status.php", (user_show_codes()?$stock_id . " - ":"") . $description, "stock_id=$stock_id");
341                 $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>";
342         else
343                 //hyperlink_params_separate($path_to_root . "/inventory/inquiry/stock_status.php", $stock_id, "stock_id=$stock_id");
344                 $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>";
345         if($echo)
346                 echo $preview_str;
347         return $preview_str;
348 }
349
350 function view_stock_status_cell($stock_id, $description=null)
351 {
352         echo "<td>";
353         view_stock_status($stock_id, $description);
354         echo "</td>";
355 }
356
357 //--------------------------------------------------------------------------------------
358
359 function display_debit_or_credit_cells($value)
360 {
361         if ($value > 0)
362         {
363                 amount_cell($value);
364                 label_cell("");
365         }
366         elseif ($value < 0)
367         {
368                 label_cell("");
369                 amount_cell(abs($value));
370         }
371         else
372         {
373                 //label_cell("");
374                 amount_cell(0);
375                 label_cell("");
376         }
377 }
378
379 //--------------------------------------------------------------------------------------
380
381 function display_customer_trans_tax_details($tax_items, $columns)
382 {
383     while ($tax_item = db_fetch($tax_items))
384     {
385         $tax = number_format2($tax_item['amount'],user_price_dec());
386         if ($tax_item['included_in_price'])
387                 label_row(_("Included") . " " . $tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%) " .
388                         _("Amount") . ": $tax", "", "colspan=$columns align=right", "align=right");
389         else
390                 label_row($tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%)",
391                         $tax, "colspan=$columns align=right", "align=right");
392     }
393 }
394
395 //--------------------------------------------------------------------------------------
396
397 function display_supp_trans_tax_details($tax_items, $columns)
398 {
399     while ($tax_item = db_fetch($tax_items))
400     {
401         $tax = number_format2(abs($tax_item['amount']),user_price_dec());
402         if ($tax_item['included_in_price'])
403                 label_row(_("Included") . " " . $tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%) " .
404                         _("Amount") . ": $tax", "colspan=$columns align=right", "align=right");
405         else
406                 label_row($tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%)",
407                         $tax, "colspan=$columns align=right", "align=right");
408     }
409 }
410
411 //--------------------------------------------------------------------------------------
412
413 function display_edit_tax_items($taxes, $columns, $tax_included, $leftspan=0)
414 {
415         $total = 0;
416
417     foreach ($taxes as $taxitem)
418     {
419         if ($tax_included)
420         {
421                 label_row(_("Included") . " " . $taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%) " .
422                         _("Amount:") . " " . number_format2($taxitem['Value'],user_price_dec()), "", "colspan=$columns align=right", "align=right",$leftspan);
423         }
424         else
425         {
426                 label_row($taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%)",
427                         number_format2($taxitem['Value'],user_price_dec()), "colspan=$columns align=right", "align=right",$leftspan);
428                 $total +=  round2($taxitem['Value'], user_price_dec());
429         }
430     }
431
432     return $total;
433 }
434
435 //--------------------------------------------------------------------------------------
436
437 function display_footer_exit()
438 {
439         br(2);
440         end_page();
441         exit;
442 }
443
444 //--------------------------------------------------------------------------------------
445
446 function display_allocations($alloc_result, $total)
447 {
448         global $table_style;
449
450         if (!$alloc_result || db_num_rows($alloc_result) == 0)
451                 return;
452
453     display_heading2(_("Allocations"));
454
455     start_table("$table_style width=80%");
456
457     $th = array( _("Type"), _("Number"), _("Date"), _("Total Amount"),
458         _("Left to Allocate"), _("This Allocation"));
459         table_header($th);
460     $k = $total_allocated = 0;
461
462     while ($alloc_row = db_fetch($alloc_result))
463     {
464
465         alt_table_row_color($k);
466
467         label_cell(systypes::name($alloc_row['type']));
468         label_cell(get_trans_view_str($alloc_row['type'],$alloc_row['trans_no']));
469         label_cell(sql2date($alloc_row['tran_date']));
470         $alloc_row['Total'] = round2($alloc_row['Total'], user_price_dec());
471         $alloc_row['amt'] = round2($alloc_row['amt'], user_price_dec());
472         amount_cell($alloc_row['Total']);
473         //amount_cell($alloc_row['Total'] - $alloc_row['PrevAllocs'] - $alloc_row['amt']);
474         amount_cell($alloc_row['Total'] - $alloc_row['amt']);
475         amount_cell($alloc_row['amt']);
476         end_row();
477
478         $total_allocated += $alloc_row['amt'];
479     }
480     start_row();
481         label_cell(_("Total Allocated:"), "align=right colspan=5");
482         amount_cell($total_allocated);
483         end_row();
484         start_row();
485     label_cell(_("Left to Allocate:"), "align=right colspan=5");
486     $total = round2($total, user_price_dec());
487     amount_cell($total - $total_allocated);
488     end_row();
489
490     end_table(1);
491 }
492
493 //--------------------------------------------------------------------------------------
494
495 function display_allocations_from($person_type, $person_id, $type, $type_no, $total)
496 {
497         switch ($person_type)
498         {
499                 case payment_person_types::customer() :
500                         $alloc_result = get_allocatable_to_cust_transactions($person_id, $type_no, $type);
501                         display_allocations($alloc_result, $total);
502                         return;
503                 case payment_person_types::supplier() :
504                         $alloc_result = get_allocatable_to_supp_transactions($person_id, $type_no, $type);
505                         display_allocations($alloc_result, $total);
506                         return;
507         }
508 }
509
510 //--------------------------------------------------------------------------------------
511 //
512 //      Expands selected quick entry $id into GL posings and adds to cart.
513 //              returns calculated amount posted to bank GL account.
514 //
515 function display_quick_entries(&$cart, $id, $base, $type, $descr='')
516 {
517         $bank_amount = 0;
518         
519         if (!isset($id) || $id == null || $id == "")
520         {
521                 display_error( _("No Quick Entries are defined."));
522                 set_focus('totamount');
523         }
524         else
525         {
526                 if ($type == QE_DEPOSIT)
527                         $base = -$base;
528                 if ($type != QE_SUPPINV)        // only one quick entry on journal/bank transaction
529                         $cart->clear_items();
530                 $qe = get_quick_entry($id);
531                 if ($descr != '') $qe['description'] .= ': '.$descr;
532                 $qe_lines = get_quick_entry_lines($id);
533                 while ($qe_line = db_fetch($qe_lines))
534                 {
535                         switch (strtolower($qe_line['action'])) {
536                                 case "=": // post current base amount to GL account
537                                         $part = $base;
538                                         break;
539                                 case "a": // post amount to GL account and reduce base
540                                         $part = $qe_line['amount'];
541                                         break;
542                                 case "a+": // post amount to GL account and increase base
543                                         $part = $qe_line['amount']; $base += $part;
544                                         break;
545                                 case "a-": // post amount to GL account and reduce base
546                                         $part = $qe_line['amount']; $base -= $part;
547                                         break;
548                                 case "%":       // store acc*amount% to GL account
549                                         $part = round2($base * $qe_line['amount'] / 100, user_price_dec());
550                                         break;
551                                 case "%+":      // ditto & increase base amount
552                                         $part = round2($base * $qe_line['amount'] / 100, user_price_dec());
553                                         $base += $part;
554                                         break;
555                                 case "%-":      // ditto & reduce base amount
556                                         $part = round2($base * $qe_line['amount'] / 100, user_price_dec());
557                                         $base -= $part;
558                                         break;
559                                 case "t": // post taxes calculated on base amount
560                                 case "t+": // ditto & increase base amount
561                                 case "t-": // ditto & reduce base amount
562                                         $taxes = get_taxes_for_item_tax($qe_line['dest_id']);
563                                         $tax_total = 0;
564                                         foreach ($taxes as $index => $item_tax) {
565                                                 if(substr($qe_line['action'],0,1) != 'T')
566                                                         $tax = round2($base * $item_tax['rate'] 
567                                                                 / ($item_tax['rate'] + 100),  user_price_dec());
568                                                 else
569                                                         $tax = round2($base * $item_tax['rate'] / 100,  user_price_dec());
570
571                                                 $gl_code = ($type == QE_DEPOSIT || ($type == QE_JOURNAL && $base < 0)) 
572                                                         ? $item_tax['sales_gl_code'] : $item_tax['purchasing_gl_code'];
573                                                 $tax_total += $tax;
574                                                 if ($type != QE_SUPPINV)
575                                                         $cart->add_gl_item($gl_code, 
576                                                                 $qe_line['dimension_id'], $qe_line['dimension2_id'], 
577                                                                 $tax, $qe['description']);
578                                                 else 
579                                                 {
580                                                         $acc_name = get_gl_account_name($gl_code);
581                                                         $cart->add_gl_codes_to_trans($gl_code, 
582                                                                 $acc_name, $qe_line['dimension_id'], 
583                                                                 $qe_line['dimension2_id'], $tax, $qe['description']);
584                                                 }
585                                         }
586                                         if (strpos($qe_line['action'], '+'))
587                                                 $base += $tax_total;
588                                         elseif (strpos($qe_line['action'], '-'))
589                                                 $base -= $tax_total;
590                                         continue 2;
591                         }
592                         if ($type != QE_SUPPINV)
593                                 $cart->add_gl_item($qe_line['dest_id'], $qe_line['dimension_id'],
594                                         $qe_line['dimension2_id'], $part, $qe['description']);
595                         else 
596                         {
597                                 $acc_name = get_gl_account_name($qe_line['dest_id']);
598                                 $cart->add_gl_codes_to_trans($qe_line['dest_id'], 
599                                         $acc_name, $qe_line['dimension_id'], 
600                                         $qe_line['dimension2_id'], $part, $qe['description']);
601                         }
602                 }
603         }       
604         return $bank_amount;
605 }
606
607
608 function get_js_open_window($width, $height)
609 {
610         $js ="function openWindow(url, title)\n"
611                 . "{\n"
612                 . " var left = (screen.width - $width) / 2;\n"
613                 . " var top = (screen.height - $height) / 2;\n"
614                 . " return window.open(url, title, 'width=$width,height=$height,left='+left+',top='+top+',screenX='+left+',screenY='+top+',status=no,scrollbars=yes');\n"
615                 . "}\n";
616         return $js;
617 }
618
619 /*
620   Setting focus on element $name in $form.
621   If $form<0 $name is element id.
622 */
623 function set_focus($name, $form_no=0) {
624   global $Ajax;
625         $Ajax->addFocus(true, $name);
626     $_POST['_focus'] = $name;
627 }
628 //
629 //      Set default focus on first field $name if not set yet
630 //      Returns unique name if $name=null
631 //      
632 function default_focus($name=null, $form_no=0) {
633         static $next; 
634         if ($name==null) 
635                 $name = uniqid('_el',true);
636     if (!isset($_POST['_focus'])) {
637           set_focus($name);
638     }
639         return $name;
640 }
641 /*
642         Reset focus to next control element (e.g. link).
643 */
644 function reset_focus()
645 {
646         unset($_POST['_focus']);
647 }
648
649 function get_js_png_fix()
650 {
651         $js = "function fixPNG(myImage)\n"
652                 . "{\n"
653                 . " var arVersion = navigator.appVersion.split(\"MSIE\")\n"
654                 . " var version = parseFloat(arVersion[1])\n"
655         . " if ((version >= 5.5) && (version < 7) && (document.body.filters))\n"
656         . " {\n"
657         . "  var imgID = (myImage.id) ? \"id='\" + myImage.id + \"' \" : \"\"\n"
658                 . "  var imgClass = (myImage.className) ? \"class='\" + myImage.className + \"' \" : \"\"\n"
659                 . "  var imgTitle = (myImage.title) ?\n"
660                 . "    \"title='\" + myImage.title  + \"' \" : \"title='\" + myImage.alt + \"' \"\n"
661                 . "  var imgStyle = \"display:inline-block;\" + myImage.style.cssText\n"
662                 . "  var strNewHTML = \"<span \" + imgID + imgClass + imgTitle\n"
663         . "    + \" style=\\\"\" + \"width:\" + myImage.width\n"
664         . "    + \"px; height:\" + myImage.height\n"
665         . "    + \"px;\" + imgStyle + \";\"\n"
666         . "    + \"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader\"\n"
667         . "    + \"(src=\'\" + myImage.src + \"\', sizingMethod='scale');\\\"></span>\"\n"
668                 . "  myImage.outerHTML = strNewHTML\n"
669         . " }\n"
670                 . "}\n";
671         return $js;
672 }
673
674 function get_js_date_picker()
675 {
676     global $comp_path, $go_debug;
677     $fpath = $comp_path.'/'.user_company().'/js_cache/'.'date_picker.js';
678
679     if (!file_exists($fpath) || $go_debug) {
680
681         global $dateseps, $date_system;
682
683         $how = user_date_format();                              // 0 = us/ca, 1 = eu, au, nz, 2 = jp, sw
684         $sep = $dateseps[user_date_sep()];              // date separator
685         $wstart = (($date_system == 1 || $date_system == 2) ? 6 : ($how == 0 ? 0 : 1));                 // weekstart (sun = 0, mon = 1)
686         $months = array(_("January"),_("February"),_("March"),_("April"),_("May"),_("June"),_("July"),_("August"),_("September"),_("October"),_("November"),_("December"));
687         $wdays = array(_("Su"),_("Mo"),_("Tu"),_("We"),_("Th"),_("Fr"),_("Sa"));
688         $wno = _("W"); // week no
689         $back = _("Back");
690         if ($date_system == 1)
691                 list($cyear, $cmon, $cday) = gregorian_to_jalali(date("Y"), date("n"), date("j"));
692         else if ($date_system == 2)
693                 list($cyear, $cmon, $cday) = gregorian_to_islamic(date("Y"), date("n"), date("j"));
694
695
696         $js = "
697 function positionInfo(object) {
698   var p_elm = object;
699   this.getElementLeft = getElementLeft;
700   function getElementLeft() {
701     var x = 0;
702     var elm;
703     if(typeof(p_elm) == 'object'){
704       elm = p_elm;
705     } else {
706       elm = document.getElementById(p_elm);
707     }
708     while (elm != null) {
709       x+= elm.offsetLeft;
710       elm = elm.offsetParent;
711     }
712     return parseInt(x);
713   }
714   this.getElementWidth = getElementWidth;
715   function getElementWidth(){
716     var elm;
717     if(typeof(p_elm) == 'object'){
718       elm = p_elm;
719     } else {
720       elm = document.getElementById(p_elm);
721     }
722     return parseInt(elm.offsetWidth);
723   }
724   this.getElementRight = getElementRight;
725   function getElementRight(){
726     return getElementLeft(p_elm) + getElementWidth(p_elm);
727   }
728   this.getElementTop = getElementTop;
729   function getElementTop() {
730     var y = 0;
731     var elm;
732     if(typeof(p_elm) == 'object'){
733       elm = p_elm;
734     } else {
735       elm = document.getElementById(p_elm);
736     }
737     while (elm != null) {
738       y+= elm.offsetTop;
739       elm = elm.offsetParent;
740     }
741     return parseInt(y);
742   }
743   this.getElementHeight = getElementHeight;
744   function getElementHeight(){
745     var elm;
746     if(typeof(p_elm) == 'object'){
747       elm = p_elm;
748     } else {
749       elm = document.getElementById(p_elm);
750     }
751     return parseInt(elm.offsetHeight);
752   }
753   this.getElementBottom = getElementBottom;
754   function getElementBottom(){
755     return getElementTop(p_elm) + getElementHeight(p_elm);
756   }
757 }
758 function CC() {
759   var calendarId = 'CC';
760   var currentYear = 0;
761   var currentMonth = 0;
762   var currentDay = 0;
763   var selectedYear = 0;
764   var selectedMonth = 0;
765   var selectedDay = 0;
766   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]'];
767   var wdays = ['$wdays[0]', '$wdays[1]', '$wdays[2]', '$wdays[3]', '$wdays[4]', '$wdays[5]', '$wdays[6]'];
768   var dateField = null;
769   function getProperty(p_property){
770     var p_elm = calendarId;
771     var elm = null;
772     if(typeof(p_elm) == 'object'){
773       elm = p_elm;
774     } else {
775       elm = document.getElementById(p_elm);
776     }
777     if (elm != null){
778       if(elm.style){
779         elm = elm.style;
780         if(elm[p_property]){
781           return elm[p_property];
782         } else {
783           return null;
784         }
785       } else {
786         return null;
787       }
788     }
789   }
790   function setElementProperty(p_property, p_value, p_elmId){
791     var p_elm = p_elmId;
792     var elm = null;
793     if(typeof(p_elm) == 'object'){
794       elm = p_elm;
795     } else {
796       elm = document.getElementById(p_elm);
797     }
798     if((elm != null) && (elm.style != null)){
799       elm = elm.style;
800       elm[ p_property ] = p_value;
801     }
802   }
803   function setProperty(p_property, p_value) {
804     setElementProperty(p_property, p_value, calendarId);
805   }
806   function getDaysInMonth(year, month) {
807 ";
808         if ($date_system == 1)
809                 $js .= "
810     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];
811 ";
812         else if ($date_system == 2)
813                 $js .= "
814     return [30,29,30,29,30,29,30,29,30,29,30,(((((11 * year) + 14) % 30) < 11) ? 30 : 29)][month-1];
815 ";
816         else
817                 $js .= "
818     return [31,((!(year % 4 ) && ( (year % 100 ) || !( year % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][month-1];
819 ";
820         $js .= "
821   }
822   function getDayOfWeek(year, month, day) {
823 ";
824         if ($date_system == 1 || $date_system == 2)
825         {
826                 $js .= "
827         function mod(a, b)
828         {
829             return a - (b * Math.floor(a / b));
830         }
831         function jwday(j)
832         {
833             return mod(Math.floor((j + 1.5)), 7);
834         }
835 ";
836         if ($date_system == 1)
837                 $js .= "
838     var epbase, epyear, t;
839     epbase = year - ((year >= 0) ? 474 : 473);
840     epyear = 474 + mod(epbase, 2820);
841     t = day + ((month <= 7) ? ((month - 1) * 31) : (((month - 1) * 30) + 6)) +
842       Math.floor(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 +
843       Math.floor(epbase / 2820) * 1029983 + (1948320.5 - 1);
844 ";
845         else if ($date_system == 2)
846                 $js .= "
847         var t;
848         t = Math.floor((11 * year + 3) / 30) + 354 * year + 30 * month -
849           Math.floor((month - 1) / 2) + day + 1948440 - 385;
850 ";
851         $js .= "
852     return jwday(t);
853 ";
854         }
855         else
856                 $js .= "
857     var date = new Date(year,month-1,day)
858     return date.getDay();
859 ";
860         $js .= "
861   }
862   this.clearDate = clearDate;
863   function clearDate() {
864     dateField.value = '';
865     hide();
866   }
867   this.getWeek = getWeek;
868   function getWeek(year, month, day) {
869 ";
870         if ($how == 0)
871                 $js .= "  day++;";
872         $js .= "
873     var date = new Date(year,month-1,day);
874     var D = date.getDay();
875     if(D == 0) D = 7;
876     date.setDate(date.getDate() + (4 - D));
877     var YN = date.getFullYear();
878     var ZBDoCY = Math.floor((date.getTime() - new Date(YN, 0, 1, -6)) / 86400000);
879     var WN = 1 + Math.floor(ZBDoCY / 7);
880     return WN;
881   }
882   this.setDate = setDate;
883   function setDate(year, month, day) {
884     if (dateField) {
885       if (month < 10) {month = '0' + month;}
886       if (day < 10) {day = '0' + day;}
887 ";
888         if ($how == 0)
889                 $js .= "
890       var dateString = month+'$sep'+day+'$sep'+year;
891 ";
892         else if ($how == 1)
893                 $js .= "
894       var dateString = day+'$sep'+month+'$sep'+year;
895 ";
896         else
897                 $js .= "
898       var dateString = year+'$sep'+month+'$sep'+day;
899 ";
900         $js .= "
901       dateField.value = dateString;
902           setFocus(dateField.name);
903         if(dateField.getAttribute('aspect')=='cdate')
904       setElementProperty('color', (dateField.value==user.date ? 'black':'red'), dateField);
905           if (dateField.className=='searchbox')
906                 dateField.onblur();
907       hide();
908     }
909     return;
910   }
911   this.changeMonth = changeMonth;
912   function changeMonth(change) {
913     currentMonth += change;
914     currentDay = 0;
915     if(currentMonth > 12) {
916       currentMonth = 1;
917       currentYear++;
918     } else if(currentMonth < 1) {
919       currentMonth = 12;
920       currentYear--;
921     }
922     calendar = document.getElementById(calendarId);
923     calendar.innerHTML = calendarDrawTable();
924   }
925   this.changeYear = changeYear;
926   function changeYear(change) {
927     currentYear += change;
928     currentDay = 0;
929     calendar = document.getElementById(calendarId);
930     calendar.innerHTML = calendarDrawTable();
931   }
932   function getCurrentYear() {
933     var year = new Date().getYear();
934     if(year < 1900) year += 1900;
935     return year;
936   }
937   function getCurrentMonth() {
938     return new Date().getMonth() + 1;
939   }
940   function getCurrentDay() {
941     return new Date().getDate();
942   }
943   function calendarDrawTable() {
944     var dayOfMonth = 1;
945     var wstart = $wstart;
946     var wno = '&nbsp;$wno&nbsp;';
947     var validDay = 0;
948     var startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
949     var daysInMonth = getDaysInMonth(currentYear, currentMonth);
950     var css_class = null; //CSS class for each day
951     var table = \"<table cellspacing='0' cellpadding='0' border='0'>\";
952     table += \"<tr class='header'>\";
953     table += \"  <td colspan='2' class='previous'><a href='javascript:changeCCMonth(-1);'>&lt;</a><br><a href='javascript:changeCCYear(-1);'>&laquo;</a></td>\";
954     table += \"  <td colspan='4' class='title'>\" + months[currentMonth-1] + \"<br>\" + currentYear + \"</td>\";
955     table += \"  <td colspan='2' class='next'><a href='javascript:changeCCMonth(1);'>&gt;</a><br><a href='javascript:changeCCYear(1);'>&raquo;</a></td>\";
956     table += \"</tr>\";
957     table += \"<tr>\";
958     table += \"<th class='weekno'>\"+wno+\"</th>\";
959     for (var n=0; n<7; n++)
960         table += \"<th>\" + wdays[(wstart+n)%7]+\"</th>\";
961     table += \"</tr>\";
962     for(var week=0; week < 6; week++) {
963       table += \"<tr>\";
964       for(var n=0; n < 7; n++) {
965         dayOfWeek = (wstart+n)%7;
966         if(week == 0 && startDayOfWeek == dayOfWeek) {
967           validDay = 1;
968         } else if (validDay == 1 && dayOfMonth > daysInMonth) {
969           validDay = 0;
970         }
971         if (n==0)
972         {
973                 if (dayOfMonth > daysInMonth)
974                         table += \"<td class='empty'>&nbsp;</td>\";
975                 else
976                         table += \"<td class='weekno'>\"+getWeek(currentYear, currentMonth, dayOfMonth)+\"</td>\";
977         }
978         if(validDay) {
979           if (dayOfMonth == selectedDay && currentYear == selectedYear && currentMonth == selectedMonth) {
980             css_class = 'current';
981 ";
982         if ($date_system == 1 || $date_system == 2)
983                 $js .= "
984           } else if (dayOfWeek == 5) {
985 ";
986         else
987                 $js .= "
988           } else if (dayOfWeek == 0 || dayOfWeek == 6) {
989 ";
990                 $js .= "
991             css_class = 'weekend';
992           } else {
993             css_class = 'weekday';
994           }
995           table += \"<td><a class='\"+css_class+\"' href=\\\"javascript:setCCDate(\"+currentYear+\",\"+currentMonth+\",\"+dayOfMonth+\")\\\">\"+dayOfMonth+\"</a></td>\";
996           dayOfMonth++;
997         } else {
998           table += \"<td class='empty'>&nbsp;</td>\";
999         }
1000       }
1001       table += \"</tr>\";
1002     }
1003     table += \"<tr class='header'><th colspan='8' style='padding: 3px;text-align:center;'><a href='javascript:hideCC();'>$back</a></td></tr>\";
1004     table += \"</table>\";
1005     return table;
1006   }
1007   this.show = show;
1008   function show(field) {
1009     can_hide = 0;
1010     if (dateField == field) {
1011       return;
1012     } else {
1013       dateField = field;
1014     }
1015     if(dateField) {
1016       try {
1017         var dateString = new String(dateField.value);
1018         var dateParts = dateString.split('$sep');
1019 ";
1020         if ($how == 0)
1021                 $js .= "
1022         selectedMonth = parseInt(dateParts[0],10);
1023         selectedDay = parseInt(dateParts[1],10);
1024         selectedYear = parseInt(dateParts[2],10);
1025 ";
1026         else if ($how == 1)
1027                 $js .= "
1028         selectedDay = parseInt(dateParts[0],10);
1029         selectedMonth = parseInt(dateParts[1],10);
1030         selectedYear = parseInt(dateParts[2],10);
1031 ";
1032         else
1033                 $js .= "
1034         selectedYear = parseInt(dateParts[0],10);
1035         selectedMonth = parseInt(dateParts[1],10);
1036         selectedDay = parseInt(dateParts[2],10);
1037 ";
1038         $js .= "
1039       } catch(e) {}
1040     }
1041     if (!(selectedYear && selectedMonth && selectedDay)) {
1042 ";
1043         if ($date_system == 1 || $date_system == 2)
1044         {
1045                 $js .= "
1046       selectedMonth = $cmon;
1047       selectedDay = $cday;
1048       selectedYear = $cyear;
1049 ";
1050         }
1051         else
1052         {
1053                 $js .= "
1054       selectedMonth = getCurrentMonth();
1055       selectedDay = getCurrentDay();
1056       selectedYear = getCurrentYear();
1057 ";
1058         }
1059         $js .= "
1060     }
1061     currentMonth = selectedMonth;
1062     currentDay = selectedDay;
1063     currentYear = selectedYear;
1064     if(document.getElementById){
1065       calendar = document.getElementById(calendarId);
1066       calendar.innerHTML = calendarDrawTable(currentYear, currentMonth);
1067       var fieldPos = new positionInfo(dateField);
1068       var calendarPos = new positionInfo(calendarId);
1069       var x = fieldPos.getElementLeft();
1070       var y = fieldPos.getElementBottom();
1071       setProperty('left', x + 'px');
1072       setProperty('top', y + 'px');
1073       setProperty('display', 'block');
1074       if (document.all) {
1075         setElementProperty('left', x + 'px', 'CCIframe');
1076         setElementProperty('top', y + 'px', 'CCIframe');
1077         setElementProperty('width', calendarPos.getElementWidth() + 'px', 'CCIframe');
1078         setElementProperty('height', calendarPos.getElementHeight() + 'px', 'CCIframe');
1079         setElementProperty('display', 'block', 'CCIframe');
1080       }
1081     }
1082   }
1083   this.hide = hide;
1084   function hide() {
1085     if(dateField) {
1086       setProperty('display', 'none');
1087       setElementProperty('display', 'none', 'CCIframe');
1088       dateField = null;
1089     }
1090   }
1091   this.visible = visible;
1092   function visible() {
1093     return dateField
1094   }
1095   this.can_hide = can_hide;
1096   var can_hide = 0;
1097 }
1098 var cC = new CC();
1099 function date_picker(textField) {
1100   cC.show(textField);
1101 }
1102 function hideCC() {
1103   if (cC.visible()) {
1104     cC.hide();
1105   }
1106 }
1107 function setCCDate(year, month, day) {
1108   cC.setDate(year, month, day);
1109 }
1110 function changeCCYear(change) {
1111   cC.changeYear(change);
1112 }
1113 function changeCCMonth(change) {
1114   cC.changeMonth(change);
1115 }
1116 document.write(\"<iframe id='CCIframe' src='javascript:false;' frameBorder='0' scrolling='no'></iframe>\");
1117 document.write(\"<div id='CC'></div>\");";
1118
1119      cache_js_file($fpath, $js);
1120     }
1121     add_js_ufile($fpath);
1122
1123  return '';
1124 }
1125
1126 function alert($msg)
1127 {
1128         echo "\n<script type=\"text/javascript\">\n"
1129                 . "<!--\n"
1130                 . "alert('$msg');\n"
1131                 . "-->\n"
1132                 . "</script>\n";
1133 }
1134
1135 if (!function_exists('_vd'))
1136 {
1137         function _vd($mixed, $title = '', $exit = false)
1138         {
1139         // Only the site admin is able to proceed here.
1140         $str = (!empty($title) ? ($title .':') : '') .'<pre>';
1141         $str .= print_r($mixed, true); //var_dump($mixed);
1142         $str .= "</pre>\n";
1143                 display_error('<table><tr><td>'.$str.'</td></tr></table>');
1144         if ($exit)
1145                 exit;
1146         }
1147 }
1148
1149 function display_backtrace($cond=true, $msg='') {
1150
1151         if ($cond) {
1152                 if ($msg) 
1153                         $str = "<center><span class='headingtext'>$msg</span></center>\n";
1154                 else
1155                         $str = '';
1156                 $str .= '<table border=0>';
1157         $trace = debug_backtrace();
1158                 foreach($trace as $trn => $tr) {
1159                         if (!$trn) continue;
1160                         $str .= '<tr><td>';
1161                         $str .= $tr['file'].':'.$tr['line'].': ';
1162                         $str .= '</td><td>';
1163                         if (isset($tr['type'])) {
1164                                 if($tr['type'] == '::') {
1165                                         $str .= $tr['class'].'::';
1166                                 } else if($tr['type'] == '->') {
1167                                         $str .= '('.$tr['class'].' Object)'.'->';
1168                                 }
1169                         }
1170                         foreach($tr['args'] as $n=>$a) {
1171                                 if (is_object($tr['args'][$n]))
1172                                         $tr['args'][$n] = "(".get_class($tr['args'][$n])." Object)";
1173                                 if (is_array($tr['args'][$n]))
1174                                         $tr['args'][$n] = "(Array[".count($tr['args'][$n])."])";
1175                                 else
1176                                         $tr['args'][$n] = "'".$tr['args'][$n]."'";
1177                         }
1178                         $str .= $tr['function'].'('. implode(',',$tr['args']).')</td>';
1179
1180                         $str .= '</tr>';
1181                 }
1182                 
1183                 $str .= '</table>';
1184         display_error($str);
1185         }
1186 }
1187
1188 ?>