public function set_parent($invoice_id)
 {
     if (!get_post($invoice_id)) {
         return false;
     }
     $invoice = wc_gzdp_get_invoice($invoice_id);
     update_post_meta($this->id, '_invoice_parent_id', $invoice->id);
     update_post_meta($this->id, '_invoice_parent_number', $invoice->get_number());
     update_post_meta($this->id, '_invoice_parent_title', $invoice->get_title());
     $this->parent = $invoice;
 }
function wc_gzdp_get_invoices_by_order($order, $type = false)
{
    $return = array();
    if ($order->invoices) {
        foreach ($order->invoices as $invoice) {
            $invoice_obj = wc_gzdp_get_invoice($invoice);
            if (!$invoice_obj || $type && !$invoice_obj->is_type($type)) {
                continue;
            }
            $return[$invoice] = $invoice_obj;
        }
    }
    return $return;
}
 /**
  * Check if we need to download a file and check validity
  */
 public static function download_invoice()
 {
     global $wp;
     if (isset($wp->query_vars['view-bill'])) {
         $invoice_id = absint($wp->query_vars['view-bill']);
         if (!empty($invoice_id)) {
             $invoice = wc_gzdp_get_invoice($invoice_id);
             $order_id = $invoice->order;
             if (!current_user_can('manage_woocommerce') && !current_user_can('view_order', $order_id)) {
                 wp_die(__('Cheatin huh?', 'woocommerce-germanized-pro'));
             }
             self::download($invoice);
         }
     }
 }
 /**
  * trigger function.
  *
  * @access public
  * @return void
  */
 function trigger($object)
 {
     if (!is_object($object)) {
         $object = get_post($object);
         if ($object->post_type == 'shop_order') {
             $object = wc_get_order($object->ID);
         } else {
             if ($object->post_type == 'invoice') {
                 $object = wc_gzdp_get_invoice($object->ID);
             }
         }
     }
     if (is_object($object)) {
         $this->object = $object;
         // Look for the actual invoice
         if ($object instanceof WC_Order) {
             if ($object->invoices) {
                 foreach ($object->invoices as $invoice) {
                     $invoice = wc_gzdp_get_invoice($invoice);
                     if ($invoice->is_type('simple')) {
                         $this->object = $invoice;
                         break;
                     }
                 }
             }
         }
         if (!$this->object instanceof WC_GZDP_Invoice) {
             return;
         }
         $recipient = $this->object->recipient;
         $this->recipient = $recipient['mail'];
         $this->find['order-date'] = '{order_date}';
         $this->find['order-number'] = '{order_number}';
         $this->find['invoice-number'] = '{invoice_number}';
         $order_data = $this->object->order_data;
         $this->replace['order-date'] = date_i18n(wc_date_format(), strtotime($order_data['date']));
         $this->replace['order-number'] = $this->object->order;
         $this->replace['invoice-number'] = $this->object->get_number(true);
         $this->object->mark_as_sent();
     }
     if (!$this->get_recipient()) {
         return;
     }
     $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
 }
 /**
  * trigger function.
  *
  * @access public
  * @return void
  */
 function trigger($object)
 {
     if (!is_object($object)) {
         $object = get_post($object);
         if ($object->post_type == 'shop_order') {
             $object = wc_get_order($object->ID);
         } else {
             if ($object->post_type == 'invoice') {
                 $object = wc_gzdp_get_invoice($object->ID);
             }
         }
     }
     if (is_object($object)) {
         $this->object = $object;
         // Look for the actual invoice
         if ($object instanceof WC_Order) {
             if ($object->invoices) {
                 foreach ($object->invoices as $invoice) {
                     $invoice = wc_gzdp_get_invoice($invoice);
                     if ($invoice->is_type('cancellation')) {
                         $this->object = $invoice;
                         break;
                     }
                 }
             }
         }
         if (!$this->object instanceof WC_GZDP_Invoice) {
             return;
         }
         $recipient = $this->object->recipient;
         $this->recipient = $recipient['mail'];
         $this->find['invoice_number_parent'] = '{invoice_number_parent}';
         $this->find['invoice-number'] = '{invoice_number}';
         $this->replace['invoice-number-parent'] = $this->object->parent->get_number(true);
         $this->replace['invoice-number'] = $this->object->get_number(true);
         $this->object->mark_as_sent();
     }
     if (!$this->get_recipient()) {
         return;
     }
     $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
 }
 public function save_invoices($post, $post_id)
 {
     if (!wp_verify_nonce($_REQUEST['wc_gzdp_invoice_data'], 'woocommerce_save_data')) {
         return;
     }
     $order = wc_get_order($post);
     $invoices = $_POST['invoice'];
     if (!empty($invoices)) {
         foreach ($invoices as $id) {
             $id = absint($id);
             if (empty($id)) {
                 continue;
             }
             $invoice = wc_gzdp_get_invoice($id);
             $data = array();
             if (!empty($_POST)) {
                 foreach ($_POST as $key => $field) {
                     if (!empty($field) && substr($key, 0, 8) == 'invoice_' && preg_replace("/[^0-9]/", "", $key) == $id) {
                         $data[substr(str_replace($id, '', $key), 0, -1)] = sanitize_text_field($field);
                     }
                 }
             }
             if (!isset($data['invoice_generate']) && $invoice->is_new()) {
                 continue;
             } else {
                 if (isset($data['invoice_send']) && $invoice->has_attachment()) {
                     $invoice->send_to_customer();
                     continue;
                 } else {
                     if (isset($data['invoice_delete'])) {
                         $invoice->delete(true);
                         continue;
                     }
                 }
             }
             $invoice->refresh($data, $order);
         }
     }
 }