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