-
Notifications
You must be signed in to change notification settings - Fork 20
/
api_post.php
executable file
·1177 lines (1056 loc) · 45.7 KB
/
api_post.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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Copyright (c) 2013, Intacct OpenSource Initiative
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* OVERVIEW
* The general pattern for using this SDK is to first create an instance of api_session and call either
* connectCredentials or connectSessionId to start an active session with the Intacct Web Services gateway.
* You will then pass the api_session as an argument in the api_post class methods. intacctws-php handles all
* XML serialization and de-serialization and HTTPS transport.
*/
require_once 'api_util.php';
require_once 'api_viewFilter.php';
require_once 'api_viewFilters.php';
require_once 'api_returnFormat.php';
require_once 'api_objDef.php';
require_once 'api_ddsJob.php';
require_once 'api_ddsFileConfiguration.php';
require_once 'api_userPermissions.php';
/**
* Class api_post
*
* Collection of static methods for interacting with the Intacct Web Services.
*/
class api_post
{
private static $lastRequest;
private static $lastResponse;
private static $dryRun;
const DEFAULT_PAGESIZE = 1000;
const DEFAULT_MAXRETURN = 100000;
const DDS_JOBTYPE_ALL = 'all';
const DDS_JOBTYPE_CHANGE = 'change';
/**
* Read one or more records by their key. For platform objects, the key is the 'id' field.
* For standard objects, the key is the 'recordno' field. Results are returned as a php structured array
*
* @param String $object the integration name for the object
* @param String $id a comma separated list of keys for each record you wish to read
* @param String $fields a comma separated list of fields to return
* @param \api_session|Object $session an instance of the php_session object
*
* @return Array of records
*/
public static function read($object, $id, $fields, api_session $session)
{
$readXml = "<read><object>$object</object><keys>$id</keys><fields>$fields</fields><returnFormat>csv</returnFormat></read>";
$objCsv = api_post::post($readXml, $session);
api_post::validateReadResults($objCsv);
$objAry = api_util::csvToPhp($objCsv);
if (count(explode(",", $id)) > 1) {
return $objAry;
} else {
return $objAry[0];
}
}
/**
* Create one or more records. Object types can be mixed and can be either standard or custom.
* Check the developer documentation to see which standard objects are supported in this method
*
* @param Array $records is an array of records to create. Follow the pattern
* $records = array(array('myobjecttype' => array('field1' => 'value',
* 'field2' => 'value')),
* array('myotherobjecttype' => array('field1' => 'value',
* 'field2' => 'value')));
* @param api_session $session instance of api_session object with valid connection
*
* @throws Exception
* @return Array array of keys to the objects created
*/
public static function create($records, api_session $session)
{
if (count($records) > 100) {
throw new Exception("Attempting to create more than 100 records. (" . count($records) . ") ");
}
// Convert the record into an xml structure
$createXml = "<create>";
$node = "";
foreach ($records as $record) {
$nodeAry = array_keys($record);
$node = $nodeAry[0];
$objXml = api_util::phpToXml($node, $record[$node]);
$createXml = $createXml . $objXml;
}
$createXml = $createXml . "</create>";
$res = api_post::post($createXml, $session);
$records = api_post::processUpdateResults($res, $node);
return $records;
}
/**
* Update one or more records. Object types can be mixed and can be either standard or custom.
* Check the developer documentation to see which standard objects are supported in this method
*
* @param Array $records an array of records to update. Follow the pattern
* $records = array(array('mycustomobjecttype' => array('id' => 112233, // you must pass the id value
* 'updatefield' => 'updateValue')),
* array('mystandardobjecttype' => array('recordno' => 555, // you must pass the recordno value for standard objects
* 'updatefield' => 'updateValue')));
* @param api_session $session api_session object with a valid connection
*
* @throws Exception
* @return array An array of 'ids' updated in the method invocation
*/
public static function update($records, api_session $session)
{
if (count($records) > 100) {
throw new Exception("Attempting to update more than 100 records.");
}
// convert the $records array into an xml structure
$updateXml = "<update>";
$node = '';
foreach ($records as $record) {
$nodeAry = array_keys($record);
$node = $nodeAry[0];
$objXml = api_util::phpToXml($node, $record[$node]);
$updateXml = $updateXml . $objXml;
}
$updateXml = $updateXml . "</update>";
$res = api_post::post($updateXml, $session);
return api_post::processUpdateResults($res, $node);
}
/**
* Checks to see if a record exists. If so, it updates, else it creates
*
* @param String $object The type of object to perform upsert on
* @param Array $records Array of records to upsert. Should be passed in the same format as used with
* create and update
* @param Mixed $nameField the field name used for lookup of existence
* @param Mixed $keyField the field name used for the internal key (needed for update)
* @param api_session $session An active api_session object
* @param bool $readOnlyName Optional. You shouldn't normally set this to true unless the value in the
* name field is actually set by the platform and you're passing a formulated value
* that should not be passed in the create or update
*
* @throws Exception
* @return null
*/
public static function upsert($object, $records, $nameField, $keyField, api_session $session, $readOnlyName = false)
{
if (count($records) > 100) {
throw new Exception("You can only upsert up to 100 records at a time. You passed " . count($records) . " $object records.");
}
$keys = array();
foreach ($records as $record) {
$keys[] = htmlspecialchars(str_replace('\'', '\\', $record[$object][$nameField]));
}
$where = "$nameField in ('" . join("','", $keys) . "')";
$existingRecords = api_post::readByQuery($object, $where, "$nameField,$keyField", $session);
if (!is_array($existingRecords)) {
$existingRecords = array();
}
$toUpdate = array(); $toCreate = array();
if (count($existingRecords) == 0) {
if ($readOnlyName == true) {
foreach ($records as $key => $rec) {
unset($records[$key][$object][$nameField]);
}
}
api_post::create($records, $session);
} else {
// convert the result into an array of keys
$existingKeys = array();
foreach ($existingRecords as $rec) {
$existingKeys[] = $rec[$nameField];
}
// also create an index by name
$existingByName = array();
foreach ($existingRecords as $rec) {
$existingByName[$rec[$nameField]] = $rec[$keyField];
}
foreach ($records as $rec) {
if (in_array($rec[$object][$nameField], $existingKeys)) {
$toUpdate[] = $rec;
} else {
$toCreate[] = $rec;
}
}
// convert the create and update arrays into operable structures
if (count($toCreate) > 0) {
if ($readOnlyName === true) {
foreach ($toCreate as $key => $rec) {
unset($toCreate[$key][$object][$nameField]);
}
}
api_post::create($toCreate, $session);
}
if (count($toUpdate) > 0) {
foreach ($toUpdate as $updateKey => $updateRec) {
$toUpdate[$updateKey][$object][$keyField] = $existingByName[$updateRec[$object][$nameField]];
if ($readOnlyName === true) {
unset($toUpdate[$updateKey][$object][$nameField]);
}
}
api_post::update($toUpdate, $session);
}
}
}
/**
* Delete one or more records
*
* @param String $object integration code of object type to delete
* @param String $ids String a comma separated list of keys. use 'id' values for custom
* objects and 'recordno' values for standard objects
* @param api_session $session instance of api_session object
*
* @return null
*/
public static function delete($object, $ids, api_session $session)
{
$deleteXml = "<delete><object>$object</object><keys>$ids</keys></delete>";
api_post::post($deleteXml, $session);
}
/**
* Run any Intacct API method not directly implemented in this class. You must pass
* valid XML for the method you wish to invoke.
*
* @param String $xml valid XML for the method you wish to invoke
* @param api_session $session an api_session instance with a valid connection
* @param string $dtdVersion Either "2.1" or "3.0" defaults to "3.0"
*
* @return String the XML response from Intacct
*/
public static function otherMethod($xml, api_session $session, $dtdVersion="3.0")
{
return api_post::post($xml, $session, $dtdVersion);
}
/**
* Run any Intacct API method not directly implemented in this class. You must pass
* valid XML for the method you wish to invoke.
*
* @param String $function for 2.1 function (create_sotransaction, etc)
* @param Array $phpObj an array for the object. Do not nest in another array() wrapper
* @param api_session $session an api_session instance with a valid connection
*
* @return String the XML response from Intacct
*/
public static function call21Method($function, $phpObj, api_session $session)
{
$xml = api_util::phpToXml($function, array($phpObj));
return api_post::post($xml, $session, "2.1");
}
/**
* Run any Intacct API method not directly implemented in this class. You must pass
* valid XML for the method you wish to invoke.
*
* @param Array $phpObj an array for all the functions .
* @param api_session $session an api_session instance with a valid connection
* @param string $dtdVersion DTD Version. Either "2.1" or "3.0". Defaults to "2.1"
*
* @return String the XML response from Intacct
*/
public static function sendFunctions($phpObj, api_session $session, $dtdVersion="2.1")
{
$xml = api_util::phpToXml('content', array($phpObj));
return api_post::post($xml, $session, $dtdVersion, true);
}
/**
* Get a list of standard objects by passing structured filters, sorts, and fields arguments.
*
* @param string $object the object to list
* @param Array $filter filters in a phpObj that will convert to get_list filters in phpToXml
* @param Array $sorts sorts in a phpObj that will convert to get_list sort in phpToXml
* @param Array $fields list of fields in a phpObj that will convert to get_list fields in phpToXml
* @param api_session $session an api_session instance with a valid connection
* @param string $dtdVersion DTD Version. Either "2.1" or "3.0". Defaults to "2.1"
*
* @return String the XML response from Intacct
*/
public static function get_list($object, $filter, $sorts, $fields, api_session $session, $dtdVersion="2.1")
{
$get_list = array();
$get_list['@object'] = $object;
if ($filter != null) {
$get_list['filter'] = $filter;
}
if ($sorts != null) {
$get_list['sorts'] = $sorts;
}
if ($fields != null) {
$get_list['fields'] = $fields;
}
$func['function'][] = array (
'@controlid' => 'control1',
'get_list' => $get_list
);
$xml = api_util::phpToXml('content', array($func));
$res = api_post::post($xml, $session, $dtdVersion, true);
$count = 0;
$ret = api_post::processListResults($res, api_returnFormat::PHPOBJ, $count);
$toReturn = $ret[$object];
if (is_array($toReturn)) {
$keys = array_keys($toReturn);
if (!is_numeric($keys[0])) {
$toReturn = array ($toReturn);
}
}
return $toReturn;
}
/**
* Handy wrapper for 2.1 update methods
*
* @param String $function for 2.1 function (create_sotransaction, etc)
* @param String $key The attribute key
* @param Array $phpObj an array for the object. Do not nest in another array() wrapper
* @param api_session $session an api_session instance with a valid connection
*
* @return String the XML response from Intacct
*/
public static function call21UpdateMethod($function, $key, $phpObj, api_session $session)
{
$xml = api_util::phpToXml($function, array($phpObj));
$xml = str_replace("<$function", "<$function key=\"$key\"", $xml);
return api_post::post($xml, $session, "2.1");
}
/**
* Return the records defined in a platform view. Views define an object, a collection of field, sorting,
* and filtering. You may pass additional filters via the api_viewFilters object
*
* @param String $viewName either the textual name of the view or the original id of the view
* (object#originalid). Note view names are not guaranteed to be unique, so you are always safer referencing the
* view using the notation[object]#[originalid]. Example: "CUSTOMER#123456@654321
* @param api_session $session instance of the api session object
* @param api_viewFilters $filterObj Object instance of the api_viewFilters object
* @param int $maxRecords defaults to 100000
* @param string $returnFormat String defaults to phpobj. Use one of the constants defined in
* api_returnFormat class
*
* @throws Exception
* @return Mixed Depends on the return format argument. Returns a string unless phpobj is the return format
* in which case returns an array
*/
public static function readView(
$viewName, api_session $session, api_viewFilters $filterObj=null, $maxRecords = self::DEFAULT_MAXRETURN,
$returnFormat = api_returnFormat::PHPOBJ
) {
$pageSize = ($maxRecords <= self::DEFAULT_PAGESIZE) ? $maxRecords : self::DEFAULT_PAGESIZE;
// set the return format
api_returnFormat::validateReturnFormat($returnFormat);
if ($returnFormat == api_returnFormat::PHPOBJ) {
$returnFormatArg = api_returnFormat::CSV;
} else {
$returnFormatArg = $returnFormat;
}
// process the filters array
$filtersXmlStr = '';
if ($filterObj !== null) {
$filters = $filterObj->filters;
$condition = $filterObj->operator;
$filtersXml = array();
foreach ($filters as $filter) {
$filtersXml[] = "<filterExpression><field>{$filter->field}</field><operator>{$filter->operator}</operator><value>{$filter->value}</value></filterExpression>";
}
$filtersXmlStr = "<filters><filterCondition>$condition</filterCondition>" . join("", $filtersXml) . "</filters>";
}
$viewName = HTMLSpecialChars($viewName);
$readXml="<readView><view>$viewName</view><pagesize>$pageSize</pagesize><returnFormat>$returnFormatArg</returnFormat>$filtersXmlStr</readView>";
$response = api_post::post($readXml, $session);
api_post::validateReadResults($response);
$phpobj = array(); $csv = ''; $json = ''; $xml = ''; $count = 0;
$$returnFormat = self::processReadResults($response, $count, $returnFormat);
if ($count == $pageSize && $count < $maxRecords) {
while (true) {
$readXml = "<readMore><view>$viewName</view></readMore>";
try {
$response = api_post::post($readXml, $session);
api_post::validateReadResults($response);
$pageCount = 0;
$page = self::processReadResults($response, $pageCount, $returnFormat);
$count += $pageCount;
if ($returnFormat == api_returnFormat::PHPOBJ) {
foreach ($page as $objRec) {
$phpobj[] = $objRec;
}
} elseif ($returnFormat == api_returnFormat::CSV) {
// append all but the first row to the CSV file
$page = explode("\n", $page);
array_shift($page);
$csv .= implode($page, "\n");
} elseif ($returnFormat == api_returnFormat::XML) {
// just add the xml string
$xml .= $page;
}
}
catch (Exception $ex) {
// for now, pass the exception on
Throw new Exception($ex);
}
if ($pageCount < $pageSize || $count >= $maxRecords) {
break;
}
}
}
return $$returnFormat;
}
/**
* Read records using a query. Specify the object you want to query and something like a "where" clause
*
* @param String $object the object upon which to run the query
* @param String $query the query string to execute. Use SQL operators
* @param String $fields A comma separated list of fields to return
* @param api_session $session An instance of the api_session object with a valid connection
* @param int $maxRecords number of records to return. Defaults to 100000
* @param string $returnFormat defaults to php object. Pass one of the valid constants from api_returnFormat class
*
* @return mixed either string or array of objects depending on returnFormat argument
*/
public static function readByQuery($object, $query, $fields, api_session $session, $maxRecords=self::DEFAULT_MAXRETURN, $returnFormat=api_returnFormat::PHPOBJ)
{
$pageSize = ($maxRecords <= self::DEFAULT_PAGESIZE) ? $maxRecords : self::DEFAULT_PAGESIZE;
if ($returnFormat == api_returnFormat::PHPOBJ) {
$returnFormatArg = api_returnFormat::CSV;
} else {
$returnFormatArg = $returnFormat;
}
// TODO: Implement returnFormat. Today we only support PHPOBJ
$query = HTMLSpecialChars($query);
$readXml = "<readByQuery><object>$object</object><query>$query</query><fields>$fields</fields><returnFormat>$returnFormatArg</returnFormat>";
$readXml .= "<pagesize>$pageSize</pagesize>";
$readXml .= "</readByQuery>";
$response = api_post::post($readXml, $session);
if ($returnFormatArg == api_returnFormat::CSV && trim($response) == "") {
// csv with no records will have no response, so avoid the error from validate and just return
return '';
}
api_post::validateReadResults($response);
$phpobj = array(); $csv = ''; $json = ''; $xml = '';
$$returnFormat = self::processReadResults($response, $thiscount, $returnFormat);
$totalcount = $thiscount;
// we have no idea if there are more if CSV is returned, so just check
// if the last count returned was $pageSize
while ($thiscount == $pageSize && $totalcount <= $maxRecords) {
$readXml = "<readMore><object>$object</object></readMore>";
try {
$response = api_post::post($readXml, $session);
api_post::validateReadResults($response);
$page = self::processReadResults($response, $pageCount, $returnFormat);
$totalcount += $pageCount;
$thiscount = $pageCount;
switch($returnFormat) {
case api_returnFormat::PHPOBJ:
foreach ($page as $objRec) {
$phpobj[] = $objRec;
}
break;
case api_returnFormat::CSV:
$page = explode("\n", $page);
array_shift($page);
$csv .= implode($page, "\n");
break;
case api_returnFormat::XML:
$xml .= $page;
break;
default:
throw new Exception("Invalid return format: " . $returnFormat);
break;
}
}
catch (Exception $ex) {
// we've probably exceeded the limit
break;
}
}
return $$returnFormat;
}
/**
* Inspect an object to get a list of its fields
*
* @param String $object The integration name of the object. Pass '*' to get a complete list of objects
* @param bool|String $detail Whether or not to return data type information for the fields.
* @param api_session $session Instance of an api_session object with a valid connection
*
* @return String the raw xml returned by Intacct
*/
public static function inspect($object, $detail, api_session $session)
{
$inspectXML = "<inspect detail='$detail'><object>$object</object></inspect>";
$objXml = api_post::post($inspectXML, $session);
$simpleXml = simplexml_load_string($objXml);
$objDefXml = $simpleXml->operation->result->data->Type;
$objDef = new api_objDef($objDefXml);
return $objDef;
//return $objAry;
}
/**
* Read an object by its name field (vid for standard objects)
*
* @param String $object object type
* @param String $name comma separated list of names.
* @param String $fields comma separated list of fields.
* @param api_session $session instance of api_session object.
*
* @return Array of objects. If only one name is passed, the fields will be directly accessible.
*/
public static function readByName($object, $name, $fields, api_session $session)
{
$name = HTMLSpecialChars($name);
$readXml = "<readByName><object>$object</object><keys>$name</keys><fields>$fields</fields><returnFormat>csv</returnFormat></readByName>";
$objCsv = api_post::post($readXml, $session);
if (trim($objCsv) == "") {
// csv with no records will have no response, so avoid the error from validate and just return
return '';
}
api_post::validateReadResults($objCsv);
$objAry = api_util::csvToPhp($objCsv);
if (count(explode(",", $name)) > 1) {
return $objAry;
} else {
return $objAry[0];
}
}
/**
* Reads all the records related to a source record through a named relationship.
*
* @param String $object the integration name of the object
* @param String $keys a comma separated list of 'id' values of the source records from which you want to read related records
* @param String $relation the name of the relationship. This will determine the type of object you are reading
* @param String $fields a comma separated list of fields to return
* @param api_session $session api_session object with valid connection
*
* @return Array of objects
*/
public static function readRelated($object, $keys, $relation, $fields, api_session $session)
{
$readXml = "<readRelated><object>$object</object><keys>$keys</keys><relation>$relation</relation><fields>$fields</fields><returnFormat>csv</returnFormat></readRelated>";
$objCsv = api_post::post($readXml, $session);
api_post::validateReadResults($objCsv);
$objAry = api_util::csvToPhp($objCsv);
return $objAry;
}
/**
* WARNING: This method will attempt to delete all records of a given object type
* Deletes first 10000 by default
*
* @param String $object object type
* @param api_session $session instance of api_session object.
* @param Integer $max [optional] Maximum number of records to delete. Default is 10000
*
* @return Integer count of records deleted
*/
public static function deleteAll($object, api_session $session, $max=10000)
{
// read all the record ids for the given object
$ids = api_post::readByQuery($object, "id > 0", "id", $session, $max);
if (!count($ids) > 0) {
return 0;
}
$count = 0;
$delIds = array();
foreach ($ids as $rec) {
$delIds[] = $rec['id'];
if (count($delIds) == 100) {
api_post::delete($object, implode(",", $delIds), $session);
$count += 100;
$delIds = array();
}
}
if (count($delIds) > 0) {
api_post::delete($object, implode(",", $delIds), $session);
$count += count($delIds);
}
return $count;
}
/**
* WARNING: This method will attempt to delete all records of a given object type given a query
* Deletes first 10000 by default
*
* @param String $object object type
* @param String $query the query string to execute. Use SQL operators
* @param api_session $session instance of api_session object.
* @param Integer $max [optional] Maximum number of records to delete. Default is 10000
*
* @return Integer count of records deleted
*/
public static function deleteByQuery($object, $query, api_session $session, $max=10000)
{
// read all the record ids for the given object
$ids = api_post::readByQuery($object, "id > 0 and $query", "id", $session, $max);
if ((!is_array($ids) && trim($ids) == '') || !count($ids) > 0) {
return 0;
}
$count = 0;
$delIds = array();
foreach ($ids as $rec) {
$delIds[] = $rec['id'];
if (count($delIds) == 100) {
api_post::delete($object, implode(",", $delIds), $session);
$count += 100;
$delIds = array();
}
}
if (count($delIds) > 0) {
api_post::delete($object, implode(",", $delIds), $session);
$count += count($delIds);
}
return $count;
}
/**
* Run a DDS job. Note that DDS is not GA yet
*
* @param api_session $session connected instance of api_session
* @param string $object object on which to run the job
* @param string $cloudDelivery Cloud delivery destination to which to deliver the results.
* @param string $jobType type of job: all or changes
* @param null $timestamp if changes, then the time stamp from which to pull
*
* @return String
*/
public static function runDdsJob(api_session $session, $object, $cloudDelivery, $jobType, $timestamp=null)
{
if ($jobType == self::DDS_JOBTYPE_ALL) {
$tsString = '';
} else if ($jobType == self::DDS_JOBTYPE_CHANGE) {
$tsString = "<timeStamp>" . date("c", strtotime($timestamp)) . "</timeStamp>";
} else {
throw new Exception ("Invalid job type. Use one of the DDS_JOBTYPE* constants.");
}
$runXml
= "<runDdsJob><object>$object</object><cloudDelivery>$cloudDelivery</cloudDelivery>
<jobType>$jobType</jobType>$tsString</runDdsJob>";
$response = api_post::post($runXml, $session);
// for now, call read on the key
$responseXml = simplexml_load_string($response);
/**
* @var simpleXmlElement $responseKey;
*/
$ddsJob = new api_ddsJob($responseXml->operation->result->data->ddsjob);
return $ddsJob;
}
/**
* Get a list of objects enabled for DDS. Note DDS is not enabled yet
*
* @param api_session $session Instance of connected api_session
*
* @return array List of objects supported by DDS
*/
public static function getDdsObjects(api_session $session)
{
$runXml = "<getDdsObjects/>";
$response = api_post::post($runXml, $session);
api_post::validateReadResults($response);
$simpleXml = simplexml_load_string($response);
$return = array();
$objects = $simpleXml->operation->result->data->DdsObjects->Objects;
foreach ($objects->Object as $object) {
$return[] = (string)$object;
}
return $return;
}
/**
* Get the DDL for creating the table for an object. Note, DDS is not enabled yet
*
* @param api_session $session instance of connected api_session
* @param string $object Name of object for which to retrieve DDS
*
* @return String
*/
public static function getDdsDdl(api_session $session, $object)
{
$runXml = "<getDdsDdl><object>$object</object></getDdsDdl>";
$response = api_post::post($runXml, $session);
api_post::validateReadResults($response);
$simpleXml = simplexml_load_string($response);
$ddl = (string)$simpleXml->operation->result->data->DdsDdl->Ddl;
return $ddl;
}
/**
* Get the effective list of permissions for a user, whether the company is configured for user-specific permissions
* or role-based permissions
*
* @param string $userId The User ID
* @param api_session $sess Connected api_session object
*
* @throws Exception
* @return api_userPermissions
*/
public static function getUserPermissions($userId, api_session $sess)
{
$runXml = "<getUserPermissions><userId>$userId</userId></getUserPermissions>";
$response = api_post::post($runXml, $sess, "2.1");
$respElem = new simpleXmlElement($response);
if ($respElem === false) {
throw new Exception("Invalid XML response in getUserPermissions.");
}
$permsElem = $respElem->operation->result->data;
return new api_userPermissions($permsElem);
}
/**
* Internal method for posting the invocation to the Intacct XML Gateway
*
* @param String $xml the XML request document
* @param api_session $session an api_session instance with an active connection
* @param string $dtdVersion Either "2.1" or "3.0". Defaults to "3.0"
* @param boolean $multiFunc whether or not this invocation calls multiple methods. Default is false
*
* @throws Exception
* @return String the XML response document
*/
private static function post($xml, api_session $session, $dtdVersion="3.0", $multiFunc=false)
{
$sessionId = $session->sessionId;
$endPoint = $session->endPoint;
$senderId = $session->senderId;
$senderPassword = $session->senderPassword;
$transaction = ( $session->transaction ) ? 'true' : 'false' ;
$templateHead =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<request>
<control>
<senderid>{$senderId}</senderid>
<password>{$senderPassword}</password>
<controlid>foobar</controlid>
<uniqueid>false</uniqueid>
<dtdversion>{$dtdVersion}</dtdversion>
{%validate}
</control>
<operation transaction='{$transaction}'>
<authentication>
<sessionid>{$sessionId}</sessionid>
</authentication>";
$contentHead =
"<content>
<function controlid=\"foobar\">";
$contentFoot =
"</function>
</content>";
$templateFoot =
"</operation>
</request>";
if (is_null($session->getResponseValidation())) {
$templateHead = str_replace("{%validate}", '', $templateHead);
} else {
$templateHead = str_replace(
"{%validate}", '<validate>' . $session->getResponseValidation() . '</validate>', $templateHead
);
}
if ($multiFunc) {
$xml = $templateHead . $xml . $templateFoot;
} else {
$xml = $templateHead . $contentHead . $xml . $contentFoot . $templateFoot;
}
if (self::$dryRun == true) {
self::$lastRequest = $xml;
return null;
}
$count = 0; // retry five times on too many operations
$res = "";
while (true) {
$res = api_post::execute($xml, $endPoint);
// If we didn't get a response, we had a poorly constructed XML request.
try {
api_post::validateResponse($res, $xml);
break;
}
catch (Exception $ex) {
if (strpos($ex->getMessage(), "too many operations") !== false) {
$count++;
if ($count >= 5) {
throw new Exception($ex);
}
} else {
throw new Exception($ex);
}
}
}
return $res;
}
/**
* You won't normally use this function, but if you just want to pass a fully constructed XML document
* to Intacct, then use this function.
*
* @param String $body a Valid XML string
* @param String $endPoint URL to post the XML to
*
* @throws exception
* @return String the raw XML returned by Intacct
*/
public static function execute($body, $endPoint)
{
self::$lastRequest = $body;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endPoint);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 3000); //Seconds until timeout
curl_setopt($ch, CURLOPT_POST, 1);
// TODO: Research and correct the problem with CURLOPT_SSL_VERIFYPEER
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // yahoo doesn't like the api.intacct.com CA
$body = "xmlrequest=" . urlencode($body);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
$error = curl_error($ch);
if ($error != "") {
throw new exception($error);
}
curl_close($ch);
self::$lastResponse = $response;
return $response;
}
/**
* Validate the response from Intacct and look for request level errors
*
* @param String $response The XML response document
*
* @return Array Array of errors encountered
* @throws Exception
*/
public static function findResponseErrors($response)
{
$errorArray = array();
// don't send errors to the log
libxml_use_internal_errors(true);
// the client asked for a non-xml response (csv or json)
$simpleXml = @simplexml_load_string($response);
if ($simpleXml === false) {
return null;
}
libxml_use_internal_errors(false);
// look for a failure in the operation, but not the result
if (isset($simpleXml->operation->errormessage)) {
$errorArray[] = array ( 'desc' => api_util::xmlErrorToString($simpleXml->operation->errormessage));
}
// if we didn't get an operation, the request failed and we should raise an exception
// with the error details
// did the method invocation fail?
if (!isset($simpleXml->operation)) {
if (isset($simpleXml->errormessage)) {
$errorArray[] = array ( 'desc' => api_util::xmlErrorToString($simpleXml->errormessage));
}
} else {
$results = $simpleXml->xpath('/response/operation/result');
foreach ($results as $result) {
if ((string)$result->status == "failure") {
$errorArray[] = array (
'controlid' => (string)$result->controlid,
'desc' => api_util::xmlErrorToString($result->errormessage)
);
}
}
}
return $errorArray;
}
/**
* Validate the response from Intacct and look for request level errors
*
* @param String $response The XML response document
*
* @throws Exception
* @return null
*/
private static function validateResponse($response)
{
// don't send errors to the log
libxml_use_internal_errors(true);
// the client asked for a non-xml response (csv or json)
$simpleXml = @simplexml_load_string($response);
if ($simpleXml === false) {
return;
}
libxml_use_internal_errors(false);
// look for a failure in the operation, but not the result
if (isset($simpleXml->operation->errormessage)) {
$error = $simpleXml->operation->errormessage->error[0];
throw new Exception("[ERROR: " . $error->errorno . "] " . $error->description2);
}
// if we didn't get an operation, the request failed and we should raise an exception
// with the error details
// did the method invocation fail?
if (!isset($simpleXml->operation)) {
if (isset($simpleXml->errormessage)) {
throw new Exception("[Error] " . api_util::xmlErrorToString($simpleXml->errormessage));
}
} else {
$results = $simpleXml->operation->result;
foreach ($results as $res) {
if ($res->status == "failure") {
throw new Exception("[Error] " . api_util::xmlErrorToString($res->errormessage));
}
}
}
return;
}
/**
* Parses the response document from update requests and returns an array of ids affected
*