forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSalesTopItemsInquiry.php
268 lines (241 loc) · 8.83 KB
/
SalesTopItemsInquiry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
include('includes/session.php');
$Title = _('Top Sales Inquiry');
include('includes/header.php');
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/transactions.png" title="' . _('Sales Inquiry') . '" alt="" />' . ' ' . _('Top Sales Items Inquiry') . '</p>';
echo '<div class="page_help_text">' . _('Select the parameters for the report') . '</div><br />';
if (!isset($_POST['DateRange'])){
/* then assume report is for This Month - maybe wrong to do this but hey better than reporting an error?*/
$_POST['DateRange']='ThisMonth';
}
echo '<form id="form1" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post">';
echo '<div>';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<table cellpadding="2" class="selection">
<tr><td valign="top">
<table>';
echo '<tr>
<th colspan="2" class="centre">' . _('Date Selection') . '</th>
</tr>
<tr>
<td>' . _('Custom Range') . ':</td>
<td><input type="radio" name="DateRange" value="Custom" ';
if ($_POST['DateRange']=='Custom'){
echo 'checked="checked"';
}
echo ' onchange="ReloadForm(form1.ShowSales)" /></td>
</tr>
<tr>
<td>' . _('This Week') . ':</td>
<td><input type="radio" name="DateRange" value="ThisWeek" ';
if ($_POST['DateRange']=='ThisWeek'){
echo 'checked="checked"';
}
echo ' onchange="ReloadForm(form1.ShowSales)" /></td>
</tr>
<tr>
<td>' . _('This Month') . ':</td>
<td><input type="radio" name="DateRange" value="ThisMonth" ';
if ($_POST['DateRange']=='ThisMonth'){
echo 'checked="checked"';
}
echo ' onchange="ReloadForm(form1.ShowSales)" /></td>
</tr>
<tr>
<td>' . _('This Quarter') . ':</td>
<td><input type="radio" name="DateRange" value="ThisQuarter" ';
if ($_POST['DateRange']=='ThisQuarter'){
echo 'checked="checked"';
}
echo ' onchange="ReloadForm(form1.ShowSales)" /></td>
</tr>';
if ($_POST['DateRange']=='Custom'){
if (!isset($_POST['FromDate'])){
unset($_POST['ShowSales']);
$_POST['FromDate'] = Date($_SESSION['DefaultDateFormat'],mktime(1,1,1,Date('m')-12,Date('d')+1,Date('Y')));
$_POST['ToDate'] = Date($_SESSION['DefaultDateFormat']);
}
echo '<tr>
<td>' . _('Date From') . ':</td>
<td><input type="text" class="date" name="FromDate" maxlength="10" size="11" value="' . $_POST['FromDate'] . '" /></td>
</tr>';
echo '<tr>
<td>' . _('Date To') . ':</td>
<td><input type="text" class="date" name="ToDate" maxlength="10" size="11" value="' . $_POST['ToDate'] . '" /></td>
</tr>';
}
echo '</table></td>
<td valign="top">
<table>'; //new sub table to set parameters for order of display
if (!isset($_POST['OrderBy'])){ //default to order by net sales
$_POST['OrderBy']='NetSales';
}
echo '<tr><th colspan="2" class="centre">' . _('Display') . '</th>
</tr>
<tr>
<td>' . _('Order By Net Sales') . ':</td>
<td><input type="radio" name="OrderBy" value="NetSales" ';
if ($_POST['OrderBy']=='NetSales'){
echo 'checked="checked"';
}
echo ' /></td>
</tr>
<tr>
<td>' . _('Order By Quantity') . ':</td>
<td><input type="radio" name="OrderBy" value="Quantity" ';
if ($_POST['OrderBy']=='Quantity'){
echo 'checked="checked"';
}
if (!isset($_POST['NoToDisplay'])){
$_POST['NoToDisplay']=20;
}
echo ' /></td>
</tr>
<tr>
<td>' . _('Number to Display') . ':</td>
<td><input type="text" class="number" name="NoToDisplay" size="4" maxlength="4" value="' . $_POST['NoToDisplay'] .'" /></td>
</tr>
</table>
</td></tr>
</table>';
echo '<br /><div class="centre"><input tabindex="4" type="submit" name="ShowSales" value="' . _('Show Sales') . '" />';
echo '</div>';
echo '<br />';
echo '</div>
</form>';
if (isset($_POST['ShowSales'])){
$InputError=0; //assume no input errors now test for errors
if ($_POST['DateRange']=='Custom'){
if (!Is_Date($_POST['FromDate'])){
$InputError = 1;
prnMsg(_('The date entered for the from date is not in the appropriate format. Dates must be entered in the format') . ' ' . $_SESSION['DefaultDateFormat'], 'error');
}
if (!Is_Date($_POST['ToDate'])){
$InputError = 1;
prnMsg(_('The date entered for the to date is not in the appropriate format. Dates must be entered in the format') . ' ' . $_SESSION['DefaultDateFormat'], 'error');
}
if (Date1GreaterThanDate2($_POST['FromDate'],$_POST['ToDate'])){
$InputError = 1;
prnMsg(_('The from date is expected to be a date prior to the to date. Please review the selected date range'),'error');
}
}
switch ($_POST['DateRange']) {
case 'ThisWeek':
$FromDate = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y')));
$ToDate = date('Y-m-d');
break;
case 'ThisMonth':
$FromDate = date('Y-m-d',mktime(0,0,0,date('m'),1,date('Y')));
$ToDate = date('Y-m-d');
break;
case 'ThisQuarter':
switch (date('m')) {
case 1:
case 2:
case 3:
$QuarterStartMonth=1;
break;
case 4:
case 5:
case 6:
$QuarterStartMonth=4;
break;
case 7:
case 8:
case 9:
$QuarterStartMonth=7;
break;
default:
$QuarterStartMonth=10;
}
$FromDate = date('Y-m-d',mktime(0,0,0,$QuarterStartMonth,1,date('Y')));
$ToDate = date('Y-m-d');
break;
case 'Custom':
$FromDate = FormatDateForSQL($_POST['FromDate']);
$ToDate = FormatDateForSQL($_POST['ToDate']);
}
$sql = "SELECT stockmaster.stockid,
stockmaster.description,
stockcategory.categorydescription,
SUM(CASE WHEN stockmoves.type=10
OR stockmoves.type=11 THEN
-qty
ELSE 0 END) as salesquantity,
SUM(CASE WHEN stockmoves.type=10 THEN
price*(1-discountpercent)* -qty
ELSE 0 END) as salesvalue,
SUM(CASE WHEN stockmoves.type=11 THEN
price*(1-discountpercent)* (-qty)
ELSE 0 END) as returnvalue,
SUM(CASE WHEN stockmoves.type=11
OR stockmoves.type=10 THEN
price*(1-discountpercent)* (-qty)
ELSE 0 END) as netsalesvalue,
SUM((standardcost * -qty)) as cost
FROM stockmoves INNER JOIN stockmaster
ON stockmoves.stockid=stockmaster.stockid
INNER JOIN stockcategory
ON stockmaster.categoryid=stockcategory.categoryid
WHERE (stockmoves.type=10 or stockmoves.type=11)
AND show_on_inv_crds =1
AND trandate>='" . $FromDate . "'
AND trandate<='" . $ToDate . "'
GROUP BY stockmaster.stockid,
stockmaster.description,
stockcategory.categorydescription ";
if ($_POST['OrderBy']=='NetSales'){
$sql .= " ORDER BY netsalesvalue DESC ";
} else {
$sql .= " ORDER BY salesquantity DESC ";
}
if (is_numeric($_POST['NoToDisplay'])){
if ($_POST['NoToDisplay'] > 0){
$sql .= " LIMIT " . $_POST['NoToDisplay'];
}
}
$ErrMsg = _('The sales data could not be retrieved because') . ' - ' . DB_error_msg();
$SalesResult = DB_query($sql,$ErrMsg);
echo '<table cellpadding="2" class="selection">';
echo'<tr>
<th>' . _('Rank') . '</th>
<th>' . _('Item') . '</th>
<th>' . _('Category') . '</th>
<th>' . _('Sales Value') . '</th>
<th>' . _('Refunds') . '</th>
<th>' . _('Net Sales') . '</th>
<th>' . _('Sales') . '<br />' . _('Quantity') . '</th>
</tr>';
$CumulativeTotalSales = 0;
$CumulativeTotalRefunds = 0;
$CumulativeTotalNetSales = 0;
$CumulativeTotalQuantity = 0;
$i=1;
while ($SalesRow=DB_fetch_array($SalesResult)) {
echo '<tr class="striped_row">
<td>' . $i . '</td>
<td>' . $SalesRow['stockid'] . ' - ' . $SalesRow['description'] . '</td>
<td>' . $SalesRow['categorydescription'] . '</td>
<td class="number">' . locale_number_format($SalesRow['salesvalue'],$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td class="number">' . locale_number_format($SalesRow['returnvalue'],$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td class="number">' . locale_number_format($SalesRow['netsalesvalue'],$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td class="number">' . locale_number_format($SalesRow['salesquantity'],'Variable') . '</td>
</tr>';
$i++;
$CumulativeTotalSales += $SalesRow['salesvalue'];
$CumulativeTotalRefunds += $SalesRow['returnvalue'];
$CumulativeTotalNetSales += ($SalesRow['salesvalue']+$SalesRow['returnvalue']);
$CumulativeTotalQuantity += $SalesRow['salesquantity'];
} //loop around category sales for the period
echo '<tr class="striped_row"><td colspan="8"><hr /></td></tr>';
echo '<tr class="striped_row">';
echo '<td class="number" colspan="3">' . _('GRAND Total') . '</td>
<td class="number">' . locale_number_format($CumulativeTotalSales,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td class="number">' . locale_number_format($CumulativeTotalRefunds,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td class="number">' . locale_number_format($CumulativeTotalNetSales,$_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td class="number">' . locale_number_format($CumulativeTotalQuantity,'Variable') . '</td>
</tr>';
echo '</table>';
} //end of if user hit show sales
include('includes/footer.php');
?>