-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parcelforce.php
598 lines (453 loc) · 18.8 KB
/
Parcelforce.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<?php
/*
* The MIT License
*
* Copyright 2014 Alexander Pechkarev <alexpechkarev@gmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Package generates pre-advice electronic file that required by Parcelforce exrpessTransfer solution.
*
* @author Alexander Pechkarev <alexpechkarev@gmail.com>
*/
namespace Parcelforce\ExpressTransfer;
use Carbon\Carbon;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Parcelforce\ExpressTransfer\models\ConsNumber;
use Parcelforce\ExpressTransfer\models\FileNumber;
class Parcelforce {
/*
|--------------------------------------------------------------------------
| Global
|--------------------------------------------------------------------------
*/
protected $config;
protected $fileContent;
protected $dateObj;
protected $ftpConn;
/**
* Init cinfig file and run config cheks
*/
public function __construct($config) {
$this->config = $config;
// initiate checks
$this->setup();
}
/***/
/**
* Perform necessary checks
* @return boolean
* @throws \RuntimeException
*/
public function setup(){
// Check if files directory has been created
if(!File::isDirectory($this->config['filePath'])):
throw new \RuntimeException('It looks like config file has not been published. Use php artisan config:publish command.');
endif;
// Check if files directory has been created
if(!File::isWritable($this->config['filePath'])):
throw new \RuntimeException('Please make sure that files directory is writable.');
endif;
// initialized fileNumber with database on first run
if( !Schema::hasTable($this->config['filenum_table']['tableName']) ):
$this->createTable($this->config['filenum_table']);
FileNumber::create(array($this->config['filenum_table']['fieldName'] => $this->config['fileNumber'] ));
$this->config['header_record']['header_bath_number'] = $this->padWithZero();
$this->config['fileName'].= $this->padWithZero().'.tmp';
$this->setFileNumber();
else:
$this->setFileName();
endif;
// initialized dr_consignment_number with database on first run
if( !Schema::hasTable($this->config['consnum_table']['tableName'])):
$this->createTable($this->config['consnum_table']);
ConsNumber::create(array($this->config['consnum_table']['fieldName'] => $this->config['dr_consignment_number']['number'] ));
else:
$this->getConsignmentNumber();
endif;
/**
* Set header type to SKEL
* Allowing UK Domestic services despatches only
*
* config default: DSCC - UK Domestic collection request
*/
if($this->config['header_record']['header_file_type'] === 'SKEL'){
$this->config['deliveryDetails']['dr_location_id'] = 1;
}
return true;
}
/***/
/**
* Initiate process
* - generate file content
* - generate footer content
* - create consignment file
* - upload file
* @param array $data - array of data
* @return string File content
*/
public function process($data, $upload = TRUE){
// set collection date to default config value
if($this->config['header_record']['header_dispatch_date'] === 'CCYYMMDD'):
$this->setDate();
endif;
// set header record
$this->fileContent = $this->getHeader();
// process consignment data
$this->setRecord($data);
// set trailer record
$this->fileContent.= $this->getFooter();
//create file
$this->createFile();
// upload file
if(!empty($upload)):
$this->uploadFile();
endif;
return $this->fileContent;
}
/***/
/**
* Generating file content based on the given data
*
* @throws \InvalidArgumentException
*/
public function setRecord(){
if(func_num_args() != 1):
throw new \InvalidArgumentException("Invaild number of arguments given. Expecting an array.");
endif;
$data = func_get_args();
$cc = new \ArrayIterator($data[0]);
if($cc->count() < 1):
throw new \InvalidArgumentException("Invaild collection data.");
endif;
while( $cc->valid() ):
$item = $cc->current();
$cc->next();
// if sender details are given as parameter than merge with default
$senderDetails = isset($item['senderDetails'])
? array_merge($this->config['senderDetails'], $item['senderDetails'])
: $this->config['senderDetails'];
// for SKEL file type remove following fields
if($this->config['header_record']['header_file_type'] === 'SKEL'):
unset($senderDetails['senderContactName']);
unset($senderDetails['senderContactNumber']);
unset($senderDetails['senderVehicle']);
unset($senderDetails['senderPaymentMethod']);
unset($senderDetails['senderPaymentValue']);
endif;
// check that mandatory fields specified [not null]
try{
array_count_values($senderDetails);
}catch(\ErrorException $e){
throw new \InvalidArgumentException("Mandatory field ". array_search(null, $senderDetails, true). " must not be NULL!");
}
// Setting sender record
// increment record count
$this->config['footer_record']['trailer_record_count']++;
$this->fileContent.= implode($this->config['delimiterChar'], $senderDetails)."\r\n";
// generate consignment number
$this->config['deliveryDetails']['consignment_number'] = implode('', $this->config['dr_consignment_number']);
// increment consignment number for next package
$this->getConsignmentNumber();
$this->setConsignmentNumber();
//merge with default delivery details
$deliveryDetails = array_merge($this->config['deliveryDetails'], $item['deliveryDetails']);
// check that mandatory fields specified [not null]
try{
array_count_values($deliveryDetails);
}catch(\ErrorException $e){
throw new \InvalidArgumentException("Mandatory field ". array_search(null, $deliveryDetails, true). " must not be NULL!");
}
// increment record count
$this->config['footer_record']['trailer_record_count']++;
// Setting delivery record
$this->fileContent.= implode($this->config['delimiterChar'], $deliveryDetails)."\r\n";
endwhile;
}
/***/
/*
|-----------------------------------------------------------------------
| Helper methods
|-----------------------------------------------------------------------
*/
/**
* Setting collection date at run time
* @param type $date
*/
public function setDate($date = FALSE){
$this->config['collectionDate'] = $date ? $date : $this->config['collectionDate'];
// set date object
$this->dateObj = Carbon::parse($this->config['collectionDate'], $this->config['timeZone']);
// setting dispatch date
$this->config['header_record']['header_dispatch_date'] = $this->dateObj->format('Ymd');
}
/***/
/**
* Pad left with zeros
* @return string
*/
public function padWithZero(){
$num = (strlen((string)$this->config['fileNumber'] )+4) - strlen((string)$this->config['fileNumber'] );
return str_pad($this->config['fileNumber'], $num, 0, STR_PAD_LEFT);
}
/***/
/**
* Set file name
* Also set batch number
*/
public function setFileName(){
$this->config['fileNumber'] = FileNumber::orderBy('id', 'DESC')
->first()
->{$this->config['filenum_table']['fieldName']};
// reset file and batch numbers to 1 when reached 9999
if($this->config['fileNumber'] == 10000):
$this->config['fileNumber'] = 1;
$this->dropTable($this->config['filenum_table']);
$this->createTable($this->config['filenum_table']);
FileNumber::create(array($this->config['filenum_table']['fieldName'] => $this->config['fileNumber'] ));
endif;
// pad number left with zeros and set file name
$this->config['fileName'].= $this->padWithZero().'.tmp';
/**
* Unique number per batch, to be created by the source system
* Start at 1 and increment by 1 per batch After 9999 is reached, restart at 1
*/
// pad number left with zeros and set file name
$this->config['header_record']['header_bath_number'] = $this->padWithZero();
// incremnt file number for next run
$this->setFileNumber();
}
/***/
/**
* Get file name
*/
public function setFileNumber(){
FileNumber::create(array($this->config['filenum_table']['fieldName'] => ($this->config['fileNumber']+1) ));
}
/***/
/**
* Get consignment number from databse and assign to config
* Format integer
* Max/Min - 6
*/
public function getConsignmentNumber(){
$this->config['dr_consignment_number']['number'] = ConsNumber::orderBy('id', 'DESC')
->first()
->{$this->config['consnum_table']['fieldName']};
// set check digit for new consignment number
$this->setCheckDigit();
}
/***/
/**
* Set consignment number by incrementing it's value by 1 and store db
* Format integer
* Max/Min - 6
*/
public function setConsignmentNumber(){
ConsNumber::create(array($this->config['consnum_table']['fieldName'] => ($this->config['dr_consignment_number']['number']+1) ));
}
/***/
/**
* Drop database tables
*/
public function reset(){
// drop file number table
$this->dropTable($this->config['filenum_table']);
// drop consignment number table
$this->dropTable($this->config['consnum_table']);
}
/***/
/**
* Generate Check Digit
* Thus, given a 6 digit number of 162738 the check digit calculation is as follows:
1) 1 x 4 = 4
6 x 2 = 12
2 x 3 = 6
7 x 5 = 35
3 x 9 = 27
8 x 7 = 56
2) 4 + 12 + 6 + 35 + 27 + 56 = 140
3) 140 ¸ 11 = 12 remainder 8
4) 11 - 8 = 3
*
5) Check digit = 3
*/
public function setCheckDigit(){
$sum = ($this->config['dr_consignment_number']['number'][0] * 4)
+ ($this->config['dr_consignment_number']['number'][1] * 2)
+ ($this->config['dr_consignment_number']['number'][2] * 3)
+ ($this->config['dr_consignment_number']['number'][3] * 5)
+ ($this->config['dr_consignment_number']['number'][4] * 9)
+ ($this->config['dr_consignment_number']['number'][5] * 7) ;
$rem = $sum % 11;
$checkdigit = 0;
if((11 -$rem) == 10):
$checkdigit = 0;
elseif((11 - $rem) == 11):
$checkdigit = 5;
else:
$checkdigit = 11 - $rem;
endif;
$this->config['dr_consignment_number']['check_digit'] = $checkdigit;
}
/***/
/*
|-----------------------------------------------------------------------
| Header / Footer methods
|-----------------------------------------------------------------------
*/
/**
* Get hader record
*/
public function getHeader(){
return implode($this->config['delimiterChar'], $this->config['header_record'])."\r\n";
}
/***/
/**
* Get trailer record
*/
public function getFooter(){
return implode($this->config['delimiterChar'], $this->config['footer_record']);
}
/***/
/*
|-----------------------------------------------------------------------
| Databse helper methods
|-----------------------------------------------------------------------
*/
/**
* Creating database table
* @param array $tbl - ["tableName"=>"tbl_name", "fieldName"=>"fld_name"]
* @throws \InvalidArgumentException
*/
public function createTable($tbl){
if(is_array($tbl)
&& array_key_exists('tableName', $tbl)
&& array_key_exists('fieldName', $tbl)):
Schema::create($tbl['tableName'], function(Blueprint $table) use($tbl)
{
$table->increments('id');
$table->integer($tbl['fieldName']);
});
else:
throw new \InvalidArgumentException('Invalid agruments given. Expecting array ["tableName"=>"tbl_name", "fieldName"=>fld_name"]');
endif;
}
/***/
/**
* Drop database table
* @param array $tbl - ["tableName"=>"tbl_name", "fieldName"=>fld_name"]
* @throws \InvalidArgumentException
*/
public function dropTable($tbl){
if(is_array($tbl)
&& array_key_exists('tableName', $tbl)
&& array_key_exists('fieldName', $tbl)):
Schema::dropIfExists($tbl['tableName']);
else:
throw new \InvalidArgumentException('Invalid agruments given. Expecting array ["tableName"=>"tbl_name, "fieldName"=>fld_name]');
endif;
}
/***/
/*
|-----------------------------------------------------------------------
| File methods
|-----------------------------------------------------------------------
*/
/**
* Creating file and writing consignment details
*
* @return boolean
* @throws \RuntimeException
*/
public function createFile(){
// write to the file
if (File::put($this->config['filePath'].$this->config['fileName'], $this->fileContent) === false):
throw new \RuntimeException('Unable to write to: '.$this->config['fileName']);
endif;
return true;
}
/***/
/**
* Uploading file to FTP
*
* @throws \RuntimeException
*/
public function uploadFile(){
// establish connection
$this->ftpConn = ftp_connect($this->config['ftpHost']);
if(empty($this->ftpConn)):
throw new \RuntimeException("Unable to connect to FTP - ".$this->config['ftpHost']);
endif;
// attempt login
if(ftp_login($this->ftpConn, $this->config['ftpUser'], $this->config['ftpPass']) === false):
throw new \RuntimeException("Unable to FTP login with - ".$this->config['ftpUser']);
endif;
// turn passive mode on
ftp_pasv($this->ftpConn, true);
// upload file
if( ftp_put($this->ftpConn, $this->config['ftpUploadPath']."/".$this->config['fileName'],
$this->config['filePath'].$this->config['fileName'], FTP_ASCII)){
// get file info
$info = pathinfo($this->config['fileName']);
$new_file_name = $info['filename'];
// remove file extension and put in the final location path
if(ftp_rename($this->ftpConn, $this->config['ftpUploadPath']."/".$this->config['fileName'],
$this->config['ftpLocationPath']."/".$new_file_name)):
endif;
}else{
// Error uploading file
}
// close conection
ftp_close($this->ftpConn);
}
/***/
/*
|-----------------------------------------------------------------------
| Getters
|-----------------------------------------------------------------------
*/
/**
* Get file content
* @return string
*/
public function getFileContent(){
return $this->fileContent;
}
/***/
/**
* Get date object
* @return Carbon object
*/
public function getDateObj(){
return $this->dateObj;
}
/***/
/**
* Get config file of current instance
* @return array
*/
public function getConfig(){
return $this->config;
}
/***/
}