Copy custom field values when copying Invoice

I’ve had the idea to copy custom field values when copying an Invoice. Tried to do it myself, but it failed.

Steps I did:

In application/modules/invoices/models/mdl_invoices.php (where public function copy_invoice begins) I added the following after the loop of $invoice_tax_rates:


    $invoice_custom_fields = $this->mdl_invoice_custom->where('invoice_id', $source_id)->get()->result();

    foreach ($invoice_custom_fields as $invoice_custom_field) {
        $db_array = array(
            'invoice_id' => $target_id,
            'invoice_custom_ansprechpartner' => $invoice_custom_field->invoice_custom_ansprechpartner,
            'invoice_custom_sonstiges' => $invoice_custom_field->invoice_custom_sonstiges,
            'invoice_custom_mahnungsdatum' => $invoice_custom_field->invoice_custom_mahnungsdatum

This throws no errors but it doesn’t copy the value of the fields either + I can’t do it like the example snippet. There will be more custom fields which I won’t have the name of if this goes to a production system.

Now I’m asking for help. Hope you guys can come up with a great solution that would fit, maybe give tips whatsoever. Would appreciate it. :smile:

you just forgot to save the values. To achieve this just add the following line after the foreach:

$this->mdl_invoice_custom->save($target_id, $db_array);

Or you can use the save_custom() method in mdl_invoice_custom model.

Cheers Lars

1 Like

I have the same problem: I need custom fields to be copied because they are containing important information.

Is it possible to get a little more information about using the “save_custom()” method?

I need custom fields to be copied when copying a document and (even more important) when automatically creating recurring documents.

Would be nice to have a solution that copies every custom field without the need to modify the code when a new custom field was created.

Thanks for any help and advice

Hi Visualcookie,

Pretty late reply, sorry - I hope you found a means to do this since you posted, anyway I’ll post a possible way to acheive the desired behaviour.

I’m new to InvoicePlane (1 day !) and I really appreciate it. As I was configuring/tweeking it to fit my needs, I bumped into a very similar question: I wanted to copy custom fields from a quote to an invoice when using the “quote to invoice” functionality. For instance, I have created for both quotes and invoices a custom field “project” that holds the project name (to be displayed in the quotes/invoices titles), and I don’t want to have to re-type it for each quote-to-invoice creation.

Here is what I did, and it works really well (latest version, 1.4.6) =>


        $quote_customs = $this->mdl_quote_custom->where('quote_id', $this->input->post('quote_id'))->get();

        if ($quote_customs->num_rows()) {
            $quote_customs = $quote_customs->row();
            unset($quote_customs->quote_id, $quote_customs->quote_custom_id);

            foreach ($quote_customs as $key => $val) {
                $db_array = array(
                    str_replace('quote', 'invoice', $key) => $val
                $this->mdl_invoice_custom->save_custom($invoice_id, $db_array);

I think that you can use the same basis for the invoice/quote copy method (just don’t use str_replace('quote', 'invoice', $key) => $val in the foreach section but just $key => $val)

This should work fine :smile:

As of the above example for the quote-to-invoice conversion, I have a perfect mapping (quote custom fields are all set up the same way for invoices), and I don’t think it would work properly if you have some quote custom fields that do not exist for invoices => in this case, a checking would be required before including the custom field into $db_array)

I hope that this helps!