0

I have list of objects (invoices) that have few fields in it. Name, date, description and amountPreTax and amountNet.

In view above the table with all invoices I have fields to filter the list. Search by term, set date 'date to' set date 'date from'.

Under that table I have visible sum of amountPreTax and sum amountNet of all visible invoices.

After I'll filter the list putting some text into search filed or setting 'date to' or 'date from' and list is getting shorter, I want fields of sum updated, so they are sum of only visible invoices.

As far as I know, I need to create one filter for filtering invoices and another for calculating the sum - but I don't know how to write that down and if it's right way to do :)

..

Ok, so far these are my filters (I know, I need to join them as one):

For search term:

import { Injectable, Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'invoicesFilter',
    pure: false
})
@Injectable()
export class FilterInvoices implements PipeTransform {
    transform(invoices: any[], term: any): any {
        if (invoices && term !== undefined) {
            return invoices.filter((invoice) => {
                return (
                    invoice.number.toLowerCase().indexOf(term.toLowerCase()) !== -1 ||
                    invoice.company.toLowerCase().indexOf(term.toLowerCase()) !== -1 ||
                    invoice.data.toLowerCase().indexOf(term.toLowerCase()) !== -1
                );
            });
        }
        return invoices;
    }
}

For 'date from':

import { Injectable, Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'dateFrom',
    pure: false
})
@Injectable()
export class FilterDatesFrom implements PipeTransform {
    transform(invoices: any[], date1: any, whichDates: number): any {
        var isDate1 = /(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d/.test(date1);
        if (invoices && isDate1) {
            date1 = date1 + '';
            date1 = date1.split('.');
            date1[0]=parseInt(date1[0]);
            date1[1]=parseInt(date1[1]);
            date1[2]=parseInt(date1[2]);
            return invoices.filter((invoice) => {
                var passDate1 = false;
                var passDate2 = false;
                var date3 = invoice.data;
                var date4 = invoice.datePost;
                date3 = date3.split('.');
                date3[0] = parseInt(date3[0]);
                date3[1] = parseInt(date3[1]);
                date3[2] = parseInt(date3[2]);
                date4 = date4.split('.');
                date4[0] = parseInt(date4[0]);
                date4[1] = parseInt(date4[1]);
                date4[2] = parseInt(date4[2]);
                if(whichDates == 2 || whichDates == 0){
                    if(date1[2]<date4[2]){
                        return invoice;
                    }
                    else if(date1[2]==date4[2])
                    {
                        if(date1[1]<date4[1])
                        {
                            return invoice;
                        }
                        else if(date1[1]==date4[1])
                        {
                            if(date1[0]<=date4[0]){
                                return invoice;
                            }
                        }
                    }
                }
                else if(whichDates == 1 || whichDates == 0){
                    if(date1[2]<date3[2]){
                        return invoice;
                    }
                    else if(date1[2]==date3[2])
                    {
                        if(date1[1]<date3[1])
                        {
                            return invoice;
                        }
                        else if(date1[1]==date3[1])
                        {
                            if(date1[0]<=date3[0]){
                                return invoice;
                            }
                        }
                    }
                }
            });
        }
        return invoices;
    }
}

And 'dates to'

import { Injectable, Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'dateTo',
    pure: false
})
@Injectable()
export class FilterDatesTo implements PipeTransform {
    transform(invoices: any[], date1: any, whichDates: any): any {
        var isDate1 = /(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d/.test(date1);
        if (invoices && isDate1) {
            date1 = date1 + '';
            date1 = date1.split('.');
            date1[0]=parseInt(date1[0]);
            date1[1]=parseInt(date1[1]);
            date1[2]=parseInt(date1[2]);
            return invoices.filter((invoice) => {
                var passDate1 = false;
                var passDate2 = false;
                var date3 = invoice.data;
                var date4 = invoice.datePost;
                date3 = date3.split('.');
                date3[0] = parseInt(date3[0]);
                date3[1] = parseInt(date3[1]);
                date3[2] = parseInt(date3[2]);
                date4 = date4.split('.');
                date4[0] = parseInt(date4[0]);
                date4[1] = parseInt(date4[1]);
                date4[2] = parseInt(date4[2]);
                if(whichDates == 2 || whichDates == 0){
                    if(date1[2]>date4[2]){
                        return invoice;
                    }
                    else if(date1[2]==date4[2])
                    {
                        if(date1[1]>date4[1])
                        {
                            return invoice;
                        }
                        else if(date1[1]==date4[1])
                        {
                            if(date1[0]>=date4[0]){
                                return invoice;
                            }
                        }
                    }
                }
                if(whichDates == 1 || whichDates == 0){
                    if(date1[2]>date3[2]){
                        return invoice;
                    }
                    else if(date1[2]==date3[2])
                    {
                        if(date1[1]>date3[1])
                        {
                            return invoice;
                        }
                        else if(date1[1]==date3[1])
                        {
                            if(date1[0]>=date3[0]){
                                return invoice;
                            }
                        }
                    }
                }
            });
        }
        return invoices;
    }
}

This is typical object (invoice) structure:

{
    "_id": {
        "$oid": "588b2f345c5948001162fc15"
    },
    "number": "FV/004/12/16",
    "data": "12.12.2016",
    "datePost": "27.01.2017",
    "amountPreTax": 5347.43,
    "amountNet": 4347.5,
    "company": "Stacato Introligatorstwo Małgorzata Tomaszewska",
    "hasTax": "false",
    "orderId": "588a060ebe646d0011df997e",
    "orderNum": "00003",
    "clientNum": "010",
    "clientName": "Eurocasha",
    "isRevenue": false,
    "paid": true,
    "created": "2017-01-27T11:29:50.864Z",
    "file": null
}

And I need to do two sums: "amountPreTax" and another for "amountNet".

Here is part of my html view:

<tr *ngFor="
let invoice of invoices |
invoicesFilter:term |
dateFrom: date1:whichDates |
dateTo: date2:whichDates">

inside this table i have iterated which displays data of each invoice.

Under that table I need to display sum of amountPreTax and amountNet so I could display it like

{{sumNet | number : '1.2-2'}} zł
{{sumPreTax | number : '1.2-2'}} zł
4
  • 1
    so we have three different filters (search by term, date from, date to).... Well, first, instead of three pipes (search, date from and date to), I'd prefer a single filter with 3 (nullables) parameters and another one to sum the filtered values. Share some code, as piece of your data, so it'll be easier to help you. Commented Feb 9, 2017 at 3:17
  • You are right. I was thinking about it, but I wosn't sure how to implement this. In another post below I placed code. Commented Feb 9, 2017 at 3:44
  • I have edited my first post and added code. Commented Feb 9, 2017 at 3:48
  • For a sum pipe - take a look at this answer I posted recently stackoverflow.com/a/42164971/6806381 Commented Feb 11, 2017 at 10:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.