Do not allow editing invoice if allocated > 0
[fa-stable.git] / sales / inquiry / customer_inquiry.php
1 <?php
2
3 $page_security = 1;
4 $path_to_root="../..";
5 include_once($path_to_root . "/includes/session.inc");
6
7 include_once($path_to_root . "/sales/includes/sales_ui.inc");
8 include_once($path_to_root . "/sales/includes/sales_db.inc");
9 include_once($path_to_root . "/reporting/includes/reporting.inc");
10
11 $js = "";
12 if ($use_popup_windows)
13         $js .= get_js_open_window(900, 500);
14 if ($use_date_picker)
15         $js .= get_js_date_picker();
16 page(_("Customer Transactions"), false, false, "", $js);
17
18
19 if (isset($_GET['customer_id']))
20 {
21         $_POST['customer_id'] = $_GET['customer_id'];
22 }
23
24 //------------------------------------------------------------------------------------------------
25
26 start_form();
27
28 if (!isset($_POST['customer_id']))
29         $_POST['customer_id'] = get_global_customer();
30
31 start_table("class='tablestyle_noborder'");
32 start_row();
33
34 customer_list_cells(_("Select a customer: "), 'customer_id', null, true);
35
36 date_cells(_("From:"), 'TransAfterDate', '', null, -30);
37 date_cells(_("To:"), 'TransToDate', '', null, 1);
38
39 if (!isset($_POST['filterType']))
40         $_POST['filterType'] = 0;
41
42 cust_allocations_list_cells(null, 'filterType', $_POST['filterType'], true);
43
44 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), true);
45 end_row();
46 end_table();
47
48 end_form();
49
50 set_global_customer($_POST['customer_id']);
51
52 //------------------------------------------------------------------------------------------------
53
54 function display_customer_summary($customer_record)
55 {
56         global $table_style;
57
58         $past1 = get_company_pref('past_due_days');
59         $past2 = 2 * $past1;
60     if ($customer_record["dissallow_invoices"] != 0)
61     {
62         echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
63     }
64
65         $nowdue = "1-" . $past1 . " " . _('Days');
66         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
67         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
68
69     start_table("width=80% $table_style");
70     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
71         $pastdue1, $pastdue2, _("Total Balance"));
72     table_header($th);
73
74         start_row();
75     label_cell($customer_record["curr_code"]);
76     label_cell($customer_record["terms"]);
77         amount_cell($customer_record["Balance"] - $customer_record["Due"]);
78         amount_cell($customer_record["Due"] - $customer_record["Overdue1"]);
79         amount_cell($customer_record["Overdue1"] - $customer_record["Overdue2"]);
80         amount_cell($customer_record["Overdue2"]);
81         amount_cell($customer_record["Balance"]);
82         end_row();
83
84         end_table();;
85 }
86
87 //------------------------------------------------------------------------------------------------
88
89 function get_transactions()
90 {
91     $date_after = date2sql($_POST['TransAfterDate']);
92     $date_to = date2sql($_POST['TransToDate']);
93
94     $sql = "SELECT ".TB_PREF."debtor_trans.*,
95                 ".TB_PREF."debtors_master.name AS CustName, ".TB_PREF."debtors_master.curr_code AS CustCurrCode,
96                 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
97                 .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount)
98                 AS TotalAmount, ".TB_PREF."debtor_trans.alloc AS Allocated,
99                 ((".TB_PREF."debtor_trans.type = 10)
100                         AND (".TB_PREF."debtor_trans.due_date < '" . date2sql(Today()) . "')
101                         AND ((".TB_PREF."debtor_trans.ov_amount + "
102                           .TB_PREF."debtor_trans.ov_gst + "
103                           .TB_PREF."debtor_trans.ov_freight )>".TB_PREF."debtor_trans.alloc
104                 )) AS OverDue
105                 FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
106                 WHERE ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
107                         AND ".TB_PREF."debtor_trans.tran_date >= '$date_after'
108                         AND ".TB_PREF."debtor_trans.tran_date <= '$date_to'";
109
110         if ($_POST['customer_id'] != reserved_words::get_all())
111                 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
112
113         if ($_POST['filterType'] != reserved_words::get_all())
114         {
115                 if ($_POST['filterType'] == '1')
116                 {
117                         $sql .= " AND (".TB_PREF."debtor_trans.type = 10 OR ".TB_PREF."debtor_trans.type = 1) ";
118                 }
119                 elseif ($_POST['filterType'] == '2')
120                 {
121                         $sql .= " AND (".TB_PREF."debtor_trans.type = 10) ";
122                 }
123                 elseif ($_POST['filterType'] == '3')
124                 {
125                         $sql .= " AND (".TB_PREF."debtor_trans.type = " . systypes::cust_payment() . " OR ".TB_PREF."debtor_trans.type = 2) ";
126                 }
127                 elseif ($_POST['filterType'] == '4')
128                 {
129                         $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
130                 }
131                 elseif ($_POST['filterType'] == '5')
132                 {
133                         $sql .= " AND ".TB_PREF."debtor_trans.type = 13 ";
134                 }
135
136         if ($_POST['filterType'] == '2')
137         {
138                 $today =  date2sql(Today());
139                 $sql .= " AND ".TB_PREF."debtor_trans.due_date < '$today'
140                                 AND (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
141                                 .TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_freight + "
142                                 .TB_PREF."debtor_trans.ov_discount - ".TB_PREF."debtor_trans.alloc > 0) ";
143         }
144         }
145
146     $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date DESC,
147           ".TB_PREF."debtor_trans.type,".TB_PREF."debtor_trans.trans_no ";
148
149     return db_query($sql,"No transactions were returned");
150 }
151
152 //------------------------------------------------------------------------------------------------
153
154 div_start('totals_tbl');
155 if ($_POST['customer_id'] != "" && $_POST['customer_id'] != reserved_words::get_all())
156 {
157         $customer_record = get_customer_details($_POST['customer_id'], $_POST['TransToDate']);
158     display_customer_summary($customer_record);
159     echo "<br>";
160 }
161 div_end();
162 //------------------------------------------------------------------------------------------------
163
164 $result = get_transactions();
165
166 //------------------------------------------------------------------------------------------------
167 if(get_post('RefreshInquiry'))
168 {
169         $Ajax->activate('trans_tbl');
170         $Ajax->activate('totals_tbl');
171 }
172 //------------------------------------------------------------------------------------------------
173 print_hidden_script(10);
174
175 div_start('trans_tbl');
176 if (db_num_rows($result) == 0)
177 {
178         display_note(_("The selected customer has no transactions for the given dates."), 0, 2);
179 } else {
180
181         start_table("$table_style width='80%'");
182
183         $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
184                 _("Customer"), _("Branch"), _("Currency"), _("Debit"), _("Credit"), "", "","","");
185
186         if ($_POST['customer_id'] != reserved_words::get_all()) {
187           unset($th[6], $th[8]);
188         }
189
190         table_header($th);
191
192
193         $j = 1;
194         $k = 0; //row colour counter
195         $over_due = false;
196         while ($myrow = db_fetch($result))
197         {
198
199         if ($myrow['OverDue'] == 1)
200         {
201                 start_row("class='overduebg'");
202                 $over_due = true;
203         }
204         else
205                 alt_table_row_color($k);
206
207         $edit_page='';
208         $due_date_str = '';
209         $credit_me_str = '';
210
211         switch($myrow['type']) {
212          case 10:
213                 $due_date_str = sql2date($myrow["due_date"]);
214                         /*Show a link to allow an invoice to be credited */
215                     // only allow crediting if it's not been totally allocated
216                 if ($myrow["TotalAmount"] - $myrow["Allocated"] > 0)
217                         $credit_me_str = "<a href='$path_to_root/sales/customer_credit_invoice.php?InvoiceNumber=" . $myrow["trans_no"] . "'>" . _("Credit This") . "</a>";
218                 if (get_voided_entry(10, $myrow["trans_no"]) === false && $myrow['Allocated'] == 0) // 2008-11-19 Joe Hunt
219                         $edit_page= $path_to_root.'/sales/customer_invoice.php?ModifyInvoice='
220                                         . $myrow['trans_no'];
221                 break;
222
223          case 11:
224                 if (get_voided_entry(11, $myrow["trans_no"]) === false && $myrow['Allocated'] == 0) // 2008-11-19 Joe Hunt
225                 {        
226                         if ($myrow['order_']==0) // free-hand credit note
227                             $edit_page= $path_to_root.'/sales/credit_note_entry.php?ModifyCredit='
228                                         . $myrow['trans_no'];
229                         else    // credit invoice
230                             $edit_page= $path_to_root.'/sales/customer_credit_invoice.php?ModifyCredit='
231                                         . $myrow['trans_no'];
232                 }                       
233                 break;
234
235          case 13:
236                 if (get_voided_entry(13, $myrow["trans_no"]) === false)
237                         $edit_page= $path_to_root.'/sales/customer_delivery.php?ModifyDelivery='
238                                         . $myrow['trans_no']; 
239                 break;
240         }
241
242         $date = sql2date($myrow["tran_date"]);
243
244         if ($myrow["order_"] > 0)
245                 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
246         else
247                 $preview_order_str = "";
248
249         $gl_trans_str = get_gl_view_str_cell($myrow["type"], $myrow["trans_no"]);
250
251         $branch_name = "";
252         if ($myrow["branch_code"] > 0)
253         {
254                 $branch_name = get_branch_name($myrow["branch_code"]);
255         }
256
257         $preview_trans_str = get_trans_view_str($myrow["type"], $myrow["trans_no"]);
258
259         label_cell(systypes::name($myrow["type"]));
260
261         label_cell($preview_trans_str);
262         label_cell($preview_order_str);
263         label_cell($myrow["reference"]);
264         label_cell($date, "nowrap");
265         label_cell($due_date_str, "nowrap");
266         if ($_POST['customer_id'] == reserved_words::get_all())
267                 label_cell($myrow["CustName"]);
268         label_cell($branch_name);
269         if ($_POST['customer_id'] == reserved_words::get_all())
270                 label_cell($myrow["CustCurrCode"]);
271         display_debit_or_credit_cells(
272             $myrow['type']==11 || $myrow['type']==12 || $myrow['type']==2 ?
273                 -$myrow["TotalAmount"] : $myrow["TotalAmount"]);
274
275         echo $gl_trans_str;
276
277         label_cell($edit_page=='' ? '' :        "<a href='$edit_page'>" . _('Edit') . '</a>');
278         if ($myrow['type'] == 10 || $myrow['type'] == 11)
279                 label_cell(print_document_link($myrow['trans_no'], _("Print"), true, $myrow['type']));
280         else
281                 label_cell("");
282
283         if ($credit_me_str != "")
284                 label_cell($credit_me_str, "nowrap");
285         else
286                 label_cell('');
287         end_row();
288
289                 $j++;
290                 if ($j == 12)
291                 {
292                         $j = 1;
293                         table_header($th);
294                 } //end of page full new headings if
295         } //end of transaction while loop
296
297         end_table(1);
298
299         if ($over_due)
300                 display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
301 }
302 div_end();
303 end_page();
304
305 ?>