c20262d5acbb17d3a39e14cf62dfe44d953c7123
[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 + "
59         .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount)
60                 AS TotalAmount,
61                 ".TB_PREF."debtor_trans.alloc AS Allocated,
62                 ((".TB_PREF."debtor_trans.type = 10)
63                 AND ".TB_PREF."debtor_trans.due_date < '" . date2sql(Today()) . "') AS OverDue
64         FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
65         WHERE ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
66                         AND (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
67                         .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount != 0)
68                 AND ".TB_PREF."debtor_trans.tran_date >= '$data_after'
69                 AND ".TB_PREF."debtor_trans.tran_date <= '$date_to'";
70
71         if ($_POST['customer_id'] != reserved_words::get_all())
72                 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
73
74         if (isset($_POST['filterType']) && $_POST['filterType'] != reserved_words::get_all())
75         {
76                 if ($_POST['filterType'] == '1' || $_POST['filterType'] == '2')
77                 {
78                         $sql .= " AND ".TB_PREF."debtor_trans.type = 10 ";
79                 }
80                 elseif ($_POST['filterType'] == '3')
81                 {
82                         $sql .= " AND ".TB_PREF."debtor_trans.type = " . systypes::cust_payment();
83                 }
84                 elseif ($_POST['filterType'] == '4')
85                 {
86                         $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
87                 }
88
89         if ($_POST['filterType'] == '2')
90         {
91                 $today =  date2sql(Today());
92                 $sql .= " AND ".TB_PREF."debtor_trans.due_date < '$today'
93                                 AND (round(abs(".TB_PREF."debtor_trans.ov_amount + "
94                                 .TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + "
95                                 .TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount) - ".TB_PREF."debtor_trans.alloc,6) > 0) ";
96         }
97         }else
98         {
99             $sql .= " AND ".TB_PREF."debtor_trans.type != 13 ";
100         }
101
102
103         if (!check_value('showSettled'))
104         {
105                 $sql .= " AND (round(abs(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
106                 .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + "
107                 .TB_PREF."debtor_trans.ov_discount) - ".TB_PREF."debtor_trans.alloc,6) != 0) ";
108         }
109
110     $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date";
111
112     return db_query($sql,"No transactions were returned");
113 }
114
115 //------------------------------------------------------------------------------------------------
116
117 $result = get_transactions();
118
119 if (db_num_rows($result) == 0)
120 {
121         display_note(_("The selected customer has no transactions for the given dates."), 1, 1);
122         end_page();
123         exit;
124 }
125
126 //------------------------------------------------------------------------------------------------
127
128 start_table("$table_style width='80%'");
129
130 if ($_POST['customer_id'] == reserved_words::get_all())
131         $th = array(_("Type"), _("Number"), _("Reference"), _("Order"), _("Date"), _("Due Date"),
132                 _("Customer"), _("Currency"), _("Debit"), _("Credit"), _("Allocated"), _("Balance"), "");
133 else
134         $th = array(_("Type"), _("Number"), _("Reference"), _("Order"), _("Date"), _("Due Date"),
135                 _("Debit"), _("Credit"), _("Allocated"), _("Balance"), "");
136
137 table_header($th);
138
139
140 $j = 1;
141 $k = 0; //row colour counter
142 $over_due = false;
143 while ($myrow = db_fetch($result))
144 {
145
146         if ($myrow['OverDue'] == 1 && (abs($myrow["TotalAmount"]) - $myrow["Allocated"] != 0))
147         {
148                 start_row("class='overduebg'");
149                 $over_due = true;
150         }
151         else
152                 alt_table_row_color($k);
153
154         $date = sql2date($myrow["tran_date"]);
155
156         if ($myrow["order_"] > 0)
157                 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
158         else
159                 $preview_order_str = "";
160
161         $allocations_str = "";
162
163         $allocations = "<a href='$path_to_root/sales/allocations/customer_allocate.php?trans_no=" . $myrow["trans_no"] ."&trans_type=" . $myrow["type"] ."'>" . _("Allocation") . "</a>";
164
165         $due_date_str = "";
166
167         if ($myrow["type"] == 10)
168                 $due_date_str = sql2date($myrow["due_date"]);
169         elseif ($myrow["type"] == 11 && $myrow['TotalAmount'] > 0)
170         {
171                 /*its a credit note which could have an allocation */
172                 $allocations_str = $allocations;
173
174         }
175         elseif (($myrow["type"] == systypes::cust_payment() || $myrow["type"] == systypes::bank_deposit()) &&
176                 ($myrow['TotalAmount'] - $myrow['Allocated']) > 0)
177         {
178                 /*its a receipt  which could have an allocation*/
179                 $allocations_str = $allocations;
180
181         }
182         elseif ($myrow["type"] == systypes::cust_payment() && $myrow['TotalAmount'] < 0)
183         {
184                 /*its a negative receipt */
185         }
186
187         label_cell(systypes::name($myrow["type"]));
188         label_cell(get_trans_view_str($myrow["type"], $myrow["trans_no"]));
189         label_cell($myrow["reference"]);
190         label_cell($preview_order_str);
191         label_cell(sql2date($myrow["tran_date"]), "nowrap");
192         label_cell($due_date_str, "nowrap");
193         if ($_POST['customer_id'] == reserved_words::get_all())
194         {
195                 label_cell($myrow["CustName"]);
196                 label_cell($myrow["CustCurrCode"]);
197         }
198         display_debit_or_credit_cells(
199             $myrow['type']==11 || $myrow['type']==12 || $myrow['type']==2 ?
200                 -$myrow["TotalAmount"] : $myrow["TotalAmount"]);
201         amount_cell(abs($myrow["Allocated"]));
202         amount_cell(abs($myrow["TotalAmount"]) - $myrow["Allocated"]);
203         label_cell($allocations_str);
204
205
206         end_row();
207
208         $j++;
209         If ($j == 12)
210         {
211                 $j = 1;
212                 table_header($th);
213         }
214 //end of page full new headings if
215 }
216 //end of while loop
217
218 end_table(1);
219 if ($over_due)
220         display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
221
222 end_page();
223
224 ?>