Typica is a free program for professional coffee roasters. https://typica.us
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

purchase.xml 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. <sqldrop data="0" display="0" showdata="false" editable="true" id="certification">
  122. <query>SELECT DISTINCT certification FROM certifications ORDER BY certification ASC</query>
  123. </sqldrop>
  124. </column>
  125. </row>
  126. </layout>
  127. </formarray>
  128. <layout type="horizontal">
  129. <layout type="vertical">
  130. <label>Fees:</label>
  131. <sqltablearray columns="2" id="fees">
  132. <column name="Fee Description" />
  133. <column name="Amount" />
  134. </sqltablearray>
  135. </layout>
  136. <button name="Submit" type="push" id="submit" />
  137. </layout>
  138. </layout>
  139. <program>
  140. <![CDATA[
  141. var window = this;
  142. var convertToPounds = function(w, u) {
  143. switch(u) {
  144. case "g":
  145. return w * 0.0022;
  146. case "oz":
  147. return w * 0.0625;
  148. case "Kg":
  149. return w * 2.2;
  150. }
  151. return w;
  152. };
  153. var convertToPerPounds = function(w, u) {
  154. switch(u) {
  155. case "g":
  156. return w / 0.0022;
  157. case "oz":
  158. return w / 0.0625;
  159. case "Kg":
  160. return w / 2.2;
  161. }
  162. return w;
  163. };
  164. this.windowTitle = TTR("purchase", "Typica - Coffee Purchase");
  165. var form = findChildObject(this, 'form');
  166. form.setMaximumElementHeight(320);
  167. var appendForm = function() {
  168. form.addElements(1);
  169. var thisCoffee = form.elementAt(form.elements() - 1);
  170. var decafButton = findChildObject(thisCoffee, 'isDecaf');
  171. var methodField = findChildObject(thisCoffee, 'decafMethod');
  172. methodField.enabled = false;
  173. decafButton.stateChanged.connect(function(state) {
  174. if(state == 0)
  175. {
  176. methodField.enabled = false;
  177. }
  178. else
  179. {
  180. methodField.enabled = true;
  181. }
  182. });
  183. var certificationButton = findChildObject(thisCoffee, 'addcert');
  184. var certSource = findChildObject(thisCoffee, 'certification');
  185. var certTarget = findChildObject(thisCoffee, 'certdisplay');
  186. certificationButton.clicked.connect(function() {
  187. if(thisCoffee.certificationArray === undefined)
  188. {
  189. thisCoffee.certificationArray = new Array();
  190. }
  191. thisCoffee.certificationArray.push(certSource.currentText);
  192. certTarget.text = thisCoffee.certificationArray.toString();
  193. certSource.currentIndex = -1;
  194. });
  195. };
  196. appendForm();
  197. var itemButton = findChildObject(this, 'newForm');
  198. itemButton.clicked.connect(function() {
  199. appendForm();
  200. });
  201. var invoiceField = findChildObject(this, 'invoice');
  202. var vendorField = findChildObject(this, 'vendor');
  203. var feesTable = findChildObject(this, 'fees');
  204. var submitButton = findChildObject(this, 'submit');
  205. var dateField = findChildObject(this, 'date');
  206. var validate = function() {
  207. if(vendorField.currentText == '') {
  208. displayError(TTR("purchase", "Missing Input"),
  209. TTR("purchase", "Vendor is a required field."));
  210. return false;
  211. }
  212. for(var i = 0; i < form.elements(); i++) {
  213. if(findChildObject(form.elementAt(i), 'name').text == '') {
  214. displayError(TTR("purchase", "Missing Input"),
  215. TTR("purchase", "Item is a required field."));
  216. return false;
  217. }
  218. if(findChildObject(form.elementAt(i), 'quantity').text == '') {
  219. displayError(TTR("purchase", "Missing Input"),
  220. TTR("purchase", "Quantity is a required field."));
  221. return false;
  222. }
  223. if(findChildObject(form.elementAt(i), 'cost').text == '') {
  224. displayError(TTR("purchase", "Missing Input"),
  225. TTR("purchase", "Cost is a required field."));
  226. return false;
  227. }
  228. if(findChildObject(form.elementAt(i), 'origin').currentText == '') {
  229. displayError(TTR("purchase", "Missing Input"),
  230. TTR("purchase", "Origin is a required field."));
  231. return false;
  232. }
  233. if(findChildObject(form.elementAt(i), 'bags').text == '') {
  234. displayError(TTR("purchase", "Missing Input"),
  235. TTR("purchase", "Bags is a required field."));
  236. return false;
  237. }
  238. if(findChildObject(form.elementAt(i), 'isDecaf').checked) {
  239. if(findChildObject(form.elementAt(i), 'decafMethod').text == '') {
  240. displayError(TTR("purchase", "Missing Input"),
  241. TTR("purchase", "Decaffeination method is a required field for decaffeinated coffees."));
  242. return false;
  243. }
  244. }
  245. }
  246. return true;
  247. };
  248. submitButton.clicked.connect(function() {
  249. if(!validate()) {
  250. return;
  251. }
  252. var query = new QSqlQuery;
  253. var q = "INSERT INTO invoices (id, invoice, vendor, time) VALUES (default, :invoice, :vendor, :date) RETURNING id";
  254. query.prepare(q);
  255. query.bind(":invoice", invoiceField.text);
  256. query.bind(":vendor", vendorField.currentText);
  257. query.bind(":date", dateField.date);
  258. query.exec();
  259. query.next();
  260. var invoiceNumber = query.value(0);
  261. for(var i = 0; i < form.elements(); i++)
  262. {
  263. var current = form.elementAt(i);
  264. var nameEntry = findChildObject(current, 'name');
  265. var quantityEntry = findChildObject(current, 'quantity');
  266. var unitEntry = findChildObject(current, 'units');
  267. var costEntry = findChildObject(current, 'cost');
  268. var originEntry = findChildObject(current, 'origin');
  269. var decafSelection = findChildObject(current, 'isDecaf');
  270. var decafEntry = findChildObject(current, 'decafMethod');
  271. if(nameEntry.currentText == '')
  272. {
  273. continue;
  274. }
  275. if(quantityEntry.text == '')
  276. {
  277. continue;
  278. }
  279. if(costEntry.text == '')
  280. {
  281. continue;
  282. }
  283. if(originEntry.currentText == '')
  284. {
  285. continue;
  286. }
  287. if(decafSelection.checked)
  288. {
  289. if(decafEntry.currentText == '')
  290. {
  291. continue;
  292. }
  293. }
  294. if(decafSelection.checked)
  295. {
  296. 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";
  297. }
  298. else
  299. {
  300. 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";
  301. }
  302. query.prepare(q);
  303. query.bind(":name", nameEntry.currentText);
  304. var referenceEntry = findChildObject(current, 'reference');
  305. if(referenceEntry.currentText == '')
  306. {
  307. query.bind(":reference", null);
  308. }
  309. else
  310. {
  311. query.bind(":reference", referenceEntry.currentText);
  312. }
  313. query.bind(":unit", 'lb');
  314. query.bind(":origin", originEntry.currentText);
  315. var regionEntry = findChildObject(current, 'region');
  316. if(regionEntry.currentText == '')
  317. {
  318. query.bind(":region", null);
  319. }
  320. else
  321. {
  322. query.bind(":region", regionEntry.currentText);
  323. }
  324. var producerEntry = findChildObject(current, 'producer');
  325. if(producerEntry.currentText == '')
  326. {
  327. query.bind(":producer", null);
  328. }
  329. else
  330. {
  331. query.bind(":producer", producerEntry.currentText);
  332. }
  333. var gradeEntry = findChildObject(current, 'grade');
  334. if(gradeEntry.currentText == '')
  335. {
  336. query.bind(":grade", null);
  337. }
  338. else
  339. {
  340. query.bind(":grade", gradeEntry.currentText);
  341. }
  342. var millingEntry = findChildObject(current, 'milling');
  343. if(millingEntry.currentText == '')
  344. {
  345. query.bind(":milling", null);
  346. }
  347. else
  348. {
  349. query.bind(":milling", millingEntry.currentText);
  350. }
  351. var dryingEntry = findChildObject(current, 'drying');
  352. if(dryingEntry.currentText == '')
  353. {
  354. query.bind(":drying", null);
  355. }
  356. else
  357. {
  358. query.bind(":drying", dryingEntry.currentText);
  359. }
  360. if(decafSelection.checked)
  361. {
  362. query.bind(":decafMethod", decafEntry.currentText);
  363. }
  364. query.exec();
  365. query.next();
  366. var item_id = query.value(0);
  367. q = "INSERT INTO purchase (time, item, quantity, cost, vendor, person) VALUES(:time, :item, :quantity, :cost, :vendor, :user)";
  368. query.prepare(q);
  369. query.bind(":time", dateField.date);
  370. query.bind(":item", item_id);
  371. query.bind(":quantity", convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText));
  372. var costModifier = findChildObject(current, 'costModifier');
  373. if(costModifier.currentText == 'per unit')
  374. {
  375. query.bind(":cost", convertToPerPounds(parseFloat(costEntry.text), unitEntry.currentText));
  376. }
  377. else
  378. {
  379. query.bind(":cost", Number(costEntry.text) / convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText));
  380. }
  381. query.bind(":vendor", vendorField.currentText);
  382. query.bind(":user", Application.currentTypicaUser());
  383. query.exec();
  384. q = "INSERT INTO lb_bag_conversion (item, conversion) VALUES(:item, :conversion)";
  385. query.prepare(q);
  386. query.bind(":item", item_id);
  387. var bagsEntry = findChildObject(current, 'bags');
  388. var conversion = convertToPounds(parseFloat(quantityEntry.text), unitEntry.currentText) / Number(bagsEntry.text);
  389. query.bind(":conversion", conversion);
  390. query.exec();
  391. q = "INSERT INTO invoice_items (invoice_id, record_type, item_id, description, cost) VALUES(:id, 'PURCHASE', :item, :description, :cost)";
  392. query.prepare(q);
  393. query.bind(":id", invoiceNumber);
  394. query.bind(":item", item_id);
  395. query.bind(":description", nameEntry.currentText);
  396. if(costModifier.currentText == 'per unit')
  397. {
  398. query.bind(":cost", Number(costEntry.text) * Number(quantityEntry.text));
  399. }
  400. else
  401. {
  402. query.bind(":cost", Number(costEntry.text));
  403. }
  404. query.exec();
  405. var certifications = findChildObject(current, 'certdisplay');
  406. var certlist = certifications.text.split(",");
  407. q = "INSERT INTO certifications (item, certification) VALUES (:item, :certification)";
  408. query.prepare(q);
  409. query.bind(":item", item_id);
  410. for(var j = 0; j < certlist.length; j++)
  411. {
  412. query.bind(":certification", certlist[j]);
  413. query.exec();
  414. }
  415. }
  416. var descriptionArray = sqlToArray(feesTable.columnArray(0, 0));
  417. var q = "INSERT INTO invoice_items (invoice_id, record_type, item_id, description, cost) VALUES (:id, 'FEE', NULL, :description, :cost)";
  418. query.prepare(q);
  419. query.bind(":id", invoiceNumber);
  420. if(descriptionArray.length > 0)
  421. {
  422. for(var i = 0; i < descriptionArray.length; i++)
  423. {
  424. if(feesTable.data(i, 0, 0).value == '')
  425. {
  426. continue;
  427. }
  428. if(feesTable.data(i, 1, 0).value == '')
  429. {
  430. continue;
  431. }
  432. query.bind(":description", feesTable.data(i, 0, 0).value);
  433. query.bind(":cost", Number(feesTable.data(i, 1, 0).value));
  434. query.exec();
  435. }
  436. }
  437. window.close();
  438. });
  439. this.showMaximized();
  440. ]]>
  441. </program>
  442. </window>