|
@@ -0,0 +1,240 @@
|
|
|
+openerp.eiru_payment_term_generator = function (instance, local) {
|
|
|
+ local.widgetInstance = null;
|
|
|
+ local.parentInstance = null;
|
|
|
+
|
|
|
+ local.AccountPaymentTermWidget = instance.Widget.extend({
|
|
|
+ template : "eiru_payment_term_generator.AccountPaymentTerm",
|
|
|
+ init:function(parent){
|
|
|
+ this._super(parent);
|
|
|
+ this.buttons = parent.$buttons;
|
|
|
+ },
|
|
|
+
|
|
|
+ updateId : function(id){
|
|
|
+ var self = this;
|
|
|
+ self.id=id;
|
|
|
+ },
|
|
|
+
|
|
|
+ reloadLine: function() {
|
|
|
+ local.parentInstance.reload();
|
|
|
+ },
|
|
|
+
|
|
|
+ start: function () {
|
|
|
+ var self = this;
|
|
|
+ this.$el.click(function () {
|
|
|
+ self.fecthInitial();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ fecthInitial: function(){
|
|
|
+ var id= openerp.webclient._current_state.id;
|
|
|
+ var self = this;
|
|
|
+ self.fecthSale(id).then(function(sale){
|
|
|
+ return sale;
|
|
|
+ }).then(function(sale){
|
|
|
+ self.sale = sale;
|
|
|
+ return self.fetchProductTemplate();
|
|
|
+ }).then(function(ProductTemplate){
|
|
|
+ self.ProductTemplate = ProductTemplate;
|
|
|
+ return self.fetchProductProduct();
|
|
|
+ }).then(function(ProductProduct){
|
|
|
+ self.ProductProduct = ProductProduct;
|
|
|
+ self.inicializarBuscador();
|
|
|
+ });
|
|
|
+ return false;
|
|
|
+ },
|
|
|
+
|
|
|
+ fecthSale: function(id){
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields=['id','name','partner_id','state'];
|
|
|
+ var domain=[['id','=', id]];
|
|
|
+ var Sale = new instance.web.Model('sale.order');
|
|
|
+ Sale.query(fields).filter(domain).order_by('id').all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchProductTemplate: function () {
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields=['id','name','curva'];
|
|
|
+ var domain=[['active','=', true],['product_website_sale_type','=', true],['sale_ok','=', true]];
|
|
|
+ var ProductTemplate = new instance.web.Model('product.template');
|
|
|
+ ProductTemplate.query(fields).filter(domain).order_by('id').all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchProductProduct: function () {
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields=['id','name','product_tmpl_id','attribute_str','default_code','ean13'];
|
|
|
+ var domain=[['active','=', true],['sale_ok','=', true]];
|
|
|
+ var ProductProduct = new instance.web.Model('product.product');
|
|
|
+ ProductProduct.query(fields).filter(domain).order_by('id').all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ valorNull:function(dato){
|
|
|
+ var valor ="";
|
|
|
+ if (dato){
|
|
|
+ valor=dato;
|
|
|
+ }
|
|
|
+ return valor;
|
|
|
+ },
|
|
|
+
|
|
|
+ getTemplate : function(id){
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.ProductTemplate, function(item){
|
|
|
+ return item.id == id;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ getProduct : function(product_tmpl_id){
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.ProductProduct, function(item){
|
|
|
+ return item.product_tmpl_id[0] == product_tmpl_id;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ getProductProduct : function(id){
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.ProductProduct, function(item){
|
|
|
+ return item.id == id;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ inicializarBuscador: function () {
|
|
|
+ var self = this;
|
|
|
+ var selectProduct;
|
|
|
+ var searchType = $('#SearchByCurve').is(":checked");
|
|
|
+ if(searchType){
|
|
|
+ var results = self.ProductTemplate;
|
|
|
+ results = _.map(results, function (item) {
|
|
|
+ return {
|
|
|
+ label: item.name,
|
|
|
+ value: item.id + '-'+ item.name
|
|
|
+ }
|
|
|
+ });
|
|
|
+ self.$('#productSearch').autocomplete({
|
|
|
+ source: results,
|
|
|
+ minLength:3,
|
|
|
+ response: function (e, ui){
|
|
|
+ if (ui.content instanceof Array && ui.content.length === 1) {
|
|
|
+ selectProduct = ui.content[0];
|
|
|
+ $(this).autocomplete("close");
|
|
|
+ $(this).val('');
|
|
|
+ self.factInsert(selectProduct);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ select: function (e, ui) {
|
|
|
+ $(this).val('');
|
|
|
+ selectProduct = ui.item;
|
|
|
+ self.factInsert(selectProduct);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }else{
|
|
|
+ var results = self.ProductProduct;
|
|
|
+ results = _.map(results, function (item) {
|
|
|
+ return {
|
|
|
+ label: '[ ' + self.valorNull(item.default_code) + ' - ' + self.valorNull(item.ean13) + ' ] ' + item.name + ' (' + self.valorNull(item.attribute_str) + ')',
|
|
|
+ value: item.id + ' - '+ item.name + ' (' + item.attribute_str + ')'
|
|
|
+ }
|
|
|
+ });
|
|
|
+ $('#productSearch').autocomplete({
|
|
|
+ source: results,
|
|
|
+ minLength: 3,
|
|
|
+ response: function (e, ui){
|
|
|
+ if (ui.content instanceof Array && ui.content.length === 1) {
|
|
|
+ selectProduct = ui.content[0];
|
|
|
+ $(this).autocomplete("close");
|
|
|
+ $(this).val('');
|
|
|
+ self.factInsertProduct(selectProduct);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ select: function (e, ui) {
|
|
|
+ $(this).val('');
|
|
|
+ selectProduct = ui.item;
|
|
|
+ self.factInsertProduct(selectProduct);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ factInsert:function(selectProduct){
|
|
|
+ var self = this;
|
|
|
+ var product = selectProduct.value.split('-');
|
|
|
+ var template = self.getTemplate(product[0]);
|
|
|
+ var variant = self.getProduct(product[0]);
|
|
|
+ template = template[0].curva.split(',');
|
|
|
+ var qty = 0;
|
|
|
+ var contador = 0;
|
|
|
+ for (var i = 0; i < variant.length; i++) {
|
|
|
+ qty = template[contador];
|
|
|
+ contador += 1;
|
|
|
+ self.joinSaleLine(variant[i].id, qty).then(function(results) {
|
|
|
+ return results;
|
|
|
+ }).then(function(){
|
|
|
+ self.reloadLine();
|
|
|
+ });
|
|
|
+ if (contador === template.length) {
|
|
|
+ contador = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ factInsertProduct:function(selectProduct){
|
|
|
+ var self = this;
|
|
|
+ var product = selectProduct.value.split('-');
|
|
|
+ var variant = self.getProductProduct(product[0]);
|
|
|
+ var qty = 1;
|
|
|
+ self.joinSaleLine(variant[0].id, qty).then(function(results) {
|
|
|
+ return results;
|
|
|
+ }).then(function(){
|
|
|
+ self.reloadLine();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ joinSaleLine: function(product_id, qty) {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var sale = new openerp.web.Model('account.payment.term');
|
|
|
+ sale.call('join_sale_lines',[
|
|
|
+ {
|
|
|
+ id: self.sale[0].id,
|
|
|
+ product_id: product_id,
|
|
|
+ product_uom_qty: qty
|
|
|
+ }
|
|
|
+ ], {
|
|
|
+ context: new openerp.web.CompoundContext()
|
|
|
+ }).then(function(results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ if (instance.web && instance.web.FormView) {
|
|
|
+ instance.web.FormView.include({
|
|
|
+ load_form: function (record) {
|
|
|
+ this._super.apply(this, arguments);
|
|
|
+ if (this.model !== 'account.payment.term') return;
|
|
|
+ local.parentInstance = this;
|
|
|
+ if (local.widgetInstance) {
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
+ }
|
|
|
+ local.widgetInstance = new local.AccountPaymentTermWidget(this);
|
|
|
+ var elemento = this.$el.find('.oe_form_nosheet');
|
|
|
+ console.log(elemento);
|
|
|
+ elemento = elemento.find('.calculate_term_box');
|
|
|
+ console.log(elemento);
|
|
|
+ local.widgetInstance.appendTo(elemento);
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|