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.

319 lines
12 KiB

4 years ago
4 years ago
4 years ago
4 years ago
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 id of caisse from config
  123. get_balance_id: function(){
  124. if (this.config.balance_id) {
  125. return this.config.balance_id;
  126. }
  127. },
  128. // returns the id of balance from config
  129. get_caisse_id: function(){
  130. if (this.config.caisse_id) {
  131. return this.config.caisse_id;
  132. }
  133. },
  134. // returns if the pos is balance from config
  135. get_is_balance_free: function(){
  136. if (this.config.is_balance_free) {
  137. return this.config.is_balance_free;
  138. }
  139. },
  140. get_is_comptoir: function(){
  141. if (this.config.is_comptoir) {
  142. return this.config.is_comptoir;
  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_transaction_by_id: function(id){
  194. return this.transaction_by_id[id];
  195. },
  196. get_transactions_sorted: function(max_count){
  197. max_count = max_count ? Math.min(this.transaction_sorted.length, max_count) : this.transaction_sorted.length;
  198. var transactions = [];
  199. for (var i = 0; i < max_count; i++) {
  200. transactions.push(this.transaction_by_barcode[this.transaction_sorted[i]]);
  201. }
  202. return transactions;
  203. },
  204. remove_transactions: function(barcodes){
  205. for(var i = 0; i < barcodes.length; i++) {
  206. var transaction = this.transaction_by_barcode[barcodes[i]];
  207. if (transaction){
  208. var index_s = this.transaction_sorted.indexOf(transaction.container_ean13);
  209. this.transaction_sorted.splice(index_s, 1);
  210. delete this.transaction_by_id[transaction.id];
  211. delete this.transaction_by_barcode[transaction.container_ean13];
  212. }
  213. }
  214. },
  215. transaction_by_barcode: function(barcode){
  216. return this.transaction_by_barcode[barcode];
  217. },
  218. get_product_name: function(transaction){
  219. return transaction.name;
  220. },
  221. get_today_date: function(){
  222. var today = new Date();
  223. var date = today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate();
  224. return date;
  225. },
  226. });
  227. // Add container to order line
  228. models.Orderline = models.Orderline.extend({
  229. export_as_JSON: function(){
  230. var pack_lot_ids = [];
  231. if (this.has_product_lot){
  232. this.pack_lot_lines.each(_.bind( function(item) {
  233. return pack_lot_ids.push([0, 0, item.export_as_JSON()]);
  234. }, this));
  235. }
  236. return {
  237. qty: this.get_quantity(),
  238. price_unit: this.get_unit_price(),
  239. price_subtotal: this.get_price_without_tax(),
  240. price_subtotal_incl: this.get_price_with_tax(),
  241. discount: this.get_discount(),
  242. product_id: this.get_product().id,
  243. tax_ids: [[6, false, _.map(this.get_applicable_taxes(), function(tax){ return tax.id; })]],
  244. id: this.id,
  245. pack_lot_ids: pack_lot_ids,
  246. //custom starts here
  247. tare: this.get_tare(),
  248. container_id: this.get_container() ? this.get_container().id : null,
  249. container_barcode: this.get_container() ? this.get_container().barcode : null,
  250. container_weight: this.get_container() ? this.get_container().weight : null,
  251. caisse_id: this.pos.config.caisse_id,
  252. };
  253. },
  254. }),
  255. models.load_models({
  256. model: 'pos.transaction',
  257. fields: ['product_id', 'name', 'balance_id', 'ean13', 'write_date', 'container_ean13', 'weight_net', 'price_product', 'price_net', 'weight_tare'],
  258. domain: function(self){ return [['write_date','>',self.db.get_today_date()]]; },
  259. loaded: function(self, transactions){
  260. self.db.add_transactions(transactions);
  261. },
  262. });
  263. });