7f3b6196a5c07c251421d1363439fb4c9be3998e
[fa-stable.git] / purchasing / inquiry / supplier_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_SUPPLIERALLOC';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/db_pager.inc");
15 include($path_to_root . "/includes/session.inc");
16
17 include($path_to_root . "/purchasing/includes/purchasing_ui.inc");
18 $js = "";
19 if ($SysPrefs->use_popup_windows)
20         $js .= get_js_open_window(900, 500);
21 if (user_use_date_picker())
22         $js .= get_js_date_picker();
23 page(_($help_context = "Supplier Allocation Inquiry"), false, false, "", $js);
24
25 if (isset($_GET['supplier_id']))
26 {
27         $_POST['supplier_id'] = $_GET['supplier_id'];
28 }
29 if (isset($_GET['FromDate']))
30 {
31         $_POST['TransAfterDate'] = $_GET['FromDate'];
32 }
33 if (isset($_GET['ToDate']))
34 {
35         $_POST['TransToDate'] = $_GET['ToDate'];
36 }
37
38 //------------------------------------------------------------------------------------------------
39
40 start_form();
41
42 if (!isset($_POST['supplier_id']))
43         $_POST['supplier_id'] = get_global_supplier();
44
45 start_table(TABLESTYLE_NOBORDER);
46 start_row();
47
48 supplier_list_cells(_("Select a supplier: "), 'supplier_id', $_POST['supplier_id'], true);
49
50 date_cells(_("From:"), 'TransAfterDate', '', null, -user_transaction_days());
51 date_cells(_("To:"), 'TransToDate', '', null, 1);
52
53 supp_allocations_list_cell("filterType", null);
54
55 check_cells(_("show settled:"), 'showSettled', null);
56
57 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
58
59 set_global_supplier($_POST['supplier_id']);
60
61 end_row();
62 end_table();
63 //------------------------------------------------------------------------------------------------
64 function check_overdue($row)
65 {
66         return ($row['TotalAmount']>$row['Allocated']) && 
67                 $row['OverDue'] == 1;
68 }
69
70 function systype_name($dummy, $type)
71 {
72         global $systypes_array;
73         
74         return $systypes_array[$type];
75 }
76
77 function view_link($trans)
78 {
79         return get_trans_view_str($trans["type"], $trans["trans_no"]);
80 }
81
82 function due_date($row)
83 {
84         return (($row["type"] == ST_SUPPINVOICE) || ($row["type"]== ST_SUPPCREDIT))
85                 ? $row["due_date"] : "";
86 }
87
88 function fmt_balance($row)
89 {
90         $value = ($row["type"] == ST_BANKPAYMENT || $row["type"] == ST_SUPPCREDIT || $row["type"] == ST_SUPPAYMENT)     ? -$row["TotalAmount"] - $row["Allocated"]
91                 : ($row["type"] == ST_JOURNAL ? abs($row["TotalAmount"]) - $row["Allocated"] :
92                         $row["TotalAmount"] - $row["Allocated"]);
93         return $value;
94 }
95
96 function alloc_link($row)
97 {
98         $link = 
99         pager_link(_("Allocations"),
100                 "/purchasing/allocations/supplier_allocate.php?trans_no=" .
101                         $row["trans_no"]. "&trans_type=" . $row["type"]. "&supplier_id=" . $row["supplier_id"], ICON_ALLOC );
102
103         if ($row["type"] == ST_BANKPAYMENT || $row["type"] == ST_SUPPAYMENT ||
104                 (($row["type"] == ST_SUPPCREDIT || $row["type"] == ST_JOURNAL) && $row["TotalAmount"] < 0))
105                 return floatcmp(-$row["TotalAmount"], $row["Allocated"]) ? $link : '';
106
107         $link = 
108         pager_link(_("Payment"),
109                 "/purchasing/supplier_payment.php?supplier_id=".$row["supplier_id"]."&PInvoice=" 
110                         . $row["trans_no"]."&trans_type=" . $row["type"], ICON_MONEY);
111
112         if ($row["type"] == ST_SUPPINVOICE || (($row["type"] == ST_SUPPCREDIT || $row["type"] == ST_JOURNAL) && $row["TotalAmount"] > 0))
113                 return floatcmp($row["TotalAmount"], $row["Allocated"]) ? $link : '';
114
115
116 }
117
118 function fmt_debit($row)
119 {
120         $value = -$row["TotalAmount"];
121         return $value>=0 ? price_format($value) : '';
122
123 }
124
125 function fmt_credit($row)
126 {
127         $value = $row["TotalAmount"];
128         return $value>0 ? price_format($value) : '';
129 }
130 //------------------------------------------------------------------------------------------------
131
132 $sql = get_sql_for_supplier_allocation_inquiry(get_post('TransAfterDate'),get_post('TransToDate'),
133         get_post('filterType'), get_post('supplier_id'), check_value('showSettled'));
134
135 $cols = array(
136         _("Type") => array('fun'=>'systype_name'),
137         _("#") => array('fun'=>'view_link', 'ord'=>''),
138         _("Reference"), 
139         _("Supplier") => array('ord'=>''), 
140         _("Supp Reference"),
141         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'asc'),
142         _("Due Date") => array('fun'=>'due_date'),
143         _("Currency") => array('align'=>'center'),
144         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
145         _("Credit") => array('align'=>'right', 'insert'=>true, 'fun'=>'fmt_credit'), 
146         _("Allocated") => 'amount', 
147         _("Balance") => array('type'=>'amount', 'insert'=>true, 'fun'=>'fmt_balance'),
148         array('insert'=>true, 'fun'=>'alloc_link')
149         );
150
151 if ($_POST['supplier_id'] != ALL_TEXT) {
152         $cols[_("Supplier")] = 'skip';
153         $cols[_("Currency")] = 'skip';
154 }
155 //------------------------------------------------------------------------------------------------
156
157 $table =& new_db_pager('doc_tbl', $sql, $cols);
158 $table->set_marker('check_overdue', _("Marked items are overdue."));
159
160 $table->width = "90%";
161
162 display_db_pager($table);
163
164 end_form();
165 end_page();