Skip to content

Commit

Permalink
Add v2 sales entity and api
Browse files Browse the repository at this point in the history
  • Loading branch information
szeber committed Dec 28, 2016
1 parent 3554558 commit 0b7dfe4
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 3 deletions.
63 changes: 63 additions & 0 deletions src/ShoppinPal/Vend/Api/V2/Sales.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace ShoppinPal\Vend\Api\V2;

use ShoppinPal\Vend\DataObject\Entity\V2\CollectionResult;
use ShoppinPal\Vend\DataObject\Entity\V2\Sale;
use YapepBase\Communication\CurlHttpRequest;

/**
* Implements the V2 Sales API
*/
class Sales extends V2ApiAbstract
{

/**
* Returns a collection of sales.
*
* @param int $pageSize The number of items to return per page.
* @param null $before The version to succeed the last returned version.
* @param null $after The version to precede the first returned version
* @param bool $includeDeleted If TRUE, deleted items will be returned as well. (required to synchronise deletions)
*
* @return CollectionResult
*/
public function getCollection(
$pageSize = 50,
$before = null,
$after = null,
$includeDeleted = false
)
{
$params = [
'page_size' => $pageSize,
];

if (!empty($before)) {
$params['before'] = $before;
}

if (!empty($after)) {
$params['after'] = $after;
}

if ($includeDeleted) {
$params['deleted'] = 1;
}

$request = $this->getAuthenticatedRequestForUri('api/2.0/sales', $params);
$request->setMethod(CurlHttpRequest::METHOD_GET);

$result = $this->sendRequest($request, 'sale get collection');

$sales = [];

foreach ($result['data'] as $sale) {
$sales[] = new Sale($sale, Sale::UNKNOWN_PROPERTY_IGNORE);
}

return new CollectionResult(
$result['version']['min'], $result['version']['max'], $sales
);
}
}
80 changes: 80 additions & 0 deletions src/ShoppinPal/Vend/DataObject/Entity/V2/Sale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace ShoppinPal\Vend\DataObject\Entity\V2;

use ShoppinPal\Vend\DataObject\Entity\EntityDoAbstract;

class Sale extends EntityDoAbstract
{

const STATUS_OPEN = 'OPEN';
const STATUS_SAVED = 'SAVED';
const STATUS_CLOSED = 'CLOSED';
const STATUS_LAYBY = 'LAYBY';
const STATUS_LAYBY_CLOSED = 'LAYBY_CLOSED';
const STATUS_ONACCOUNT = 'ONACCOUNT';
const STATUS_ONACCOUNT_CLOSED = 'ONACCOUNT_CLOSED';
const STATUS_VOIDED = 'VOIDED';

protected $subEntities = [
'lineItems' => [
self::SUB_ENTITY_KEY_TYPE => self::SUB_ENTITY_TYPE_COLLECTION,
self::SUB_ENTITY_KEY_CLASS => SaleLineItem::class,
],
'payments' => [
self::SUB_ENTITY_KEY_TYPE => self::SUB_ENTITY_TYPE_COLLECTION,
self::SUB_ENTITY_KEY_CLASS => SalePayment::class,
],
'taxes' => [
self::SUB_ENTITY_KEY_TYPE => self::SUB_ENTITY_TYPE_COLLECTION,
self::SUB_ENTITY_KEY_CLASS => SaleTax::class,
],
];

public $id;

public $outletId;

public $registerId;

public $userId;

public $customerId;

public $invoiceNumber;

public $status;

public $note;

public $shortCode;

public $returnFor;

public $totalPrice;

public $totalTax;

public $totalLoyalty;

public $createdAt;

public $updatedAt;

public $saleDate;

public $deletedAt;

/** @var SaleLineItem[] */
public $lineItems = [];

/** @var SalePayment[] */
public $payments = [];

public $version;

public $receiptNumber;

/** @var SaleTax */
public $taxes = [];
}
73 changes: 73 additions & 0 deletions src/ShoppinPal/Vend/DataObject/Entity/V2/SaleLineItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace ShoppinPal\Vend\DataObject\Entity\V2;

use ShoppinPal\Vend\DataObject\Entity\EntityDoAbstract;

