PHP code example of fieldnation / fieldnation-sdk

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);

/**
 * @returns ResultInterface
 */
$result = $fnWorkOrder->publish();

/**
 * 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);

/**
 * @returns ResultInterface
 */
$result = $fnWorkOrder->approve();

/**
 * @returns ResultInterface
 */
$result = $fnWorkOrder->cancel();

/**
 * 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);

/**
 * Create a label
 */
$label = new \FieldNation\Label();
$label->setName('New Work');
$label->setHideFromTech(true);

/**
 * @returns ResultInterface
 */
$result = $fnWorkOrder->addLabel($label);

/**
 * Create a label
 */
$label = new \FieldNation\Label();
$label->setName('New Work');

/**
 * @returns ResultInterface
 */
$result = $fnWorkOrder->removeLabel($label);

/**
 * Closeout Requirement
 */
$upload picture');

/**
 * @returns ResultInterface
 */
$result = $fnWorkOrder->resolveCloseoutRequirement($

/**
 * Shipment
 */
$shipment = new \FieldNation\Shipment();
$shipment->setVendor('USPS');
$shipment->setTrackingId('my-tracking-number');

/**
 * @returns ResultInterface
 */
$result = $fnWorkOrder->addShipment($shipment);

// 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);

/**
 * Create a TimRange object
 */
$schedule = new TimeRange();
$schedule->setTimeBegin(new \DateTime('now'));
$schedule->setTimeEnd(new \DateTime('now'));

/**
 * @returns ResultInterface
 */
$result = $fnWorkOrder->updateSchedule($schedule);

/**
 * @returns WorkOrderInterface
 */
$fnWorkOrder = $fnWorkOrder->get(); // get all metadata and reassign the existing variable

/**
 * @returns string from \FieldNation\WorkOrderStatuses
 */
$status = $fnWorkOrder->getStatus();

/**
 * @returns \FieldNation\TechnicianInterface
 */
$provider = $fnWorkOrder->getAssignedProvider();

/**
 * @returns \FieldNation\ProgressInterface
 */
$progress = $fnWorkOrder->getProgress();

/**
 * @returns \FieldNation\PaymentInterface
 */
$payment = $fnWorkOrder->getPayment();

/**
 * @returns \FieldNation\MessageInterface[]
 */
$messages = $fnWorkOrder->getMessages();

/**
 * @returns from \FieldNation\Document[]
 */
$documents = $fnWorkOrder->getAttachedDocuments();

/**
 * @returns \FieldNation\Shipment[]
 */
$shipments = $fnWorkOrder->getShipments();

/**
 * 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);

/**
 * @returns \FieldNation\ProjectInterface[]
 */
$projects = $fn->getProjects();

/**
 * @returns \FieldNation\DocumentInterface[]
 */
$documents = $fn->getDocuments();

/**
 * @returns string
 */
$shippingId = $fn->getShippingIdFrom('my-tracking-number');

use \FieldNation\SDK;
use \FieldNation\ClassMapFactoryInterface;

SDK::configure(function (ClassMapFactoryInterface $classMap) {
    $classMap->setWorkOrder(\MyBusinessNamespace\MyClass::class);
});

// Default configuration
SDK::configure(function (ClassMapFactoryInterface $classMap) {
    $classMap->setAdditionalExpense(\FieldNation\AdditionalExpense::class);
    $classMap->setAdditionalField(\FieldNation\AdditionalField::class);
    $classMap->setBlendedPay(\FieldNation\BlendedPay::class);
    $classMap->setCheckInOut(\FieldNation\CheckInOut::class);
    $classMap->setCloseoutRequirement(\FieldNation\CloseoutRequirement::class);
    $classMap->setCustomField(\FieldNation\CustomField::class);
    $classMap->setDocument(\FieldNation\Document::class);
    $classMap->setFixedPay(\FieldNation\FixedPay::class);
    $classMap->setGroup(\FieldNation\Group::class);
    $classMap->setHistory(\FieldNation\History::class);
    $classMap->setLabel(\FieldNation\Label::class);
    $classMap->setMessage(\FieldNation\Message::class);
    $classMap->setPayInfo(\FieldNation\PayInfo::class);
    $classMap->setPaymentDeduction(\FieldNation\PaymentDeduction::class);
    $classMap->setPayment(\FieldNation\Payment::class);
    $classMap->setProblem(\FieldNation\Problem::class);
    $classMap->setProgress(\FieldNation\Progress::class);
    $classMap->setProject(\FieldNation\Project::class);
    $classMap->setRatePay(\FieldNation\RatePay::class);
    $classMap->setServiceDescription(\FieldNation\ServiceDescription::class);
    $classMap->setServiceLocation(\FieldNation\ServiceLocation::class);
    $classMap->setShipmentHistory(\FieldNation\ShipmentHistory::class);
    $classMap->setShipment(\FieldNation\Shipment::class);
    $classMap->setTechnician(\FieldNation\Technician::class);
    $classMap->setTemplate(\FieldNation\Template::class);
    $classMap->setTimeRange(\FieldNation\TimeRange::class);
    $classMap->setWorkLog(\FieldNation\WorkLog::class);
    $classMap->setWorkOrder(\FieldNation\WorkOrder::class);
});