Fixed bug no 1703891 Reserved MySQL words used as column names
[fa-stable.git] / sales / inquiry / customer_allocation_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
10 $js = "";
11 if ($use_popup_windows)
12         $js .= get_js_open_window(900, 500);
13 page(_("Customer Allocation Inquiry"), false, false, "", $js);
14
15 if (isset($_GET['customer_id']))
16 {
17         $_POST['customer_id'] = $_GET['customer_id'];
18 }
19
20 //------------------------------------------------------------------------------------------------
21
22 if (!isset($_POST['customer_id']))
23         $_POST['customer_id'] = get_global_customer();
24
25 start_form(false, true);
26
27 start_table("class='tablestyle_noborder'");
28 start_row();
29
30 customer_list_cells(_("Select a customer: "), 'customer_id', $_POST['customer_id'], true);
31
32 date_cells(_("from:"), 'TransAfterDate', null, -30);
33 date_cells(_("to:"), 'TransToDate', null, 1);
34
35 cust_allocations_list_cells(_("Type:"), 'filterType', null);
36
37 check_cells(" " . _("show settled:"), 'showSettled', null);
38
39 submit_cells('Refresh Inquiry', _("Search"));
40
41 set_global_customer($_POST['customer_id']);
42
43 end_row();
44 end_table();
45 end_form();
46
47 //------------------------------------------------------------------------------------------------
48
49 function get_transactions()
50 {
51     $data_after = date2sql($_POST['TransAfterDate']);
52     $date_to = date2sql($_POST['TransToDate']);
53
54     $sql = "SELECT ".TB_PREF."debtor_trans.*,
55                 ".TB_PREF."debtors_master.name AS CustName, ".TB_PREF."debtors_master.curr_code AS CustCurrCode,
56         (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount)
57                 AS TotalAmount,
58                 ".TB_PREF."debtor_trans.alloc AS Allocated,
59                 ((".TB_PREF."debtor_trans.type = 10)
60                 AND ".TB_PREF."debtor_trans.due_date < '" . date2sql(Today()) . "') AS OverDue
61         FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
62         WHERE ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
63                         AND (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount != 0)
64                 AND ".TB_PREF."debtor_trans.tran_date >= '$data_after'
65                 AND ".TB_PREF."debtor_trans.tran_date <= '$date_to'";
66
67         if ($_POST['customer_id'] != reserved_words::get_all())
68                 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
69
70         if (isset($_POST['filterType']) && $_POST['filterType'] != reserved_words::get_all())
71         {
72                 if ($_POST['filterType'] == '1' || $_POST['filterType'] == '2') 
73                 {
74                         $sql .= " AND ".TB_PREF."debtor_trans.type = 10 ";
75                 } 
76                 elseif ($_POST['filterType'] == '3') 
77                 {
78                         $sql .= " AND ".TB_PREF."debtor_trans.type = " . systypes::cust_payment();
79                 } 
80                 elseif ($_POST['filterType'] == '4') 
81                 {
82                         $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
83                 }
84
85         if ($_POST['filterType'] == '2') 
86         {
87                 $today =  date2sql(Today());
88                 $sql .= " AND ".TB_PREF."debtor_trans.due_date < '$today'
89                                 AND (round(abs(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount) - ".TB_PREF."debtor_trans.alloc,6) > 0) ";
90         }
91         }
92
93         if (!check_value('showSettled')) 
94         {
95                 $sql .= " AND (round(abs(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount) - ".TB_PREF."debtor_trans.alloc,6) != 0) ";
96         }
97
98     $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date";
99
100     return db_query($sql,"No transactions were returned");
101 }
102
103 //------------------------------------------------------------------------------------------------
104
105 $result = get_transactions();
106
107 if (db_num_rows($result) == 0)
108 {
109         display_note(_("The selected customer has no transactions for the given dates."), 1, 1);
110         end_page();
111         exit;
112 }
113
114 //------------------------------------------------------------------------------------------------
115
116 start_table("$table_style width='80%'");
117
118 if ($_POST['customer_id'] == reserved_words::get_all())
119         $th = array(_("Type"), _("Number"), _("Reference"), _("Order"), _("Date"), _("Due Date"),
120                 _("Customer"), _("Currency"), _("Debit"), _("Credit"), _("Allocated"), _("Balance"), "");
121 else
122         $th = array(_("Type"), _("Number"), _("Reference"), _("Order"), _("Date"), _("Due Date"),
123                 _("Debit"), _("Credit"), _("Allocated"), _("Balance"), "");
124
125 table_header($th);
126
127
128 $j = 1;
129 $k = 0; //row colour counter
130 $over_due = false;
131 while ($myrow = db_fetch($result)) 
132 {
133
134         if ($myrow['OverDue'] == 1 && (abs($myrow["TotalAmount"]) - $myrow["Allocated"] != 0))
135         {
136                 start_row("class='overduebg'");
137                 $over_due = true;
138         } 
139         else
140                 alt_table_row_color($k);
141
142         $date = sql2date($myrow["tran_date"]);
143
144         if ($myrow["order_"] > 0)
145                 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
146         else
147                 $preview_order_str = "";
148
149         $allocations_str = "";
150
151         $allocations = "<a href='$path_to_root/sales/allocations/customer_allocate.php?trans_no=" . $myrow["trans_no"] ."&trans_type=" . $myrow["type"] ."'>" . _("Allocation") . "</a>";
152
153         $due_date_str = "";
154
155         if ($myrow["type"] == 10)
156                 $due_date_str = sql2date($myrow["due_date"]);
157         elseif ($myrow["type"] == 11 && $myrow['TotalAmount'] < 0) 
158         {
159                 /*its a credit note which could have an allocation */
160                 $allocations_str = $allocations;
161
162         } 
163         elseif ($myrow["type"] == systypes::cust_payment() && 
164                 ($myrow['TotalAmount'] + $myrow['Allocated']) < 0) 
165         {
166                 /*its a receipt  which could have an allocation*/
167                 $allocations_str = $allocations;
168
169         } 
170         elseif ($myrow["type"] == systypes::cust_payment() && $myrow['TotalAmount'] > 0) 
171         {
172                 /*its a negative receipt */
173         }
174
175         label_cell(systypes::name($myrow["type"]));
176
177         label_cell(get_customer_trans_view_str($myrow["type"], $myrow["trans_no"]));
178         label_cell($myrow["reference"]);
179         label_cell($preview_order_str);
180         label_cell(sql2date($myrow["tran_date"]), "nowrap");
181         label_cell($due_date_str, "nowrap");
182         if ($_POST['customer_id'] == reserved_words::get_all())
183         {
184                 label_cell($myrow["CustName"]);
185                 label_cell($myrow["CustCurrCode"]);
186         }       
187         display_debit_or_credit_cells($myrow["TotalAmount"]);
188         amount_cell(abs($myrow["Allocated"]));
189         amount_cell(abs($myrow["TotalAmount"]) - $myrow["Allocated"]);
190         label_cell($allocations_str);
191
192
193         end_row();
194
195         $j++;
196         If ($j == 12)
197         {
198                 $j = 1;
199                 table_header($th);
200         }
201 //end of page full new headings if
202 }
203 //end of while loop
204
205 end_table(1);
206 if ($over_due)
207         display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
208         
209 end_page();
210
211 ?>