While working on system which handles invoices and contractors payments (and more) I bumped on to yet another problem.
Because of development speed I decided to use third party invoicing web app to handle our invoices (creating, sending pdf in emails and so on). On my part it was only simple Invoice model (only with resource uri), and UserPayment (with amount and invoice_id).
The idea was simple: you may add many payments to invoice, until it’s fully paid – the status changes. That’s it.
But then we wanted to incorporate mass payments system, where every user has unique account number to which he transfers his payments. And some issues appeared. How to connect payments with correct invoice? If we have correct invoice – what to do, when payment is smaller than invoice amount? What to do if it’s bigger?
The first, habitual, answer (this is the way we were doing it in a super complicated way in my previous company I worked for) is to connect Invoices and Payments in many-to-many style. So one Invoice can be connected with many not big enough Payments. And bigger Payment may be partially connected with more than one Invoice. So we needed InvoicePayment model with invoice_id and payment_id and amount field, and we need to check if there’s enough money in InvoicePayment for this Invoice so we may mark it ‘paid’, and if there’s not all money “used” from some Payment, so we may add it to some other not yet paid Invoice. And there’s lot of stuff to be implemented. Lot’s of what-if’s to be solved, and since it is a volunteer project for my friends and I’m not getting money from them (since they’re starting their business, and they do some other amazing job and I wanted to help them) I started getting headache.
Actually two headaches. First it was all about those what-if’s, and how to make and implement algorithms to connects this entities. But the second one was: when I finish I would need to support it. There will definitely be bugs, I would have to track them, and fix them and not to break anything else while fixing this.
I don’t want to do all that!!!
And then it hit me: what if I don’t do that at all?
I asked my product-owner-friend If he mind not having such information about which payment belongs to which invoice. He didn’t.
Invoice X may be marked as paid only if
Amount of invoice X <= (sum of all payments) - (sum of amounts of all 'paid' invoices)
Tada! And this long post lead to my amazing conclusion. And I learned this by building up course for 6k people in Alps. When you’re asked to do something you should ask yourself:
“What if I don’t do that?”
Or different questions:
Does my solution, which appeared immediately in my mind is brilliant idea, or just habitual answer because I did it that way before? Don’t stop to ask yourself this once a while ;)