StationID API Problems

General support regarding TimeTrex, such as
configuring policies/taxes or processing payroll.
Locked
EricM
Posts: 30
Joined: Wed Jan 16, 2013 2:22 pm

StationID API Problems

Post by EricM »

I'm testing out intergrating TimeTrex with some of our other software using PHP-SOAP. The sample code worked fine.

When I went to add a test of getUserPunch in api_client_example.php I was told that my station ID is not allowed. I double checked that I had a rule to allow ANY source and ANY station on the server (I did).

I then added a call to getCurrentStation in the sample code to see what my station was. I started to notice that each time I connected, a new Station was created in the web interface on the server, with a selection type of "Only Selected Groups". I would then see the getUserPunch fail because my station was not allowed.

I set one of the stations to "Allow all Groups" "All Branches" and "All Departments", and then hardcoded my getUserPunch to use that station ID. That worked.

Am I supposed to set my station ID ahead of time somewhere in the api_client_example.php? Or is there a way to set the server to consider ANY source and ANY station to include PHP-SOAP?
Attachments
php.png
shaunw
Posts: 7839
Joined: Tue Sep 19, 2006 2:22 pm

Re: StationID API Problems

Post by shaunw »

Code: Select all

getUserPunch( $user_id = NULL, $epoch = NULL, $station_id = NULL, $company_id = NULL ) 
Send getUserPunch() the 3rd parameter as the station_id that you want the punches to use.
EricM
Posts: 30
Joined: Wed Jan 16, 2013 2:22 pm

Re: StationID API Problems

Post by EricM »

Right, I have already done that, and know that works.

I was hoping to find a more generic solution. Aren't there going to be other commands that will need a stationID set? Or is it just getUserPunch?

Do you know why allowing "Any" station and "Any" Source doesn't work in this case?
shaunw
Posts: 7839
Joined: Tue Sep 19, 2006 2:22 pm

Re: StationID API Problems

Post by shaunw »

getUserPunch(), setUserPunch() are pretty much the only functions that require a station ID like this.

Having a wildcard (ANY, ANY) station defined allows all stations of that type to punch in/out, but a station still has to *exist* first, so that is why the station ID is required.
EricM
Posts: 30
Joined: Wed Jan 16, 2013 2:22 pm

Re: StationID API Problems

Post by EricM »

I've found that getUserPunch has some quirks if you specify a user_id other than the current user. It will return the type_id and status_id for the specified user, but the user_id and first_name and last_name of the PHP-SOAP user.

It looks like this is because APIPunch.class.php always assume the current user is equivalent to the user_id. To fix this I changed

$user_obj = $ulf->getCurrent();
to
$user_obj = $ulf->getById($user_id)->getCurrent();

and also changed
'user_id' => $this->getCurrentUserObject()->getId(),
'first_name' => $this->getCurrentUserObject()->getFirstName(),
'last_name' => $this->getCurrentUserObject()->getLastName(),
to
'user_id' => $user_id,
'first_name' => getById( $user_id )->getCurrent()->getFirstName(),
'last_name' => getById( $user_id )->getCurrent()->getLastName(),

in 3 different places.

I still need to look over the file more to see if other changes could be made, and test it a bit.
shaunw
Posts: 7839
Joined: Tue Sep 19, 2006 2:22 pm

Re: StationID API Problems

Post by shaunw »

It sounds to me like you should be using setPunch() instead of setUserPunch(). setUserPunch() is only designed to mimic an actual employees punch, as if you were logged in as that employee. setPunch() can be used to create punches for any employees as if you were an administrator.
EricM
Posts: 30
Joined: Wed Jan 16, 2013 2:22 pm

Re: StationID API Problems

Post by EricM »

I'm attempting to use getUserPunch to get a users next punch, and then setting it with setPunch. Is there an easier way to do this?
shaunw
Posts: 7839
Joined: Tue Sep 19, 2006 2:22 pm

Re: StationID API Problems

Post by shaunw »

You can use the switchUser() function to switch your session to the actual employee, then getUserPunch() doesn't require any modifications. Thats typically how it was designed to work. It shows how to use it at the bottom of the API example file.
EricM
Posts: 30
Joined: Wed Jan 16, 2013 2:22 pm

Re: StationID API Problems

Post by EricM »

It looks like switchuser is failing the following permission check in APIAuthentication.class.php:

if ( $this->getPermissionObject()->Check('company','view') AND $this->getPermissionObject()->Check('company','login_other_user') ) {
(If I comment this out it works fine)

I am currently logging in via PHP-SOAP with an administrator account and have give it all of the permissions that are listed in the Timetrex interface (for testing).

I looked at the database with pgadmin III and do not see 'company' 'view' or 'company' 'login_other_user' in the permissions.

Is there a way to add and enable this in the Timetrex GUI? Or do I need to manually edit my database to get switchuser to work?
shaunw
Posts: 7839
Joined: Tue Sep 19, 2006 2:22 pm

Re: StationID API Problems

Post by shaunw »

I checked with the developers, I guess the functionality I was referring you to is only available in the Business or Professional Editions of TimeTrex.
EricM
Posts: 30
Joined: Wed Jan 16, 2013 2:22 pm

Re: StationID API Problems

Post by EricM »

In APIAuthentication I made it so that only user_id one can switchusers by changing

Code: Select all

if ( $this->getPermissionObject()->Check('company','view') AND $this->getPermissionObject()->Check('company','login_other_user') ) {
to

Code: Select all

if ( $this->getCurrentUserObject()->getId() == '1') {  
Then in APIPunch I changed

Code: Select all

if ( is_object($current_station) AND $current_station->checkAllowed( $user_id, $station_id, $station_type ) == TRUE ) {
to

Code: Select all

if ( '1'  == '1' ) {
to allow all stations

And then I commented out the 3 instances of:

Code: Select all

//'station_id' => $current_station->getId(),

This seems to enable all the API functionality for punching in and out for my application.

Here is the example code I am using to punch people in and out.

Code: Select all

echo "\nScanner Ready\n";
$input = fgets(STDIN, 1024);
$api_session = new TimeTrexClientAPI();
$api_session->Login( $TIMETREX_USERNAME, $TIMETREX_PASSWORD );
if ( $TIMETREX_SESSION_ID == FALSE ) {
	echo "Login Failed!<br>\n";
	exit;
}
echo "Session ID: $TIMETREX_SESSION_ID<br>\n";
$user_obj = new TimeTrexClientAPI( 'User' );
$result = $user_obj->getUser(array('filter_data' => array('employee_number' => $input)));
$user_data = $result->getResult();
print "#{$user_data[0]['id']}: {$user_data[0]['last_name']}, {$user_data[0]['first_name']}\n";
$auth_obj = new TimeTrexClientAPI( 'Authentication' );
$auth_obj->switchUser( $user_data[0]['id'] );
$result = $auth_obj->getCurrentUser();
$punch_obj = new TimeTrexClientAPI( 'Punch' );
$result = $punch_obj->getUserPunch();
$result = $result->getResult();
$result = $punch_obj->setUserPunch($result);
It would be helpful if the API was clearer about what functionality is supposed to be available in each version of TimeTrex. Otherwise it is hard to tell what is broken, and what is just supposed to be paid for.
Locked