Browse Source

Update INSERTs and trigger functions to propagate Typica users

Neal Wilson 6 years ago
parent
commit
631f99e1ca

+ 7
- 5
config/Windows/greeninventory.xml View File

@@ -65,8 +65,8 @@
65 65
             button.clicked.connect(function() {
66 66
                 q = "INSERT INTO ";
67 67
                 q += (types.currentIndex == 0 ?
68
-                    "inventory (time, item, quantity)" :
69
-                    "loss (time, item, quantity, reason)");
68
+                    "inventory (time, item, quantity, person)" :
69
+                    "loss (time, item, quantity, reason, person)");
70 70
                 q += " VALUES ('now', ";
71 71
                 q = q + items.currentData();
72 72
                 q = q + ", ";
@@ -82,10 +82,12 @@
82 82
                     q = q + ")";
83 83
                 }
84 84
                 q += (types.currentIndex == 0 ?
85
-                    ")" :
86
-                    ", '" + reason.text + "')");
85
+                    ", :user)" :
86
+                    ", '" + reason.text + "', :user)");
87 87
                 query = new QSqlQuery();
88
-                query.exec(q);
88
+                query.prepare(q);
89
+				query.bind(":user", Application.currentTypicaUser());
90
+				query.exec();
89 91
                 updateStatus();
90 92
             });
91 93
             items['currentIndexChanged(int)'].connect(updateStatus);

+ 2
- 1
config/Windows/greensales.xml View File

@@ -53,7 +53,7 @@
53 53
 			var submit = findChildObject(this, 'submit');
