Index: trunk/tools/invoice-generator/invoice.php |
— | — | @@ -37,12 +37,8 @@ |
38 | 38 | return date( $this->conf->dateFormat, $time ); |
39 | 39 | } |
40 | 40 | |
41 | | - function generateInvoice( $num = false ) { |
| 41 | + function generateInvoice( $num ) { |
42 | 42 | $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 | | - } |
47 | 43 | $periodStart = $this->addMonths( $epochStart, $num - 1 ); |
48 | 44 | $periodEnd = strtotime( "-1 day", $this->addMonths( $periodStart, 1 ) ); |
49 | 45 | $replacements = array( |
— | — | @@ -82,7 +78,56 @@ |
83 | 79 | return $result; |
84 | 80 | } |
85 | 81 | |
| 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 | + |
86 | 110 | 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 | + |
87 | 132 | $invoice = $this->generateInvoice( $num ); |
88 | 133 | |
89 | 134 | $headers = 'MIME-Version: 1.0' . "\r\n"; |
— | — | @@ -95,8 +140,17 @@ |
96 | 141 | $headers .= "Cc: {$this->conf->ccTo}\r\n"; |
97 | 142 | } |
98 | 143 | |
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 | + } |
100 | 150 | } |
| 151 | + |
| 152 | + function debugLog( $str ) { |
| 153 | + #echo $str; |
| 154 | + } |
101 | 155 | } |
102 | 156 | |
103 | 157 | $invoice = new Invoice; |
Index: trunk/tools/invoice-generator/invoice.tpl |
— | — | @@ -49,7 +49,7 @@ |
50 | 50 | <td></td> |
51 | 51 | <td colspan="3" align="right"><b>Invoice Total</b></td> |
52 | 52 | <td></td> |
53 | | -<td align="right"><?= $sum ?> <?= $currency ?></td> |
| 53 | +<td align="right"><?= sprintf( "%.2f", $sum ) ?> <?= $currency ?></td> |
54 | 54 | |
55 | 55 | </tr> |
56 | 56 | </table> |
Index: trunk/tools/invoice-generator/conf.php.sample |
— | — | @@ -23,3 +23,13 @@ |
24 | 24 | $bccTo = 'example2@example'; |
25 | 25 | $ccTo = false; |
26 | 26 | $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; |