Merging version 2.1 RC to main trunk.
[fa-stable.git] / sales / allocations / customer_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 . "/sales/includes/sales_ui.inc");
18 include_once($path_to_root . "/sales/includes/sales_db.inc");
19 $js = "";
20 if ($use_popup_windows)
21         $js .= get_js_open_window(900, 500);
22 page(_("Customer Allocations"), false, false, "", $js);
23
24 //--------------------------------------------------------------------------------
25 if ($ret = context_restore()) {
26         if(isset($ret['customer_id']))
27                 $_POST['customer_id'] = $ret['customer_id'];
28 }
29 if (isset($_POST['_customer_id_editor'])) {
30         context_call($path_to_root.'/sales/manage/customers.php?debtor_no='.$_POST['customer_id'] );
31 }
32
33 start_form();
34         /* show all outstanding receipts and credits to be allocated */
35         /*Clear any previous allocation records */
36         if (isset($_SESSION['alloc']))
37         {
38                 unset($_SESSION['alloc']->allocs);
39                 unset($_SESSION['alloc']);
40         }
41     if (!isset($_POST['customer_id']))
42         $_POST['customer_id'] = get_global_customer();
43
44     echo "<center>" . _("Select a customer: ") . "&nbsp;&nbsp;";
45         customer_list('customer_id', $_POST['customer_id'], true, true);
46     echo "<br>";
47     check(_("Show Settled Items:"), 'ShowSettled', null, true);
48         echo "</center><br><br>";
49
50         set_global_customer($_POST['customer_id']);
51
52         if (isset($_POST['customer_id']) && ($_POST['customer_id'] == reserved_words::get_all()))
53         {
54                 unset($_POST['customer_id']);
55         }
56
57         /*if (isset($_POST['customer_id'])) {
58                 $custCurr = get_customer_currency($_POST['customer_id']);
59                 if (!is_company_currency($custCurr))
60                         echo _("Customer Currency:") . $custCurr;
61         }*/
62
63         $settled = false;
64         if (check_value('ShowSettled'))
65                 $settled = true;
66
67         $customer_id = null;
68         if (isset($_POST['customer_id']))
69                 $customer_id = $_POST['customer_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                 "/sales/allocations/customer_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 check_settled($row)
95 {
96         return $row['settled'] == 1;
97 }
98
99
100 $sql = get_allocatable_from_cust_sql($customer_id, $settled);
101
102 $cols = array(
103         _("Transaction Type") => array('fun'=>'systype_name'),
104         _("#") => array('fun'=>'trans_view'),
105         _("Reference"), 
106         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'asc'),
107         _("Customer") => array('ord'=>''),
108         _("Currency") => array('align'=>'center'),
109         _("Total") => 'amount', 
110         _("Left to Allocate") => array('align'=>'right','insert'=>true, 'fun'=>'amount_left'), 
111         array('insert'=>true, 'fun'=>'alloc_link')
112         );
113
114 if (isset($_POST['customer_id'])) {
115         $cols[_("Customer")] = 'skip';
116         $cols[_("Currency")] = 'skip';
117 }
118
119 $table =& new_db_pager('alloc_tbl', $sql, $cols);
120 $table->set_marker('check_settled', _("Marked items are settled."), 'settledbg', 'settledfg');
121
122 if (get_post('_ShowSettled_update') || get_post('_customer_id_update')) {
123         $table->set_sql($sql);
124         $table->set_columns($cols);
125 }
126 $table->width = "75%";
127
128 display_db_pager($table);
129 end_form();
130
131 end_page();
132 ?>