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 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. var validate = function() {
  205. if(vendorField.currentText == '') {
  206. displayError(TTR("purchase", "Missing Input"),
  207. TTR("purchase", "Vendor is a required field."));
  208. return false;
  209. }
  210. for(var i = 0; i < form.elements(); i++) {
  211. if(findChildObject(form.elementAt(i), 'name').text == '') {
  212. displayError(TTR("purchase", "Missing Input"),
  213. TTR("purchase", "Item is a required field."));
  214. return false;
  215. }
  216. if(findChildObject(form.elementAt(i), 'quantity').text == '') {
  217. displayError(TTR("purchase", "Missing Input"),
  218. TTR("purchase", "Quantity is a required field."));
  219. return false;
  220. }
  221. if(findChildObject(form.elementAt(i), 'cost').text == '') {
  222. displayError(TTR("purchase", "Missing Input"),
  223. TTR("purchase", "Cost is a required field."));
  224. return false;
  225. }
  226. if(findChildObject(form.elementAt(i), 'origin').currentText == '') {
  227. displayError(TTR("purchase", "Missing Input"),
  228. TTR("purchase", "Origin is a required field."));
  229. return false;
  230. }
  231. if(findChildObject(form.elementAt(i), 'bags').text == '') {
  232. displayError(TTR("purchase", "Missing Input"),
  233. TTR("purchase", "Bags is a required field."));
  234. return false;
  235. }
  236. if(findChildObject(form.elementAt(i), 'isDecaf').checked) {
  237. if(findChildObject(form.elementAt(i), 'decafEntry').currentText) {
  238. displayError(TTR("purchase", "Missing Input"),
  239. TTR("purchase", "Decaffeination method is a required field for decaffeinated coffees."));
  240. return false;
  241. }
  242. }
  243. }
  244. return true;
  245. };
  246. submitButton.clicked.connect(function() {
  247. if(!validate()) {
  248. return;
  249. }
  250. var query = new QSqlQuery;
  251. var q = "INSERT INTO invoices (id, invoice, vendor, time) VALUES (default, :invoice, :vendor, :date) RETURNING id";
  252. query.prepare(q);
  253. query.bind(":invoice", invoiceField.text);
  254. query.bind(":vendor", vendorField.currentText);
  255. query.bind(":date", dateField.date);
  256. query.exec();
  257. query.next();
  258. var invoiceNumber = query.value(0);
  259. for(var i = 0; i < form.elements(); i++)
  260. {
  261. var current = form.elementAt(i);
  262. var nameEntry = findChildObject(current, 'name');
  263. var quantityEntry = findChildObject(current, 'quantity');
  264. var unitEntry = findChildObject(current, 'units');
  265. var costEntry = findChildObject(current, 'cost');
  266. var originEntry = findChildObject(current, 'origin');
  267. var decafSelection = findChildObject(current, 'isDecaf');
  268. var decafEntry = findChildObject(current, 'decafMethod');
  269. if(nameEntry.currentText == '')
  270. {
  271. continue;
  272. }
  273. if(quantityEntry.text == '')
  274. {
  275. continue;
  276. }
  277. if(costEntry.text == '')
  278. {
  279. continue;
  280. }
  281. if(originEntry.currentText == '')
  282. {
  283. continue;
  284. }
  285. if(decafSelection.checked)
  286. {
  287. if(decafEntry.currentText == '')
  288. {
  289. continue;
  290. }
  291. }
  292. if(decafSelection.checked)
  293. {
  294. 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";
  295. }
  296. else
  297. {
  298. 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";
  299. }
  300. query.prepare(q);
  301. query.bind(":name", nameEntry.currentText);
  302. var referenceEntry = findChildObject(current, 'reference');
  303. if(referenceEntry.currentText == '')
  304. {
  305. query.bind(":reference", null);
  306. }
  307. else
  308. {
  309. query.bind(":reference", referenceEntry.currentText);
  310. }
  311. query.bind(":unit", 'lb');
  312. query.bind(":origin", originEntry.currentText);
  313. var regionEntry = findChildObject(current, 'region');
  314. if(regionEntry.currentText == '')
  315. {
  316. query.bind(":region", null);
  317. }
  318. else
  319. {
  320. query.bind(":region", regionEntry.currentText);
  321. }
  322. var producerEntry = findChildObject(current, 'producer');
  323. if(producerEntry.currentText == '')
  324. {
  325. query.bind(":producer", null);
  326. }
  327. else
  328. {
  329. query.bind(":producer", producerEntry.currentText);
  330. }
  331. var gradeEntry = findChildObject(current, 'grade');
  332. if(gradeEntry.currentText == '')
  333. {
  334. query.bind(":grade", null);
  335. }
  336. else
  337. {
  338. query.bind(":grade", gradeEntry.currentText);
  339. }
  340. var millingEntry = findChildObject(current, 'milling');
  341. if(millingEntry.currentText == '')
  342. {
  343. query.bind(":milling", null);
  344. }
  345. else
  346. {
  347. query.bind(":milling", millingEntry.currentText);
  348. }
  349. var dryingEntry = findChildObject(current, 'drying');
  350. if(dryingEntry.currentText == '')
  351. {
  352. query.bind(":drying", null);
  353. }
  354. else
  355. {
  356. query.bind(":drying", dryingEntry.currentText);
  357. }
  358. if(decafSelection.checked)
  359. {
  360. query.bind(":decafMethod", decafEntry.currentText);
  361. }
  362. query.exec();
  363. query.next();
  364. var item_id = query.value(0);
  365. q = "INSERT INTO purchase (time, item, quantity, cost, vendor, person) VALUES(:time, :item, :quantity, :cost, :vendor, :user)";
  366. query.prepare(q);
  367. query.bind(":time", dateField.date);
  368. query.bind(":item", item_id);
  369. query.bind(":quantity", convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText));
  370. var costModifier = findChildObject(current, 'costModifier');
  371. if(costModifier.currentText == 'per unit')
  372. {
  373. query.bind(":cost", convertToPerPounds(parseFloat(costEntry.text), unitEntry.currentText));
  374. }
  375. else
  376. {
  377. query.bind(":cost", Number(costEntry.text) / convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText));
  378. }
  379. query.bind(":vendor", vendorField.currentText);
  380. query.bind(":user", Application.currentTypicaUser());
  381. query.exec();
  382. q = "INSERT INTO lb_bag_conversion (item, conversion) VALUES(:item, :conversion)";
  383. query.prepare(q);
  384. query.bind(":item", item_id);
  385. var bagsEntry = findChildObject(current, 'bags');
  386. var conversion = convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText) / Number(bagsEntry.text);
  387. query.bind(":conversion", conversion);
  388. query.exec();
  389. q = "INSERT INTO invoice_items (invoice_id, record_type, item_id, description, cost) VALUES(:id, 'PURCHASE', :item, :description, :cost)";
  390. query.prepare(q);
  391. query.bind(":id", invoiceNumber);
  392. query.bind(":item", item_id);
  393. query.bind(":description", nameEntry.currentText);
  394. if(costModifier.currentText == 'per unit')
  395. {
  396. query.bind(":cost", Number(costEntry.text) * Number(quantityEntry.text));
  397. }
  398. else
  399. {
  400. query.bind(":cost", Number(costEntry.text));
  401. }
  402. query.exec();
  403. var certifications = findChildObject(current, 'certdisplay');
  404. var certlist = certifications.text.split(",");
  405. q = "INSERT INTO certifications (item, certification) VALUES (:item, :certification)";
  406. query.prepare(q);
  407. query.bind(":item", item_id);
  408. for(var j = 0; j < certlist.length; j++)
  409. {
  410. query.bind(":certification", certlist[j]);
  411. query.exec();
  412. }
  413. }
  414. var descriptionArray = sqlToArray(feesTable.columnArray(0, 0));
  415. var q = "INSERT INTO invoice_items (invoice_id, record_type, item_id, description, cost) VALUES (:id, 'FEE', NULL, :description, :cost)";
  416. query.prepare(q);
  417. query.bind(":id", invoiceNumber);
  418. if(descriptionArray.length > 0)
  419. {
  420. for(var i = 0; i < descriptionArray.length; i++)
  421. {
  422. if(feesTable.data(i, 0, 0).value == '')
  423. {
  424. continue;
  425. }
  426. if(feesTable.data(i, 1, 0).value == '')
  427. {
  428. continue;
  429. }
  430. query.bind(":description", feesTable.data(i, 0, 0).value);
  431. query.bind(":cost", Number(feesTable.data(i, 1, 0).value));
  432. query.exec();
  433. }
  434. }
  435. window.close();
  436. });
  437. this.showMaximized();
  438. ]]>
  439. </program>
  440. </window>