Changeset 2666
- Timestamp:
- 02/11/06 11:59:13 (19 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/bin/pkinvoice
r2663 r2666 46 46 hasPIL = 1 47 47 48 from pykota.tool import PyKotaTool, PyKotaToolError, PyKotaCommandLineError, crashed, N_ 48 from pykota.tool import PyKotaToolError, PyKotaCommandLineError, crashed, N_ 49 from pykota.dumper import DumPyKota 49 50 from pykota.config import PyKotaConfigError 50 51 from pykota.storage import PyKotaStorageError … … 62 63 -v | --version Prints edpykota's version number then exits. 63 64 -h | --help Prints this message then exits. 64 65 -g | --groups Generate invoices for users groups instead of users.66 65 67 66 -l | --logo img Use the image as the banner's logo. The logo will … … 75 74 -n | --number N Sets the number of the first invoice. This number 76 75 will automatically be incremented for each invoice. 77 78 -r | --reference v Uses v as the initial reference value : an invoice79 will be generated for any account balance's value80 lower or equal to the reference value. The81 default reference value is of course 0.0 credits.82 76 83 77 -o | --output f.pdf Defines the name of the invoice file which will … … 94 88 information will be included. 95 89 96 user1 through userN and group1 through groupN can use wildcards if 97 needed. If no user argument is used, a wildcard of '*' is assumed, 98 meaning include all users or groups. 99 90 -s | --start date Sets the starting date for the print jobs invoiced. 91 92 -e | --end date Sets the ending date for the print jobs invoiced. 93 94 user1 through userN can use wildcards if needed. If no user argument is 95 used, a wildcard of '*' is assumed, meaning include all users. 96 97 Dates formating with --start and --end : 98 99 YYYY : year boundaries 100 YYYYMM : month boundaries 101 YYYYMMDD : day boundaries 102 YYYYMMDDhh : hour boundaries 103 YYYYMMDDhhmm : minute boundaries 104 YYYYMMDDhhmmss : second boundaries 105 yesterday[+-NbDays] : yesterday more or less N days (e.g. : yesterday-15) 106 today[+-NbDays] : today more or less N days (e.g. : today-15) 107 tomorrow[+-NbDays] : tomorrow more or less N days (e.g. : tomorrow-15) 108 now[+-NbDays] : now more or less N days (e.g. now-15) 109 110 'now' and 'today' are not exactly the same since today represents the first 111 or last second of the day depending on if it's used in a start= or end= 112 date expression. The utility to be able to specify dates in the future is 113 a question which remains to be answered :-) 114 100 115 examples : 101 116 102 $ pkinvoice --reference 15.0 --unit EURO --output invoices.pdf 103 104 Will generate a PDF document containing invoices for all users whose 105 account balance is below 15.0 credits. For each user the amount due 106 will be (15.0 - AccountBalance) EURO when that value is positive. 107 No VAT information will be included. 117 $ pkinvoice --unit EURO --output invoices.pdf --start=now-30 118 119 Will generate a PDF document containing invoices for all users 120 who have spent some credits last month. Invoices will be done in 121 EURO. No VAT information will be included. 108 122 """) 109 123 110 class PKInvoice( PyKotaTool) :124 class PKInvoice(DumPyKota) : 111 125 """A class for pkinvoice.""" 112 126 def getPageSize(self, pgsize) : … … 120 134 pass 121 135 122 def pagePDF(self, invoicenumber, name, amount, vatamount) :136 def pagePDF(self, invoicenumber, entry, vat, start, end) : 123 137 """Generates a new page in the PDF document.""" 124 sys.stderr.write("#%06i %s %.2f %.2f\n" % (invoicenumber, name, amount, vatamount))125 138 self.canvas.doForm("background") 139 140 extractonly = { "username" : entry.Name } 141 if start : 142 extractonly["start"] = start 143 if end : 144 extractonly["end"] = end 145 records = self.storage.extractHistory(extractonly) 146 amount = vatamount = 0.0 147 vatamount = 0.0 148 if records : 149 records = self.summarizeDatas(records, "history", extractonly, True) 150 fieldnames = records[0] 151 fields = {} 152 for i in range(len(fieldnames)) : 153 fields[fieldnames[i]] = i 154 numberofbytes = records[1][fields["jobsizebytes"]] 155 numberofpages = records[1][fields["jobsize"]] 156 amount = records[1][fields["jobprice"]] 157 if amount > 0.0 : 158 # There's something due ! 159 ht = ((amount * 10000.0) / (100.0 + vat)) / 100.0 160 vatamount = amount - ht 161 sys.stderr.write("#%06i %s %s %s %.2f %.2f\n" % (invoicenumber, entry.Name, numberofpages, numberofbytes, amount, vatamount)) 162 126 163 self.canvas.showPage() 127 164 128 def initPDF(self, pagesize,logo) :165 def initPDF(self, logo) : 129 166 """Initializes the PDF document.""" 130 167 self.pdfDocument = cStringIO.StringIO() 131 self.canvas = c = canvas.Canvas(self.pdfDocument, pagesize=pagesize, pageCompression=1) 168 self.canvas = c = canvas.Canvas(self.pdfDocument, \ 169 pagesize=self.pagesize, \ 170 pageCompression=1) 132 171 133 172 c.setAuthor(pwd.getpwuid(os.geteuid())[0]) … … 135 174 c.setSubject("This is an invoice generated with PyKota") 136 175 137 xcenter = pagesize[0] / 2.0138 ycenter = pagesize[1] / 2.0176 xcenter = self.pagesize[0] / 2.0 177 ycenter = self.pagesize[1] / 2.0 139 178 140 ypos = pagesize[1] - (2 * cm)179 ypos = self.pagesize[1] - (2 * cm) 141 180 142 181 self.canvas.beginForm("background") … … 158 197 159 198 # New top 160 xpos = pagesize[0] / 5.0199 xpos = self.pagesize[0] / 5.0 161 200 ypos -= (1 * cm) + 20 162 201 … … 193 232 194 233 try : 195 reference = float(options["reference"])196 except :197 raise PyKotaCommandLineError, _("Incorrect value '%s' for the --reference command line option") % options["reference"]198 199 try :200 234 number = float(options["number"]) 201 235 if number <= 0 : … … 204 238 raise PyKotaCommandLineError, _("Incorrect value '%s' for the --number command line option") % options["number"] 205 239 206 pagesize = self.getPageSize(options["pagesize"])207 if pagesize is None :208 pagesize = self.getPageSize("a4")240 self.pagesize = self.getPageSize(options["pagesize"]) 241 if self.pagesize is None : 242 self.pagesize = self.getPageSize("a4") 209 243 self.printInfo(_("Invalid 'pagesize' option %s, defaulting to A4.") % options["pagesize"], "warn") 210 244 … … 216 250 self.display("%s...\n" % _("Processing")) 217 251 218 suffix = (options["groups"] and "Group") or "User" 219 entries = getattr(self.storage, "getMatching%ss" % suffix)(",".join(names)) 252 entries = self.storage.getMatchingUsers(",".join(names)) 220 253 if entries : 221 self.initPDF( pagesize,options["logo"].strip())254 self.initPDF(options["logo"].strip()) 222 255 nbtotal = len(entries) 223 256 for i in range(nbtotal) : 224 257 entry = entries[i] 225 amount = reference - entry.AccountBalance 226 if amount > 0.0 : 227 # There's something due ! 228 ht = ((amount * 10000.0) / (100.0 + vat)) / 100.0 229 vatamount = amount - ht 230 self.pagePDF(number, entry.Name, amount, vatamount) 231 number += 1 258 self.pagePDF(number, entry, vat, options["start"], options["end"]) 259 number += 1 232 260 if outfname != "-" : 233 261 percent = 100.0 * float(i) / float(nbtotal) … … 243 271 retcode = 0 244 272 try : 245 defaults = { "reference" : "0.0", 246 "vat" : "0.0", 273 defaults = { "vat" : "0.0", 247 274 "unit" : N_("Credits"), 248 275 "output" : "-", … … 251 278 "number" : "1", 252 279 } 253 short_options = "vho: gr:u:V:p:l:n:"254 long_options = ["help", "version", \255 " groups", "reference=", "unit=", "output=", \280 short_options = "vho:r:u:V:p:l:n:s:e:" 281 long_options = ["help", "version", "start=", "end=", \ 282 "reference=", "unit=", "output=", \ 256 283 "pagesize=", "logo=", "vat=", "number="] 257 284 … … 267 294 options["version"] = options["v"] or options["version"] 268 295 269 options[" groups"] = options["g"] or options["groups"]270 options[" reference"] = options["r"] or options["reference"] or defaults["reference"]296 options["start"] = options["s"] or options["start"] 297 options["end"] = options["e"] or options["end"] 271 298 options["vat"] = options["V"] or options["vat"] or defaults["vat"] 272 299 options["unit"] = options["u"] or options["unit"] or defaults["unit"]