Implement inventory features

If you want your app to utilize QuickBooks Online inventory features, set up this basic inventory implementation.

Step 1: Learn about basic accounting implementations

For your app’s implementations, make sure you pre-create entities so your app can reference them in transactions.

Transactions in QuickBooks frequently need to reference common entities like account, taxcode, and customer. If you have pre-created versions of common objects and entities, all your app needs to do is reference their IDs while creating transactions like invoices and bills.

If these entities don’t already exist, the transactions your app creates won’t have anything to reference. Referenced entities can’t be created on the fly within a transaction.

Step 2: Code the inventory implementation

This implementation generally does the following:


Inventory implementations

.NET

Java

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
//Initialize OAuth2RequestValidator and ServiceContext
ServiceContext serviceContext = base.IntializeContext(realmId);
DataService dataService = new DataService(serviceContext);

//create income account
Account newAccount = new Account{
    Name = "My "+type.ToString()+ random.Next(),
    AccountType = AccountTypeEnum.Income,
    AccountSubType = AccountSubTypeEnum.SalesOfProductIncome.ToString(),
    AccountTypeSpecified = true,
    SubAccountSpecified = true
};
Account addedIncomeAccount = dataService.Add<Account>(newAccount);

//create expense account
newAccount = new Account{
    Name = "My "+type.ToString()+ random.Next(),
    AccountType = AccountTypeEnum.CostofGoodsSold,
    AccountSubType = AccountSubTypeEnum.SuppliesMaterialsCogs.ToString(),
    AccountTypeSpecified = true,
    SubAccountSpecified = true
};
Account addedIncomeAccount = dataService.Add<Account>(newAccount);

//create asset account
Account newAccount = new Account{
    Name = "My "+type.ToString()+ random.Next(),
    AccountType = AccountTypeEnum.OtherAsset,
    AccountSubType = AccountSubTypeEnum.Inventory.ToString(),
    AccountTypeSpecified = true,
    SubAccountSpecified = true
};
Account addedIncomeAccount = dataService.Add<Account>(newAccount);

//create inventory item using income, expense, asset account
Item newItem = new Item{
    Type = ItemTypeEnum.Inventory,
    Name = "My Inventory 15"  +Guid.NewGuid().ToString("N"),
    QtyOnHand = 10,
    InvStartDate = DateTime.Today,
    Description = "New Inventory with quantity 10",
    TrackQtyOnHand = true,
    TypeSpecified = true,
    QtyOnHandSpecified = true,
    TrackQtyOnHandSpecified = true,
    InvStartDateSpecified = true
};
newItem.IncomeAccountRef = new ReferenceType(){Value = incomeAccount.Id};
newItem.ExpenseAccountRef = new ReferenceType(){Value = expenseAccount.Id};
newItem.AssetAccountRef = new ReferenceType(){Value = assetAccount.Id};
Item addedInventory = dataService.Add<Item>(newItem);

//create invoice for the inventory item
Invoice invoice = new Invoice();
invoice.CustomerRef = new ReferenceType(){Value = customer.Id};

List<Line> lineList = new List<Line>();
Line line = new Line();
line.Amount = new Decimal(100.00);
line.AmountSpecified = true;
lineList.Add(line);
invoice.Line = lineList.ToArray();
SalesItemLineDetail salesItemLineDetail = new SalesItemLineDetail();
salesItemLineDetail.Qty = new Decimal(1.0);
salesItemLineDetail.QtySpecified = true;
salesItemLineDetail.ItemRef = new ReferenceType(){Value = addedInventory.Id};
line.AnyIntuitObject = salesItemLineDetail;
ine.DetailType = LineDetailTypeEnum.SalesItemLineDetail;
line.DetailTypeSpecified = true;
Invoice addedInvoice = dataService.Add<Invoice>(invoice);

// Query inventory item - the quantity should be reduced
QueryService<Item> querySvcItem = new QueryService<Item>(serviceContext);
Item queryInventory = querySvcItem.ExecuteIdsQuery("SELECT * FROM Item WHERE Name = '"+addedInventory.Name+"'").FirstOrDefault();
Tip: Some methods, like creating or querying accounts, are omitted in the sample for brevity. Visit our GitHub to see the complete code.

 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
//create oauth object
OAuth2Authorizer oauth = new OAuth2Authorizer(accessToken);
//create context
Context context = new Context(oauth, ServiceType.QBO, realmId);
//create DataService
DataService service = new DataService(context);

//create income account
Account account = new Account();
account.setName("Income " + RandomStringUtils.randomAlphabetic(5));
account.setAccountType(AccountTypeEnum.INCOME);
account.setAccountSubType(AccountSubTypeEnum.SALES_OF_PRODUCT_INCOME.value());
Account incomeBankAccount = service.add(account);

