forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrices_Customer.php
466 lines (400 loc) · 16.3 KB
/
Prices_Customer.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php
include('includes/session.php');
$result = DB_query("SELECT debtorsmaster.name,
debtorsmaster.currcode,
debtorsmaster.salestype,
currencies.decimalplaces AS currdecimalplaces
FROM debtorsmaster INNER JOIN currencies
ON debtorsmaster.currcode=currencies.currabrev
WHERE debtorsmaster.debtorno='" . $_SESSION['CustomerID'] . "'");
$myrow = DB_fetch_array($result);
$Title = _('Special Prices for') . ' '. htmlspecialchars($myrow['name'], ENT_QUOTES, 'UTF-8');
include('includes/header.php');
if (isset($_GET['Item'])){
$Item = $_GET['Item'];
}elseif (isset($_POST['Item'])){
$Item = $_POST['Item'];
}
if (!isset($Item) OR !isset($_SESSION['CustomerID']) OR $_SESSION['CustomerID']==''){
prnMsg( _('A customer must be selected from the customer selection screen') . ', '
. _('then an item must be selected before this page is called') . '. '
. _('The product selection page should call this page with a valid product code'),'info');
echo '<br />';
include('includes/footer.php');
exit;
}
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/maintenance.png" title="' . _('Search') .
'" alt="" />' . _('Special Customer Prices') . '</p><br />';
echo '<b>' . htmlspecialchars($myrow['name'], ENT_QUOTES, 'UTF-8') . ' ' . _('in') . ' ' . $myrow['currcode'] . '<br />' . ' ' . _('for') . ' ';
$CurrCode = $myrow['currcode'];
$SalesType = $myrow['salestype'];
$CurrDecimalPlaces = $myrow['currdecimalplaces'];
$result = DB_query("SELECT stockmaster.description,
stockmaster.mbflag
FROM stockmaster
WHERE stockmaster.stockid='" . $Item . "'");
$myrow = DB_fetch_row($result);
if (DB_num_rows($result)==0){
prnMsg( _('The part code entered does not exist in the database') . '. ' . _('Only valid parts can have prices entered against them'),'error');
$InputError=1;
}
if ($myrow[1]=='K'){
prnMsg(_('The part selected is a kit set item') .', ' . _('these items explode into their components when selected on an order') . ', ' . _('prices must be set up for the components and no price can be set for the whole kit'),'error');
exit;
}
echo $Item . ' - ' . $myrow[0] . '</b><br />';
if (isset($_POST['submit'])) {
//initialise no input errors assumed initially before we test
$InputError = 0;
/* actions to take once the user has clicked the submit button
ie the page has called itself with some user input */
//first off validate inputs sensible
if (!is_numeric(filter_number_format($_POST['Price'])) OR $_POST['Price']=='') {
$InputError = 1;
$msg = _('The price entered must be numeric');
}
if ($_POST['Branch'] !=''){
$sql = "SELECT custbranch.branchcode
FROM custbranch
WHERE custbranch.debtorno='" . $_SESSION['CustomerID'] . "'
AND custbranch.branchcode='" . $_POST['Branch'] . "'";
$result = DB_query($sql);
if (DB_num_rows($result) ==0){
$InputError =1;
$msg = _('The branch code entered is not currently defined');
}
}
if (! Is_Date($_POST['StartDate'])){
$InputError =1;
$msg = _('The date this price is to take effect from must be entered in the format') . ' ' . $_SESSION['DefaultDateFormat'];
}
if ($_POST['EndDate']!='0000-00-00'){
if (! Is_Date($_POST['EndDate']) AND $_POST['EndDate']!=''){ //EndDate can also be blank for default prices
$InputError =1;
$msg = _('The date this price is be in effect to must be entered in the format') . ' ' . $_SESSION['DefaultDateFormat'];
}
if (Date1GreaterThanDate2($_POST['StartDate'],$_POST['EndDate']) AND $_POST['EndDate']!=''){
$InputError =1;
$msg = _('The end date is expected to be after the start date, enter an end date after the start date for this price');
}
if (Date1GreaterThanDate2(Date($_SESSION['DefaultDateFormat']),$_POST['EndDate']) AND $_POST['EndDate']!=''){
$InputError =1;
$msg = _('The end date is expected to be after today. There is no point entering a new price where the effective date is before today!');
}
if (trim($_POST['EndDate'])==''){
$_POST['EndDate'] = '0000-00-00';
}
}
if ((isset($_POST['Editing']) AND $_POST['Editing']=='Yes') AND mb_strlen($Item)>1 AND $InputError !=1) {
//editing an existing price
$sql = "UPDATE prices SET typeabbrev='" . $SalesType . "',
currabrev='" . $CurrCode . "',
price='" . filter_number_format($_POST['Price']) . "',
branchcode='" . $_POST['Branch'] . "',
startdate='" . FormatDateForSQL($_POST['StartDate']) . "',
enddate='" . FormatDateForSQL($_POST['EndDate']) . "'
WHERE prices.stockid='" . $Item . "'
AND prices.typeabbrev='" . $SalesType . "'
AND prices.currabrev='" . $CurrCode . "'
AND prices.startdate='" . $_POST['OldStartDate'] . "'
AND prices.enddate='" . $_POST['OldEndDate'] . "'
AND prices.debtorno='" . $_SESSION['CustomerID'] . "'";
$msg = _('Price Updated');
} elseif ($InputError !=1) {
/*Selected price is null cos no item selected on first time round so must be adding a record must be submitting new entries in the new price form */
$sql = "INSERT INTO prices (stockid,
typeabbrev,
currabrev,
debtorno,
price,
branchcode,
startdate,
enddate)
VALUES ('".$Item."',
'".$SalesType."',
'".$CurrCode."',
'" . $_SESSION['CustomerID'] . "',
'" . filter_number_format($_POST['Price']) . "',
'" . $_POST['Branch'] . "',
'" . FormatDateForSQL($_POST['StartDate']) . "',
'" . FormatDateForSQL($_POST['EndDate']) . "'
)";
$msg = _('Price added') . '.';
}
//run the SQL from either of the above possibilites
if ($InputError!=1){
$result = DB_query($sql,'','',false,false);
if (DB_error_no()!=0){
If ($msg==_('Price Updated')){
$msg = _('The price could not be updated because') . ' - ' . DB_error_msg();
} else {
$msg = _('The price could not be added because') . ' - ' . DB_error_msg();
}
}else {
ReSequenceEffectiveDates ($Item, $SalesType, $CurrCode, $_SESSION['CustomerID']);
unset($_POST['EndDate']);
unset($_POST['StartDate']);
unset($_POST['Price']);
}
}
prnMsg($msg);
} elseif (isset($_GET['delete'])) {
//the link to delete a selected record was clicked instead of the submit button
$sql="DELETE FROM prices
WHERE prices.stockid = '". $Item ."'
AND prices.typeabbrev='". $SalesType ."'
AND prices.currabrev ='". $CurrCode ."'
AND prices.debtorno='" . $_SESSION['CustomerID'] . "'
AND prices.branchcode='" . $_GET['Branch'] . "'
AND prices.startdate='" . $_GET['StartDate'] . "'
AND prices.enddate='" . $_GET['EndDate'] . "'";
$result = DB_query($sql);
prnMsg( _('This price has been deleted') . '!','success');
}
//Always do this stuff
//Show the normal prices in the currency of this customer
$sql = "SELECT prices.price,
prices.currabrev,
prices.typeabbrev,
prices.startdate,
prices.enddate
FROM prices
WHERE prices.stockid='" . $Item . "'
AND prices.typeabbrev='". $SalesType ."'
AND prices.currabrev ='". $CurrCode ."'
AND prices.debtorno=''
ORDER BY currabrev,
typeabbrev,
startdate";
$ErrMsg = _('Could not retrieve the normal prices set up because');
$DbgMsg = _('The SQL used to retrieve these records was');
$result = DB_query($sql,$ErrMsg,$DbgMsg);
echo '<table><tr><td valign="top">';
echo '<table class="selection">';
if (DB_num_rows($result) == 0) {
echo '<tr><td>' . _('There are no default prices set up for this part') . '</td></tr>';
} else {
echo '<tr><th>' . _('Normal Price') . '</th></tr>';
while ($myrow = DB_fetch_array($result)) {
if ($myrow['enddate']=='0000-00-00'){
$EndDateDisplay = _('No End Date');
} else {
$EndDateDisplay = ConvertSQLDate($myrow['enddate']);
}
printf('<tr class="striped_row">
<td class="number">%s</td>
<td class="date">%s</td>
<td class="date">%s</td>
</tr>',
locale_number_format($myrow['price'],$CurrDecimalPlaces),
ConvertSQLDate($myrow['startdate']),
$EndDateDisplay);
}
}
echo '</table></td><td valign="top">';
//now get the prices for the customer selected
$sql = "SELECT prices.price,
prices.branchcode,
custbranch.brname,
prices.startdate,
prices.enddate
FROM prices LEFT JOIN custbranch
ON prices.branchcode= custbranch.branchcode
WHERE prices.typeabbrev = '".$SalesType."'
AND prices.stockid='".$Item."'
AND prices.debtorno='" . $_SESSION['CustomerID'] . "'
AND prices.currabrev='".$CurrCode."'
AND (custbranch.debtorno='" . $_SESSION['CustomerID'] . "' OR
custbranch.debtorno IS NULL)
ORDER BY prices.branchcode,
prices.startdate";
$ErrMsg = _('Could not retrieve the special prices set up because');
$DbgMsg = _('The SQL used to retrieve these records was');
$result = DB_query($sql,$ErrMsg,$DbgMsg);
echo '<table class="selection">';
if (DB_num_rows($result) == 0) {
echo '<tr><td>' . _('There are no special prices set up for this part') . '</td></tr>';
} else {
/*THERE IS ALREADY A spl price setup */
echo '<tr>
<th>' . _('Special Price') . '</th>
<th>' . _('Branch') . '</th>
</tr>';
while ($myrow = DB_fetch_array($result)) {
if ($myrow['branchcode']==''){
$Branch = _('All Branches');
} else {
$Branch = $myrow['brname'];
}
if ($myrow['enddate']=='0000-00-00'){
$EndDateDisplay = _('No End Date');
} else {
$EndDateDisplay = ConvertSQLDate($myrow['enddate']);
}
echo '<tr style="background-color:#CCCCCC">
<td class="number">' . locale_number_format($myrow['price'],$CurrDecimalPlaces) . '</td>
<td>' . $Branch . '</td>
<td>' . $myrow['units'] . '</td>
<td class="number">' . $myrow['conversionfactor'] . '</td>
<td>' . ConvertSQLDate($myrow['startdate']) . '</td>
<td>' . $EndDateDisplay . '</td>
<td><a href="'.htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8').'?Item='.$Item.'&Price='.$myrow['price'].'&Branch='.$myrow['branchcode'].
'&StartDate='.$myrow['startdate'].'&EndDate='.$myrow['enddate'].'&Edit=1">' . _('Edit') . '</a></td>
<td><a href="'.htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8').'?Item='.$Item.'&Branch='.$myrow['branchcode'].'&StartDate='.$myrow['startdate'] .'&EndDate='.$myrow['enddate'].'&delete=yes" onclick="return confirm(\'' . _('Are you sure you wish to delete this price?') . '\');">' . _('Delete') . '</a></td>
</tr>';
}
//END WHILE LIST LOOP
}
echo '</table></td></tr></table><br />';
echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
echo '<div>';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<input type="hidden" name="Item" value="' . $Item . '" />';
if (isset($_GET['Edit']) and $_GET['Edit']==1){
echo '<input type="hidden" name="Editing" value="Yes" />';
echo '<input type="hidden" name="OldStartDate" value="' . $_GET['StartDate'] .'" />';
echo '<input type="hidden" name="OldEndDate" value="' . $_GET['EndDate'] . '" />';
$_POST['Price']=$_GET['Price'];
$_POST['Branch']=$_GET['Branch'];
$_POST['StartDate'] = ConvertSQLDate($_GET['StartDate']);
if (Is_Date($_GET['EndDate'])){
$_POST['EndDate'] = ConvertSQLDate($_GET['EndDate']);
} else {
$_POST['EndDate']='';
}
}
if (!isset($_POST['Branch'])) {
$_POST['Branch']='';
}
if (!isset($_POST['Price'])) {
$_POST['Price']=0;
}
if (!isset($_POST['StartDate'])){
$_POST['StartDate'] = Date($_SESSION['DefaultDateFormat']);
}
if (!isset($_POST['EndDate'])){
$_POST['EndDate'] = '';
}
$sql = "SELECT branchcode,
brname
FROM custbranch
WHERE debtorno='" . $_SESSION['CustomerID'] . "'";
$result = DB_query($sql);
echo '<table class="selection">
<tr>
<td>' . _('Branch') . ':</td>
<td><select name="Branch">';
if ($myrow['branchcode']=='') {
echo '<option selected="selected" value="">' . _('All Branches') . '</option>';
} else {
echo '<option value="">' . _('All Branches') . '</option>';
}
while ($myrow=DB_fetch_array($result)) {
if ($myrow['branchcode']==$_GET['Branch']) {
echo '<option selected="selected" value="'.$myrow['branchcode'].'">' . htmlspecialchars($myrow['brname'], ENT_QUOTES, 'UTF-8') . '</option>';
} else {
echo '<option value="'.$myrow['branchcode'].'">' . htmlspecialchars($myrow['brname'], ENT_QUOTES, 'UTF-8') . '</option>';
}
}
echo '</select></td></tr>';
echo '<tr>
<td>' . _('Start Date') . ':</td>
<td><input type="text" name="StartDate" class="date" size="11" maxlength="10" value="' . $_POST['StartDate'] . '" /></td>
</tr>';
echo '<tr>
<td>' . _('End Date') . ':</td>
<td><input type="text" name="EndDate" class="date" size="11" maxlength="10" value="' . $_POST['EndDate'] . '" /></td></tr>';
echo '<tr><td>' . _('Price') . ':</td>
<td><input type="text" class="number" name="Price" size="11" maxlength="10" value="' . locale_number_format($_POST['Price'],2) . '" /></td>
</tr>
</table>';
echo '<br />
<div class="centre">
<input type="submit" name="submit" value="' . _('Enter Information') . '" />
</div>
</div>
</form>';
include('includes/footer.php');
exit;
function ReSequenceEffectiveDates ($Item, $PriceList, $CurrAbbrev, $CustomerID) {
/*This is quite complicated - the idea is that prices set up should be unique and there is no way two prices could be returned as valid - when getting a price in includes/GetPrice.inc the logic is to first look for a price of the salestype/currency within the effective start and end dates - then if not get the price with a start date prior but a blank end date (the default price). We would not want two prices where the effective dates fall between an existing price so it is necessary to update enddates of prices - with me - I am just hanging on here myself
Prices with no end date are default prices and need to be ignored in this resquence*/
$SQL = "SELECT branchcode,
startdate,
enddate
FROM prices
WHERE debtorno='" . $CustomerID . "'
AND stockid='" . $Item . "'
AND currabrev='" . $CurrAbbrev . "'
AND typeabbrev='" . $PriceList . "'
AND enddate<>''
ORDER BY
branchcode,
startdate,
enddate";
$result = DB_query($SQL);
unset($BranchCode);
while ($myrow = DB_fetch_array($result)){
if (!isset($BranchCode)){
unset($NextDefaultStartDate); //a price with a blank end date
unset($NextStartDate);
unset($EndDate);
unset($StartDate);
$BranchCode = $myrow['branchcode'];
}
if (isset($NextStartDate)){
if (Date1GreaterThanDate2(ConvertSQLDate($myrow['startdate']),$NextStartDate)){
$NextStartDate = ConvertSQLDate($myrow['startdate']);
if (Date1GreaterThanDate2(ConvertSQLDate($EndDate),ConvertSQLDate($myrow['startdate']))) {
/*Need to make the end date the new start date less 1 day */
$SQL = "UPDATE prices SET enddate = '" . FormatDateForSQL(DateAdd($NextStartDate,'d',-1)) . "'
WHERE stockid ='" .$Item . "'
AND currabrev='" . $CurrAbbrev . "'
AND typeabbrev='" . $PriceList . "'
AND startdate ='" . $StartDate . "'
AND enddate = '" . $EndDate . "'
AND debtorno ='" . $CustomerID . "'
AND branchcode='" . $BranchCode . "'";
$UpdateResult = DB_query($SQL);
}
} //end of if startdate after NextStartDate - we have a new NextStartDate
} //end of if set NextStartDate
else {
$NextStartDate = ConvertSQLDate($myrow['startdate']);
}
$StartDate = $myrow['startdate'];
$EndDate = $myrow['enddate'];
}
//Now look for duplicate prices with no end
$SQL = "SELECT price,
startdate,
enddate
FROM prices
WHERE debtorno=''
AND stockid='" . $Item . "'
AND currabrev='" . $CurrAbbrev . "'
AND typeabbrev='" . $PriceList . "'
AND debtorno ='" . $CustomerID . "'
AND branchcode=''
AND enddate ='0000-00-00'
ORDER BY startdate";
$result = DB_query($SQL);
while ($myrow = DB_fetch_array($result)) {
if (isset($OldStartDate)){
/*Need to make the end date the new start date less 1 day */
$NewEndDate = FormatDateForSQL(DateAdd(ConvertSQLDate($myrow['startdate']),'d',-1));
$SQL = "UPDATE prices SET enddate = '" . $NewEndDate . "'
WHERE stockid ='" .$Item . "'
AND currabrev='" . $CurrAbbrev . "'
AND typeabbrev='" . $PriceList . "'
AND startdate ='" . $OldStartDate . "'
AND debtorno ='" . $CustomerID . "'
AND branchcode=''
AND enddate = '0000-00-00'
AND debtorno =''";
$UpdateResult = DB_query($SQL);
}
$OldStartDate = $myrow['startdate'];
} // end of loop around duplicate no end date prices
}
?>