474,077 Members | 3,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Grouping report by financial year

2 New Member
I have a script which prints extracts from a database to produce an HTML report by month, grouped by calendar year. The relevant part of the script is below. I now want to adapt the script to group by financial year eg 2007/08 March to April inclusive. Can anyone suggest how I could adapt the script, maybe be creating a new variable 'finyear'?

Expand|Select|Wrap|Line Numbers
  1. sub report_5 () {
  2.     my ($title) = @_;
  3.     output_title($title,5);
  4.  
  5.     output_heading ('Month', '| Sales', ' | Sales value', ' | Renewals', ' | Renewals value', ' | Total', ' | Total value');
  6.  
  7.     my $query = "select 
  8.             SUM(IF(ord.order_prod_code = 'SUB1YR', 1,     0))     as sales_count, 
  9.             SUM(IF(ord.order_prod_code = 'SUB1YR', amt,     0))     as sales_amt, 
  10.             SUM(IF(ord.order_prod_code = 'SUB1YR', 0,     1))     as renewals_count, 
  11.             SUM(IF(ord.order_prod_code = 'SUB1YR', 0,     amt))     as renewals_amt, 
  12.             DATE_FORMAT(pay.date, \"%M% %Y\") as month, 
  13.             DATE_FORMAT(pay.date, \"%Y\") as year
  14.     from 
  15.             $main::UsersTable         as users,
  16.             $main::SubscriptionTable     as subs,
  17.             $main::CustTable         as cust,
  18.             $main::TxnTable         as txn,
  19.             $main::OrderTable         as ord,
  20.             $main::PaymentTable         as pay
  21.             where users.user_id     = subs.user_id
  22.             and   pay.paymkey     = subs.payment_id                and   pay.txn_id     = txn.txnkey
  23.             and   ord.order_txn_id     = txn.txnkey
  24.             and   cust.custkey     = txn.txn_cust_id
  25.             and   pay.status = 'AUTH'                    group by DATE_FORMAT(pay.date, \"%Y\"), DATE_FORMAT(pay.date, \"%M% %Y\")
  26.             order by pay.date desc
  27.             ";
##etc
Jan 3 '08 #1
2 1355
eWish
971 Recognized Expert Contributor
From the looks of the code supplied the sub routine is only taking 1 argument which does not effect the query itself. Where is the code that determines the date you want to select between?

--Kevin
Jan 3 '08 #2
clarihorn
2 New Member
From the looks of the code supplied the sub routine is only taking 1 argument which does not effect the query itself. Where is the code that determines the date you want to select between?

--Kevin
As I really don't understand Perl code, perhaps it's easier if I copy the whole code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. BEGIN {
  4.         require 'config.pl';
  5.         require 'akinit.pl';
  6. }
  7. require 'login_users.pl';
  8. require 'logout.pl';
  9. require 'dbconnect.pl';
  10. require 'modem.pl';
  11. require 'misc.pl';
  12. use Audit;
  13.  
  14.  
  15. $HtmlStart = read_html('login.htm');
  16.  
  17. use DBI;
  18. use CGI;
  19. use CGI::Carp qw(fatalsToBrowser);
  20.  
  21.  
  22. use Login;
  23.  
  24. &DBconnect;
  25.  
  26. # Initialise a few variables
  27. $query_CGI = new CGI;
  28. @param_CGI=$query_CGI->param();
  29. my $countDel;
  30. $Nbr_Parms=$#param_CGI+1;
  31.  
  32. $html_format = $query_CGI->param('html_format');
  33. $password = $query_CGI->param('password');
  34.  
  35. if ($html_format) {
  36.     print "content-Type: text/html\n\n";
  37.     print '<html><body><nobr>';
  38. }
  39. else {
  40.     print "content-Type: text/plain\n\n";
  41.     print "Use CTRL+F in your browser to find text, File->Save As to save a copy to import into Works\n";
  42.     print "e.g. save to a file called transactions.csv\n";
  43. }
  44.  
  45.  
  46. if ($html_format) {
  47. }
  48. report_5('Net monthly sales totals');
  49.  
  50. if ($html_format) {
  51.     print '</nobr></body></html>';
  52. }
  53.  
  54.  
  55.  
  56.  
  57. sub report_5 () {
  58.     my ($title) = @_;
  59.     output_title($title,5);
  60.  
  61.     output_heading ('Month', '| Sales', ' | Sales value', ' | Renewals', ' | Renewals value', ' | Total', ' | Total value');
  62.  
  63.     my $query = "select 
  64.             SUM(IF(ord.order_prod_code = 'SUB1YR', 1,     0))     as sales_count, 
  65.             SUM(IF(ord.order_prod_code = 'SUB1YR', amt,     0))     as sales_amt, 
  66.             SUM(IF(ord.order_prod_code = 'SUB1YR', 0,     1))     as renewals_count, 
  67.             SUM(IF(ord.order_prod_code = 'SUB1YR', 0,     amt))     as renewals_amt, 
  68.             DATE_FORMAT(pay.date, \"%M% %Y\") as month, 
  69.             DATE_FORMAT(pay.date, \"%Y\") as year
  70.     from 
  71.             $main::UsersTable         as users,
  72.             $main::SubscriptionTable     as subs,
  73.             $main::CustTable         as cust,
  74.             $main::TxnTable         as txn,
  75.             $main::OrderTable         as ord,
  76.             $main::PaymentTable         as pay
  77.             where users.user_id     = subs.user_id
  78.             and   pay.paymkey     = subs.payment_id                and   pay.txn_id     = txn.txnkey
  79.             and   ord.order_txn_id     = txn.txnkey
  80.             and   cust.custkey     = txn.txn_cust_id
  81.             and   pay.status = 'AUTH'                    group by DATE_FORMAT(pay.date, \"%Y\"), DATE_FORMAT(pay.date, \"%M% %Y\")
  82.             order by pay.date desc
  83.             ";
  84.  
  85.  
  86.  
  87.     my $sth = $dbh->prepare($query);
  88.     $sth->execute();
  89.  
  90.     $country_count = 0;
  91.     $country_sales = 0;
  92.     $country = '';
  93.     $year = '';
  94.  
  95.     while (my $hash = $sth->fetchrow_hashref()) {
  96.         $month = $hash->{month};
  97.         $amt = $hash->{amt};
  98.         $count = $hash->{count};
  99.  
  100.  
  101.         if (($year ne $hash->{year}) and ($year ne '')) {
  102.             output_bold_record("Total for $year",$y_sales_count_total, $y_sales_amt_total,$y_renewals_count_total,$y_renewals_amt_total,$y_all_count_total,$y_all_amt_total);
  103.             if ($html_format) {
  104.                 print '<tr height=10><td></td></tr>';
  105.             }
  106.             $y_sales_count_total     = 0;
  107.             $y_sales_amt_total     = 0;
  108.             $y_renewals_count_total = 0;
  109.             $y_renewals_amt_total     = 0;
  110.             $y_all_count_total     = 0;
  111.             $y_all_amt_total     = 0;
  112.         }
  113.  
  114.         $year = $hash->{year};
  115.  
  116.  
  117.         output_record(
  118.             $month,
  119.             $hash->{sales_count},
  120.             $hash->{sales_amt},
  121.             $hash->{renewals_count},
  122.             $hash->{renewals_amt},
  123.             $hash->{sales_count} + $hash->{renewals_count},
  124.             $hash->{sales_amt} + $hash->{renewals_amt}
  125.         );
  126.         $sales_count_total     += $hash->{sales_count};
  127.         $sales_amt_total     += $hash->{sales_amt};
  128.         $renewals_count_total     += $hash->{renewals_count};
  129.         $renewals_amt_total     += $hash->{renewals_amt};
  130.         $all_count_total     += $hash->{sales_count} + $hash->{renewals_count};
  131.         $all_amt_total         += $hash->{sales_amt} + $hash->{renewals_amt};
  132.  
  133.  
  134.  
  135.         $y_sales_count_total     += $hash->{sales_count};
  136.         $y_sales_amt_total     += $hash->{sales_amt};
  137.         $y_renewals_count_total += $hash->{renewals_count};
  138.         $y_renewals_amt_total     += $hash->{renewals_amt};
  139.         $y_all_count_total     += $hash->{sales_count} + $hash->{renewals_count};
  140.         $y_all_amt_total     += $hash->{sales_amt} + $hash->{renewals_amt};
  141.     }
  142.  
  143.     output_bold_record("Total for $year",$y_sales_count_total, $y_sales_amt_total,$y_renewals_count_total,$y_renewals_amt_total,$y_all_count_total,$y_all_amt_total);
  144.     if ($html_format) {
  145.         print '<tr height=10><td></td></tr>';
  146.     }
  147.     output_bold_record('Total',$sales_count_total, $sales_amt_total,$renewals_count_total,$renewals_amt_total,$all_count_total,$all_amt_total);
  148.  
  149.     output_footer();
  150. }
  151.  
  152.  
  153.  
  154. sub get_current_status () {
  155.     my ($dbh, $user_id) = @_;
  156.  
  157.     $Query="SELECT first_name, last_name FROM $UsersTable as u , $SubscriptionTable as s WHERE u.user_id =?
  158.             AND NOW() >= s.start_date
  159.             AND NOW() <= s.end_date
  160.         AND u.user_id = s.user_id";
  161.  
  162.     $sth_user = $dbh->prepare($Query)
  163.         ||print "AK572 could not select $Query $!\n $DBI::errstr";
  164.     $sth_user->execute($user_id)
  165.         ||print "AK573 could not select $Query $!\n $DBI::errstr";
  166.  
  167.     if ($sth_user->rows) {
  168.         return 'LIVE';
  169.     }
  170.     else {
  171.         return 'LAPSED';
  172.     }
  173. }
  174.  
  175.  
  176. sub superseded () {
  177.  
  178.     #
  179.     # See if there are any subscriptions for the user that end after the date provided
  180.     #
  181.  
  182.     my ($dbh, $user_id, $end_date) = @_;
  183.  
  184.     $Query="SELECT * FROM $UsersTable as u , $SubscriptionTable as s WHERE u.user_id =?
  185.             AND s.end_date > ?
  186.         AND u.user_id = s.user_id";
  187.  
  188.     $sth_user = $dbh->prepare($Query)
  189.         ||print "AK574 could not select $Query $!\n $DBI::errstr";
  190.     $sth_user->execute($user_id, $end_date)
  191.         ||print "AK575 could not select $Query $!\n $DBI::errstr";
  192.  
  193.     if ($sth_user->rows) {
  194.         return 1;
  195.     }
  196.     else {
  197.         return 0;
  198.     }
  199. }
  200.  
  201.  
  202. sub output_title () {
  203.     my ($title, $name) = @_;
  204.  
  205.     if ($html_format) {
  206.         print "<a name=$name>";
  207.         print "<p><H2>$title </H2>\n";
  208.     }
  209.     else {
  210.         print "\n$title \n";
  211.     }
  212. }
  213.  
  214.  
  215. sub output_sub_total () {
  216.     if ($html_format) {
  217.         print "<tr><td colspan=$no_fields><nobr><b>";
  218.         print join(' ', @_);
  219.         print '</b></nobr></td></tr>' . "\n";
  220.     }
  221.     else {
  222.         print join(' ', @_) . "\n";
  223.     }
  224. }
  225.  
  226. sub output_record () {
  227.     if ($html_format) {
  228.             print '<tr><td><nobr>';
  229.             print join('</nobr></td><td align=right><nobr>', @_);
  230.             print '</nobr></td></tr>' . "\n";
  231.     }
  232.     else {
  233.         print join(',', @_) . "\n";
  234.     }
  235. }
  236.  
  237. sub output_bold_record () {
  238.     if ($html_format) {
  239.             print '<tr><td><b><nobr>';
  240.             print join('</nobr></b></td><td align=right><b><nobr>', @_);
  241.             print '</nobr></b></td></tr>' . "\n";
  242.     }
  243.     else {
  244.         print join(',', @_) . "\n";
  245.     }
  246. }
  247. sub output_heading () {
  248.     $no_fields = scalar(@_);
  249.     if ($html_format) {
  250.         print '<table>';
  251.         print '<tr><td><nobr><b>';
  252.         print join('</b></nobr></td><td><nobr><b>', @_);
  253.         print '</b></nobr></td></tr>' . "\n";
  254.     }
  255.     else {
  256.         print join(',', @_) . "\n";
  257.     }
  258. }
  259.  
  260. sub output_footer () {
  261.     if ($html_format) {
  262.         print '</table>';
  263.     }
  264.     else {
  265.         print "\n\n\n";
  266.     }
  267. }
  268.  
  269.  
Jan 4 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2492
by: Jon via AccessMonster.com | last post by:
Hi Guys, My "Sorting and Grouping" in my report looks like this: Field/Expression Sort Order sID Ascending ((= Project Name Ascending User *** Ascending ((= rID Ascending
3
2874
by: RD | last post by:
Say Company's financial year starts October First and say we are now March 17th. How do you determine which quarter of the Financial year - not the actual year - March 17th is in. Thanks for any help, Bob
2
1670
by: mpmason14 | last post by:
i am trying to sort a report by the year from newest to oldest. however there is a catch... the format of the records i want to sort by is YYMMDD - i am trying to figure out how to get it sorted from the most current (2006) to the oldest (in the 80s). any ideas without having to recreate a new column manually? there are over 500 records, need i say more?
1
1134
by: paulquinlan100 | last post by:
Hi I have a table of investment history ranging over about 10 years, with the structure: Inv_Ref Inv_Type Inv_date I need to produce a report where the data is grouped into columns by Financial year eg
3
5436
by: Jimmy | last post by:
Is there a way to sort/group a report based on the second column of a combo box, i.e. the text associated with the primary key number?
8
1705
by: Ryan | last post by:
I have a SQL view that shows data by Category and Year - so the data may look something like this. Category - Year - Amount Fruit - 2006 - $12,000 Fruit - 2007 - $16,000 Vegetables - 2006 - $15,000 Vegatables - 2007 - $25,000 Say I want to show that data in Tabular view with Category being the Y-Axis and Year being the X-Axis, how would I go about doing this? I'm having a
3
5852
by: sfjnet | last post by:
Hi I am relatively new to Access so I hope I am not embarrassing myself with this question. I have created a database showing contract details (contract number, contractors, dates, other relavant info). Also on the data entry form there is a separate field for each month of the financial year and the user can enter a value in the relevant month(s) corresponding to the number of hours the contractor has worked that month. The users...
7
4338
by: p0lp0l | last post by:
I'm too dummy. Pls help me... The question: Write a C program that accepts for staff of cOMPANY two data values: monthly salary (ringgit amount) and a character value for performance evaluation. The performance evaluation can have the values E (exceed expectation), M (meet expectation), and N (need improvement). We want the program to compute and display the bonus for the
2
6157
by: rajukotla | last post by:
How to find out the number of days in a financial year. suppose from date is sep1st,2006.and to date is aug24th,2009. the entire range involves financial years.i.e., april1st,2006-march31st,2007 april1st,2007-march31st,2008 april1st,2008-march31st,2009 april1st,2009-march31st,2010.
0
10627
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
12276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11683
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10403
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8797
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7954
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
6766
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6948
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
5002
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.