calcinai / xero-php

A php library for the Xero API, with a cleaner OAuth interface and ORM-like abstraction.
MIT License
359 stars 261 forks source link

Application::savePropertiesDirectly does not work with CreditNote Allocations #224

Open fabianhoward opened 7 years ago

fabianhoward commented 7 years ago

If one retrieves a CreditNote with an Allocation, adds a second one to it with CreditNote::addAllocation, and then saves it, a Request object is built which calls a PUT with the following body which includes the original Allocation.

<Allocations>
    <Allocation>
        <Invoice>
            <InvoiceNumber>INV-0283</InvoiceNumber>
            <InvoiceID>d035b0ee-3bc1-431f-85ee-a027f2502323</InvoiceID>
        </Invoice>
        <AppliedAmount>150</AppliedAmount>
        <Date>2017-02-07</Date>
    </Allocation>
    <Allocation>
        <Invoice>
            <Type>ACCREC</Type>
            <Contact>
                <ContactID>20b898ac-2cbe-4477-9427-9c554d6d99b5</ContactID>
                <ContactStatus>ACTIVE</ContactStatus>
                <Name>Default Organisation</Name>
                <Addresses>
                    <Address>
                        <AddressType>STREET</AddressType>
                    </Address>
                    <Address>
                        <AddressType>POBOX</AddressType>
                    </Address>
                </Addresses>
                <Phones>
                    <Phone>
                        <PhoneType>DDI</PhoneType>
                    </Phone>
                    <Phone>
                        <PhoneType>DEFAULT</PhoneType>
                    </Phone>
                    <Phone>
                        <PhoneType>FAX</PhoneType>
                    </Phone>
                    <Phone>
                        <PhoneType>MOBILE</PhoneType>
                    </Phone>
                </Phones>
                <IsSupplier>false</IsSupplier>
                <IsCustomer>true</IsCustomer>
                <UpdatedDateUTC>2017-02-03T12:57:29+00:00</UpdatedDateUTC>
                <Balances></Balances>
                <HasAttachments>false</HasAttachments>
            </Contact>
            <LineItems>
                <LineItem>
                    <Description>Predicted usage from last invoice</Description>
                    <Quantity>1</Quantity>
                    <UnitAmount>-150</UnitAmount>
                    <ItemCode>PU</ItemCode>
                    <AccountCode>200</AccountCode>
                    <TaxType>NONE</TaxType>
                </LineItem>
                <LineItem>
                    <Description>Seat 25/07/2016-25/07/2016 (at £50 per billing period)</Description>
                    <Quantity>3</Quantity>
                    <UnitAmount>1.6129032258065</UnitAmount>
                    <ItemCode>AU</ItemCode>
                    <AccountCode>200</AccountCode>
                    <TaxType>NONE</TaxType>
                </LineItem>
                <LineItem>
                    <Description>Seat 25/07/2016-31/07/2016 (at £40 per billing period)</Description>
                    <Quantity>3</Quantity>
                    <UnitAmount>9.0322580645161</UnitAmount>
                    <ItemCode>AU</ItemCode>
                    <AccountCode>200</AccountCode>
                    <TaxType>NONE</TaxType>
                </LineItem>
                <LineItem>
                    <Description>Seat 31/07/2016-31/07/2016 (at £1 per billing period)</Description>
                    <Quantity>3</Quantity>
                    <UnitAmount>0.032258064516129</UnitAmount>
                    <ItemCode>AU</ItemCode>
                    <AccountCode>200</AccountCode>
                    <TaxType>NONE</TaxType>
                </LineItem>
                <LineItem>
                    <Description>Seat 31/07/2016-24/08/2016 (at £100 per billing period)</Description>
                    <Quantity>3</Quantity>
                    <UnitAmount>80.645161290323</UnitAmount>
                    <ItemCode>AU</ItemCode>
                    <AccountCode>200</AccountCode>
                    <TaxType>NONE</TaxType>
                </LineItem>
                <LineItem>
                    <Description>Predicted usage for next billing period</Description>
                    <Quantity>1</Quantity>
                    <UnitAmount>300</UnitAmount>
                    <ItemCode>PU</ItemCode>
                    <AccountCode>200</AccountCode>
                    <TaxType>NONE</TaxType>
                </LineItem>
            </LineItems>
            <Date>2017-02-07</Date>
            <DueDate>2017-02-18</DueDate>
            <LineAmountTypes>Exclusive</LineAmountTypes>
            <InvoiceNumber>INV-0287</InvoiceNumber>
            <CurrencyCode>GBP</CurrencyCode>
            <CurrencyRate>1</CurrencyRate>
            <Status>AUTHORISED</Status>
            <SentToContact>false</SentToContact>
            <SubTotal>423.96</SubTotal>
            <TotalTax>0</TotalTax>
            <Total>423.96</Total>
            <InvoiceID>a14ff345-5d09-444a-b3a9-c4c19b83719a</InvoiceID>
            <AmountDue>423.96</AmountDue>
            <AmountPaid>0</AmountPaid>
            <UpdatedDateUTC>2017-02-07T16:18:58+00:00</UpdatedDateUTC>
        </Invoice>
        <AppliedAmount>105.60</AppliedAmount>
    </Allocation>
</Allocations>

which errors out with XeroPHP\Remote\Exception\BadRequestException: A validation exception occurred (Credit Note cannot be allocated as the allocation amount is greater than the amount remaining on the Credit Note)

I believe the problem is that the API only expects new Allocations and tries to recreate the existing one erroring out, which fits with the above error message since twice the original allocation + the new allocation is indeed larger that the total amount of the credit note.

calcinai commented 7 years ago

I'll try to find a way around this so you don't have to use that hack!