1. Go to this page and download the library: Download fieldnation/fieldnation-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fieldnation / fieldnation-sdk example snippets
$fnCompanyId = $_ENV['FIELD_NATION_COMPANY_ID'];
$fnApiKey = $_ENV['FIELD_NATION_API_KEY'];
$fnEffectiveUser = $_ENV['FIELD_NATION_EFFECTIVE_USER']; // optional - First admin user will be used if not provided.
$credentials = new \FieldNation\LoginCredentials($fnCompanyId, $fnApiKey, $fnEffectiveUser);
$fn = new \FieldNation\SDK($credentials);
$fnCompanyId = $_ENV['FIELD_NATION_COMPANY_ID'];
$fnApiKey = $_ENV['FIELD_NATION_API_KEY'];
$credentials = new \FieldNation\LoginCredentials($fnCompanyId, $fnApiKey, $fnEffectiveUser);
$credentials->setEnvironment("https://stable.fieldnation.com");
$fn = new \FieldNation\SDK($credentials);
class MyBusinessTicket
{
private $id;
private $title;
private $description;
private $startDateTime;
// ... setters and getters for the properties
}
use FieldNation\WorkOrderSerializerInterface;
class MyBusinessTicket implements WorkOrderSerializerInterface
{
private $id;
private $title;
private $description;
private $startDateTime;
private $fieldNationId;
// ... setters and getters for the properties
// WorkOrderSerializerInterface methods
/**
* Get the name of the project the work order should be a member of.
*
* If not present, the work order will not belong to a project (default behavior).
* If the project does not already exist, it will be created automatically and your effectiveUser
* (See Login structure) will be the default manager for all newly-created projects.
*
* @return string
*/
public function getGroup()
{
return NULL;
}
/**
* Get the general descriptive information relevant to the job.
*
* @return FieldNation\ServiceDescriptionInterface
*/
public function getDescription()
{
$description = new \FieldNation\ServiceDescription();
$description->setCategoryId(1); // See docs for category IDs
$description->setTitle($this->title);
$description->setDescription($this->description);
return $description;
}
/**
* Get where the job site is located.
*
* @return FieldNation\ServiceLocationInterface
*/
public function getLocation()
{
// My business only has one service location
$locationType = \FieldNation\LocationTypes::COMMERCIAL;
$location = new \FieldNation\ServiceLocation();
$location->setType($locationType);
$location->setName('Foo Co');
$location->setAddress1('123 Main Street');
$location->setCity('Minneapolis');
$location->setState('MN');
$location->setPostalCode('55402');
$location->setCountry('US');
$location->setContactName('Bob');
$location->setContactEmail('[email protected]');
$location->setContactPhone('612-555-3485');
return $location;
}
/**
* Get scheduling information for the Work Order, including any applicable end time.
*
* @return FieldNation\TimeRangeInterface
*/
public function getStartTime()
{
$time = new \FieldNation\TimeRange();
$time->setTimeBegin($this->startDateTime);
return $time;
}
/**
* Get payment details to be advertised on the Work Order.
* @return FieldNation\PayInfoInterface
*/
public function getPayInfo()
{
// All of our tickets are a flat $20 rate with a 5 hour max
$info = new \FieldNation\PayInfo();
$rate = new \FieldNation\RatePay();
$rate->setRate(20.0);
$rate->setMaxUnits(5.0);
$info->setPerHour($rate);
return $info;
}
/**
* Get whether to allow the technician to upload files to the Work Order.
* If enabled, this Work Order will inherit
$fnCompanyId = $_ENV['FIELD_NATION_COMPANY_ID'];
$fnApiKey = $_ENV['FIELD_NATION_API_KEY'];
$fnEffectiveUser = $_ENV['FIELD_NATION_EFFECTIVE_USER'];
$credentials = new \FieldNation\LoginCredentials($fnCompanyId, $fnApiKey, $fnEffectiveUser);
$fn = new \FieldNation\SDK($credentials);
$myTicket = new MyBusinessTicket();
$myTicket->setId(uniqid());
$myTicket->setTitle('Fix something at this place');
$myTicket->setDescription('Something went wrong. Fix it.');
$myTicket->setStartDateTime(new \DateTime());
// Returns a \FieldNation\WorkOrderInterface object
$fnWorkOrder = $fn->createWorkOrder($myTicket);
$myTicket->setFieldNationId($fnWorkOrder->getId());
// Just created a work order
$fnCompanyId = $_ENV['FIELD_NATION_COMPANY_ID'];
$fnApiKey = $_ENV['FIELD_NATION_API_KEY'];
$fnEffectiveUser = $_ENV['FIELD_NATION_EFFECTIVE_USER'];
$credentials = new \FieldNation\LoginCredentials($fnCompanyId, $fnApiKey, $fnEffectiveUser);
$fn = new \FieldNation\SDK($credentials);
$myTicket = new MyBusinessTicket();
$myTicket->setId(uniqid());
$myTicket->setTitle('Fix something at this place');
$myTicket->setDescription('Something went wrong. Fix it.');
$myTicket->setStartDateTime(new \DateTime());
// Returns a \FieldNation\WorkOrderInterface object
$fnWorkOrder = $fn->createWorkOrder($myTicket);
$myTicket->setFieldNationId($fnWorkOrder->getId());
// Fetching a work order after it was created
$ticket = $db->getTicket(1234); // pseudo code for fetching your ticket
$fnWorkOrder = $fn->getExistingWorkOrder($ticket->fnId);
/**
* Create a provider object to route to
*/
$provider = new \FieldNation\Technician();
$provider->setId('1');
/**
* @returns ResultInterface
*/
$result = $fnWorkOrder->routeTo($provider);
/**
* Create a group to route to
*/
$group = new \FieldNation\Group();
$group->setId('100');
/**
* @returns ResultInterface
*/
$result = $fnWorkOrder->routeTo($group);
/**
* Create your document.
*/
$document = new \FieldNation\Document();
$document->setTitle('Instructions');
$document->setType('application/pdf');
$document->setUpdateTime(new \DateTime('now', new \DateTimeZone('UTC')));
/**
* @returns ResultInterface
*/
$result = $fnWorkOrder->attach($document);
/**
* Create the document object
*/
$document = new \FieldNation\Document();
$document->setTitle('Instructions');
/**
* @returns ResultInterface
*/
$result = $fnWorkOrder->detach($document);
/**
* One of your custom fields
*/
$field = new \FieldNation\CustomField();
$field->setName('My Business Ticket ID');
/**
* @returns ResultInterface
*/
$result = $fnWorkOrder->addAdditionalField($field);
// If you don't have the Field Nation shipment id, you can get it with your tracking number
$result = $fn->getShippingIdFrom('my-tracking-number');
$shipment = new \FieldNation\Shipment();
$shipment->setId($result->getShipmentId());
/**
* @returns ResultInterface
*/
$result = $fnWorkOrder->deleteShipment($shipment);
/**
* Optionally you can filter your query by the status of the work order.
* If the status is NULL we'll return all work orders.
* You can get statuses from the \FieldNation\WorkOrderStatuses class
*/
$status = \FieldNation\WorkOrderStatuses::PUBLISHED
/**
* @returns \FieldNation\WorkOrderInterface[]
*/
$workOrders = $fn->getWorkOrders($status);
use \FieldNation\SDK;
use \FieldNation\ClassMapFactoryInterface;
SDK::configure(function (ClassMapFactoryInterface $classMap) {
$classMap->setWorkOrder(\MyBusinessNamespace\MyClass::class);
});