Merging version 2.1 RC to main trunk.
[fa-stable.git] / purchasing / allocations / supplier_allocation_main.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $path_to_root="../..";
13 $page_security = 3;
14 include($path_to_root . "/includes/db_pager.inc");
15 include_once($path_to_root . "/includes/session.inc");
16
17 include_once($path_to_root . "/includes/date_functions.inc");
18
19 include_once($path_to_root . "/includes/ui.inc");
20 include_once($path_to_root . "/includes/banking.inc");
21
22 include_once($path_to_root . "/sales/includes/sales_db.inc");
23 $js = "";
24 if ($use_popup_windows)
25         $js .= get_js_open_window(900, 500);
26 page(_("Supplier Allocations"), false, false, "", $js);
27
28 //--------------------------------------------------------------------------------
29 if ($ret = context_restore()) {
30         if(isset($ret['supplier_id']))
31                 $_POST['supplier_id'] = $ret['supplier_id'];
32 }
33 if (isset($_POST['_supplier_id_editor'])) {
34         context_call($path_to_root.'/purchasing/manage/suppliers.php?supplier_id='.$_POST['supplier_id'] );
35 }
36
37 //--------------------------------------------------------------------------------
38
39 start_form();
40
41         /* show all outstanding receipts and credits to be allocated */
42         /*Clear any previous allocation records */
43         if (isset($_SESSION['alloc']))
44         {
45                 unset($_SESSION['alloc']->allocs);
46                 unset($_SESSION['alloc']);
47         }
48     if (!isset($_POST['supplier_id']))
49         $_POST['supplier_id'] = get_global_supplier();
50
51     echo "<center>" . _("Select a Supplier: ") . "&nbsp;&nbsp;";
52         supplier_list('supplier_id', $_POST['supplier_id'], true, true);
53     echo "<br>";
54     check(_("Show Settled Items:"), 'ShowSettled', null, true);
55         echo "</center><br><br>";
56         end_form();
57         set_global_supplier($_POST['supplier_id']);
58
59         if (isset($_POST['supplier_id']) && ($_POST['supplier_id'] == reserved_words::get_all())) 
60         {
61                 unset($_POST['supplier_id']);
62         }
63
64         $settled = false;
65         if (check_value('ShowSettled'))
66                 $settled = true;
67         $supplier_id = null;
68         if (isset($_POST['supplier_id']))
69                 $supplier_id = $_POST['supplier_id'];
70
71 //--------------------------------------------------------------------------------
72 function systype_name($dummy, $type)
73 {
74         return systypes::name($type);
75 }
76
77 function trans_view($trans)
78 {
79         return get_trans_view_str($trans["type"], $trans["trans_no"]);
80 }
81
82 function alloc_link($row)
83 {
84         return pager_link(_("Allocate"),
85                 "/purchasing/allocations/supplier_allocate.php?trans_no="
86                         .$row["trans_no"] . "&trans_type=" . $row["type"], ICON_MONEY );
87 }
88
89 function amount_left($row)
90 {
91         return price_format(-$row["Total"]-$row["alloc"]);
92 }
93
94 function amount_total($row)
95 {
96         return -$row["Total"];
97 }
98
99 function check_settled($row)
100 {
101         return $row['settled'] == 1;
102 }
103
104
105 $sql = get_allocatable_from_supp_sql($supplier_id, $settled);
106
107 $cols = array(
108         _("Transaction Type") => array('fun'=>'systype_name'),
109         _("#") => array('fun'=>'trans_view'),
110         _("Reference"), 
111         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'asc'),
112         _("Supplier") => array('ord'=>''),
113         _("Currency") => array('align'=>'center'),
114         _("Total") => 'amount', 
115         _("Left to Allocate") => array('align'=>'right','insert'=>true, 'fun'=>'amount_left'), 
116         array('insert'=>true, 'fun'=>'alloc_link')
117         );
118
119 if (isset($_POST['customer_id'])) {
120         $cols[_("Supplier")] = 'skip';
121         $cols[_("Currency")] = 'skip';
122 }
123
124 $table =& new_db_pager('alloc_tbl', $sql, $cols);
125 $table->set_marker('check_settled', _("Marked items are settled."), 'settledbg', 'settledfg');
126
127 if (get_post('_ShowSettled_update') || get_post('_supplier_id_update') ) {
128         $table->set_sql($sql);
129         $table->set_columns($cols);
130 }
131 $table->width = "80%";
132 start_form();
133
134 display_db_pager($table);
135
136 end_form();
137 end_page();
138 ?>