-
Notifications
You must be signed in to change notification settings - Fork 95
/
TransactionBuilder.php
727 lines (666 loc) · 23.4 KB
/
TransactionBuilder.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
<?php
namespace Avalara;
use GuzzleHttp\Client;
/*****************************************************************************
* Transaction Builder Convenience Object *
* *
* This file is not automatically generated by the AvaTax SDK process. *
* You may edit this file. *
* *
*****************************************************************************/
/**
* TransactionBuilder helps you construct a new transaction using a literate interface
*/
class TransactionBuilder
{
/**
* The in-progress model
*/
private $_model;
/**
* Keeps track of the line number when adding multiple lines
*/
private $_line_number;
/**
* The client that will be used to create the transaction
*/
private $_client;
/**
* TransactionBuilder helps you construct a new transaction using a literate interface
*
* @param AvaTaxClient $client The AvaTaxClient object to use to create this transaction
* @param string $companyCode The code of the company for this transaction
* @param string $type The type of transaction to create (See DocumentType::* for a list of allowable values)
* @param string $customerCode The customer code for this transaction
* @param string|null $dateTime The datetime of the transaction, defaults to current time when null (Format: Y-m-d)
*/
public function __construct($client, $companyCode, $type, $customerCode, $dateTime = null)
{
$this->_client = $client;
$this->_line_number = 1;
$this->_model = [
'companyCode' => $companyCode,
'customerCode' => $customerCode,
'date' => $dateTime !== null ? $dateTime : date('Y-m-d'),
'type' => $type,
'lines' => [],
];
}
/**
* Set the commit flag of the transaction.
*
* @return
*/
public function withCommit()
{
$this->_model['commit'] = true;
return $this;
}
/**
* Enable diagnostic information
*
* @return TransactionBuilder
*/
public function withDiagnostics()
{
$this->_model['debugLevel'] = TaxDebugLevel::C_DIAGNOSTIC;
return $this;
}
/**
* Set a specific discount amount
*
* @param float $discount
* @return TransactionBuilder
*/
public function withDiscountAmount($discount)
{
$this->_model['discount'] = $discount;
return $this;
}
/**
* Set if discount is applicable for the current line
*
* @param boolean discounted
* @return TransactionBuilder
*/
public function withItemDiscount($discounted)
{
$li = $this->getMostRecentLineIndex();
$this->_model['lines'][$li]['discounted'] = $discounted;
return $this;
}
/**
* Set a specific transaction code
*
* @param string code
* @return TransactionBuilder
*/
public function withTransactionCode($code)
{
$this->_model['code'] = $code;
return $this;
}
/**
* Set the document type
*
* @param string type (See DocumentType::* for a list of allowable values)
* @return TransactionBuilder
*/
public function withType($type)
{
$this->_model['type'] = $type;
return $this;
}
/**
* Set VAT business identification number for customer
*
* @param string no
* @return TransactionBuilder
*/
public function withBusinessIdentificationNo($no)
{
$this->_model['businessIdentificationNo'] = $no;
return $this;
}
/**
* Set a customer email
*
* @param string email
* @return TransactionBuilder
*/
public function withEmail($email)
{
$this->_model['email'] = $email;
return $this;
}
/**
* Set client application customer or usage type
*
* @param string code (See API endpoint `/api/v2/definitions/entityusecodes` for a list of allowable values)
* @return TransactionBuilder
*/
public function withEntityUseCode($code)
{
$this->_model['entityUseCode'] = $code;
return $this;
}
/**
* Set purchase order number
*
* @param string no
* @return TransactionBuilder
*/
public function withPurchaseOrderNo($no)
{
$this->_model['purchaseOrderNo'] = $no;
return $this;
}
/**
* Set customer-provided reference code
*
* @param string code
* @return TransactionBuilder
*/
public function withReferenceCode($code)
{
$this->_model['referenceCode'] = $code;
return $this;
}
/**
* Set the sale location code for reporting this document to the tax authority
*
* @param string code
* @return TransactionBuilder
*/
public function withReportingLocationCode($code)
{
$this->_model['reportingLocationCode'] = $code;
return $this;
}
/**
* Set flag for seller as importer of record
*
* @param bool $isSeller
*
* @return TransactionBuilder
*/
public function withSellerIsImporterOfRecord($isSeller = true)
{
$this->_model['isSellerImporterOfRecord'] = $isSeller;
return $this;
}
/**
* Set exchange rate information
*
* @param float rate
* @param date effectiveDate
* @return TransactionBuilder
*/
public function withExchangeRate($rate, $effectiveDate = null)
{
$this->_model['exchangeRate'] = $rate;
if ($effectiveDate) {
$this->_model['exchangeRateEffectiveDate'] = $effectiveDate;
}
return $this;
}
/**
* Specific a exchange rate currency code for this transaction
*
* @param string $exchangeRateCurrencyCode Specific the three-character ISO 4217 code that is being used for exchange rate for this transaction (optional)
* @return TransactionBuilder
*/
public function withExchangeRateCurrencyCode($exchangeRateCurrencyCode)
{
$this->_model['exchangeRateCurrencyCode'] = $exchangeRateCurrencyCode;
return $this;
}
/**
* Add a parameter at the document level
*
* @param string name
* @param string value
* @return TransactionBuilder
*/
public function withParameter($name, $value)
{
if (empty($this->_model['parameters'])) $this->_model['parameters'] = [];
$this->_model['parameters'][$name] = $value;
return $this;
}
/**
* Add a parameter to the current line
*
* @param string name
* @param string value
* @return TransactionBuilder
*/
public function withLineParameter($name, $value)
{
$li = $this->getMostRecentLineIndex();
if (empty($this->_model['lines'][$li]['parameters'])) {
$this->_model['lines'][$li]['parameters'] = [];
}
$this->_model['lines'][$li]['parameters'][$name] = $value;
return $this;
}
/**
* Add a parameter to the current line when unit is specified
*
* @param string name
* @param string value
* @param string unit
* @return TransactionBuilder
*/
public function withLineParameterAndUnit($name, $value, $unit)
{
$li = $this->getMostRecentLineIndex();
if (empty($this->_model['lines'][$li]['parameters'])) {
$this->_model['lines'][$li]['parameters'] = [];
}
$l = [
'name' => $name,
'value' => $value,
'unit' => $unit
];
$this->_model['lines'][$li]['parameters'][] = $l;
return $this;
}
/**
* Add an address to this transaction
*
* @param string type Address Type (See AddressType::* for a list of allowable values)
* @param string line1 The street address, attention line, or business name of the location.
* @param string line2 The street address, business name, or apartment/unit number of the location.
* @param string line3 The street address or apartment/unit number of the location.
* @param string city City of the location.
* @param string region State or Region of the location.
* @param string postalCode Postal/zip code of the location.
* @param string country The two-letter country code of the location.
* @return TransactionBuilder
*/
public function withAddress($type, $line1, $line2, $line3, $city, $region, $postalCode, $country)
{
if (empty($this->_model['addresses'])) $this->_model['addresses'] = [];
$ai = [
'line1' => $line1,
'line2' => $line2,
'line3' => $line3,
'city' => $city,
'region' => $region,
'postalCode' => $postalCode,
'country' => $country
];
$this->_model['addresses'][$type] = $ai;
return $this;
}
/**
* Add an address to this transaction using an existing company location code.
*
* AvaTax will search for a company location whose code matches the `locationCode` parameter and use the address
* of that location.
*
* @param string type Address Type (See AddressType::* for a list of allowable values)
* @param string locationCode The location code of the address
* @return TransactionBuilder
*/
public function WithAddressLocationCode($type, $locationCode)
{
if (empty($this->_model['addresses'])) $this->_model['addresses'] = [];
$ai = [
'locationCode' => $locationCode
];
$this->_model['addresses'][$type] = $ai;
return $this;
}
/**
* Add a lat/long coordinate to this transaction
*
* @param string $type Address Type (See AddressType::* for a list of allowable values)
* @param float $latitude The latitude of the geolocation for this transaction
* @param float $longitude The longitude of the geolocation for this transaction
* @return TransactionBuilder
*/
public function withLatLong($type, $latitude, $longitude)
{
$this->_model['addresses'][$type] = [
'latitude' => $latitude,
'longitude' => $longitude
];
return $this;
}
/**
* Add an address to this line
*
* @param string type Address Type (See AddressType::* for a list of allowable values)
* @param string line1 The street address, attention line, or business name of the location.
* @param string line2 The street address, business name, or apartment/unit number of the location.
* @param string line3 The street address or apartment/unit number of the location.
* @param string city City of the location.
* @param string region State or Region of the location.
* @param string postalCode Postal/zip code of the location.
* @param string country The two-letter country code of the location.
* @return TransactionBuilder
*/
public function withLineAddress($type, $line1, $line2, $line3, $city, $region, $postalCode, $country)
{
$li = $this->getMostRecentLineIndex();
$this->_model['lines'][$li]['addresses'][$type] = [
'line1' => $line1,
'line2' => $line2,
'line3' => $line3,
'city' => $city,
'region' => $region,
'postalCode' => $postalCode,
'country' => $country
];
return $this;
}
/**
* Add a document-level Tax Override to the transaction.
* - A TaxDate override requires a valid DateTime object to be passed.
* TODO: Verify Tax Override constraints and add exceptions.
*
* @param string $type Type of the Tax Override (See TaxOverrideType::* for a list of allowable values)
* @param string $reason Reason of the Tax Override.
* @param float $taxAmount Amount of tax to apply. Required for a TaxAmount Override.
* @param date $taxDate Date of a Tax Override. Required for a TaxDate Override.
* @return TransactionBuilder
*/
public function withTaxOverride($type, $reason, $taxAmount, $taxDate)
{
$this->_model['taxOverride'] = [
'type' => $type,
'reason' => $reason,
'taxAmount' => $taxAmount,
'taxDate' => $taxDate
];
// Continue building
return $this;
}
/**
* Add a line-level Tax Override to the current line.
* - A TaxDate override requires a valid DateTime object to be passed.
* TODO: Verify Tax Override constraints and add exceptions.
*
* @param string $type Type of the Tax Override (See TaxOverrideType::* for a list of allowable values)
* @param string $reason Reason of the Tax Override.
* @param float $taxAmount Amount of tax to apply. Required for a TaxAmount Override.
* @param date $taxDate Date of a Tax Override. Required for a TaxDate Override.
* @return TransactionBuilder
*/
public function withLineTaxOverride($type, $reason, $taxAmount, $taxDate)
{
// Address the DateOverride constraint.
if (($type == TaxOverrideType::C_TAXDATE) && (empty($taxDate))) {
throw new Exception("A valid date is required for a Tax Date Tax Override.");
}
$li = $this->getMostRecentLineIndex();
$this->_model['lines'][$li]['taxOverride'] = [
'type' => $type,
'reason' => $reason,
'taxAmount' => $taxAmount,
'taxDate' => $taxDate
];
// Continue building
return $this;
}
/**
* Set description for current line
*
* @param string description
* @return TransactionBuilder
* @throws \Exception
*/
public function withLineDescription($description)
{
$li = $this->getMostRecentLineIndex();
$this->_model['lines'][$li]['description'] = $description;
return $this;
}
/**
* Set flag on current line to indicate tax is included
*
* @return TransactionBuilder
* @throws \Exception
*/
public function withLineTaxIncluded()
{
$li = $this->getMostRecentLineIndex();
$this->_model['lines'][$li]['taxIncluded'] = true;
return $this;
}
/**
* Set customer defined fields for current line
*
* @param string ref1
* @param string ref2
* @return TransactionBuilder
* @throws \Exception
*/
public function withLineCustomFields($ref1, $ref2 = null)
{
$li = $this->getMostRecentLineIndex();
$this->_model['lines'][$li]['ref1'] = $ref1;
if ($ref2) {
$this->_model['lines'][$li]['ref2'] = $ref2;
}
return $this;
}
/**
* Set HS code for current line
*
* @param string hsCode
* @return TransactionBuilder
* @throws \Exception
*/
public function withLineHsCode($hsCode)
{
$li = $this->getMostRecentLineIndex();
$this->_model['lines'][$li]['hsCode'] = $hsCode;
return $this;
}
/**
* Add a line to this transaction
*
* @param float $amount Value of the item.
* @param float $quantity Quantity of the item.
* @param string $itemCode
* @param string $taxCode Tax Code of the item.
- If itemCode is blank and taxCode is left blank, the default item (P0000000) is assumed.
- If itemCode is mapped to a taxCode in AvaTax Management System then the mapped taxCode
takes precedence, even if left blank or provided with any other value.
* @param string $lineNumber Custom Line number, defaults to auto-incremented number if null
* @return TransactionBuilder
*/
public function withLine($amount, $quantity, $itemCode, $taxCode, $lineNumber = null)
{
if($lineNumber === null) {
$lineNumber = $this->_line_number;
}
$l = [
'number' => $lineNumber,
'quantity' => $quantity,
'amount' => $amount,
'taxCode' => $taxCode,
'itemCode' => $itemCode
];
array_push($this->_model['lines'], $l);
$this->_line_number++;
// Continue building
return $this;
}
/**
* Add a line to this transaction
*
* @param float $amount Value of the line
* @param string $type Address Type (See AddressType::* for a list of allowable values)
* @param string $line1 The street address, attention line, or business name of the location.
* @param string $line2 The street address, business name, or apartment/unit number of the location.
* @param string $line3 The street address or apartment/unit number of the location.
* @param string $city City of the location.
* @param string $region State or Region of the location.
* @param string $postalCode Postal/zip code of the location.
* @param string $country The two-letter country code of the location.
* @param string $lineNumber Custom Line number, defaults to auto-incremented number if null
* @return TransactionBuilder
*/
public function withSeparateAddressLine($amount, $type, $line1, $line2, $line3, $city, $region, $postalCode,
$country, $lineNumber = null)
{
if($lineNumber === null) {
$lineNumber = $this->_line_number;
}
$l = [
'number' => $lineNumber,
'quantity' => 1,
'amount' => $amount,
'addresses' => [
$type => [
'line1' => $line1,
'line2' => $line2,
'line3' => $line3,
'city' => $city,
'region' => $region,
'postalCode' => $postalCode,
'country' => $country
]
]
];
// Put this line in the model
array_push($this->_model['lines'], $l);
$this->_line_number++;
// Continue building
return $this;
}
/**
* Add a line with an exemption to this transaction
*
* @param float $amount The amount of this line item
* @param string $exemptionCode The exemption code for this line item
* @param string $lineNumber Custom Line number, defaults to auto-incremented number if null
* @return TransactionBuilder
*/
public function withExemptLine($amount, $itemCode, $exemptionCode, $lineNumber = null)
{
if($lineNumber === null) {
$lineNumber = $this->_line_number;
}
$l = [
'number' => $lineNumber,
'quantity' => 1,
'amount' => $amount,
'exemptionCode' => $exemptionCode,
'itemCode' => $itemCode
];
array_push($this->_model['lines'], $l);
$this->_line_number++;
// Continue building
return $this;
}
/**
* Add a exemption number for this document
*
* @param string $exemptionNo Exemption Number for this document.
* @return TransactionBuilder
*/
public function withExemptionNo($exemptionNo)
{
$this->_model['exemptionNo'] = $exemptionNo;
return $this;
}
/**
* Specific a currency code for this transaction
*
* @param string $currencyCode Specific the three-character ISO 4217 code that is being used for this transaction (optional)
* @return TransactionBuilder
*/
public function withCurrencyCode($currencyCode)
{
$this->_model['currencyCode'] = $currencyCode;
return $this;
}
/**
* Checks to see if the current model has a line.
*
* @return int
*/
private function getMostRecentLineIndex()
{
$c = count($this->_model['lines']);
if ($c <= 0) {
throw new \Exception("No lines have been added. The $memberName method applies to the most recent line. To use this function, first add a line.");
}
return $c-1;
}
/**
* Get the line number of the most recently added line
*
* @return int|null
*/
public function getCurrentLineNumber()
{
try {
$li = $this->getMostRecentLineIndex();
return $this->_model['lines'][$li]['number'];
} catch (\Exception $e) {
return null;
}
}
/**
* Create this transaction
*
* @param string $include Specifies objects to include in the response after transaction is created
* @return TransactionModel
*/
public function create($include = null)
{
return $this->_client->createTransaction($include, $this->_model);
}
/**
* Create this transaction, or adjust an existing transaction that has the same transaction code.
*
* @param string $include
* @return TransactionModel
*/
public function createOrAdjust($include = null)
{
$createOrAdjustTransactionModel = new CreateOrAdjustTransactionModel();
$createOrAdjustTransactionModel->createTransactionModel = $this->_model;
return $this->_client->createOrAdjustTransaction($include, $createOrAdjustTransactionModel);
}
/**
* Create a transaction adjustment request that can be used with the AdjustTransaction() API call
*
* @return AdjustTransactionModel
*/
public function createAdjustmentRequest($desc, $reason)
{
return [
'newTransaction' => $this->_model,
'adjustmentDescription' => $desc,
'adjustmentReason' => $reason
];
}
/**
* Add Avalara LineItemModel objects to the lines array
*
* @return TransactionBuilder
*/
public function withLineItem($thing)
{
$mode = is_array($thing) ? 'multi' : 'single';
if ($mode === 'single') {
$this->_model['lines'][] = (array)$thing;
$this->_line_number++;
}
if ($mode === 'multi') {
foreach($thing as $lineItem) {
$this->_model['lines'][] = (array)$lineItem;
$this->_line_number++;
}
}
return $this;
}
}
?>