|
@@ -0,0 +1,206 @@
|
|
|
+openerp.eiru_sale_order = function (instance, local) {
|
|
|
+ local.widgetInstance = null;
|
|
|
+ local.parentInstance = null;
|
|
|
+
|
|
|
+ local.SaleOrderWidget = instance.Widget.extend({
|
|
|
+ template : "eiru_sale_order.SaleOrder",
|
|
|
+ init:function(parent){
|
|
|
+ this._super(parent);
|
|
|
+ this.buttons = parent.$buttons;
|
|
|
+ },
|
|
|
+ updateId : function(id){
|
|
|
+ var self = this;
|
|
|
+ self.id=id;
|
|
|
+ },
|
|
|
+ start: function () {
|
|
|
+ var self = this;
|
|
|
+ this.$el.click(function (e) {
|
|
|
+ self.fecthInitial();
|
|
|
+ });
|
|
|
+ // self.buttons.click(function(e) {
|
|
|
+ // /* E (Editar) */
|
|
|
+ // if (e.target.accessKey === 'E')
|
|
|
+ // self.$el.css('display','flex');
|
|
|
+ // /* C (Crear) */
|
|
|
+ // if (e.target.accessKey === 'C')
|
|
|
+ // self.$el.css('display','none');
|
|
|
+ // /* S (Guarrdar) */
|
|
|
+ // if (e.target.accessKey === 'S')
|
|
|
+ // self.$el.css('display','none');
|
|
|
+ // /* D (Cancelar) */
|
|
|
+ // if (e.target.accessKey === 'D')
|
|
|
+ // self.$el.css('display','none');
|
|
|
+ // });
|
|
|
+ },
|
|
|
+
|
|
|
+ // actualizar lineas
|
|
|
+
|
|
|
+ // reloadLine: function() {
|
|
|
+ // openerp.parentInstance.reload();
|
|
|
+ // },
|
|
|
+
|
|
|
+ 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 self.drawPDF();
|
|
|
+ });
|
|
|
+ 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 self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields=['id','name','curva'];
|
|
|
+ var domain=[['active','=', true],['product_website_sale_type','=', 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 self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields=['id','name','product_tmpl_id','attribute_str'];
|
|
|
+ var product_tmpl_id = _.flatten(_.map(self.ProductTemplate,function(map){
|
|
|
+ return map.id;
|
|
|
+ }));
|
|
|
+ var domain=[['active','=', true],['product_tmpl_id','=', product_tmpl_id]];
|
|
|
+ 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;
|
|
|
+ },
|
|
|
+
|
|
|
+ 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;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ inicializarBuscador: function () {
|
|
|
+ var self = this;
|
|
|
+ var results = self.ProductTemplate;
|
|
|
+ results = _.map(results, function (item) {
|
|
|
+ return {
|
|
|
+ label: item.id + '-'+ item.name,
|
|
|
+ value: item.id + '-'+ item.name
|
|
|
+ }
|
|
|
+ });
|
|
|
+ self.$('#productSearch').autocomplete({
|
|
|
+ source: results,
|
|
|
+ minLength:0,
|
|
|
+ // search: function(event, ui) {
|
|
|
+ // if (!(self.$('#productSearch').val())){
|
|
|
+ // self.factInsert();
|
|
|
+ // }
|
|
|
+ // },
|
|
|
+ // close: function( event, ui ) {
|
|
|
+ // self.factInsert();
|
|
|
+ // },
|
|
|
+ select: function(event, ui) {
|
|
|
+ self.factInsert();
|
|
|
+ // self.$('#productSearch').html('');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ factInsert:function(){
|
|
|
+ var self = this;
|
|
|
+ var product = this.$el.find('#productSearch').val().split('-');
|
|
|
+ var productproduct = self.ProductProduct;
|
|
|
+ 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;
|
|
|
+ // console.log(variant[i].name + ' - ' + variant[i].attribute_str + ' - ' + qty);
|
|
|
+
|
|
|
+ self.joinSaleLine(variant[i].id, qty);
|
|
|
+
|
|
|
+ // self.joinSaleLine(variant[i].id, qty).then(function(results) {
|
|
|
+ // return results;
|
|
|
+ // }).then(function(results){
|
|
|
+ // self.reloadLine()
|
|
|
+ // if (!results)
|
|
|
+ // results = false;
|
|
|
+ // });
|
|
|
+ break;
|
|
|
+ if (contador == template.length) {
|
|
|
+ contador = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ joinSaleLine: function(product_id, qty) {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var sale = new openerp.web.Model('sale.order');
|
|
|
+ sale.call('join_payslip_faults',[
|
|
|
+ {
|
|
|
+ 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 !== 'sale.order') return;
|
|
|
+ local.parentInstance = this;
|
|
|
+ if (local.widgetInstance) {
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
+ }
|
|
|
+ local.widgetInstance = new local.SaleOrderWidget(this);
|
|
|
+ var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
|
|
|
+ elemento = elemento.find('.product_search_box');
|
|
|
+ local.widgetInstance.appendTo(elemento);
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|