forked from IntuitDeveloper/QBOConceptsTutorial-PHP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLandingTheJob.php
361 lines (317 loc) · 10.8 KB
/
LandingTheJob.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
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use QuickBooksOnline\API\DataService\DataService;
use QuickBooksOnline\API\Facades\Customer;
use QuickBooksOnline\API\Facades\Item;
use QuickBooksOnline\API\Facades\Estimate;
use QuickBooksOnline\API\Facades\Invoice;
use QuickBooksOnline\API\Facades\Account;
//Import Facade classes you are going to use here
//For example, if you need to use Customer, add
//use QuickBooksOnline\API\Facades\Customer;
session_start();
function landingTheJob()
{
/* This sample performs the folowing functions:
1. Add 1 customer
2. Add 1 item
3 Create Estimate
4. Update Amount in the Estimate
5. Convert Estimates to Invoices
6. Update invoice to add $5 discount
*/
// Create SDK instance
$config = include('config.php');
$dataService = DataService::Configure(array(
'auth_mode' => 'oauth2',
'ClientID' => $config['client_id'],
'ClientSecret' => $config['client_secret'],
'RedirectURI' => $config['oauth_redirect_uri'],
'scope' => $config['oauth_scope'],
'baseUrl' => "development"
));
/*
* Retrieve the accessToken value from session variable
*/
$accessToken = $_SESSION['sessionAccessToken'];
$dataService->throwExceptionOnError(true);
/*
* Update the OAuth2Token of the dataService object
*/
$dataService->updateOAuth2Token($accessToken);
/*
* Create Estimate Item using a Item Ref and Customer Ref
*/
$estimateRequestObj = getEstimateCreateRequestObj($dataService);
$estimateCreateResponseObj = $dataService->Add($estimateRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Estimate with Id={$estimateCreateResponseObj->Id}. Reconstructed response body:\n\n";
}
print_r($estimateCreateResponseObj);
/*
* Update Amount in the Estimate
*/
$estimateId = $estimateCreateResponseObj->Id;
$estimate = $this->dataService->FindbyId('estimate', $estimateId);
$estimateUpdateRequestObj = Estimate::update($estimate , [
"TotalAmt" => 200
]);
$estimateUpdateResponseObj = $this->dataService->Update($estimateUpdateRequestObj);
print_r($estimateUpdateResponseObj);
/*
* Convert Estimate to Invoice
*/
$theInvoiceRequestObj = Invoice::create([
"CustomerRef"=> $estimateUpdateResponseObj->CustomRef,
"LinkedTxn"=> [
"TxnId"=> $estimateId,
"TxnType"=> "Estimate"
],
"TotalAmt" => 100.00,
"Line" => $estimate->Line
]);
$resultingInvoiceResponseObj = $this->dataService->Add($theInvoiceRequestObj);
print_r($resultingInvoiceResponseObj);
/*
* Update Invoice to add 5$ Discount
*/
$theInvoiceResourceObj = Invoice::update($resultingInvoiceResponseObj, [
"Line" => [
[
"Amount" => 5,
"DetailType" => "DiscountLineDetail",
"DiscountLineDetail" => [
"PercentBased" => false,
]
]
]
]);
$theInvoiceResponseObj = $this->dataService->Update($theInvoiceResourceObj);
print_r($theInvoiceResponseObj);
}
/*
Create and return an Estimate object
- The item name must be unique
- The customer name must be unique
*/
function getEstimateCreateRequestObj($dataService) {
// Fetch Item and Customer Refs needed to create an Estimate
$itemRef = getItemObj($dataService);
$customerRef = getCustomerObj($dataService);
return Estimate::create([
"Line" => [
[
"Description" => "Used Car",
"Amount" => 100,
"DetailType" => "SalesItemLineDetail",
"SalesItemLineDetail" => [
"ItemRef" => [
"value" => $itemRef->Id,
"name" => $itemRef->Name
],
"UnitPrice" => 100,
"Qty" => 1,
"TaxCodeRef" => [
"value" => "NON"
]
]
],
[
"Amount" => 100,
"DetailType" => "SubTotalLineDetail",
"SubTotalLineDetail" => []
]
],
"TxnTaxDetail" => [
"TotalTax" => 0
],
"CustomerRef" => [
"value" => $customerRef->Id,
"name"=> $customerRef->Name
],
"CustomerMemo" => [
"value" => "Thank you for your business and have a great day!"
],
"TotalAmt" => 100,
"ApplyTaxAfterDiscount" => false,
"PrintStatus" => "NeedToPrint",
"EmailStatus" => "NotSet",
"BillEmail" => '[email protected]'
]);
}
function getItemObj($dataService) {
$itemName = 'Sample-Item';
$itemArray = $dataService->Query("select * from Item WHERE Name='" . $itemName . "'");
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
if (sizeof($itemArray) > 0) {
return current($itemArray);
}
}
// Fetch IncomeAccount, ExoenseAccount and AssetAccount Refs needed to create an Item
$incomeAccount = getIncomeAccountObj($dataService);
$expenseAccount = getExpenseAccountObj($dataService);
$assetAccount = getAssetAccountObj($dataService);
// Create Item
$dateTime = new \DateTime('NOW');
$ItemObj = Item::create([
"Name" => $itemName,
"Description" => "This is the sales description.",
"Active" => true,
"FullyQualifiedName" => "Office Supplies",
"Taxable" => true,
"UnitPrice" => 25,
"Type" => "Inventory",
"IncomeAccountRef"=> [
"value"=> $incomeAccount->Id
],
"PurchaseDesc"=> "This is the purchasing description.",
"PurchaseCost"=> 35,
"ExpenseAccountRef"=> [
"value"=> $expenseAccount->Id
],
"AssetAccountRef"=> [
"value"=> $assetAccount->Id
],
"TrackQtyOnHand" => true,
"QtyOnHand"=> 100,
"InvStartDate"=> $dateTime
]);
$resultingItemObj = $dataService->Add($ItemObj);
$itemId = $resultingItemObj->Id; // This needs to be passed in the Invoice creation later
echo "Created item Id={$itemId}. Reconstructed response body below:\n";
$result = json_encode($resultingItemObj, JSON_PRETTY_PRINT);
print_r($result . "\n\n\n");
return $resultingItemObj;
}
/*
Find if a customer with DisplayName if not, create one and return
*/
function getCustomerObj($dataService) {
$customerName = 'Bob-Smith';
$customerArray = $dataService->Query("select * from Customer where DisplayName='" . $customerName . "'");
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
if (sizeof($customerArray) > 0) {
return current($customerArray);
}
}
// Create Customer
$customerRequestObj = Customer::create([
"DisplayName" => $customerName . getGUID()
]);
$customerResponseObj = $dataService->Add($customerRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Customer with Id={$customerResponseObj->Id}.\n\n";
return $customerResponseObj;
}
}
/*
Find if an account of Income type exists, if not, create one
*/
function getIncomeAccountObj($dataService) {
$accountArray = $dataService->Query("select * from Account where AccountType='" . INCOME_ACCOUNT_TYPE . "' and AccountSubType='" . INCOME_ACCOUNT_SUBTYPE . "'");
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
if (sizeof($accountArray) > 0) {
return current($accountArray);
}
}
// Create Income Account
$incomeAccountRequestObj = Account::create([
"AccountType" => INCOME_ACCOUNT_TYPE,
"AccountSubType" => INCOME_ACCOUNT_SUBTYPE,
"Name" => "IncomeAccount-" . getGUID()
]);
$incomeAccountObject = $dataService->Add($incomeAccountRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Income Account with Id={$incomeAccountObject->Id}.\n\n";
return $incomeAccountObject;
}
}
/*
Find if an account of "Cost of Goods Sold" type exists, if not, create one
*/
function getExpenseAccountObj($dataService) {
$accountArray = $dataService->Query("select * from Account where AccountType='" . EXPENSE_ACCOUNT_TYPE . "' and AccountSubType='" . EXPENSE_ACCOUNT_SUBTYPE . "'");
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
if (sizeof($accountArray) > 0) {
return current($accountArray);
}
}
// Create Expense Account
$expenseAccountRequestObj = Account::create([
"AccountType" => EXPENSE_ACCOUNT_TYPE,
"AccountSubType" => EXPENSE_ACCOUNT_SUBTYPE,
"Name" => "ExpenseAccount-" . getGUID()
]);
$expenseAccountObj = $dataService->Add($expenseAccountRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Expense Account with Id={$expenseAccountObj->Id}.\n\n";
return $expenseAccountObj;
}
}
/*
Find if an account of "Other Current Asset" type exists, if not, create one
*/
function getAssetAccountObj($dataService) {
$accountArray = $dataService->Query("select * from Account where AccountType='" . ASSET_ACCOUNT_TYPE . "' and AccountSubType='" . ASSET_ACCOUNT_SUBTYPE . "'");
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
if (sizeof($accountArray) > 0) {
return current($accountArray);
}
}
// Create Asset Account
$assetAccountRequestObj = Account::create([
"AccountType" => ASSET_ACCOUNT_TYPE,
"AccountSubType" => ASSET_ACCOUNT_SUBTYPE,
"Name" => "AssetAccount-" . getGUID()
]);
$assetAccountObj = $dataService->Add($assetAccountRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Asset Account with Id={$assetAccountObj->Id}.\n\n";
return $assetAccountObj;
}
}
// Generate GUID to associate with the sample account names
function getGUID(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = // "{"
$hyphen.substr($charid, 0, 8);
return $uuid;
}
}
$result = landingTheJob();
?>