Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

purchase.xml 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <window id="purchase">
  2. <layout type="vertical">
  3. <layout type="horizontal">
  4. <button name="Add Item" type="push" id="newForm" />
  5. <label>Date:</label>
  6. <calendar id="date"/>
  7. <label>Vendor:</label>
  8. <sqldrop data="0" display="0" showdata="false" editable="true" id="vendor">
  9. <query>SELECT DISTINCT vendor FROM purchase UNION SELECT '' ORDER BY vendor ASC</query>
  10. </sqldrop>
  11. <label>Invoice:</label>
  12. <line id="invoice" />
  13. </layout>
  14. <formarray id="form">
  15. <layout type="grid">
  16. <row>
  17. <column><label>Item:</label></column>
  18. <column>
  19. <sqldrop data="0" display="0" showdata="false" editable="true" id="name">
  20. <query>SELECT DISTINCT name FROM coffees UNION SELECT '' ORDER BY name ASC</query>
  21. </sqldrop>
  22. </column>
  23. <column />
  24. <column><label>Reference:</label></column>
  25. <column>
  26. <sqldrop data="0" display="0" showdata="false" editable="true" id="reference">
  27. <query>SELECT DISTINCT reference FROM coffees UNION SELECT '' ORDER BY reference ASC</query>
  28. </sqldrop>
  29. </column>
  30. </row>
  31. <row>
  32. <column><label>Quantity:</label></column>
  33. <column>
  34. <line validator="numeric" id="quantity" />
  35. </column>
  36. <column>
  37. <sqldrop data="0" display="0" showdata="false" editable="false" id="units">
  38. <query>SELECT * FROM (VALUES('Lb'),('Kg')) AS q (unit)</query>
  39. </sqldrop>
  40. </column>
  41. </row>
  42. <row>
  43. <column><label>Cost:</label></column>
  44. <column>
  45. <line validator="numeric" id="cost" />
  46. </column>
  47. <column>
  48. <sqldrop data="0" display="0" showdata="false" editable="false" id="costModifier">
  49. <query>SELECT 'per unit' UNION SELECT 'total'</query>
  50. </sqldrop>
  51. </column>
  52. </row>
  53. <row>
  54. <column><label>Origin:</label></column>
  55. <column>
  56. <sqldrop data="0" display="0" showdata="false" editable="true" id="origin">
  57. <query>SELECT DISTINCT origin FROM coffees UNION SELECT '' ORDER BY origin ASC</query>
  58. </sqldrop>
  59. </column>
  60. <column />
  61. <column><label>Region:</label></column>
  62. <column>
  63. <sqldrop data="0" display="0" showdata="false" editable="true" id="region">
  64. <query>SELECT DISTINCT region FROM coffees UNION SELECT '' ORDER BY region ASC</query>
  65. </sqldrop>
  66. </column>
  67. </row>
  68. <row>
  69. <column><label>Producer</label></column>
  70. <column>
  71. <sqldrop data="0" display="0" showdata="false" editable="true" id="producer">
  72. <query>SELECT DISTINCT producer FROM coffees UNION SELECT '' ORDER BY producer ASC</query>
  73. </sqldrop>
  74. </column>
  75. <column />
  76. <column><label>Grade:</label></column>
  77. <column>
  78. <sqldrop data="0" display="0" showdata="false" editable="true" id="grade">
  79. <query>SELECT DISTINCT grade FROM coffees UNION SELECT '' ORDER BY grade ASC</query>
  80. </sqldrop>
  81. </column>
  82. </row>
  83. <row>
  84. <column><label>Milling:</label></column>
  85. <column>
  86. <sqldrop data="0" display="0" showdata="false" editable="true" id="milling">
  87. <query>SELECT DISTINCT milling FROM coffees UNION SELECT '' ORDER BY milling ASC</query>
  88. </sqldrop>
  89. </column>
  90. <column />
  91. <column><label>Drying:</label></column>
  92. <column>
  93. <sqldrop data="0" display="0" showdata="false" editable="true" id="drying">
  94. <query>SELECT DISTINCT drying FROM coffees UNION SELECT '' ORDER BY drying ASC</query>
  95. </sqldrop>
  96. </column>
  97. </row>
  98. <row>
  99. <column><label>Bags:</label></column>
  100. <column>
  101. <line validator="numeric" id="bags" />
  102. </column>
  103. <column>
  104. <button type="check" id="isDecaf" name="Decaffeinated" />
  105. </column>
  106. <column><label>by method:</label></column>
  107. <column>
  108. <sqldrop data="0" display="0" showdata="false" editable="true" id="decafMethod">
  109. <query>SELECT DISTINCT decaf_method FROM decaf_coffees UNION SELECT '' ORDER BY decaf_method ASC</query>
  110. </sqldrop>
  111. </column>
  112. </row>
  113. <row>
  114. <column colspan="2">
  115. <line id="certdisplay" writable="false" />
  116. </column>
  117. <column column="2">
  118. <button type="push" name="Add Certification" id="addcert" />
  119. </column>
  120. <column colspan="2">
  121. <line id="certification" />
  122. </column>
  123. </row>
  124. </layout>
  125. </formarray>
  126. <layout type="horizontal">
  127. <layout type="vertical">
  128. <label>Fees:</label>
  129. <sqltablearray columns="2" id="fees">
  130. <column name="Fee Description" />
  131. <column name="Amount" />
  132. </sqltablearray>
  133. </layout>
  134. <button name="Submit" type="push" id="submit" />
  135. </layout>
  136. </layout>
  137. <program>
  138. <![CDATA[
  139. var window = this;
  140. var convertToPounds = function(w, u) {
  141. switch(u) {
  142. case "g":
  143. return w * 0.0022;
  144. case "oz":
  145. return w * 0.0625;
  146. case "Kg":
  147. return w * 2.2;
  148. }
  149. return w;
  150. };
  151. var convertToPerPounds = function(w, u) {
  152. switch(u) {
  153. case "g":
  154. return w / 0.0022;
  155. case "oz":
  156. return w / 0.0625;
  157. case "Kg":
  158. return w / 2.2;
  159. }
  160. return w;
  161. };
  162. this.windowTitle = 'Typica - Coffee Purchase';
  163. var form = findChildObject(this, 'form');
  164. form.setMaximumElementHeight(320);
  165. var appendForm = function() {
  166. form.addElements(1);
  167. var thisCoffee = form.elementAt(form.elements() - 1);
  168. var decafButton = findChildObject(thisCoffee, 'isDecaf');
  169. var methodField = findChildObject(thisCoffee, 'decafMethod');
  170. methodField.enabled = false;
  171. decafButton.stateChanged.connect(function(state) {
  172. if(state == 0)
  173. {
  174. methodField.enabled = false;
  175. }
  176. else
  177. {
  178. methodField.enabled = true;
  179. }
  180. });
  181. var certificationButton = findChildObject(thisCoffee, 'addcert');
  182. var certSource = findChildObject(thisCoffee, 'certification');
  183. var certTarget = findChildObject(thisCoffee, 'certdisplay');
  184. certificationButton.clicked.connect(function() {
  185. if(thisCoffee.certificationArray === undefined)
  186. {
  187. thisCoffee.certificationArray = new Array();
  188. }
  189. thisCoffee.certificationArray.push(certSource.text);
  190. certTarget.text = thisCoffee.certificationArray.toString();
  191. certSource.text = '';
  192. });
  193. };
  194. appendForm();
  195. var itemButton = findChildObject(this, 'newForm');
  196. itemButton.clicked.connect(function() {
  197. appendForm();
  198. });
  199. var invoiceField = findChildObject(this, 'invoice');
  200. var vendorField = findChildObject(this, 'vendor');
  201. var feesTable = findChildObject(this, 'fees');
  202. var submitButton = findChildObject(this, 'submit');
  203. var dateField = findChildObject(this, 'date');
  204. submitButton.clicked.connect(function() {
  205. var query = new QSqlQuery;
  206. var q = "INSERT INTO invoices VALUES (default, :invoice, :vendor, :date) RETURNING id";
  207. query.prepare(q);
  208. query.bind(":invoice", invoiceField.text);
  209. query.bind(":vendor", vendorField.currentText);
  210. query.bind(":date", dateField.date);
  211. query.exec();
  212. query.next();
  213. var invoiceNumber = query.value(0);
  214. for(var i = 0; i < form.elements(); i++)
  215. {
  216. var current = form.elementAt(i);
  217. var nameEntry = findChildObject(current, 'name');
  218. var quantityEntry = findChildObject(current, 'quantity');
  219. var unitEntry = findChildObject(current, 'units');
  220. var costEntry = findChildObject(current, 'cost');
  221. var originEntry = findChildObject(current, 'origin');
  222. var decafSelection = findChildObject(current, 'isDecaf');
  223. var decafEntry = findChildObject(current, 'decafMethod');
  224. if(nameEntry.currentText == '')
  225. {
  226. continue;
  227. }
  228. if(quantityEntry.text == '')
  229. {
  230. continue;
  231. }
  232. if(costEntry.text == '')
  233. {
  234. continue;
  235. }
  236. if(originEntry.currentText == '')
  237. {
  238. continue;
  239. }
  240. if(decafSelection.checked)
  241. {
  242. if(decafEntry.currentText == '')
  243. {
  244. continue;
  245. }
  246. }
  247. if(decafSelection.checked)
  248. {
  249. q = "INSERT INTO decaf_coffees VALUES (default, :name, :reference, :unit, 0, 'Coffee: Unroasted', :origin, :region, :producer, :grade, :milling, :drying, :decafMethod) RETURNING id";
  250. }
  251. else
  252. {
  253. q = "INSERT INTO coffees VALUES (default, :name, :reference, :unit, 0, 'Coffee: Unroasted', :origin, :region, :producer, :grade, :milling, :drying) RETURNING id";
  254. }
  255. query.prepare(q);
  256. query.bind(":name", nameEntry.currentText);
  257. var referenceEntry = findChildObject(current, 'reference');
  258. if(referenceEntry.currentText == '')
  259. {
  260. query.bind(":reference", null);
  261. }
  262. else
  263. {
  264. query.bind(":reference", referenceEntry.currentText);
  265. }
  266. query.bind(":unit", 'lb');
  267. query.bind(":origin", originEntry.currentText);
  268. var regionEntry = findChildObject(current, 'region');
  269. if(regionEntry.currentText == '')
  270. {
  271. query.bind(":region", null);
  272. }
  273. else
  274. {
  275. query.bind(":region", regionEntry.currentText);
  276. }
  277. var producerEntry = findChildObject(current, 'producer');
  278. if(producerEntry.currentText == '')
  279. {
  280. query.bind(":producer", null);
  281. }
  282. else
  283. {
  284. query.bind(":producer", producerEntry.currentText);
  285. }
  286. var gradeEntry = findChildObject(current, 'grade');
  287. if(gradeEntry.currentText == '')
  288. {
  289. query.bind(":grade", null);
  290. }
  291. else
  292. {
  293. query.bind(":grade", gradeEntry.currentText);
  294. }
  295. var millingEntry = findChildObject(current, 'milling');
  296. if(millingEntry.currentText == '')
  297. {
  298. query.bind(":milling", null);
  299. }
  300. else
  301. {
  302. query.bind(":milling", millingEntry.currentText);
  303. }
  304. var dryingEntry = findChildObject(current, 'drying');
  305. if(dryingEntry.currentText == '')
  306. {
  307. query.bind(":drying", null);
  308. }
  309. else
  310. {
  311. query.bind(":drying", dryingEntry.currentText);
  312. }
  313. if(decafSelection.checked)
  314. {
  315. query.bind(":decafMethod", decafEntry.currentText);
  316. }
  317. query.exec();
  318. query.next();
  319. var item_id = query.value(0);
  320. q = "INSERT INTO purchase VALUES(:time, :item, :quantity, :cost, :vendor)";
  321. query.prepare(q);
  322. query.bind(":time", dateField.date);
  323. query.bind(":item", item_id);
  324. query.bind(":quantity", convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText));
  325. var costModifier = findChildObject(current, 'costModifier');
  326. if(costModifier.currentText == 'per unit')
  327. {
  328. query.bind(":cost", convertToPerPounds(parseFloat(costEntry.text), unitEntry.currentText));
  329. }
  330. else
  331. {
  332. query.bind(":cost", Number(costEntry.text) / convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText));
  333. }
  334. query.bind(":vendor", vendorField.currentText);
  335. query.exec();
  336. q = "INSERT INTO lb_bag_conversion VALUES(:item, :conversion)";
  337. query.prepare(q);
  338. query.bind(":item", item_id);
  339. var bagsEntry = findChildObject(current, 'bags');
  340. var conversion = convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText) / Number(bagsEntry.text);
  341. query.bind(":conversion", conversion);
  342. query.exec();
  343. q = "INSERT INTO invoice_items VALUES(:id, 'PURCHASE', :item, :description, :cost)";
  344. query.prepare(q);
  345. query.bind(":id", invoiceNumber);
  346. query.bind(":item", item_id);
  347. query.bind(":description", nameEntry.currentText);
  348. if(costModifier.currentText == 'per unit')
  349. {
  350. query.bind(":cost", Number(costEntry.text) * Number(quantityEntry.text));
  351. }
  352. else
  353. {
  354. query.bind(":cost", Number(costEntry.text));
  355. }
  356. query.exec();
  357. var certifications = findChildObject(current, 'certdisplay');
  358. var certlist = certifications.text.split(",");
  359. q = "INSERT INTO certifications VALUES (:item, :certification)";
  360. query.prepare(q);
  361. query.bind(":item", item_id);
  362. for(var j = 0; j < certlist.length; j++)
  363. {
  364. query.bind(":certification", certlist[j]);
  365. query.exec();
  366. }
  367. }
  368. var descriptionArray = sqlToArray(feesTable.columnArray(0, 0));
  369. var q = "INSERT INTO invoice_items VALUES (:id, 'FEE', NULL, :description, :cost)";
  370. query.prepare(q);
  371. query.bind(":id", invoiceNumber);
  372. if(descriptionArray.length > 0)
  373. {
  374. for(var i = 0; i < descriptionArray.length; i++)
  375. {
  376. if(feesTable.data(i, 0, 0).value == '')
  377. {
  378. continue;
  379. }
  380. if(feesTable.data(i, 1, 0).value == '')
  381. {
  382. continue;
  383. }
  384. query.bind(":description", feesTable.data(i, 0, 0).value);
  385. query.bind(":cost", Number(feesTable.data(i, 1, 0).value));
  386. query.exec();
  387. }
  388. }
  389. window.close();
  390. });
  391. this.showMaximized();
  392. ]]>
  393. </program>
  394. </window>