XeroAPI / xero-php-oauth2

Xero PHP SDK for oAuth 2 generated from Xero API OpenAPI Spec 3.0
MIT License
91 stars 65 forks source link

Line item tracking category #209

Closed AndyTurff closed 3 years ago

AndyTurff commented 3 years ago

Hoping someone can help. I have been able to create invoices from the API on Xero fine. However I now need to add a tracking category (option and name) to line items and for the life of me I can't get the syntax right. Here is just one example of my attempts... $lineitemsX[$x] = new XeroAPI\XeroPHP\Models\Accounting\LineItem; $TrackingCategory[$x] = new XeroAPI\XeroPHP\Models\Accounting\TrackingCategory;

    $lineitemsX[$x]
        ->setDescription($partNum[$x])
        ->setQuantity($deliveryQty[$x])
        ->setUnitAmount($unit[$x])
        ->setAccountCode($vatCode)
                    ->setDiscountRate($discount)
        ->setTaxType($xeroCode[$x])

        ->TrackingCategory()->setName($dept[$x])
        ->TrackingCategory()->setOption($name[$x]);

Any help would be very much appreciated.

SidneyAllen commented 3 years ago

@AndyTurff - here is an example of how to add tracking options to line items

        $dateValue = new DateTime('2020-10-10');
        $dueDateValue = new DateTime('2020-10-28');
        $summarizeErrors = false;
        $unitdp = 2;
        $contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
        $contact->setContactID($contactID);

        $lineItemTracking = new XeroAPI\XeroPHP\Models\Accounting\LineItemTracking;
        $lineItemTracking->setTrackingCategoryID($oneTrackingCategoryId);
        $lineItemTracking->setTrackingOptionID($trackingOptionID);            
        $lineItemTrackings = [$lineItemTracking];

        $lineItem = new XeroAPI\XeroPHP\Models\Accounting\LineItem;
        $lineItem->setDescription('HelloWorld');
        $lineItem->setQuantity(1.0);
        $lineItem->setUnitAmount(20.0);
        $lineItem->setAccountCode('400');
        $lineItem->setTracking($lineItemTrackings);
        $lineItems = [];
        array_push($lineItems, $lineItem);

        $invoice = new XeroAPI\XeroPHP\Models\Accounting\Invoice;
        $invoice->setType(XeroAPI\XeroPHP\Models\Accounting\Invoice::TYPE_ACCREC);
        $invoice->setContact($contact);
        $invoice->setDate($dateValue);
        $invoice->setDate($dueDateValue);
        $invoice->setLineItems($lineItems);
        $invoice->setReference('Website Design 123');
        $invoice->setStatus(XeroAPI\XeroPHP\Models\Accounting\Invoice::STATUS_DRAFT);

        $invoices = new XeroAPI\XeroPHP\Models\Accounting\Invoices;
        $arr_invoices = [];
        array_push($arr_invoices, $invoice);
        $invoices->setInvoices($arr_invoices);

        try {
        $result = $apiInstance->createInvoices($xeroTenantId, $invoices, $summarizeErrors, $unitdp);
        } catch (Exception $e) {
        echo 'Exception when calling AccountingApi->createInvoices: ', $e->getMessage(), PHP_EOL;
        }
AndyTurff commented 3 years ago

Perfect. Works fine. Thank you so much SidneyAllen. I hadn't seen LineItemTracking documented anywhere so probably wouldn't have found the solution. Once again thank you for your help and rapid reply. Have a wonderful weekend.