//create expense account
account = new Account();
account.setName("Income " + RandomStringUtils.randomAlphabetic(5));
account.setAccountType(AccountTypeEnum.COST_OF_GOODS_SOLD);
account.setAccountSubType(AccountSubTypeEnum.SUPPLIES_MATERIALS_COGS.value());
Account expenseBankAccount = service.add(account);

//create asset account
account = new Account();
account.setName("Income " + RandomStringUtils.randomAlphabetic(5));
account.setAccountType(AccountTypeEnum.OTHER_CURRENT_ASSET);
account.setAccountSubType(AccountSubTypeEnum.INVENTORY.value());
Account assetAccount = service.add(account);

//create inventory item using income, expense, asset account
Item item = new Item();
item.setType(ItemTypeEnum.INVENTORY);
item.setName("Inventory Item " + RandomStringUtils.randomAlphanumeric(5));
item.setInvStartDate(new Date());
item.setQtyOnHand(BigDecimal.valueOf(10));
item.setTrackQtyOnHand(true);
item.setIncomeAccountRef(createRef(incomeBankAccount));
item.setExpenseAccountRef(createRef(expenseBankAccount));
item.setAssetAccountRef(createRef(assetAccount));
Item savedItem = service.add(item);

//create invoice for the inventory item
Invoice invoice = new Invoice();
invoice.setCustomerRef(createRef(customer)); //query or create a customer

List<Line> invLine = new ArrayList<Line>();
Line line = new Line();
line.setAmount(new BigDecimal("100"));
line.setDetailType(LineDetailTypeEnum.SALES_ITEM_LINE_DETAIL);
SalesItemLineDetail silDetails = new SalesItemLineDetail();
silDetails.setQty(BigDecimal.valueOf(1));
silDetails.setItemRef(createRef(savedItem));
line.setSalesItemLineDetail(silDetails);
invLine.add(line);
invoice.setLine(invLine);
service.add(invoice);

// Query inventory item - the quantity should be reduced
Item itemsRemaining = service.findById(savedItem);
Tip: Some methods, like creating or querying accounts, are omitted in the sample for brevity. Visit our GitHub to see the complete code.

 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
<?php
//create dataservice
$dataService->updateOAuth2Token($accessToken);

//create income account
    $incomeAccountRequestObj = Account::create([
    "AccountType" => INCOME_ACCOUNT_TYPE,
    "AccountSubType" => INCOME_ACCOUNT_SUBTYPE,
    "Name" => "IncomeAccount" . uniqid()
]);
$incomeAccount = $dataService->Add($incomeAccountRequestObj);

//create expense account
$expenseAccountRequestObj = Account::create([
    "AccountType" => EXPENSE_ACCOUNT_TYPE,
    "AccountSubType" => EXPENSE_ACCOUNT_SUBTYPE,
    "Name" => "ExpenseAccount" . uniqid()
]);
$expenseAccount = $dataService->Add($expenseAccountRequestObj);

//create asset account
$assetAccountRequestObj = Account::create([
    "AccountType" => ASSET_ACCOUNT_TYPE,
    "AccountSubType" => ASSET_ACCOUNT_SUBTYPE,
    "Name" => "AssetAccount-" . uniqid()
]);
$assetAccount = $dataService->Add($assetAccountRequestObj);

//create inventory item using income, expense, asset account
$itemCreateRequestObj = Item::create([
    "Name" => "Inventory Supplier Sample - " . uniqid(),
    "UnitPrice" => 10,
    "IncomeAccountRef" => ["value" => $incomeAccount->Id],
    "ExpenseAccountRef" => ["value" => $expenseAccount>Id],
    "AssetAccountRef" => ["value" => $expenseAccount>Id],
    "Type" => "Inventory",
    "TrackQtyOnHand" => true,
    "QtyOnHand" => 10,
    "InvStartDate" => "2018-04-01"
]);
$itemCreateResponseObj = $dataService->Add($itemCreateRequestObj);

//create invoice for the inventory item
$invoiceCreateRequestObj = Invoice::create([
    "CustomerRef" => ["value" => $customerObj>Id],
    "Line" => [[
        "Amount" => 20.00,
        "DetailType" => "SalesItemLineDetail",
        "SalesItemLineDetail" => ["ItemRef" => ["value" => $itemCreateResponseObj>Id]],
        "UnitPrice" => 10,
        "Qty" => 1
    ]]
]);
$invoiceCreateResponseObj = $dataService->Add($invoiceCreateRequestObj);

// Query inventory item - the quantity should be reduced
$itemReadResponseObj = $dataService->FindbyId('item', $itemCreateResponseObj->Id);
?>
Tip: Some methods, like creating or querying accounts, are omitted in the sample for brevity. Visit our GitHub to see the complete code.
Step 3: Set up other basic implementations

Depending on what basic QuickBooks features you want your app to utilize, you can set up other basic accounting implementations: