How to get the number of sales sold in a day

Hi is there a reason why I cannot find the number of sales sold in a day even though I use the function count() or sizeof() the array of $oids?

$oids = $result->getOrders()[$x]->getId();

I always get a slightly higher number of sales like +3 or maybe +4 of my actual number of sales

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…

What is the value of the $body? I still don’t know what you’re actually searching for, sorry. Are you searching for COMPLETED orders only, or?

Oops sorry here is my code @sjosey I do not know if it is the the app that shows the wrong amount of sales or just my code problem

<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-17T00:00:00+08:00');
$created_at->setEndAt('2021-05-17T23:59:59+08:00'); 

$date_time_filter = new \Square\Models\SearchOrdersDateTimeFilter();
$date_time_filter->setCreatedAt($created_at);

$filter = new \Square\Models\SearchOrdersFilter();
$filter->setDateTimeFilter($date_time_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)."</br>";
    
    echo sizeof($oids);
} 

else 
{
    $errors = $api_response->getErrors();
}

?>
</html>

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:

  1. COMPLETED orders are for sure paid for, so go ahead and add them up
  2. 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.
  3. CANCELED should be ignored since no payment occurred.
  4. You would also (in the future) want to check for if it’s a refund, and handle that accordingly

Let me know if that still doesn’t help though


image
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>

Have you completed any $0 orders? Can you share the location_id so I can take a closer look?

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 ?

Ok I have found a solution, not sure if it is pratical. Here is my code:

<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-17T00:00:00+08:00');
$created_at->setEndAt('2021-05-17T23:59:59+08:00'); 

$date_time_filter = new \Square\Models\SearchOrdersDateTimeFilter();
$date_time_filter->setCreatedAt($created_at);

$filter = new \Square\Models\SearchOrdersFilter();
$filter->setDateTimeFilter($date_time_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();
    $totalMoney = array();
    
    if (is_array($orders) || is_object($orders)) {
    foreach($orders as $x => $val) {
        $lineItems = $result->getOrders()[$x]->getLineItems();
        $oids[] =  $result->getOrders()[$x]->getId();
        $totalMoney[] = $result->getOrders()[$x]->getTotalMoney()->getAmount();
        
        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) - $counts['0']). "</br>";
} 

else 
{
    $errors = $api_response->getErrors();
}

?>
</html>