Browse Source

Cost of Green for Roasted Coffee Report

Neal Wilson 11 years ago
parent
commit
5897ed1583
1 changed files with 133 additions and 0 deletions
  1. 133
    0
      config/Reports/cogr.xml

+ 133
- 0
config/Reports/cogr.xml View File

@@ -0,0 +1,133 @@
1
+<window id="greencost">
2
+	<reporttitle>Production:->Cost of Green Coffee for Roasted Coffee</reporttitle>
3
+	<layout type="vertical">
4
+		<webview id="report" />
5
+	</layout>
6
+	<menu name="File">
7
+		<item id="print" shortcut="Ctrl+P">Print</item>
8
+	</menu>
9
+	<program>
10
+		<![CDATA[
11
+			this.windowTitle = "Typica - Cost of Green Coffee for Roasted Coffee";
12
+			var report = findChildObject(this, 'report');
13
+			var printMenu = findChildObject(this, 'print');
14
+			printMenu.triggered.connect(function() {
15
+				report.print();
16
+			});
17
+			function refresh() {
18
+				var buffer = new QBuffer;
19
+				buffer.open(3);
20
+				var output = new XmlWriter(buffer);
21
+				output.writeStartDocument("1.0");
22
+				output.writeDTD('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg.dtd">');
23
+                output.writeStartElement("html");
24
+                output.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
25
+                output.writeStartElement("head");
26
+                output.writeTextElement("title", "Cost of Green Coffee for Roasted Coffee");
27
+                output.writeEndElement();
28
+                output.writeStartElement("body");
29
+                output.writeTextElement("h1", "Cost of Green Coffee for Roasted Coffee");
30
+				var query = new QSqlQuery();
31
+				query.exec("SELECT item, (SELECT name FROM items WHERE id = item) AS name FROM current_items ORDER BY name");
32
+				var roastedItems = new Array();
33
+				var roastedNames = new Array();
34
+				while(query.next()) {
35
+					roastedItems.push(query.value(0));
36
+					roastedNames.push(query.value(1));
37
+				}
38
+				var recipes = new Array();
39
+				var recipeQuantities = new Array();
40
+				query.prepare("SELECT unroasted_id, unroasted_quantity FROM roasting_log WHERE roasted_id = :item AND time = (SELECT max(time) FROM roasting_log WHERE roasted_id = :item2)");
41
+				for(var i = 0; i < roastedItems.length; i++) {
42
+					query.bind("item", roastedItems[i]);
43
+					query.bind("item2", roastedItems[i]);
44
+					query.exec();
45
+					if(query.next()) {
46
+						recipes.push(query.value(0));
47
+						recipeQuantities.push(query.value(1));
48
+					} else {
49
+						print("Error 1");
50
+					}
51
+				}
52
+				query.prepare("SELECT 1 / (-1 * (min((unroasted_total_quantity - roasted_quantity)  / unroasted_total_quantity) - 1)), 1 / (-1 * (max((unroasted_total_quantity - roasted_quantity)/unroasted_total_quantity) - 1)), 1 / (-1 * (avg((unroasted_total_quantity - roasted_quantity) / unroasted_total_quantity) - 1)) FROM roasting_log WHERE roasted_id = :ri AND unroasted_id = :gi AND unroasted_total_quantity > 0 AND roasted_quantity > 0 AND approval = true");
53
+				var mins = new Array();
54
+				var maxes = new Array();
55
+				var means = new Array();
56
+				for(var i = 0; i < roastedItems.length; i++) {
57
+					query.bind(":ri", Number(roastedItems[i]));
58
+					query.bind(":gi", recipes[i]);
59
+					query.exec();
60
+					if(query.next()) {
61
+						mins.push(query.value(0));
62
+						maxes.push(query.value(1));
63
+						means.push(query.value(2));
64
+					} else {
65
+						print("Error 2");
66
+					}
67
+				}
68
+				var proportionalCosts = new Array();
69
+				query.prepare("SELECT cost * :proportion FROM purchase WHERE item = :id");
70
+				for(var i = 0; i < roastedItems.length; i++) {
71
+					greens = sqlToArray(recipes[i]);
72
+					weights = sqlToArray(recipeQuantities[i]);
73
+					proportions = new Array();
74
+					quantitySum = weights.reduce(function(p, c) {
75
+						return p + c;
76
+					});
77
+					for(var j = 0; j < weights.length; j++) {
78
+						proportions.push(weights[j]/quantitySum);
79
+					}
80
+					partialSum = 0;
81
+					for(var j = 0; j < greens.length; j++) {
82
+						query.bind(":proportion", proportions[j]);
83
+						query.bind(":id", greens[j]);
84
+						query.exec();
85
+						if(query.next()) {
86
+							partialSum += query.value(0);
87
+						} else {
88
+							print("Error 3");
89
+						}
90
+					}
91
+					proportionalCosts.push(partialSum);
92
+				}
93
+				var minCosts = new Array();
94
+				var maxCosts = new Array();
95
+				var meanCosts = new Array();
96
+				for(var i = 0; i < roastedItems.length; i++) {
97
+					minCosts.push(proportionalCosts[i] * mins[i]);
98
+					maxCosts.push(proportionalCosts[i] * maxes[i]);
99
+					meanCosts.push(proportionalCosts[i] * means[i]);
100
+				}
101
+				output.writeStartElement("table");
102
+				output.writeAttribute("rules", "groups");
103
+				output.writeAttribute("cellpadding", "3px");
104
+				output.writeStartElement("thead");
105
+				output.writeStartElement("tr");
106
+				output.writeTextElement("th", "Coffee");
107
+				output.writeTextElement("th", "Minimum Cost");
108
+				output.writeTextElement("th", "Maximum Cost");
109
+				output.writeTextElement("th", "Mean Cost");
110
+				output.writeEndElement();
111
+				output.writeEndElement();
112
+				output.writeStartElement("tbody");
113
+				for(var i = 0; i < roastedItems.length; i++) {
114
+					output.writeStartElement("tr");
115
+					output.writeTextElement("td", roastedNames[i]);
116
+					output.writeTextElement("td", Number(minCosts[i]).toFixed(2));
117
+					output.writeTextElement("td", Number(maxCosts[i]).toFixed(2));
118
+					output.writeTextElement("td", Number(meanCosts[i]).toFixed(2));
119
+					output.writeEndElement();
120
+				}
121
+				output.writeEndElement();
122
+				output.writeEndElement();
123
+				output.writeEndElement();
124
+				output.writeEndElement();
125
+				output.writeEndDocument();
126
+				report.setContent(buffer);
127
+				buffer.close();
128
+			};
129
+			refresh();
130
+		]]>
131
+	</program>
132
+</window>
133
+

Loading…
Cancel
Save