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