|
@@ -0,0 +1,480 @@
|
|
|
+function report_expense_comparative(reporting){
|
|
|
+ "use strict";
|
|
|
+
|
|
|
+ var model = openerp;
|
|
|
+
|
|
|
+ reporting.ReportExpenseComparativeWidget = reporting.Base.extend({
|
|
|
+ template: 'ReportExpenseComparative',
|
|
|
+ rowsData :[],
|
|
|
+ content :[],
|
|
|
+
|
|
|
+ events:{
|
|
|
+ 'click #generate' : 'fetchGenerate',
|
|
|
+ 'change #current-date' : 'ShowDateRange',
|
|
|
+ },
|
|
|
+
|
|
|
+ init : function(parent){
|
|
|
+ this._super(parent);
|
|
|
+ },
|
|
|
+
|
|
|
+ start: function () {
|
|
|
+ var table = this.$el.find('#table');
|
|
|
+ table.bootstrapTable({data : self.rowsData});
|
|
|
+ var date = new model.eiru_reports.ReportDatePickerWidget(self);
|
|
|
+ date.fecthFecha();
|
|
|
+ this.fetchInitial();
|
|
|
+ },
|
|
|
+
|
|
|
+ valorNull:function(dato){
|
|
|
+ var valor = "";
|
|
|
+ if (dato){
|
|
|
+ valor = dato;
|
|
|
+ }
|
|
|
+ return valor;
|
|
|
+ },
|
|
|
+
|
|
|
+ ShowDateRange : function(){
|
|
|
+ var self = this;
|
|
|
+ var date = self.$el.find('#current-date').val();
|
|
|
+ if(date == 'range'){
|
|
|
+ self.$el.find('.datepicker').css('display','block');
|
|
|
+ }
|
|
|
+ if(date != 'range'){
|
|
|
+ self.$el.find('.datepicker').css('display','none');
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchInitial: function () {
|
|
|
+ var self = this;
|
|
|
+ self.fetchDataSQL().then(function (DataSQL) {
|
|
|
+ return DataSQL;
|
|
|
+ }).then(function(DataSQL) {
|
|
|
+ self.AccountInvoiceLine = DataSQL.invoice_lines;
|
|
|
+ self.ProductCategory = DataSQL.categories;
|
|
|
+ self.ResCompany = DataSQL.companies;
|
|
|
+ if(self.ResCompany.length > 1){
|
|
|
+ self.$el.find('#current-company').append('<option value="9999999">Todas las empresas</option>');
|
|
|
+ _.each(self.ResCompany,function(item){
|
|
|
+ self.$el.find('#current-company').append('<option value="' + item.id + '">' + item.name + '</option>');
|
|
|
+ });
|
|
|
+ }else{
|
|
|
+ self.$el.find('.company').css('display','none');
|
|
|
+ }
|
|
|
+
|
|
|
+ self.ResStore = DataSQL.stores;
|
|
|
+ if(self.ResStore.length > 1){
|
|
|
+ self.$el.find('#current-store').append('<option value="9999999">Todas las sucursales</option>');
|
|
|
+ _.each(self.ResStore,function(item){
|
|
|
+ self.$el.find('#current-store').append('<option value="' + item.id + '">' + item.name + '</option>');
|
|
|
+ });
|
|
|
+ }else{
|
|
|
+ self.$el.find('.store').css('display','none');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ self.$el.find('#generate').css('display','inline');
|
|
|
+ return;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchDataSQL: function() {
|
|
|
+ var self = this;
|
|
|
+ var data = $.get('/reports-agroyguazu-expense');
|
|
|
+ return data;
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchGenerate: function () {
|
|
|
+ var self = this;
|
|
|
+ self.$el.find('.search-form').block({
|
|
|
+ message: null,
|
|
|
+ overlayCSS: {
|
|
|
+ backgroundColor: '#FAFAFA'
|
|
|
+ }
|
|
|
+ });
|
|
|
+ self.$el.find('.report-form').block({
|
|
|
+ message: null,
|
|
|
+ overlayCSS: {
|
|
|
+ backgroundColor: '#FAFAFA'
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return self.BuildTable();
|
|
|
+ },
|
|
|
+
|
|
|
+ /*====================================================================
|
|
|
+ UPDATE SELECTIONS
|
|
|
+ ====================================================================*/
|
|
|
+ updateSelections: function () {
|
|
|
+ var self = this;
|
|
|
+ var store = '';
|
|
|
+ var company = self.$el.find('#current-company').val();
|
|
|
+ if(company != 9999999){
|
|
|
+ /*===================
|
|
|
+ STORE SELECTION
|
|
|
+ ===================*/
|
|
|
+ store = self.$el.find('#current-store').empty();
|
|
|
+ self.$el.find('#current-store').append('<option value="9999999">Todas las sucursales</option>');
|
|
|
+ _.each(self.ResStore,function(item){
|
|
|
+ if(parseFloat(company) == item.company_id[0]){
|
|
|
+ self.$el.find('#current-store').append('<option value="' + item.id + '">' + item.name + '</option>');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }else{
|
|
|
+ /*===================
|
|
|
+ STORE SELECTION
|
|
|
+ ===================*/
|
|
|
+ store = self.$el.find('#current-store').empty();
|
|
|
+ self.$el.find('#current-store').append('<option value="9999999">Todas las sucursales</option>');
|
|
|
+ _.each(self.ResStore,function(item){
|
|
|
+ self.$el.find('#current-store').append('<option value="' + item.id + '">' + item.name + '</option>');
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ getAccountInvoiceLine: function (categ_id) {
|
|
|
+ var self = this;
|
|
|
+ var content = self.AccountInvoiceLine;
|
|
|
+ var date = self.$el.find('#current-date').val();
|
|
|
+ var desde = self.$el.find('#from').val();
|
|
|
+ var hasta = self.$el.find('#to').val();
|
|
|
+ var store = self.$el.find('#current-store').val();
|
|
|
+ var data = [];
|
|
|
+ var start = '';
|
|
|
+ var finish = '';
|
|
|
+ if(store && store != 9999999){
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return inv.store_id == store;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ if(date != 9999999){
|
|
|
+ if(date == 'range'){
|
|
|
+ if(desde){
|
|
|
+ date = desde.split('/');
|
|
|
+ date = (date[2]+"-"+date[1]+"-"+date[0]);
|
|
|
+ start = moment(date).format('YYYY-MM');
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM-DD') >= date;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ if(hasta){
|
|
|
+ date = hasta.split('/');
|
|
|
+ date = (date[2]+"-"+date[1]+"-"+date[0]);
|
|
|
+ finish = moment(date).format('YYYY-MM');
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM-DD') <= date;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(date == 'today'){
|
|
|
+ var today = moment().format('YYYY-MM-DD');
|
|
|
+ start = today;
|
|
|
+ finish = today;
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM-DD') == today;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ if(date == 'yesterday'){
|
|
|
+ var yesterday = moment().add(-1,'days').format('YYYY-MM-DD');
|
|
|
+ start = yesterday;
|
|
|
+ finish = yesterday;
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM-DD') == yesterday;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ if(date == 'currentMonth'){
|
|
|
+ var currentMonth = moment().format('YYYY-MM');
|
|
|
+ start = currentMonth;
|
|
|
+ finish = currentMonth;
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM') == currentMonth;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ if(date == 'lastMonth'){
|
|
|
+ var lastMonth = moment().add(-1,'months').format('YYYY-MM');
|
|
|
+ start = lastMonth;
|
|
|
+ finish = lastMonth;
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM') == lastMonth;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ content.sort(function (a, b) {
|
|
|
+ if (a.date > b.date) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ if (a.date < b.date) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ });
|
|
|
+
|
|
|
+ // console.log(content);
|
|
|
+
|
|
|
+ if(content.length > 0){
|
|
|
+ if(date == 9999999){
|
|
|
+ start = moment(content[0].date).format('YYYY-MM');
|
|
|
+ finish = moment(content[content.length-1].date).format('YYYY-MM');
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!desde){
|
|
|
+ start = moment(content[0].date).format('YYYY-MM');
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!hasta){
|
|
|
+ finish = moment(content[content.length -1].date).format('YYYY-MM');
|
|
|
+ }
|
|
|
+
|
|
|
+ var total = 0;
|
|
|
+ for (var i = 0; i < 12; i++) {
|
|
|
+ total = _.reduce(_.map(content,function(item) {
|
|
|
+ if(item.categ_id == categ_id && moment(item.date).format('YYYY-MM') == start){
|
|
|
+ return item.quantity * item.price_unit;
|
|
|
+ }else{
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ data.push({
|
|
|
+ 'date': start,
|
|
|
+ 'amount': total
|
|
|
+ });
|
|
|
+ start = moment(start).add(1,'months').format('YYYY-MM');
|
|
|
+ if(start > finish){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return data;
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ getColumns: function(){
|
|
|
+ var self = this;
|
|
|
+ var columns = [];
|
|
|
+ columns.push({
|
|
|
+ field: 'name',
|
|
|
+ title: 'Descripcion',
|
|
|
+ footerFormatter: 'Totales',
|
|
|
+ });
|
|
|
+
|
|
|
+ moment.locale('es', {
|
|
|
+ months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'),
|
|
|
+ });
|
|
|
+
|
|
|
+ var content = self.AccountInvoiceLine;
|
|
|
+ var date = self.$el.find('#current-date').val();
|
|
|
+ var desde = self.$el.find('#from').val();
|
|
|
+ var hasta = self.$el.find('#to').val();
|
|
|
+ var store = self.$el.find('#current-store').val();
|
|
|
+ var data = [];
|
|
|
+ var start = '';
|
|
|
+ var finish = '';
|
|
|
+ if(store && store != 9999999){
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return inv.store_id == store;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ if(date != 9999999){
|
|
|
+ if(date == 'range'){
|
|
|
+ if(desde){
|
|
|
+ date = desde.split('/');
|
|
|
+ date = (date[2]+"-"+date[1]+"-"+date[0]);
|
|
|
+ start = date;
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM-DD') >= date;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ if(hasta){
|
|
|
+ date = hasta.split('/');
|
|
|
+ date = (date[2]+"-"+date[1]+"-"+date[0]);
|
|
|
+ finish = date;
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM-DD') <= date;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(date == 'today'){
|
|
|
+ var today = moment().format('YYYY-MM-DD');
|
|
|
+ start = today;
|
|
|
+ finish = today;
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM-DD') == today;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ if(date == 'yesterday'){
|
|
|
+ var yesterday = moment().add(-1,'days').format('YYYY-MM-DD');
|
|
|
+ start = yesterday;
|
|
|
+ finish = yesterday;
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM-DD') == yesterday;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ if(date == 'currentMonth'){
|
|
|
+ var currentMonth = moment().format('YYYY-MM');
|
|
|
+ start = currentMonth;
|
|
|
+ finish = currentMonth;
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM') == currentMonth;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ if(date == 'lastMonth'){
|
|
|
+ var lastMonth = moment().add(-1,'months').format('YYYY-MM');
|
|
|
+ start = lastMonth;
|
|
|
+ finish = lastMonth;
|
|
|
+ content = _.flatten(_.filter(content,function (inv){
|
|
|
+ return moment(inv.date).format('YYYY-MM') == lastMonth;
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ content.sort(function (a, b) {
|
|
|
+ if (a.date > b.date) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ if (a.date < b.date) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ if(content.length > 0){
|
|
|
+ if(date == 9999999){
|
|
|
+ start = moment(content[0].date).format('YYYY-MM');
|
|
|
+ finish = moment(content[content.length-1].date).format('YYYY-MM');
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!desde){
|
|
|
+ start = moment(content[0].date).format('YYYY-MM');
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!hasta){
|
|
|
+ finish = moment(content[content.length -1].date).format('YYYY-MM');
|
|
|
+ }
|
|
|
+
|
|
|
+ for (var i = 0; i < 12; i++) {
|
|
|
+ var column_name = start;
|
|
|
+ columns.push({
|
|
|
+ field: moment(start).format('MMMM'),
|
|
|
+ title: moment(start).format('MMMM/YY'),
|
|
|
+ align: 'right',
|
|
|
+ footerFormatter: totalFormatter,
|
|
|
+ });
|
|
|
+ start = moment(start).add(1,'months').format('YYYY-MM');
|
|
|
+ if(start > finish){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ columns.push({
|
|
|
+ field: 'Total',
|
|
|
+ title: 'Totales',
|
|
|
+ align: 'right',
|
|
|
+ cellStyle: columnStyle,
|
|
|
+ footerFormatter: totalFormatter,
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ return columns;
|
|
|
+ },
|
|
|
+
|
|
|
+ BuildTable: function(){
|
|
|
+ var self = this;
|
|
|
+ console.log(self);
|
|
|
+ var data = [];
|
|
|
+ var columns = self.getColumns();
|
|
|
+ var AccountInvoiceLine = 0;
|
|
|
+ var CurrencyBase = self.ResCompany[0].currency_id;
|
|
|
+ _.each(self.ProductCategory,function(item) {
|
|
|
+ AccountInvoiceLine = self.getAccountInvoiceLine(item.id);
|
|
|
+ data.push({
|
|
|
+ 'name': item.name,
|
|
|
+ 'symbol': CurrencyBase.symbol,
|
|
|
+ 'decimal_places': CurrencyBase.decimal_places,
|
|
|
+ 'thousands_separator': CurrencyBase.thousands_separator,
|
|
|
+ 'decimal_separator': CurrencyBase.decimal_separator,
|
|
|
+ });
|
|
|
+ _.each(AccountInvoiceLine,function(index) {
|
|
|
+ if(moment(index.date).format('MM') == '01'){
|
|
|
+ data[data.length -1].Enero = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].enero = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '02'){
|
|
|
+ data[data.length -1].Febrero = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].febrero = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '03'){
|
|
|
+ data[data.length -1].Marzo = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].marzo = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '04'){
|
|
|
+ data[data.length -1].Abril = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].abril = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '05'){
|
|
|
+ data[data.length -1].Mayo = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].mayo = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '06'){
|
|
|
+ data[data.length -1].Junio = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].junio = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '07'){
|
|
|
+ data[data.length -1].Julio = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].julio = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '08'){
|
|
|
+ data[data.length -1].Agosto = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].agosto = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '09'){
|
|
|
+ data[data.length -1].Septiembre = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].septiembre = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '10'){
|
|
|
+ data[data.length -1].Octubre = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].octubre = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '11'){
|
|
|
+ data[data.length -1].Noviembre = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].noviembre = (index.amount);
|
|
|
+ }
|
|
|
+ if(moment(index.date).format('MM') == '12'){
|
|
|
+ data[data.length -1].Diciembre = (accounting.formatMoney(index.amount, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].diciembre = (index.amount);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ var total = _.reduce(_.map(AccountInvoiceLine,function(inv) {
|
|
|
+ return inv.amount;
|
|
|
+ }),function(memo, num) {
|
|
|
+ return memo + num;
|
|
|
+ },0);
|
|
|
+ data[data.length -1].Total = (accounting.formatMoney(total, '', CurrencyBase.decimal_places, CurrencyBase.thousands_separator, CurrencyBase.decimal_separator));
|
|
|
+ data[data.length -1].total = total;
|
|
|
+ });
|
|
|
+ // data.push({
|
|
|
+ // 'symbol': CurrencyBase.symbol,
|
|
|
+ // 'decimal_places': CurrencyBase.decimal_places,
|
|
|
+ // 'thousands_separator': CurrencyBase.thousands_separator,
|
|
|
+ // 'decimal_separator': CurrencyBase.decimal_separator,
|
|
|
+ // });
|
|
|
+ self.content = data;
|
|
|
+ self.loadTable(data,columns);
|
|
|
+ self.$el.find('.report-form').css('display','block');
|
|
|
+ self.$el.find('.search-form').unblock();
|
|
|
+ self.$el.find('.report-form').unblock();
|
|
|
+ },
|
|
|
+
|
|
|
+ loadTable:function(data,columns){
|
|
|
+ var self = this;
|
|
|
+ var table = this.$el.find('#table');
|
|
|
+ table.bootstrapTable('destroy');
|
|
|
+ table.bootstrapTable({
|
|
|
+ data: data,
|
|
|
+ columns: columns,
|
|
|
+ });
|
|
|
+ table.bootstrapTable('load', data);
|
|
|
+ },
|
|
|
+
|
|
|
+ });
|
|
|
+}
|