forked from IntuitDeveloper/QBOConceptsTutorial-PHP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PayBill.php
345 lines (307 loc) · 11.8 KB
/
PayBill.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
<?php
/**
* <README:>
* Here is a quick tutorial to create a vendor, bill for a vendor, a billPayment for a bill and a vendorCredit for a vendor.
* 0. create necessay dependency entities: an expense account (for creating bill), a bank account (for creating billPayment) and a customer account (for creating vendorCredit)
* 1. Add a new vendor
* 2. Add a new bill item and set the value of vendorRef to be the Id of the vendor created in 1
* 3. Create a billPayment for the bill created in 2. We need to set value of vendorRef to be the Id of the vendor created in 1 and Line.LinkedTxn.TxnId to be the Id of the bill created in 2
* 4. Create a vendorCredit item for the vendor created in 1. We need to set value of vendorRef to be the Id of the vendor created in 1
* </README:>
*/
require_once(__DIR__ . '/vendor/autoload.php');
use QuickBooksOnline\API\DataService\DataService;
use QuickBooksOnline\API\Core\Http\Serialization\XmlObjectSerializer;
use QuickBooksOnline\API\Facades\Vendor;
use QuickBooksOnline\API\Facades\Bill;
use QuickBooksOnline\API\Facades\BillPayment;
use QuickBooksOnline\API\Facades\VendorCredit;
use QuickBooksOnline\API\Facades\Account;
use QuickBooksOnline\API\Facades\Customer;
session_start();
function payBill()
{
// 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 necessary entities for paying bills. We are ceate an expense account (for creating bill), a bank account (for creating billPayment) and a customer account (for creating vendorCredit).
*/
$accountExpenseRef = getExpenseAccountObj($dataService);
$bankaccountRef = getBankAccountObj($dataService);
$customerRef = getCustomerObj($dataService);
$vendorRef = getVendorObj($dataService);
/*
* To create a bill object, we need to provide at least one line(Individual line items of a transaction. Required fields include: Id, Amount and DetailType) and VendorRef (with id from the created vendor object above).(Refer to: https://developer.intuit.com/docs/api/accounting/vendor)
*/
$billCreate = Bill::create([
"Line" =>
[
[
"Id" => "1",
"Amount" => 200.00,
"DetailType" => "AccountBasedExpenseLineDetail",
"AccountBasedExpenseLineDetail" =>
[
"AccountRef" =>
[
"value" => $accountExpenseRef->Id
]
]
]
],
"VendorRef" =>
[
"value" =>$vendorRef->Id
]
]);
/*
* create a bill object via dataService.
*/
$bill = $dataService->Add($billCreate);
print_r("***************************************************\n");
$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 Bill=";
print_r($bill);
}
/*
* build a billPayment object for creation. A billPayment object represents the payment transaction for a bill that the business owner receives from a vendor for goods or services purchased from the vendor.
* To create a billPayment, we need to provide at least one line(Individual line items of a transaction. Required fields include: Id, Amount and DetailType), VendorRef (with id from the created vendor object above), PayType(Check, CreditCard), CheckPayment(if PayType is Check)/CreditCardPayment(if PayType is CreditCard), TotalAmt.(Refer to: https://developer.intuit.com/docs/api/accounting/billpayment)
*/
$billPayMentCreate = BillPayment::create([
"VendorRef" => [
"value" => $vendorRef->Id
],
"PayType" => "Check",
"CheckPayment" => [
"BankAccountRef" => [
"value" => $bankaccountRef->Id
]
],
"TotalAmt" => 200.00,
"PrivateNote" => "Acct. 1JK90",
"Line" => [
[
"Amount" => 200.00,
"LinkedTxn" => [
[
"TxnId" => $bill->Id,
"TxnType" => "Bill"
]
]
]
]
]);
/*
* create a billPayment object via dataService.
*/
$billPayment = $dataService->Add($billPayMentCreate);
print_r("***************************************************\n");
$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 BillPayment=";
print_r($billPayment);
}
/**
* Build a vendorCredit object. The vendorCredit object is an accounts payable transaction that represents a refund or credit of payment for goods or services. It is a credit that a vendor owes you for various reasons such as overpaid bill, returned merchandise, or other reasons.
* To create a vendorCredit, we need to provide at least one line(Individual line items of a transaction. Required fields include: Id, Amount and DetailType), VendorRef (with id from the created vendor object above).(Refer to: https://developer.intuit.com/docs/api/accounting/vendorcredit)
*/
$vendorCreditCreate= VendorCredit::create([
"TxnDate" => "2018-04-01",
"Line" => [
[
"Id" =>"1",
"Amount" => 90.00,
"DetailType" => "AccountBasedExpenseLineDetail",
"AccountBasedExpenseLineDetail" =>
[
"CustomerRef" =>
[
"value" =>$customerRef->Id
],
"AccountRef" =>
[
"value" =>$bankaccountRef->Id
],
"BillableStatus" => "Billable",
"TaxCodeRef" =>
[
"value" =>"TAX"
]
]
]
],
"VendorRef" =>
[
"value" => $vendorRef->Id
],
"TotalAmt" => 90.00
]);
/*
* create a vendorCredit object via dataService.
*/
$vendorCredit = $dataService->Add($vendorCreditCreate);
print_r("***************************************************\n");
$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 VendorCredit=";
print_r($vendorCredit);
}
}
/*
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 "Bank" type exists, if not, create one
*/
function getBankAccountObj($dataService) {
$accountArray = $dataService->Query("select * from Account where AccountType='" . 'Bank' . "' and AccountSubType='" . 'Bank' . "'");
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
if (sizeof($accountArray) > 0) {
return current($accountArray);
}
}
// Create Expense Account
$bankAccountRequestObj = Account::create([
"AccountType" => 'Bank',
"AccountSubType" => 'Bank',
"Name" => "BankAccount-" . getGUID()
]);
$bankAccountObj = $dataService->Add($bankAccountRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Expense Account with Id={$bankAccountObj->Id}.\n\n";
return $bankAccountObj;
}
}
/*
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 a Vendor exists
*/
function getVendorObj($dataService) {
$vendorName = 'Joes-Vendor';
$vendorArray = $dataService->Query("select * from Vendor where DisplayName='" . $vendorName . "'");
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
if (sizeof($vendorArray) > 0) {
return current($vendorArray);
}
}
// Create Customer
$vendorRequestObj = Customer::create([
"DisplayName" => $vendorName . getGUID()
]);
$vendorResponseObj = $dataService->Add($vendorRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Customer with Id={$vendorResponseObj->Id}.\n\n";
return $vendorResponseObj;
}
}
// 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 = payBill();
?>