cleaned all trailing white space from source files.
[sdk] / samples / db / MovieCollection / borrowerReport.ec
1 import "movieSchema"
2 import "reports"
3
4 Date d;
5 class BorrowerReportDetail : Detail
6 {
7    size = { 500, 22 };
8    font = { "Arial", 10 };
9
10    keepTogether = true;
11
12    Label movieName      { this, anchor = { left = 44, top = 2, right = 0.30, bottom = 2 } };
13    Label dateBorrowed   { this, anchor = { left = 0.5, top = 2, right = 0.20, bottom = 2 } };
14
15    bool OnCreate(void)
16    {
17       String s;
18       BorrowerReport report = (BorrowerReport)((ReportDestination)master).GetReport();
19       RowMovies row = (RowMovies)report.groupings[1].row;
20
21       s = row.name; movieName.text = s; delete s;
22       s = PrintString((ShortDate)row.dateBorrowed); dateBorrowed.text = s; delete s;
23       return true;
24    }
25 }
26
27 class BorrowerReportPageHeader : Detail
28 {
29    size = { 500, 30 };
30    font = { "Arial", 10, bold = true };
31
32    Label name { this, anchor = { left = 44, top = 9, right = 0.65, bottom = 2 }, text = "Movie Name" };
33    Label dateBorrowed { this, anchor = { left = 0.5, top = 9, right = 4, bottom = 2 }, text = "Date Borrowed" };
34
35    void OnRedraw(Surface surface)
36    {
37       int x = clientSize.w - 1, y = clientSize.h - 1;
38       surface.Rectangle(0, 5, x, y);
39    }
40 }
41
42 class BorrowerGroupHeader : Detail
43 {
44    size = { 500, 28 };
45    font = { "Arial", 10, bold = true };
46
47    keepTogether = true;
48
49    Label name { this, anchor = { left = 4, top = 7, right = 0.65, bottom = 2 } };
50    Label phone { this, anchor = { left = 0.4, top = 7, right = 4, bottom = 2 } };
51
52    bool OnCreate(void)
53    {
54       String s;
55       BorrowerReport report = (BorrowerReport)((ReportDestination)master).GetReport();
56       RowBorrowers row = (RowBorrowers)report.groupings[0].row;
57       s = row.name; name.text = s; delete s;
58       s = row.phoneNumber; phone.text = s; delete s;
59       return true;
60    }
61
62    void OnRedraw(Surface surface)
63    {
64       int x = clientSize.w - 1, y = clientSize.h - 1;
65       surface.Rectangle(0, 4, x, y - 2);
66    }
67 }
68
69 class BorrowerGroupContinuation : Detail
70 {
71    size = { 500, 28 };
72    font = { "Arial", 10, bold = true };
73
74    keepTogether = true;
75
76    Label name { this, anchor = { left = 4, top = 7, right = 0.65, bottom = 2 } };
77
78    bool OnCreate(void)
79    {
80       String s;
81       BorrowerReport report = (BorrowerReport)((ReportDestination)master).GetReport();
82       RowBorrowers row = (RowBorrowers)report.groupings[0].row;
83
84       s = PrintString(row.name, " (Continued)"); name.text = s; delete s;
85       return true;
86    }
87
88    void OnRedraw(Surface surface)
89    {
90       int x = clientSize.w - 1, y = clientSize.h - 1;
91       surface.Rectangle(0, 4, x, y - 2);
92    }
93 }
94
95 static int numMovies;
96
97 class BorrowerGroupFooter : Detail
98 {
99    size = { 500, 40 };
100    font = { "Arial", 10, bold = true };
101
102    keepTogether = true;
103
104    Label title { this, anchor = { left = 0.85, top = 9, right = 0.03, bottom = 2 }, text = "Total count:" };
105    Label total { this, anchor = { left = 0.97, top = 9, right = 9, bottom = 2 } };
106
107    bool OnCreate(void)
108    {
109       String s = PrintString(numMovies);
110       total.text = s;
111       delete s;
112       return true;
113    }
114
115    void OnRedraw(Surface surface)
116    {
117       int x = clientSize.w - 1, y = clientSize.h - 1;
118       surface.HLine(0, x, 5);
119    }
120 }
121
122 class BorrowerGrouping : Grouping
123 {
124    RowMovies rowMovies { };
125    bool ShouldSkip()
126    {
127       RowBorrowers r = (RowBorrowers)row;
128       int id = r.id;
129       return !rowMovies.Find(dbfield("Movies", borrower), middle, nil, id);
130    }
131 }
132
133 int daysAgo;
134
135 class BorrowerReport : CommonReport
136 {
137    pageHeader = class(BorrowerReportPageHeader);
138    rowDetail = class(BorrowerReportDetail);
139
140    BorrowerReport()
141    {
142       groupings.size = 2;
143
144       groupings[1] = groupings[0];
145
146       groupings[0] = BorrowerGrouping { };
147       groupings[0].field = dbfield("Borrowers", id);
148       groupings[0].header = class(BorrowerGroupHeader);
149       groupings[0].continuation = class(BorrowerGroupContinuation);
150       groupings[0].footer = class(BorrowerGroupFooter);
151    }
152
153    bool ExecuteData(Database db)
154    {
155       DateTime now, d;
156       TimeStamp t;
157       now.GetLocalTime();
158       t = now;
159       t -= 60 * 60 * 24 * daysAgo; // Go back 90 days
160       d = t;
161
162       if(daysAgo)
163       {
164          static char reportTitle[256];
165          sprintf(reportTitle, "Movies borrowed for more than %d days", daysAgo);
166          title = reportTitle;
167       }
168       else
169          title = "Borrowed movies";
170
171       groupings[0].row = RowBorrowers { };
172       groupings[0].row.query = "SELECT ROWID, * FROM `Borrowers` ORDER BY `Name`;";
173       groupings[0].row.Select(nil);
174
175       groupings[1].row = RowMovies { };
176       groupings[1].row.query = "SELECT ROWID, * FROM `Movies` WHERE `Date Borrowed` < ? AND `Borrower` = ? ORDER BY `Date Borrowed`;";
177       groupings[1].row.SetQueryParamObject(1, Date { d.year, d.month, d.day }, class(Date));
178       return true;
179    }
180
181    void ExecuteRowData(int group)
182    {
183       if(group == 0)
184       {
185          groupings[1].row.SetQueryParam(2, (int)groupings[0].row.sysID);
186          numMovies = 0;
187       }
188       else if(group == 1)
189          numMovies++;
190    }
191 }