forked from chilek/lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cashimportparser.php
259 lines (227 loc) · 8.14 KB
/
cashimportparser.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
<?php
/*
* LMS version 1.11-git
*
* (C) Copyright 2001-2013 LMS Developers
*
* Please, see the doc/AUTHORS for more information about authors!
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* $Id$
*/
include(ConfigHelper::getConfig('phpui.import_config', 'cashimportcfg.php'));
if(!isset($patterns) || !is_array($patterns))
{
$error['file'] = trans('Configuration error. Patterns array not found!');
}
elseif(isset($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name']) && $_FILES['file']['size'])
{
$file = file($_FILES['file']['tmp_name']);
$filename = $_FILES['file']['name'];
$patterns_cnt = isset($patterns) ? sizeof($patterns) : 0;
$ln = 0;
foreach($file as $line)
{
$id = NULL;
$count = 0;
$ln++;
if($patterns_cnt) foreach($patterns as $idx => $pattern)
{
$theline = $line;
if(strtoupper($pattern['encoding']) != 'UTF-8')
{
$theline = @iconv($pattern['encoding'], 'UTF-8//TRANSLIT', $theline);
}
if(!preg_match($pattern['pattern'], $theline, $matches))
$count++;
else
break;
}
// line isn't matching to any pattern
if($count == $patterns_cnt)
{
if(trim($line) != '')
$error['lines'][$ln] = $patterns_cnt == 1 ? $theline : $line;
continue; // go to next line
}
$name = isset($matches[$pattern['pname']]) ? trim($matches[$pattern['pname']]) : '';
$lastname = isset($matches[$pattern['plastname']]) ? trim($matches[$pattern['plastname']]) : '';
$comment = isset($matches[$pattern['pcomment']]) ? trim($matches[$pattern['pcomment']]) : '';
$time = isset($matches[$pattern['pdate']]) ? trim($matches[$pattern['pdate']]) : '';
$value = str_replace(',','.', isset($matches[$pattern['pvalue']]) ? trim($matches[$pattern['pvalue']]) : '');
if(!$pattern['pid'])
{
if(!empty($pattern['pid_regexp']))
$regexp = $pattern['pid_regexp'];
else
$regexp = '/.*ID[:\-\/]([0-9]{0,4}).*/i';
if(preg_match($regexp, $theline, $matches))
$id = $matches[1];
}
else
$id = isset($matches[$pattern['pid']]) ? intval($matches[$pattern['pid']]) : NULL;
// seek invoice number
if(!$id && !empty($pattern['invoice_regexp']))
{
if(preg_match($pattern['invoice_regexp'], $theline, $matches))
{
$invid = $matches[$pattern['pinvoice_number']];
$invyear = $matches[$pattern['pinvoice_year']];
$invmonth = !empty($pattern['pinvoice_month']) && $pattern['pinvoice_month'] > 0 ? intval($matches[$pattern['pinvoice_month']]) : 1;
if($invid && $invyear)
{
$from = mktime(0,0,0, $invmonth, 1, $invyear);
$to = mktime(0,0,0, !empty($pattern['pinvoice_month']) && $pattern['pinvoice_month'] > 0 ? $invmonth + 1 : 13, 1, $invyear);
$id = $DB->GetOne('SELECT customerid FROM documents
WHERE number=? AND cdate>=? AND cdate<? AND type IN (?,?)',
array($invid, $from, $to, DOC_INVOICE, DOC_CNOTE));
}
}
}
if(!$id && $name && $lastname)
{
$uids = $DB->GetCol('SELECT id FROM customers WHERE UPPER(lastname)=UPPER(?) and UPPER(name)=UPPER(?)', array($lastname, $name));
if(sizeof($uids)==1)
$id = $uids[0];
}
elseif($id && (!$name || !$lastname))
{
if($tmp = $DB->GetRow('SELECT lastname, name FROM customers WHERE id = ?', array($id)))
{
$lastname = $tmp['lastname'];
$name = $tmp['name'];
}
else
$id = NULL;
}
if($time)
{
if(preg_match($pattern['date_regexp'], $time, $date))
{
$time = mktime(0,0,0,
$date[$pattern['pmonth']],
$date[$pattern['pday']],
$date[$pattern['pyear']]);
}
elseif(!is_numeric($time))
$time = time();
}
else
$time = time();
if(!empty($pattern['comment_replace']))
$comment = preg_replace($pattern['comment_replace']['from'], $pattern['comment_replace']['to'], $comment);
// remove unneeded spaces and cut $customer and $comment to fit into database (150 chars limit)
$customer = trim($lastname.' '.$name);
$customer = preg_replace('/[ ]+/',' ',$customer);
$customer = substr($customer,0,150);
$comment = trim($comment);
$comment = preg_replace('/[ ]+/',' ',$comment);
if (strlen($comment) > 150)
$comment = substr($comment, 0, 150);
if(!empty($pattern['use_line_hash']))
$hash = md5($theline.(!empty($pattern['line_idx_hash']) ? $ln : ''));
else
$hash = md5($time.$value.$customer.$comment.(!empty($pattern['line_idx_hash']) ? $ln : ''));
if(is_numeric($value))
{
if(isset($pattern['modvalue']) && $pattern['modvalue'])
{
$value = str_replace(',','.', $value * $pattern['modvalue']);
}
if(!$DB->GetOne('SELECT id FROM cashimport WHERE hash = ?', array($hash)))
{
// Add file
if (!$sourcefileid) {
$idate = time();
$DB->Execute('INSERT INTO sourcefiles (name, idate, userid)
VALUES (?, ?, ?)',
array($filename, $idate, $AUTH->id));
$sourcefileid = $DB->GetLastInsertId('sourcefiles');
if ($SYSLOG) {
$args = array(
$SYSLOG_RESOURCE_KEYS[SYSLOG_RES_SOURCEFILE] => $sourcefileid,
'name' => $filename,
'idate' => $idate,
$SYSLOG_RESOURCE_KEYS[SYSLOG_RES_USER] => $AUTH->id,
);
$SYSLOG->AddMessage(SYSLOG_RES_SOURCEFILE, SYSLOG_OPER_ADD, $args,
array($SYSLOG_RESOURCE_KEYS[SYSLOG_RES_SOURCEFILE],
$SYSLOG_RESOURCE_KEYS[SYSLOG_RES_USER]));
}
}
if(!empty($_POST['source']))
$sourceid = intval($_POST['source']);
elseif(!empty($pattern['id']))
$sourceid = intval($pattern['id']);
else
$sourceid = NULL;
$args = array(
'date' => $time,
'value' => $value,
'customer' => $customer,
$SYSLOG_RESOURCE_KEYS[SYSLOG_RES_CUST] => $id,
'description' => $comment,
'hash' => $hash,
$SYSLOG_RESOURCE_KEYS[SYSLOG_RES_CASHSOURCE] => $sourceid,
$SYSLOG_RESOURCE_KEYS[SYSLOG_RES_SOURCEFILE] => $sourcefileid,
);
$DB->Execute('INSERT INTO cashimport (date, value, customer,
customerid, description, hash, sourceid, sourcefileid)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)', array_values($args));
if ($SYSLOG) {
$args[$SYSLOG_RESOURCE_KEYS[SYSLOG_RES_CASHIMPORT]] = $DB->GetLastInsertID('cashimport');
$SYSLOG->AddMessage(SYSLOG_RES_CASHIMPORT, SYSLOG_OPER_ADD, $args,
array($SYSLOG_RESOURCE_KEYS[SYSLOG_RES_CASHIMPORT],
$SYSLOG_RESOURCE_KEYS[SYSLOG_RES_CUST],
$SYSLOG_RESOURCE_KEYS[SYSLOG_RES_CASHSOURCE],
$SYSLOG_RESOURCE_KEYS[SYSLOG_RES_SOURCEFILE]));
}
} else {
$error['lines'][$ln] = array(
'customer' => $customer,
'customerid' => $id,
'date' => $time,
'value' => $value,
'comment' => $comment
);
}
}
}
include(MODULES_DIR.'/cashimport.php');
die;
}
elseif(isset($_FILES['file'])) // upload errors
switch($_FILES['file']['error'])
{
case 1:
case 2: $error['file'] = trans('File is too large.'); break;
case 3: $error['file'] = trans('File upload has finished prematurely.'); break;
case 4: $error['file'] = trans('Path to file was not specified.'); break;
default: $error['file'] = trans('Problem during file upload.'); break;
}
$layout['pagetitle'] = trans('Cash Operations Import');
$SESSION->save('backto', $_SERVER['QUERY_STRING']);
$sourcefiles = $DB->GetAll('SELECT s.*, u.name AS username,
(SELECT COUNT(*) FROM cashimport WHERE sourcefileid = s.id) AS count
FROM sourcefiles s
LEFT JOIN users u ON (u.id = s.userid)
ORDER BY s.idate DESC LIMIT 10');
$SMARTY->assign('error', $error);
$SMARTY->assign('customerlist', $LMS->GetCustomerNames());
$SMARTY->assign('sourcelist', $DB->GetAll('SELECT id, name FROM cashsources ORDER BY name'));
$SMARTY->assign('sourcefiles', $sourcefiles);
$SMARTY->display('cash/cashimport.html');
?>