Author Topic: Trigger nbill auto generate invoice  (Read 1175 times)

Offline jsneo

  • Full Member
  • ***
  • Posts: 28
    • View Profile
Trigger nbill auto generate invoice
« on: 06/June/2011, 07:42:20 PM »
Hi russell,

I am hacking directly into DT Register in order to have NBill create the corresponding order, invoice and income record whenever an user register for an event using DT Register.

Basically, the payment is being handled by DT Register and I will only require NBill to create the corresponding records as well as email the invoice to the user.

I have managed to create an order whenever a user register for an event in DT Register. However, instead of manually updating the invoice and income line. I wonder if there's any methods under nbf_cms::interops that allows me to trigger Nbill auto generate invoice and income functions?

I understand this is beyond the scope here but please help give me a pointer to the correct directions.

Would it be even possible to call nbf_cms::interops from another component like DT Register?
« Last Edit: 06/June/2011, 07:51:17 PM by jsneo »

Offline netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,955
    • View Profile
Re: Trigger nbill auto generate invoice
« Reply #1 on: 06/June/2011, 08:20:33 PM »
To be honest, I have never tried loading up the nBill environment from another component, and you are right, it falls outside the scope of normal support, but here's an idea of how you might be able to do it:

<?php
//Load environment
ob_start(); //Turn on output buffering so we can discard nBill's output
$_REQUEST['action']='about'; //We are going to spoof a call to the nBill about box just so we can load the environment (hopefully)
include_once('/path/to/your/site/administrator/components/com_nbill/admin.nbill.php');
ob_end_clean(); //Discard buffer

//Generate invoice(s) and email them to the client
include_once('/path/to/your/site/administrator/components/com_nbill/framework/classes/nbill.invoice.generator.class.php');
$document_ids = array();
nbf_generator::generate_invoices(array($order_id), $vendor_id, true, &$document_ids);

//Create income record(s)
$_REQUEST['document_id'] = implode(",", $document_ids);
$_REQUEST['action'] = 'income';
$_REQUEST['task'] = 'silent';
$_REQUEST['method'] = 'GG'; //Use an appropriate value from the jos_nbill_xref_payment_method table
$_REQUEST['reference'] = $ref; //Whatever reference you want to give the income (typically a transaction ID from the payment gateway)
$_REQUEST['notes'] = $notes' //Whatever you want to add to the notes section, if anything
$_REQUEST['return'] = 'http://www.address_to_return_to.com/index.php?option=com_mycomponent&etc'; //Perhaps a fly in the ointment - the income generator will redirect when it has finished, and there is no way around that - you will have to provide a URL to redirect to.
include_once('/path/to/your/site/administrator/components/com_nbill/admin.proc/income.php');
multi_generate_income();


Naturally if the above doesn't work (and it probably won't!), I can't really delve into a lengthy investigation, but it might be enough to get you started.

Offline jsneo

  • Full Member
  • ***
  • Posts: 28
    • View Profile
Re: Trigger nbill auto generate invoice
« Reply #2 on: 07/June/2011, 07:36:41 PM »
Hi Russell,

I am receiving the following page (screenshot) after inserting part of the code you gave. It looks like Nbill have some mechanism inbuilt to prevent third part components from accessing it. I suppose its part of the security design of it. Is there any ways that we may bypass that?

Offline netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,955
    • View Profile
Re: Trigger nbill auto generate invoice
« Reply #3 on: 07/June/2011, 10:01:06 PM »
No, that is the text from the about box. If you read the comments in the code I posted, I was suggesting making a call to the about box just for the purposes of loading the nBill environment, but discarding the output. The screenshot you attached is the output that needs discarding!

Offline jsneo

  • Full Member
  • ***
  • Posts: 28
    • View Profile
Re: Trigger nbill auto generate invoice
« Reply #4 on: 13/June/2011, 02:39:20 PM »
Hi Russell,

Thanks for the help so far. I have successfully create the invoice using the methods provided. However, there remains an error when calling the multi_generate_income() method. It appears that the method does not exists.

In additional, could you advise what is the file/class/method for sending of invoice?

Thanks

Offline netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,955
    • View Profile
Re: Trigger nbill auto generate invoice
« Reply #5 on: 13/June/2011, 03:31:58 PM »
However, there remains an error when calling the multi_generate_income() method. It appears that the method does not exists.
Ah yes, I am working with the next release - that function was not available in 2.1.1. I'm afraid you are stymied on that until the next version is released (I can't even give you a patch as there are other dependencies). I don't have a release date in mind yet. In the meantime, I think you would have to update the database directly yourself, sorry.

In additional, could you advise what is the file/class/method for sending of invoice?
The function is:

nbf_email::email_document($invoice_id);

...which is in the /administrator/components/com_nbill/framework/classes/nbill.email.class.php file.

Offline jsneo

  • Full Member
  • ***
  • Posts: 28
    • View Profile
Re: Trigger nbill auto generate invoice
« Reply #6 on: 13/June/2011, 04:29:29 PM »
Hi Russell,

Does nbf_generator::generate_invoices returns the invoice id? Secondly, how do i validate if the email is sent properly?

On another note, through the invoices are created, I can't see them under the user profile. Could you provide me some possible cause of this issue?
« Last Edit: 13/June/2011, 07:15:52 PM by jsneo »

Offline netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,955
    • View Profile
Re: Trigger nbill auto generate invoice
« Reply #7 on: 14/June/2011, 09:35:45 AM »
Does nbf_generator::generate_invoices returns the invoice id? Secondly, how do i validate if the email is sent properly?
The invoice (document) IDs (there could be more than one after calling that function) are returned in the 4th parameter. Here is the full function definition:
Code: [Select]
public static function generate_invoices($id_array = array(), $vendor_id, $paid_in_full = false, &$document_ids, $now = 0, $related_quote_id = 0, $delay_emails = false)
On another note, through the invoices are created, I can't see them under the user profile. Could you provide me some possible cause of this issue?
I can only suggest you create an invoice using nBill then compare the resulting database records with the ones generated and spot the difference.

Offline jsneo

  • Full Member
  • ***
  • Posts: 28
    • View Profile
Re: Trigger nbill auto generate invoice
« Reply #8 on: 14/June/2011, 12:17:33 PM »
Hi Russell,

I have tried triggering the send invoice using the document_id but to no avail.
Similarly, I am unable to see the invoice/orders under the user profile even for orders/invoices generated manually from the administrator console.

Offline netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,955
    • View Profile
Re: Trigger nbill auto generate invoice
« Reply #9 on: 14/June/2011, 01:15:37 PM »
Are you sure the invoice is associated with an entity ID that in turn is associated with a contact ID that in turn is associated with a Joomla user record?