function report_purchase(reporting){
"use strict";
var model = openerp;
reporting.ReportPurchaseWidget = reporting.Base.extend({
template: 'ReportPurchase',
rowsData :[],
content :[],
events:{
'click #generate':'fetchGenerate',
'click .print-report':'clickOnAction',
'change #current-company':'updateSelections',
'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 reporting.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.fetchIntialSQL().then(function (IntialSQL) {
return IntialSQL;
}).then(function(IntialSQL) {
/*
=================================
RES COMPANY
=================================
*/
self.ResCompany = IntialSQL.companies;
self.CompanyLogo = IntialSQL.logo;
if(self.ResCompany.length > 1){
self.$el.find('#current-company').append('');
_.each(self.ResCompany,function(item){
self.$el.find('#current-company').append('');
});
}else{
self.$el.find('.company').css('display','none');
}
/*
=================================
RES STORE
=================================
*/
self.ResStore = IntialSQL.stores;
if(self.ResStore.length > 1){
self.$el.find('#current-store').append('');
_.each(self.ResStore,function(item){
self.$el.find('#current-store').append('');
});
}else{
self.$el.find('.store').css('display','none');
}
/*
=================================
ACCOUNT JOURNAL
=================================
*/
self.AccountJournal = IntialSQL.journals;
if(self.AccountJournal.length > 1){
self.$el.find('#current-journal').append('');
_.each(self.AccountJournal,function(item){
if(item.type == 'sale'){
self.$el.find('#current-journal').append('');
}
});
}else{
self.$el.find('.journal').css('display','none');
}
self.ResUsers = IntialSQL.users;
var store_ids = _.flatten(_.map(self.ResStore, function (item) {
return item.id;
}));
var ResUsers = _.flatten(_.filter(self.ResUsers,function (item) {
return _.contains(store_ids, item.store_id);
}));
if(ResUsers.length > 1){
self.$el.find('#current-user').append('');
_.each(ResUsers,function(item){
self.$el.find('#current-user').append('');
});
}
});
self.$el.find('#generate').css('display','inline');
return;
},
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'
}
});
this.fetchDataSQL().then(function(DataSQL) {
return DataSQL;
}).then(function (DataSQL) {
self.AccountInvoice = DataSQL.invoices;
self.AccountVoucher = DataSQL.vouchers;
return self.BuildTable();
});
},
fetchIntialSQL: function() {
var self = this;
var data = $.get('/report-filter-data');
return data;
},
fetchDataSQL: function() {
var self = this;
var data = $.get('/report-purchase-history');
return data;
},
/*====================================================================
UPDATE SELECTIONS
====================================================================*/
updateSelections: function () {
var self = this;
var store;
var company = self.$el.find('#current-company').val();
if(company != 9999999){
store = self.$el.find('#current-store').empty();
self.$el.find('#current-store').append('');
_.each(self.ResStore,function(item){
if(parseFloat(company) == item.company_id[0]){
self.$el.find('#current-store').append('');
}
});
}else{
store = self.$el.find('#current-store').empty();
self.$el.find('#current-store').append('');
_.each(self.ResStore,function(item){
self.$el.find('#current-store').append('');
});
}
},
getAccountInvoice:function() {
var self = this;
var content = self.AccountInvoice;
var company = self.$el.find('#current-company').val();
var store = self.$el.find('#current-store').val();
var state = self.$el.find('#current-state').val();
var user = self.$el.find('#current-user').val();
var date = self.$el.find('#current-date').val();
var desde = self.$el.find('#from').val();
var hasta = self.$el.find('#to').val();
var store_ids = _.flatten(_.map(self.ResStore, function (item) {
return item.id;
}));
var company_ids = _.flatten(_.map(self.ResCompany, function (item) {
return item.id;
}));
content = _.flatten(_.filter(content,function (item) {
return _.contains(store_ids, item.store_id) && _.contains(company_ids, item.company_id);
}));
if(company && company != 9999999){
content = _.flatten(_.filter(content,function (item) {
return item.company_id == company;
}));
}
if(store && store != 9999999){
content = _.flatten(_.filter(content,function (item) {
return item.store_id == store;
}));
}
if(state && state != 9999999){
content = _.flatten(_.filter(content,function (item) {
return item.state == state;
}));
}
if(user && user != 9999999){
content = _.flatten(_.filter(content,function (item) {
return item.user_id == user;
}));
}
if(date && date != 9999999){
if(date == 'range'){
if(desde){
date = desde.split('/');
date = (date[2]+"-"+date[1]+"-"+date[0]);
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]);
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');
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');
content = _.flatten(_.filter(content,function (inv) {
return moment(inv.date).format('YYYY-MM-DD') === yesterday;
}));
}
if(date == 'currentMonth'){
var currentMonth = moment().format('YYYY-MM');
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');
content = _.flatten(_.filter(content,function (inv) {
return moment(inv.date).format('YYYY-MM') === lastMonth;
}));
}
}
return content;
},
getAccountVoucher: function (number) {
var self = this;
return _.filter(self.AccountVoucher,function (item) {
return item.reference == number;
});
},
BuildTable: function(){
var self = this;
var data = [];
var residual = 0;
var CurrencyBase = self.ResCompany[0].currency_id;
/*
==========================================
ACCOUNT INVOICE
==========================================
*/
var AccountInvoice = self.getAccountInvoice();
console.log(AccountInvoice);
_.each(AccountInvoice, function(item){
residual = 0;
if(item.state == 'open'){
residual = self.getAccountVoucher(item.number);
residual = _.reduce(_.map(residual,function(item){
return item.amount_currency;
}), function(memo, num){
return memo + num;
},0);
residual = item.amount - residual;
}
data.push({
/*=======================
IDS
=======================*/
id:item.invoice_id,
origin:item.origin,
/*=======================
INFO
=======================*/
supplier_invoice_number:self.valorNull(item.supplier_invoice_number),
number:item.number,
date:moment(item.date).format('DD/MM/YYYY'),
user_name:item.user_name,
user_id:item.user_id,
customer_ruc:self.valorNull(item.supplier_ruc),
partner_name:self.valorNull(item.supplier_name),
residual:accounting.formatMoney(residual,'',CurrencyBase.decimal_places,CurrencyBase.thousands_separator,CurrencyBase.decimal_separator),
untaxed:accounting.formatMoney(item.amount - item.amount_tax,'',CurrencyBase.decimal_places,CurrencyBase.thousands_separator,CurrencyBase.decimal_separator),
tax:accounting.formatMoney(item.amount_tax,'',CurrencyBase.decimal_places,CurrencyBase.thousands_separator,CurrencyBase.decimal_separator),
total:accounting.formatMoney(item.amount,'',CurrencyBase.decimal_places,CurrencyBase.thousands_separator,CurrencyBase.decimal_separator),
/*=======================
VALORES SIN FORMATEAR
=======================*/
date_no_format: moment(item.date).format('YYYY-MM-DD'),
residual_no_format:residual,
untaxed_no_format:item.amount - item.amount_tax,
tax_no_format:item.amount_tax,
total_no_format:item.amount,
/*==============================
TOTAL FOOTER CONFIGURATION
==============================*/
decimal_places:CurrencyBase.decimal_places,
thousands_separator:CurrencyBase.thousands_separator,
decimal_separator:CurrencyBase.decimal_separator,
});
});
data.sort(function (a, b) {
if (a.date_no_format > b.date_no_format) {
return -1;
}
if (a.date_no_format < b.date_no_format) {
return 1;
}
return 0;
});
self.content = data;
self.loadTable(data);
self.$el.find('.report-form').css('display','block');
self.$el.find('.search-form').unblock();
self.$el.find('.report-form').unblock();
},
loadTable:function(rowsTable){
var self = this;
self.rowsData = rowsTable;
var table = this.$el.find('#table');
table.bootstrapTable('load', rowsTable);
},
clickOnAction: function (e) {
var self = this;
var ResCompany;
var CurrencyBase;
var action = this.$el.find(e.target).val();
var company = $('#current-company').val();
if(company && company != 9999999){
ResCompany = _.flatten(_.filter(self.CompanyLogo,function (inv) {
return inv.id == company;
}));
ResCompany = ResCompany[0];
CurrencyBase = ResCompany[0].currency_id;
}else{
ResCompany = self.CompanyLogo[0];
CurrencyBase = self.ResCompany[0].currency_id;
}
var getColumns=[];
var rows=[];
var table = this.$el.find("#table");
var column = table.bootstrapTable('getVisibleColumns');
var row = table.bootstrapTable('getData');
var residual = ResidualFooter(row);
var untaxed = UntaxedFooter(row);
var tax = TaxFooter(row);
var total = TotalFooter(row);
row.push({
number:'Totales',
residual:residual,
untaxed:untaxed,
tax:tax,
total:total,
});
if (action === 'pdf') {
var data = _.map(column, function (val){
return val.field;
});
_.each(_.map(column,function(val){
return val;
}), function(item){
getColumns.push([{
title: item.title,
dataKey: item.field
}]);
});
/*
============================================================
CONFIGURACION DEL PDF
============================================================
*/
var pdf_title = 'Facturas de Compra.';
var pdf_type = '';
var pdf_name = 'facturas_de_compra_';
var pdf_columnStyles = {
supplier_invoice_number:{columnWidth: 15,halign:'left'},
number:{columnWidth: 30,halign:'left'},
origin:{columnWidth: 15, halign:'center'},
date_invoice:{halign:'center'},
user_name:{ halign:'left'},
customer_ruc:{columnWidth:12, halign:'left'},
partner_name:{columnWidth: 30, halign:'left'},
residual:{columnWidth: 20, halign:'right'},
untaxed:{columnWidth: 20, halign:'right'},
tax:{columnWidth: 20, halign:'right'},
total:{columnWidth: 20, halign:'right'},
};
/*
============================================================
LLAMAR FUNCION DE IMPRESION
============================================================
*/
var filter = self.getFilter();
var pdf = new reporting.ReportPdfWidget(self);
pdf.drawPDF(
_.flatten(getColumns),
row,
ResCompany,
pdf_title,
pdf_type,
pdf_name,
pdf_columnStyles,
filter
);
}
},
getFilter: function(){
var self = this;
var company = self.$el.find('#current-company').val();
var store = self.$el.find('#current-store').val();
var state = self.$el.find('#current-state').val();
var date = self.$el.find('#current-date').val();
var desde = self.$el.find('#from').val();
var hasta = self.$el.find('#to').val();
var filter = [];
if(company && company != 9999999){
var ResCompany = _.filter(self.ResCompany, function(item){
return item.id == company;
});
filter.push({
title:'Empresa',
value: ResCompany[0].name,
});
}
if(store && store != 9999999){
var ResStore = _.filter(self.ResStore,function (item) {
return item.id == store;
});
filter.push({
title: 'Sucursal',
value: ResStore[0].name,
});
}
if(state && state != 9999999){
filter.push({
title: 'Estado',
value: $('#current-state option:selected').text(),
});
}
if(date && date != 9999999){
moment.locale('es', {
months: 'Enero_Febrero_Marzo_Abril_Mayo_Junio_Julio_Agosto_Septiembre_Octubre_Noviembre_Diciembre'.split('_'),
});
if(date == 'range'){
filter.push({
title: 'Fecha',
value: desde +' al '+hasta,
});
}else {
var fecha;
if(date == 'today'){
fecha = moment().format('DD/MM/YYYY');
}
if(date == 'yesterday'){
fecha = moment().add(-1,'days').format('DD/MM/YYYY');
}
if(date == 'currentMonth'){
fecha = moment().format('MMMM/YYYY');
}
if(date == 'lastMonth'){
fecha = moment().add(-1,'months').format('MMMM/YYYY');
}
filter.push({
title: 'Fecha',
value: fecha,
});
}
}
return filter;
},
});
}