quipuapp / quipu-api-php

Official PHP client for the Quipu API
GNU General Public License v3.0
0 stars 1 forks source link

Can't create Expense invoices #1

Closed nifunifatralara closed 7 years ago

nifunifatralara commented 7 years ago

When trying to crate Expense invoices, there's an issue with the Numeration Series. The API forces us to send a Quipu_Api_Numeration object, but Expenses invoices/tickets have no numeration.

I've modified class-quipu-api-invoice.php to accept a "kind" item in the attributes array of the invoice, and that seems to have been accepted but I'm stuck with the numeration series

        "data" => array(
            "type" => "invoices",
            "attributes" => array(
                "kind" => "$order[kind]",
                "number" => "$order[number]",
                "issue_date" => "$order[issue_date]",
                "paid_at" => "$order[paid_at]",
                "payment_method" => "$order[payment_method]"
            ),
            "relationships" => array(
                "contact" => array(
                    "data" => array(
                        "id" => "$contact_id",
                        "type" => "contacts"                        
                    )
                )
            )

If I send a numeration object (with the invoice number of the expense invoice I have) I get this error:

Message: POST: invoices - Error: not valid for expenses => /data/relationships/book_entry_numeration -

and if I send it empty (null), I get :

Message: POST: numbering_series - Error: Empty Response - Args: Array ( [data] => )

How are we supposed to create expense invoices?

nifunifatralara commented 7 years ago

and if I don't send a numeration object at all, the invoice creation just silently fails and does nothing

nifunifatralara commented 7 years ago

Just found a solution!

in class-quipu-api-invoice.php replace :

public function create_invoice($order) {
    $contact_id = $this->contact->get_id();
    $num_series_id = $this->num_series->get_id();

with :

public function create_invoice($order) {
    $contact_id = $this->contact->get_id();
    if ($order['kind'] == 'expenses') {
        $num_series_id = '';
    } else {
        $num_series_id = $this->num_series->get_id();
    }   

and then change $postData like this :

    $postData = array(
        "data" => array(
            "type" => "invoice",
            "attributes" => array(
                "kind" => "$order[kind]",
                "number" => "$order[number]",
                "issue_date" => "$order[issue_date]",
                "paid_at" => "$order[paid_at]",
                "payment_method" => "$order[payment_method]"
            ),
            "relationships" => array(
                "contact" => array(
                    "data" => array(
                        "id" => "$contact_id",
                        "type" => "contacts"                        
                    )
                )
            )
        )
    );

now the $order sent must have a "kind" parameter set ("income" or "expenses")

and you have to make sure you never call set_numeration() for invoices of type "expenses" when creating the invoice

    $quipu_invoice->set_contact($contact);

    if ($order['kind'] == 'income') {
        $quipu_invoice->set_numeration($numeration);
    }

    $quipu_invoice->create_invoice($order);
nifunifatralara commented 7 years ago

$order must also have a "number" set for expenses invoices to work (that number is the invoice number)

albertbellonch commented 7 years ago

Hi @nifunifatralara,

Thanks for contacting us. I'm happy that you found a solution so you can create expenses!

This first version of the PHP library for Quipu's API was developed with the purpose of allowing ecommerces' (WooCommerce & PrestaShop, for now) order synchronization to work with Quipu. We decided to just implement income invoices for now:

Although we will improve the PHP plugin (and finish developing the Ruby one) in the future, we do not have exact plans for that yet. Nevertheless, you can propose changes via pull requests and we will happily accept them.

Regarding this specific issue:

You are more than invited to issue a PR that handles both issues. :)

Best,

PS. You can refer to the API documentation for precise details.

albertbellonch commented 7 years ago

HI @nifunifatralara,

Just a clarification on the second part of this issue.

Number is not compulsory but, if it is not set, Quipu will not assign it by default. There are two different numbers for expenses:

I hope everything is clearer now.