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