[2660] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
| 4 | # PyKota Invoice generator |
---|
| 5 | # |
---|
| 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 7 | # |
---|
| 8 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
| 9 | # This program is free software; you can redistribute it and/or modify |
---|
| 10 | # it under the terms of the GNU General Public License as published by |
---|
| 11 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 12 | # (at your option) any later version. |
---|
| 13 | # |
---|
| 14 | # This program is distributed in the hope that it will be useful, |
---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | # GNU General Public License for more details. |
---|
| 18 | # |
---|
| 19 | # You should have received a copy of the GNU General Public License |
---|
| 20 | # along with this program; if not, write to the Free Software |
---|
| 21 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
| 25 | # |
---|
| 26 | |
---|
| 27 | import sys |
---|
| 28 | import os |
---|
| 29 | import pwd |
---|
[2668] | 30 | import time |
---|
[2663] | 31 | import cStringIO |
---|
| 32 | |
---|
| 33 | try : |
---|
| 34 | from reportlab.pdfgen import canvas |
---|
| 35 | from reportlab.lib import pagesizes |
---|
| 36 | from reportlab.lib.units import cm |
---|
| 37 | except ImportError : |
---|
| 38 | hasRL = 0 |
---|
| 39 | else : |
---|
| 40 | hasRL = 1 |
---|
| 41 | |
---|
| 42 | try : |
---|
| 43 | import PIL.Image |
---|
| 44 | except ImportError : |
---|
| 45 | hasPIL = 0 |
---|
| 46 | else : |
---|
| 47 | hasPIL = 1 |
---|
| 48 | |
---|
[2790] | 49 | from pykota.tool import Percent, PyKotaToolError, PyKotaCommandLineError, crashed, N_ |
---|
[2666] | 50 | from pykota.dumper import DumPyKota |
---|
[2660] | 51 | |
---|
| 52 | __doc__ = N_("""pkinvoice v%(__version__)s (c) %(__years__)s %(__author__)s |
---|
| 53 | |
---|
| 54 | An invoice generator for PyKota. |
---|
| 55 | |
---|
| 56 | command line usage : |
---|
| 57 | |
---|
| 58 | pkinvoice [options] user1 user2 ... userN |
---|
| 59 | |
---|
| 60 | options : |
---|
| 61 | |
---|
[2811] | 62 | -v | --version Prints pkinvoice's version number then exits. |
---|
[2660] | 63 | -h | --help Prints this message then exits. |
---|
| 64 | |
---|
[2811] | 65 | -l | --logo img Use the image as the invoice's logo. The logo will |
---|
[2661] | 66 | be drawn at the center top of the page. The default |
---|
| 67 | logo is /usr/share/pykota/logos/pykota.jpeg |
---|
| 68 | |
---|
| 69 | -p | --pagesize sz Sets sz as the page size. Most well known |
---|
| 70 | page sizes are recognized, like 'A4' or 'Letter' |
---|
| 71 | to name a few. The default size is A4. |
---|
| 72 | |
---|
| 73 | -n | --number N Sets the number of the first invoice. This number |
---|
| 74 | will automatically be incremented for each invoice. |
---|
| 75 | |
---|
[2660] | 76 | -o | --output f.pdf Defines the name of the invoice file which will |
---|
| 77 | be generated as a PDF document. If not set or |
---|
| 78 | set to '-', the PDF document is sent to standard |
---|
| 79 | output. |
---|
| 80 | |
---|
| 81 | -u | --unit u Defines the name of the unit to use on the invoice. |
---|
| 82 | The default unit is 'Credits', optionally translated |
---|
| 83 | to your native language if it is supported by PyKota. |
---|
| 84 | |
---|
[2662] | 85 | -V | --vat p Sets the percent value of the applicable VAT to be |
---|
| 86 | exposed. The default is 0.0, meaning no VAT |
---|
| 87 | information will be included. |
---|
[2660] | 88 | |
---|
[2666] | 89 | -s | --start date Sets the starting date for the print jobs invoiced. |
---|
[2660] | 90 | |
---|
[2666] | 91 | -e | --end date Sets the ending date for the print jobs invoiced. |
---|
| 92 | |
---|
| 93 | user1 through userN can use wildcards if needed. If no user argument is |
---|
| 94 | used, a wildcard of '*' is assumed, meaning include all users. |
---|
| 95 | |
---|
[3069] | 96 | Dates formatting with --start and --end : |
---|
[2666] | 97 | |
---|
| 98 | YYYY : year boundaries |
---|
| 99 | YYYYMM : month boundaries |
---|
| 100 | YYYYMMDD : day boundaries |
---|
| 101 | YYYYMMDDhh : hour boundaries |
---|
| 102 | YYYYMMDDhhmm : minute boundaries |
---|
| 103 | YYYYMMDDhhmmss : second boundaries |
---|
| 104 | yesterday[+-NbDays] : yesterday more or less N days (e.g. : yesterday-15) |
---|
| 105 | today[+-NbDays] : today more or less N days (e.g. : today-15) |
---|
| 106 | tomorrow[+-NbDays] : tomorrow more or less N days (e.g. : tomorrow-15) |
---|
| 107 | now[+-NbDays] : now more or less N days (e.g. now-15) |
---|
| 108 | |
---|
| 109 | 'now' and 'today' are not exactly the same since today represents the first |
---|
| 110 | or last second of the day depending on if it's used in a start= or end= |
---|
| 111 | date expression. The utility to be able to specify dates in the future is |
---|
| 112 | a question which remains to be answered :-) |
---|
| 113 | |
---|
[2660] | 114 | examples : |
---|
| 115 | |
---|
[2666] | 116 | $ pkinvoice --unit EURO --output invoices.pdf --start=now-30 |
---|
[2660] | 117 | |
---|
[2666] | 118 | Will generate a PDF document containing invoices for all users |
---|
| 119 | who have spent some credits last month. Invoices will be done in |
---|
| 120 | EURO. No VAT information will be included. |
---|
[2660] | 121 | """) |
---|
| 122 | |
---|
[2666] | 123 | class PKInvoice(DumPyKota) : |
---|
[2660] | 124 | """A class for pkinvoice.""" |
---|
[2663] | 125 | def getPageSize(self, pgsize) : |
---|
| 126 | """Returns the correct page size or None if not found.""" |
---|
| 127 | try : |
---|
| 128 | return getattr(pagesizes, pgsize.upper()) |
---|
| 129 | except AttributeError : |
---|
| 130 | try : |
---|
| 131 | return getattr(pagesizes, pgsize.lower()) |
---|
| 132 | except AttributeError : |
---|
| 133 | pass |
---|
| 134 | |
---|
[2668] | 135 | def printVar(self, label, value, size) : |
---|
| 136 | """Outputs a variable onto the PDF canvas. |
---|
| 137 | |
---|
| 138 | Returns the number of points to substract to current Y coordinate. |
---|
| 139 | """ |
---|
| 140 | xcenter = (self.pagesize[0] / 2.0) - 1*cm |
---|
| 141 | self.canvas.saveState() |
---|
| 142 | self.canvas.setFont("Helvetica-Bold", size) |
---|
| 143 | self.canvas.setFillColorRGB(0, 0, 0) |
---|
| 144 | self.canvas.drawRightString(xcenter, self.ypos, "%s :" % label) |
---|
| 145 | self.canvas.setFont("Courier-Bold", size) |
---|
| 146 | self.canvas.setFillColorRGB(0, 0, 1) |
---|
| 147 | self.canvas.drawString(xcenter + 0.5*cm, self.ypos, value) |
---|
| 148 | self.canvas.restoreState() |
---|
| 149 | self.ypos -= (size + 4) |
---|
| 150 | |
---|
| 151 | def pagePDF(self, invoicenumber, entry, vat, start, end, unitname) : |
---|
[2663] | 152 | """Generates a new page in the PDF document.""" |
---|
[2666] | 153 | extractonly = { "username" : entry.Name } |
---|
| 154 | if start : |
---|
| 155 | extractonly["start"] = start |
---|
| 156 | if end : |
---|
| 157 | extractonly["end"] = end |
---|
| 158 | records = self.storage.extractHistory(extractonly) |
---|
| 159 | amount = vatamount = 0.0 |
---|
| 160 | vatamount = 0.0 |
---|
| 161 | if records : |
---|
[2668] | 162 | self.canvas.doForm("background") |
---|
| 163 | self.ypos = self.yorigine - (cm + 20) |
---|
[2666] | 164 | records = self.summarizeDatas(records, "history", extractonly, True) |
---|
| 165 | fieldnames = records[0] |
---|
| 166 | fields = {} |
---|
| 167 | for i in range(len(fieldnames)) : |
---|
| 168 | fields[fieldnames[i]] = i |
---|
| 169 | numberofbytes = records[1][fields["jobsizebytes"]] |
---|
| 170 | numberofpages = records[1][fields["jobsize"]] |
---|
| 171 | amount = records[1][fields["jobprice"]] |
---|
| 172 | if amount > 0.0 : |
---|
| 173 | # There's something due ! |
---|
| 174 | ht = ((amount * 10000.0) / (100.0 + vat)) / 100.0 |
---|
| 175 | vatamount = amount - ht |
---|
[2689] | 176 | self.printVar(_("Invoice"), "#%s" % invoicenumber, 22) |
---|
| 177 | self.printVar(_("Username"), entry.Name, 22) |
---|
| 178 | self.ypos -= 20 |
---|
| 179 | if start : |
---|
| 180 | self.printVar(_("Since"), start, 14) |
---|
| 181 | if end : |
---|
| 182 | self.printVar(_("Until"), end, 14) |
---|
| 183 | self.printVar(_("Edited on"), time.strftime("%c", time.localtime()), 14) |
---|
| 184 | |
---|
| 185 | self.ypos -= 20 |
---|
| 186 | # self.printVar(_("Number of bytes"), str(numberofbytes), 14) |
---|
| 187 | self.printVar(_("Number of pages printed"), str(numberofpages), 14) |
---|
| 188 | self.ypos -= 20 |
---|
| 189 | self.printVar(_("Amount due"), "%.2f %s" % (amount, unitname), 22) |
---|
| 190 | if vat : |
---|
| 191 | self.ypos += 8 |
---|
| 192 | self.printVar("%s (%.2f%%)" % (_("Included VAT"), vat), "%.2f %s" % (vatamount, unitname), 14) |
---|
| 193 | self.canvas.showPage() |
---|
| 194 | return 1 |
---|
[2668] | 195 | return 0 |
---|
[2666] | 196 | |
---|
| 197 | def initPDF(self, logo) : |
---|
[2663] | 198 | """Initializes the PDF document.""" |
---|
| 199 | self.pdfDocument = cStringIO.StringIO() |
---|
[2666] | 200 | self.canvas = c = canvas.Canvas(self.pdfDocument, \ |
---|
| 201 | pagesize=self.pagesize, \ |
---|
| 202 | pageCompression=1) |
---|
[2663] | 203 | |
---|
| 204 | c.setAuthor(pwd.getpwuid(os.geteuid())[0]) |
---|
| 205 | c.setTitle("PyKota invoices") |
---|
[2668] | 206 | c.setSubject("Invoices generated with PyKota") |
---|
[2663] | 207 | |
---|
| 208 | |
---|
| 209 | self.canvas.beginForm("background") |
---|
| 210 | self.canvas.saveState() |
---|
| 211 | |
---|
[2668] | 212 | self.ypos = self.pagesize[1] - (2 * cm) |
---|
| 213 | |
---|
| 214 | xcenter = self.pagesize[0] / 2.0 |
---|
[2663] | 215 | if logo : |
---|
| 216 | try : |
---|
| 217 | imglogo = PIL.Image.open(logo) |
---|
| 218 | except : |
---|
| 219 | self.printInfo("Unable to open image %s" % logo, "warn") |
---|
| 220 | else : |
---|
| 221 | (width, height) = imglogo.size |
---|
| 222 | multi = float(width) / (8 * cm) |
---|
| 223 | width = float(width) / multi |
---|
| 224 | height = float(height) / multi |
---|
[2668] | 225 | self.ypos -= height |
---|
| 226 | c.drawImage(logo, xcenter - (width / 2.0), \ |
---|
| 227 | self.ypos, \ |
---|
| 228 | width, height) |
---|
[2663] | 229 | |
---|
[2668] | 230 | self.ypos -= (cm + 20) |
---|
| 231 | self.canvas.setFont("Helvetica-Bold", 14) |
---|
| 232 | self.canvas.setFillColorRGB(0, 0, 0) |
---|
| 233 | self.canvas.drawCentredString(xcenter, self.ypos, "%s :" % _("Here's the invoice for your printouts")) |
---|
[2663] | 234 | |
---|
[2668] | 235 | self.yorigine = self.ypos |
---|
[2663] | 236 | self.canvas.restoreState() |
---|
| 237 | self.canvas.endForm() |
---|
| 238 | |
---|
| 239 | def endPDF(self, fname) : |
---|
| 240 | """Flushes the PDF generator.""" |
---|
| 241 | self.canvas.save() |
---|
| 242 | if fname != "-" : |
---|
| 243 | outfile = open(fname, "w") |
---|
| 244 | outfile.write(self.pdfDocument.getvalue()) |
---|
| 245 | outfile.close() |
---|
| 246 | else : |
---|
| 247 | sys.stdout.write(self.pdfDocument.getvalue()) |
---|
| 248 | sys.stdout.flush() |
---|
| 249 | |
---|
[2660] | 250 | def main(self, names, options) : |
---|
| 251 | """Generate invoices.""" |
---|
[2663] | 252 | if not hasRL : |
---|
| 253 | raise PyKotaToolError, "The ReportLab module is missing. Download it from http://www.reportlab.org" |
---|
| 254 | if not hasPIL : |
---|
| 255 | raise PyKotaToolError, "The Python Imaging Library is missing. Download it from http://www.pythonware.com/downloads" |
---|
| 256 | |
---|
[2660] | 257 | if not self.config.isAdmin : |
---|
| 258 | raise PyKotaCommandLineError, "%s : %s" % (pwd.getpwuid(os.geteuid())[0], _("You're not allowed to use this command.")) |
---|
| 259 | |
---|
[2661] | 260 | try : |
---|
| 261 | vat = float(options["vat"]) |
---|
| 262 | if not (0.0 <= vat < 100.0) : |
---|
| 263 | raise ValueError |
---|
| 264 | except : |
---|
| 265 | raise PyKotaCommandLineError, _("Incorrect value '%s' for the --vat command line option") % options["vat"] |
---|
| 266 | |
---|
| 267 | try : |
---|
[2668] | 268 | firstnumber = number = int(options["number"]) |
---|
[2661] | 269 | if number <= 0 : |
---|
| 270 | raise ValueError |
---|
| 271 | except : |
---|
| 272 | raise PyKotaCommandLineError, _("Incorrect value '%s' for the --number command line option") % options["number"] |
---|
| 273 | |
---|
[2666] | 274 | self.pagesize = self.getPageSize(options["pagesize"]) |
---|
| 275 | if self.pagesize is None : |
---|
| 276 | self.pagesize = self.getPageSize("a4") |
---|
[2663] | 277 | self.printInfo(_("Invalid 'pagesize' option %s, defaulting to A4.") % options["pagesize"], "warn") |
---|
| 278 | |
---|
[2660] | 279 | if not names : |
---|
| 280 | names = [ "*" ] |
---|
| 281 | |
---|
[2790] | 282 | percent = Percent(self) |
---|
[2661] | 283 | outfname = options["output"] |
---|
| 284 | if outfname != "-" : |
---|
[2790] | 285 | percent.display("%s..." % _("Extracting datas")) |
---|
[2666] | 286 | entries = self.storage.getMatchingUsers(",".join(names)) |
---|
[2790] | 287 | percent.setSize(len(entries)) |
---|
[2663] | 288 | if entries : |
---|
[2790] | 289 | percent.display("\n%s\n" % _("Generating invoices")) |
---|
[2666] | 290 | self.initPDF(options["logo"].strip()) |
---|
[2790] | 291 | for entry in entries : |
---|
[2668] | 292 | number += self.pagePDF(number, entry, vat, options["start"], options["end"], options["unit"]) |
---|
[2663] | 293 | if outfname != "-" : |
---|
[2790] | 294 | percent.oneMore() |
---|
[2663] | 295 | |
---|
[2668] | 296 | if number > firstnumber : |
---|
| 297 | self.endPDF(outfname) |
---|
[2663] | 298 | |
---|
| 299 | if outfname != "-" : |
---|
[2790] | 300 | percent.done() |
---|
[2660] | 301 | |
---|
| 302 | if __name__ == "__main__" : |
---|
| 303 | retcode = 0 |
---|
| 304 | try : |
---|
[2666] | 305 | defaults = { "vat" : "0.0", |
---|
[2662] | 306 | "unit" : N_("Credits"), |
---|
[2660] | 307 | "output" : "-", |
---|
[2661] | 308 | "pagesize" : "a4", \ |
---|
| 309 | "logo" : "/usr/share/pykota/logos/pykota.jpeg", |
---|
| 310 | "number" : "1", |
---|
[2660] | 311 | } |
---|
[3067] | 312 | short_options = "vho:u:V:p:l:n:s:e:" |
---|
[2666] | 313 | long_options = ["help", "version", "start=", "end=", \ |
---|
[3067] | 314 | "unit=", "output=", \ |
---|
[2661] | 315 | "pagesize=", "logo=", "vat=", "number="] |
---|
[2660] | 316 | |
---|
| 317 | # Initializes the command line tool |
---|
| 318 | invoiceGenerator = PKInvoice(doc=__doc__) |
---|
| 319 | invoiceGenerator.deferredInit() |
---|
| 320 | |
---|
| 321 | # parse and checks the command line |
---|
[2662] | 322 | (options, args) = invoiceGenerator.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=True) |
---|
[2660] | 323 | |
---|
| 324 | # sets long options |
---|
| 325 | options["help"] = options["h"] or options["help"] |
---|
| 326 | options["version"] = options["v"] or options["version"] |
---|
| 327 | |
---|
[2666] | 328 | options["start"] = options["s"] or options["start"] |
---|
| 329 | options["end"] = options["e"] or options["end"] |
---|
[2660] | 330 | options["vat"] = options["V"] or options["vat"] or defaults["vat"] |
---|
| 331 | options["unit"] = options["u"] or options["unit"] or defaults["unit"] |
---|
| 332 | options["output"] = options["o"] or options["output"] or defaults["output"] |
---|
[2661] | 333 | options["pagesize"] = options["p"] or options["pagesize"] or defaults["pagesize"] |
---|
| 334 | options["number"] = options["n"] or options["number"] or defaults["number"] |
---|
| 335 | options["logo"] = options["l"] or options["logo"] |
---|
| 336 | if options["logo"] is None : # Allows --logo="" to disable the logo entirely |
---|
| 337 | options["logo"] = defaults["logo"] |
---|
[2660] | 338 | |
---|
| 339 | if options["help"] : |
---|
| 340 | invoiceGenerator.display_usage_and_quit() |
---|
| 341 | elif options["version"] : |
---|
| 342 | invoiceGenerator.display_version_and_quit() |
---|
| 343 | else : |
---|
| 344 | retcode = invoiceGenerator.main(args, options) |
---|
| 345 | except KeyboardInterrupt : |
---|
| 346 | sys.stderr.write("\nInterrupted with Ctrl+C !\n") |
---|
| 347 | retcode = -3 |
---|
| 348 | except PyKotaCommandLineError, msg : |
---|
| 349 | sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) |
---|
| 350 | retcode = -2 |
---|
| 351 | except SystemExit : |
---|
| 352 | pass |
---|
| 353 | except : |
---|
| 354 | try : |
---|
| 355 | invoiceGenerator.crashed("pkinvoice failed") |
---|
| 356 | except : |
---|
| 357 | crashed("pkinvoice failed") |
---|
| 358 | retcode = -1 |
---|
| 359 | |
---|
| 360 | try : |
---|
| 361 | invoiceGenerator.storage.close() |
---|
| 362 | except (TypeError, NameError, AttributeError) : |
---|
| 363 | pass |
---|
| 364 | |
---|
| 365 | sys.exit(retcode) |
---|