29
29
use QuickBooksOnline \API \Data \IPPAttachable ;
30
30
use QuickBooksOnline \API \Data \IPPIntuitEntity ;
31
31
use QuickBooksOnline \API \Data \IPPTaxService ;
32
+ use QuickBooksOnline \API \Data \IPPid ;
32
33
use QuickBooksOnline \API \Exception \IdsException ;
33
34
use QuickBooksOnline \API \Exception \ServiceException ;
34
35
use QuickBooksOnline \API \Exception \IdsExceptionManager ;
@@ -685,7 +686,7 @@ public function FindById($entity, $Id = null)
685
686
throw new IdsException ('Argument Null Exception when calling FindById for Endpoint: ' . get_class ($ entity ));
686
687
}
687
688
$ this ->verifyOperationAccess ($ entity , __FUNCTION__ );
688
- $ entityId = $ entity ->Id ;
689
+ $ entityId = $ this -> getIDString ( $ entity ->Id ) ;
689
690
// Normal case
690
691
$ uri = implode (CoreConstants::SLASH_CHAR , array ('company ' , $ this ->serviceContext ->realmId , $ urlResource , $ entityId ));
691
692
// Send request
@@ -840,19 +841,22 @@ public function Upload($imgBits, $fileName, $mimeType, $objAttachable)
840
841
/**
841
842
* Returns PDF for entities which can be downloaded as PDF
842
843
* @param IPPIntuitEntity $entity
844
+ * @param Directory a writable directory for the PDF to be saved.
843
845
* @return boolean
844
846
* @throws IdsException, SdkException
845
847
*
846
848
*/
847
- public function DownloadPDF ($ entity )
849
+ public function DownloadPDF ($ entity, $ dir = null )
848
850
{
849
851
$ this ->validateEntityId ($ entity );
850
852
$ this ->verifyOperationAccess ($ entity , __FUNCTION__ );
851
853
854
+ //Find the ID
855
+ $ entityID = $ this ->getIDString ($ entity ->Id );
852
856
$ uri = implode (CoreConstants::SLASH_CHAR , array ('company ' ,
853
857
$ this ->serviceContext ->realmId ,
854
858
self ::getEntityResourceName ($ entity ),
855
- $ entity -> Id ,
859
+ $ entityID ,
856
860
CoreConstants::getType (CoreConstants::CONTENTTYPE_APPLICATIONPDF )));
857
861
$ requestParameters = $ this ->getGetRequestParameters ($ uri , CoreConstants::CONTENTTYPE_APPLICATIONPDF );
858
862
$ restRequestHandler = $ this ->getRestHandler ();
@@ -866,7 +870,7 @@ public function DownloadPDF($entity)
866
870
return null ;
867
871
} else {
868
872
$ this ->lastError = false ;
869
- return $ this ->processDownloadedContent (new ContentWriter ($ responseBody ), $ responseCode , $ this ->getExportFileNameForPDF ($ entity , "pdf " ));
873
+ return $ this ->processDownloadedContent (new ContentWriter ($ responseBody ), $ responseCode , $ this ->getExportFileNameForPDF ($ entity , "pdf " ), $ dir );
870
874
}
871
875
}
872
876
@@ -884,16 +888,17 @@ public function SendEmail($entity, $email = null)
884
888
$ this ->validateEntityId ($ entity );
885
889
$ this ->verifyOperationAccess ($ entity , __FUNCTION__ );
886
890
891
+ $ entityId =$ this ->getIDString ($ entity ->Id );
887
892
$ uri = implode (CoreConstants::SLASH_CHAR , array ('company ' ,
888
893
$ this ->serviceContext ->realmId ,
889
894
self ::getEntityResourceName ($ entity ),
890
- $ entity -> Id ,
895
+ $ entityId ,
891
896
'send ' ));
892
897
893
898
if (is_null ($ email )) {
894
- $ this ->logInfo ("Entity " . get_class ($ entity ) . " with id= " . $ entity -> Id . " is using default email " );
899
+ $ this ->logInfo ("Entity " . get_class ($ entity ) . " with id= " . $ entityId . " is using default email " );
895
900
} else {
896
- $ this ->logInfo ("Entity " . get_class ($ entity ) . " with id= " . $ entity -> Id . " is using $ email " );
901
+ $ this ->logInfo ("Entity " . get_class ($ entity ) . " with id= " . $ entityId . " is using $ email " );
897
902
if (!$ this ->verifyEmailAddress ($ email )) {
898
903
$ this ->logError ("Valid email is expected, but received $ email " );
899
904
throw new SdkException ("Valid email is expected, but received $ email " );
@@ -907,11 +912,11 @@ public function SendEmail($entity, $email = null)
907
912
* Retrieves specified entities based passed page number and page size and query
908
913
*
909
914
* @param string $query Query to issue
910
- * @param int $pageNumber Starting page number
911
- * @param int $pageSize Page size
915
+ * @param int $startPosition Starting page number
916
+ * @param int $maxResults Page size
912
917
* @return array Returns an array of entities fulfilling the query. If the response is Empty, it will return NULL
913
918
*/
914
- public function Query ($ query , $ pageNumber = 0 , $ pageSize = 500 )
919
+ public function Query ($ query , $ startPosition = null , $ maxResults = null )
915
920
{
916
921
$ this ->serviceContext ->IppConfiguration ->Logger ->RequestLog ->Log (TraceLevel::Info, "Called Method Query. " );
917
922
@@ -922,7 +927,7 @@ public function Query($query, $pageNumber = 0, $pageSize = 500)
922
927
}
923
928
924
929
$ httpsUri = implode (CoreConstants::SLASH_CHAR , array ('company ' , $ this ->serviceContext ->realmId , 'query ' ));
925
- $ httpsPostBody = $ query ;
930
+ $ httpsPostBody = $ this -> appendPaginationInfo ( $ query, $ startPosition , $ maxResults ) ;
926
931
927
932
$ requestParameters = $ this ->getPostRequestParameters ($ httpsUri , $ httpsContentType );
928
933
$ restRequestHandler = $ this ->getRestHandler ();
@@ -951,6 +956,38 @@ public function Query($query, $pageNumber = 0, $pageSize = 500)
951
956
}
952
957
}
953
958
959
+ /**
960
+ * Append the Pagination Data to the Query string if it is not appended
961
+ * @param Integer StartPostion
962
+ * @param Integer MaxResults
963
+ * @return String The query string
964
+ */
965
+ private function appendPaginationInfo ($ query , $ startPosition , $ maxResults ){
966
+ $ query = trim ($ query );
967
+ if (isset ($ startPosition ) && !empty ($ startPosition )){
968
+ if (stripos ($ query , "STARTPOSITION " ) === false ){
969
+ if (stripos ($ query , "MAXRESULTS " ) !== false ){
970
+ //In MaxResult is defined,we don't set startPosition
971
+ }else {
972
+ $ query = $ query . " " . "STARTPOSITION " . $ startPosition ;
973
+ }
974
+ }else {
975
+ //Ignore the startPosition if it is already used on the query
976
+ }
977
+ }
978
+
979
+ if (isset ($ maxResults ) && !empty ($ maxResults )){
980
+ if (stripos ($ query , "MAXRESULTS " ) === false ){
981
+ $ query = $ query . " " . "MAXRESULTS " . $ maxResults ;
982
+ }else {
983
+ //Ignore the maxResults if it is already used on the query
984
+ }
985
+ }
986
+
987
+ return $ query ;
988
+
989
+ }
990
+
954
991
/**
955
992
* Retrieves all entities by name
956
993
*
@@ -1292,7 +1329,7 @@ public function getExportFileNameForPDF($entity, $ext, $usetimestamp = true)
1292
1329
//TODO: add timestamp or GUID
1293
1330
$ this ->validateEntityId ($ entity );
1294
1331
1295
- return self ::getEntityResourceName ($ entity ) . "_ " . $ entity ->Id . ($ usetimestamp ? "_ " . time () : "" ) . ". $ ext " ;
1332
+ return self ::getEntityResourceName ($ entity ) . "_ " . $ this -> getIDString ( $ entity ->Id ) . ($ usetimestamp ? "_ " . time () : "" ) . ". $ ext " ;
1296
1333
}
1297
1334
1298
1335
/**
@@ -1315,11 +1352,13 @@ protected function getRestHandler()
1315
1352
* @param string $fileName
1316
1353
* @return mixed full path with filename or open handler
1317
1354
*/
1318
- protected function processDownloadedContent (ContentWriter $ writer , $ responseCode , $ fileName = null )
1355
+ protected function processDownloadedContent (ContentWriter $ writer , $ responseCode , $ fileName = null , $ dir )
1319
1356
{
1320
1357
$ writer ->setPrefix ($ this ->getPrefixFromSettings ());
1321
1358
try {
1322
- if ($ this ->isTempFile ()) {
1359
+ if (isset ($ dir ) && !empty ($ dir )){
1360
+ $ writer ->saveFile ($ dir , $ fileName );
1361
+ }else if ($ this ->isTempFile ()) {
1323
1362
$ writer ->saveTemp ();
1324
1363
} elseif ($ this ->isFileExport ()) {
1325
1364
$ writer ->saveFile ($ this ->getFileExportDir (), $ fileName );
@@ -1577,4 +1616,17 @@ public function getCompanyPreferences()
1577
1616
return $ parsedResponseBody ;
1578
1617
}
1579
1618
}
1619
+
1620
+ /**
1621
+ * Get the actual ID string value of either an IPPid object, or an Id string
1622
+ * @param Object $id
1623
+ * @return String Id
1624
+ */
1625
+ private function getIDString ($ id ){
1626
+ if ($ id instanceof IPPid || $ id instanceof QuickBooksOnline \API \Data \IPPid){
1627
+ return (String )$ id ->value ;
1628
+ }else {
1629
+ return (String )$ id ;
1630
+ }
1631
+ }
1580
1632
}
0 commit comments