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