forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPricesByCost.php
301 lines (284 loc) · 13.7 KB
/
PricesByCost.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
include ('includes/session.php');
$Title = _('Update of Prices By A Multiple Of Cost');
include ('includes/header.php');
echo '<p class="page_title_text"><img src="' . $RootPath . '/css/' . $Theme . '/images/inventory.png" title="' . _('Inventory') . '" alt="" />' . ' ' . _('Update Price By Cost') . '</p>';
if (isset($_POST['submit']) OR isset($_POST['update'])) {
if ($_POST['Margin'] == '') {
header('Location: PricesByCost.php');
}
if ($_POST['Comparator'] == 1) {
$Comparator = '<=';
} else {
$Comparator = '>=';
} /*end of else Comparator */
if ($_POST['StockCat'] != 'all') {
$Category = " AND stockmaster.categoryid = '" . $_POST['StockCat'] . "'";
} else {
$Category ='';
}/*end of else StockCat */
$sql = "SELECT stockmaster.stockid,
stockmaster.description,
prices.debtorno,
prices.branchcode,
(stockmaster.materialcost + stockmaster.labourcost + stockmaster.overheadcost) as cost,
prices.price as price,
prices.debtorno AS customer,
prices.branchcode AS branch,
prices.startdate,
prices.enddate,
currencies.decimalplaces,
currencies.rate
FROM stockmaster INNER JOIN prices
ON stockmaster.stockid=prices.stockid
INNER JOIN currencies
ON prices.currabrev=currencies.currabrev
WHERE stockmaster.discontinued = 0
" . $Category . "
AND prices.price" . $Comparator . "(stockmaster.materialcost + stockmaster.labourcost + stockmaster.overheadcost) * '" . filter_number_format($_POST['Margin']) . "'
AND prices.typeabbrev ='" . $_POST['SalesType'] . "'
AND prices.currabrev ='" . $_POST['CurrCode'] . "'
AND (prices.enddate>='" . Date('Y-m-d') . "' OR prices.enddate='0000-00-00')";
$result = DB_query($sql);
$numrow = DB_num_rows($result);
if ($_POST['submit'] == 'Update') {
//Update Prices
$PriceCounter =0;
while ($myrow = DB_fetch_array($result)) {
/*The logic here goes like this:
* 1. If the price at the same start and end date already exists then do nowt!!
* 2. If not then check if a price with the start date of today already exists - then we should be updating it
* 3. If not either of the above then insert the new price
*/
$SQLTestExists = "SELECT price FROM prices
WHERE stockid = '" . $_POST['StockID_' . $PriceCounter] . "'
AND prices.typeabbrev ='" . $_POST['SalesType'] . "'
AND prices.currabrev ='" . $_POST['CurrCode'] . "'
AND prices.debtorno ='" . $_POST['DebtorNo_' . $PriceCounter] . "'
AND prices.branchcode ='" . $_POST['BranchCode_' . $PriceCounter] . "'
AND prices.startdate ='" . $_POST['StartDate_' . $PriceCounter] . "'
AND prices.enddate ='" . $_POST['EndDate_' . $PriceCounter] . "'
AND prices.price ='" . filter_number_format($_POST['Price_' . $PriceCounter]) . "'";
$TestExistsResult = DB_query($SQLTestExists);
if (DB_num_rows($TestExistsResult)==0){ //the price doesn't currently exist
//now check to see if a new price has already been created from start date of today
$SQLTestExists = "SELECT price FROM prices
WHERE stockid = '" . $_POST['StockID_' . $PriceCounter] . "'
AND prices.typeabbrev ='" . $_POST['SalesType'] . "'
AND prices.currabrev ='" . $_POST['CurrCode'] . "'
AND prices.debtorno ='" . $_POST['DebtorNo_' . $PriceCounter] . "'
AND prices.branchcode ='" . $_POST['BranchCode_' . $PriceCounter] . "'
AND prices.startdate ='" . date('Y-m-d') . "'";
$TestExistsResult = DB_query($SQLTestExists);
if (DB_num_rows($TestExistsResult)==1){
//then we are updating
$SQLUpdate = "UPDATE prices SET price = '" . filter_number_format($_POST['Price_' . $PriceCounter]) . "'
WHERE stockid = '" . $_POST['StockID_' . $PriceCounter] . "'
AND prices.typeabbrev ='" . $_POST['SalesType'] . "'
AND prices.currabrev ='" . $_POST['CurrCode'] . "'
AND prices.debtorno ='" . $_POST['DebtorNo_' . $PriceCounter] . "'
AND prices.branchcode ='" . $_POST['BranchCode_' . $PriceCounter] . "'
AND prices.startdate ='" . date('Y-m-d') . "'
AND prices.enddate ='" . $_POST['EndDate_' . $PriceCounter] . "'";
$ResultUpdate = DB_query($SQLUpdate);
} else { //there is not a price already starting today so need to create one
//update the old price to have an end date of yesterday too
$SQLUpdate = "UPDATE prices SET enddate = '" . FormatDateForSQL(DateAdd(Date($_SESSION['DefaultDateFormat']),'d',-1)) . "'
WHERE stockid = '" . $_POST['StockID_' . $PriceCounter] . "'
AND prices.typeabbrev ='" . $_POST['SalesType'] . "'
AND prices.currabrev ='" . $_POST['CurrCode'] . "'
AND prices.debtorno ='" . $_POST['DebtorNo_' . $PriceCounter] . "'
AND prices.branchcode ='" . $_POST['BranchCode_' . $PriceCounter] . "'
AND prices.startdate ='" . $_POST['StartDate_' . $PriceCounter] . "'
AND prices.enddate ='" . $_POST['EndDate_' . $PriceCounter] . "'";
$Result = DB_query($SQLUpdate);
//we need to add a new price from today
$SQLInsert = "INSERT INTO prices ( stockid,
price,
typeabbrev,
currabrev,
debtorno,
branchcode,
startdate
) VALUES (
'" . $_POST['StockID_' . $PriceCounter] . "',
'" . filter_number_format($_POST['Price_' . $PriceCounter]) . "',
'" . $_POST['SalesType'] . "',
'" . $_POST['CurrCode'] . "',
'" . $_POST['DebtorNo_' . $PriceCounter] . "',
'" . $_POST['BranchCode_' . $PriceCounter] . "',
'" . date('Y-m-d') . "'
)";
$ResultInsert = DB_query($SQLInsert);
}
}
$PriceCounter++;
}//end while loop
DB_free_result($result); //clear the old result
$result = DB_query($sql); //re-run the query with the updated prices
$numrow = DB_num_rows($result); // get the new number - should be the same!!
}
$sqlcat = "SELECT categorydescription
FROM stockcategory
WHERE categoryid='" . $_POST['StockCat'] . "'";
$ResultCat = DB_query($sqlcat);
$CategoryRow = DB_fetch_array($ResultCat);
$sqltype = "SELECT sales_type
FROM salestypes
WHERE typeabbrev='" . $_POST['SalesType'] . "'";
$ResultType = DB_query($sqltype);
$SalesTypeRow = DB_fetch_array($ResultType);
if (isset($CategoryRow['categorgdescription'])) {
$CategoryText = $CategoryRow['categorgdescription'] . ' ' . _('category');
} else {
$CategoryText = _('all Categories');
} /*end of else Category */
echo '<div class="page_help_text">' . _('Items in') . ' ' . $CategoryText . ' ' . _('With Prices') . ' ' . $Comparator . '' . $_POST['Margin'] . ' ' . _('times') . ' ' . _('Cost in Price List') . ' ' . $SalesTypeRow['sales_type'] . '</div><br /><br />';
if ($numrow > 0) { //the number of prices returned from the main prices query is
echo '<form action="' .htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') .'" method="post" id="update">';
echo '<div>';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo'<input type="hidden" value="' . $_POST['StockCat'] . '" name="StockCat" />
<input type="hidden" value="' . $_POST['Margin'] . '" name="Margin" />
<input type="hidden" value="' . $_POST['CurrCode'] . '" name="CurrCode" />
<input type="hidden" value="' . $_POST['Comparator'] . '" name="Comparator" />
<input type="hidden" value="' . $_POST['SalesType'] . '" name="SalesType" />';
echo '<table class="selection">
<thead>
<tr>
<th class="ascending">' . _('Code') . '</th>
<th class="ascending">' . _('Description') . '</th>
<th class="ascending">' . _('Customer') . '</th>
<th class="ascending">' . _('Branch') . '</th>
<th class="ascending">' . _('Start Date') . '</th>
<th class="ascending">' . _('End Date') . '</th>
<th class="ascending">' . _('Cost') . '</th>
<th class="ascending">' . _('GP %') . '</th>
<th class="ascending">' . _('Price Proposed') . '</th>
<th class="ascending">' . _('List Price') . '</th>
<tr>
</thead>
<tbody>';
$PriceCounter =0;
while ($myrow = DB_fetch_array($result)) {
//get cost
if ($myrow['cost'] == '') {
$Cost = 0;
} else {
$Cost = $myrow['cost'];
} /*end of else Cost */
//variables for update
echo '<input type="hidden" value="' . $myrow['stockid'] . '" name="StockID_' . $PriceCounter .'" />
<input type="hidden" value="' . $myrow['debtorno'] . '" name="DebtorNo_' . $PriceCounter .'" />
<input type="hidden" value="' . $myrow['branchcode'] . '" name="BranchCode_' . $PriceCounter .'" />
<input type="hidden" value="' . $myrow['startdate'] . '" name="StartDate_' . $PriceCounter .'" />
<input type="hidden" value="' . $myrow['enddate'] . '" name="EndDate_' . $PriceCounter .'" />';
//variable for current margin
if ($myrow['price'] != 0){
$CurrentGP = (($myrow['price']/$myrow['rate'])-$Cost)*100 / ($myrow['price']/$myrow['rate']);
} else {
$CurrentGP = 0;
}
//variable for proposed
$ProposedPrice = $Cost * filter_number_format($_POST['Margin']);
if ($myrow['enddate']=='0000-00-00'){
$EndDateDisplay = _('No End Date');
} else {
$EndDateDisplay = ConvertSQLDate($myrow['enddate']);
}
echo '<tr class="striped_row">
<td>' . $myrow['stockid'] . '</td>
<td>' . $myrow['description'] . '</td>
<td>' . $myrow['customer'] . '</td>
<td>' . $myrow['branch'] . '</td>
<td>' . ConvertSQLDate($myrow['startdate']) . '</td>
<td>' . $EndDateDisplay . '</td>
<td class="number">' . locale_number_format($Cost, $_SESSION['CompanyRecord']['decimalplaces']) . '</td>
<td class="number">' . locale_number_format($CurrentGP, 1) . '%</td>
<td class="number">' . locale_number_format($ProposedPrice, $myrow['decimalplaces']) . '</td>
<td><input type="text" class="number" name="Price_' . $PriceCounter . '" maxlength="14" size="10" value="' . locale_number_format($myrow['price'],$myrow['decimalplaces']) . '" /></td>
</tr> ';
$PriceCounter++;
} //end of looping
echo '</tbody>
<tfoot>
<tr>
<td class="number" colspan="4"><input type="submit" name="submit" value="' . _('Update') . '" onclick="return confirm(\'' . _('If the prices above do not have a commencement date as today, this will create new prices with commencement date of today at the entered figures and update the existing prices with historical start dates to have an end date of yesterday. Are You Sure?') . '\');" /></td>
<td class="text" colspan="3"><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '"><input type="submit" value="' . _('Back') . '" /></a></td>
</tr>
</tfoot>
</table>
</div>
</form>';
} else {
prnMsg(_('There were no prices meeting the criteria specified to review'),'info');
echo '<br /><div class="centre"><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">' . _('Back') . '<a/></div>';
}
} else { /*The option to submit was not hit so display form */
echo '<div class="page_help_text">' . _('Prices can be displayed based on their relation to cost') . '</div><br />';
echo '<br />
<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post">';
echo '<div>';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<table class="selection">';
$SQL = "SELECT categoryid, categorydescription
FROM stockcategory
ORDER BY categorydescription";
$result1 = DB_query($SQL);
echo '<tr>
<td>' . _('Category') . ':</td>
<td><select name="StockCat">';
echo '<option value="all">' . _('All Categories') . '</option>';
while ($myrow1 = DB_fetch_array($result1)) {
echo '<option value="' . $myrow1['categoryid'] . '">' . $myrow1['categorydescription'] . '</option>';
}
echo '</select></td></tr>';
echo '<tr>
<td>' . _('Price') . '
<select name="Comparator">
<option value="1">' . _('Less than or equal to') . '</option>
<option value="2">' . _('Greater than or equal to') . '</option>';
if ($_SESSION['WeightedAverageCosting']==1) {
echo '</select>' . ' '. _('Average Cost') . ' x </td>';
} else {
echo '</select>' . ' '. _('Standard Cost') . ' x </td>';
}
if (!isset($_POST['Margin'])){
$_POST['Margin']=1;
}
echo '<td><input type="text" class="number" name="Margin" maxlength="8" size="8" value="' .$_POST['Margin'] . '" /></td>
</tr>';
$result = DB_query("SELECT typeabbrev, sales_type FROM salestypes");
echo '<tr><td>' . _('Sales Type') . '/' . _('Price List') . ':</td>
<td><select name="SalesType">';
while ($myrow = DB_fetch_array($result)) {
if ($_POST['SalesType'] == $myrow['typeabbrev']) {
echo '<option selected="selected" value="' . $myrow['typeabbrev'] . '">' . $myrow['sales_type'] . '</option>';
} else {
echo '<option value="' . $myrow['typeabbrev'] . '">' . $myrow['sales_type'] . '</option>';
}
} //end while loop
DB_data_seek($result, 0);
$result = DB_query("SELECT currency, currabrev FROM currencies");
echo '</select></td>
</tr>
<tr>
<td>' . _('Currency') . ':</td>
<td><select name="CurrCode">';
while ($myrow = DB_fetch_array($result)) {
if (isset($_POST['CurrCode']) and $_POST['CurrCode'] == $myrow['currabrev']) {
echo '<option selected="selected" value="' . $myrow['currabrev'] . '">' . $myrow['currency'] . '</option>';
} else {
echo '<option value="' . $myrow['currabrev'] . '">' . $myrow['currency'] . '</option>';
}
} //end while loop
DB_data_seek($result, 0);
echo '</select></td></tr>';
echo '</table>
<br />
<div class="centre"><input type="submit" name="submit" value="' . _('Submit') . '" /></div>
</div>
</form>';
} /*end of else not submit */
include ('includes/footer.php');
?>