<commit_before>/*
    POS Automatic Cashdrawer module for Odoo
    Copyright (C) 2016 <NAME>
    @author: <NAME>
    The licence is in the file __openerp__.py
*/

odoo.define('pos_automatic_cashdrawer.pos_automatic_cashdrawer', function (require) {
    "use strict";
    var screens = require('point_of_sale.screens');
    var devices = require('point_of_sale.devices');
    var models = require('point_of_sale.models');
    var core = require('web.core');
    var chrome = require('point_of_sale.chrome');
    var gui = require('point_of_sale.gui');
    var _t = core._t;
    var QWeb = core.qweb;

    models.load_fields("account.journal", ['payment_mode',
                                           'iface_automatic_cashdrawer']);
    models.load_fields("pos.config", ['iface_automatic_cashdrawer',
                                      'iface_automatic_cashdrawer_ip_address',
                                      'iface_automatic_cashdrawer_tcp_port',
                                      'iface_automatic_cashdrawer_display_accept_button',
                                      'iface_automatic_cashdrawer_screen_on_top',
                                      'group_pos_automatic_cashlogy',
                                      'group_pos_automatic_cashlogy_config'
                                      ]);

    // Overload 'set_cashier' function to display correctly
    // unauthorized function after cashier changed
    var _set_cashier_ = models.PosModel.prototype.set_cashier;
    models.PosModel.prototype.set_cashier = function(user){
        this.gui.display_access_right_cashlogy(user);
        _set_cashier_.call(this, user);
    };

    // If the user is just checking automatic cashdrawer, then the system will try to connect proxy.
    var after_load_server_data_original = models.PosModel.prototype.after_load_server_data;
    models.PosModel = models.PosModel.extend({
        after_load_server_data: function(session, attributes) {
            if (this.config.iface_automatic_cashdrawer) {
                this.config.use_proxy = true;
            }
            return after_load_server_data_original.call(this);
        },
    });

    models.Paymentline = models.Paymentline.extend({
        get_automatic_cashdrawer: function() {
            return this.cashregister.journal.iface_automatic_cashdrawer;
        },
    });

    gui.Gui.prototype.display_access_right_cashlogy = function(user){
        if (user.groups_id.indexOf(this.pos.config.group_pos_automatic_cashlogy[0]) != -1){
            $('.js_auto_cashdrawer_init').removeClass('oe_hidden');
        }
        else {
            $('.js_auto_cashdrawer_init').addClass('oe_hidden');
        }
        console.log(this.pos.config);
        if (user.groups_id.indexOf(this.pos.config.group_pos_automatic_cashlogy_config[0]) != -1){
            
            $('.js_auto_cashdrawer_config').removeClass('oe_hidden');
        }
        else {
            $('.js_auto_cashdrawer_config').addClass('oe_hidden');
        }
    };
    
    devices.ProxyDevice.include({
        init: function(){
            this._super.apply(this, arguments);
            this.set('cashdrawer_status', {});
            this.set_connection_cashdrawer_status('disconnected');
        },
        set_connection_cashdrawer_status: function(cashdrawer_status, drivers){
            var oldstatus = this.get('cashdrawer_status');
            var newstatus = {};
            newstatus.cashdrawer_status = cashdrawer_status;
            newstatus.drivers = cashdrawer_status === 'disconnected' ? {} : oldstatus.drivers;
            newstatus.drivers = drivers ? drivers : newstatus.drivers;
            this.set('cashdrawer_status', newstatus);
        },
        automatic_cashdrawer_transaction_start: function(screen) {
            var order = this.pos.get_order();
            var line = order.selected_paymentline;
            if (line) {
                var data = {
                        'amount': order.get_due(line),
                        'display_accept_button': this.pos.config.iface_automatic_cashdrawer_display_accept_button,
                        'screen_on_top': this.pos.config.iface_automatic_cashdrawer_screen_on_top
                        };
                this.message('automatic_cashdrawer_transaction_start', {'payment_info' : JSON.stringify(data)}).then(function (answer) {
                    // Check if there was any error or a value
                    var answer_info = answer['info'];
                    if (answer_info) {
                        var answer_type_expression = /[a-zA-Z]+/g;
                        var answer_type = answer_info.match(answer_type_expression);
                        if (answer_type) {
                            // If there is an answer type
                            if (answer_type[0] == "WR" && answer_type[1] == "CANCEL") {
                                // Case #WR:CANCEL#b#c#d#e# :
                                // TODO : check what to do here. But I think this should do nothing.
                            }
                            else if (answer_type[0] == "ER" && answer_type[1] == "BUSY") {
                                // Case #ER:BUSY# : answer_type[0] == "ER"
                                // TODO : check what to do here. But I think this wont append because the cash drawer wont give back this error.
                            }
                            else if (answer_type[0] == "ER") {
                                // Case #ER:xxxx#b#c#d#e# : answer_type[0] == "ER"
                                // TODO : check what to do here. But I think this wont append because the cash drawer wont give back this error.
                            }
                            else if (answer_type[0] == "WR" && answer_type[1] == "LEVEL") {
                                // Case #WR:LEVEL#b#c#d#e#:
                                // The return says that an amount was correctly given to the cache machine
                                var amount_expression = /[0-9]+/g;
                                var amount_expression = answer_info.match(amount_expression);
                                var amount_in = amount_expression[0] / 100;
                                var amount_out = amount_expression[1] / 100;
                                if (!amount_in == 0) {
                                    // TODO : Check the amount_out and what is display on screen ?
                                    line.set_amount(amount_in);
                                    screen.order_changes();
                                    screen.render_paymentlines();
                                    var amount_in_formatted = screen.format_currency_no_symbol(amount_in);
                                    screen.$('.paymentline.selected .edit').text(amount_in_formatted);
                                    screen.$('.delete-button').css('display', 'none');
                                    screen.$('.automatic-cashdrawer-transaction-start').css('display', 'none');
                                }
                            }
                        }
                    }
                });
            }
            // TODO : this function should check the real amount received, and correct it / handle warnings and errors sent from the cashdrawer
        },
        automatic_cashdrawer_connection_check: function(){
            // call this function on POS loading to be able to know if the machine is available
            var data = {
                    'ip_address': this.pos.config.iface_automatic_cashdrawer_ip_address,
                    'tcp_port': this.pos.config.iface_automatic_cashdrawer_tcp_port
                    }
            var self = this;
            self.message('automatic_cashdrawer_connection_check', {'connection_info' : JSON.stringify(data)});
        },
        automatic_cashdrawer_connection_init: function(){
            // TODO : call this function on POS loading
            // TODO : only managers should be able to see/clic this button
            var data = {
                    'ip_address': this.pos.config.iface_automatic_cashdrawer_ip_address,
                    'tcp_port': this.pos.config.iface_automatic_cashdrawer_tcp_port
                    }
            this.message('automatic_cashdrawer_connection_init', {'connection_info' : JSON.stringify(data)}).then(function(answer) {
                console.log(answer);
            });
        },
        automatic_cashdrawer_connection_exit: function(){
            // TODO : call this function on POS exit
            // TODO : only managers should  be able to see/clic this button
            this.message('automatic_cashdrawer_connection_exit').then(function(answer) {
                console.log(answer);
            });
        },
        automatic_cashdrawer_connection_display_backoffice: function(){
            // TODO : only managers should  be able to see/clic this button
            var data = {'bo' : 'null'}
            this.message('automatic_cashdrawer_display_backoffice', {'backoffice_info' : JSON.stringify(data)}).then(function(answer) {
                console.log(answer);
            });
        },
    });
    screens.NumpadWidget.include({
        // Overload 'start' function to display correctly unauthorized function
        // at the beginning of the session, based on current user
        start: function() {
            this._super();
            this.gui.display_access_right_cashlogy(this.pos.get_cashier());
        },
    });
    
    screens.PaymentScreenWidget.include({
//            render_paymentlines : function(){
//                this._super.apply(this, arguments);
//                var self = this;
//                        // Directly call the automatic cashdrawer if the journal is checked
////                  self.pos.proxy.automatic_cashdrawer_transaction_start($(this).data('cid'), self);
//                this.$('.paymentlines-container').unbind('click').on('click','.automatic-cashdrawer-transaction-start', function(event){
//                // Why this "on" thing links several time the button to the action if I don't use "unlink" to reset the button links before ?
//                            //console.log(event.target);<commit_msg>[WKAR] work around to by pass the bug from odoo. Should be rolled back when it's fixed in Odoo.<commit_after>/*
    POS Automatic Cashdrawer module for Odoo
    Copyright (C) 2016 <NAME>
    @author: <NAME>
    The licence is in the file __openerp__.py
*/

odoo.define('pos_automatic_cashdrawer.pos_automatic_cashdrawer', function (require) {
    "use strict";
    var screens = require('point_of_sale.screens');
    var devices = require('point_of_sale.devices');
    var models = require('point_of_sale.models');
    var core = require('web.core');
    var chrome = require('point_of_sale.chrome');
    var gui = require('point_of_sale.gui');
    var _t = core._t;
    var QWeb = core.qweb;

    models.load_fields("account.journal", ['payment_mode',
                                           'iface_automatic_cashdrawer']);
    models.load_fields("pos.config", ['iface_automatic_cashdrawer',
                                      'iface_automatic_cashdrawer_ip_address',
                                      'iface_automatic_cashdrawer_tcp_port',
                                      'iface_automatic_cashdrawer_display_accept_button',
                                      'iface_automatic_cashdrawer_screen_on_top',
                                      'group_pos_automatic_cashlogy',
                                      'group_pos_automatic_cashlogy_config'
                                      ]);

    // Overload 'set_cashier' function to display correctly
    // unauthorized function after cashier changed
    var _set_cashier_ = models.PosModel.prototype.set_cashier;
    models.PosModel.prototype.set_cashier = function(user){
    	this.gui.display_access_right_cashlogy(user);
        _set_cashier_.call(this, user);
    };

    // If the user is just checking automatic cashdrawer, then the system will try to connect proxy.
    var after_load_server_data_original = models.PosModel.prototype.after_load_server_data;
    models.PosModel = models.PosModel.extend({
        after_load_server_data: function(session, attributes) {
            if (this.config.iface_automatic_cashdrawer) {
                this.config.use_proxy = true;
            }
            return after_load_server_data_original.call(this);
        },
    });

    models.Paymentline = models.Paymentline.extend({
        get_automatic_cashdrawer: function() {
            return this.cashregister.journal.iface_automatic_cashdrawer;
        },
    });

    gui.Gui.prototype.display_access_right_cashlogy = function(user){
//    	if (user.groups_id !== undefined){
//	    	alert(user.groups_id);
//    		if (user.groups_id.indexOf(this.pos.config.group_pos_automatic_cashlogy[0]) != -1){
	        $('.js_auto_cashdrawer_init').removeClass('oe_hidden');
//	        }
//	        else {
//	            $('.js_auto_cashdrawer_init').addClass('oe_hidden');
//	        }
	        console.log(this.pos.config);
//	        if (user.groups_id.indexOf(this.pos.config.group_pos_automatic_cashlogy_config[0]) != -1){
	            $('.js_auto_cashdrawer_config').removeClass('oe_hidden');
//	        }
//	        else {
//	            $('.js_auto_cashdrawer_config').addClass('oe_hidden');
//	        }
//    	}
    };
    
    devices.ProxyDevice.include({
        init: function(){
            this._super.apply(this, arguments);
            this.set('cashdrawer_status', {});
            this.set_connection_cashdrawer_status('disconnected');
        },
        set_connection_cashdrawer_status: function(cashdrawer_status, drivers){
            var oldstatus = this.get('cashdrawer_status');
            var newstatus = {};
            newstatus.cashdrawer_status = cashdrawer_status;
            newstatus.drivers = cashdrawer_status === 'disconnected' ? {} : oldstatus.drivers;
            newstatus.drivers = drivers ? drivers : newstatus.drivers;
            this.set('cashdrawer_status', newstatus);
        },
        automatic_cashdrawer_transaction_start: function(screen) {
            var order = this.pos.get_order();
            var line = order.selected_paymentline;
            if (line) {
                var data = {
                        'amount': order.get_due(line),
                        'display_accept_button': this.pos.config.iface_automatic_cashdrawer_display_accept_button,
                        'screen_on_top': this.pos.config.iface_automatic_cashdrawer_screen_on_top
                        };
                this.message('automatic_cashdrawer_transaction_start', {'payment_info' : JSON.stringify(data)}).then(function (answer) {
                    // Check if there was any error or a value
                    var answer_info = answer['info'];
                    if (answer_info) {
                        var answer_type_expression = /[a-zA-Z]+/g;
                        var answer_type = answer_info.match(answer_type_expression);
                        if (answer_type) {
                            // If there is an answer type
                            if (answer_type[0] == "WR" && answer_type[1] == "CANCEL") {
                                // Case #WR:CANCEL#b#c#d#e# :
                                // TODO : check what to do here. But I think this should do nothing.
                            }
                            else if (answer_type[0] == "ER" && answer_type[1] == "BUSY") {
                                // Case #ER:BUSY# : answer_type[0] == "ER"
                                // TODO : check what to do here. But I think this wont append because the cash drawer wont give back this error.
                            }
                            else if (answer_type[0] == "ER") {
                                // Case #ER:xxxx#b#c#d#e# : answer_type[0] == "ER"
                                // TODO : check what to do here. But I think this wont append because the cash drawer wont give back this error.
                            }
                            else if (answer_type[0] == "WR" && answer_type[1] == "LEVEL") {
                                // Case #WR:LEVEL#b#c#d#e#:
                                // The return says that an amount was correctly given to the cache machine
                                var amount_expression = /[0-9]+/g;
                                var amount_expression = answer_info.match(amount_expression);
                                var amount_in = amount_expression[0] / 100;
                                var amount_out = amount_expression[1] / 100;
                                if (!amount_in == 0) {
                                    // TODO : Check the amount_out and what is display on screen ?
                                    line.set_amount(amount_in);
                                    screen.order_changes();
                                    screen.render_paymentlines();
                                    var amount_in_formatted = screen.format_currency_no_symbol(amount_in);
                                    screen.$('.paymentline.selected .edit').text(amount_in_formatted);
                                    screen.$('.delete-button').css('display', 'none');
                                    screen.$('.automatic-cashdrawer-transaction-start').css('display', 'none');
                                }
                            }
                        }
                    }
                });
            }
            // TODO : this function should check the real amount received, and correct it / handle warnings and errors sent from the cashdrawer
        },
        automatic_cashdrawer_connection_check: function(){
            // call this function on POS loading to be able to know if the machine is available
            var data = {
                    'ip_address': this.pos.config.iface_automatic_cashdrawer_ip_address,
                    'tcp_port': this.pos.config.iface_automatic_cashdrawer_tcp_port
                    }
            var self = this;
            self.message('automatic_cashdrawer_connection_check', {'connection_info' : JSON.stringify(data)});
        },
        automatic_cashdrawer_connection_init: function(){
            // TODO : call this function on POS loading
            // TODO : only managers should be able to see/clic this button
            var data = {
                    'ip_address': this.pos.config.iface_automatic_cashdrawer_ip_address,
                    'tcp_port': this.pos.config.iface_automatic_cashdrawer_tcp_port
                    }
            this.message('automatic_cashdrawer_connection_init', {'connection_info' : JSON.stringify(data)}).then(function(answer) {
                console.log(answer);
            });
        },
        automatic_cashdrawer_connection_exit: function(){
            // TODO : call this function on POS exit
            // TODO : only managers should  be able to see/clic this button
            this.message('automatic_cashdrawer_connection_exit').then(function(answer) {
                console.log(answer);
            });
        },
        automatic_cashdrawer_connection_display_backoffice: function(){
            // TODO : only managers should  be able to see/clic this button
            var data = {'bo' : 'null'}
            this.message('automatic_cashdrawer_display_backoffice', {'backoffice_info' : JSON.stringify(data)}).then(function(answer) {
                console.log(answer);
            });
        },
    });
    screens.NumpadWidget.include({
        // Overload 'start' function to display correctly unauthorized function
        // at the beginning of the session, based on current user
        start: function() {
            this._super();
//            alert('before call');
//            alert(this.pos.get_cashier().groups_id);
//            alert('after call');
            this.gui.display_access_right_cashlogy(this.pos.get_cashier());
        },
    });
    
    screens.PaymentScreenWidget.include({
//            render_paymentlines : function(){
//                this._super.apply(this, arguments);
//                var self = this;
//                        // Directly call the automatic cashdrawer if the journal is checked
////                  self.pos.proxy.automatic_cashdrawer_transaction_start($(this).data('cid'), self);
//                this.$('.paymentlines-container').unbind('click').on('click','.automatic-cashdrawer-transaction-start', function(event){
//                // Why this "on" thing links several time the button to the action if I don't use "unlink" to reset the button links before ?
//                            //console.log(event.target);
{'fraction_non_alphanumeric': 0.10077705295471988, 'fraction_numerical': 0.002686108979278588, 'mean_word_length': 1.684996780424984, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 62, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '16126', 'n_tokens_mistral': 5636, 'n_tokens_neox': 5282, 'n_words': 1098}