Skip to content
Alyssom Fernandes
Sections
CV
Back to projects

Fuel Mind

Reads fuel invoices (NF-e) and closes the month.

controle-entradas-posto.web.appScreens from the public demo, with sample data; interface in Brazilian Portuguese
Fuel Mind: group view with liters delivered, purchase spending, and freight by company, as a chart and a table
Fuel Mind on a phone: dashboard
On this page: 01Why it exists

Why it exists

Fuel deliveries were entered in one spreadsheet per fuel station and TRR (fuel distributor). From those spreadsheets, the data went to another one, which calculated vehicle rental and the drivers’ freight. Mistyped license plates, driver names, invoice numbers, and amounts, plus broken formulas, delayed the audits and made them harder; I built the tool to put an end to that.

Problem, system, and proof

Problem

License plates, drivers, invoices, and amounts typed in by hand, with broken formulas.

System

Tracks fuel deliveries and freight. It reads the NF-e XML, calculates vehicle rental and driver pay, and closes the month in PDF or Excel.

Proof

145 tests

120 unit and 25 Firestore rules tests, on GitHub Actions. Checked on 9 Nov 2026.

A closer look

  1. Imports the NF-e

    Reads the XML of the electronic invoice (NF-e) to record each fuel delivery.

  2. Rental and freight

    Freight month by month, with vehicle rental and driver pay at month-end close.

  3. Closes the month

    Monthly close in PDF or Excel.

  4. Deliveries on one dashboard

    Invoices for delivered fuel, liters, and monthly spending, all on one dashboard.

  5. Compares with the previous period

    Invoices, liters, and spending stack up on the phone screen, each with its percentage change.

How it was built

Architecture

Fuel Mind is plain JavaScript on Firebase, with no framework and no build step. The code lives in three folders: src/core (Firebase, session, sync), src/shared (calculations and the PDF and spreadsheet template), and src/screens, one screen per file. index.html loads all scripts into the same global scope. Master data lives in dados/compartilhado, and each company's invoices in a document of their own, dados/lanc__{id}.

View in code: index.html L1165–1201 firebase.js L44–67 lancamentos.js L89–155 utils.js L869–960 README.md L214–252

Data flow
  1. NF-e XML
  2. Invoice form
  3. Company document
  4. Freight calculation
  5. PDF/Excel monthly close

Technical decisions

  1. One invoice document per company

    A Firestore rule evaluates the whole document: while all invoices lived together, anyone who could read their own could read every company's. I split the invoices into dados/lanc__{id}, with the company id in the name, and the rule extracts that id and checks it against the profile's empresaIds. Using the id, not the name, lets a company be renamed without moving the document or redoing permissions.

    View in code: firebase.js L44–67 firestore.rules L285–299 firestore.rules L99–104

  2. Freight rates with effective periods

    No freight amount is stored: the screen recalculates everything every time it opens. With the rate as a single value, changing it silently rewrote months already paid. So each change closes the previous effective period and opens a new one, and calcularFretesDoMes uses the rate and the driver percentage in effect on the unloading date, plus the truck combination that included that license plate on that day.

    View in code: utils.js L308–343 utils.js L850–869 frete.test.js L39–55

  3. Portuguese-format numbers without type="number"

    In Chrome set to Portuguese, <input type="number"> fails silently: a comma empties the field and the mouse wheel changes the value. I replaced it with text fields, and parseNumeroBR interprets the number. numerico.js brings back the numeric keyboard on phones and the formatting, with each field's decimal places; the price accepts up to ten, the limit of vUnCom in the NF-e (Brazilian electronic invoice).

    View in code: numerico.js L1–58 utils.js L613 numerico.test.js

Challenge

Admin restricted to their own companies

Companies and month-end closes live in lists inside a single document, and Firestore rules can't iterate over a list. Without a limit, an admin of company A could change company B's freight rate through the console. The solution was to compare before and after with removeAll, which leaves only the changed items, and check each one against the profile. Since the rules have no loops, they check up to 8 changed items per write and reject any write that changes more than that.

View in code: firestore.rules L197–239 firestore.rules L256–266 firestore-regras.test.js L197–230 README.md L181–185

Testing

The project has 120 unit tests with node --test, with nothing to install: they load the files in src/ with vm, as the browser does. They cover freight, effective periods, the closed-month lock, matching each NF-e to the supply base that issued it, numbers, and the PDF and spreadsheet template. The security rules have 25 tests on the Firestore emulator, with a demo- project that doesn't exist in the cloud. GitHub Actions runs both on every push.

View in code: frete.test.js L11–23 firestore-regras.test.js testes.yml README.md L159–166

Stack and links

Stack

JavaScript, Firebase, Chart.js, jsPDF, SheetJS, node:test, GitHub Actions