123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- <window id="purchase">
- <layout type="vertical">
- <layout type="horizontal">
- <button name="Add Item" type="push" id="newForm" />
- <label>Date:</label>
- <calendar id="date"/>
- <label>Vendor:</label>
- <sqldrop data="0" display="0" showdata="false" editable="true" id="vendor">
- <query>SELECT DISTINCT vendor FROM purchase UNION SELECT '' ORDER BY vendor ASC</query>
- </sqldrop>
- <label>Invoice:</label>
- <line id="invoice" />
- </layout>
- <formarray id="form">
- <layout type="grid">
- <row>
- <column><label>Item:</label></column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="true" id="name">
- <query>SELECT DISTINCT name FROM coffees UNION SELECT '' ORDER BY name ASC</query>
- </sqldrop>
- </column>
- <column />
- <column><label>Reference:</label></column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="true" id="reference">
- <query>SELECT DISTINCT reference FROM coffees UNION SELECT '' ORDER BY reference ASC</query>
- </sqldrop>
- </column>
- </row>
- <row>
- <column><label>Quantity:</label></column>
- <column>
- <line validator="numeric" id="quantity" />
- </column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="false" id="units">
- <query>SELECT * FROM (VALUES('Lb'),('Kg')) AS q (unit)</query>
- </sqldrop>
- </column>
- </row>
- <row>
- <column><label>Cost:</label></column>
- <column>
- <line validator="numeric" id="cost" />
- </column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="false" id="costModifier">
- <query>SELECT 'per unit' UNION SELECT 'total'</query>
- </sqldrop>
- </column>
- </row>
- <row>
- <column><label>Origin:</label></column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="true" id="origin">
- <query>SELECT DISTINCT origin FROM coffees UNION SELECT '' ORDER BY origin ASC</query>
- </sqldrop>
- </column>
- <column />
- <column><label>Region:</label></column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="true" id="region">
- <query>SELECT DISTINCT region FROM coffees UNION SELECT '' ORDER BY region ASC</query>
- </sqldrop>
- </column>
- </row>
- <row>
- <column><label>Producer</label></column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="true" id="producer">
- <query>SELECT DISTINCT producer FROM coffees UNION SELECT '' ORDER BY producer ASC</query>
- </sqldrop>
- </column>
- <column />
- <column><label>Grade:</label></column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="true" id="grade">
- <query>SELECT DISTINCT grade FROM coffees UNION SELECT '' ORDER BY grade ASC</query>
- </sqldrop>
- </column>
- </row>
- <row>
- <column><label>Milling:</label></column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="true" id="milling">
- <query>SELECT DISTINCT milling FROM coffees UNION SELECT '' ORDER BY milling ASC</query>
- </sqldrop>
- </column>
- <column />
- <column><label>Drying:</label></column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="true" id="drying">
- <query>SELECT DISTINCT drying FROM coffees UNION SELECT '' ORDER BY drying ASC</query>
- </sqldrop>
- </column>
- </row>
- <row>
- <column><label>Bags:</label></column>
- <column>
- <line validator="numeric" id="bags" />
- </column>
- <column>
- <button type="check" id="isDecaf" name="Decaffeinated" />
- </column>
- <column><label>by method:</label></column>
- <column>
- <sqldrop data="0" display="0" showdata="false" editable="true" id="decafMethod">
- <query>SELECT DISTINCT decaf_method FROM decaf_coffees UNION SELECT '' ORDER BY decaf_method ASC</query>
- </sqldrop>
- </column>
- </row>
- <row>
- <column colspan="2">
- <line id="certdisplay" writable="false" />
- </column>
- <column column="2">
- <button type="push" name="Add Certification" id="addcert" />
- </column>
- <column colspan="2">
- <sqldrop data="0" display="0" showdata="false" editable="true" id="certification">
- <query>SELECT DISTINCT certification FROM certifications ORDER BY certification ASC</query>
- </sqldrop>
- </column>
- </row>
- </layout>
- </formarray>
- <layout type="horizontal">
- <layout type="vertical">
- <label>Fees:</label>
- <sqltablearray columns="2" id="fees">
- <column name="Fee Description" />
- <column name="Amount" />
- </sqltablearray>
- </layout>
- <button name="Submit" type="push" id="submit" />
- </layout>
- </layout>
- <program>
- <![CDATA[
- var window = this;
- var convertToPounds = function(w, u) {
- switch(u) {
- case "g":
- return w * 0.0022;
- case "oz":
- return w * 0.0625;
- case "Kg":
- return w * 2.2;
- }
- return w;
- };
- var convertToPerPounds = function(w, u) {
- switch(u) {
- case "g":
- return w / 0.0022;
- case "oz":
- return w / 0.0625;
- case "Kg":
- return w / 2.2;
- }
- return w;
- };
- this.windowTitle = TTR("purchase", "Typica - Coffee Purchase");
- var form = findChildObject(this, 'form');
- form.setMaximumElementHeight(320);
- var appendForm = function() {
- form.addElements(1);
- var thisCoffee = form.elementAt(form.elements() - 1);
- var decafButton = findChildObject(thisCoffee, 'isDecaf');
- var methodField = findChildObject(thisCoffee, 'decafMethod');
- methodField.enabled = false;
- decafButton.stateChanged.connect(function(state) {
- if(state == 0)
- {
- methodField.enabled = false;
- }
- else
- {
- methodField.enabled = true;
- }
- });
- var certificationButton = findChildObject(thisCoffee, 'addcert');
- var certSource = findChildObject(thisCoffee, 'certification');
- var certTarget = findChildObject(thisCoffee, 'certdisplay');
- certificationButton.clicked.connect(function() {
- if(thisCoffee.certificationArray === undefined)
- {
- thisCoffee.certificationArray = new Array();
- }
- thisCoffee.certificationArray.push(certSource.currentText);
- certTarget.text = thisCoffee.certificationArray.toString();
- certSource.currentIndex = -1;
- });
- };
- appendForm();
- var itemButton = findChildObject(this, 'newForm');
- itemButton.clicked.connect(function() {
- appendForm();
- });
- var invoiceField = findChildObject(this, 'invoice');
- var vendorField = findChildObject(this, 'vendor');
- var feesTable = findChildObject(this, 'fees');
- var submitButton = findChildObject(this, 'submit');
- var dateField = findChildObject(this, 'date');
- var validate = function() {
- if(vendorField.currentText == '') {
- displayError(TTR("purchase", "Missing Input"),
- TTR("purchase", "Vendor is a required field."));
- return false;
- }
- for(var i = 0; i < form.elements(); i++) {
- if(findChildObject(form.elementAt(i), 'name').text == '') {
- displayError(TTR("purchase", "Missing Input"),
- TTR("purchase", "Item is a required field."));
- return false;
- }
- if(findChildObject(form.elementAt(i), 'quantity').text == '') {
- displayError(TTR("purchase", "Missing Input"),
- TTR("purchase", "Quantity is a required field."));
- return false;
- }
- if(findChildObject(form.elementAt(i), 'cost').text == '') {
- displayError(TTR("purchase", "Missing Input"),
- TTR("purchase", "Cost is a required field."));
- return false;
- }
- if(findChildObject(form.elementAt(i), 'origin').currentText == '') {
- displayError(TTR("purchase", "Missing Input"),
- TTR("purchase", "Origin is a required field."));
- return false;
- }
- if(findChildObject(form.elementAt(i), 'bags').text == '') {
- displayError(TTR("purchase", "Missing Input"),
- TTR("purchase", "Bags is a required field."));
- return false;
- }
- if(findChildObject(form.elementAt(i), 'isDecaf').checked) {
- if(findChildObject(form.elementAt(i), 'decafMethod').text == '') {
- displayError(TTR("purchase", "Missing Input"),
- TTR("purchase", "Decaffeination method is a required field for decaffeinated coffees."));
- return false;
- }
- }
- }
- return true;
- };
- submitButton.clicked.connect(function() {
- if(!validate()) {
- return;
- }
- var query = new QSqlQuery;
- var q = "INSERT INTO invoices (id, invoice, vendor, time) VALUES (default, :invoice, :vendor, :date) RETURNING id";
- query.prepare(q);
- query.bind(":invoice", invoiceField.text);
- query.bind(":vendor", vendorField.currentText);
- query.bind(":date", dateField.date);
- query.exec();
- query.next();
- var invoiceNumber = query.value(0);
- for(var i = 0; i < form.elements(); i++)
- {
- var current = form.elementAt(i);
- var nameEntry = findChildObject(current, 'name');
- var quantityEntry = findChildObject(current, 'quantity');
- var unitEntry = findChildObject(current, 'units');
- var costEntry = findChildObject(current, 'cost');
- var originEntry = findChildObject(current, 'origin');
- var decafSelection = findChildObject(current, 'isDecaf');
- var decafEntry = findChildObject(current, 'decafMethod');
- if(nameEntry.currentText == '')
- {
- continue;
- }
- if(quantityEntry.text == '')
- {
- continue;
- }
- if(costEntry.text == '')
- {
- continue;
- }
- if(originEntry.currentText == '')
- {
- continue;
- }
- if(decafSelection.checked)
- {
- if(decafEntry.currentText == '')
- {
- continue;
- }
- }
- if(decafSelection.checked)
- {
- q = "INSERT INTO decaf_coffees (id, name, reference, unit, quantity, category, origin, region, producer, grade, milling, drying, decaf_method) VALUES (default, :name, :reference, :unit, 0, 'Coffee: Unroasted', :origin, :region, :producer, :grade, :milling, :drying, :decafMethod) RETURNING id";
- }
- else
- {
- q = "INSERT INTO coffees (id, name, reference, unit, quantity, category, origin, region, producer, grade, milling, drying) VALUES (default, :name, :reference, :unit, 0, 'Coffee: Unroasted', :origin, :region, :producer, :grade, :milling, :drying) RETURNING id";
- }
- query.prepare(q);
- query.bind(":name", nameEntry.currentText);
- var referenceEntry = findChildObject(current, 'reference');
- if(referenceEntry.currentText == '')
- {
- query.bind(":reference", null);
- }
- else
- {
- query.bind(":reference", referenceEntry.currentText);
- }
- query.bind(":unit", 'lb');
- query.bind(":origin", originEntry.currentText);
- var regionEntry = findChildObject(current, 'region');
- if(regionEntry.currentText == '')
- {
- query.bind(":region", null);
- }
- else
- {
- query.bind(":region", regionEntry.currentText);
- }
- var producerEntry = findChildObject(current, 'producer');
- if(producerEntry.currentText == '')
- {
- query.bind(":producer", null);
- }
- else
- {
- query.bind(":producer", producerEntry.currentText);
- }
- var gradeEntry = findChildObject(current, 'grade');
- if(gradeEntry.currentText == '')
- {
- query.bind(":grade", null);
- }
- else
- {
- query.bind(":grade", gradeEntry.currentText);
- }
- var millingEntry = findChildObject(current, 'milling');
- if(millingEntry.currentText == '')
- {
- query.bind(":milling", null);
- }
- else
- {
- query.bind(":milling", millingEntry.currentText);
- }
- var dryingEntry = findChildObject(current, 'drying');
- if(dryingEntry.currentText == '')
- {
- query.bind(":drying", null);
- }
- else
- {
- query.bind(":drying", dryingEntry.currentText);
- }
- if(decafSelection.checked)
- {
- query.bind(":decafMethod", decafEntry.currentText);
- }
- query.exec();
- query.next();
- var item_id = query.value(0);
- q = "INSERT INTO purchase (time, item, quantity, cost, vendor, person) VALUES(:time, :item, :quantity, :cost, :vendor, :user)";
- query.prepare(q);
- query.bind(":time", dateField.date);
- query.bind(":item", item_id);
- query.bind(":quantity", convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText));
- var costModifier = findChildObject(current, 'costModifier');
- if(costModifier.currentText == 'per unit')
- {
- query.bind(":cost", convertToPerPounds(parseFloat(costEntry.text), unitEntry.currentText));
- }
- else
- {
- query.bind(":cost", Number(costEntry.text) / convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText));
- }
- query.bind(":vendor", vendorField.currentText);
- query.bind(":user", Application.currentTypicaUser());
- query.exec();
- q = "INSERT INTO lb_bag_conversion (item, conversion) VALUES(:item, :conversion)";
- query.prepare(q);
- query.bind(":item", item_id);
- var bagsEntry = findChildObject(current, 'bags');
- var conversion = convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText) / Number(bagsEntry.text);
- query.bind(":conversion", conversion);
- query.exec();
- q = "INSERT INTO invoice_items (invoice_id, record_type, item_id, description, cost) VALUES(:id, 'PURCHASE', :item, :description, :cost)";
- query.prepare(q);
- query.bind(":id", invoiceNumber);
- query.bind(":item", item_id);
- query.bind(":description", nameEntry.currentText);
- if(costModifier.currentText == 'per unit')
- {
- query.bind(":cost", Number(costEntry.text) * Number(quantityEntry.text));
- }
- else
- {
- query.bind(":cost", Number(costEntry.text));
- }
- query.exec();
- var certifications = findChildObject(current, 'certdisplay');
- var certlist = certifications.text.split(",");
- q = "INSERT INTO certifications (item, certification) VALUES (:item, :certification)";
- query.prepare(q);
- query.bind(":item", item_id);
- for(var j = 0; j < certlist.length; j++)
- {
- query.bind(":certification", certlist[j]);
- query.exec();
- }
- }
- var descriptionArray = sqlToArray(feesTable.columnArray(0, 0));
- var q = "INSERT INTO invoice_items (invoice_id, record_type, item_id, description, cost) VALUES (:id, 'FEE', NULL, :description, :cost)";
- query.prepare(q);
- query.bind(":id", invoiceNumber);
- if(descriptionArray.length > 0)
- {
- for(var i = 0; i < descriptionArray.length; i++)
- {
- if(feesTable.data(i, 0, 0).value == '')
- {
- continue;
- }
- if(feesTable.data(i, 1, 0).value == '')
- {
- continue;
- }
- query.bind(":description", feesTable.data(i, 0, 0).value);
- query.bind(":cost", Number(feesTable.data(i, 1, 0).value));
- query.exec();
- }
- }
- window.close();
- });
- this.showMaximized();
- ]]>
- </program>
- </window>
|