https://msqc.myshopify.com/admin/reports/finances?apiClientId=580111&since=2022-01-27&until=2022-01-27

## Sales

Gross Sales $455,297.53
Discounts -$223,148.46
Returns -$11,410.14
Net Sales $220,738.93
Shipping $17,408.00
Taxes $14,750.99
Total Sales $252,897.92

## Liabilities

Gift card sales $1,505.00

select sum(pre_tax_price_set_shop_money_amount) as pre_tax_price_set_shop_money_amount
from shopify.order o
join shopify.order_line_item oli on o.id = oli.order_id
where date(o.processed_at at time zone 'america/chicago') = '2022-01-27'
  and order_status_url not like 'https://dev-preview%'
  and fulfillment_service <> 'gift_card';
-[ RECORD 1 ]-----------------------+------------
pre_tax_price_set_shop_money_amount | 232149.0700

The `pretaxpricesetshopmoneyamount` is the `Gross Sales`.

—-

select
           sum(total_tax_set_shop_money_amount) as total_tax_set_shop_money_amount
   from shopify.refund r
   join shopify.refund_line_item rli on rli.refund_id = r.id
   where date(r.processed_at at time zone 'america/chicago') = '2022-01-27'
;
 total_tax_set_shop_money_amount
---------------------------------
                        727.7800

This is needed to offset the taxes total.

NOTE: This is because we need to take into account the "order adjustments" which aren't currently stored.


select sum(total_tax_set_shop_money_amount) as total_tax_set_shop_money_amount,
       sum(total_shipping_price_set_shop_money_amount) as total_shipping_price_set_shop_money_amount,
       sum(total_discounts_set_shop_money_amount) as total_discounts_set_shop_money_amount
from shopify.order o
where date(o.processed_at at time zone 'america/chicago') = '2022-01-27'
  and order_status_url not like 'https://dev-preview%'
tonacatecuhtli-prod-> ;
-[ RECORD 1 ]------------------------------+------------
total_tax_set_shop_money_amount            | 15485.9800
total_shipping_price_set_shop_money_amount | 17637.0000
total_discounts_set_shop_money_amount      | 223148.4600

The `totaldiscountssetshopmoneyamount` is the `Discounts`.

Unfortunately, `15485.9800` isn't a great answer for `$14,750.99`. :( (tax) Unfortunately, `15485.9800 - 713.5700 = 14772.4100` is also not `$14,750.99`. (713.57 from below.) (NOTE: This is because we need to take into account the "order adjustments" which aren't currently stored.)

Unfortunately, `17637.0000` isn't a great answer for `$17,408.00`. :( (shipping)


select sum(pre_tax_price_set_shop_money_amount) as pre_tax_price_set_shop_money_amount
from shopify.order o
join shopify.order_line_item oli on o.id = oli.order_id
where date(o.processed_at at time zone 'america/chicago') = '2022-01-27'
  and order_status_url not like 'https://dev-preview%'
  and fulfillment_service = 'gift_card';
-[ RECORD 1 ]-----------------------+----------
pre_tax_price_set_shop_money_amount | 1505.0000

The `oliquantpriceamount` is the `Gift card sales`.


with r as (
select r.id,
         ot.kind,
         ot.gateway,
         sum(amount) as amount
  from shopify.refund r
  join (select distinct refund_id, order_transaction_id from shopify.refund_order_transaction) rot on rot.refund_id = r.id
  join shopify.order_transaction ot on rot.order_transaction_id = ot.id
  join shopify.order o on o.id = ot.order_id
  where date(r.processed_at at time zone 'america/chicago') = '2022-01-27'
  group by r.id, ot.kind, ot.gateway
),
rli as (
select r.id,
       r.kind,
       r.gateway,
       sum(subtotal_set_shop_money_amount) as subtotal,
       sum(total_tax_set_shop_money_amount) as total_tax
from r
join shopify.refund_line_item rli on rli.refund_id = r.id
group by r.id, r.kind, r.gateway
)
select r.kind,
       r.gateway,
       sum(amount),
       sum(subtotal) as subtotal,
       sum(total_tax) as total_tax
from r
join rli using (id, kind, gateway)
group by kind, rollup(gateway)
;
  kind  |     gateway      |    sum     |  subtotal  | total_tax
--------+------------------+------------+------------+-----------
 refund |                  |    22.6200 |    20.9500 |    1.6700
 refund | cash             |     9.5200 |     8.7500 |    0.7700
 refund | exchange-credit  |    28.1600 |    25.9000 |    2.2600
 refund | gift_card        |   186.9700 |   184.3000 |   13.5400
 refund | paypal           |  2823.6700 |  2623.5800 |  168.7200
 refund | shopify_payments |  6635.8500 |  6060.6100 |  413.7000
 refund | stripe           |  1672.6400 |  1582.9000 |  112.9100
 refund |                  | 11379.4300 | 10506.9900 |  713.5700
(8 rows)

Unfortunately, `11379.4300` is close to, but not equal to `$11,410.14`. :(