|
@@ -0,0 +1,493 @@
|
|
|
+function report_pos_orders(reporting){
|
|
|
+ "use strict";
|
|
|
+
|
|
|
+ var model = openerp;
|
|
|
+
|
|
|
+ reporting.ReportPosOrdersWidget = reporting.Base.extend({
|
|
|
+ template: 'ReportPosOrders',
|
|
|
+ rowsData :[],
|
|
|
+ content:[],
|
|
|
+
|
|
|
+ events:{
|
|
|
+ 'click #toolbar > button' : 'clickOnAction',
|
|
|
+ 'click #X' : 'fectSearch',
|
|
|
+ 'click #A' : 'fectSearch',
|
|
|
+ 'click #B' : 'fectSearch',
|
|
|
+ 'click #C' : 'fectSearch',
|
|
|
+ 'click #D' : 'fectSearch',
|
|
|
+ 'click #Z' : 'fectSearch',
|
|
|
+ 'change #from' : 'fectSearch',
|
|
|
+ 'change #to': 'fectSearch',
|
|
|
+ 'change #current-floor': 'fectSearch',
|
|
|
+ 'change #current-table': 'fectSearch',
|
|
|
+ 'click-row.bs.table #table' : 'clickAnalysisDetail',
|
|
|
+ },
|
|
|
+
|
|
|
+ init : function(parent){
|
|
|
+ this._super(parent);
|
|
|
+ },
|
|
|
+
|
|
|
+ start: function () {
|
|
|
+ var table = this.$el.find('#table');
|
|
|
+ table.bootstrapTable({data : self.rowData});
|
|
|
+ this.fecthFecha();
|
|
|
+ this.submitForm();
|
|
|
+ },
|
|
|
+
|
|
|
+ clickAnalysisDetail: function(e, row, $element, field){
|
|
|
+ if (field == 'order_name'){
|
|
|
+ this.do_action({
|
|
|
+ name : "Pedido",
|
|
|
+ type : 'ir.actions.act_window',
|
|
|
+ res_model : "pos.order",
|
|
|
+ views : [[false,'form']],
|
|
|
+ target : 'new',
|
|
|
+ domain : [['id','=', row.order_id]],
|
|
|
+ context : {},
|
|
|
+ flags : {'form': {'action_buttons': false, 'options': {'mode': 'view'}}},
|
|
|
+ res_id : row.order_id,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (field == 'product_name'){
|
|
|
+ this.do_action({
|
|
|
+ name : "Productos",
|
|
|
+ type : 'ir.actions.act_window',
|
|
|
+ res_model : "product.product",
|
|
|
+ views : [[false,'form']],
|
|
|
+ target : 'new',
|
|
|
+ domain : [['id','=', row.product_id]],
|
|
|
+ context : {},
|
|
|
+ flags : {'form': {'action_buttons': false, 'options': {'mode': 'view'}}},
|
|
|
+ res_id : row.product_id,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ e.stopImmediatePropagation();
|
|
|
+ },
|
|
|
+
|
|
|
+ fecthFecha: function() {
|
|
|
+ var to;
|
|
|
+ var dateFormat1 = "mm/dd/yy",
|
|
|
+ from = $( "#from" )
|
|
|
+ .datepicker({
|
|
|
+ dateFormat: "dd/mm/yy",
|
|
|
+ changeMonth: true,
|
|
|
+ numberOfMonths: 1,
|
|
|
+ })
|
|
|
+ .on( "change", function() {
|
|
|
+ to.datepicker( "option", "minDate", getDate(this), "dd/mm/yyyy");
|
|
|
+ });
|
|
|
+ to = $( "#to" ).datepicker({
|
|
|
+ dateFormat: "dd/mm/yy",
|
|
|
+ defaultDate: "+7d",
|
|
|
+ changeMonth: true,
|
|
|
+ numberOfMonths: 1,
|
|
|
+ })
|
|
|
+ .on( "change", function() {
|
|
|
+ from.datepicker( "option", "maxDate", getDate(this));
|
|
|
+ });
|
|
|
+ function getDate( element ) {
|
|
|
+ var fechaSel =element.value.split('/');
|
|
|
+ var date;
|
|
|
+ try {
|
|
|
+ date = $.datepicker.parseDate( dateFormat1, (fechaSel[1]+"/"+fechaSel[0]+"/"+fechaSel[2]));
|
|
|
+ } catch( error ) {
|
|
|
+ date = null;
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ valorNull:function(dato){
|
|
|
+ var valor ="";
|
|
|
+ if (dato){
|
|
|
+ valor=dato;
|
|
|
+ }
|
|
|
+ return valor;
|
|
|
+ },
|
|
|
+
|
|
|
+ // Cansultar
|
|
|
+ submitForm: function () {
|
|
|
+ var self = this;
|
|
|
+ this.fetchPosOrder().then(function(PosOrder) {
|
|
|
+ self.PosOrder = PosOrder;
|
|
|
+ return PosOrder;
|
|
|
+ }).then(function (PosOrder) {
|
|
|
+ return self.fetchPosOrderLine();
|
|
|
+ }).then(function (PosOrderLine) {
|
|
|
+ self.PosOrderLine = PosOrderLine;
|
|
|
+ return self.fetchResPartner();
|
|
|
+ }).then(function (ResPartner){
|
|
|
+ self.ResPartner = ResPartner;
|
|
|
+ return self.fetchProductProduct();
|
|
|
+ }).then(function (ProductProduct) {
|
|
|
+ self.ProductProduct = ProductProduct;
|
|
|
+ return self.fetchPosFloor();
|
|
|
+ }).then(function(PosFloor){
|
|
|
+ self.PosFloor = PosFloor;
|
|
|
+ self.$el.find('#current-floor').append('<option value="9999999">Ubicacion</option>');
|
|
|
+ _.each(PosFloor,function(item){
|
|
|
+ self.$el.find('#current-floor').append('<option value="' + item.id + '">' + item.name + '</option>');
|
|
|
+ });
|
|
|
+ return self.fetchPosTable();
|
|
|
+ }).then(function(PosTable){
|
|
|
+ self.PosTable = PosTable;
|
|
|
+ self.$el.find('#current-table').append('<option value="9999999">Mesa o Delivery</option>');
|
|
|
+ _.each(PosTable,function(item){
|
|
|
+ self.$el.find('#current-table').append('<option value="' + item.id + '">' + item.name + '</option>');
|
|
|
+ });
|
|
|
+ self.searchProduct();
|
|
|
+ return self.BuildTable();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // Ventas (Point Of Sale)
|
|
|
+ fetchPosOrder: function () {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields = ['id','name','date_order','lines','table_id'];
|
|
|
+ var domain = [['state', 'not in', ['draft','cancel']]];
|
|
|
+ var PosOrder = new model.web.Model('pos.order');
|
|
|
+ PosOrder.query(fields).filter(domain).all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ // Lineas de venta (Point Of Sale)
|
|
|
+ fetchPosOrderLine: function () {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var ids = _.flatten(_.map(self.PosOrder, function (item) {
|
|
|
+ return item.id;
|
|
|
+ }));
|
|
|
+ var fields = ['id', 'name', 'order_id', 'product_id', 'qty', 'price_unit', 'price_subtotal_incl']
|
|
|
+ var domain = [['order_id', 'in', ids]];
|
|
|
+ var PosOrderLine = new model.web.Model('pos.order.line');
|
|
|
+ PosOrderLine.query().filter(domain).all().then(function (results) {
|
|
|
+ defer.resolve(results)
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ // clientes
|
|
|
+ fetchResPartner: function () {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields = ['id','name','ruc'];
|
|
|
+ var domain = [['active', '=', true],['customer', '=', true]];
|
|
|
+ var ResPartner = new model.web.Model('res.partner');
|
|
|
+ ResPartner.query(fields).filter(domain).all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ // Productos
|
|
|
+ fetchProductProduct: function(){
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var porduct_ids = _.flatten(_.map(self.PosOrderLine, function (item) {
|
|
|
+ return item.product_id[0];
|
|
|
+ }));
|
|
|
+ var fields = ['id','name', 'default_code', 'name_template','ean13', 'standard_price','type','attribute_str'];
|
|
|
+ var domain = [['id', 'in', porduct_ids]];
|
|
|
+ var ProductProduct = new model.web.Model('product.product');
|
|
|
+ ProductProduct.query(fields).filter(domain).all().then(function (results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ // Mesa o delivery
|
|
|
+ fetchPosTable: function(){
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields = ['id','name', 'floor_id'];
|
|
|
+ var ProductProduct = new model.web.Model('pos.table');
|
|
|
+ ProductProduct.query(fields).filter().all().then(function (results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ // Salon o Delivery
|
|
|
+ fetchPosFloor: function(){
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields = ['id','name', 'table_ids'];
|
|
|
+ var ProductProduct = new model.web.Model('pos.floor');
|
|
|
+ ProductProduct.query(fields).filter().all().then(function (results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ getPosOrder: function(id){
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.PosOrder,function (item) {
|
|
|
+ return item.id == id;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ getPosTable: function(id){
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.PosTable,function (item) {
|
|
|
+ return item.id == id;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ getProductProduct: function(id){
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.ProductProduct,function (item) {
|
|
|
+ return item.id == id;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ BuildTable: function(){
|
|
|
+ var self = this;
|
|
|
+ var PosOrderLine = self.PosOrderLine;
|
|
|
+ var data = [];
|
|
|
+ var order;
|
|
|
+ var product;
|
|
|
+ _.each(PosOrderLine, function(item){
|
|
|
+ product = self.getProductProduct(item.product_id[0]);
|
|
|
+ order = self.getPosOrder(item.order_id[0]).shift();
|
|
|
+ var place_id;
|
|
|
+ var place_name;
|
|
|
+ var floor;
|
|
|
+ if(order.table_id){
|
|
|
+ floor = self.getPosTable(order.table_id[0]).shift();
|
|
|
+ floor = floor.floor_id[0];
|
|
|
+ place_id = order.table_id[0];
|
|
|
+ place_name = order.table_id[1];
|
|
|
+ }
|
|
|
+ data.push({
|
|
|
+ id : item.id,
|
|
|
+ order_id : item.order_id[0],
|
|
|
+ order_name : item.order_id[1],
|
|
|
+ date : order.date_order,
|
|
|
+ date_order : moment(order.date_order).format("DD/MM/YYYY"),
|
|
|
+ table_id : self.valorNull(place_id),
|
|
|
+ table_name : self.valorNull(place_name),
|
|
|
+ floor_id : floor,
|
|
|
+ product_id : item.product_id[0],
|
|
|
+ product_name : item.product_id[1],
|
|
|
+ price_unit : accounting.formatNumber(item.price_unit,0,".",","),
|
|
|
+ qty : accounting.formatNumber(item.qty,0,".",","),
|
|
|
+ price_subtotal_incl : accounting.formatNumber(item.price_subtotal_incl,0,".",","),
|
|
|
+ });
|
|
|
+ });
|
|
|
+ self.content = data;
|
|
|
+ self.loadTable(data)
|
|
|
+ },
|
|
|
+
|
|
|
+ fectSearch: function(){
|
|
|
+ var self = this;
|
|
|
+ var floor =this.$el.find('#current-floor').val();
|
|
|
+ var table =this.$el.find('#current-table').val();
|
|
|
+ var desde =this.$el.find('#from').val();
|
|
|
+ var hasta =this.$el.find('#to').val();
|
|
|
+ var product = this.$el.find('#product').val().split('-');
|
|
|
+ var content = self.content;
|
|
|
+
|
|
|
+ // Hoy
|
|
|
+ if ($('#A').is(":checked")){
|
|
|
+ content = _.filter(content, function (inv){
|
|
|
+ return moment(inv.date_order).format("YYYY-MM-DD") == moment().format("YYYY-MM-DD");
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Ayer
|
|
|
+ if ($('#B').is(":checked")){
|
|
|
+ content = _.filter(content, function (inv){
|
|
|
+ return moment(inv.date_order).format("YYYY-MM-DD") == moment().add(-1,"days").format("YYYY-MM-DD");
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Mes Actual
|
|
|
+ if ($('#C').is(":checked")){
|
|
|
+ content = _.filter(content, function (inv){
|
|
|
+ return moment(inv.date_order).format("YYYY-MM") == moment().format("YYYY-MM");
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Mes Pasado
|
|
|
+ if ($('#D').is(":checked")){
|
|
|
+ content = _.filter(content, function (inv){
|
|
|
+ return moment(inv.date_order).format("YYYY-MM") == moment().add(-1,'months').format("YYYY-MM");
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Rango de fechas
|
|
|
+ if ($('#Z').is(":checked")){
|
|
|
+ $('#datepicker').css('display','block');
|
|
|
+ if (desde.length > 0){
|
|
|
+ content = _.filter(content, function (inv){
|
|
|
+ return moment(inv.date_order).format("YYYY-MM-DD") >= moment(desde).format("YYYY-MM-DD");
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (hasta.length > 0){
|
|
|
+ content = _.filter(content, function (inv){
|
|
|
+ return moment(inv.date_order).format("YYYY-MM-DD") <= moment(hasta).format("YYYY-MM-DD");
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $('#datepicker').css('display','none');
|
|
|
+ }
|
|
|
+
|
|
|
+ // Filtrar por piso o delivery
|
|
|
+ if (floor != 9999999){
|
|
|
+ content=_.filter(content, function (inv){
|
|
|
+ return inv.floor_id == floor;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Filtrar por mesa o delivery
|
|
|
+ if (table != 9999999){
|
|
|
+ content=_.filter(content, function (inv){
|
|
|
+ return inv.table_id == table;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Buscar Producto
|
|
|
+ if (product != ""){
|
|
|
+ content = _.filter(content, function(inv){
|
|
|
+ return inv.product_id == product[0];
|
|
|
+ });
|
|
|
+ }
|
|
|
+ self.loadTable(content);
|
|
|
+ },
|
|
|
+
|
|
|
+ searchProduct: function () {
|
|
|
+ var self = this;
|
|
|
+ var results = self.ProductProduct;
|
|
|
+ results = _.map(results, function (item) {
|
|
|
+ return {
|
|
|
+ label: item.id + '- '+ ' [ ' + self.valorNull(item.default_code) + ' - ' + self.valorNull(item.ean13) + ' ] ' + item.name + ' ( ' + self.valorNull(item.attribute_str) + ' ) ' ,
|
|
|
+ value: item.id + '- '+ ' [ ' + self.valorNull(item.default_code) + ' - ' + self.valorNull(item.ean13) + ' ] ' + item.name + ' ( ' + self.valorNull(item.attribute_str) + ' ) '
|
|
|
+ }
|
|
|
+ });
|
|
|
+ self.$('#product').autocomplete({
|
|
|
+ source: results,
|
|
|
+ minLength:0,
|
|
|
+ search: function(event, ui) {
|
|
|
+ if (!(self.$('#product').val())){
|
|
|
+ self.fectSearch();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ close: function( event, ui ) {
|
|
|
+ self.fectSearch();
|
|
|
+ },
|
|
|
+ select: function(event, ui) {
|
|
|
+ self.fectSearch();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ loadTable:function(rowsTable){
|
|
|
+ var self = this;
|
|
|
+ self.rowsData = rowsTable;
|
|
|
+ var table = this.$el.find('#table');
|
|
|
+ table.bootstrapTable('load',rowsTable);
|
|
|
+ },
|
|
|
+
|
|
|
+ clickOnAction: function (e) {
|
|
|
+ var action = this.$el.find(e.target).val();
|
|
|
+ var self = this;
|
|
|
+ var getColumns=[];
|
|
|
+ var rows=[];
|
|
|
+ var table = this.$el.find("#table");
|
|
|
+ var data2 = table.bootstrapTable('getVisibleColumns');
|
|
|
+ if (action === 'pdf') {
|
|
|
+ var dataNEW = _.map(data2, function (val){ return val.field});
|
|
|
+ _.each(this.rowsData,function (item){
|
|
|
+ rows.push(_.pick(item, dataNEW));
|
|
|
+ });
|
|
|
+ // Obtener los nombre de la Cabezera
|
|
|
+ _.each(_.map(data2,function(val){
|
|
|
+ return val}), function(item){
|
|
|
+ getColumns.push([{
|
|
|
+ title: item.title,
|
|
|
+ dataKey: item.field
|
|
|
+ }]);
|
|
|
+ });
|
|
|
+ // Llamar al pdf
|
|
|
+ this.drawPDF(_.flatten(getColumns),rows)
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ drawPDF: function (getColumns,rows) {
|
|
|
+ var self = this;
|
|
|
+ var rowsPdf=[];
|
|
|
+ var desde =(this.$el.find('#from').val());
|
|
|
+ var hasta =(this.$el.find('#to').val());
|
|
|
+ var totalPagesExp = "{total_pages_count_string}";
|
|
|
+ var pdfDoc = new jsPDF();
|
|
|
+
|
|
|
+ // Cantidad
|
|
|
+ var qty =_.reduce(_.map(rows,function(item){
|
|
|
+ var valor=0;
|
|
|
+ if (item.qty){valor = parseFloat(((item.qty.replace(".","")).replace(",",".")))}return valor}), function(memo, num){ return memo + num; },0);
|
|
|
+
|
|
|
+ var price_subtotal_incl =_.reduce(_.map(rows,function(item){
|
|
|
+ var valor =0;
|
|
|
+ if (item.price_subtotal_incl){valor=parseFloat(((item.price_subtotal_incl.replace(".","")).replace(",",".")))} return valor}), function(memo, num){ return memo + num; },0);
|
|
|
+
|
|
|
+ rowsPdf = rows;
|
|
|
+
|
|
|
+ rowsPdf.push({
|
|
|
+ qty : accounting.formatNumber(qty,0,".",","),
|
|
|
+ price_subtotal_incl : accounting.formatNumber(price_subtotal_incl,0,".",","),
|
|
|
+ });
|
|
|
+
|
|
|
+ pdfDoc.autoTable(getColumns, rowsPdf, {
|
|
|
+ styles: { overflow: 'linebreak', fontSize: 8, columnWidth: 'wrap'},
|
|
|
+ columnStyles: {
|
|
|
+ order_name: {halign:'left', fontStyle: 'bold'},
|
|
|
+ date_order :{halign:'left'},
|
|
|
+ table_id :{halign:'left'},
|
|
|
+ product_name :{halign:'left' },
|
|
|
+ price_unit : {halign:'right' },
|
|
|
+ qty : {halign:'right' },
|
|
|
+ price_subtotal_incl : {halign:'right'},
|
|
|
+ },
|
|
|
+ margin: { top: 16, horizontal: 7},
|
|
|
+ addPageContent: function (data) {
|
|
|
+ pdfDoc.setFontSize(12);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
+ pdfDoc.text('Análisis de pedidos (Point Of Sale)', data.settings.margin.left, 10);
|
|
|
+
|
|
|
+ if(desde.length > 0 || hasta.length > 0){
|
|
|
+ var fecha='';
|
|
|
+ if(desde){
|
|
|
+ fecha=fecha.concat(' Desde '+desde);
|
|
|
+ }
|
|
|
+ if (hasta){
|
|
|
+ fecha=fecha.concat(' Hasta '+hasta);
|
|
|
+ }
|
|
|
+ pdfDoc.setFontSize(10);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40)
|
|
|
+ pdfDoc.text(fecha, data.settings.margin.left,14);
|
|
|
+ }
|
|
|
+ // FOOTER
|
|
|
+ var str = "Pagina " + data.pageCount;
|
|
|
+ // Total page number plugin only available in jspdf v1.0+
|
|
|
+ if (typeof pdfDoc.putTotalPages === 'function') {
|
|
|
+ str = str + " de " + totalPagesExp;
|
|
|
+ }
|
|
|
+ pdfDoc.setFontSize(9);
|
|
|
+ pdfDoc.setFontStyle('bold');
|
|
|
+ pdfDoc.setTextColor(40);
|
|
|
+ pdfDoc.text(str, data.settings.margin.left, pdfDoc.internal.pageSize.height - 5);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (typeof pdfDoc.putTotalPages === 'function') {
|
|
|
+ pdfDoc.putTotalPages(totalPagesExp);
|
|
|
+ }
|
|
|
+ pdfDoc.save('Análisis de pedidos.pdf')
|
|
|
+ },
|
|
|
+ });
|
|
|
+}
|