From b5c33b4fcbef837e72e22b078f331ecc23c4a1f5 Mon Sep 17 00:00:00 2001 From: jekkos-t520 Date: Thu, 12 Feb 2015 15:19:57 +0100 Subject: [PATCH] Add $YCO parameter (invoice numbering per year) --- application/controllers/sales.php | 7 +++++++ application/models/sale.php | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/application/controllers/sales.php b/application/controllers/sales.php index 2852a30a0..1fae1910a 100644 --- a/application/controllers/sales.php +++ b/application/controllers/sales.php @@ -279,11 +279,18 @@ class Sales extends Secure_area if (empty($invoice_number)) { $invoice_number=$this->config->config['sales_invoice_format']; + // don't query if this variable isn't used + if (strstr($invoice_number,'$YCO')) + { + $invoice_number_year=$this->Sale->get_invoice_number_for_year(date('Y')); + $invoice_number=str_replace('$YCO',$invoice_number_year,$invoice_number); + } } $invoice_count=$this->Sale->get_invoice_count(); $invoice_number=str_replace('$CO',$invoice_count,$invoice_number); $invoice_count=$this->Sale_suspended->get_invoice_count(); $invoice_number=str_replace('$SCO',$invoice_count,$invoice_number); + $invoice_number=strftime($invoice_number); $customer_id=$this->sale_lib->get_customer(); diff --git a/application/models/sale.php b/application/models/sale.php index 58bbcf4b3..470751fd9 100644 --- a/application/models/sale.php +++ b/application/models/sale.php @@ -22,6 +22,16 @@ class Sale extends CI_Model $this->db->where('invoice_number', $invoice_number); return $this->db->get(); } + + function get_invoice_number_for_year($year, $start_from = 0) + { + $this->db->select("COUNT( 1 ) AS invoice_number_year", FALSE); + $this->db->from('sales'); + $this->db->where("DATE_FORMAT(sale_time, '%Y' ) = ", $year, FALSE); + $this->db->where("invoice_number IS NOT ", "NULL", FALSE); + $result = $this->db->get()->row_array(); + return ($start_from + $result[ 'invoice_number_year' ] + 1); + } function exists($sale_id) {