opensourcepos / opensourcepos

Open Source Point of Sale is a web based point of sale application written in PHP using CodeIgniter framework. It uses MySQL as the data back end and has a Bootstrap 3 based user interface.
http://www.opensourcepos.org
Other
3.44k stars 2.18k forks source link

Add error dialog when adding an item to a sale fails #3362

Open chunter2 opened 2 years ago

chunter2 commented 2 years ago

Installation information

Issue / Bug / Question / New Feature

I'm looking for a way to have a dialog box pop up in front of the sales module if an item fails to add for any reason. Currently a red bar comes up with the 'Item add to Sale failed' message but I'm noticing if the cashier isn't watching the screen they carry on scanning barcodes and that item gets missed. I'd be nice to have something they need to click to carry on with the sale. Also would there be a way to log this if it isn't already logged?

Thanks

chunter2 commented 2 years ago

@odiea As a start do you know of a way to log the current error message to the server? I have logging enabled on the server but when I force the error message to come up in the browser I don't see anything specific in the log file.

odiea commented 2 years ago

Sorry I am not a programmer and I do not believe that these error messages get into the error log.

chunter2 commented 2 years ago

@jekkos @objecttothis Would either of you know how to get this error message into the log file? I did a quick search of the code but didn't find an example of what I was looking for.

objecttothis commented 2 years ago

This kind of thing should be logged if it isn't already but it would be in the OSPOS log, not the server log.

chunter2 commented 2 years ago

@objecttothis Just reading this page https://github.com/opensourcepos/opensourcepos/wiki/OSPOS-DEVEL-for-Error-Logging-in-OSPOS but I'm not seeing the OSPOS log. Is this still a server side log file?

objecttothis commented 2 years ago

The logs are stored either in /public/logs/ or /application/logs/. I can't remember off the top of my head which. In order for the logs to be generated you need to enable the logging in the config file and the webserver needs to have access to the /logs/ folder.

chunter2 commented 2 years ago

@objecttothis I had already enabled logging by setting $config['log_threshold'] = 4 in application/config/config.php and this got me files like log-2021-11-19.php in the application/logs folder of ospos but I don't see any other log files in there. I'm running Ubuntu 20.04 with Apache. Is there something with Apache I need to enable?

objecttothis commented 2 years ago

Apache, PHP, mysql, etc all do their own logging. The only OSPOS stuff you'd see in the webserver logs would be things like coding errors. You won't see anything likely to do with an item not being added like what you are asking for outside of OSPOS logs. If they aren't in there then they should be added. I would need to pinpoint the cause of why the item doesn't get added to tell you where in the code you need to trigger the logging. The logging function itself is very easy. Just one line. log_message('ERROR', [Message]);

chunter2 commented 2 years ago

@objecttothis I was able to get it to work. The error message was in the add function in the application/controllers/Sales.php file. I was able to add the log_message function right after the $data['error'] line and then trigger the error from the sales module and I now see the message in the log file. Now I can see how often this happens. Thanks again for the help.

As a side note I searched for 'error' with the quotes in the code and only found 18 of them only in application/controllers files but none of them seem to be logged.

chunter2 commented 2 years ago

@objecttothis Next question, I'd like to add the value entered in the item box of the sales module to the log_message function when it fails to add the item. I see this in the application/controllers/Sales.php file

$item_id_or_number_or_item_kit_or_receipt = $this->input->post('item');

but when I print it out like this

log_message('ERROR','Unable to add item ' . $item_id_or_number_or_item_kit_or_receipt);

it just shows up as -1.

I'd like to print it out because I suspect there's more issues than just scanning two quickly.

Thanks

objecttothis commented 2 years ago

Hi @chunter2. To preface, I'd like to encourage you to submit your enhancements as pull requests. By doing so, you contribute to the overall application for the community. I'd be glad to get you started... once you've done it once or twice it really is pretty easy.

OK, now to your message:

@objecttothis Next question, I'd like to add the value entered in the item box of the sales module to the log_message function when it fails to add the item. I see this in the application/controllers/Sales.php file

$item_id_or_number_or_item_kit_or_receipt = $this->input->post('item');

but when I print it out like this

log_message('ERROR','Unable to add item ' . $item_id_or_number_or_item_kit_or_receipt);

it just shows up as -1.

I'd like to print it out because I suspect there's more issues than just scanning two quickly.

Thanks

A couple things:

1- String interpolation is usually better here than concatenation, but given how ... unfortunate that variable name is, I'd probably leave it with the concatenation you have there.

2- If it prints as ERROR: Unable to add item -1. it's printing exactly what the value of that variable is at the point that you are inserting the log message. You are correct that there is likely something more going on. -1 is the standard value for a new item... in fact if should already be a global variable in the code. If this were happening in the items controller that would be one thing, but I think you are saying this is happening in the sales module, so anything that gets scanned into the search there should already be an item in the system. It can't add something to the register that doesn't exist. 3.3.2 is an older version, so I recommend you try making a copy of your database and seeing if you can recreate this against 3.3.4

When you're testing, you can use var_dump to try to narrow down the cause of the problem and the exact lines of code. The very best option is to use xdebug to set break points and step through the code, but if you can't do that then use var_dump. Then recreate the issue. Here is a gist that I created that shows you an example of using var_dump to dump the contents of a variable to the log and see what it's doing.

https://gist.github.com/objecttothis/81fcfa6d85d8e01d8a7e99884bdb2a3c

