-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAccounting.php
163 lines (137 loc) · 4.88 KB
/
Accounting.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
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use QuickBooksOnline\API\DataService\DataService;
use QuickBooksOnline\API\Core\Http\Serialization\XmlObjectSerializer;
use QuickBooksOnline\API\Facades\Account;
use QuickBooksOnline\API\Facades\JournalEntry;
// 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 accounting()
{
// 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);
/*
* Usecase 1
* Generate names for the Bank Account and Credit Card Account
*/
$bankAccountRef = getBankAccountObj($dataService);
$creditCardAccountRef = getCreditCardAccountObj($dataService);
/*
* Usecase 2
* Make Journal Entry using the above two accounts created / Updated
*/
$theResourceObj = JournalEntry::create([
"Line" => [
[
"Id" => "0",
"Description" => "nov portion of rider insurance",
"Amount" => 100.0,
"DetailType" => "JournalEntryLineDetail",
"JournalEntryLineDetail" => [
"PostingType" => "Debit",
"AccountRef" => [
"value" => $bankAccountRef->Id ]
]
],
[
"Description" => "nov portion of rider insurance",
"Amount" => 100.0,
"DetailType" => "JournalEntryLineDetail",
"JournalEntryLineDetail" => [
"PostingType" => "Credit",
"AccountRef" => [
"value" => $creditCardAccountRef->Id ]
]
]
]
]);
$resultingObj = $dataService->Add($theResourceObj);
$result = json_encode($resultingObj, JSON_PRETTY_PRINT);
print "Created Id={$resultingObj->Id}. Reconstructed response body:\n\n";
print_r($result);
}
/*
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='" . 'Checking' . "'");
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
if (is_array($accountArray) && sizeof($accountArray) > 0) {
return current($accountArray);
}
}
// Create Expense Account
$bankAccountRequestObj = Account::create([
"AccountType" => 'Bank',
"AccountSubType" => 'Checking',
"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 an account of "Bank" type exists, if not, create one
*/
function getCreditCardAccountObj($dataService) {
$accountArray = $dataService->Query("select * from Account where AccountType='" . 'Credit Card' . "'");
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
if (is_array($accountArray) && sizeof($accountArray) > 0) {
return current($accountArray);
}
}
// Create Expense Account
$creditCardAccountRequestObj = Account::create([
"AccountType" => 'Credit Card',
"Name" => "CreditCardAccount-" . getGUID()
]);
$creditCardAccountResponseObj = $dataService->Add($creditCardAccountRequestObj);
$error = $dataService->getLastError();
if ($error) {
logError($error);
} else {
echo "Created Expense Account with Id={$creditCardAccountResponseObj->Id}.\n\n";
return $creditCardAccountResponseObj;
}
}
// 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 = accounting();
?>