Current code.
[capital-apms.git] / inc / action / process / period_gst.php
blob2c224e70eb97ab6c0c7a2565ce57757b65579e90
1 <?php
2 /**
3 * Component for creating a batch of transactions reflecting GST for the period specified
4 * @package apms
5 * @subpackage period_gst
6 * @author Andrew McMillan <andrew@mcmillan.net.nz>
7 * @copyright Morphoss Ltd <http://www.morphoss.com/>
8 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
9 */
11 param_to_global('companycode', 'int' );
12 param_to_global('period_start', 'int' );
13 param_to_global('period_end', 'int' );
15 get_office_accounts( 'GST-BAD', 'GST-IN', 'GST-OUT', 'GST-SUSPENSE' );
17 $gst_bad_ac = $office_accounts['GST-BAD']->accountcode;
18 $gst_in_ac = $office_accounts['GST-IN']->accountcode;
19 $gst_out_ac = $office_accounts['GST-OUT']->accountcode;
21 $gst_washup = $office_accounts['GST-SUSPENSE']->accountcode;
23 require_once( "transaction_helpers.php" );
25 $qry = new AwlQuery("SELECT min(startdate), max(enddate) FROM month WHERE monthcode >= ? AND monthcode <= ?", $period_start, $period_end);
26 if ( $qry->Exec("period_gst") && $qry->rows() > 0 ) {
27 $period = $qry->Fetch();
29 else {
30 $c->messages[] = "Unable to find period start/end";
31 return true;
34 $sql = <<<EOQ
35 SELECT accountcode, sum(balance) AS sum
36 FROM accountbalance
37 WHERE monthcode >= ? AND monthcode <= ?
38 AND entitytype = 'L' AND entitycode = ?
39 AND accountcode IN ( ?, ?, ? )
40 GROUP BY accountcode
41 EOQ;
43 $qry = new AwlQuery( $sql, intval($period_start), intval($period_end), intval($companycode), $gst_bad_ac, $gst_in_ac, $gst_out_ac );
44 if ( $qry->Exec("period_gst") && $qry->rows() > 0 ) {
46 $batch = new NewBatch( 'AUTO', 0, "Period GST from $period->min to $period->max");
47 $batch->newDocument( "Period GST from $period->min to $period->max", 'Period GST', 'JRNL' );
48 $journal_total = 0.0;
50 while( $account_balance = $qry->Fetch() ) {
51 if ( $account_balance->sum != 0.0 ) {
52 $activity = '';
53 switch( $account_balance->accountcode ) {
54 case $gst_out_ac: $activity = 'collected '; break;
55 case $gst_in_ac: $activity = 'paid out '; break;
56 case $gst_bad_ac: $activity = 'on bad debt written off '; break;
58 $description = sprintf( 'GST %sfrom %s to %s', $activity, $period->min, $period->max);
59 $batch->newTransaction( 'L', $companycode, $account_balance->accountcode, $period->max, - $account_balance->sum, $description );
60 $journal_total += $account_balance->sum;
63 $description = sprintf( 'GST %sdue for the period %s to %s', ($journal_total > 0 ? 'refund ' : ''), $period->min, $period->max);
64 $batch->newTransaction( 'L', $companycode, $gst_washup, $period->max, $journal_total, $description );
65 $batch->Commit();
66 $c->messages[] = sprintf( "GST batch %s created.", $batch->getCode());
68 else {
69 $c->messages[] = "No GST batch to create.";