[MERGE] forward port of branch 8.0 up to ed1c173
[odoo/odoo.git] / addons / point_of_sale / static / src / js / models.js
index 9a8064a..07869f1 100644 (file)
@@ -279,14 +279,12 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
             label: 'fonts',
             loaded: function(self){
                 var fonts_loaded = new $.Deferred();
-
                 // Waiting for fonts to be loaded to prevent receipt printing
                 // from printing empty receipt while loading Inconsolata
                 // ( The font used for the receipt ) 
                 waitForWebfonts(['Lato','Inconsolata'], function(){
                     fonts_loaded.resolve();
                 });
-
                 // The JS used to detect font loading is not 100% robust, so
                 // do not wait more than 5sec
                 setTimeout(function(){
@@ -518,7 +516,6 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
         // wrapper around the _save_to_server that updates the synch status widget
         _flush_orders: function(orders, options) {
             var self = this;
-
             this.set('synch',{ state: 'connecting', pending: orders.length});
 
             return self._save_to_server(orders, options).done(function (server_ids) {
@@ -912,6 +909,7 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
             this.screen_data = {};  // see ScreenSelector
             this.receipt_type = 'receipt';  // 'receipt' || 'invoice'
             this.temporary = attributes.temporary || false;
+            this.to_invoice = false;
             return this;
         },
         is_empty: function(){
@@ -1056,11 +1054,47 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
                 return sum + paymentLine.get_amount();
             }), 0);
         },
-        getChange: function() {
-            return this.getPaidTotal() - this.getTotalTaxIncluded();
+        getChange: function(paymentline) {
+            if (!paymentline) {
+                var change = this.getPaidTotal() - this.getTotalTaxIncluded();
+            } else {
+                var change = -this.getTotalTaxIncluded(); 
+                var lines  = this.get('paymentLines').models;
+                for (var i = 0; i < lines.length; i++) {
+                    change += lines[i].get_amount();
+                    if (lines[i] === paymentline) {
+                        break;
+                    }
+                }
+            }
+            return round_pr(Math.max(0,change), this.pos.currency.rounding);
+        },
+        getDueLeft: function(paymentline) {
+            if (!paymentline) {
+                var due = this.getTotalTaxIncluded() - this.getPaidTotal();
+            } else {
+                var due = this.getTotalTaxIncluded();
+                var lines = this.get('paymentLines').models;
+                for (var i = 0; i < lines.length; i++) {
+                    if (lines[i] === paymentline) {
+                        break;
+                    } else {
+                        due -= lines[i].get_amount();
+                    }
+                }
+            }
+            return round_pr(Math.max(0,due), this.pos.currency.rounding);
+        },
+        isPaid: function(){
+            return this.getDueLeft() === 0;
         },
-        getDueLeft: function() {
-            return this.getTotalTaxIncluded() - this.getPaidTotal();
+        isPaidWithCash: function(){
+            return !!this.get('paymentLines').find( function(pl){
+                return pl.cashregister.journal.type === 'cash';
+            });
+        },
+        finalize: function(){
+            this.destroy();
         },
         // sets the type of receipt 'receipt'(default) or 'invoice'
         set_receipt_type: function(type){
@@ -1092,6 +1126,25 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal
                 }
             }
         },
+        set_to_invoice: function(to_invoice) {
+            this.to_invoice = to_invoice;
+        },
+        is_to_invoice: function(){
+            return this.to_invoice;
+        },
+        // remove all the paymentlines with zero money in it
+        clean_empty_paymentlines: function() {
+            var lines = this.get('paymentLines').models;
+            var empty = [];
+            for ( var i = 0; i < lines.length; i++) {
+                if (!lines[i].get_amount()) {
+                    empty.push(lines[i]);
+                }
+            }
+            for ( var i = 0; i < empty.length; i++) {
+                this.removePaymentline(empty[i]);
+            }
+        },
         //see set_screen_data
         get_screen_data: function(key){
             return this.screen_data[key];