diff --git a/app/Controllers/Receivings.php b/app/Controllers/Receivings.php index 6601d8477..6dd022b23 100644 --- a/app/Controllers/Receivings.php +++ b/app/Controllers/Receivings.php @@ -162,7 +162,7 @@ class Receivings extends Secure_Controller * @param $item_id * @return void */ - public function edit_item($item_id): void + public function postEditItem($item_id): void { $data = []; @@ -223,7 +223,7 @@ class Receivings extends Secure_Controller * @param $item_number * @return void */ - public function postDelete_item($item_number): void + public function getDeleteItem($item_number): void { $this->receiving_lib->delete_item($item_number); diff --git a/app/Controllers/Sales.php b/app/Controllers/Sales.php index 4c78a5d99..cef276a21 100644 --- a/app/Controllers/Sales.php +++ b/app/Controllers/Sales.php @@ -43,11 +43,13 @@ use ReflectionException; */ class Sales extends Secure_Controller { + protected $helpers = ['form', 'file']; + public function __construct() { parent::__construct('sales'); - helper('file'); +// helper('file'); $this->session = session(); $this->barcode_lib = new Barcode_lib(); @@ -324,7 +326,7 @@ class Sales extends Secure_Controller * Multiple Payments. Called in the view. * @return void */ - public function add_payment(): void + public function postAddPayment(): void { $data = []; @@ -333,14 +335,16 @@ class Sales extends Secure_Controller //TODO: See the code block below. This too needs to be ternary notation. if($payment_type !== lang('Sales.giftcard')) { - $this->validator->setRule('amount_tendered', 'lang:sales_amount_tendered', 'trim|required|numeric'); + $rules = ['amount_tendered' => 'trim|required|decimal',]; + $messages = ['amount_tendered' => lang('Sales.must_enter_numeric')]; } else { - $this->validator->setRule('amount_tendered', 'lang:sales_amount_tendered', 'trim|required'); + $rules = ['amount_tendered' => 'trim|required',]; + $messages = ['amount_tendered' => lang('Sales.must_enter_numeric_giftcard')]; } - if(!$this->validate([])) + if(!$this->validate($rules, $messages)) {//TODO: the code below should be refactored to the following ternary notation since it's much more readable and concise: //$data['error'] = $payment_type === lang('Sales.giftcard') // ? $data['error'] = lang('Sales.must_enter_numeric_giftcard') @@ -441,7 +445,7 @@ class Sales extends Secure_Controller } } - $this->_reload($data); //TODO: Hungarian notation + $this->_reload($data); } /** @@ -517,7 +521,6 @@ class Sales extends Secure_Controller { if(!$this->sale_lib->add_item($kit_item_id, $item_location, $quantity, $discount, $discount_type, PRICE_MODE_KIT, $kit_price_option, $kit_print_option, $price)) { - log_message('info', '>>> fail point 1'); $data['error'] = lang('Sales.unable_to_add_item'); } else @@ -530,7 +533,6 @@ class Sales extends Secure_Controller $stock_warning = NULL; 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)) { - log_message('info', '>>> fail point 2'); $data['error'] = lang('Sales.unable_to_add_item'); } elseif($stock_warning != NULL) @@ -542,7 +544,6 @@ class Sales extends Secure_Controller { if(!$this->sale_lib->add_item($item_id_or_number_or_item_kit_or_receipt, $item_location, $quantity, $discount, $discount_type, PRICE_MODE_STANDARD, NULL, NULL, $price)) { - log_message('info', '>>> fail point 3'); $data['error'] = lang('Sales.unable_to_add_item'); } else @@ -559,38 +560,43 @@ class Sales extends Secure_Controller * @param string $line * @return void */ - public function edit_item(string $line): void + public function postEditItem(string $line): void { $data = []; - $this->validator->setRule('price', 'lang:sales_price', 'required|numeric'); - $this->validator->setRule('quantity', 'lang:sales_quantity', 'required|numeric'); - $this->validator->setRule('discount', 'lang:sales_discount', 'required|numeric'); + $rules = [ + 'price' => 'trim|required|numeric', + 'quantity' => 'trim|required|numeric', + 'discount' => 'trim|required|numeric', + ]; - $description = $this->request->getPost('description', FILTER_SANITIZE_STRING); - $serialnumber = $this->request->getPost('serialnumber', FILTER_SANITIZE_STRING); - $price = parse_decimals($this->request->getPost('price', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)); - $quantity = parse_quantity($this->request->getPost('quantity', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)); - $discount_type = $this->request->getPost('discount_type', FILTER_SANITIZE_STRING); - $discount = $discount_type ? parse_quantity($this->request->getPost('discount', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)) : parse_decimals($this->request->getPost('discount', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)); - - $item_location = $this->request->getPost('location', FILTER_SANITIZE_NUMBER_INT); - $discounted_total = $this->request->getPost('discounted_total') != '' ? $this->request->getPost('discounted_total', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION) : NULL; - - if(!$this->validate([])) + if($this->validate($rules)) { + + $description = $this->request->getPost('description', FILTER_SANITIZE_STRING); + $serialnumber = $this->request->getPost('serialnumber', FILTER_SANITIZE_STRING); + $price = parse_decimals($this->request->getPost('price', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)); + $quantity = parse_quantity($this->request->getPost('quantity', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)); + $discount_type = $this->request->getPost('discount_type', FILTER_SANITIZE_STRING); + $discount = $discount_type ? parse_quantity($this->request->getPost('discount', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)) : parse_decimals($this->request->getPost('discount', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION)); + + $item_location = $this->request->getPost('location', FILTER_SANITIZE_NUMBER_INT); + $discounted_total = $this->request->getPost('discounted_total') != '' ? $this->request->getPost('discounted_total', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION) : NULL; + + $this->sale_lib->edit_item($line, $description, $serialnumber, $quantity, $discount, $discount_type, $price, $discounted_total); $this->sale_lib->empty_payments(); + + $data['warning'] = $this->sale_lib->out_of_stock($this->sale_lib->get_item_id($line), $item_location); + } else { $data['error'] = lang('Sales.error_editing_item'); } - $data['warning'] = $this->sale_lib->out_of_stock($this->sale_lib->get_item_id($line), $item_location); - - $this->_reload($data); //TODO: Hungarian notation + $this->_reload($data); } /** @@ -599,7 +605,7 @@ class Sales extends Secure_Controller * @return void * @throws ReflectionException */ - public function postDelete_item(int $item_id): void + public function getDeleteItem(int $item_id): void { $this->sale_lib->delete_item($item_id); @@ -612,7 +618,7 @@ class Sales extends Secure_Controller * Called in the view. * @return void */ - public function remove_customer(): void + public function getRemoveCustomer(): void { $this->sale_lib->clear_giftcard_remainder(); $this->sale_lib->clear_rewards_remainder(); diff --git a/app/Controllers/Secure_Controller.php b/app/Controllers/Secure_Controller.php index 6375b06c7..29dae7ac9 100644 --- a/app/Controllers/Secure_Controller.php +++ b/app/Controllers/Secure_Controller.php @@ -26,6 +26,7 @@ class Secure_Controller extends BaseController $this->employee = model('Employee'); $this->module = model('Module'); $config = config('OSPOS')->settings; + $validation = \Config\Services::validation(); if(!$this->employee->is_logged_in()) { diff --git a/app/Helpers/locale_helper.php b/app/Helpers/locale_helper.php index 78504f132..3f6cc18d3 100644 --- a/app/Helpers/locale_helper.php +++ b/app/Helpers/locale_helper.php @@ -391,7 +391,11 @@ function to_decimals(?float $number, string $decimals = NULL, int $type = Number return $fmt->format($number); } -function parse_quantity(string $number): float +/** + * @param string $number + * @return false|float|int|mixed|string + */ +function parse_quantity(string $number) { return parse_decimals($number, quantity_decimals()); } diff --git a/app/Libraries/Sale_lib.php b/app/Libraries/Sale_lib.php index beba03f1b..68a153699 100644 --- a/app/Libraries/Sale_lib.php +++ b/app/Libraries/Sale_lib.php @@ -418,7 +418,7 @@ class Sale_lib * @param string $payment_amount * @param int $cash_adjustment */ - public function add_payment(int $payment_id, string $payment_amount, int $cash_adjustment = CASH_ADJUSTMENT_FALSE): void + public function add_payment(string $payment_id, string $payment_amount, int $cash_adjustment = CASH_ADJUSTMENT_FALSE): void { $payments = $this->get_payments(); if(isset($payments[$payment_id])) @@ -802,18 +802,15 @@ class Sale_lib //TODO: this function needs to be reworked... way too many parameters. Also, optional parameters must go after mandatory parameters. public function add_item(int &$item_id, int $item_location, string $quantity = '1', string &$discount = '0.0', int $discount_type = 0, int $price_mode = PRICE_MODE_STANDARD, int $kit_price_option = NULL, int $kit_print_option = NULL, string $price_override = NULL, string $description = NULL, string $serialnumber = NULL, int $sale_id = NULL, bool $include_deleted = FALSE, bool $print_option = NULL, bool $line = NULL): bool { - log_message('info', '>>>add_item: point 1: $item_id-' . $item_id . ', $include_deleted-' . $include_deleted); $item_info = $this->item->get_info_by_id_or_number($item_id, $include_deleted); //make sure item exists if(empty($item_info)) { - $item_id = -1; //TODO: Replace -1 with constant - log_message('info', '>>>add_item: point 2'); + $item_id = NEW_ENTRY; return FALSE; } - log_message('info', '>>>add_item: point 3'); $applied_discount = $discount; $item_id = $item_info->item_id; $item_type = $item_info->item_type; @@ -825,7 +822,6 @@ class Sale_lib { $price = $price_override; } - log_message('info', '>>>add_item: point 4'); if($price_mode == PRICE_MODE_KIT) { @@ -859,7 +855,6 @@ class Sale_lib } // Serialization and Description - log_message('info', '>>>add_item: point 6'); //Get all items in the cart so far... $items = $this->get_cart(); @@ -874,7 +869,6 @@ class Sale_lib $insertkey = 0; //Key to use for new entry. //TODO: $insertkey is never used $updatekey = 0; //Key to use to update(quantity) - log_message('info', '>>>add_item: point 7'); foreach($items as $item) { //We primed the loop so maxkey is 0 the first time. @@ -895,7 +889,6 @@ class Sale_lib } } } - log_message('info', '>>>add_item: point 8'); $insertkey = $maxkey + 1;//TODO Does not follow naming conventions. //array/cart records are identified by $insertkey and item_id is just another field. @@ -940,13 +933,11 @@ class Sale_lib } $attribute_links = $this->attribute->get_link_values($item_id, 'sale_id', $sale_id, Attribute::SHOW_IN_SALES)->getRowObject(); - log_message('info', '>>>add_item: point 9'); //Item already exists and is not serialized, add to quantity if(!$itemalreadyinsale || $item_info->is_serialized) { $item_quantity = model(Item_quantity::class); - log_message('info', '>>>add_item: point 10'); $item = [ $insertkey => [ @@ -977,22 +968,19 @@ class Sale_lib 'tax_category_id' => $item_info->tax_category_id ] ]; - log_message('info', '>>>add_item: point 11'); + //add to existing array $items += $item; } else { - log_message('info', '>>>add_item: point 12'); $line = &$items[$updatekey]; $line['quantity'] = $quantity; $line['total'] = $total; $line['discounted_total'] = $discounted_total; } - log_message('info', '>>>add_item: point 13'); $this->set_cart($items); - log_message('info', '>>>add_item: point 14'); return TRUE; } @@ -1065,7 +1053,7 @@ class Sale_lib * @param string|NULL $discounted_total * @return bool */ - public function edit_item(string $line, string $description, string $serialnumber, string $quantity, string $discount, string $discount_type, string $price, string $discounted_total = NULL): bool + public function edit_item(string $line, string $description, string $serialnumber, string $quantity, string $discount, ?string $discount_type, ?string $price, ?string $discounted_total = NULL): bool { $items = $this->get_cart(); if(isset($items[$line])) diff --git a/app/Models/Item.php b/app/Models/Item.php index 59afe7e9c..083083c35 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -357,15 +357,10 @@ class Item extends Model */ public function get_info_by_id_or_number(int $item_id, bool $include_deleted = TRUE) { - log_message('info','>>>get_info_by_id_or_number: item_id-' . $item_id); - $builder = $this->db->table('items'); $builder->groupStart(); $builder->where('items.item_number', $item_id); - log_message('info','>>>get_info_by_id_or_number: ctype_digit($item_id)-' . ctype_digit($item_id)); - log_message('info','>>>get_info_by_id_or_number: substr($item_id, 0, 1)-' . substr($item_id, 0, 1)); - // check if $item_id is a number and not a string starting with 0 // because cases like 00012345 will be seen as a number where it is a barcode if(ctype_digit(strval($item_id)) && substr($item_id, 0, 1) != '0') diff --git a/app/Views/receivings/receiving.php b/app/Views/receivings/receiving.php index 399e5df9a..7bb3d3a53 100644 --- a/app/Views/receivings/receiving.php +++ b/app/Views/receivings/receiving.php @@ -149,9 +149,9 @@ if (isset($success)) foreach(array_reverse($cart, TRUE) as $line => $item) { ?> - 'form-horizontal', 'id' => "cart_$line"]) ?> + 'form-horizontal', 'id' => "cart_$line"]) ?> - ') ?> + ') ?>
diff --git a/app/Views/sales/register.php b/app/Views/sales/register.php index 3e3358452..e41d68978 100644 --- a/app/Views/sales/register.php +++ b/app/Views/sales/register.php @@ -176,11 +176,11 @@ if(isset($success)) foreach(array_reverse($cart, TRUE) as $line => $item) { ?> - 'form-horizontal', 'id' => "cart_$line"]) ?> + 'form-horizontal', 'id' => "cart_$line"]) ?> '); + echo anchor(esc("$controller_name/deleteItem/$line"), ''); echo form_hidden('location', $item['item_location']); echo form_input (['type' => 'hidden', 'name' => 'item_id', 'value'=>$item['item_id']]); ?> @@ -426,7 +426,7 @@ if(isset($success))  ' . lang('Common.remove') . ' ' . lang('Customers.customer'), ['class' => 'btn btn-danger btn-sm', 'id' => 'remove_customer_button', 'title' => lang('Common.remove') . ' ' . lang('Customers.customer')] ) @@ -505,7 +505,7 @@ if(isset($success)) if($payments_cover_total) { ?> - 'add_payment_form', 'class' => 'form-horizontal']) ?> + 'add_payment_form', 'class' => 'form-horizontal']) ?> @@ -552,7 +552,7 @@ if(isset($success)) else { ?> - 'add_payment_form', 'class' => 'form-horizontal']) ?> + 'add_payment_form', 'class' => 'form-horizontal']) ?>
@@ -719,18 +719,18 @@ $(document).ready(function() $("#remove_customer_button").click(function() { - $.post("", redirect); + $.post("", redirect); }); $(".delete_item_button").click(function() { const item_id = $(this).data('item-id'); - $.post("" + item_id, redirect); + $.post("" + item_id, redirect); }); $(".delete_payment_button").click(function() { const item_id = $(this).data('payment-id'); - $.post("" + item_id, redirect); + $.post("" + item_id, redirect); }); $("input[name='item_number']").change(function() {