If you can pinpoint where the issue is in the Sales Controller Add function then that will go a long way in figuring this out. Some of the questions that I would be asking of your staff are what the item numbers are that they experience this problem on, then try to recreate it yourself... it's possible that it's something about the barcode number that is causing the issue with the add.

chunter2 commented 2 years ago

@objecttothis Thanks for the encouragement. I have done a couple pull requests but only for bug fixes or other small additions. I've managed to get myself out of sync with master since our system is live. I don't have a development environment, just a test system running on a raspberry pi and a production system running on a PC. My plan is to push the latest (hopefully 3.3.6) to my production system with my custom changes and then create pull requests for them. Most of the changes are related to the sales module. I'm questioning a configuration page specifically for the sales module since most of my changes involve commenting out parts of the code.

For my log message output, yes it's printing -1. What I'm really trying to do is print the actual string that was inputted in the item box, I think I'm too far down the chain in the code for where the logging is. When I test I know what I'm typing is not in the items list but I'd like to see that string in the log file. My excuse for all the questions is I'm more of a C/C++ developer so this php/web based code is a fair bit out of my comfort zone. :) But I'm trying.

objecttothis commented 2 years ago

I haven't looked at the code but I'm guessing that at some point what is passed from the view is sent to an exists() function. You could start there to log the input.

objecttothis commented 2 years ago

@objecttothis Thanks for the encouragement. I have done a couple pull requests but only for bug fixes or other small additions. I've managed to get myself out of sync with master since our system is live. I don't have a development environment, just a test system running on a raspberry pi and a production system running on a PC. My plan is to push the latest (hopefully 3.3.6) to my production system with my custom changes and then create pull requests for them. Most of the changes are related to the sales module. I'm questioning a configuration page specifically for the sales module since most of my changes involve commenting out parts of the code.

For my log message output, yes it's printing -1. What I'm really trying to do is print the actual string that was inputted in the item box, I think I'm too far down the chain in the code for where the logging is. When I test I know what I'm typing is not in the items list but I'd like to see that string in the log file. My excuse for all the questions is I'm more of a C/C++ developer so this php/web based code is a fair bit out of my comfort zone. :) But I'm trying.

No worries. I've spent the majority of my development time in compiled languages (C++, C# and Java) so I completely understand where you're coming from. I don't know if you're familiar with MVC, but OSPOS is built on CodeIgniter which uses the MVC paradigm. Getting a feel for that will help you to predict the path between different parts of the application and make the debugging a little quicker for you.

chunter2 commented 2 years ago

@objecttothis I found what I was looking for. Turns out the item_id_or_number_or_item_kit_or_receipt variable is an input/output variable. I added a log message to the add_item function in application/libraries/Sale_lib.php before it gets set to -1 and I now see the string I'm looking for.

I'm suspecting our cashiers are somehow able to get the "hint" string (Start typing Item Name or scan Barcode) initially loaded in the items text box to stay and then the barcode is added to it. I'm able to reproduce this by tabbing around the page until I get to the item text box and then pushing the right arrow key to go to the end. I know for sure they're not doing that but there must be some other way they're doing it.

chunter2 commented 2 years ago

@objecttothis After a couple days of observation it looks like there could be two issues. One being the "hint" I mentioned above and the other being the cashiers scanning too fast. Basically I see the end of the barcode being printed out to the log file and it can be any number of characters log. I think what's happening is the page hasn't refreshed before the next barcode is scanned and since the barcode scanner is basically a keyboard, only the end is making it into the webpage. I'm now thinking playing some sort of sound when this happens may be a better solution than a dialog box they have to click on. Or maybe both. Any chance there's a quick way to play a sound in OSPOS/CodeIgniter?

Thanks

objecttothis commented 2 years ago

@objecttothis After a couple days of observation it looks like there could be two issues. One being the "hint" I mentioned above and the other being the cashiers scanning too fast. Basically I see the end of the barcode being printed out to the log file and it can be any number of characters log. I think what's happening is the page hasn't refreshed before the next barcode is scanned and since the barcode scanner is basically a keyboard, only the end is making it into the webpage. I'm now thinking playing some sort of sound when this happens may be a better solution than a dialog box they have to click on. Or maybe both. Any chance there's a quick way to play a sound in OSPOS/CodeIgniter?

Thanks

Maybe. I haven't looked into it, but I would be surprised if it wasn't possible. A couple things may resolve the issue:

chunter2 commented 2 years ago

I was able to get sound working. The top of the add_item function in application/libraries/Sale_lib.php now looks like this.

$item_info = $this->CI->Item->get_info_by_id_or_number($item_id, $include_deleted);
$sound_file_name = "error.mp3";

//make sure item exists
if(empty($item_info))
{
    echo '<audio autoplay="true" style="display:none;"><source src="'.$sound_file_name.'"></audio>';
    log_message('ERROR','Empty item = ' . $item_id);
    $item_id = -1;
    return FALSE;
}

I now play a sound and log the error. We'll see if any of the cashiers jump when they hear the sound. :)

I think speeding up the interface will help a lot but I think disabling the textbox may not since a new scan will just skip completely. Since they don't always look at the screen they just won't notice. Looking forward to your work in CI4.

odiea commented 2 years ago

@chunter2 Where did you place your error.mp3 file?

chunter2 commented 2 years ago

@odiea I just put it in the public folder. So for me

/var/www/html/ospos/public

It's just the Windows XP error sound. :) I downloaded it from here. https://www.myinstants.com/media/sounds/erro.mp3

odiea commented 2 years ago

Thanks. Works great