-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathManagingInventory.php
292 lines (264 loc) · 9.36 KB
/
ManagingInventory.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
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use QuickBooksOnline\API\DataService\DataService;
//Import Facade classes you are going to use here
use QuickBooksOnline\API\Facades\Item;
use QuickBooksOnline\API\Facades\Invoice;
use QuickBooksOnline\API\Facades\Account;
use QuickBooksOnline\API\Facades\Customer;
const INCOME_ACCOUNT_TYPE = "Income";
const INCOME_ACCOUNT_SUBTYPE = "SalesOfProductIncome";
const EXPENSE_ACCOUNT_TYPE = "Cost of Goods Sold";
const EXPENSE_ACCOUNT_SUBTYPE = "SuppliesMaterialsCogs";
const ASSET_ACCOUNT_TYPE = "Other Current Asset";
const ASSET_ACCOUNT_SUBTYPE = "Inventory";
const CUSTOMER_NAME = "Bob Smith";
session_start();
/*
This function provides a runnable sample to understand
how QuickBooks Online APIs can be used to manage Inventory items
*/
function manageInventory()
{
// 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);
//Start write your business logic here
$dataService->throwExceptionOnError(true);
// Create Inventory Item with initial quantity on hand of 10
$itemCreateRequestObj = getItemCreateRequestObj($dataService);
$itemCreateResponseObj = $dataService->Add($itemCreateRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Item with Id={$itemCreateResponseObj->Id}. Reconstructed response body:\n\n";
}
//print result
print_r($itemCreateResponseObj);
// Create Invoice using above item and set the quantity of items to be used as numItems
$numItems = 2;
$invoiceCreateRequestObj = getInvoiceCreateRequestObj($dataService, $itemCreateResponseObj, $numItems);
$invoiceCreateResponseObj = $dataService->Add($invoiceCreateRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Invoice with Id={$invoiceCreateResponseObj->Id}. Reconstructed response body:\n\n";
}
//print result
print_r($invoiceCreateResponseObj);
// Read the above created Item again and validate that the "Quantity on hand" is reduced by $numItems
$itemReadResponseObj = $dataService->FindbyId('item', $itemCreateResponseObj->Id);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Read Item with Id={$itemReadResponseObj->Id}. Reconstructed response body:\n\n";
}
//print result
print_r($itemReadResponseObj);
print_r("Item Quantity on Hand = " . $itemReadResponseObj->QtyOnHand);
}
/*
Create and return an Item object of Type = Inventory
- The item name must be unique
- Sales items must have IncomeAccountRef
- Purchase items must have ExpenseAccountRef
- Reference to the Inventory Asset account that tracks the current value of the inventory (AssetAccountRef)
- Set TrackQtyOnHand to true to track quantity on hand
- Set QtyOnHand as current quantity of the Inventory items available for sale
- Set UnitPrioce as the monetary value of the service or product, as expressed in the home currency
- Set InvStartDate as the date of opening balance for the inventory transaction
*/
function getItemCreateRequestObj($dataService) {
// Fetch account Refs needed to create an Inventory Item
$incomeAccount = getIncomeAccountObj($dataService);
$expenseAccount = getExpenseAccountObj($dataService);
$assetAccount = getAssetAccountObj($dataService);
return Item::create([
"Name" => "Inventory Supplier Sample - " . uniqid(),
"UnitPrice" => 10,
"IncomeAccountRef" => [
"value" => $incomeAccount->Id,
"name" => $incomeAccount->Name
],
"ExpenseAccountRef" => [
"value" => $expenseAccount->Id,
"name" => $expenseAccount->Name
],
"AssetAccountRef" => [
"value" => $assetAccount->Id,
"name" => $assetAccount->Name
],
"Type" => "Inventory",
"TrackQtyOnHand" => true,
"QtyOnHand" => 10,
"InvStartDate" => "2018-04-01"
]);
}
/*
Create and return an Invoice object
- Must have at least one SalesItemLineDetail
- Must have a populated CustomerRef element referring to the customer who owes money
- Specify the ItemRef in SalesItemLineDetail to be the item that you just created above
- Specify the Qty (quantity) of items that are used as part of this Invoice creation
*/
function getInvoiceCreateRequestObj($dataService, $itemRef, $numItems) {
// Fetch Customer Ref needed to create this Invoice
$customerObj = getCustomerObj($dataService);
return Invoice::create([
"Line" => [
[
"Amount" => 20.00,
"DetailType" => "SalesItemLineDetail",
"SalesItemLineDetail" => [
"ItemRef" => [
"value" => $itemRef->Id,
"name" => $itemRef->Name
],
"UnitPrice" => 10,
"Qty" => $numItems,
]
]
],
"CustomerRef"=> [
"value"=> $customerObj->Id
]
]);
}
/*
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 (is_array($accountArray) && sizeof($accountArray) > 0) {
return current($accountArray);
}
}
// Create Income Account
$incomeAccountRequestObj = Account::create([
"AccountType" => INCOME_ACCOUNT_TYPE,
"AccountSubType" => INCOME_ACCOUNT_SUBTYPE,
"Name" => "IncomeAccount-" . uniqid()
]);
$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 (is_array($accountArray) && sizeof($accountArray) > 0) {
return current($accountArray);
}
}
// Create Expense Account
$expenseAccountRequestObj = Account::create([
"AccountType" => EXPENSE_ACCOUNT_TYPE,
"AccountSubType" => EXPENSE_ACCOUNT_SUBTYPE,
"Name" => "ExpenseAccount-" . uniqid()
]);
$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 (is_array($accountArray) && sizeof($accountArray) > 0) {
return current($accountArray);
}
}
// Create Asset Account
$assetAccountRequestObj = Account::create([
"AccountType" => ASSET_ACCOUNT_TYPE,
"AccountSubType" => ASSET_ACCOUNT_SUBTYPE,
"Name" => "AssetAccount-" . uniqid()
]);
$assetAccountObj = $dataService->Add($assetAccountRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Asset Account with Id={$assetAccountObj->Id}.\n\n";
return $assetAccountObj;
}
}
/*
Find if a customer with DisplayName of "Bob Smith" exists, if not, create one and return
*/
function getCustomerObj($dataService) {
$customerArray = $dataService->Query("select * from Customer where DisplayName='" . CUSTOMER_NAME . "'");
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
if (is_array($customerArray) && sizeof($customerArray) > 0) {
return current($customerArray);
}
}
// Create Customer
$customerRequestObj = Customer::create([
"DisplayName" => CUSTOMER_NAME
]);
$customerResponseObj = $dataService->Add($customerRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Customer with Id={$customerResponseObj->Id}.\n\n";
return $customerResponseObj;
}
}
function logError($error) {
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";
}
}
manageInventory();
?>