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.

299 lines
11 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. © 2020 Le Filament (<http://www.le-filament.com>)
  3. License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. */
  5. odoo.define('vracoop_pos_free_balance_v2.models_and_db_balance', function (require) {
  6. "use strict";
  7. var PosDB = require('point_of_sale.DB');
  8. var models = require('point_of_sale.models');
  9. var rpc = require('web.rpc');
  10. var config = require('web.config');
  11. var config = require('web.config');
  12. var core = require('web.core');
  13. var QWeb = core.qweb;
  14. var utils = require('web.utils');
  15. var field_utils = require('web.field_utils');
  16. var round_di = utils.round_decimals;
  17. var round_pr = utils.round_precision;
  18. // include not available => extend
  19. models.PosModel = models.PosModel.extend({
  20. scan_container_check: function(parsed_code){
  21. var transactions = this.db.get_transactions_sorted(1000);
  22. var container = this.db.get_container_by_barcode(
  23. parsed_code.base_code);
  24. var selected_order = this.get_order();
  25. selected_order.add_container(container);
  26. var today = new Date();
  27. var date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
  28. // var isostring = today.toISOString();
  29. // var localestring = today.toLocaleString();
  30. // var dateUTC = today.toUTCString();
  31. var min = today.getMinutes();
  32. if (min < 10) {
  33. min = "0" + today.getMinutes();
  34. }
  35. var hour = today.getHours()-1;
  36. if (hour < 10) {
  37. hour = "0" + hour;
  38. }
  39. var time = (hour) + ":" + min + ":" + today.getSeconds();
  40. var date_time = date + ' ' + time;
  41. for(var i = 0, len = transactions.length; i < len; i++) {
  42. var transaction = transactions[i];
  43. if (
  44. (transaction.container_ean13 == parsed_code.base_code) &&
  45. transaction.write_date > date_time ) {
  46. return transaction;
  47. }
  48. }
  49. return false;
  50. },
  51. // saves the transaction locally and try to send it to the backend.
  52. // it returns a deferred that succeeds after having tried to send
  53. // the container and all the other pending containers.
  54. push_transaction: function(transaction, opts) {
  55. opts = opts || {};
  56. var self = this;
  57. if(transaction){
  58. this.db.add_transactions([transaction]);
  59. }
  60. var pushed = new $.Deferred();
  61. this.flush_mutex.exec(function(){
  62. var flushed = self._save_transactions_to_server(self.db.get_transactions_sorted(), opts);
  63. flushed.always(function(ids){
  64. pushed.resolve();
  65. });
  66. return flushed;
  67. });
  68. return pushed;
  69. },
  70. // send an array of containers to the server
  71. // available options:
  72. // - timeout: timeout for the rpc call in ms
  73. // returns a deferred that resolves with the list of
  74. // server generated ids for the sent containers
  75. _save_transactions_to_server: function (transactions, options) {
  76. var self = this;
  77. var transactions= transactions.filter(transaction => !( "id" in transaction))
  78. if (!transactions || !transactions.length) {
  79. var result = $.Deferred();
  80. result.resolve([]);
  81. return result;
  82. }
  83. options = options || {};
  84. var timeout = typeof options.timeout === 'number' ? options.timeout : 7500 * transactions.length;
  85. return rpc.query({
  86. model: 'pos.transaction',
  87. method: 'create_from_ui',
  88. args: [transactions],
  89. }, {
  90. timeout: timeout,
  91. })
  92. .then(function (server_ids) {
  93. _.each(transactions, function(transaction, key){
  94. transaction["id"] = server_ids[key]
  95. });
  96. self.set('failed',false);
  97. return server_ids;
  98. }).fail(function (type, error){
  99. if(error.code === 200 ){ // Business Logic Error, not a connection problem
  100. //if warning do not need to display traceback!!
  101. if (error.data.exception_type == 'warning') {
  102. delete error.data.debug;
  103. }
  104. // Hide error if already shown before ...
  105. if ((!self.get('failed') || options.show_error) && !options.to_invoice) {
  106. self.gui.show_popup('error-traceback',{
  107. 'title': error.data.message,
  108. 'body': error.data.debug
  109. });
  110. }
  111. self.set('failed',error);
  112. }
  113. console.error('Failed to send transactions:', transactions);
  114. });
  115. },
  116. // returns the header text from config
  117. get_name_header: function(){
  118. if (this.config.explication_header) {
  119. return this.config.explication_header;
  120. }
  121. },
  122. // returns the header text from config
  123. get_balance_id: function(){
  124. if (this.config.balance_id) {
  125. return this.config.balance_id;
  126. }
  127. },
  128. get_is_comptoir: function(){
  129. if (this.config.is_comptoir) {
  130. return this.config.is_comptoir;
  131. }
  132. },
  133. // load_placeholder_transactions: function(){
  134. // var self = this;
  135. // var fields = _.find(this.models,function(model){
  136. // return model.model === 'pos.transaction';
  137. // }).fields;
  138. // var domain = [['barcode', '=', 'CONTAINER']];
  139. // // no need to load it when active because it is already done in standard
  140. // return rpc.query({
  141. // model: 'product.product',
  142. // method: 'search_read',
  143. // args: [domain, fields],
  144. // }).then(function(products){
  145. // self.db.add_products(_.map(products, function (product) {
  146. // return new models.Product({}, product);
  147. // }));
  148. // });
  149. // },
  150. });
  151. PosDB.include({
  152. init: function(parent, options) {
  153. this._super(parent, options);
  154. this.transaction_sorted = [];
  155. this.transaction_by_barcode = {};
  156. this.transaction_by_id = {};
  157. this.transaction_write_date = null;
  158. this.header_text = "";
  159. },
  160. // returns the header text from config
  161. get_name_header: function(){
  162. return this.db.get_name_header();
  163. },
  164. add_transactions: function(transactions) {
  165. var updated_count = 0;
  166. var new_write_date = '';
  167. for(var i = 0, len = transactions.length; i < len; i++) {
  168. var transaction = transactions[i];
  169. if (this.transaction_write_date &&
  170. new Date(this.transaction_write_date).getTime() + 1000 >=
  171. new Date(transaction.write_date).getTime() ) {
  172. continue;
  173. } else if ( new_write_date < transaction.write_date ) {
  174. new_write_date = transaction.write_date;
  175. }
  176. if (!this.transaction_by_barcode[transaction.container_ean13]) {
  177. this.transaction_sorted.push(transaction.container_ean13);
  178. }
  179. this.transaction_by_barcode[transaction.container_ean13] = transaction;
  180. updated_count += 1;
  181. }
  182. this.transaction_write_date = new_write_date || this.transaction_write_date;
  183. if (updated_count) {
  184. // If there were updates, we need to completely
  185. // rebuild the search string and the id indexing
  186. // this.container_search_string = "";
  187. this.transaction_by_id = {};
  188. for (var barcode in this.transaction_by_barcode) {
  189. var transaction = this.transaction_by_barcode[barcode];
  190. if(transaction.id){
  191. this.transaction_by_id[transaction.id] = transaction;
  192. }
  193. // this.container_search_string += this._container_search_string(container);
  194. }
  195. }
  196. return updated_count;
  197. },
  198. get_orders: function(){
  199. return this.load('transactions',[]);
  200. },
  201. get_transaction_by_id: function(id){
  202. return this.transaction_by_id[id];
  203. },
  204. get_transactions_sorted: function(max_count){
  205. max_count = max_count ? Math.min(this.transaction_sorted.length, max_count) : this.transaction_sorted.length;
  206. var transactions = [];
  207. for (var i = 0; i < max_count; i++) {
  208. transactions.push(this.transaction_by_barcode[this.transaction_sorted[i]]);
  209. }
  210. return transactions;
  211. },
  212. remove_transactions: function(barcodes){
  213. for(var i = 0; i < barcodes.length; i++) {
  214. var transaction = this.transaction_by_barcode[barcodes[i]];
  215. if (transaction){
  216. var index_s = this.transaction_sorted.indexOf(transaction.container_ean13);
  217. this.transaction_sorted.splice(index_s, 1);
  218. delete this.transaction_by_id[transaction.id];
  219. delete this.transaction_by_barcode[transaction.container_ean13];
  220. }
  221. }
  222. },
  223. transaction_by_barcode: function(barcode){
  224. return this.transaction_by_barcode[barcode];
  225. },
  226. get_product_name: function(transaction){
  227. return transaction.name;
  228. },
  229. get_today_date: function(){
  230. var today = new Date();
  231. var date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
  232. return date;
  233. },
  234. });
  235. models.load_models({
  236. model: 'pos.transaction',
  237. fields: ['product_id', 'name', 'balance_id', 'ean13', 'write_date', 'container_ean13', 'weight_net', 'price_product', 'price_net', 'weight_tare'],
  238. domain: function(self){ return [['write_date','>',self.db.get_today_date()]]; },
  239. loaded: function(self, transactions){
  240. self.db.add_transactions(transactions);
  241. // return self.load_placeholder_transactions();
  242. },
  243. });
  244. });