There are two parts to closing accruals for a month. The first part is to help Ops move accrued revenue from a future month back to the month where we are closing revenue.

How to "move" instances that are accruing in one month back to another month?

Queries

-- First, check which month we are accruing the instance
SELECT * FROM "accruedRevenues"
WHERE "instanceId" = {{INSTANCE_ID}} AND "deletedAt" IS NULL
ORDER BY "accruedAt" DESC; -- Query #1

SELECT * FROM "instanceUpdates"
WHERE "instanceId" = {{INSTANCE_ID}}
ORDER BY "createdAt" DESC;

UPDATE "instanceUpdates"
SET "createdAt" = '{{YEAR}}-{{MONTH}}-{{DAY}} 12:00:00.00+00', "updatedAt" = NOW()
WHERE id = {{INSTANCE_UPDATE_ID}};

If you have no accruals when you run Query #1, then it can only be because of three reasons:

  1. You don't have a quote.
  2. You have a duplicate quote → See Script: Clean up instances with multiple non-deleted quotes
  3. The instance was NOT moved to Done stage yet.

How to actually close accruals:

Check amount accrued on each month:

SET TIMEZONE='America/Los_Angeles';
SELECT
  DATE_TRUNC('month', ar."accruedAt") AS month,
  SUM(amount)/100.0 AS amount
FROM
  "accruedRevenues" ar
  JOIN instances i ON ar."instanceId" = i.id
  JOIN "assistants" a ON "i"."assistantId" = a.id
  JOIN "companies" c ON "a"."companyId" = c.id
WHERE
  ar."deletedAt" IS NULL 
  AND (c."externalInternal" = 'External' OR c."externalInternal" IS NULL)
GROUP BY MONTH
ORDER BY month;

Command to close accruals

node build/src/workers/setLastClosedAt.js --date=2022-06-01 --force

After that run (twice)