r87001 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87000‎ | r87001 | r87002 >
Date:01:32, 27 April 2011
Author:tstarling
Status:deferred
Tags:
Comment:
* Add a feature allowing duplicate invoices to be suppressed. This means invoices can be sent from a daily anacron job instead of needing fcron to be installed.
* Two decimal places on total value
Modified paths:
  • /trunk/tools/invoice-generator/conf.php.sample (modified) (history)
  • /trunk/tools/invoice-generator/invoice.php (modified) (history)
  • /trunk/tools/invoice-generator/invoice.tpl (modified) (history)

Diff [purge]

Index: trunk/tools/invoice-generator/invoice.php
@@ -37,12 +37,8 @@
3838 return date( $this->conf->dateFormat, $time );
3939 }
4040
41 - function generateInvoice( $num = false ) {
 41+ function generateInvoice( $num ) {
4242 $epochStart = strtotime( $this->conf->epochStart );
43 - if ( $num === false ) {
44 - // Calculate number of months since invoiceStart
45 - $num = $this->timeInMonths( time() ) - $this->timeInMonths( $epochStart ) + 1;
46 - }
4743 $periodStart = $this->addMonths( $epochStart, $num - 1 );
4844 $periodEnd = strtotime( "-1 day", $this->addMonths( $periodStart, 1 ) );
4945 $replacements = array(
@@ -82,7 +78,56 @@
8379 return $result;
8480 }
8581
 82+ function getCurrentInvoiceNumber() {
 83+ // Calculate number of months since epochStart
 84+ $epochStart = strtotime( $this->conf->epochStart );
 85+ return $this->timeInMonths( time() ) - $this->timeInMonths( $epochStart ) + 1;
 86+ }
 87+
 88+ function getLastSentInvoiceNumber() {
 89+ if ( !file_exists( $this->conf->dataDirectory . '/last-sent' ) ) {
 90+ return false;
 91+ }
 92+ $last = file_get_contents( $this->conf->dataDirectory . '/last-sent' );
 93+ if ( $last && trim( $last ) ) {
 94+ return intval( trim( $last ) );
 95+ } else {
 96+ return false;
 97+ }
 98+ }
 99+
 100+ function setLastSentInvoiceNumber( $num ) {
 101+ if ( empty( $this->conf->dataDirectory ) ) {
 102+ return;
 103+ }
 104+ if ( !file_exists( $this->conf->dataDirectory ) ) {
 105+ mkdir( $this->conf->dataDirectory );
 106+ }
 107+ file_put_contents( $this->conf->dataDirectory . '/last-sent', "$num\n" );
 108+ }
 109+
86110 function mailInvoice( $num = false ) {
 111+ $dedupe = false;
 112+ if ( $num === false ) {
 113+ $num = $this->getCurrentInvoiceNumber();
 114+ if ( !empty( $this->conf->sendDayOfMonth ) && !empty( $this->conf->dataDirectory ) ) {
 115+ $dedupe = true;
 116+ }
 117+ }
 118+
 119+ if ( $dedupe ) {
 120+ $last = $this->getLastSentInvoiceNumber();
 121+ if ( $last == $num ) {
 122+ $this->debugLog( "No invoice sent: same month as before\n" );
 123+ return;
 124+ }
 125+ $dayOfMonth = idate( 'd' );
 126+ if ( $dayOfMonth < $this->conf->sendDayOfMonth ) {
 127+ $this->debugLog( "No invoice sent: not the specified day yet\n" );
 128+ return;
 129+ }
 130+ }
 131+
87132 $invoice = $this->generateInvoice( $num );
88133
89134 $headers = 'MIME-Version: 1.0' . "\r\n";
@@ -95,8 +140,17 @@
96141 $headers .= "Cc: {$this->conf->ccTo}\r\n";
97142 }
98143
99 - mail( $this->conf->emailTo, $invoice['subject'], $invoice['text'], $headers );
 144+ $success = mail( $this->conf->emailTo, $invoice['subject'], $invoice['text'], $headers );
 145+
 146+ if ( $dedupe && $success ) {
 147+ echo "Invoice sent to {$this->conf->emailTo}\n";
 148+ $this->setLastSentInvoiceNumber( $num );
 149+ }
100150 }
 151+
 152+ function debugLog( $str ) {
 153+ #echo $str;
 154+ }
101155 }
102156
103157 $invoice = new Invoice;
Index: trunk/tools/invoice-generator/invoice.tpl
@@ -49,7 +49,7 @@
5050 <td></td>
5151 <td colspan="3" align="right"><b>Invoice Total</b></td>
5252 <td></td>
53 -<td align="right"><?= $sum ?> <?= $currency ?></td>
 53+<td align="right"><?= sprintf( "%.2f", $sum ) ?> <?= $currency ?></td>
5454
5555 </tr>
5656 </table>
Index: trunk/tools/invoice-generator/conf.php.sample
@@ -23,3 +23,13 @@
2424 $bccTo = 'example2@example';
2525 $ccTo = false;
2626 $emailFrom = 'example2@example';
 27+
 28+# The day of the month on which to send a new invoice
 29+# If this is false, the invoice will be sent every time the script is run
 30+# If this is set to a number, the script can be run daily, and a new invoice
 31+# will be sent only after the specified day of the month is passed. This allows
 32+# the script to be run from a normal crontab without duplicates being sent.
 33+$sendDayOfMonth = false;
 34+
 35+# The directory in which the last invoice number sent is recorded
 36+$dataDirectory = false;

Status & tagging log