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] [filterexpr] |
59 | | |
60 | | options : |
61 | | |
62 | | -v | --version Prints pkinvoice's version number then exits. |
63 | | -h | --help Prints this message then exits. |
64 | | |
65 | | -l | --logo img Use the image as the invoice's logo. The logo will |
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 | | |
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 | | |
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. |
88 | | |
89 | | |
90 | | Use the filter expressions to extract only parts of the |
91 | | datas. Allowed filters are of the form : |
92 | | |
93 | | key=value |
94 | | |
95 | | Allowed keys for now are : |
96 | | |
97 | | username User's name |
98 | | printername Printer's name |
99 | | hostname Client's hostname |
100 | | jobid Job's Id |
101 | | billingcode Job's billing code |
102 | | start Job's date of printing |
103 | | end Job's date of printing |
104 | | |
105 | | Dates formatting with 'start' and 'end' filter keys : |
106 | | |
107 | | YYYY : year boundaries |
108 | | YYYYMM : month boundaries |
109 | | YYYYMMDD : day boundaries |
110 | | YYYYMMDDhh : hour boundaries |
111 | | YYYYMMDDhhmm : minute boundaries |
112 | | YYYYMMDDhhmmss : second boundaries |
113 | | yesterday[+-NbDays] : yesterday more or less N days (e.g. : yesterday-15) |
114 | | today[+-NbDays] : today more or less N days (e.g. : today-15) |
115 | | tomorrow[+-NbDays] : tomorrow more or less N days (e.g. : tomorrow-15) |
116 | | now[+-NbDays] : now more or less N days (e.g. now-15) |
117 | | |
118 | | 'now' and 'today' are not exactly the same since today represents the first |
119 | | or last second of the day depending on if it's used in a start= or end= |
120 | | date expression. The utility to be able to specify dates in the future is |
121 | | a question which remains to be answered :-) |
122 | | |
123 | | Contrary to other PyKota management tools, wildcard characters are not |
124 | | expanded, so you can't use them. |
125 | | |
126 | | examples : |
127 | | |
128 | | $ pkinvoice --unit EURO --output /tmp/invoices.pdf start=now-30 |
129 | | |
130 | | Will generate a PDF document containing invoices for all users |
131 | | who have spent some credits last month. Invoices will be done in |
132 | | EURO. No VAT information will be included. |
133 | | """) |
134 | | |
368 | | retcode = 0 |
369 | | try : |
370 | | defaults = { "vat" : "0.0", |
371 | | "unit" : N_("Credits"), |
372 | | "output" : "-", |
373 | | "pagesize" : "a4", \ |
374 | | "logo" : "/usr/share/pykota/logos/pykota.jpeg", |
375 | | "number" : "1", |
376 | | } |
377 | | short_options = "vho:u:V:p:l:n:" |
378 | | long_options = ["help", "version", "unit=", "output=", \ |
379 | | "pagesize=", "logo=", "vat=", "number="] |
380 | | |
381 | | # Initializes the command line tool |
382 | | invoiceGenerator = PKInvoice(doc=__doc__) |
383 | | invoiceGenerator.deferredInit() |
384 | | |
385 | | # parse and checks the command line |
386 | | (options, args) = invoiceGenerator.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=True) |
387 | | |
388 | | # sets long options |
389 | | options["help"] = options["h"] or options["help"] |
390 | | options["version"] = options["v"] or options["version"] |
391 | | |
392 | | options["vat"] = options["V"] or options["vat"] or defaults["vat"] |
393 | | options["unit"] = options["u"] or options["unit"] or defaults["unit"] |
394 | | options["output"] = options["o"] or options["output"] or defaults["output"] |
395 | | options["pagesize"] = options["p"] or options["pagesize"] or defaults["pagesize"] |
396 | | options["number"] = options["n"] or options["number"] or defaults["number"] |
397 | | options["logo"] = options["l"] or options["logo"] |
398 | | if options["logo"] is None : # Allows --logo="" to disable the logo entirely |
399 | | options["logo"] = defaults["logo"] |
400 | | |
401 | | if options["help"] : |
402 | | invoiceGenerator.display_usage_and_quit() |
403 | | elif options["version"] : |
404 | | invoiceGenerator.display_version_and_quit() |
405 | | else : |
406 | | retcode = invoiceGenerator.main(args, options) |
407 | | except KeyboardInterrupt : |
408 | | logerr("\nInterrupted with Ctrl+C !\n") |
409 | | retcode = -3 |
410 | | except PyKotaCommandLineError, msg : |
411 | | logerr("%s : %s\n" % (sys.argv[0], msg)) |
412 | | retcode = -2 |
413 | | except SystemExit : |
414 | | pass |
415 | | except : |
416 | | try : |
417 | | invoiceGenerator.crashed("pkinvoice failed") |
418 | | except : |
419 | | crashed("pkinvoice failed") |
420 | | retcode = -1 |
421 | | |
422 | | try : |
423 | | invoiceGenerator.storage.close() |
424 | | except (TypeError, NameError, AttributeError) : |
425 | | pass |
426 | | |
427 | | sys.exit(retcode) |
| 271 | parser = PyKotaOptionParser(description=_("Invoice generator for PyKota."), |
| 272 | usage="pkinvoice [options] [filterexpr]") |
| 273 | parser.add_option("-l", "--logo", |
| 274 | dest="logo", |
| 275 | default=u"/usr/share/pykota/logos/pykota.jpeg", |
| 276 | help=_("The image to use as a logo. The logo will be drawn at the center top of the page. The default logo is %default")) |
| 277 | parser.add_option("-p", "--pagesize", |
| 278 | type="string", |
| 279 | action="callback", |
| 280 | callback=checkandset_pagesize, |
| 281 | dest="pagesize", |
| 282 | default=u"A4", |
| 283 | help=_("Set the size of the page. Most well known page sizes are recognized, like 'A4' or 'Letter' to name a few. The default page size is %default")) |
| 284 | parser.add_option("-n", "--number", |
| 285 | dest="number", |
| 286 | type="int", |
| 287 | action="callback", |
| 288 | callback=checkandset_positiveint, |
| 289 | default=1, |
| 290 | help=_("Sets the number of the first invoice. This number will automatically be incremented for each invoice. The default value is %default")) |
| 291 | parser.add_option("-o", "--output", |
| 292 | dest="output", |
| 293 | type="string", |
| 294 | default=u"-", |
| 295 | help=_("The name of the file to which the PDF invoices will be written. If not set or set to '%default', the PDF document will be sent to the standard output.")) |
| 296 | |
| 297 | # TODO : due to Python's optparse.py bug #1498146 fixed in rev 46861 |
| 298 | # TODO : we can't use 'default=_("Credits")' for this option |
| 299 | parser.add_option("-u", "--unit", |
| 300 | dest="unit", |
| 301 | type="string", |
| 302 | help=_("The name of the unit to use on the invoices. The default value is 'Credits' or its locale translation.")) |
| 303 | |
| 304 | parser.add_option("-V", "--vat", |
| 305 | dest="vat", |
| 306 | type="float", |
| 307 | action="callback", |
| 308 | callback=checkandset_percent, |
| 309 | default=0.0, |
| 310 | help=_("The value in percent of the applicable VAT to be exposed. The default is %default, meaning no VAT.")) |
| 311 | |
| 312 | parser.add_filterexpression("username", _("User's name")) |
| 313 | parser.add_filterexpression("printername", _("Printer's name")) |
| 314 | parser.add_filterexpression("hostname", _("Host's name")) |
| 315 | parser.add_filterexpression("jobid", _("Job's id")) |
| 316 | parser.add_filterexpression("billingcode", _("Job's billing code")) |
| 317 | parser.add_filterexpression("start", _("Job's date of printing")) |
| 318 | parser.add_filterexpression("end", _("Job's date of printing")) |
| 319 | |
| 320 | parser.add_example('--unit EURO --output /tmp/invoices.pdf start=now-30', |
| 321 | _("This would generate a PDF document containing invoices for all users who have spent some credits last month. Amounts would be in EURO and not VAT information would be included.")) |
| 322 | |
| 323 | run(parser, PKInvoice) |