Skip to content

Commit

Permalink
Fix issue #78
Browse files Browse the repository at this point in the history
  • Loading branch information
hlu2 committed Aug 9, 2018
1 parent 8d51279 commit 797d1a2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Facades/FacadeClassMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ public static function EnumTypeMatch(){
'Gender' => 'gender',
'UseTimeEntry' => 'TimeEntryUsedForPaychecksEnum',
'SpecialItemType' => 'SpecialItemTypeEnum',
'TaxTypeApplicable' => 'TaxTypeApplicablityEnum'
'TaxTypeApplicable' => 'TaxTypeApplicablityEnum',
'PostingType' => 'PostingTypeEnum',
'JournalEntryType' => 'EntityTypeEnum'
];
}

Expand Down Expand Up @@ -179,7 +181,9 @@ public static function OtherAntiPatternNameEntity(){
'ItemAssemblyLine' => 'ItemComponentLine',
'SalesTaxRateList' => 'TaxRateList',
'PurchaseTaxRateList' => 'TaxRateList',
'AdjustmentTaxRateList' => 'TaxRateList'
'AdjustmentTaxRateList' => 'TaxRateList',
//Use JournalEntryEntity to replace Entity
'JournalEntryEntity' => 'EntityTypeRef'
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/Facades/FacadeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ public static function simpleAppendClassNameSpace($key){
private static function assignValue($targetObject, $key, $value){
//Reflection Class
$reflectionClassOfTargetObject = new \ReflectionClass($targetObject);
if(strcasecmp($key, "JournalEntryEntity") == 0){
$key = "Entity";
}else if(strcasecmp($key, "JournalEntryType") == 0){
$key = "Type";
}
$property = $reflectionClassOfTargetObject->getProperty($key);
if($property instanceof \ReflectionProperty){
$value = FacadeHelper::convertValueTypeToAppropriateString($value);
Expand Down
27 changes: 27 additions & 0 deletions src/Facades/JournalEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
class JournalEntry{

public static function create(array $data, $throwException = TRUE){
$data = JournalEntry::changeArrayName($data);
if(!isset($data) || empty($data)) throw new \Exception("Passed array for creating JournalEntry is Empty");
//QBO should not call the Entity as DataEnityt
$JournalEntryObject = FacadeHelper::reflectArrayToObject("JournalEntry", $data, $throwException );
return $JournalEntryObject;
}
Expand All @@ -13,6 +15,7 @@ public static function create(array $data, $throwException = TRUE){
* This is an immutable function
*/
public static function update($objToUpdate, array $data){
$data = JournalEntry::changeArrayName($data);
$classOfObj = get_class($objToUpdate);
if(strcmp($classOfObj, FacadeHelper::simpleAppendClassNameSpace("JournalEntry")) != 0){
throw new \Exception("Target object class:{" . $classOfObj . "} is not an instance of JournalEntry.");
Expand All @@ -23,4 +26,28 @@ public static function update($objToUpdate, array $data){
return $clonedOfObj;
}


private static function changeArrayName(&$array){
if(is_array($array)){
foreach($array as $key => &$value){
if(is_array($value)){
JournalEntry::changeArrayName($value);
}

if(strcmp($key, "Entity") == 0){
$array["JournalEntryEntity"] = $array["Entity"];
unset($array["Entity"]);
}

if(strcmp($key, "Type") == 0){
$array["JournalEntryType"] = $array["Type"];
unset($array["Type"]);
}

}
}

return $array;
}

}
78 changes: 78 additions & 0 deletions src/_Samples/JournalEntryCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
//Replace the line with require "vendor/autoload.php" if you are using the Samples from outside of _Samples folder
include('../config.php');


use QuickBooksOnline\API\DataService\DataService;
use QuickBooksOnline\API\Core\Http\Serialization\XmlObjectSerializer;
use QuickBooksOnline\API\Facades\JournalEntry;
// Prep Data Services
$dataService = DataService::Configure(array(
'auth_mode' => 'oauth2',
'ClientID' => "Q0fXL014zAv3wzmlhwXMEHTrKepfAshCRjztEu58ZokzCD5T7D",
'ClientSecret' => "stfnZfuSZUDay6cJSWtvQ9HkWiKFbcI9YuBTET5P",
'accessTokenKey' =>
'eyJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiZGlyIn0..qMhDp-FoKX9dtjmkN62gpQ.VmnY8diZN3FNxI5Wz1axd70pkexOZU9vrimA8PvGDdshOP9OmpA6TsqhxggYf631Q_77zGKDQW2-feNZFKan7jDakTsCUoZCYn9N3RRGy8CI4NVeGfgMJokqhK6rxxcajdjBqQ5EDS_uJKEAPUEfJ1JzEcEHF7Yyp0e6Mj13pkNtvFNTmyvmIoLOU9dfwWbDlvPxJFm_bpjPXM8m4-3wzIr0AXMDqjNlmCZpht9-W5r_UtkkZ66O4Ob06gFGt2t2NYU7jCrW_hOhQwM1urtRjusb9aTh7jElohX7yIipfWyU4nLUIC4QeZOOuKgphuYyej3PgbuoQQCgf0rEvyZ_gkpfmITB684m596RUhiVMvZYPPGd578qcMMwTawpf1fl9bh5p7uvS0izIoGAWWh-l16Gm8al2nJ9_iMIsnNpCPTFuVglu5fckCciByjlOz60OMJHWI0M3uhHu6-22VpUzNpOp3rZ6DDkeVLPWh6KVYDc2JeDJMHkt4TaDK0yb9Xu4qDY-ZPKXAY4eZocdZSTelmldTqXC1oq2E2-HmuGQRuMWdlMzM1UyF8GPlOztTwpuP9GSsfWZJ-TY85Hhv623zcmNYbk21c1YpGryJ-PN4KCRnjaLahlj2pBiPn4_HpYC9z_FrR2G1tzIR-RgZTq2qx47Ppuv5iAPWwQR222aKqVg2XruBsjusU7Ks0wLWp3.VzeZzhi19SkswkLvuXmw9g',
'refreshTokenKey' => "L011530994357pUIdF4rZSpMC5XCZ2TV4ypu4pOpfen4VRvYzl",
'QBORealmID' => "193514611894164",
'baseUrl' => "Development"
));
$dataService->setLogLocation("/Users/hlu2/Desktop/newFolderForLog");
$dataService->throwExceptionOnError(true);
$theResourceObj = JournalEntry::create([
"Line" => [
[
"Id" => "0",
"Description" => "nov portion of rider insurance",
"Amount" => 100.0,
"DetailType" => "JournalEntryLineDetail",
"JournalEntryLineDetail" => [
"PostingType" => "Debit",
"Entity" => [
"Type" => "Vendor",
"EntityRef" => [
"value" => "something",
"name" => "somethingelse"
]
],
"AccountRef" => [
"value" => "39",
"name" => "Opening Bal Equity"
]
]
],
[
"Description" => "nov portion of rider insurance",
"Amount" => 100.0,
"DetailType" => "JournalEntryLineDetail",
"JournalEntryLineDetail" => [
"PostingType" => "Credit",
"Entity" => [
"Type" => "Vendor",
"EntityRef" => [
"value" => "something",
"name" => "somethingelse"
]
],
"AccountRef" => [
"value" => "44",
"name" => "Notes Payable"
]
]
]
]
]);

var_dump($theResourceObj);
$resultingObj = $dataService->Add($theResourceObj);
$error = $dataService->getLastError();
if ($error) {
echo "The Status code is: " . $error->getHttpStatusCode() . "\n";
echo "The Helper message is: " . $error->getOAuthHelperError() . "\n";
echo "The Response message is: " . $error->getResponseBody() . "\n";
}
else {
echo "Created Id={$resultingObj->Id}. Reconstructed response body:\n\n";
$xmlBody = XmlObjectSerializer::getPostXmlFromArbitraryEntity($resultingObj, $urlResource);
echo $xmlBody . "\n";
}

0 comments on commit 797d1a2

Please sign in to comment.