SambaPOS API Integration with NewBook PMS/Booking System


#104

@QMcKay as an alternative that will work too. We have access to .net DateTime object for easier date handling.

function dateTest()
{
   var d = DateTime.Now;
   return d.ToString("dd.MM.yyyy hh:mm:ss.fff");
} 

ref: https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx


#105

LOL SambaPOS gone wild.

What I’m working for few days is just scripting.


#106

A post was merged into an existing topic: SambaPOS 5.1.48


#107

So I tested it and it works. There is one hiccup. I am now trying to parse the data and it is surrounded by Brackets. I am trying to strip the brackets but I know I have the syntax wrong. Can someone take a look.

 var response = respone.replace(/^.*string\[^\].*"(.*)"$/, '$1');

 var responseObject = JSON.parse(response);

I know that syntax is wrong and I have tried to fix it but it seems I cant get the right syntax for replace. All i need to do is strip the [ ] from the data.


#108

Nevermind I figured out I did not need to do that.

@JTRTech here is a sample script.

This gets site_id from site_list

 function JSONTest(raction)

{

 var url = 'https://testau.newbookpms.com/rest/'+raction;

 var api = '{\"api_key\":\"instances_xxxxxxxxxxxxxxx\"}';

 var username = 'sambapos';

 var password = 'xxxxxxxxxxxxxx';

 var response = web.PostJson(url,api,username,password);

 var responseObject = JSON.parse(response);

 return responseObject[0].site_id;

}

for test type JSTONTest('site_list')


#109

:smile: looking good, had to go out last night so didnt get chance to go any further with .48.
Should be interesting tonight - quite excited :smile:


#110

Its working! :grinning: :laughing: :blush: :stuck_out_tongue_closed_eyes:
Cant beleive code is that simple however imagine there is a fair bit of extra code behind the web.PostJson helper :smile:
Thats to all of you for your help, am so so so relived, after Tuesday I was so depressed after hours of research and not getting anyway.
Thanks emre for adding that helper code :slight_smile:

Now for the FUN big and configuring the the actual data integration in Samba which is the bit I was actually looking forward to.

First thing I want to do is get the Room Entities to pull the guest names but have a question on the best method to do this:
My first thought is to start with a more basic manual update per room to get started although this is probably not ideal for a production use.
My question is if I use a script call in a custom entity field at what point foes that script get called?

  • Entity screen open?
  • Entity selection?
  • Entity edit?

Also am trying to find the best topic for use of {CALL:x} so I can try and keep scripts as flexible as possible using variables within the call tag.


#111

Am just keeping notes here don’t pay too much attention - if you do read and see any mistakes please let me know :wink:
PS @kendash thanks for the postcode configuration task, am just dissecting the call finction in that which is proving very useful :smile:


{ CALL : <handler> . <function> ( [variable] ) }
{ CALL : <handler> . <function> ( ['value'] ) }

