What is the query for the $result? Orders API can return OPEN or CANCELED orders, so you need to search for ones that have been paid for explicitly. Also, refunds are separate order objects, so if you count all the objects, and there is at least one refund, then it would be +1 over the actual number of sales as well.
The query of the $result is
$api_response = $client->getOrdersApi()->searchOrders($body);
if ($api_response->isSuccess()) {
$result = $api_response->getResult();
$orders = $result->getOrders();
…}
I have checked through the each day order I don’t see any refunds…
So based on this, it doesn’t look like you’re filtering by a particular status, so this is touching on my earlier point: with this query, you’ll get all orders regardless of status (OPEN, CANCELED, COMPLETED). OPEN and especially CANCELED may not have payments associated with them, so you wouldn’t want to consider those in gross sales, since you didn’t actually sell anything.
You probably want to look for:
COMPLETED orders are for sure paid for, so go ahead and add them up
OPEN orders may be paid for (depending on if you have fulfillments and how you’re updating them). So, you probably want to check for tenders within the order object and see if there’s a payment.
CANCELED should be ignored since no payment occurred.
You would also (in the future) want to check for if it’s a refund, and handle that accordingly
Hi @sjosey, from the first image u can see the payments made are only 361 and the total collected is $1351.7 but from what I have get from Order API after setting the filter to COMPLETED order is 365 payment but the right amount of total collected, $1351.7.
<html>
<?php
require_once(__DIR__ . '/vendor/autoload.php');
use Square\SquareClient;
use Square\Environment;
$client = new SquareClient([
'accessToken' => 'XXXX',
'environment' => Environment::PRODUCTION,
]);
$location_ids = ['XXXX'];
$created_at = new \Square\Models\TimeRange();
$created_at->setStartAt('2021-05-26T00:00:00+08:00');
$created_at->setEndAt('2021-05-26T23:59:59+08:00');
$date_time_filter = new \Square\Models\SearchOrdersDateTimeFilter();
$date_time_filter->setCreatedAt($created_at);
$states = ['COMPLETED'];
$state_filter = new \Square\Models\SearchOrdersStateFilter($states);
$filter = new \Square\Models\SearchOrdersFilter();
$filter->setDateTimeFilter($date_time_filter);
$filter->setStateFilter($state_filter);
$sort = new \Square\Models\SearchOrdersSort('CREATED_AT');
$sort->setSortOrder('DESC');
$query = new \Square\Models\SearchOrdersQuery();
$query->setFilter($filter);
$query->setSort($sort);
$body = new \Square\Models\SearchOrdersRequest();
$body->setLocationIds($location_ids);
$body->setQuery($query);
$body->setLimit(10000);
$body->setReturnEntries(false);
$api_response = $client->getOrdersApi()->searchOrders($body);
if ($api_response->isSuccess()) {
$result = $api_response->getResult();
$orders = $result->getOrders();
$grossSales = array();
$oids = array();
if (is_array($orders) || is_object($orders)) {
foreach($orders as $x => $val) {
$lineItems = $result->getOrders()[$x]->getLineItems();
$oids[] = $result->getOrders()[$x]->getId();
if (is_array($lineItems) || is_object($lineItems)){
foreach($lineItems as $q => $val2){
$lineItemsID = $lineItems[$q]->getUid();
$itemName = $lineItems[$q]->getName();
$itemQty = $lineItems[$q]->getQuantity();
$catalogObjID = $lineItems[$q]->getCatalogobjectid();
$grossSales[] = $lineItems[$q]->getGrossSalesMoney()->getAmount();
}
}
}
}
$sum = 0;
foreach($grossSales as $key=>$value)
{
$sum+= $value;
}
echo ($sum/100);
echo sizeof($oids);
}
else
{
$errors = $api_response->getErrors();
}
?>
</html>
Yes I have completed a few $0 orders. Is there a way where I can also not count the $0 sale similar to the Square app where it does not take $0 order as a sale count ?