forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Z_ImportPriceList.php
169 lines (143 loc) · 6.15 KB
/
Z_ImportPriceList.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
<?php
include('includes/session.php');
$Title = _('Import Sales Price List');
include('includes/header.php');
echo '<p class="page_title_text"><img alt="" src="' . $RootPath . '/css/' . $Theme .
'/images/maintenance.png" title="' .
_('Import Price List from CSV file') . '" />' . ' ' .
_('Import Price List from CSV file') . '</p>';
$FieldHeadings = array(
'StockID', // 0 'STOCKID',
'SalesType', // 1 'Price list id',
'CurrencyCode', // 2 'Currency Code',
'Price' // 3 'Price'
);
if (isset($_FILES['PriceListFile']) and $_FILES['PriceListFile']['name']) { //start file processing
//check file info
$FileName = $_FILES['PriceListFile']['name'];
$TempName = $_FILES['PriceListFile']['tmp_name'];
$FileSize = $_FILES['PriceListFile']['size'];
$FieldTarget = 4;
$InputError = 0;
//get file handle
$FileHandle = fopen($TempName, 'r');
//get the header row
$HeadRow = fgetcsv($FileHandle, 10000, ',');
//check for correct number of fields
if ( count($HeadRow) != count($FieldHeadings) ) {
prnMsg (_('File contains') . ' '. count($HeadRow). ' ' . _('columns, expected') . ' '. count($FieldHeadings). '. ' . _('Download the template to see the expected columns.'),'error');
fclose($FileHandle);
include('includes/footer.php');
exit;
}
//test header row field name and sequence
$HeadingColumnNumber = 0;
foreach ($HeadRow as $HeadField) {
if ( trim(mb_strtoupper($HeadField)) != trim(mb_strtoupper($FieldHeadings[$HeadingColumnNumber]))) {
prnMsg (_('The file to import the price list from contains incorrect column headings') . ' '. mb_strtoupper($HeadField). ' != '. mb_strtoupper($FieldHeadings[$HeadingColumnNumber]). '<br />' . _('The column headings must be') . ' StockID, SalesType, CurrencyCode, Price','error');
fclose($FileHandle);
include('includes/footer.php');
exit;
}
$HeadingColumnNumber++;
}
//start database transaction
DB_Txn_Begin();
//loop through file rows
$LineNumber = 1;
while ( ($myrow = fgetcsv($FileHandle, 10000, ',')) !== FALSE ) {
//check for correct number of fields
$FieldCount = count($myrow);
if ($FieldCount != $FieldTarget){
prnMsg ($FieldTarget . ' ' . _('fields required') . ', '. $FieldCount. ' ' . _('fields received'),'error');
fclose($FileHandle);
include('includes/footer.php');
exit;
}
// cleanup the data (csv files often import with empty strings and such)
$StockID = mb_strtoupper($myrow[0]);
foreach ($myrow as &$value) {
$value = trim($value);
$value = str_replace('"', '', $value);
}
//first off check that the item actually exist
$sql = "SELECT COUNT(stockid) FROM stockmaster WHERE stockid='" . $StockID . "'";
$result = DB_query($sql);
$testrow = DB_fetch_row($result);
if ($testrow[0] == 0) {
$InputError = 1;
prnMsg (_('Stock item') . ' "'. $myrow[0]. '" ' . _('does not exist'),'error');
}
//Then check that the price list actually exists
$sql = "SELECT COUNT(typeabbrev) FROM salestypes WHERE typeabbrev='" . $myrow[1] . "'";
$result = DB_query($sql);
$testrow = DB_fetch_row($result);
if ($testrow[0] == 0) {
$InputError = 1;
prnMsg (_('SalesType/Price List') . ' "' . $myrow[1]. '" ' . _('does not exist'),'error');
}
//Then check that the currency code actually exists
$sql = "SELECT COUNT(currabrev) FROM currencies WHERE currabrev='" . $myrow[2] . "'";
$result = DB_query($sql);
$testrow = DB_fetch_row($result);
if ($testrow[0] == 0) {
$InputError = 1;
prnMsg (_('Currency') . ' "' . $myrow[2] . '" ' . _('does not exist'),'error');
}
//Finally force the price to be a double
$myrow[3] = (double)$myrow[3];
if ($InputError !=1){
//Firstly close any open prices for this item
$sql = "UPDATE prices
SET enddate='" . FormatDateForSQL($_POST['StartDate']) . "'
WHERE stockid='" . $StockID . "'
AND enddate>'" . date('Y-m-d') . "'
AND typeabbrev='" . $myrow[1] . "'";
$result = DB_query($sql);
//Insert the price
$sql = "INSERT INTO prices (stockid,
typeabbrev,
currabrev,
price,
startdate
) VALUES (
'" . $myrow[0] . "',
'" . $myrow[1] . "',
'" . $myrow[2] . "',
'" . $myrow[3] . "',
'" . FormatDateForSQL($_POST['StartDate']) . "')";
$ErrMsg = _('The price could not be added because');
$DbgMsg = _('The SQL that was used to add the price failed was');
$result = DB_query($sql, $ErrMsg, $DbgMsg);
}
if ($InputError == 1) { //this row failed so exit loop
break;
}
$LineNumber++;
}
if ($InputError == 1) { //exited loop with errors so rollback
prnMsg(_('Failed on row '. $LineNumber. '. Batch import has been rolled back.'),'error');
DB_Txn_Rollback();
} else { //all good so commit data transaction
DB_Txn_Commit();
prnMsg( _('Batch Import of') .' ' . $FileName . ' '. _('has been completed. All transactions committed to the database.'),'success');
}
fclose($FileHandle);
} else { //show file upload form
echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '" method="post" class="noPrint" enctype="multipart/form-data">';
echo '<div class="centre">';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<div class="page_help_text">' .
_('This function loads a new sales price list from a comma separated variable (csv) file.') . '<br />' .
_('The file must contain four columns, and the first row should be the following headers:') . '<br />StockID, SalesType, CurrencyCode, Price<br />' .
_('followed by rows containing these four fields for each price to be uploaded.') . '<br />' .
_('The StockID, SalesType, and CurrencyCode fields must have a corresponding entry in the stockmaster, salestypes, and currencies tables.') . '</div>';
echo '<br /><input type="hidden" name="MAX_FILE_SIZE" value="1000000" />' .
_('Prices effective from') . ': <input type="text" name="StartDate" maxlength="10" size="11" class="date" value="' . date($_SESSION['DefaultDateFormat']) . '" /> ' .
_('Upload file') . ': <input name="PriceListFile" type="file" />
<input type="submit" name="submit" value="' . _('Send File') . '" />
</div>
</form>';
}
include('includes/footer.php');
?>