Code clean-up
[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 {
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 style='vertical-align:top;' 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 }
415
416 //--------------------------------------------------------------------------------------
417
418 function display_customer_trans_tax_details($tax_items, $columns)
419 {
420     while ($tax_item = db_fetch($tax_items))
421     {
422         $tax = number_format2($tax_item['amount'],user_price_dec());
423         if ($tax_item['included_in_price'])
424                 label_row(_("Included") . " " . $tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%) " .
425                         _("Amount") . ": $tax", "", "colspan=$columns align=right", "align=right");
426         else
427                 label_row($tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%)",
428                         $tax, "colspan=$columns align=right", "align=right");
429     }
430 }
431
432 //--------------------------------------------------------------------------------------
433
434 function display_supp_trans_tax_details($tax_items, $columns)
435 {
436     while ($tax_item = db_fetch($tax_items))
437     {
438         $tax = number_format2(abs($tax_item['amount']),user_price_dec());
439         if ($tax_item['included_in_price'])
440                 label_row(_("Included") . " " . $tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%) " .
441                         _("Amount") . ": $tax", "colspan=$columns align=right", "align=right");
442         else
443                 label_row($tax_item['tax_type_name'] . " (" . $tax_item['rate'] . "%)",
444                         $tax, "colspan=$columns align=right", "align=right");
445     }
446 }
447
448 //--------------------------------------------------------------------------------------
449
450 function display_edit_tax_items($taxes, $columns, $tax_included, $leftspan=0)
451 {
452         $total = 0;
453
454     foreach ($taxes as $taxitem)
455     {
456         if ($tax_included)
457         {
458                 label_row(_("Included") . " " . $taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%) " .
459                         _("Amount:") . " " . number_format2($taxitem['Value'],user_price_dec()), "", "colspan=$columns align=right", "align=right",$leftspan);
460         }
461         else
462         {
463                 label_row($taxitem['tax_type_name'] . " (" . $taxitem['rate'] . "%)",
464                         number_format2($taxitem['Value'],user_price_dec()), "colspan=$columns align=right", "align=right",$leftspan);
465                 $total +=  round2($taxitem['Value'], user_price_dec());
466         }
467     }
468
469     return $total;
470 }
471
472 //--------------------------------------------------------------------------------------
473
474 function display_footer_exit()
475 {
476         br(2);
477         end_page();
478         exit;
479 }
480
481 //--------------------------------------------------------------------------------------
482
483 function display_allocations($alloc_result, $total)
484 {
485         global $table_style;
486
487         if (!$alloc_result || db_num_rows($alloc_result) == 0)
488                 return;
489
490     display_heading2(_("Allocations"));
491
492     start_table("$table_style width=80%");
493
494     $th = array( _("Type"), _("Number"), _("Date"), _("Total Amount"),
495         _("Left to Allocate"), _("This Allocation"));
496         table_header($th);
497     $k = $total_allocated = 0;
498
499     while ($alloc_row = db_fetch($alloc_result))
500     {
501
502         alt_table_row_color($k);
503
504         label_cell(systypes::name($alloc_row['type']));
505         label_cell(get_trans_view_str($alloc_row['type'],$alloc_row['trans_no']));
506         label_cell(sql2date($alloc_row['tran_date']));
507         $alloc_row['Total'] = round2($alloc_row['Total'], user_price_dec());
508         $alloc_row['amt'] = round2($alloc_row['amt'], user_price_dec());
509         amount_cell($alloc_row['Total']);
510         //amount_cell($alloc_row['Total'] - $alloc_row['PrevAllocs'] - $alloc_row['amt']);
511         amount_cell($alloc_row['Total'] - $alloc_row['amt']);
512         amount_cell($alloc_row['amt']);
513         end_row();
514
515         $total_allocated += $alloc_row['amt'];
516     }
517     start_row();
518         label_cell(_("Total Allocated:"), "align=right colspan=5");
519         amount_cell($total_allocated);
520         end_row();
521         start_row();
522     label_cell(_("Left to Allocate:"), "align=right colspan=5");
523     $total = round2($total, user_price_dec());
524     amount_cell($total - $total_allocated);
525     end_row();
526
527     end_table(1);
528 }
529
530 //--------------------------------------------------------------------------------------
531
532 function display_allocations_from($person_type, $person_id, $type, $type_no, $total)
533 {
534         switch ($person_type)
535         {
536                 case payment_person_types::customer() :
537                         $alloc_result = get_allocatable_to_cust_transactions($person_id, $type_no, $type);
538                         display_allocations($alloc_result, $total);
539                         return;
540                 case payment_person_types::supplier() :
541                         $alloc_result = get_allocatable_to_supp_transactions($person_id, $type_no, $type);
542                         display_allocations($alloc_result, $total);
543                         return;
544         }
545 }
546
547 //--------------------------------------------------------------------------------------
548 //
549 //      Expands selected quick entry $id into GL posings and adds to cart.
550 //              returns calculated amount posted to bank GL account.
551 //
552 function display_quick_entries(&$cart, $id, $base, $type, $descr='')
553 {
554         $bank_amount = 0;
555         
556         if (!isset($id) || $id == null || $id == "")
557         {
558                 display_error( _("No Quick Entries are defined."));
559                 set_focus('totamount');
560         }
561         else
562         {
563                 if ($type == QE_DEPOSIT)
564                         $base = -$base;
565                 if ($type != QE_SUPPINV)        // only one quick entry on journal/bank transaction
566                         $cart->clear_items();
567                 $qe = get_quick_entry($id);
568                 if ($descr != '') $qe['description'] .= ': '.$descr;
569                 $qe_lines = get_quick_entry_lines($id);
570                 while ($qe_line = db_fetch($qe_lines))
571                 {
572                         switch (strtolower($qe_line['action'])) {
573                                 case "=": // post current base amount to GL account
574                                         $part = $base;
575                                         break;
576                                 case "a": // post amount to GL account and reduce base
577                                         $part = $qe_line['amount'];
578                                         break;
579                                 case "a+": // post amount to GL account and increase base
580                                         $part = $qe_line['amount']; $base += $part;
581                                         break;
582                                 case "a-": // post amount to GL account and reduce 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 = round2($base * $item_tax['rate'] 
604                                                                 / ($item_tax['rate'] + 100),  user_price_dec());
605                                                 else
606                                                         $tax = round2($base * $item_tax['rate'] / 100,  user_price_dec());
607
608                                                 $gl_code = ($type == QE_DEPOSIT || ($type == QE_JOURNAL && $base < 0)) 
609                                                         ? $item_tax['sales_gl_code'] : $item_tax['purchasing_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                                                 {
617                                                         $acc_name = get_gl_account_name($gl_code);
618                                                         $cart->add_gl_codes_to_trans($gl_code, 
619                                                                 $acc_name, $qe_line['dimension_id'], 
620                                                                 $qe_line['dimension2_id'], $tax, $qe['description']);
621                                                 }
622                                         }
623                                         if (strpos($qe_line['action'], '+'))
624                                                 $base += $tax_total;
625                                         elseif (strpos($qe_line['action'], '-'))
626                                                 $base -= $tax_total;
627                                         continue 2;
628                         }
629                         if ($type != QE_SUPPINV)
630                                 $cart->add_gl_item($qe_line['dest_id'], $qe_line['dimension_id'],
631                                         $qe_line['dimension2_id'], $part, $qe['description']);
632                         else 
633                         {
634                                 $acc_name = get_gl_account_name($qe_line['dest_id']);
635                                 $cart->add_gl_codes_to_trans($qe_line['dest_id'], 
636                                         $acc_name, $qe_line['dimension_id'], 
637                                         $qe_line['dimension2_id'], $part, $qe['description']);
638                         }
639                 }
640         }       
641         return $bank_amount;
642 }
643
644
645 function get_js_open_window($width, $height)
646 {
647         $js ="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         return $js;
654 }
655
656 /*
657   Setting focus on element $name in $form.
658   If $form<0 $name is element id.
659 */
660 function set_focus($name, $form_no=0) {
661   global $Ajax;
662         $Ajax->addFocus(true, $name);
663     $_POST['_focus'] = $name;
664 }
665 //
666 //      Set default focus on first field $name if not set yet
667 //
668 function default_focus($name, $form_no=0) {
669     if (!isset($_POST['_focus'])) {
670           set_focus($name);
671     }
672 }
673
674 function get_js_png_fix()
675 {
676         $js = "function fixPNG(myImage)\n"
677                 . "{\n"
678                 . " var arVersion = navigator.appVersion.split(\"MSIE\")\n"
679                 . " var version = parseFloat(arVersion[1])\n"
680         . " if ((version >= 5.5) && (version < 7) && (document.body.filters))\n"
681         . " {\n"
682         . "  var imgID = (myImage.id) ? \"id='\" + myImage.id + \"' \" : \"\"\n"
683                 . "  var imgClass = (myImage.className) ? \"class='\" + myImage.className + \"' \" : \"\"\n"
684                 . "  var imgTitle = (myImage.title) ?\n"
685                 . "    \"title='\" + myImage.title  + \"' \" : \"title='\" + myImage.alt + \"' \"\n"
686                 . "  var imgStyle = \"display:inline-block;\" + myImage.style.cssText\n"
687                 . "  var strNewHTML = \"<span \" + imgID + imgClass + imgTitle\n"
688         . "    + \" style=\\\"\" + \"width:\" + myImage.width\n"
689         . "    + \"px; height:\" + myImage.height\n"
690         . "    + \"px;\" + imgStyle + \";\"\n"
691         . "    + \"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader\"\n"
692         . "    + \"(src=\'\" + myImage.src + \"\', sizingMethod='scale');\\\"></span>\"\n"
693                 . "  myImage.outerHTML = strNewHTML\n"
694         . " }\n"
695                 . "}\n";
696         return $js;
697 }
698
699 function get_js_date_picker()
700 {
701     global $comp_path, $go_debug;
702     $fpath = $comp_path.'/'.user_company().'/js_cache/'.'date_picker.js';
703
704     if (!file_exists($fpath) || $go_debug) {
705
706         global $dateseps, $date_system;
707
708         $how = user_date_format();                              // 0 = us/ca, 1 = eu, au, nz, 2 = jp, sw
709         $sep = $dateseps[user_date_sep()];              // date separator
710         $wstart = (($date_system == 1 || $date_system == 2) ? 6 : ($how == 0 ? 0 : 1));                 // weekstart (sun = 0, mon = 1)
711         $months = array(_("January"),_("February"),_("March"),_("April"),_("May"),_("June"),_("July"),_("August"),_("September"),_("October"),_("November"),_("December"));
712         $wdays = array(_("Su"),_("Mo"),_("Tu"),_("We"),_("Th"),_("Fr"),_("Sa"));
713         $wno = _("W"); // week no
714         $back = _("Back");
715         if ($date_system == 1)
716                 list($cyear, $cmon, $cday) = gregorian_to_jalali(date("Y"), date("n"), date("j"));
717         else if ($date_system == 2)
718                 list($cyear, $cmon, $cday) = gregorian_to_islamic(date("Y"), date("n"), date("j"));
719
720
721         $js = "
722 function positionInfo(object) {
723   var p_elm = object;
724   this.getElementLeft = getElementLeft;
725   function getElementLeft() {
726     var x = 0;
727     var elm;
728     if(typeof(p_elm) == 'object'){
729       elm = p_elm;
730     } else {
731       elm = document.getElementById(p_elm);
732     }
733     while (elm != null) {
734       x+= elm.offsetLeft;
735       elm = elm.offsetParent;
736     }
737     return parseInt(x);
738   }
739   this.getElementWidth = getElementWidth;
740   function getElementWidth(){
741     var elm;
742     if(typeof(p_elm) == 'object'){
743       elm = p_elm;
744     } else {
745       elm = document.getElementById(p_elm);
746     }
747     return parseInt(elm.offsetWidth);
748   }
749   this.getElementRight = getElementRight;
750   function getElementRight(){
751     return getElementLeft(p_elm) + getElementWidth(p_elm);
752   }
753   this.getElementTop = getElementTop;
754   function getElementTop() {
755     var y = 0;
756     var elm;
757     if(typeof(p_elm) == 'object'){
758       elm = p_elm;
759     } else {
760       elm = document.getElementById(p_elm);
761     }
762     while (elm != null) {
763       y+= elm.offsetTop;
764       elm = elm.offsetParent;
765     }
766     return parseInt(y);
767   }
768   this.getElementHeight = getElementHeight;
769   function getElementHeight(){
770     var elm;
771     if(typeof(p_elm) == 'object'){
772       elm = p_elm;
773     } else {
774       elm = document.getElementById(p_elm);
775     }
776     return parseInt(elm.offsetHeight);
777   }
778   this.getElementBottom = getElementBottom;
779   function getElementBottom(){
780     return getElementTop(p_elm) + getElementHeight(p_elm);
781   }
782 }
783 function CC() {
784   var calendarId = 'CC';
785   var currentYear = 0;
786   var currentMonth = 0;
787   var currentDay = 0;
788   var selectedYear = 0;
789   var selectedMonth = 0;
790   var selectedDay = 0;
791   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]'];
792   var wdays = ['$wdays[0]', '$wdays[1]', '$wdays[2]', '$wdays[3]', '$wdays[4]', '$wdays[5]', '$wdays[6]'];
793   var dateField = null;
794   function getProperty(p_property){
795     var p_elm = calendarId;
796     var elm = null;
797     if(typeof(p_elm) == 'object'){
798       elm = p_elm;
799     } else {
800       elm = document.getElementById(p_elm);
801     }
802     if (elm != null){
803       if(elm.style){
804         elm = elm.style;
805         if(elm[p_property]){
806           return elm[p_property];
807         } else {
808           return null;
809         }
810       } else {
811         return null;
812       }
813     }
814   }
815   function setElementProperty(p_property, p_value, p_elmId){
816     var p_elm = p_elmId;
817     var elm = null;
818     if(typeof(p_elm) == 'object'){
819       elm = p_elm;
820     } else {
821       elm = document.getElementById(p_elm);
822     }
823     if((elm != null) && (elm.style != null)){
824       elm = elm.style;
825       elm[ p_property ] = p_value;
826     }
827   }
828   function setProperty(p_property, p_value) {
829     setElementProperty(p_property, p_value, calendarId);
830   }
831   function getDaysInMonth(year, month) {
832 ";
833         if ($date_system == 1)
834                 $js .= "
835     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];
836 ";
837         else if ($date_system == 2)
838                 $js .= "
839     return [30,29,30,29,30,29,30,29,30,29,30,(((((11 * year) + 14) % 30) < 11) ? 30 : 29)][month-1];
840 ";
841         else
842                 $js .= "
843     return [31,((!(year % 4 ) && ( (year % 100 ) || !( year % 400 ) ))?29:28),31,30,31,30,31,31,30,31,30,31][month-1];
844 ";
845         $js .= "
846   }
847   function getDayOfWeek(year, month, day) {
848 ";
849         if ($date_system == 1 || $date_system == 2)
850         {
851                 $js .= "
852         function mod(a, b)
853         {
854             return a - (b * Math.floor(a / b));
855         }
856         function jwday(j)
857         {
858             return mod(Math.floor((j + 1.5)), 7);
859         }
860 ";
861         if ($date_system == 1)
862                 $js .= "
863     var epbase, epyear, t;
864     epbase = year - ((year >= 0) ? 474 : 473);
865     epyear = 474 + mod(epbase, 2820);
866     t = day + ((month <= 7) ? ((month - 1) * 31) : (((month - 1) * 30) + 6)) +
867       Math.floor(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 +
868       Math.floor(epbase / 2820) * 1029983 + (1948320.5 - 1);
869 ";
870         else if ($date_system == 2)
871                 $js .= "
872         var t;
873         t = Math.floor((11 * year + 3) / 30) + 354 * year + 30 * month -
874           Math.floor((month - 1) / 2) + day + 1948440 - 385;
875 ";
876         $js .= "
877     return jwday(t);
878 ";
879         }
880         else
881                 $js .= "
882     var date = new Date(year,month-1,day)
883     return date.getDay();
884 ";
885         $js .= "
886   }
887   this.clearDate = clearDate;
888   function clearDate() {
889     dateField.value = '';
890     hide();
891   }
892   this.getWeek = getWeek;
893   function getWeek(year, month, day) {
894 ";
895         if ($how == 0)
896                 $js .= "  day++;";
897         $js .= "
898     var date = new Date(year,month-1,day);
899     var D = date.getDay();
900     if(D == 0) D = 7;
901     date.setDate(date.getDate() + (4 - D));
902     var YN = date.getFullYear();
903     var ZBDoCY = Math.floor((date.getTime() - new Date(YN, 0, 1, -6)) / 86400000);
904     var WN = 1 + Math.floor(ZBDoCY / 7);
905     return WN;
906   }
907   this.setDate = setDate;
908   function setDate(year, month, day) {
909     if (dateField) {
910       if (month < 10) {month = '0' + month;}
911       if (day < 10) {day = '0' + day;}
912 ";
913         if ($how == 0)
914                 $js .= "
915       var dateString = month+'$sep'+day+'$sep'+year;
916 ";
917         else if ($how == 1)
918                 $js .= "
919       var dateString = day+'$sep'+month+'$sep'+year;
920 ";
921         else
922                 $js .= "
923       var dateString = year+'$sep'+month+'$sep'+day;
924 ";
925         $js .= "
926       dateField.value = dateString;
927           setFocus(dateField.name);
928           if (dateField.className=='searchbox')
929                 dateField.onblur();
930       hide();
931     }
932     return;
933   }
934   this.changeMonth = changeMonth;
935   function changeMonth(change) {
936     currentMonth += change;
937     currentDay = 0;
938     if(currentMonth > 12) {
939       currentMonth = 1;
940       currentYear++;
941     } else if(currentMonth < 1) {
942       currentMonth = 12;
943       currentYear--;
944     }
945     calendar = document.getElementById(calendarId);
946     calendar.innerHTML = calendarDrawTable();
947   }
948   this.changeYear = changeYear;
949   function changeYear(change) {
950     currentYear += change;
951     currentDay = 0;
952     calendar = document.getElementById(calendarId);
953     calendar.innerHTML = calendarDrawTable();
954   }
955   function getCurrentYear() {
956     var year = new Date().getYear();
957     if(year < 1900) year += 1900;
958     return year;
959   }
960   function getCurrentMonth() {
961     return new Date().getMonth() + 1;
962   }
963   function getCurrentDay() {
964     return new Date().getDate();
965   }
966   function calendarDrawTable() {
967     var dayOfMonth = 1;
968     var wstart = $wstart;
969     var wno = '&nbsp;$wno&nbsp;';
970     var validDay = 0;
971     var startDayOfWeek = getDayOfWeek(currentYear, currentMonth, dayOfMonth);
972     var daysInMonth = getDaysInMonth(currentYear, currentMonth);
973     var css_class = null; //CSS class for each day
974     var table = \"<table cellspacing='0' cellpadding='0' border='0'>\";
975     table += \"<tr class='header'>\";
976     table += \"  <td colspan='2' class='previous'><a href='javascript:changeCCMonth(-1);'>&lt;</a><br><a href='javascript:changeCCYear(-1);'>&laquo;</a></td>\";
977     table += \"  <td colspan='4' class='title'>\" + months[currentMonth-1] + \"<br>\" + currentYear + \"</td>\";
978     table += \"  <td colspan='2' class='next'><a href='javascript:changeCCMonth(1);'>&gt;</a><br><a href='javascript:changeCCYear(1);'>&raquo;</a></td>\";
979     table += \"</tr>\";
980     table += \"<tr>\";
981     table += \"<th class='weekno'>\"+wno+\"</th>\";
982     for (var n=0; n<7; n++)
983         table += \"<th>\" + wdays[(wstart+n)%7]+\"</th>\";
984     table += \"</tr>\";
985     for(var week=0; week < 6; week++) {
986       table += \"<tr>\";
987       for(var n=0; n < 7; n++) {
988         dayOfWeek = (wstart+n)%7;
989         if(week == 0 && startDayOfWeek == dayOfWeek) {
990           validDay = 1;
991         } else if (validDay == 1 && dayOfMonth > daysInMonth) {
992           validDay = 0;
993         }
994         if (n==0)
995         {
996                 if (dayOfMonth > daysInMonth)
997                         table += \"<td class='empty'>&nbsp;</td>\";
998                 else
999                         table += \"<td class='weekno'>\"+getWeek(currentYear, currentMonth, dayOfMonth)+\"</td>\";
1000         }
1001         if(validDay) {
1002           if (dayOfMonth == selectedDay && currentYear == selectedYear && currentMonth == selectedMonth) {
1003             css_class = 'current';
1004 ";
1005         if ($date_system == 1 || $date_system == 2)
1006                 $js .= "
1007           } else if (dayOfWeek == 5) {
1008 ";
1009         else
1010                 $js .= "
1011           } else if (dayOfWeek == 0 || dayOfWeek == 6) {
1012 ";
1013                 $js .= "
1014             css_class = 'weekend';
1015           } else {
1016             css_class = 'weekday';
1017           }
1018           table += \"<td><a class='\"+css_class+\"' href=\\\"javascript:setCCDate(\"+currentYear+\",\"+currentMonth+\",\"+dayOfMonth+\")\\\">\"+dayOfMonth+\"</a></td>\";
1019           dayOfMonth++;
1020         } else {
1021           table += \"<td class='empty'>&nbsp;</td>\";
1022         }
1023       }
1024       table += \"</tr>\";
1025     }
1026     table += \"<tr class='header'><th colspan='8' style='padding: 3px;text-align:center;'><a href='javascript:hideCC();'>$back</a></td></tr>\";
1027     table += \"</table>\";
1028     return table;
1029   }
1030   this.show = show;
1031   function show(field) {
1032     can_hide = 0;
1033     if (dateField == field) {
1034       return;
1035     } else {
1036       dateField = field;
1037     }
1038     if(dateField) {
1039       try {
1040         var dateString = new String(dateField.value);
1041         var dateParts = dateString.split('$sep');
1042 ";
1043         if ($how == 0)
1044                 $js .= "
1045         selectedMonth = parseInt(dateParts[0],10);
1046         selectedDay = parseInt(dateParts[1],10);
1047         selectedYear = parseInt(dateParts[2],10);
1048 ";
1049         else if ($how == 1)
1050                 $js .= "
1051         selectedDay = parseInt(dateParts[0],10);
1052         selectedMonth = parseInt(dateParts[1],10);
1053         selectedYear = parseInt(dateParts[2],10);
1054 ";
1055         else
1056                 $js .= "
1057         selectedYear = parseInt(dateParts[0],10);
1058         selectedMonth = parseInt(dateParts[1],10);
1059         selectedDay = parseInt(dateParts[2],10);
1060 ";
1061         $js .= "
1062       } catch(e) {}
1063     }
1064     if (!(selectedYear && selectedMonth && selectedDay)) {
1065 ";
1066         if ($date_system == 1 || $date_system == 2)
1067         {
1068                 $js .= "
1069       selectedMonth = $cmon;
1070       selectedDay = $cday;
1071       selectedYear = $cyear;
1072 ";
1073         }
1074         else
1075         {
1076                 $js .= "
1077       selectedMonth = getCurrentMonth();
1078       selectedDay = getCurrentDay();
1079       selectedYear = getCurrentYear();
1080 ";
1081         }
1082         $js .= "
1083     }
1084     currentMonth = selectedMonth;
1085     currentDay = selectedDay;
1086     currentYear = selectedYear;
1087     if(document.getElementById){
1088       calendar = document.getElementById(calendarId);
1089       calendar.innerHTML = calendarDrawTable(currentYear, currentMonth);
1090       var fieldPos = new positionInfo(dateField);
1091       var calendarPos = new positionInfo(calendarId);
1092       var x = fieldPos.getElementLeft();
1093       var y = fieldPos.getElementBottom();
1094       setProperty('left', x + 'px');
1095       setProperty('top', y + 'px');
1096       setProperty('display', 'block');
1097       if (document.all) {
1098         setElementProperty('left', x + 'px', 'CCIframe');
1099         setElementProperty('top', y + 'px', 'CCIframe');
1100         setElementProperty('width', calendarPos.getElementWidth() + 'px', 'CCIframe');
1101         setElementProperty('height', calendarPos.getElementHeight() + 'px', 'CCIframe');
1102         setElementProperty('display', 'block', 'CCIframe');
1103       }
1104     }
1105   }
1106   this.hide = hide;
1107   function hide() {
1108     if(dateField) {
1109       setProperty('display', 'none');
1110       setElementProperty('display', 'none', 'CCIframe');
1111       dateField = null;
1112     }
1113   }
1114   this.visible = visible;
1115   function visible() {
1116     return dateField
1117   }
1118   this.can_hide = can_hide;
1119   var can_hide = 0;
1120 }
1121 var cC = new CC();
1122 function date_picker(textField) {
1123   cC.show(textField);
1124 }
1125 function hideCC() {
1126   if (cC.visible()) {
1127     cC.hide();
1128   }
1129 }
1130 function setCCDate(year, month, day) {
1131   cC.setDate(year, month, day);
1132 }
1133 function changeCCYear(change) {
1134   cC.changeYear(change);
1135 }
1136 function changeCCMonth(change) {
1137   cC.changeMonth(change);
1138 }
1139 document.write(\"<iframe id='CCIframe' src='javascript:false;' frameBorder='0' scrolling='no'></iframe>\");
1140 document.write(\"<div id='CC'></div>\");";
1141
1142      cache_js_file($fpath, $js);
1143     }
1144     add_js_ufile($fpath);
1145
1146  return '';
1147 }
1148
1149 function alert($msg)
1150 {
1151         echo "\n<script type=\"text/javascript\">\n"
1152                 . "<!--\n"
1153                 . "alert('$msg');\n"
1154                 . "-->\n"
1155                 . "</script>\n";
1156 }
1157
1158 if (!function_exists('_vd'))
1159 {
1160         function _vd($mixed, $title = '', $exit = false)
1161         {
1162         // Only the site admin is able to proceed here.
1163         $str = (!empty($title) ? ($title .':') : '') .'<pre>';
1164         $str .= print_r($mixed, true); //var_dump($mixed);
1165         $str .= "</pre>\n";
1166                 display_error('<table><tr><td>'.$str.'</td></tr></table>');
1167         if ($exit)
1168                 exit;
1169         }
1170 }
1171
1172 function display_backtrace($cond=true, $msg='') {
1173
1174         if ($cond) {
1175                 if ($msg) 
1176                         $str = "<center><span class='headingtext'>$msg</span></center>\n";
1177                 else
1178                         $str = '';
1179                 $str .= '<table border=0>';
1180         $trace = debug_backtrace();
1181                 foreach($trace as $trn => $tr) {
1182                         if (!$trn) continue;
1183                         $str .= '<tr><td>';
1184                         $str .= $tr['file'].':'.$tr['line'].': ';
1185                         $str .= '</td><td>';
1186                         if (isset($tr['type'])) {
1187                                 if($tr['type'] == '::') {
1188                                         $str .= $tr['class'].'::';
1189                                 } else if($tr['type'] == '->') {
1190                                         $str .= '('.$tr['class'].' Object)'.'->';
1191                                 }
1192                         }
1193                         foreach($tr['args'] as $n=>$a) {
1194                                 if (is_object($tr['args'][$n]))
1195                                         $tr['args'][$n] = "(".get_class($tr['args'][$n])." Object)";
1196                                 if (is_array($tr['args'][$n]))
1197                                         $tr['args'][$n] = "(Array[".count($tr['args'][$n])."])";
1198                                 else
1199                                         $tr['args'][$n] = "'".$tr['args'][$n]."'";
1200                         }
1201                         $str .= $tr['function'].'('. implode(',',$tr['args']).')</td>';
1202
1203                         $str .= '</tr>';
1204                 }
1205                 
1206                 $str .= '</table>';
1207         display_error($str);
1208         }
1209 }
1210
1211 ?>