I thought I had read that OpenXCom already counts bullets. I think it does something like this after the battle:
1- Count all bullets left in all clips of a given type, call that n1
2- Divide n1 by the capacity of the clip, call that n2
3- Round n2 down to the lower integer, call that n3
4- Give back n3 clips to the geoscape.
An example of this would be having 4 heavy plasma clips and you fired 28 shots. At the end of the battle you have n1 = 112 shots left (4*35-28). Divide 112 by 35, n2 = 3.2, so you are given back n3 = 3 heavy plasma clips back (ignoring whatever was recovered and any extra you brought). That's 3*35 = 105 shots. The remaining 7 shots are lost since we only track full clips. You didn't lost all your clips, but you lost something.
Unfortunately, this method does not help if you used only one clip of a given kind and the enemy has none (a situation that happens relatively often with special weapons in mods like Piratez or the FMP). In that case, n2 is always smaller than one, so n3 is always 0, so firing one single shot always takes away your whole magazine.
Over time, you lose out on a ridiculous number of shots, way, way more than you fired. For that reason, I tend to limit my use of special weapons (ex.: my 2 flamethrower soldiers use exactly 1 tank's worth of shots between them if possible, certainly never more. So I lose 1 tank/battle. If they fired a tiny bit more, I'd lose 2. If they fire less than a full tank's worth, some shots are wasted and I lost a tank any ways).
My suggestion is to add a "step 3.5", which would be:
3.5- Take n4 = n2-n3. Pick a number between 0 and 1. If that number is smaller than n4, add one to n3.
In the above example, n4 = n2-n3 = 0.2. So you would have a 20% chance to get an extra clip back. At first, this sounds like 20% chance of cheating (you fired all those shots for free!), but it is only "compensation" for all those extra shots you lose when you roll above n4.
This solution will never have a miracle (it is always at most 1 clip away from the exact answer) and works well if you had only one clip and only fired a few shots. Most of the time you fired for free, but once in a while you lose the whole clip, meaning that on average you lose exactly the number of shots you should.