54 54
 			submit.clicked.connect(function() {
55 55
 				var query = new QSqlQuery();
56
-				query.prepare("INSERT INTO sale (time, item, quantity, customer) VALUES(:time, :item, :quantity, :customer)");
56
+				query.prepare("INSERT INTO sale (time, item, quantity, customer, person) VALUES(:time, :item, :quantity, :customer, :user)");
57 57
 				query.bind(":time", dateField.text);
58 58
 				if(customerField.text == "") {
59 59
 					query.bind(":customer", null);
@@ -62,6 +62,7 @@
62 62
 					query.bind(":customer", customerField.text);
63 63
 				}
64 64
 				var coffeesArray = sqlToArray(items.columnArray(0, 32));
65
+				query.bind(":user", Application.currentTypicaUser());
65 66
 				if(coffeesArray.length > 0)
66 67
 				{
67 68
 					for(var i = 0; i < coffeesArray.length; i++)

+ 5
- 3
config/Windows/manuallogentry.xml View File

@@ -405,7 +405,7 @@
405 405
 				query.exec();
406 406
 				query.next();
407 407
 				var roastedId = query.value(0);
408
-				query.prepare("INSERT INTO roasting_log (time, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, transaction_type, annotation, machine, duration, approval, humidity, barometric, indoor_air, outdoor_air, files) VALUES (:time, :unroastedids, :greens, :green, :roastedid, :roasted, 'SAMPLEROAST', :note, :machine, :duration, TRUE, NULL, NULL, NULL, NULL, :files)");
408
+				query.prepare("INSERT INTO roasting_log (time, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, transaction_type, annotation, machine, duration, approval, humidity, barometric, indoor_air, outdoor_air, files, person) VALUES (:time, :unroastedids, :greens, :green, :roastedid, :roasted, 'SAMPLEROAST', :note, :machine, :duration, TRUE, NULL, NULL, NULL, NULL, :files, :user)");
409 409
 				query.bind(":time", roastTime.text);
410 410
 				query.bind(":unroastedids", "{" + greenId + "}");
411 411
 				query.bind(":greens", "{" + convertToPounds(parseFloat(sampleGreenWeight.text), sampleGreenUnit.currentText) + "}");
@@ -416,9 +416,10 @@
416 416
 				query.bind(":machine", Number(selectedRoasterID));
417 417
 				query.bind(":duration", roastDuration.text);
418 418
 				query.bind(":files", "{" + fileID + "}");
419
+				query.bind(":user", Application.currentTypicaUser());
419 420
 				query.exec();
420 421
 			} else {
421
-				var q = "INSERT INTO roasting_log (time, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, transaction_type, annotation, machine, duration, approval, humidity, barometric, indoor_air, outdoor_air, files) VALUES (:time, ";
422
+				var q = "INSERT INTO roasting_log (time, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, transaction_type, annotation, machine, duration, approval, humidity, barometric, indoor_air, outdoor_air, files, person) VALUES (:time, ";
422 423
 				q += productionGreenTable.columnArray(0, 32);
423 424
 				q += ", ";
424 425
 				for(var i = 0; i < productionGreenTable.data(i, 1, 0).value != ""; i++) {
@@ -433,11 +434,12 @@
433 434
 				q += selectedRoasterID;
434 435
 				q += ", :duration, TRUE, NULL, NULL, NULL, NULL, '{";
435 436
 				q += fileID;
436
-				q += "}')";
437
+				q += "}', :user)";
437 438
 				query.prepare(q);
438 439
 				query.bind(":time", roastTime.text);
439 440
 				query.bind(":annotation", notes.plainText);
440 441
 				query.bind(":duration", roastDuration.text);
442
+				query.bind(":user", Application.currentTypicaUser());
441 443
 				query.exec();
442 444
 			}
443 445
 			query = query.invalidate();

+ 2
- 2
config/Windows/navigation.xml View File

@@ -285,9 +285,7 @@
285 285
 		/* Update trigger functions to make column names explicit */
286 286
 		var DBUpdateTriggers = function() {
287 287
 			var query = new QSqlQuery;
288
-			query.exec("CREATE OR REPLACE FUNCTION log_make() RETURNS trigger AS $$ BEGIN IF NEW.roasted_quantity IS NOT NULL THEN INSERT INTO make (time, item, quantity) VALUES(NEW.time, NEW.roasted_id, NEW.roasted_quantity); END IF; RETURN NEW; END; $$ LANGUAGE plpgsql");
289 288
 			query.exec("CREATE OR REPLACE FUNCTION log_make_update() RETURNS trigger AS $$ BEGIN IF NEW.roasted_quantity <> OLD.roasted_quantity AND NEW.roasted_quantity IS NOT NULL THEN INSERT INTO make (time, item, quantity) VALUES(NEW.time, NEW.roasted_id, NEW.roasted_quantity); END IF; RETURN NEW; END; $$ LANGUAGE plpgsql");
290
-			query.exec("CREATE OR REPLACE FUNCTION log_use() RETURNS trigger AS $$ DECLARE i integer := array_lower(NEW.unroasted_id, 1); u integer := array_upper(NEW.unroasted_id, 1); BEGIN IF NEW.transaction_type = 'ROAST' THEN WHILE i <= u LOOP INSERT INTO use (time, item, quantity) VALUES(NEW.time, NEW.unroasted_id[i], NEW.unroasted_quantity[i]); i := i + 1; END LOOP; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql");
291 289
 			query.exec("UPDATE TypicaFeatures SET version = 4 WHERE feature = 'base-features'");
292 290
 		};
293 291
         var DBUpdateReminders = function() {
@@ -310,6 +308,8 @@
310 308
 			query.exec("CREATE TABLE IF NOT EXISTS typica_users(name TEXT PRIMARY KEY NOT NULL, password TEXT, active boolean NOT NULL, auto_login boolean NOT NULL)");
311 309
 			/* Update session user logging to only use the database user when a Typica user is not explicitly provided. This maintains compatibility for mixed version use. */
312 310
 			query.exec("CREATE OR REPLACE FUNCTION log_session_user() RETURNS trigger AS $$ BEGIN IF NEW.person IS NULL THEN NEW.person := session_user; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql");
311
+			query.exec("CREATE OR REPLACE FUNCTION log_use() RETURNS trigger AS $$ DECLARE i integer := array_lower(NEW.unroasted_id, 1); u integer := array_upper(NEW.unroasted_id, 1); BEGIN IF NEW.transaction_type = 'ROAST' THEN WHILE i <= u LOOP INSERT INTO use (time, item, quantity, person) VALUES(NEW.time, NEW.unroasted_id[i], NEW.unroasted_quantity[i], NEW.person); i := i + 1; END LOOP; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql");
312
+			query.exec("CREATE OR REPLACE FUNCTION log_make() RETURNS trigger AS $$ BEGIN IF NEW.roasted_quantity IS NOT NULL THEN INSERT INTO make (time, item, quantity, person) VALUES(NEW.time, NEW.roasted_id, NEW.roasted_quantity, NEW.person); END IF; RETURN NEW; END; $$ LANGUAGE plpgsql");
313 313
 			
314 314
 			/* Uncomment before release */
315 315
 			// query.exec("UPDATE TypicaFeatures SET version = 7 WHERE feature = 'base-features'");

+ 3
- 2
config/Windows/newbatch.xml View File

@@ -585,7 +585,7 @@
585 585
                 checkQuery = checkQuery.invalidate();
586 586
                 query = new QSqlQuery();
587 587
                 var fileno = Number(filenofield.text);
588
-                var q2 = "INSERT INTO roasting_log (time, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, transaction_type, annotation, machine, duration, approval, humidity, barometric, indoor_air, outdoor_air, files) VALUES(:time, ";
588
+                var q2 = "INSERT INTO roasting_log (time, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, transaction_type, annotation, machine, duration, approval, humidity, barometric, indoor_air, outdoor_air, files, person) VALUES(:time, ";
589 589
                 q2 = q2 + table.columnArray(0, 32);
590 590
                 q2 = q2 + ", ";
591 591
                 for(var i = 0; table.data(i, 1, 0).value != ""; i++)
@@ -603,13 +603,14 @@
603 603
                 q2 = q2 + selectedRoasterID;
604 604
                 q2 = q2 + ", :duration, :approval, NULL, NULL, NULL, NULL, '{";
605 605
                 q2 = q2 + fileno;
606
-                q2 = q2 + "}') RETURNING time";
606
+                q2 = q2 + "}', :user) RETURNING time";
607 607
                 query2 = new QSqlQuery();
608 608
                 query2.prepare(q2);
609 609
                 query2.bind(":time", timefield.text);
610 610
                 query2.bind(":annotation", notes.plainText);
611 611
                 query2.bind(":duration", duration.text);
612 612
                 query2.bind(":approval", approval.checked);
613
+				query2.bind(":user", Application.currentTypicaUser());
613 614
                 query2.exec();
614 615
                 if(!query2.next()) {
615 616
                     displayError(TTR("batchWindow", "Database Insert Failed"), TTR("batchWindow", "Failed to save batch to database. Please check inputs and connection and try again."));

+ 2
- 1
config/Windows/newsamplebatch.xml View File

@@ -347,7 +347,7 @@
347 347
 				query.exec();
348 348
 				query.next();
349 349
 				var roastedId = query.value(0);
350
-				query.prepare("INSERT INTO roasting_log (time, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, transaction_type, annotation, machine, duration, approval, humidity, barometric, indoor_air, outdoor_air, files) VALUES(:time, :unroastedids, :greens, :green, :roastedid, :roasted, 'SAMPLEROAST', :note, :machine, :duration, TRUE, NULL, NULL, NULL, NULL, :files)");
350
+				query.prepare("INSERT INTO roasting_log (time, unroasted_id, unroasted_quantity, unroasted_total_quantity, roasted_id, roasted_quantity, transaction_type, annotation, machine, duration, approval, humidity, barometric, indoor_air, outdoor_air, files, person) VALUES(:time, :unroastedids, :greens, :green, :roastedid, :roasted, 'SAMPLEROAST', :note, :machine, :duration, TRUE, NULL, NULL, NULL, NULL, :files, :user)");
351 351
 				query.bind(":time", timefield.text);
352 352
 				query.bind(":unroastedids", "{" + greenId + "}");
353 353
                                 query.bind(":greens", "{" + convertToPounds(parseFloat(green.text), GunitBox.currentText) + "}");
@@ -358,6 +358,7 @@
358 358
 				query.bind(":machine", Number(selectedRoasterID));
359 359
 				query.bind(":duration", duration.text);
360 360
 				query.bind(":files", "{" + fileno + "}");
361
+				query.bind(":user", Application.currentTypicaUser());
361 362
 				query.exec();
362 363
 				query = query.invalidate();
363 364
 				batch.close();

+ 2
- 1
config/Windows/purchase.xml View File

@@ -317,7 +317,7 @@
317 317
                     query.exec();
318 318
                     query.next();
319 319
                     var item_id = query.value(0);
320
-                    q = "INSERT INTO purchase (time, item, quantity, cost, vendor) VALUES(:time, :item, :quantity, :cost, :vendor)";
320
+                    q = "INSERT INTO purchase (time, item, quantity, cost, vendor, person) VALUES(:time, :item, :quantity, :cost, :vendor, :user)";
321 321
                     query.prepare(q);
322 322
                     query.bind(":time", dateField.date);
323 323
                     query.bind(":item", item_id);
@@ -332,6 +332,7 @@
332 332
                         query.bind(":cost", Number(costEntry.text) / convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText));
333 333
                     }
334 334
                     query.bind(":vendor", vendorField.currentText);
335
+					query.bind(":user", Application.currentTypicaUser());
335 336
                     query.exec();
336 337
                     q = "INSERT INTO lb_bag_conversion (item, conversion) VALUES(:item, :conversion)";
337 338
                     query.prepare(q);

Loading…
Cancel
Save