Customer Payment, Supplier Payment: early discount support.
[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 $page_security = 'SA_SALESALLOC';
13 $path_to_root = "../..";
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 ($SysPrefs->use_popup_windows)
21         $js .= get_js_open_window(900, 500);
22 page(_($help_context = "Customer Allocations"), false, false, "", $js);
23
24 //--------------------------------------------------------------------------------
25
26 start_form();
27 /* show all outstanding receipts and credits to be allocated */
28
29 if (!isset($_POST['customer_id']))
30         $_POST['customer_id'] = get_global_customer();
31
32 echo "<center>" . _("Select a customer: ") . "&nbsp;&nbsp;";
33 echo customer_list('customer_id', $_POST['customer_id'], true, true);
34 echo "<br>";
35
36 check(_("Show Settled Items:"), 'ShowSettled', null, true);
37 echo "</center><br><br>";
38
39 set_global_customer($_POST['customer_id']);
40
41 if (isset($_POST['customer_id']) && ($_POST['customer_id'] == ALL_TEXT))
42 {
43         unset($_POST['customer_id']);
44 }
45
46 $settled = false;
47 if (check_value('ShowSettled'))
48         $settled = true;
49
50 $customer_id = null;
51 if (isset($_POST['customer_id']))
52         $customer_id = $_POST['customer_id'];
53
54 //--------------------------------------------------------------------------------
55 function systype_name($dummy, $type)
56 {
57         global $systypes_array;
58
59         return $systypes_array[$type];
60 }
61
62 function trans_view($trans)
63 {
64         return get_trans_view_str($trans["type"], $trans["trans_no"]);
65 }
66
67 function alloc_link($row)
68 {
69         global $all_settled;
70
71         return pager_link(_("Allocate"),
72                 "/sales/allocations/customer_allocate.php?trans_no="
73                         .$row["trans_no"] . "&trans_type=" . $row["type"]. "&debtor_no=" . $row["debtor_no"], ICON_ALLOC);
74 }
75
76 function amount_total($row)
77 {
78         return price_format($row['type'] == ST_JOURNAL && $row["Total"] < 0 ? -$row["Total"] : $row["Total"]);
79 }
80
81 function amount_left($row)
82 {
83         return price_format(($row['type'] == ST_JOURNAL && $row["Total"] < 0 ? -$row["Total"] : $row["Total"])-$row["alloc"]);
84 }
85
86 function check_settled($row)
87 {
88         return $row['settled'] == 1;
89 }
90
91 $all_settled = !db_num_rows(get_allocatable_to_cust_transactions($customer_id));
92
93 if ($all_settled)
94         display_note('<b>'.
95                 ($customer_id ? _("There is no unsettled transactions for this customer.")
96                         :_("There is no unsettled transactions.")).'</b><p>');
97
98 $sql = get_allocatable_from_cust_sql($customer_id, $settled);
99
100 $cols = array(
101         _("Transaction Type") => array('fun'=>'systype_name'),
102         _("#") => array('fun'=>'trans_view', 'align'=>'right'),
103         _("Reference"), 
104         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'asc'),
105         _("Customer") => array('ord'=>''),
106         _("Currency") => array('align'=>'center'),
107         _("Total") => array('align'=>'right','fun'=>'amount_total'), 
108         _("Left to Allocate") => array('align'=>'right','insert'=>true, 'fun'=>'amount_left'), 
109         array('insert'=>true, 'fun'=>'alloc_link')
110         );
111
112 if (isset($_POST['customer_id'])) {
113         $cols[_("Customer")] = 'skip';
114         $cols[_("Currency")] = 'skip';
115 }
116
117 $table =& new_db_pager('alloc_tbl', $sql, $cols);
118 $table->set_marker('check_settled', _("Marked items are settled."), 'settledbg', 'settledfg');
119
120 $table->width = "75%";
121
122 display_db_pager($table);
123 end_form();
124
125 end_page();