Project
Getting in-person sales into the books: a Shopify POS sync
After each of a client’s big in-person sales, I move the Shopify POS orders into their custom database, where they drive the reports that decide payouts. I run the import after each sale and wrote the runbook it follows now.
- My role
- Run the import after each sale · Wrote the runbook
- Stack
- shopify
- shopify pos
- python
- mysql
- csv
Why it matters
A client of mine runs large seasonal sales in person, with checkout on Shopify POS. A single sale is about a week long and brings in well over a thousand orders. After each one, the client’s reports decide the payouts that follow.
Those reports don’t read Shopify. They read the client’s own MySQL database. So after each of the two big sales a year, I export the orders from Shopify and run a Python import script (written by the site’s original developer) that loads them into the database. If one order goes missing or gets counted twice, someone’s payout is wrong. The numbers have to match Shopify exactly.
One sale, start to finish
- Check event datesAlways step one
- Back up + count rows
- Export from ShopifyPlain CSV, never opened in Excel
- Swap in the new fileLast sale’s file archived first
- Run the import
- Compare + confirmShopify totals, client signs off
The bug: the right sales, the wrong dates
After the spring sale, the first import showed about 35% less revenue than the sale had really taken. Yet every order was in the database.
The problem was the event itself. Each sale has a start and end date in the database, and the reports only count orders between them. The event was set to three days, but the sale ran for six, so half the days’ sales were stored but never counted. Once I corrected the dates, the report matched.
For the fall sale, I checked the dates first. The event started a day after the first sales came in, so it would have missed a full day of early sales.
Nothing errors
Orders outside the event dates aren’t rejected. They’re imported, stored, then quietly left out of the report. The only sign is a total that’s too low, and you only see that if you compare.
So the runbook now starts at Step 0: check the event’s dates against the days the sale really ran. This also confirms I’m in the right database, because the latest event should be the sale I’m importing.
Why I wrote a runbook
- Twice a year is easy to forget. Six months is plenty of time to forget which folder, which file name and which check comes first. With the runbook, I don’t start from memory.
- The “hit by a bus” rule. At my job we ask: if you were hit by a bus today, could another developer pick up your work tomorrow? Grim, but true. The runbook is written so any dev who steps into the role can work through it: every command, every check, and what to do when the numbers look wrong.
The safety habits
Each of these is a step in the runbook, because skipping it could change someone’s payout.
- Back up first, and count. Before every import, I back up the three tables the import writes to and record their row counts. Afterwards, new rows should equal the orders in the export. On the fall sale, they matched the CSV exactly.
- Never open the export in Excel. Excel shortens the dates, and the import breaks on them. To peek, I use a text editor, or
headandwc -lon the server. - Archive the old file. The script always reads one file with a fixed name, so I rename the last sale’s file with a date before uploading the new one. It can’t be imported twice.
- Compare, then confirm. I check that the new rows fall inside the event dates, then compare the report’s totals with Shopify’s for the same range, using a SQL query with the report’s own filter as a second check. The client gets the figures to confirm.
- Log every run. Each sale gets a row: date range, event dates before and after any fix, row counts, totals and notes, so the next run starts with a record of the last one.
What I learned reading the code
The 35% gap sent me into the code to see exactly how the reports decide what counts.
The report adds a day to the end date
Sales are stored with a time, but event dates aren’t. A date on its own means midnight at the start of that day, so “up to the end date” would cut off the last day. The report adds a day to the end date, so the end date can simply be the last day of the sale.
Item pages use a wider window
The pages for individual items don’t use the exact event dates. They look at sales within two days either side. That’s forgiving, but it means an item page and the event report can disagree, and the first question when they do is which window each one uses.
What I’d improve next
- Automate the export. Pull orders from the Shopify API for the event’s dates instead of downloading a CSV. No file means nothing to open in Excel, and the dates arrive untouched.
- Block a double import in the database. Archiving the file works, but it relies on me. Refusing an order ID that’s already there would make a second import fail loudly instead of doubling payouts.
- Warn about orders outside the event dates. Before importing, count orders that fall outside the window. Anything above zero means the dates need checking.
- Make the totals check one command. Print the report’s totals next to Shopify’s, ready to send to the client.
The import is the easy part. Trusting the numbers is the job.