Next fixes to db_pager usage/behaviour.
[fa-stable.git] / purchasing / allocations / supplier_allocation_main.php
1 <?php
2
3 $path_to_root="../..";
4 $page_security = 3;
5 include($path_to_root . "/includes/db_pager.inc");
6 include_once($path_to_root . "/includes/session.inc");
7
8 include_once($path_to_root . "/includes/date_functions.inc");
9
10 include_once($path_to_root . "/includes/ui.inc");
11 include_once($path_to_root . "/includes/banking.inc");
12
13 include_once($path_to_root . "/sales/includes/sales_db.inc");
14 $js = "";
15 if ($use_popup_windows)
16         $js .= get_js_open_window(900, 500);
17 page(_("Supplier Allocations"), false, false, "", $js);
18
19 //--------------------------------------------------------------------------------
20 if ($ret = context_restore()) {
21         if(isset($ret['supplier_id']))
22                 $_POST['supplier_id'] = $ret['supplier_id'];
23 }
24 if (isset($_POST['_supplier_id_editor'])) {
25         context_call($path_to_root.'/purchasing/manage/suppliers.php?supplier_id='.$_POST['supplier_id'] );
26 }
27
28 //--------------------------------------------------------------------------------
29
30 start_form();
31
32         /* show all outstanding receipts and credits to be allocated */
33         /*Clear any previous allocation records */
34         if (isset($_SESSION['alloc']))
35         {
36                 unset($_SESSION['alloc']->allocs);
37                 unset($_SESSION['alloc']);
38         }
39     if (!isset($_POST['supplier_id']))
40         $_POST['supplier_id'] = get_global_supplier();
41
42     echo "<center>" . _("Select a Supplier: ") . "&nbsp;&nbsp;";
43         supplier_list('supplier_id', $_POST['supplier_id'], true, true);
44     echo "<br>";
45     check(_("Show Settled Items:"), 'ShowSettled', null, true);
46         echo "</center><br><br>";
47         end_form();
48         set_global_supplier($_POST['supplier_id']);
49
50         if (isset($_POST['supplier_id']) && ($_POST['supplier_id'] == reserved_words::get_all())) 
51         {
52                 unset($_POST['supplier_id']);
53         }
54
55         $settled = false;
56         if (check_value('ShowSettled'))
57                 $settled = true;
58         $supplier_id = null;
59         if (isset($_POST['supplier_id']))
60                 $supplier_id = $_POST['supplier_id'];
61
62 //--------------------------------------------------------------------------------
63 function systype_name($dummy, $type)
64 {
65         return systypes::name($type);
66 }
67
68 function trans_view($trans)
69 {
70         return get_trans_view_str($trans["type"], $trans["trans_no"]);
71 }
72
73 function alloc_link($row)
74 {
75         return pager_link(_("Allocate"),
76                 "/purchasing/allocations/supplier_allocate.php?trans_no="
77                         .$row["trans_no"] . "&trans_type=" . $row["type"] );
78 }
79
80 function amount_left($row)
81 {
82         return -$row["Total"]-$row["alloc"];
83 }
84
85 function amount_total($row)
86 {
87         return -$row["Total"];
88 }
89
90 function check_settled($row)
91 {
92         return $row['settled'] == 1;
93 }
94
95
96 $sql = get_allocatable_from_supp_sql($supplier_id, $settled);
97
98 $cols = array(
99         _("Transaction Type") => array('fun'=>'systype_name'),
100         _("#") => array('fun'=>'trans_view'),
101         _("Reference"), 
102         _("Date") => array('type'=>'date', 'ord'=>'asc'),
103         _("Supplier") => array('ord'=>''),
104         _("Currency") => array('align'=>'center'),
105         _("Total") => 'amount', 
106         _("Left to Allocate") => array('align'=>'right','insert'=>true, 'fun'=>'amount_left'), 
107         array('insert'=>true, 'fun'=>'alloc_link')
108         );
109
110 if (isset($_POST['customer_id'])) {
111         $cols[_("Supplier")] = 'skip';
112         $cols[_("Currency")] = 'skip';
113 }
114
115 $table =& new_db_pager('alloc_tbl', $sql, $cols);
116 $table->set_marker('check_settled', _("Marked items are settled."), 'settledbg', 'settledfg');
117
118 if (get_post('_ShowSettled_update') || get_post('_supplier_id_update') ) {
119         $table->set_sql($sql);
120         $table->set_columns($cols);
121 }
122 start_form();
123
124 display_db_pager($table);
125
126 end_form();
127 end_page();
128 ?>