Entity: Name [ Room 1 ]
Field: NewBook SiteName [ 101 ]
Field: Guest Name [ {CALL:newbook.roombooking(’{ENTITY DATA:NewBook Site Name}’ ]


#112

We are using Jscript but not pure form so sometimes we may need helpers developed to accomplish our goal. This evolves scripts even further. Scripts are probably the most powerful aspect of sambapos.


#113

OK, first question on the script;

To pull a particular rooms/booking info I need to add a second JSON line of data.

Documentation says:
{
“api_key”: “your_api_key_here”,
“site_name”: “xxxxxx”
}

Now I presume that the values in the web.PostJson(url,api,username,password) are in a specific order of url,api,username,password so api would in reality be data and I will want to build a ‘data’ variable;

This is what I have at the minute;

function roombooking(sitename)
{
 var url = 'https://testau.newbookpms.com/rest/site_inhouse_booking';
 var username = 'sambapos';
 var password = 'xxxxxxxxxxxxx';
 
// var api = \"api_key\":\"instances_xxxxxxxxxxxxx\";

 var jsondata = '{\"api_key\":\"instances_xxxxxxxxxxx\",\"site_name\":\"xxxxxx\"}';
 var response = web.PostJson(url,jsondata,username,password);
 var responseObject = JSON.parse(response);
 return response;
}

And testing with roombooking() gives expected result of the booking data inn JSON format. :smile:

HOWEVER obviously from the function roombooking(sitename) my intention is to feed the sitename in to the jsondata but

 var jsondata = '{\"api_key\":\"instances_xxxxxxxxxxx\",\"site_name\":\"sitename\"}';

Doesnt work, presumably because of the ’ ’ quotes arround the JSON { } - without them you get invalid character.

Any advice?
How can I set a variable within the JSON data?


Thought id try splitting the ’ ’ quoted sections;

 var jsondata = '{\"api_key\":\"instances_xxxxxxxxxxxxx\",\"site_name\":\" ' sitename ' \"}';

(have added a little spacing for viewing - actual code has no extra spacing around the quotes.
with test of

roombooking('xxxxxxx')

but get expected ‘;’ error


#114

SORRY SOLVED IT
Thanks to kendash Google PostCode scripts again, realized i have to use + to mix values and variables :slight_smile:


#115

Well you kind of went about it in a complicated way. Look at my example it shows that I pulled the data from site_list and I accessed the value for site_id it returned 1.

 function JSONTest(raction)

{

 var url = 'https://testau.newbookpms.com/rest/'+raction;

 var api = '{\"api_key\":\"instances_xxxxxxxxxxxxxx\"}';

 var username = 'sambapos';

 var password = 'xxxxxxxxx';

 var response = web.PostJson(url,api,username,password);

 var responseObject = JSON.parse(response);

 return responseObject[0].site_id;

}

I used raction and I fed site_list to raction. site_list is a request as documented in the api documentation.

So lets say I have a Sambapos button and I want it to show the Site ID of your Newbooks site_list. Using the script above I would simply type:

{CALL:tests.JSTONTest('site_list')}

#116

The issue was sending a second json data line to call the data from that id on the site_inhouse_booking url;
That will then return a json responce on info for the booking in that site name like guest name etc.


#117

read what I just said. Ok I will do another example showing a room id. One moment


#118

I did,
However im looking at https://testau.newbookpms.com/rest/site_inhouse_booking
I am not trying to receive a site_id from the site list request.

In order to pull a bookings info i need to specify the siten_name as a second json line;

{
"api_key": "your_api_key_here",
"site_name": "xxx"
}

which gives the boking info for that 'room/site’
Your bit is what I need to use to pull the guest first and second name from that responce :smile:

function roombooking2(sitename)
{
 var url = 'https://testau.newbookpms.com/rest/site_inhouse_booking';
 var username = 'sambapos';
 var password = 'xxxxxxxxxxxxxxxx';
// var api = \"api_key\":\"instances_xxxxxxxxxxxxxx\";
 var jsondata = '{\"api_key\":\"instances_xxxxxxxxxxxxxx\",\"site_name\":\"'+sitename+'\"}';
 var response = web.PostJson(url,jsondata,username,password);
 var responseObject = JSON.parse(response);
 return response;
}

is working for me, just need to tidy up the code :smile:

    function roombooking2(sitename)
{
 var url = 'https://testau.newbookpms.com/rest/site_inhouse_booking';
 var username = 'sambapos';
 var password = 'xxxxxxxxxxxxxxxxx';
 var api = '\"instances_xxxxxxxxxxxxxxxxxxx\"';
 var jsondata = '{\"api_key\":'+api+',\"site_name\":\"'+sitename+'\"}';
 var response = web.PostJson(url,jsondata,username,password);
 var responseObject = JSON.parse(response);
 return response;
}

looking a little cleaner :smile:


#119

function JSONTest2(sitebooking,site)

{

 var url = 'https://testau.newbookpms.com/rest/'+sitebooking;

 var sitename = '\"site_name\":\"'+site+'\"';

 var api = '{\"api_key\":\"instances_xxxxxxxxxxxxxxx\",'+sitename+'}';

 var username = 'sambapos';

 var password = 'xxxxxxxxxx';

 var response = web.PostJson(url,api,username,password);

 var responseObject = JSON.parse(response);

 return responseObject.booking_id;

}

You can declare your variables for API and Site_Name globally if that makes it easier.

See how I fed site name its value?

The sambapos call for this would be {CALL:X.JSONTest2('site_inhouse_booking','xxxxx')}


#120

This is what I have at the minute;

function bookingdetails(sitename,detail)
{
 var url = 'https://testau.newbookpms.com/rest/site_inhouse_booking';
 var username = 'sambapos';
 var password = 'xxxxxxxxx';
 var api = 'instances_xxxxxxxxxxxxxx';
 var json_api_line = '"api_key":"'+api+'"';
 var json_sitename_line = '"site_name":"'+sitename+'"';
 var jsondata = '{'+
 				json_api_line +','+
 				json_sitename_line
 				+'}';
 var response = web.PostJson(url,jsondata,username,password);
 var responseObject = JSON.parse(response);
 return responseObject.???;
}

Obviously am working on the responce object bit at the moment :smile:

PS, have found the \ backslach isnt required, presumably because the " quoes are within the ’ quote.


#121

My nest question is can I simplify the second variable using variables within the script?
Easier to show in code;

So if my test call is;

bookingdetails('DC001',pax)

into this script;

    function bookingdetails(sitename,detail)
    {
     var url = 'https://testau.newbookpms.com/rest/site_inhouse_booking';
     var username = 'sambapos';
     var password = 'xxxxxxxxxxx';
     var api = 'instances_xxxxxxxxxxxxxxx';
     var json_api_line = '"api_key":"'+api+'"';
     var json_sitename_line = '"site_name":"'+sitename+'"';
     var jsondata = '{'+
     				json_api_line +','+
     				json_sitename_line
     				+'}';
     var response = web.PostJson(url,jsondata,username,password);
     var status = '"booking_status"';
     var limit = '"auto_billing_limit"';
     var pax = '"booking_adults"';
     var responseObject = JSON.parse(response);
     return responseObject.detail;
    }

Does that make sense?


#122

I dont think I understand that script. What is variables status, limit, and pax going to do? and where did you get responseObject.detail from?


#123

With bookingdetails('xxxxx',pax) is the no ’ ’ quoted value not become a variable?