public static function recalculate_invoice_stats($id)
 {
     $invoice = Invoice::get($id);
     if (!$invoice) {
         return;
     }
     if (!$invoice->get_meta('payment_date')) {
         return;
     }
     // calculate results (in base currency)
     $subtotal = $invoice->get_payment_subtotal(true);
     $tax = $invoice->get_payment_tax(true);
     $total = $invoice->get_payment_total(true);
     $fee_amount = $invoice->get_payment_fee_amount(true);
     // adjust global stats
     $global_stats = get_option('_easy_customer_invoices_global_stats', array());
     $global_stats = self::update_stats($global_stats, $id, $subtotal, $tax, $total, $fee_amount);
     update_option('_easy_customer_invoices_global_stats', $global_stats);
     // adjust year stats
     $year = $invoice->get_data('date', 'Y');
     $year_stats = get_option('_easy_customer_invoices_year_' . $year . '_stats', array());
     $year_stats = self::update_stats($year_stats, $id, $subtotal, $tax, $total, $fee_amount);
     update_option('_easy_customer_invoices_year_' . $year . '_stats', $year_stats);
     // adjust yearmonth stats
     $yearmonth = $invoice->get_data('date', 'Ym');
     $yearmonth_stats = get_option('_easy_customer_invoices_yearmonth_' . $yearmonth . '_stats', array());
     $yearmonth_stats = self::update_stats($yearmonth_stats, $id, $subtotal, $tax, $total, $fee_amount);
     update_option('_easy_customer_invoices_yearmonth_' . $yearmonth . '_stats', $yearmonth_stats);
     // adjust customer stats
     $customer = $invoice->get_customer();
     $customer_stats = get_post_meta($customer->get_ID(), '_easy_customer_invoices_stats', true);
     $customer_stats = self::update_stats($customer_stats, $id, $subtotal, $tax, $total, $fee_amount);
     update_post_meta($customer->get_ID(), '_easy_customer_invoices_stats', $customer_stats);
 }
 public function get_invoice($request)
 {
     $invoice = Entities\Invoice::get(absint($request['id']));
     if (null === $invoice) {
         return new WP_Error('wpeci_rest_invoice_invalid_id', __('Invalid invoice id.', 'easy-customer-invoices'), array('status' => 404));
     }
     return $invoice->prepare_for_api();
 }
 public function show_pdf_invoices($ids)
 {
     $pdf = new PDF(__('Invoices', 'easy-customer-invoices'));
     foreach ($ids as $id) {
         $invoice = Invoice::get($id);
         $pdf->render($invoice);
     }
     $pdf->finalize();
     exit;
 }