Skip to content

Instantly share code, notes, and snippets.

@emreeren
Last active February 9, 2019 11:09
Show Gist options
  • Save emreeren/1894194f2f8fe9a179ff to your computer and use it in GitHub Desktop.
Save emreeren/1894194f2f8fe9a179ff to your computer and use it in GitHub Desktop.
ukpostcodes
{"AccountScreens":null,"AccountTransactionDocumentTypes":null,"AccountTransactionTypes":null,"AccountTypes":null,"Accounts":null,"Actions":null,"AutoConfigurationTasks":[{"CreateBackup":true,"Description":"Enables UK Post Code lookup feature. Uses free Google Maps service and resolves Post Codes to Street level.","Language":"en","Name":"Enable UK Post Code Lookup","SortOrder":50,"TaskData":"[{\"Action\":\"dlg.AskQuestion(\\u000d\\u000a \\\"You'll need a Google Maps API Key to enable this feature.\\\",\\u000d\\u000a \\\"OK\\\",\\\"\\\",\\\"#CC000000\\\",0,\\u000d\\u000a \\\"You can visit <link https:\\\/\\\/developers.google.com\\\/maps\\\/documentation\\\/javascript\\\/get-api-key>Google Maps<\\\/link> website to have more info about creating an API Key.\\\")\\u000d\\u000aresult = '[?Google API Key]'\",\"ActionType\":4,\"Condition\":null,\"Description\":\"Ask Google Maps API Key\"},{\"Action\":\"var result = 'Customers';\\u000d\\u000aif(!sql.Exists(\\\"Select * from EntityTypes where Name='Customers'\\\"))\\u000d\\u000a{\\u000d\\u000a var entityTypes = sql.Query('select Name from EntityTypes').Join(',');\\u000d\\u000a result = dlg.AskQuestion('Select Entity Type',entityTypes);\\u000d\\u000a}\\u000d\\u000aData.Set('EntityType',result);\",\"ActionType\":1,\"Condition\":null,\"Description\":\"Ask Customer Entity Type\"},{\"Action\":\"var apiKey = '[:Google API Key]';\\u000d\\u000aif(!apiKey){\\u000d\\u000a dlg.ShowMessage('Google API Key Not Entered');\\u000d\\u000a}\\u000d\\u000a\\u000d\\u000avar entityType = Data.Get('EntityType');\\u000d\\u000aif(entityType == undefined)\\u000d\\u000a{\\u000d\\u000a dlg.ShowMessage('Entity Type Not Selected');\\u000d\\u000a}\\u000d\\u000avar result = entityType != undefined && apiKey != '';\",\"ActionType\":3,\"Condition\":null,\"Description\":\"Test if an entity type selected\"},{\"Action\":\"var entityType = Data.Get('EntityType');\\u000d\\u000avar Id = sql.Query(\\\"select Id from EntityTypes where Name = '\\\"+entityType+\\\"'\\\").First;\\u000d\\u000aData.Set('EntityTypeId',Id);\",\"ActionType\":1,\"Condition\":null,\"Description\":\"Read Entity Type Id\"},{\"Action\":\"function CreateField(entityTypeId,fieldName)\\u000d\\u000a{\\u000d\\u000a var exists = sql.Exists(\\\"select Id from EntityCustomFields where Name = '\\\"+fieldName+\\\"' and EntityTypeId = \\\"+entityTypeId);\\u000d\\u000a if (!exists)\\u000d\\u000a sql.ExecSql(\\\"Insert into EntityCustomFields (Name,Hidden,FieldType,MaskType,EntityTypeId) Values ('\\\"+fieldName+\\\"',0,3,0,\\\"+entityTypeId +\\\")\\\");\\u000d\\u000a}\\u000d\\u000a\\u000d\\u000avar entityTypeId = Data.Get('EntityTypeId');\\u000d\\u000avar postCodeFieldName = 'PostCode';\\u000d\\u000avar cfExists = sql.Exists(\\\"Select Id from EntityCustomFields where EntityTypeId =\\\" + entityTypeId +\\\" and Name = '\\\"+postCodeFieldName+\\\"'\\\");\\u000d\\u000aif(!cfExists)\\u000d\\u000a sql.ExecSql(\\\"Insert into EntityCustomFields (Name,Hidden,FieldType,MaskType,EntityTypeId) Values ('\\\"+postCodeFieldName+\\\"',0,3,4,\\\"+entityTypeId +\\\")\\\");\\u000d\\u000avar valueSource = \\\"{CALL:postCode.Read(''$1'')}\\\";\\u000d\\u000asql.ExecSql(\\\"Update EntityCustomFields set MaskType = 4,ValueMappings = '\\\"+postCodeFieldName+\\\"=$1;Street=$2;Town=$3;County=$4', EditingFormat = '(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})',ValueSource = '\\\"+valueSource+\\\"' where Name = '\\\"+postCodeFieldName+\\\"' and EntityTypeId = \\\"+entityTypeId);\\u000d\\u000a\\u000d\\u000aCreateField(entityTypeId,'Street');\\u000d\\u000aCreateField(entityTypeId,'Town');\\u000d\\u000aCreateField(entityTypeId,'County');\",\"ActionType\":1,\"Condition\":null,\"Description\":\"Create Custom Fields\"},{\"Action\":\"Scripts:\\u000d\\u000a- Name: Postcode API\\u000d\\u000a HandlerName: postCode\\u000d\\u000a Code: \\\"function Read(postcode)\\\\r\\\\n{\\\\r\\\\n var YOUR_API_KEY = '[:Google API Key]';\\\\r\\\\n var country = 'GB';\\\\r\\\\n var urlfmt = 'https:\\\/\\\/maps.googleapis.com\\\/maps\\\/api\\\/geocode\\\/json?key='+ YOUR_API_KEY + '&components=postal_code:'+postcode;\\\\r\\\\n\\\\r\\\\n if(country != undefined) \\\\r\\\\n urlfmt += '|country:'+country;\\\\r\\\\n var content = web.Download(urlfmt);\\\\r\\\\n var obj = JSON.parse(content);\\\\r\\\\n var lat = obj.results[0].geometry.location.lat;\\\\r\\\\n var lng = obj.results[0].geometry.location.lng;\\\\r\\\\n\\\\r\\\\n var addrurl = 'https:\\\/\\\/maps.googleapis.com\\\/maps\\\/api\\\/geocode\\\/json?key=' + YOUR_API_KEY + '&latlng=' + lat + ',' + lng + '&sensor=false';\\\\r\\\\n\\\\r\\\\n var addr = web.Download(addrurl);\\\\r\\\\n var addrObject = JSON.parse(addr);\\\\r\\\\n\\\\r\\\\n var data = GetAddressComponents(addrObject.results,postcode);\\\\r\\\\n if(data == null) return postcode;\\\\r\\\\n\\\\r\\\\n var street = ReadComponent('route',data);\\\\r\\\\n if(street == '-')\\\\r\\\\n street = ReadComponent('locality',data);\\\\r\\\\n if(street == '-')\\\\r\\\\n street = ReadComponent('administrative_area_level_4',data);\\\\r\\\\n var town = ReadComponent('postal_town',data);\\\\r\\\\n var county = ReadComponent('administrative_area_level_2',data);\\\\r\\\\n\\\\r\\\\n return postcode + ',' + street + ',' + town + ',' + county;\\\\r\\\\n}\\\\r\\\\n\\\\r\\\\nfunction GetAddressComponents(results,postcode)\\\\r\\\\n{\\\\r\\\\n for(i=0;i < results.length;i++)\\\\r\\\\n {\\\\r\\\\n for(j=0; j< results[i].address_components.length;j++)\\\\r\\\\n {\\\\r\\\\n var component = results[i].address_components[j];\\\\r\\\\n if(component.long_name.replace(' ','') == postcode.replace(' ',''))\\\\r\\\\n return results[i].address_components;\\\\r\\\\n }\\\\r\\\\n }\\\\r\\\\n return null;\\\\r\\\\n}\\\\r\\\\n\\\\r\\\\nfunction ReadComponent(name,components)\\\\r\\\\n{\\\\r\\\\n for(i=0;i<components.length;i++)\\\\r\\\\n {\\\\r\\\\n var component = components[i];\\\\r\\\\n \\\\r\\\\n for(j=0;j<component.types.length;j++)\\\\r\\\\n {\\\\r\\\\n if(component.types[j] == name)\\\\r\\\\n return component.long_name;\\\\r\\\\n } \\\\r\\\\n }\\\\r\\\\n \\\\r\\\\n return '-';\\\\r\\\\n}\\\"\\u000d\\u000a\",\"ActionType\":0,\"Condition\":null,\"Description\":\"Import Scripts\"}]"}],"AutomationCommands":null,"CalculationSelectors":null,"CalculationTypes":null,"ChangePaymentTypes":null,"CustomReports":null,"Departments":null,"EntityScreens":null,"EntityTypes":null,"Numerators":null,"OrderTagGroups":null,"PaymentTypes":null,"PrintJobs":null,"PrinterTemplates":null,"Printers":null,"ProgramSettingValues":null,"Rules":null,"Scripts":null,"States":null,"TaskTypes":null,"TicketTagGroups":null,"TicketTypes":null,"Triggers":null}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment