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.

293 lines
11 KiB

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. // load_placeholder_transactions: function(){
  129. // var self = this;
  130. // var fields = _.find(this.models,function(model){
  131. // return model.model === 'pos.transaction';
  132. // }).fields;
  133. // var domain = [['barcode', '=', 'CONTAINER']];
  134. // // no need to load it when active because it is already done in standard
  135. // return rpc.query({
  136. // model: 'product.product',
  137. // method: 'search_read',
  138. // args: [domain, fields],
  139. // }).then(function(products){
  140. // self.db.add_products(_.map(products, function (product) {
  141. // return new models.Product({}, product);
  142. // }));
  143. // });
  144. // },
  145. });
  146. PosDB.include({
  147. init: function(parent, options) {
  148. this._super(parent, options);
  149. this.transaction_sorted = [];
  150. this.transaction_by_barcode = {};
  151. this.transaction_by_id = {};
  152. this.transaction_write_date = null;
  153. this.header_text = "";
  154. },
  155. // returns the header text from config
  156. get_name_header: function(){
  157. return this.db.get_name_header();
  158. },
  159. add_transactions: function(transactions) {
  160. var updated_count = 0;
  161. var new_write_date = '';
  162. for(var i = 0, len = transactions.length; i < len; i++) {
  163. var transaction = transactions[i];
  164. if (this.transaction_write_date &&
  165. new Date(this.transaction_write_date).getTime() + 1000 >=
  166. new Date(transaction.write_date).getTime() ) {
  167. continue;
  168. } else if ( new_write_date < transaction.write_date ) {
  169. new_write_date = transaction.write_date;
  170. }
  171. if (!this.transaction_by_barcode[transaction.container_ean13]) {
  172. this.transaction_sorted.push(transaction.container_ean13);
  173. }
  174. this.transaction_by_barcode[transaction.container_ean13] = transaction;
  175. updated_count += 1;
  176. }
  177. this.transaction_write_date = new_write_date || this.transaction_write_date;
  178. if (updated_count) {
  179. // If there were updates, we need to completely
  180. // rebuild the search string and the id indexing
  181. // this.container_search_string = "";
  182. this.transaction_by_id = {};
  183. for (var barcode in this.transaction_by_barcode) {
  184. var transaction = this.transaction_by_barcode[barcode];
  185. if(transaction.id){
  186. this.transaction_by_id[transaction.id] = transaction;
  187. }
  188. // this.container_search_string += this._container_search_string(container);
  189. }
  190. }
  191. return updated_count;
  192. },
  193. get_orders: function(){
  194. return this.load('transactions',[]);
  195. },
  196. get_transaction_by_id: function(id){
  197. return this.transaction_by_id[id];
  198. },
  199. get_transactions_sorted: function(max_count){
  200. max_count = max_count ? Math.min(this.transaction_sorted.length, max_count) : this.transaction_sorted.length;
  201. var transactions = [];
  202. for (var i = 0; i < max_count; i++) {
  203. transactions.push(this.transaction_by_barcode[this.transaction_sorted[i]]);
  204. }
  205. return transactions;
  206. },
  207. remove_transactions: function(barcodes){
  208. for(var i = 0; i < barcodes.length; i++) {
  209. var transaction = this.transaction_by_barcode[barcodes[i]];
  210. if (transaction){
  211. var index_s = this.transaction_sorted.indexOf(transaction.container_ean13);
  212. this.transaction_sorted.splice(index_s, 1);
  213. delete this.transaction_by_id[transaction.id];
  214. delete this.transaction_by_barcode[transaction.container_ean13];
  215. }
  216. }
  217. },
  218. transaction_by_barcode: function(barcode){
  219. return this.transaction_by_barcode[barcode];
  220. },
  221. get_product_name: function(transaction){
  222. return transaction.name;
  223. },
  224. get_today_date: function(){
  225. var today = new Date();
  226. var date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
  227. return date;
  228. },
  229. });
  230. models.load_models({
  231. model: 'pos.transaction',
  232. fields: ['product_id', 'name', 'balance_id', 'ean13', 'write_date', 'container_ean13', 'weight_net', 'price_product', 'price_net'],
  233. domain: function(self){ return [['write_date','>',self.db.get_today_date()]]; },
  234. loaded: function(self, transactions){
  235. self.db.add_transactions(transactions);
  236. // return self.load_placeholder_transactions();
  237. },
  238. });
  239. });