From a6b11e6f482bfb4641e14fb9e45bb484f71f15e1 Mon Sep 17 00:00:00 2001 From: Erastus Date: Fri, 31 Aug 2018 15:27:44 -0500 Subject: [PATCH] new discount on sales --- application/controllers/Config.php | 1 - application/controllers/Customers.php | 8 +- application/controllers/Receivings.php | 3 +- application/controllers/Reports.php | 10 +- application/controllers/Sales.php | 53 ++++------- application/language/en-US/config_lang.php | 2 +- application/language/en-US/customers_lang.php | 3 + .../language/en-US/receivings_lang.php | 2 +- application/language/en-US/reports_lang.php | 2 + application/language/es/config_lang.php | 3 +- application/language/es/customers_lang.php | 3 + application/language/es/receivings_lang.php | 2 +- application/libraries/Receiving_lib.php | 29 +++--- application/libraries/Sale_lib.php | 92 ++++++++----------- application/libraries/Tax_lib.php | 6 +- ...p => 20180820100000_discount_on_sales.php} | 4 +- .../migrations/sqlscripts/3.3.0_to_3.4.0.sql | 22 ----- .../sqlscripts/discount_on_sales.sql | 24 +++++ application/models/Customer.php | 5 +- application/models/Receiving.php | 13 ++- application/models/Sale.php | 24 ++--- .../models/reports/Detailed_receivings.php | 2 +- application/models/reports/Detailed_sales.php | 2 +- .../models/reports/Specific_discount.php | 17 ++-- .../models/reports/Specific_employee.php | 2 +- .../models/reports/Summary_discounts.php | 24 +++-- .../models/reports/Summary_payments.php | 2 +- application/models/reports/Summary_report.php | 2 +- application/models/reports/Summary_taxes.php | 8 +- application/views/configs/general_config.php | 37 +++----- application/views/customers/form.php | 48 ++++++---- application/views/receivings/receipt.php | 25 ++--- application/views/receivings/receiving.php | 30 +----- application/views/sales/invoice.php | 2 +- application/views/sales/invoice_email.php | 2 +- application/views/sales/quote.php | 2 +- application/views/sales/quote_email.php | 2 +- application/views/sales/receipt_default.php | 27 +++--- application/views/sales/receipt_email.php | 26 +++--- application/views/sales/receipt_short.php | 26 +++--- application/views/sales/register.php | 19 +--- application/views/sales/work_order.php | 2 +- application/views/sales/work_order_email.php | 2 +- import_customers.csv | 2 +- 44 files changed, 288 insertions(+), 334 deletions(-) rename application/migrations/{20180820100000_upgrade_to_3_4_0.php => 20180820100000_discount_on_sales.php} (61%) delete mode 100644 application/migrations/sqlscripts/3.3.0_to_3.4.0.sql create mode 100644 application/migrations/sqlscripts/discount_on_sales.sql diff --git a/application/controllers/Config.php b/application/controllers/Config.php index 673abf527..6bc838c7e 100644 --- a/application/controllers/Config.php +++ b/application/controllers/Config.php @@ -287,7 +287,6 @@ class Config extends Secure_Controller 'theme' => $this->input->post('theme'), 'default_sales_discount_type' => $this->input->post('default_sales_discount_type') != NULL, 'default_sales_discount' => $this->input->post('default_sales_discount'), - 'default_sales_discount_fixed' => $this->input->post('default_sales_discount_fixed'), 'enforce_privacy' => $this->input->post('enforce_privacy'), 'receiving_calculate_average_price' => $this->input->post('receiving_calculate_average_price') != NULL, 'lines_per_page' => $this->input->post('lines_per_page'), diff --git a/application/controllers/Customers.php b/application/controllers/Customers.php index 30b409c6d..68f0f5318 100644 --- a/application/controllers/Customers.php +++ b/application/controllers/Customers.php @@ -248,8 +248,8 @@ class Customers extends Persons 'consent' => $this->input->post('consent') != NULL, 'account_number' => $this->input->post('account_number') == '' ? NULL : $this->input->post('account_number'), 'company_name' => $this->input->post('company_name') == '' ? NULL : $this->input->post('company_name'), - 'discount_percent' => $this->input->post('discount_percent') == '' ? 0.00 : $this->input->post('discount_percent'), - 'discount_fixed' => $this->input->post('discount_fixed') == '' ? 0.00 : $this->input->post('discount_fixed'), + 'discount' => $this->input->post('discount') == '' ? 0.00 : $this->input->post('discount'), + 'discount_type' => $this->input->post('discount_type') == NULL ? PERCENT : $this->input->post('discount_type'), 'package_id' => $this->input->post('package_id') == '' ? NULL : $this->input->post('package_id'), 'taxable' => $this->input->post('taxable') != NULL, 'date' => $date_formatter->format('Y-m-d H:i:s'), @@ -404,8 +404,8 @@ class Customers extends Persons $customer_data = array( 'consent' => $consent, 'company_name' => $data[13], - 'discount_percent' => $data[15], - 'discount_fixed' => $data[16], + 'discount' => $data[15], + 'discount_type' => $data[16], 'taxable' => $data[17] == '' ? 0 : 1, 'date' => date('Y-m-d H:i:s'), 'employee_id' => $this->Employee->get_logged_in_employee_info()->person_id diff --git a/application/controllers/Receivings.php b/application/controllers/Receivings.php index ab77a5cfa..10eec315a 100644 --- a/application/controllers/Receivings.php +++ b/application/controllers/Receivings.php @@ -124,14 +124,13 @@ class Receivings extends Secure_Controller $price = parse_decimals($this->input->post('price')); $quantity = parse_decimals($this->input->post('quantity')); $discount = parse_decimals($this->input->post('discount')); - $discount_fixed = parse_decimals($this->input->post('discount_fixed')); $discount_type = parse_decimals($this->input->post('discount_type')); $item_location = $this->input->post('location'); $receiving_quantity = $this->input->post('receiving_quantity'); if($this->form_validation->run() != FALSE) { - $this->receiving_lib->edit_item($item_id, $description, $serialnumber, $quantity, $discount, $discount_fixed, $discount_type, $price, $receiving_quantity); + $this->receiving_lib->edit_item($item_id, $description, $serialnumber, $quantity, $discount, $discount_type, $price, $receiving_quantity); } else { diff --git a/application/controllers/Reports.php b/application/controllers/Reports.php index 703a3f521..3466d1ba3 100644 --- a/application/controllers/Reports.php +++ b/application/controllers/Reports.php @@ -884,7 +884,7 @@ class Reports extends Secure_Controller to_currency($drow['total']), to_currency($drow['cost']), to_currency($drow['profit']), - $drow['discount_percent'].'% | '.to_currency($drow['discount_fixed']))); + ($drow['discount_type'] == PERCENT)? $drow['discount'].'%':to_currency($drow['discount']))); } if(isset($report_data['rewards'][$key])) @@ -995,7 +995,7 @@ class Reports extends Secure_Controller to_currency($drow['total']), to_currency($drow['cost']), to_currency($drow['profit']), - $drow['discount_percent'].'% | '.to_currency($drow['discount_fixed']))); + ($drow['discount_type'] == PERCENT)? $drow['discount'].'%':to_currency($drow['discount']))); } if(isset($report_data['rewards'][$key])) @@ -1103,7 +1103,7 @@ class Reports extends Secure_Controller to_currency($drow['total']), to_currency($drow['cost']), to_currency($drow['profit']), - $drow['discount_percent'].'% | '.to_currency($drow['discount_fixed']))); + ($drow['discount_type'] == PERCENT)? $drow['discount'].'%':to_currency($drow['discount']))); } if(isset($report_data['rewards'][$key])) @@ -1256,7 +1256,7 @@ class Reports extends Secure_Controller to_currency($drow['total']), to_currency($drow['cost']), to_currency($drow['profit']), - $drow['discount_percent'].'% | '.to_currency($drow['discount_fixed']))); + ($drow['discount_type'] == PERCENT)? $drow['discount'].'%':to_currency($drow['discount']))); } if(isset($report_data['rewards'][$key])) @@ -1358,7 +1358,7 @@ class Reports extends Secure_Controller $drow['category'], $quantity_purchased, to_currency($drow['total']), - $drow['discount_percent'].'% | '.to_currency($drow['discount_fixed']))); + ($drow['discount_type'] == PERCENT)? $drow['discount'].'%':to_currency($drow['discount']))); } } diff --git a/application/controllers/Sales.php b/application/controllers/Sales.php index 32ea7350f..f157f45c0 100644 --- a/application/controllers/Sales.php +++ b/application/controllers/Sales.php @@ -136,13 +136,13 @@ class Sales extends Secure_Controller if($this->Customer->exists($customer_id)) { $this->sale_lib->set_customer($customer_id); - $discount_percent = $this->Customer->get_info($customer_id)->discount_percent; - $discount_fixed = $this->Customer->get_info($customer_id)->discount_fixed; + $discount = $this->Customer->get_info($customer_id)->discount; + $discount_type = $this->Customer->get_info($customer_id)->discount_type; // apply customer default discount to items that have 0 discount - if($discount_percent != '' OR $discount_fixed != '') + if($discount != '') { - $this->sale_lib->apply_customer_discount($discount_percent, $discount_fixed); + $this->sale_lib->apply_customer_discount($discount, $discount_type); } } @@ -368,7 +368,6 @@ class Sales extends Secure_Controller $data = array(); $discount = 0; - $discount_fixed = 0; $discount_type = $this->config->item('default_sales_discount_type'); // check if any discount is assigned to the selected customer @@ -376,18 +375,12 @@ class Sales extends Secure_Controller if($customer_id != -1) { // load the customer discount if any - $discount_percent = $this->Customer->get_info($customer_id)->discount_percent; - $discount_cash = $this->Customer->get_info($customer_id)->discount_fixed; - if($discount_percent != '') + $customer_discount = $this->Customer->get_info($customer_id)->discount; + $customer_discount_type = $this->Customer->get_info($customer_id)->discount_type; + if($customer_discount != '') { - $discount = $discount_percent; - $discount_type = 0; - } - - if($discount_cash != '') - { - $discount_fixed = $discount_cash; - $discount_type = 1; + $discount = $customer_discount; + $discount_type = $customer_discount_type; } } @@ -397,11 +390,6 @@ class Sales extends Secure_Controller $discount = $this->config->item('default_sales_discount'); } - if($discount_fixed == 0) - { - $discount_fixed = $this->config->item('default_sales_discount_fixed'); - } - $item_id_or_number_or_item_kit_or_receipt = $this->input->post('item'); $this->barcode_lib->parse_barcode_fields($quantity, $item_id_or_number_or_item_kit_or_receipt); $mode = $this->sale_lib->get_mode(); @@ -422,14 +410,10 @@ class Sales extends Secure_Controller $kit_price_option = $item_kit_info->price_option; $kit_print_option = $item_kit_info->print_option; // 0-all, 1-priced, 2-kit-only - if($item_kit_info->kit_discount_percent != 0 && $item_kit_info->kit_discount_percent > $discount) + if($item_kit_info->kit_discount != 0 && $item_kit_info->kit_discount > $discount) { - $discount = $item_kit_info->kit_discount_percent; - } - - if($item_kit_info->kit_discount_fixed != 0 && $item_kit_info->kit_discount_fixed > $discount_fixed) - { - $discount_fixed = $item_kit_info->kit_discount_fixed; + $discount = $item_kit_info->kit_discount; + $discount_type = $item_kit_info->discount_type; } $price = NULL; @@ -437,7 +421,7 @@ class Sales extends Secure_Controller if(!empty($kit_item_id)) { - if(!$this->sale_lib->add_item($kit_item_id, $quantity, $item_location, $discount, $discount_fixed, $discount_type, PRICE_MODE_STANDARD)) + if(!$this->sale_lib->add_item($kit_item_id, $quantity, $item_location, $discount, $discount_type, PRICE_MODE_STANDARD)) { $data['error'] = $this->lang->line('sales_unable_to_add_item'); } @@ -449,7 +433,7 @@ class Sales extends Secure_Controller // Add item kit items to order $stock_warning = NULL; - if(!$this->sale_lib->add_item_kit($item_id_or_number_or_item_kit_or_receipt, $item_location, $discount, $discount_fixed, $discount_type, $kit_price_option, $kit_print_option, $stock_warning)) + if(!$this->sale_lib->add_item_kit($item_id_or_number_or_item_kit_or_receipt, $item_location, $discount, $discount_type, $kit_price_option, $kit_print_option, $stock_warning)) { $data['error'] = $this->lang->line('sales_unable_to_add_item'); } @@ -460,7 +444,7 @@ class Sales extends Secure_Controller } else { - if(!$this->sale_lib->add_item($item_id_or_number_or_item_kit_or_receipt, $quantity, $item_location, $discount, $discount_fixed, $discount_type, PRICE_MODE_STANDARD)) + if(!$this->sale_lib->add_item($item_id_or_number_or_item_kit_or_receipt, $quantity, $item_location, $discount, $discount_type, PRICE_MODE_STANDARD)) { $data['error'] = $this->lang->line('sales_unable_to_add_item'); } @@ -479,14 +463,12 @@ class Sales extends Secure_Controller $this->form_validation->set_rules('price', 'lang:sales_price', 'required|callback_numeric'); $this->form_validation->set_rules('quantity', 'lang:sales_quantity', 'required|callback_numeric'); $this->form_validation->set_rules('discount', 'lang:sales_discount', 'required|callback_numeric'); - $this->form_validation->set_rules('discount_fixed', 'lang:sales_discount', 'required|callback_numeric'); $description = $this->input->post('description'); $serialnumber = $this->input->post('serialnumber'); $price = parse_decimals($this->input->post('price')); $quantity = parse_decimals($this->input->post('quantity')); $discount = parse_decimals($this->input->post('discount')); - $discount_fixed = parse_decimals($this->input->post('discount_fixed')); $discount_type = $this->input->post('discount_type'); $item_location = $this->input->post('location'); @@ -494,7 +476,7 @@ class Sales extends Secure_Controller if($this->form_validation->run() != FALSE) { - $this->sale_lib->edit_item($item_id, $description, $serialnumber, $quantity, $discount, $discount_fixed, $discount_type, $price, $discounted_total); + $this->sale_lib->edit_item($item_id, $description, $serialnumber, $quantity, $discount, $discount_type, $price, $discounted_total); } else { @@ -886,7 +868,8 @@ class Sales extends Secure_Controller $data['customer_location'] = ''; } $data['customer_account_number'] = $customer_info->account_number; - $data['customer_discount_percent'] = $customer_info->discount_percent; + $data['customer_discount'] = $customer_info->discount; + $data['customer_discount_type'] = $customer_info->discount_type; $package_id = $this->Customer->get_info($customer_id)->package_id; if($package_id != NULL) { diff --git a/application/language/en-US/config_lang.php b/application/language/en-US/config_lang.php index a1ac16e7b..c8ac08139 100644 --- a/application/language/en-US/config_lang.php +++ b/application/language/en-US/config_lang.php @@ -78,7 +78,7 @@ $lang["config_default_barcode_page_width_required"] = "Default Barcode Page Widt $lang["config_default_barcode_width_number"] = "Default Barcode Width must be a number."; $lang["config_default_barcode_width_required"] = "Default Barcode Width is a required field."; $lang["config_default_origin_tax_code"] = "Default Origin Tax Code"; -$lang["config_default_sales_discount"] = "Default Sales Discount %"; +$lang["config_default_sales_discount"] = "Default Sales Discount"; $lang["config_default_sales_discount_number"] = "Default Sales Discount must be a number."; $lang["config_default_sales_discount_required"] = "Default Sales Discount is a required field."; $lang["config_default_tax_name_number"] = "Default Tax Name must be a string."; diff --git a/application/language/en-US/customers_lang.php b/application/language/en-US/customers_lang.php index 8f3d9f26f..c98bbdcc6 100644 --- a/application/language/en-US/customers_lang.php +++ b/application/language/en-US/customers_lang.php @@ -48,3 +48,6 @@ $lang["customers_taxable"] = "Taxable"; $lang["customers_total"] = "Total spent"; $lang["customers_update"] = "Update Customer"; $lang["rewards_package"] = "Rewards Package"; +$lang["customers_discount_type"] = "Discount Type"; +$lang["customers_discount_fixed"] = "Fixed Discount"; +$lang["customers_discount_percent"] = "Percentage Discount"; \ No newline at end of file diff --git a/application/language/en-US/receivings_lang.php b/application/language/en-US/receivings_lang.php index 0f4138555..ee234e76a 100644 --- a/application/language/en-US/receivings_lang.php +++ b/application/language/en-US/receivings_lang.php @@ -12,7 +12,7 @@ $lang["receivings_date"] = "Receiving Date"; $lang["receivings_date_required"] = "A correct date must be entered."; $lang["receivings_date_type"] = "Date is a required field."; $lang["receivings_delete_entire_sale"] = "Delete Entire Sale"; -$lang["receivings_discount"] = "Discount %"; +$lang["receivings_discount"] = "Discount"; $lang["receivings_edit"] = "Edit"; $lang["receivings_edit_sale"] = "Edit Receiving"; $lang["receivings_employee"] = "Employee"; diff --git a/application/language/en-US/reports_lang.php b/application/language/en-US/reports_lang.php index 4563efcb4..8d03ffa46 100644 --- a/application/language/en-US/reports_lang.php +++ b/application/language/en-US/reports_lang.php @@ -123,3 +123,5 @@ $lang["reports_unit_price"] = "Retail Price"; $lang["reports_used"] = "Points Used"; $lang["reports_work_orders"] = "Work Orders"; $lang["reports_zero_and_less"] = "Zero and less"; +$lang["reports_discount_type"] = "Discount Type"; +$lang["reports_discount_fixed"] = "Fixed Discount"; \ No newline at end of file diff --git a/application/language/es/config_lang.php b/application/language/es/config_lang.php index f23f04eba..c16e03c01 100644 --- a/application/language/es/config_lang.php +++ b/application/language/es/config_lang.php @@ -78,7 +78,7 @@ $lang["config_default_barcode_page_width_required"] = "Ancho de página del cód $lang["config_default_barcode_width_number"] = "Ancho del código de barras debe ser número."; $lang["config_default_barcode_width_required"] = "Ancho del código de barras es requerido."; $lang["config_default_origin_tax_code"] = "Código de impuesto por defecto"; -$lang["config_default_sales_discount"] = "% de Descuento en Ventas predeterminado"; +$lang["config_default_sales_discount"] = "Descuento en Ventas predeterminado"; $lang["config_default_sales_discount_number"] = "Descuento en ventas predeterminado debe ser un número."; $lang["config_default_sales_discount_required"] = "Descuento en ventas predeterminado es requerido."; $lang["config_default_tax_name_number"] = "El nombre de el impuesto debe ser letras."; @@ -271,4 +271,3 @@ $lang["config_top"] = "Arriba"; $lang["config_website"] = "Sitio Web"; $lang["config_work_order_enable"] = "Soporte Ordenes de Trabajo"; $lang["config_work_order_format"] = "Formato Ordenes de trabajo"; -$lang["config_default_sales_discount_type"] = "Descuento Fijo en Ventas predeterminado"; \ No newline at end of file diff --git a/application/language/es/customers_lang.php b/application/language/es/customers_lang.php index b91aaeb99..86dd81f9c 100644 --- a/application/language/es/customers_lang.php +++ b/application/language/es/customers_lang.php @@ -48,3 +48,6 @@ $lang["customers_taxable"] = "Gravable"; $lang["customers_total"] = "Total"; $lang["customers_update"] = "Actualizar Cliente"; $lang["rewards_package"] = "Paquete de premios"; +$lang["customers_discount_type"] = "Tipo de Descuento"; +$lang["customers_discount_fixed"] = "Descuento Fijo"; +$lang["customers_discount_percent"] = "Descuento en Porcentaje"; diff --git a/application/language/es/receivings_lang.php b/application/language/es/receivings_lang.php index c64755aed..b9f5227d4 100644 --- a/application/language/es/receivings_lang.php +++ b/application/language/es/receivings_lang.php @@ -12,7 +12,7 @@ $lang["receivings_date"] = "Fecha de Recepción"; $lang["receivings_date_required"] = "Una fecha correcta debe ser ingresada."; $lang["receivings_date_type"] = "Campo Fecha es requerido."; $lang["receivings_delete_entire_sale"] = "Borrar venta completa"; -$lang["receivings_discount"] = "Descuento %"; +$lang["receivings_discount"] = "Descuento"; $lang["receivings_edit"] = "Editar"; $lang["receivings_edit_sale"] = "Editar Recepción"; $lang["receivings_employee"] = "Empleado"; diff --git a/application/libraries/Receiving_lib.php b/application/libraries/Receiving_lib.php index 5e78c9df3..218cc1242 100644 --- a/application/libraries/Receiving_lib.php +++ b/application/libraries/Receiving_lib.php @@ -159,7 +159,7 @@ class Receiving_lib $this->CI->session->unset_userdata('recv_stock_destination'); } - public function add_item($item_id, $quantity = 1, $item_location = NULL, $discount_type = 0, $discount = 0, $discount_fixed = 0, $price = NULL, $description = NULL, $serialnumber = NULL, $receiving_quantity = NULL, $include_deleted = FALSE) + public function add_item($item_id, $quantity = 1, $item_location = NULL, $discount_type = 0, $discount = 0, $price = NULL, $description = NULL, $serialnumber = NULL, $receiving_quantity = NULL, $include_deleted = FALSE) { //make sure item exists in database. if(!$this->CI->Item->exists($item_id, $include_deleted)) @@ -242,13 +242,12 @@ class Receiving_lib 'is_serialized' => $item_info->is_serialized, 'quantity' => $quantity, 'discount' => $discount, - 'discount_fixed' => $discount_fixed, 'discount_type' => $discount_type, 'in_stock' => $this->CI->Item_quantity->get_item_quantity($item_id, $item_location)->quantity, 'price' => $price, 'receiving_quantity' => $receiving_quantity, 'receiving_quantity_choices' => $receiving_quantity_choices, - 'total' => $this->get_item_total($quantity, $price, $discount, $discount_fixed, $receiving_quantity) + 'total' => $this->get_item_total($quantity, $price, $discount, $discount_type, $receiving_quantity) ) ); @@ -256,7 +255,7 @@ class Receiving_lib if($itemalreadyinsale) { $items[$updatekey]['quantity'] += $quantity; - $items[$updatekey]['total'] = $this->get_item_total($items[$updatekey]['quantity'], $price, $discount, $discount_fixed, $items[$updatekey]['receiving_quantity']); + $items[$updatekey]['total'] = $this->get_item_total($items[$updatekey]['quantity'], $price, $discount, $discount_type, $items[$updatekey]['receiving_quantity']); } else { @@ -269,7 +268,7 @@ class Receiving_lib return TRUE; } - public function edit_item($line, $description, $serialnumber, $quantity, $discount, $discount_fixed, $discount_type, $price, $receiving_quantity) + public function edit_item($line, $description, $serialnumber, $quantity, $discount, $discount_type, $price, $receiving_quantity) { $items = $this->get_cart(); if(isset($items[$line])) @@ -280,12 +279,11 @@ class Receiving_lib $line['quantity'] = $quantity; $line['receiving_quantity'] = $receiving_quantity; $line['discount'] = $discount; - $line['discount_fixed'] = $discount_fixed; if(!is_null($discount_type)){ $line['discount_type'] = $discount_type; } $line['price'] = $price; - $line['total'] = $this->get_item_total($quantity, $price, $discount, $discount_fixed, $receiving_quantity); + $line['total'] = $this->get_item_total($quantity, $price, $discount, $discount_type, $receiving_quantity); $this->set_cart($items); } @@ -318,7 +316,7 @@ class Receiving_lib foreach($this->CI->Receiving->get_receiving_items($receiving_id)->result() as $row) { - $this->add_item($row->item_id, -$row->quantity_purchased, $row->item_location, $row->discount_type, $row->discount_percent, $row->discount_fixed, $row->item_unit_price, $row->description, $row->serialnumber, $row->receiving_quantity, TRUE); + $this->add_item($row->item_id, -$row->quantity_purchased, $row->item_location, $row->discount_type, $row->discount, $row->item_unit_price, $row->description, $row->serialnumber, $row->receiving_quantity, TRUE); } $this->set_supplier($this->CI->Receiving->get_supplier($receiving_id)->person_id); @@ -343,7 +341,7 @@ class Receiving_lib foreach($this->CI->Receiving->get_receiving_items($receiving_id)->result() as $row) { - $this->add_item($row->item_id, $row->quantity_purchased, $row->item_location, $row->discount_type, $row->discount_percent, $row->discount_fixed, $row->item_unit_price, $row->description, $row->serialnumber, $row->receiving_quantity, TRUE); + $this->add_item($row->item_id, $row->quantity_purchased, $row->item_location, $row->discount_type, $row->discount, $row->item_unit_price, $row->description, $row->serialnumber, $row->receiving_quantity, TRUE); } $this->set_supplier($this->CI->Receiving->get_supplier($receiving_id)->person_id); @@ -359,12 +357,17 @@ class Receiving_lib $this->clear_reference(); } - public function get_item_total($quantity, $price, $discount_percentage, $discount_fixed, $receiving_quantity) + public function get_item_total($quantity, $price, $discount, $discount_type, $receiving_quantity) { $extended_quantity = bcmul($quantity, $receiving_quantity); $total = bcmul($extended_quantity, $price); - $discount_fraction = bcdiv($discount_percentage, 100); - $discount_amount = bcadd(bcmul($total, $discount_fraction), $discount_fixed); + $discount_amount = $discount; + if($discount_type == PERCENT) + { + $discount_fraction = bcdiv($discount, 100); + $discount_amount = bcmul($total, $discount_fraction); + } + return bcsub($total, $discount_amount); } @@ -374,7 +377,7 @@ class Receiving_lib $total = 0; foreach($this->get_cart() as $item) { - $total = bcadd($total, $this->get_item_total(($item['quantity']), $item['price'], $item['discount'], $item['discount_fixed'], $item['receiving_quantity'])); + $total = bcadd($total, $this->get_item_total(($item['quantity']), $item['price'], $item['discount'], $item['discount_type'], $item['receiving_quantity'])); } return $total; diff --git a/application/libraries/Sale_lib.php b/application/libraries/Sale_lib.php index e1ee8a512..528459fa6 100644 --- a/application/libraries/Sale_lib.php +++ b/application/libraries/Sale_lib.php @@ -450,7 +450,7 @@ class Sale_lib $item_count++; $total_units += $item['quantity']; } - $discount_amount = $this->get_item_discount($item['quantity'], $item['price'], $item['discount'], $item['discount_fixed']); + $discount_amount = $this->get_item_discount($item['quantity'], $item['price'], $item['discount'], $item['discount_type']); $total_discount = bcadd($total_discount, $discount_amount); $extended_amount = $this->get_extended_amount($item['quantity'], $item['price']); @@ -460,7 +460,7 @@ class Sale_lib if($this->CI->config->item('tax_included')) { - $subtotal = bcadd($subtotal, $this->get_extended_total_tax_exclusive($item['item_id'], $extended_discounted_amount, $item['quantity'], $item['price'], $item['discount'],$item['discount_fixed'])); + $subtotal = bcadd($subtotal, $this->get_extended_total_tax_exclusive($item['item_id'], $extended_discounted_amount, $item['quantity'], $item['price'], $item['discount'],$item['discount_type'])); } else { @@ -696,7 +696,7 @@ class Sale_lib $this->CI->session->unset_userdata('sales_rewards_remainder'); } - public function add_item(&$item_id, $quantity = 1, $item_location, $discount = 0, $discount_fixed = 0, $discount_type = 0, $price_mode = PRICE_MODE_STANDARD, $kit_price_option = NULL, $kit_print_option = NULL, $price_override = NULL, $description = NULL, $serialnumber = NULL, $include_deleted = FALSE, $print_option = NULL ) + public function add_item(&$item_id, $quantity = 1, $item_location, $discount = 0, $discount_type = 0, $price_mode = PRICE_MODE_STANDARD, $kit_price_option = NULL, $kit_print_option = NULL, $price_override = NULL, $description = NULL, $serialnumber = NULL, $include_deleted = FALSE, $print_option = NULL ) { $item_info = $this->CI->Item->get_info_by_id_or_number($item_id); @@ -753,7 +753,6 @@ class Sale_lib if($price == 0.00) { $discount = 0.00; - $discount_fixed = 0.00; } // Serialization and Description @@ -826,8 +825,8 @@ class Sale_lib } } - $total = $this->get_item_total($quantity, $price, $discount, $discount_fixed); - $discounted_total = $this->get_item_total($quantity, $price, $discount, $discount_fixed, TRUE); + $total = $this->get_item_total($quantity, $price, $discount, $discount_type); + $discounted_total = $this->get_item_total($quantity, $price, $discount, $discount_type, TRUE); if($this->CI->config->item('multi_pack_enabled') == '1') { @@ -851,7 +850,6 @@ class Sale_lib 'is_serialized' => $item_info->is_serialized, 'quantity' => $quantity, 'discount' => $discount, - 'discount_fixed' => $discount_fixed, 'discount_type' => $discount_type, 'in_stock' => $this->CI->Item_quantity->get_item_quantity($item_id, $item_location)->quantity, 'price' => $price, @@ -936,7 +934,7 @@ class Sale_lib return -1; } - public function edit_item($line, $description, $serialnumber, $quantity, $discount, $discount_fixed, $discount_type, $price, $discounted_total=NULL) + public function edit_item($line, $description, $serialnumber, $quantity, $discount, $discount_type, $price, $discounted_total=NULL) { $items = $this->get_cart(); if(isset($items[$line])) @@ -951,13 +949,12 @@ class Sale_lib $line['serialnumber'] = $serialnumber; $line['quantity'] = $quantity; $line['discount'] = $discount; - $line['discount_fixed'] = $discount_fixed; if(!is_null($discount_type)){ $line['discount_type'] = $discount_type; } $line['price'] = $price; - $line['total'] = $this->get_item_total($quantity, $price, $discount, $discount_fixed); - $line['discounted_total'] = $this->get_item_total($quantity, $price, $discount, $discount_fixed, TRUE); + $line['total'] = $this->get_item_total($quantity, $price, $discount, $line['discount_type']); + $line['discounted_total'] = $this->get_item_total($quantity, $price, $discount, $line['discount_type'], TRUE); $this->set_cart($items); } @@ -988,13 +985,13 @@ class Sale_lib foreach($this->CI->Sale->get_sale_items_ordered($sale_id)->result() as $row) { - $this->add_item($row->item_id, -$row->quantity_purchased, $row->item_location, $row->discount_percent, $row->discount_fixed, $row->discount_type, PRICE_MODE_STANDARD, NULL, NULL, $row->item_unit_price, $row->description, $row->serialnumber, TRUE); + $this->add_item($row->item_id, -$row->quantity_purchased, $row->item_location, $row->discount, $row->discount_type, PRICE_MODE_STANDARD, NULL, NULL, $row->item_unit_price, $row->description, $row->serialnumber, TRUE); } $this->set_customer($this->CI->Sale->get_customer($sale_id)->person_id); } - public function add_item_kit($external_item_kit_id, $item_location, $discount, $discount_fixed, $discount_type, $kit_price_option, $kit_print_option, &$stock_warning) + public function add_item_kit($external_item_kit_id, $item_location, $discount, $discount_type, $kit_price_option, $kit_print_option, &$stock_warning) { //KIT # $pieces = explode(' ', $external_item_kit_id); @@ -1003,7 +1000,7 @@ class Sale_lib foreach($this->CI->Item_kit_items->get_info($item_kit_id) as $item_kit_item) { - $result &= $this->add_item($item_kit_item['item_id'], $item_kit_item['quantity'], $item_location, $discount, $discount_fixed, $discount_type, PRICE_MODE_KIT, $kit_price_option, $kit_print_option, NULL, NULL, NULL, NULL); + $result &= $this->add_item($item_kit_item['item_id'], $item_kit_item['quantity'], $item_location, $discount, $discount_type, PRICE_MODE_KIT, $kit_price_option, $kit_print_option, NULL, NULL, NULL, NULL); if($stock_warning == NULL) { @@ -1021,7 +1018,7 @@ class Sale_lib foreach($this->CI->Sale->get_sale_items_ordered($sale_id)->result() as $row) { - $this->add_item($row->item_id, $row->quantity_purchased, $row->item_location, $row->discount_percent, $row->discount_fixed, $row->discount_type, PRICE_MODE_STANDARD, NULL, NULL, $row->item_unit_price, $row->description, $row->serialnumber, TRUE, $row->print_option); + $this->add_item($row->item_id, $row->quantity_purchased, $row->item_location, $row->discount, $row->discount_type, PRICE_MODE_STANDARD, NULL, NULL, $row->item_unit_price, $row->description, $row->serialnumber, TRUE, $row->print_option); } foreach($this->CI->Sale->get_sale_payments($sale_id)->result() as $row) @@ -1049,7 +1046,7 @@ class Sale_lib $this->empty_cart(); foreach($this->CI->Sale->get_sale_items_ordered($sale_id)->result() as $row) { - $this->add_item($row->item_id, $row->quantity_purchased, $row->item_location, $row->discount_percent, $row->discount_fixed, $row->discount_type, PRICE_MODE_STANDARD, NULL, NULL, $row->item_unit_price, $row->description, $row->serialnumber, TRUE, $row->print_option); + $this->add_item($row->item_id, $row->quantity_purchased, $row->item_location, $row->discount, $row->discount_type, PRICE_MODE_STANDARD, NULL, NULL, $row->item_unit_price, $row->description, $row->serialnumber, TRUE, $row->print_option); } return $this->CI->session->userdata('sales_cart'); @@ -1134,12 +1131,12 @@ class Sale_lib // This computes tax for each line item and adds it to the tax type total $tax_group = (float)$tax['percent'] . '% ' . $tax['name']; $tax_type = Tax_lib::TAX_TYPE_VAT; - $tax_basis = $this->get_item_total($item['quantity'], $item['price'], $item['discount'], $item['discount_fixed'], TRUE); + $tax_basis = $this->get_item_total($item['quantity'], $item['price'], $item['discount'], $item['discount_type'], TRUE); $tax_amount = 0; if($this->CI->config->item('tax_included')) { - $tax_amount = $this->get_item_tax($item['quantity'], $item['price'], $item['discount'], $item['discount_fixed'], $tax['percent']); + $tax_amount = $this->get_item_tax($item['quantity'], $item['price'], $item['discount'], $item['discount_type'], $tax['percent']); } elseif($this->CI->config->item('customer_sales_tax_support') == '0') { @@ -1173,7 +1170,7 @@ class Sale_lib return $sales_taxes; } - public function apply_customer_discount($discount_percent, $discount_fixed) + public function apply_customer_discount($discount, $discount_type) { // Get all items in the cart so far... $items = $this->get_cart(); @@ -1186,18 +1183,9 @@ class Sale_lib // set a new discount only if the current one is 0 if($item['discount'] == 0) { - $item['discount'] = $discount_percent; - $item['discount_fixed'] = $discount_fixed; - $item['total'] = $this->get_item_total($quantity, $price, $discount_percent, $discount_fixed); - $item['discounted_total'] = $this->get_item_total($quantity, $price, $discount_percent, $discount_fixed, TRUE); - } - - if($item['discount_fixed'] == 0) - { - $item['discount'] = $discount_percent; - $item['discount_fixed'] = $discount_fixed; - $item['total'] = $this->get_item_total($quantity, $price, $discount_percent, $discount_fixed); - $item['discounted_total'] = $this->get_item_total($quantity, $price, $discount_percent, $discount_fixed, TRUE); + $item['discount'] = $discount; + $item['total'] = $this->get_item_total($quantity, $price, $discount, $discount_type); + $item['discounted_total'] = $this->get_item_total($quantity, $price, $discount, $discount_type, TRUE); } } @@ -1211,13 +1199,7 @@ class Sale_lib { if($item['discount'] > 0) { - $item_discount = $this->get_item_discount($item['quantity'], $item['price'], $item['discount'], $item['discount_fixed']); - $discount = bcadd($discount, $item_discount); - } - - if($item['discount_fixed'] > 0) - { - $item_discount = $this->get_item_discount($item['quantity'], $item['price'], $item['discount'], $item['discount_fixed']); + $item_discount = $this->get_item_discount($item['quantity'], $item['price'], $item['discount'], $item['discount_type']); $discount = bcadd($discount, $item_discount); } } @@ -1230,39 +1212,39 @@ class Sale_lib return $this->calculate_subtotal($include_discount, $exclude_tax); } - public function get_item_total_tax_exclusive($item_id, $quantity, $price, $discount_percentage, $discount_fixed, $include_discount = FALSE) + public function get_item_total_tax_exclusive($item_id, $quantity, $price, $discount, $discount_type, $include_discount = FALSE) { $tax_info = $this->CI->Item_taxes->get_info($item_id); - $item_total = $this->get_item_total($quantity, $price, $discount_percentage, $discount_fixed, $include_discount); + $item_total = $this->get_item_total($quantity, $price, $discount, $discount_type, $include_discount); // only additive tax here foreach($tax_info as $tax) { $tax_percentage = $tax['percent']; - $item_total = bcsub($item_total, $this->get_item_tax($quantity, $price, $discount_percentage, $discount_fixed, $tax_percentage)); + $item_total = bcsub($item_total, $this->get_item_tax($quantity, $price, $discount, $discount_type, $tax_percentage)); } return $item_total; } - public function get_extended_total_tax_exclusive($item_id, $discounted_extended_amount, $quantity, $price, $discount_percentage = 0, $discount_fixed = 0) + public function get_extended_total_tax_exclusive($item_id, $discounted_extended_amount, $quantity, $price, $discount = 0, $discount_type = 0) { $tax_info = $this->CI->Item_taxes->get_info($item_id); // only additive tax here foreach($tax_info as $tax) { $tax_percentage = $tax['percent']; - $discounted_extended_amount = bcsub($discounted_extended_amount, $this->get_item_tax($quantity, $price, $discount_percentage, $discount_fixed, $tax_percentage)); + $discounted_extended_amount = bcsub($discounted_extended_amount, $this->get_item_tax($quantity, $price, $discount, $discount_type, $tax_percentage)); } return $discounted_extended_amount; } - public function get_item_total($quantity, $price, $discount_percentage, $discount_fixed, $include_discount = FALSE) + public function get_item_total($quantity, $price, $discount, $discount_type, $include_discount = FALSE) { $total = bcmul($quantity, $price); if($include_discount) { - $discount_amount = $this->get_item_discount($quantity, $price, $discount_percentage, $discount_fixed); + $discount_amount = $this->get_item_discount($quantity, $price, $discount, $discount_type); return bcsub($total, $discount_amount); } @@ -1291,17 +1273,21 @@ class Sale_lib return bcsub($extended_amount, $discount_amount); } - public function get_item_discount($quantity, $price, $discount_percentage, $discount_fixed) + public function get_item_discount($quantity, $price, $discount, $discount_type) { $total = bcmul($quantity, $price); - $discount_fraction = bcdiv($discount_percentage, 100); - - return round(bcadd(bcmul($total, $discount_fraction),$discount_fixed), totals_decimals(), PHP_ROUND_HALF_UP); + if($discount_type == PERCENT) + { + $discount_fraction = bcdiv($discount, 100); + $discount=bcmul($total,$discount_fraction); + } + + return round($discount, totals_decimals(), PHP_ROUND_HALF_UP); } - public function get_item_tax($quantity, $price, $discount_percentage, $discount_fixed, $tax_percentage) + public function get_item_tax($quantity, $price, $discount, $discount_type, $tax_percentage) { - $price = $this->get_item_total($quantity, $price, $discount_percentage, $discount_fixed, TRUE); + $price = $this->get_item_total($quantity, $price, $discount, $discount_type, TRUE); if($this->CI->config->item('tax_included')) { $tax_fraction = bcadd(100, $tax_percentage); @@ -1322,11 +1308,11 @@ class Sale_lib { if($exclude_tax && $this->CI->config->item('tax_included')) { - $subtotal = bcadd($subtotal, $this->get_item_total_tax_exclusive($item['item_id'], $item['quantity'], $item['price'], $item['discount'], $item['discount_fixed'], $include_discount)); + $subtotal = bcadd($subtotal, $this->get_item_total_tax_exclusive($item['item_id'], $item['quantity'], $item['price'], $item['discount'], $item['discount_type'], $include_discount)); } else { - $subtotal = bcadd($subtotal, $this->get_item_total($item['quantity'], $item['price'], $item['discount'], $item['discount_fixed'], $include_discount)); + $subtotal = bcadd($subtotal, $this->get_item_total($item['quantity'], $item['price'], $item['discount'], $item['discount_type'], $include_discount)); } } diff --git a/application/libraries/Tax_lib.php b/application/libraries/Tax_lib.php index 6b1f987c7..e36758b3a 100644 --- a/application/libraries/Tax_lib.php +++ b/application/libraries/Tax_lib.php @@ -31,12 +31,12 @@ class Tax_lib /* * Compute the tax basis and returns the tax amount */ - public function get_item_sales_tax($quantity, $price, $discount_percentage, $discount_fixed, $tax_percentage, $rounding_code) + public function get_item_sales_tax($quantity, $price, $discount, $discount_type, $tax_percentage, $rounding_code) { $decimals = tax_decimals(); // The tax basis should be returned at the currency scale - $tax_basis = $this->CI->sale_lib->get_item_total($quantity, $price, $discount_percentage, $discount_fixed, TRUE); + $tax_basis = $this->CI->sale_lib->get_item_total($quantity, $price, $discount, $discount_type, TRUE); return $this->get_sales_tax_for_amount($tax_basis, $tax_percentage, $rounding_code, $decimals); } @@ -223,7 +223,7 @@ class Tax_lib $decimals = tax_decimals(); // The tax basis should be returned at the currency scale - $tax_basis = $this->CI->sale_lib->get_item_total($item['quantity'], $item['price'], $item['discount'], $item['discount_fixed'], TRUE); + $tax_basis = $this->CI->sale_lib->get_item_total($item['quantity'], $item['price'], $item['discount'], $item['discount_type'], TRUE); $tax_amount = $this->get_sales_tax_for_amount($tax_basis, $tax_rate, $rounding_code, $decimals); $tax_group = (float)$tax_rate . '% ' . $tax_category; diff --git a/application/migrations/20180820100000_upgrade_to_3_4_0.php b/application/migrations/20180820100000_discount_on_sales.php similarity index 61% rename from application/migrations/20180820100000_upgrade_to_3_4_0.php rename to application/migrations/20180820100000_discount_on_sales.php index 677bad041..d0c6e401a 100644 --- a/application/migrations/20180820100000_upgrade_to_3_4_0.php +++ b/application/migrations/20180820100000_discount_on_sales.php @@ -1,6 +1,6 @@ db->dbprefix('sales') . ' AS sales INNER JOIN ' . $this->db->dbprefix('sales_items') . ' AS sales_items @@ -252,7 +252,8 @@ class Customer extends Person 'company_name' => NULL, 'account_number' => NULL, 'taxable' => 0, - 'discount_percent' => 0.00, + 'discount' => 0.00, + 'discount_type' => 0, 'package_id' => NULL, 'points' => NULL, 'sales_tax_code' => 1, diff --git a/application/models/Receiving.php b/application/models/Receiving.php index 4e1402e8c..b9c87047a 100644 --- a/application/models/Receiving.php +++ b/application/models/Receiving.php @@ -93,8 +93,7 @@ class Receiving extends CI_Model 'serialnumber' => $item['serialnumber'], 'quantity_purchased' => $item['quantity'], 'receiving_quantity' => $item['receiving_quantity'], - 'discount_percent' => $item['discount'], - 'discount_fixed' => $item['discount_fixed'], + 'discount' => $item['discount'], 'discount_type' => $item['discount_type'], 'item_cost_price' => $cur_item_info->cost_price, 'item_unit_price' => $item['price'], @@ -267,14 +266,14 @@ class Receiving extends CI_Model MAX(receivings_items.receiving_quantity) AS receiving_quantity, MAX(item_cost_price) AS item_cost_price, MAX(item_unit_price) AS item_unit_price, - MAX(discount_percent) AS discount_percent, - MAX(discount_fixed) AS discount_fixed, + MAX(discount) AS discount, + discount_type as discount_type, receivings_items.line, MAX(serialnumber) AS serialnumber, MAX(receivings_items.description) AS description, - MAX(item_unit_price * quantity_purchased * receivings_items.receiving_quantity - item_unit_price * quantity_purchased * receivings_items.receiving_quantity * discount_percent / 100) - discount_fixed AS subtotal, - MAX(item_unit_price * quantity_purchased * receivings_items.receiving_quantity - item_unit_price * quantity_purchased * receivings_items.receiving_quantity * discount_percent / 100) - discount_fixed AS total, - MAX((item_unit_price * quantity_purchased * receivings_items.receiving_quantity - item_unit_price * quantity_purchased * receivings_items.receiving_quantity * discount_percent / 100) - discount_fixed - (item_cost_price * quantity_purchased)) AS profit, + MAX(CASE WHEN receivings_items.discount_type = ' . PERCENT . ' THEN item_unit_price * quantity_purchased * receivings_items.receiving_quantity - item_unit_price * quantity_purchased * receivings_items.receiving_quantity * discount / 100 ELSE item_unit_price * quantity_purchased * receivings_items.receiving_quantity - discount END) AS subtotal, + MAX(CASE WHEN receivings_items.discount_type = ' . PERCENT . ' THEN item_unit_price * quantity_purchased * receivings_items.receiving_quantity - item_unit_price * quantity_purchased * receivings_items.receiving_quantity * discount / 100 ELSE item_unit_price * quantity_purchased * receivings_items.receiving_quantity - discount END) AS total, + MAX((CASE WHEN receivings_items.discount_type = ' . PERCENT . ' THEN item_unit_price * quantity_purchased * receivings_items.receiving_quantity - item_unit_price * quantity_purchased * receivings_items.receiving_quantity * discount / 100 ELSE item_unit_price * quantity_purchased * receivings_items.receiving_quantity - discount END) - (item_cost_price * quantity_purchased)) AS profit, MAX(item_cost_price * quantity_purchased * receivings_items.receiving_quantity ) AS cost FROM ' . $this->db->dbprefix('receivings_items') . ' AS receivings_items INNER JOIN ' . $this->db->dbprefix('receivings') . ' AS receivings diff --git a/application/models/Sale.php b/application/models/Sale.php index 65afec976..6a19f7fba 100644 --- a/application/models/Sale.php +++ b/application/models/Sale.php @@ -10,6 +10,8 @@ define('SALE_TYPE_WORK_ORDER', 2); define('SALE_TYPE_QUOTE', 3); define('SALE_TYPE_RETURN', 4); +define('PERCENT', 0); +define('FIXED', 1); /** * Sale class */ @@ -38,7 +40,7 @@ class Sale extends CI_Model $decimals = totals_decimals(); - $sale_price = 'sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100)-sales_items.discount_fixed'; + $sale_price = 'CASE WHEN sales_items.discount_type = ' . PERCENT . ' THEN sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount / 100) ELSE sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.discount END'; $tax = 'ROUND(IFNULL(SUM(sales_items_taxes.tax), 0), ' . $decimals . ')'; if($this->config->item('tax_included')) @@ -152,7 +154,7 @@ class Sale extends CI_Model $decimals = totals_decimals(); - $sale_price = 'sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100)-sales_items.discount_fixed'; + $sale_price = 'CASE WHEN sales_items.discount_type = ' . PERCENT . ' THEN sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount / 100) ELSE sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.discount END'; $sale_cost = 'SUM(sales_items.item_cost_price * sales_items.quantity_purchased)'; $tax = 'IFNULL(SUM(sales_items_taxes.tax), 0)'; @@ -652,8 +654,7 @@ class Sale extends CI_Model 'description' => character_limiter($item['description'], 255), 'serialnumber' => character_limiter($item['serialnumber'], 30), 'quantity_purchased'=> $item['quantity'], - 'discount_percent' => $item['discount'], - 'discount_fixed' => $item['discount_fixed'], + 'discount' => $item['discount'], 'discount_type' => $item['discount_type'], 'item_cost_price' => $item['cost_price'], 'item_unit_price' => $item['price'], @@ -705,7 +706,7 @@ class Sale extends CI_Model } $rounding_code = Rounding_mode::HALF_UP; // half adjust $tax_group_sequence = 0; - $item_total = $this->sale_lib->get_item_total($item['quantity'], $item['price'], $item['discount'], TRUE); + $item_total = $this->sale_lib->get_item_total($item['quantity'], $item['price'], $item['discount'], $item['discount_type'], TRUE); $tax_basis = $item_total; $item_tax_amount = 0; @@ -727,12 +728,12 @@ class Sale extends CI_Model // This computes tax for each line item and adds it to the tax type total $tax_group = (float)$row['percent'] . '% ' . $row['name']; - $tax_basis = $this->sale_lib->get_item_total($item['quantity'], $item['price'], $item['discount'], TRUE); + $tax_basis = $this->sale_lib->get_item_total($item['quantity'], $item['price'], $item['discount'], $item['discount_type'], TRUE); if($this->config->item('tax_included')) { $tax_type = Tax_lib::TAX_TYPE_VAT; - $item_tax_amount = $this->sale_lib->get_item_tax($item['quantity'], $item['price'], $item['discount'],$row['percent']); + $item_tax_amount = $this->sale_lib->get_item_tax($item['quantity'], $item['price'], $item['discount'], $item['discount_type'], $row['percent']); } elseif($this->config->item('customer_sales_tax_support') == '0') { @@ -929,8 +930,7 @@ class Sale extends CI_Model quantity_purchased, item_cost_price, item_unit_price, - discount_percent, - discount_fixed, + discount, discount_type, item_location, print_option, @@ -1117,7 +1117,7 @@ class Sale extends CI_Model $decimals = totals_decimals(); - $sale_price = 'sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100)-sales_items.discount_fixed'; + $sale_price = 'CASE WHEN sales_items.discount_type = ' . PERCENT . ' THEN sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount / 100) ELSE sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.discount END'; $sale_cost = 'SUM(sales_items.item_cost_price * sales_items.quantity_purchased)'; $tax = 'IFNULL(SUM(sales_items_taxes.tax), 0)'; @@ -1194,8 +1194,8 @@ class Sale extends CI_Model MAX(sales_items.quantity_purchased) AS quantity_purchased, MAX(sales_items.item_cost_price) AS item_cost_price, MAX(sales_items.item_unit_price) AS item_unit_price, - MAX(sales_items.discount_percent) AS discount_percent, - MAX(sales_items.discount_fixed) AS discount_fixed, + MAX(sales_items.discount) AS discount, + sales_items.discount_type AS discount_type, sales_items.line AS line, MAX(sales_items.serialnumber) AS serialnumber, MAX(sales_items.item_location) AS item_location, diff --git a/application/models/reports/Detailed_receivings.php b/application/models/reports/Detailed_receivings.php index 88e0e4317..695eb7498 100644 --- a/application/models/reports/Detailed_receivings.php +++ b/application/models/reports/Detailed_receivings.php @@ -97,7 +97,7 @@ class Detailed_receivings extends Report foreach($data['summary'] as $key=>$value) { - $this->db->select('name, item_number, category, quantity_purchased, serialnumber,total, discount_percent, discount_fixed, item_location, receivings_items_temp.receiving_quantity'); + $this->db->select('name, item_number, category, quantity_purchased, serialnumber,total, discount, discount_type, item_location, receivings_items_temp.receiving_quantity'); $this->db->from('receivings_items_temp'); $this->db->join('items', 'receivings_items_temp.item_id = items.item_id'); $this->db->where('receiving_id = '.$value['receiving_id']); diff --git a/application/models/reports/Detailed_sales.php b/application/models/reports/Detailed_sales.php index ab3579640..36ad0234c 100644 --- a/application/models/reports/Detailed_sales.php +++ b/application/models/reports/Detailed_sales.php @@ -144,7 +144,7 @@ class Detailed_sales extends Report foreach($data['summary'] as $key=>$value) { - $this->db->select('name, category, quantity_purchased, item_location, serialnumber, description, subtotal, tax, total, cost, profit, discount_percent, discount_fixed, sale_status'); + $this->db->select('name, category, quantity_purchased, item_location, serialnumber, description, subtotal, tax, total, cost, profit, discount, discount_type, sale_status'); $this->db->from('sales_items_temp'); $this->db->where('sale_id', $value['sale_id']); $data['details'][$key] = $this->db->get()->result_array(); diff --git a/application/models/reports/Specific_discount.php b/application/models/reports/Specific_discount.php index ab8a059e5..d46e6f037 100755 --- a/application/models/reports/Specific_discount.php +++ b/application/models/reports/Specific_discount.php @@ -71,11 +71,8 @@ class Specific_discount extends Report MAX(comment) AS comment'); $this->db->from('sales_items_temp'); - if($inputs['discount_type']==1){ - $this->db->where('discount_fixed >=', $inputs['discount']); - }else{ - $this->db->where('discount_percent >=', $inputs['discount']); - } + $this->db->where('discount >=', $inputs['discount']); + $this->db->where('discount_type',$inputs['discount_type']); if($inputs['sale_type'] == 'complete') { @@ -124,7 +121,7 @@ class Specific_discount extends Report foreach($data['summary'] as $key=>$value) { - $this->db->select('name, category, serialnumber, description, quantity_purchased, subtotal, tax, total, cost, profit, discount_percent, discount_fixed'); + $this->db->select('name, category, serialnumber, description, quantity_purchased, subtotal, tax, total, cost, profit, discount, discount_type'); $this->db->from('sales_items_temp'); $this->db->where('sale_id', $value['sale_id']); $data['details'][$key] = $this->db->get()->result_array(); @@ -142,11 +139,9 @@ class Specific_discount extends Report $this->db->select('SUM(subtotal) AS subtotal, SUM(tax) AS tax, SUM(total) AS total, SUM(cost) AS cost, SUM(profit) AS profit'); $this->db->from('sales_items_temp'); - if($inputs['discount_type']==1){ - $this->db->where('discount_fixed >=', $inputs['discount']); - }else{ - $this->db->where('discount_percent >=', $inputs['discount']); - } + $this->db->where('discount >=', $inputs['discount']); + $this->db->where('discount_type',$inputs['discount_type']); + if($inputs['sale_type'] == 'complete') { diff --git a/application/models/reports/Specific_employee.php b/application/models/reports/Specific_employee.php index 0243d7f1f..36bdc0957 100644 --- a/application/models/reports/Specific_employee.php +++ b/application/models/reports/Specific_employee.php @@ -117,7 +117,7 @@ class Specific_employee extends Report foreach($data['summary'] as $key=>$value) { - $this->db->select('name, category, serialnumber, description, quantity_purchased, subtotal, tax, total, cost, profit, discount_percent, discount_fixed'); + $this->db->select('name, category, serialnumber, description, quantity_purchased, subtotal, tax, total, cost, profit, discount, discount_type'); $this->db->from('sales_items_temp'); $this->db->where('sale_id', $value['sale_id']); $data['details'][$key] = $this->db->get()->result_array(); diff --git a/application/models/reports/Summary_discounts.php b/application/models/reports/Summary_discounts.php index 606f79f9c..3c94829fb 100644 --- a/application/models/reports/Summary_discounts.php +++ b/application/models/reports/Summary_discounts.php @@ -13,17 +13,21 @@ class Summary_discounts extends Summary_report public function getData(array $inputs) { - if($inputs['discount_type']==1){ - $this->db->select('MAX(CONCAT("'.$this->config->item('currency_symbol').'",sales_items.discount_fixed)) AS discount, count(*) AS count'); - $this->db->where('discount_fixed > 0'); - $this->db->group_by('sales_items.discount_fixed'); - $this->db->order_by('sales_items.discount_fixed'); - }else{ - $this->db->select('MAX(CONCAT(sales_items.discount_percent, "%")) AS discount, count(*) AS count'); - $this->db->where('discount_percent > 0'); - $this->db->group_by('sales_items.discount_percent'); - $this->db->order_by('sales_items.discount_percent'); + if($inputs['discount_type'] == FIXED) + { + $this->db->select('MAX(CONCAT("'.$this->config->item('currency_symbol').'",sales_items.discount)) AS discount, count(*) AS count'); + $this->db->where('discount_type',FIXED); } + elseif($inputs['discount_type'] == PERCENT) + { + $this->db->select('MAX(CONCAT(sales_items.discount, "%")) AS discount, count(*) AS count'); + $this->db->where('discount_type',PERCENT); + } + + $this->db->where('discount > 0'); + $this->db->group_by('sales_items.discount'); + $this->db->order_by('sales_items.discount'); + $this->db->from('sales_items AS sales_items'); $this->db->join('sales AS sales', 'sales_items.sale_id = sales.sale_id', 'inner'); diff --git a/application/models/reports/Summary_payments.php b/application/models/reports/Summary_payments.php index cbb842b4f..79e82a03c 100644 --- a/application/models/reports/Summary_payments.php +++ b/application/models/reports/Summary_payments.php @@ -14,7 +14,7 @@ class Summary_payments extends Summary_report public function getData(array $inputs) { - $this->db->select('sales_payments.payment_type, COUNT(DISTINCT sales_payments.sale_id) AS count, SUM(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100)-sales_items.discount_fixed) AS payment_amount'); + $this->db->select('sales_payments.payment_type, COUNT(DISTINCT sales_payments.sale_id) AS count, SUM(CASE WHEN sales_items.discount_type = ' . PERCENT . ' THEN sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount / 100) ELSE sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.discount END) AS payment_amount'); $this->db->from('sales_payments AS sales_payments'); $this->db->join('sales AS sales', 'sales.sale_id = sales_payments.sale_id'); $this->db->join('sales_items AS sales_items', 'sales_items.sale_id = sales_payments.sale_id', 'left'); diff --git a/application/models/reports/Summary_report.php b/application/models/reports/Summary_report.php index 9315361d0..2511f85bd 100644 --- a/application/models/reports/Summary_report.php +++ b/application/models/reports/Summary_report.php @@ -23,7 +23,7 @@ abstract class Summary_report extends Report $decimals = totals_decimals(); - $sale_price = 'sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100)-sales_items.discount_fixed'; + $sale_price = 'CASE WHEN sales_items.discount_type = ' . PERCENT . ' THEN sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount / 100) ELSE sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.discount END'; $sale_cost = 'SUM(sales_items.item_cost_price * sales_items.quantity_purchased)'; $tax = 'IFNULL(SUM(sales_items_taxes.tax), 0)'; diff --git a/application/models/reports/Summary_taxes.php b/application/models/reports/Summary_taxes.php index d57a61510..11ab9f343 100644 --- a/application/models/reports/Summary_taxes.php +++ b/application/models/reports/Summary_taxes.php @@ -41,13 +41,13 @@ class Summary_taxes extends Summary_report if($this->config->item('tax_included')) { - $sale_total = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100))'; - $sale_subtotal = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (100 / (100 + sales_items_taxes.percent)))'; + $sale_total = '(CASE WHEN sales_items.discount_type = ' . PERCENT . ' THEN sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount / 100) ELSE sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.discount END)'; + $sale_subtotal = '(CASE WHEN sales_items.discount_type = ' . PERCENT . ' THEN sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount / 100) ELSE sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.discount END * (100 / (100 + sales_items_taxes.percent)))'; } else { - $sale_total = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100) * (1 + (sales_items_taxes.percent / 100)))'; - $sale_subtotal = '(sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount_percent / 100))'; + $sale_total = '(CASE WHEN sales_items.discount_type = ' . PERCENT . ' THEN sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount / 100) ELSE sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.discount END * (1 + (sales_items_taxes.percent / 100)))'; + $sale_subtotal = '(CASE WHEN sales_items.discount_type = ' . PERCENT . ' THEN sales_items.item_unit_price * sales_items.quantity_purchased * (1 - sales_items.discount / 100) ELSE sales_items.item_unit_price * sales_items.quantity_purchased - sales_items.discount END)'; } $decimals = totals_decimals(); diff --git a/application/views/configs/general_config.php b/application/views/configs/general_config.php index 2ea8a1029..304f521c1 100644 --- a/application/views/configs/general_config.php +++ b/application/views/configs/general_config.php @@ -11,17 +11,6 @@ -
- lang->line('config_default_sales_discount_type'), 'default_sales_discount_type', array('class' => 'control-label col-xs-2')); ?> -
- 'default_sales_discount_type', - 'id' => 'default_sales_discount_type', - 'value' => 'default_sales_discount_type', - 'checked' => $this->config->item('default_sales_discount_type'))); ?> -
-
-
lang->line('config_default_sales_discount'), 'default_sales_discount', array('class' => 'control-label col-xs-2 required')); ?>
@@ -34,19 +23,19 @@ 'min' => 0, 'max' => 100, 'value' => $this->config->item('default_sales_discount'))); ?> - % -
-
-
-
- config->item('currency_symbol'); ?> - 'default_sales_discount_fixed', - 'id' => 'default_sales_discount_fixed', - 'class' => 'form-control input-sm required', - 'type' => 'number', - 'min' => 0, - 'value' => $this->config->item('default_sales_discount_fixed'))); ?> + + 'default_sales_discount_type', + 'name'=>'default_sales_discount_type', + 'value'=>1, + 'data-toggle'=>"toggle", + 'data-size'=>'small', + 'data-onstyle'=>'success', + 'data-on'=>''.$this->config->item('currency_symbol').'', + 'data-off'=>'%', + 'checked'=>$this->config->item('default_sales_discount_type')) + ); ?> +
diff --git a/application/views/customers/form.php b/application/views/customers/form.php index 1b32fb78f..6583cbf1a 100644 --- a/application/views/customers/form.php +++ b/application/views/customers/form.php @@ -40,33 +40,41 @@ load->view("people/form_basic_info"); ?> + +
+ lang->line('customers_discount_type'), 'discount_type', array('class'=>'control-label col-xs-3')); ?> +
+ + +
+
- lang->line('customers_discount'), 'discount_percent', array('class' => 'control-label col-xs-3')); ?> + lang->line('customers_discount'), 'discount', array('class' => 'control-label col-xs-3')); ?>
'discount_percent', - 'id'=>'discount_percent', + 'name'=>'discount', + 'id'=>'discount', 'class'=>'form-control input-sm', - 'value'=>$person_info->discount_percent) + 'value'=>$person_info->discount) ); ?> - % -
-
- -
-
- config->item('currency_symbol'); ?> - 'discount_fixed', - 'size'=>'5', - 'maxlength'=>'5', - 'id'=>'discount_fixed', - 'class'=>'form-control input-sm', - 'value'=>$person_info->discount_fixed) - );?> -
diff --git a/application/views/receivings/receipt.php b/application/views/receivings/receipt.php index b14a71a50..f28296538 100644 --- a/application/views/receivings/receipt.php +++ b/application/views/receivings/receipt.php @@ -92,17 +92,20 @@ { ?> - lang->line("sales_discount_included")?> - - - 0 ) - { - ?> - - lang->line("sales_discount")?> + + lang->line("sales_discount") ?> + + lang->line("sales_discount_included") ?> +
- 'discount_fixed', 'class'=>'form-control input-sm', 'value'=>to_decimals($item['discount_fixed'], 0), 'onClick'=>'this.select();')); - echo form_hidden('discount', $item['discount']); - } - else - { - echo form_input(array('name'=>'discount', 'class'=>'form-control input-sm', 'value'=>to_decimals($item['discount'], 0), 'onClick'=>'this.select();')); - echo form_hidden('discount_fixed', $item['discount_fixed']); - } - ?> + 'discount', 'class'=>'form-control input-sm', 'value'=>to_decimals($item['discount'], 0), 'onClick'=>'this.select();')); ?> 'discount_toggle', 'name'=>'discount_toggle', 'value'=>1, 'data-toggle'=>"toggle",'data-size'=>'small', 'data-onstyle'=>'success', 'data-on'=>''.$this->config->item('currency_symbol').'', 'data-off'=>'%', 'data-line'=>$line, 'checked'=>$item['discount_type'])); ?> - -
@@ -189,20 +176,13 @@ if (isset($success)) else { ?> - + - - + + lang->line('receivings_update')?> > @@ -531,7 +511,7 @@ $(document).ready(function() } } - $('[name="price"],[name="quantity"],[name="receiving_quantity"],[name="discount"],[name="discount_fixed"],[name="description"],[name="serialnumber"]').change(function() { + $('[name="price"],[name="quantity"],[name="receiving_quantity"],[name="discount"],[name="description"],[name="serialnumber"]').change(function() { $(this).parents("tr").prevAll("form:first").submit() }); diff --git a/application/views/sales/invoice.php b/application/views/sales/invoice.php index c1a6018ba..85ac25149 100755 --- a/application/views/sales/invoice.php +++ b/application/views/sales/invoice.php @@ -133,7 +133,7 @@ $(document).ready(function() - + 0): ?> diff --git a/application/views/sales/invoice_email.php b/application/views/sales/invoice_email.php index 72c0c6259..c69ed99fb 100644 --- a/application/views/sales/invoice_email.php +++ b/application/views/sales/invoice_email.php @@ -96,7 +96,7 @@ - + 0): ?> diff --git a/application/views/sales/quote.php b/application/views/sales/quote.php index a37acd8bf..acf29976d 100644 --- a/application/views/sales/quote.php +++ b/application/views/sales/quote.php @@ -135,7 +135,7 @@ if (isset($error_message)) + cols="6"> 0): ?> diff --git a/application/views/sales/quote_email.php b/application/views/sales/quote_email.php index 051801575..cf6b211fb 100644 --- a/application/views/sales/quote_email.php +++ b/application/views/sales/quote_email.php @@ -96,7 +96,7 @@ - + 0): ?> diff --git a/application/views/sales/receipt_default.php b/application/views/sales/receipt_default.php index 9192f4f86..4197e4985 100644 --- a/application/views/sales/receipt_default.php +++ b/application/views/sales/receipt_default.php @@ -96,19 +96,20 @@ { ?> - lang->line("sales_discount_included") ?> - - - 0) - { - ?> - - lang->line("sales_discount") ?> + + lang->line("sales_discount") ?> + + lang->line("sales_discount_included") ?> + - lang->line("sales_discount_included") ?> - - - 0) - { - ?> - - lang->line("sales_discount") ?> + + lang->line("sales_discount") ?> + + lang->line("sales_discount_included") ?> + - lang->line("sales_discount_included")?> - - - 0) - { - ?> - - lang->line("sales_discount") ?> + + lang->line("sales_discount") ?> + + lang->line("sales_discount_included") ?> +
- 'discount_fixed', 'class'=>'form-control input-sm', 'value'=>to_decimals($item['discount_fixed'], 0), 'tabindex'=>++$tabindex, 'onClick'=>'this.select();')); - echo form_hidden('discount', $item['discount']); - } - else - { - echo form_input(array('name'=>'discount', 'class'=>'form-control input-sm', 'value'=>to_decimals($item['discount'], 0), 'tabindex'=>++$tabindex, 'onClick'=>'this.select();')); - echo form_hidden('discount_fixed', $item['discount_fixed']); - } - ?> + 'discount', 'class'=>'form-control input-sm', 'value'=>to_decimals($item['discount'], 0), 'tabindex'=>++$tabindex, 'onClick'=>'this.select();')); ?> 'discount_toggle', 'name'=>'discount_toggle', 'value'=>1, 'data-toggle'=>"toggle",'data-size'=>'small', 'data-onstyle'=>'success', 'data-on'=>''.$this->config->item('currency_symbol').'', 'data-off'=>'%', 'data-line'=>$line, 'checked'=>$item['discount_type'])); ?>
+ + lang->line('sales_update')?> > @@ -358,7 +349,7 @@ if(isset($success)) ?> lang->line("sales_customer_discount"); ?> - + config->item('customer_reward_enable') == TRUE): ?> - + diff --git a/application/views/sales/work_order_email.php b/application/views/sales/work_order_email.php index 0e68305ce..cd71a1a1e 100644 --- a/application/views/sales/work_order_email.php +++ b/application/views/sales/work_order_email.php @@ -87,7 +87,7 @@ - +