Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

PHPbeginnercliAI generated

CLI Invoice Builder

Build a PHP command-line tool that turns a list of billed items into a finished invoice. You give it a JSON or CSV file with the client's details and the items (description, quantity and unit price), and it calculates each line's total, the subtotal, an optional discount, VAT and the grand total, then prints a neatly formatted invoice or saves it as a text or HTML file. Money is calculated in whole cents so the totals are always exact. It's a good first PHP project outside the browser: you'll learn command-line scripts, reading JSON and CSV files, classes, validating input, formatting money correctly, setting up a project with Composer, and testing with PHPUnit.

Estimate
~8h
Steps
6
Completed by
0
Proposed by
codeseed.app

PHP CLI · Composer · PHPUnit

Project roadmap

  1. 01

    Define the input and output

    ~1h

    Write README.md with an example input file (seller and client details, invoice number, date, VAT rate, optional discount, and items with description, quantity and unit price) in JSON and CSV form, and the exact invoice it should produce. Explain the rounding rule: each line total is rounded to cents, then summed. Create the project with composer init and a bin/invoice script. Done when: README.md shows the input formats and the expected invoice.

  2. 02

    Model the invoice

    ~1.5h

    Create classes Invoice, LineItem and Party. Store amounts as integers in cents. Convert prices from the input (like 12.50) to cents carefully, without floating-point errors, for example by parsing the text. Write methods for the line total, subtotal, discount, VAT and grand total. Done when: a small script creates an invoice in code and prints correct totals.

  3. 03

    Read and validate the input

    ~1.5h

    Read the input file with json_decode (with JSON_THROW_ON_ERROR) or fgetcsv depending on its extension. Validate everything: required fields, quantity greater than 0, price not negative, VAT between 0 and 100, and a valid date. Collect all errors with their item number and print them together. Done when: a file with several mistakes lists every one of them.

  4. 04

    Print and save the invoice

    ~1.5h

    Print the invoice as aligned text with str_pad and number_format: the header with both parties, the items table, and the totals. Add --out invoice.txt or invoice.html to save it instead, where the HTML version uses a simple template with every value escaped with htmlspecialchars. Done when: the printed invoice matches the example in README.md.

  5. 05

    Handle edge cases

    ~1h

    Handle an empty items list, a file that doesn't exist or can't be parsed, very long descriptions (wrap or shorten them), a 0% VAT rate, and a discount larger than the subtotal (refuse it). Exit with code 1 and a clear message on any error. Done when: each case gives a helpful message and never a PHP warning.

  6. 06

    Test it

    ~1.5h

    Install PHPUnit with Composer and write tests: converting prices to cents, line and total calculations including rounding, discount and VAT, validation errors, and reading both JSON and CSV sample files. Done when: vendor/bin/phpunit passes.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~8h · 6 steps

Tech stack

PHP CLIComposerPHPUnit