A bug in Customer Allocations fixed.
[fa-stable.git] / sales / inquiry / customer_allocation_inquiry.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
20 $js = "";
21 if ($SysPrefs->use_popup_windows)
22         $js .= get_js_open_window(900, 500);
23 if (user_use_date_picker())
24         $js .= get_js_date_picker();
25 page(_($help_context = "Customer Allocation Inquiry"), false, false, "", $js);
26
27 if (isset($_GET['customer_id']))
28 {
29         $_POST['customer_id'] = $_GET['customer_id'];
30 }
31
32 //------------------------------------------------------------------------------------------------
33
34 if (!isset($_POST['customer_id']))
35         $_POST['customer_id'] = get_global_customer();
36
37 start_form();
38
39 start_table(TABLESTYLE_NOBORDER);
40 start_row();
41
42 customer_list_cells(_("Select a customer: "), 'customer_id', $_POST['customer_id'], true);
43
44 date_cells(_("from:"), 'TransAfterDate', '', null, -user_transaction_days());
45 date_cells(_("to:"), 'TransToDate', '', null, 1);
46
47 cust_allocations_list_cells(_("Type:"), 'filterType', null);
48
49 check_cells(" " . _("show settled:"), 'showSettled', null);
50
51 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
52
53 set_global_customer($_POST['customer_id']);
54
55 end_row();
56 end_table();
57 //------------------------------------------------------------------------------------------------
58 function check_overdue($row)
59 {
60         return ($row['OverDue'] == 1 
61                 && (abs($row["TotalAmount"]) - $row["Allocated"] != 0));
62 }
63
64 function order_link($row)
65 {
66         return $row['order_']>0 ?
67                 get_customer_trans_view_str(ST_SALESORDER, $row['order_'])
68                 : "";
69 }
70
71 function systype_name($dummy, $type)
72 {
73         global $systypes_array;
74
75         return $systypes_array[$type];
76 }
77
78 function view_link($trans)
79 {
80         return get_trans_view_str($trans["type"], $trans["trans_no"]);
81 }
82
83 function due_date($row)
84 {
85         return $row["type"] == ST_SALESINVOICE ? $row["due_date"] : '';
86 }
87
88 function fmt_balance($row)
89 {
90         return ($row["type"] == ST_JOURNAL && $row["TotalAmount"] < 0 ? -$row["TotalAmount"] : $row["TotalAmount"]) - $row["Allocated"];
91 }
92
93 function alloc_link($row)
94 {
95         $link = 
96         pager_link(_("Allocation"),
97                 "/sales/allocations/customer_allocate.php?trans_no=" . $row["trans_no"] 
98                 ."&trans_type=" . $row["type"]."&debtor_no=" . $row["debtor_no"], ICON_ALLOC);
99
100         if ($row["type"] == ST_CUSTCREDIT && $row['TotalAmount'] > 0)
101         {
102                 /*its a credit note which could have an allocation */
103                 return $link;
104         } elseif ($row["type"] == ST_JOURNAL && $row['TotalAmount'] < 0)
105         {
106                 return $link;
107         } elseif (($row["type"] == ST_CUSTPAYMENT || $row["type"] == ST_BANKDEPOSIT) &&
108                 (floatcmp($row['TotalAmount'], $row['Allocated']) >= 0))
109         {
110                 /*its a receipt  which could have an allocation*/
111                 return $link;
112         }
113         elseif ($row["type"] == ST_CUSTPAYMENT && $row['TotalAmount'] <= 0)
114         {
115                 /*its a negative receipt */
116                 return '';
117         } elseif (($row["type"] == ST_SALESINVOICE && ($row['TotalAmount'] - $row['Allocated']) > 0) || $row["type"] == ST_BANKPAYMENT)
118                 return pager_link(_("Payment"),
119                         "/sales/customer_payments.php?customer_id=".$row["debtor_no"]."&SInvoice=" . $row["trans_no"], ICON_MONEY);
120
121 }
122
123 function fmt_debit($row)
124 {
125         $value =
126             $row['type']==ST_CUSTCREDIT || $row['type']==ST_CUSTPAYMENT || $row['type']==ST_BANKDEPOSIT ?
127                 -$row["TotalAmount"] : $row["TotalAmount"];
128         return $value>=0 ? price_format($value) : '';
129
130 }
131
132 function fmt_credit($row)
133 {
134         $value =
135             !($row['type']==ST_CUSTCREDIT || $row['type']==ST_CUSTPAYMENT || $row['type']==ST_BANKDEPOSIT) ?
136                 -$row["TotalAmount"] : $row["TotalAmount"];
137         return $value>0 ? price_format($value) : '';
138 }
139 //------------------------------------------------------------------------------------------------
140
141 $sql = get_sql_for_customer_allocation_inquiry(get_post('TransAfterDate'), get_post('TransToDate'),
142                 get_post('customer_id'), get_post('filterType'), check_value('showSettled'));
143
144 //------------------------------------------------------------------------------------------------
145 $cols = array(
146         _("Type") => array('fun'=>'systype_name'),
147         _("#") => array('fun'=>'view_link', 'align'=>'right'),
148         _("Reference"), 
149         _("Order") => array('fun'=>'order_link', 'ord'=>'', 'align'=>'right'), 
150         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'asc'),
151         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'),
152         _("Customer") => array('name' =>'name',  'ord'=>'asc'), 
153         _("Currency") => array('align'=>'center'),
154         _("Debit") => array('align'=>'right','fun'=>'fmt_debit'), 
155         _("Credit") => array('align'=>'right','insert'=>true, 'fun'=>'fmt_credit'), 
156         _("Allocated") => 'amount', 
157         _("Balance") => array('type'=>'amount', 'insert'=>true, 'fun'=>'fmt_balance'),
158         array('insert'=>true, 'fun'=>'alloc_link')
159         );
160
161 if ($_POST['customer_id'] != ALL_TEXT) {
162         $cols[_("Customer")] = 'skip';
163         $cols[_("Currency")] = 'skip';
164 }
165
166 $table =& new_db_pager('doc_tbl', $sql, $cols);
167 $table->set_marker('check_overdue', _("Marked items are overdue."));
168
169 $table->width = "80%";
170
171 display_db_pager($table);
172
173 end_form();
174 end_page();