123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- 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('<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');
- }
- /*
- =================================
- RES STORE
- =================================
- */
- self.ResStore = IntialSQL.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');
- }
- /*
- =================================
- ACCOUNT JOURNAL
- =================================
- */
- self.AccountJournal = IntialSQL.journals;
- if(self.AccountJournal.length > 1){
- self.$el.find('#current-journal').append('<option value="9999999">Todas las facturas</option>');
- _.each(self.AccountJournal,function(item){
- if(item.type == 'sale'){
- self.$el.find('#current-journal').append('<option value="' + item.id + '">' + item.name + '</option>');
- }
- });
- }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('<option value="9999999">Todos los responsables</option>');
- _.each(ResUsers,function(item){
- self.$el.find('#current-user').append('<option value="' + item.id + '">' + item.name + '</option>');
- });
- }
- });
- 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('<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 = 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>');
- });
- }
- },
- 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;
- },
- });
- }
|