class SaleLineItem extends EntityDoAbstract
{

protected $subEntities = [
'taxComponents' => [
self::SUB_ENTITY_KEY_TYPE => self::SUB_ENTITY_TYPE_COLLECTION,
self::SUB_ENTITY_KEY_CLASS => SaleLineItemTaxComponent::class,
],
];

public $id;

public $productId;

public $taxId;

public $discountTotal;

public $discount;

public $priceTotal;

public $price;

public $costTotal;

public $cost;

public $taxTotal;

public $tax;

public $quantity;

public $loyaltyValue;

public $note;

public $priceSet;

public $status;

public $sequence;

/** @var SaleLineItemTaxComponent[] */
public $taxComponents = [];

public $unitCost;

public $unitDiscount;

public $unitLoyaltyValue;

public $unitPrice;

public $unitTax;

public $totalCost;

public $totalDiscount;

public $totalLoyaltyValue;

public $totalPrice;

public $totalTax;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ShoppinPal\Vend\DataObject\Entity\V2;

use ShoppinPal\Vend\DataObject\Entity\EntityDoAbstract;

class SaleLineItemTaxComponent extends EntityDoAbstract
{

public $rateId;

public $totalTax;
}
33 changes: 33 additions & 0 deletions src/ShoppinPal/Vend/DataObject/Entity/V2/SalePayment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace ShoppinPal\Vend\DataObject\Entity\V2;

use ShoppinPal\Vend\DataObject\Entity\EntityDoAbstract;

class SalePayment extends EntityDoAbstract
{

public $id;

public $registerId;

public $outletId;

public $retailerPaymentTypeId;

/** @var int {@uses PaymentType::PAYMENT_TYPE_*} */
public $paymentTypeId;

public $name;

public $amount;

public $paymentDate;

public $deletedAt;

// TODO readd this later
// public $externalAttributes = [];

public $sourceId;
}
13 changes: 13 additions & 0 deletions src/ShoppinPal/Vend/DataObject/Entity/V2/SaleTax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ShoppinPal\Vend\DataObject\Entity\V2;

use ShoppinPal\Vend\DataObject\Entity\EntityDoAbstract;

class SaleTax extends EntityDoAbstract
{

public $amount;

public $id;
}
27 changes: 24 additions & 3 deletions src/ShoppinPal/Vend/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use ShoppinPal\Vend\Api\V0\Products as ProductsV0;
use ShoppinPal\Vend\Api\V0\PaymentTypes as PaymentTypesV0;
use ShoppinPal\Vend\Api\V0\Registers as RegistersV0;
use ShoppinPal\Vend\Api\V0\RegisterSales as RegisterSalesV0;
use ShoppinPal\Vend\Api\V0\RegisterSales as SalesV0;
use ShoppinPal\Vend\Api\V0\Suppliers as SuppliersV0;
use ShoppinPal\Vend\Api\V0\Taxes as TaxesV0;
use ShoppinPal\Vend\Api\V0\Users as UsersV0;
use ShoppinPal\Vend\Api\V0\Webhooks as WebhooksV0;
use ShoppinPal\Vend\Api\V2\Customers as CustomersV2;
use ShoppinPal\Vend\Api\V2\PaymentTypes as PaymentTypesV2;
use ShoppinPal\Vend\Api\V2\PriceBooks as PriceBooksV2;
use ShoppinPal\Vend\Api\V2\Sales as SalesV2;
use ShoppinPal\Vend\Auth\AuthHelper;
use ShoppinPal\Vend\Auth\OAuth;
use YapepBase\Config;
Expand Down Expand Up @@ -232,16 +233,36 @@ public function getRegistersApi($version)
*
* @param string $version The version to use. {@uses self::API_VERSION_*}
*
* @return RegisterSalesV0
* @return SalesV0|SalesV2
*
* @throws ParameterException If the version is invalid.
* @throws \YapepBase\Exception\ConfigException If the required configuration params are not set.
*
* @deprecated Will be removed in next minor or major release. Use getSalesApi instead
*/
public function getRegisterSalesApi($version)
{
return $this->getSalesApi($version);
}

/**
* Returns a Sales API handler.
*
* @param string $version The version to use. {@uses self::API_VERSION_*}
*
* @return SalesV0|SalesV2
*
* @throws ParameterException If the version is invalid.
* @throws \YapepBase\Exception\ConfigException If the required configuration params are not set.
*/
public function getSalesApi($version)
{
switch ($version) {
case self::API_VERSION_0:
return new RegisterSalesV0($this->getAuthHelper(), $this->getDomainPrefix());
return new SalesV0($this->getAuthHelper(), $this->getDomainPrefix());

case self::API_VERSION_2:
return new SalesV2($this->getAuthHelper(), $this->getDomainPrefix());

default:
throw new ParameterException('Unknown version: ' . $version);
Expand Down

0 comments on commit 0b7dfe4

Please sign in to comment.