Home > Software design >  How do I create a bar chart of the last 12 months, broken down by month?
How do I create a bar chart of the last 12 months, broken down by month?

Time:03-04

I have a CRM:

There is a customer base -> Clients

Employees database -> Employee

Operations database -> Operation

A database for connecting a client to an employee -> AssignEmployee

I need to make a "bar chart" divided into months, over the last 12 months, starting from this month, and that the bar chart, shows 1 bar = 1 month, and from it counts the result of all operations assigned to 1 employee, and displays this on the chart.

E.g. that in January, employee 1 - did $10,000 worth of operations with clients.

Currently I have made a similar chart, but it takes up much too much space.

I need to make a "bar chart" which will show the last 12 months, the score of 1 employee, 1 bar shows the score of 1 employee, how many operations he had by amount. Do I have to make this chart as in the view.py code I have below, or is there any other option?

Script:

var chart = new ApexCharts(
    document.querySelector('#chart-office-total'), {
        chart: { height: 350, type: 'bar'},
        title: { text: 'Total net income of the office for the last 12 months', align: 'center' },
        plotOptions: {
            bar: { horizontal: false, borderRadius: 30, columnWidth: '55%', endingShape: 'rounded', decimal: true}
        },
        dataLabels: {enabled: false},
        stroke: { show: true, width: 2, colors: ['transparent'] },
        colors: [ "#3478F6"],
        series: [
            { name: 'Total netto', data: {{dataTotal|safe}} }
        ],
        xaxis: {
            categories: {{values|safe}},
            axisBorder: { show: true, height: 1, width: '100%', offsetX: 0, offsetY: -1},
            axisTicks: {show: true, borderType: 'solid', height: 6, offsetX: 0, offsetY: 0}
        },
        yaxis: {
            title: {text: 'Score in €' }
        },
        fill: { opacity: 1 },
        tooltip: {
            y: {formatter: function(val) {return val   " €"} }
        }
    }
);
    chart.render();    

Views.py

def office(request, office_id):
    office = Office.objects.get(pk=office_id)

    now = datetime.date.today()
    today = now.replace(day=1)

    start_this_month = now.replace(day=1)

    end_history_1 = today - relativedelta(days=1)
    start_history1 = end_history_1.replace(day=1)

    end_history_2 = today - relativedelta(days=1, months=1)
    start_history2 = end_history_2.replace(day=1)

    end_history_3 = today - relativedelta(days=1, months=2)
    start_history3 = end_history_3.replace(day=1)

    end_history_4 = today - relativedelta(days=1, months=3)
    start_history4 = end_history_4.replace(day=1)

    end_history_5 = today - relativedelta(days=1, months=4)
    start_history5 = end_history_5.replace(day=1)

    end_history_6 = today - relativedelta(days=1, months=5)
    start_history6 = end_history_6.replace(day=1)

    end_history_7 = today - relativedelta(days=1, months=6)
    start_history7 = end_history_7.replace(day=1)

    end_history_8 = today - relativedelta(days=1, months=7)
    start_history8 = end_history_8.replace(day=1)

    end_history_9 = today - relativedelta(days=1, months=8)
    start_history9 = end_history_9.replace(day=1)

    end_history_10 = today - relativedelta(days=1, months=9)
    start_history10 = end_history_10.replace(day=1)

    end_history_11 = today - relativedelta(days=1, months=10)
    start_history11 = end_history_11.replace(day=1)

    values = []
    dataTotal = []
    dataFtd = []
    dataWd = []

    if Operation.objects.filter(client__assign_client__office=office_id).filter(status_id=5).filter(
            type_id=4) is not None:
        ftd = Operation.objects.filter(client__assign_client__office=office_id).filter(status_id=5).filter(type_id=4)
    else:
        ftd = 0

    if Operation.objects.filter(client__assign_client__office=office_id).filter(status_id=5) is not None:
        total = Operation.objects.filter(client__assign_client__office=office_id).filter(status_id=5)
    else:
        total = 0

    if Operation.objects.filter(client__assign_client__office=office_id).filter(status_id=5).filter(
            type_id=3) is not None:
        wd = Operation.objects.filter(client__assign_client__office=office_id).filter(status_id=5).filter(type_id=3)
    else:
        wd = 0

    total_1 = total.filter(date__range=(start_this_month, now)).aggregate(Sum('cash')).get('cash__sum')
    wd_1 = wd.filter(date__range=(start_this_month, now)).aggregate(Sum('cash')).get('cash__sum')
    ftd_1 = ftd.filter(date__range=(start_this_month, now)).aggregate(Sum('cash')).get('cash__sum')

    if total_1 is None: total_1 = 0
    if wd_1 is None: wd_1 = 0
    if ftd_1 is None: ftd_1 = 0

    netto_1 = total_1 - (2 * wd_1)

    values.append(today.strftime("%B"))
    dataTotal.append(int(netto_1))
    dataFtd.append(int(ftd_1))
    dataWd.append(int(wd_1))

    total_2 = total.filter(date__range=(start_history1, end_history_1)).aggregate(Sum('cash')).get('cash__sum')
    wd_2 = wd.filter(date__range=(start_history1, end_history_1)).aggregate(Sum('cash')).get('cash__sum')
    ftd_2 = ftd.filter(date__range=(start_history1, end_history_1)).aggregate(Sum('cash')).get('cash__sum')
    if total_2 is None: total_2 = 0
    if wd_2 is None: wd_2 = 0
    if ftd_2 is None: ftd_2 = 0
    netto_2 = total_2 - (2 * wd_2)
    values.append(end_history_1.strftime("%B"))
    dataTotal.append(int(netto_2))
    dataFtd.append(int(ftd_2))
    dataWd.append(int(wd_2))

    total_3 = total.filter(date__range=(start_history2, end_history_2)).aggregate(Sum('cash')).get('cash__sum')
    wd_3 = wd.filter(date__range=(start_history2, end_history_2)).aggregate(Sum('cash')).get('cash__sum')
    ftd_3 = ftd.filter(date__range=(start_history2, end_history_2)).aggregate(Sum('cash')).get('cash__sum')
    if total_3 is None: total_3 = 0
    if wd_3 is None: wd_3 = 0
    if ftd_3 is None: ftd_3 = 0
    netto_3 = total_3 - (2 * wd_3)
    values.append(end_history_2.strftime("%B"))
    dataTotal.append(int(netto_3))
    dataFtd.append(int(ftd_3))
    dataWd.append(int(wd_3))

    total_4 = total.filter(date__range=(start_history3, end_history_3)).aggregate(Sum('cash')).get('cash__sum')
    wd_4 = wd.filter(date__range=(start_history3, end_history_3)).aggregate(Sum('cash')).get('cash__sum')
    ftd_4 = ftd.filter(date__range=(start_history3, end_history_3)).aggregate(Sum('cash')).get('cash__sum')
    if total_4 is None: total_4 = 0
    if wd_4 is None: wd_4 = 0
    if ftd_4 is None: ftd_4 = 0
    netto_4 = total_4 - (2 * wd_4)
    values.append(end_history_3.strftime("%B"))
    dataTotal.append(int(netto_4))
    dataFtd.append(int(ftd_4))
    dataWd.append(int(wd_4))

    total_5 = total.filter(date__range=(start_history4, end_history_4)).aggregate(Sum('cash')).get('cash__sum')
    wd_5 = wd.filter(date__range=(start_history4, end_history_4)).aggregate(Sum('cash')).get('cash__sum')
    ftd_5 = ftd.filter(date__range=(start_history4, end_history_4)).aggregate(Sum('cash')).get('cash__sum')
    if total_5 is None: total_5 = 0
    if wd_5 is None: wd_5 = 0
    if ftd_5 is None: ftd_5 = 0
    netto_5 = total_5 - (2 * wd_5)
    values.append(end_history_4.strftime("%B"))
    dataTotal.append(int(netto_5))
    dataFtd.append(int(ftd_5))
    dataWd.append(int(wd_5))

    total_6 = total.filter(date__range=(start_history5, end_history_5)).aggregate(Sum('cash')).get('cash__sum')
    wd_6 = wd.filter(date__range=(start_history5, end_history_5)).aggregate(Sum('cash')).get('cash__sum')
    ftd_6 = ftd.filter(date__range=(start_history5, end_history_5)).aggregate(Sum('cash')).get('cash__sum')
    if total_6 is None: total_6 = 0
    if wd_6 is None: wd_6 = 0
    if ftd_6 is None: ftd_6 = 0
    netto_6 = total_6 - (2 * wd_6)
    values.append(end_history_5.strftime("%B"))
    dataTotal.append(int(netto_6))
    dataFtd.append(int(ftd_6))
    dataWd.append(int(wd_6))

    total_7 = total.filter(date__range=(start_history6, end_history_6)).aggregate(Sum('cash')).get('cash__sum')
    wd_7 = wd.filter(date__range=(start_history6, end_history_6)).aggregate(Sum('cash')).get('cash__sum')
    ftd_7 = ftd.filter(date__range=(start_history6, end_history_6)).aggregate(Sum('cash')).get('cash__sum')
    if total_7 is None: total_7 = 0
    if wd_7 is None: wd_7 = 0
    if ftd_7 is None: ftd_7 = 0
    netto_7 = total_7 - (2 * wd_7)
    values.append(end_history_6.strftime("%B"))
    dataTotal.append(int(netto_7))
    dataFtd.append(int(ftd_7))
    dataWd.append(int(wd_7))

    total_8 = total.filter(date__range=(start_history7, end_history_7)).aggregate(Sum('cash')).get('cash__sum')
    wd_8 = wd.filter(date__range=(start_history7, end_history_7)).aggregate(Sum('cash')).get('cash__sum')
    ftd_8 = ftd.filter(date__range=(start_history7, end_history_7)).aggregate(Sum('cash')).get('cash__sum')
    if total_8 is None: total_8 = 0
    if wd_8 is None: wd_8 = 0
    if ftd_8 is None: ftd_8 = 0
    netto_8 = total_8 - (2 * wd_8)
    values.append(end_history_7.strftime("%B"))
    dataTotal.append(int(netto_8))
    dataFtd.append(int(ftd_8))
    dataWd.append(int(wd_8))

    total_9 = total.filter(date__range=(start_history8, end_history_8)).aggregate(Sum('cash')).get('cash__sum')
    wd_9 = wd.filter(date__range=(start_history8, end_history_8)).aggregate(Sum('cash')).get('cash__sum')
    ftd_9 = ftd.filter(date__range=(start_history8, end_history_8)).aggregate(Sum('cash')).get('cash__sum')
    if total_9 is None: total_9 = 0
    if wd_9 is None: wd_9 = 0
    if ftd_9 is None: ftd_9 = 0
    netto_9 = total_9 - (2 * wd_9)
    values.append(end_history_8.strftime("%B"))
    dataTotal.append(int(netto_9))
    dataFtd.append(int(ftd_9))
    dataWd.append(int(wd_9))

    total_10 = total.filter(date__range=(start_history9, end_history_9)).aggregate(Sum('cash')).get('cash__sum')
    wd_10 = wd.filter(date__range=(start_history9, end_history_9)).aggregate(Sum('cash')).get('cash__sum')
    ftd_10 = ftd.filter(date__range=(start_history9, end_history_9)).aggregate(Sum('cash')).get('cash__sum')
    if total_10 is None: total_10 = 0
    if wd_10 is None: wd_10 = 0
    if ftd_10 is None: ftd_10 = 0
    netto_10 = total_10 - (2 * wd_10)
    values.append(end_history_9.strftime("%B"))
    dataTotal.append(int(netto_10))
    dataFtd.append(int(ftd_10))
    dataWd.append(int(wd_10))

    total_11 = total.filter(date__range=(start_history10, end_history_10)).aggregate(Sum('cash')).get('cash__sum')
    wd_11 = wd.filter(date__range=(start_history10, end_history_10)).aggregate(Sum('cash')).get('cash__sum')
    ftd_11 = ftd.filter(date__range=(start_history10, end_history_10)).aggregate(Sum('cash')).get('cash__sum')
    if total_11 is None: total_11 = 0
    if wd_11 is None: wd_11 = 0
    if ftd_11 is None: ftd_11 = 0
    netto_11 = total_11 - (2 * wd_11)
    values.append(end_history_10.strftime("%B"))
    dataTotal.append(int(netto_11))
    dataFtd.append(int(ftd_11))
    dataWd.append(int(wd_11))

    total_12 = total.filter(date__range=(start_history11, end_history_11)).aggregate(Sum('cash')).get('cash__sum')
    wd_12 = wd.filter(date__range=(start_history11, end_history_11)).aggregate(Sum('cash')).get('cash__sum')
    ftd_12 = ftd.filter(date__range=(start_history11, end_history_11)).aggregate(Sum('cash')).get('cash__sum')
    if total_12 is None: total_12 = 0
    if wd_12 is None: wd_12 = 0
    if ftd_12 is None: ftd_12 = 0
    netto_12 = total_12 - (2 * wd_12)
    values.append(end_history_11.strftime("%B"))
    dataTotal.append(int(netto_12))
    dataFtd.append(int(ftd_12))
    dataWd.append(int(wd_12))

    context = {
        'office': office,
        'values': values,
        'dataFtd': dataFtd,
        'dataTotal': dataTotal,
        'now': now,
        'start_this_month': start_this_month,
        'dataWd': dataWd,

    }

    return render(request, 'office.html', context)

Models.py

class Operation(models.Model):
    # def __str__(self):
    #     return self.client.name   ' '   self.client.lastname   '  Cash: '   str(self.cash)

    client = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='operations')
    cash = models.DecimalField(max_digits=12, decimal_places=2)
    date = models.DateField(blank=True, null=True)
    bank = models.ForeignKey(Bank, on_delete=models.CASCADE, related_name='operation_bank')
    type = models.ForeignKey(Status, on_delete=models.CASCADE, related_name='operation_type')
    who = models.ForeignKey(Employee, on_delete=models.CASCADE)
    status = models.ForeignKey(Status, on_delete=models.CASCADE, related_name='operation_status')

    class Meta:
        verbose_name = 'Operations'
        verbose_name_plural = 'Operations'

class AssignClient(models.Model):
    client = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='assign_client')
    salesman = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='assign_client_salesman')
    retention = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='assign_client_ret')
    office = models.ForeignKey(Office, on_delete=models.CASCADE, related_name='assign_client_office')
    reg_date = models.DateField(auto_created=True)
    active = models.BooleanField()

class Client(models.Model):
    def __str__(self):
        return self.name   " "   self.lastname

    class Meta:
        verbose_name = 'Client'
        verbose_name_plural = 'Client'

    name = models.CharField(max_length=64)
    lastname = models.CharField(max_length=64)
    phone = models.DecimalField(max_digits=20, decimal_places=0)
    email = models.EmailField(max_length=64, null=True)
    reg_time = models.DateField(blank=True, null=True)
    active = models.BooleanField(default=False)

    @property
    def office(self):
        office = Office.objects.get(assign_client_office__client=self)
        return office

    @property
    def salesman(self):
        salesman = Employee.objects.get(assign_client_salesman__client=self)
        return salesman

    @property
    def retention(self):
        retention = Employee.objects.get(assign_client_ret__client=self)
        return retention

    @property
    def manager(self):
        ret = Employee.objects.get(assign_client_ret__client=self)
        manager = Employee.objects.get(assign_employee__manager=ret)
        return manager

    @property
    def stats_ftd(self):
        cash_ftd = Operation.objects.filter(client_id=self.id).filter(type_id=4).filter(status_id=5).aggregate(
            Sum('cash')).get('cash__sum')
        if cash_ftd is None:
            cash_ftd = 0
        return cash_ftd

    @property
    def stats_depo(self):
        cash_depo = Operation.objects.filter(client_id=self.id).filter(status_id=5).aggregate(
            Sum('cash')).get('cash__sum')
        if cash_depo is None:
            cash_depo = 0
        cash_wd = Operation.objects.filter(client_id=self.id).filter(type_id=3).filter(status_id=5).aggregate(
            Sum('cash')).get('cash__sum')
        if cash_wd is None:
            cash_wd = 0
        return cash_depo - cash_wd

    @property
    def stats_wd(self):

        cash_wd = Operation.objects.filter(client_id=self.id).filter(type_id=3).filter(status_id=5).aggregate(
            Sum('cash')).get('cash__sum')
        if cash_wd is None:
            cash_wd = 0
        return cash_wd

    @property
    def stats_all(self):
        cash_ba = Operation.objects.filter(client_id=self.id).filter(status_id=5).aggregate(
            Sum('cash')).get('cash__sum')
        cash_wd = Operation.objects.filter(client_id=self.id).filter(
            type_id=3).filter(
            status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if cash_ba is None: cash_ba = 0
        if cash_wd is None:
            cash_wd = 0
        cash_balance = cash_ba - (2 * cash_wd)
        return cash_balance

class Employee(models.Model):
    def __str__(self):
        return self.name   " "   self.lastname

    class Meta:
        verbose_name = 'Employee'
        verbose_name_plural = 'Employee'

    name = models.CharField(max_length=64)
    lastname = models.CharField(max_length=64)
    phone = models.IntegerField()
    email = models.EmailField(max_length=64, null=True)
    rank = models.IntegerField()
    reg_time = models.DateField(auto_now_add=True)
    active = models.BooleanField()

    # office = models.ForeignKey(Office, on_delete=models.CASCADE, related_name='employe_office')
    # manager = models.ForeignKey(Menager, on_delete=models.CASCADE)
    # group = models.IntegerField(null=True)

    @property
    def e_ftd(self):
        emplo_ftd = Operation.objects.filter(who_id=self.id).filter(type_id=4).filter(
            status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if emplo_ftd is None: emplo_ftd = 0
        return emplo_ftd

    @property
    def e_redepo(self):
        emplo_ftd = Operation.objects.filter(who_id=self.id).filter(
            status_id=5).filter(type_id=2).aggregate(Sum('cash')).get('cash__sum')
        if emplo_ftd is None: emplo_ftd = 0
        return emplo_ftd

    @property
    def e_redepo_this_month(self):
        emplo_ftd = Operation.objects.filter(who_id=self.id).filter(status_id=5).filter(type_id=2).filter(
            date__month=datetime.date.today().month).aggregate(Sum('cash')).get('cash__sum')
        if emplo_ftd is None: emplo_ftd = 0
        return emplo_ftd

    @property
    def e_redepo_this_month_count(self):
        emplo_ftd = Operation.objects.filter(who_id=self.id).filter(status_id=5).filter(type_id=2).filter(
            date__month=datetime.date.today().month).count()
        if emplo_ftd is None: emplo_ftd = 0
        return emplo_ftd

    @property
    def e_ftd_this_month(self):
        emplo_ftd = Operation.objects.filter(who_id=self.id).filter(date__month=datetime.date.today().month).filter(
            type_id=4).filter(status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if emplo_ftd is None: emplo_ftd = 0
        return emplo_ftd

    @property
    def e_wd_this_month(self):
        emplo_ftd = Operation.objects.filter(who_id=self.id).filter(date__month=datetime.date.today().month).filter(
            type_id=3).filter(status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if emplo_ftd is None: emplo_ftd = 0
        return emplo_ftd

    @property
    def e_wd_this_month_count(self):
        emplo_ftd = Operation.objects.filter(who_id=self.id).filter(date__month=datetime.date.today().month).filter(
            type_id=3).filter(status_id=5).count()
        if emplo_ftd is None: emplo_ftd = 0
        return emplo_ftd

    @property
    def e_depo(self):
        emplo_depo = Operation.objects.filter(who_id=self.id).filter(status_id=5).aggregate(
            Sum('cash')).get('cash__sum')
        if emplo_depo is None: emplo_depo = 0
        emplo_wd = Operation.objects.filter(who_id=self.id).filter(type_id=3).filter(status_id=5).aggregate(
            Sum('cash')).get('cash__sum')
        if emplo_wd is None: emplo_wd = 0
        return emplo_depo - emplo_wd

    @property
    def e_depo_this_month(self):
        emplo_depo = Operation.objects.filter(who_id=self.id).filter(
            date__month=datetime.date.today().month).filter(status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if emplo_depo is None: emplo_depo = 0
        emplo_wd = Operation.objects.filter(who_id=self.id).filter(date__month=datetime.date.today().month).filter(
            type_id=3).filter(status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if emplo_wd is None: emplo_wd = 0
        return emplo_depo - emplo_wd

    @property
    def e_wd(self):
        emplo_wd = Operation.objects.filter(who_id=self.id).filter(type_id=3).filter(status_id=5).aggregate(
            Sum('cash')).get('cash__sum')
        if emplo_wd is None: emplo_wd = 0
        return emplo_wd

    @property
    def e_all(self):
        emplo_depo = Operation.objects.filter(who_id=self.id).filter(status_id=5).aggregate(Sum('cash')
                                                                                            ).get('cash__sum')
        emplo_wd = Operation.objects.filter(who_id=self.id).filter(type_id=3).filter(status_id=5).aggregate(
            Sum('cash')).get('cash__sum')
        if emplo_depo is None: emplo_depo = 0
        if emplo_wd is None: emplo_wd = 0

        emplo_all = emplo_depo - (2 * emplo_wd)

        return emplo_all

    @property
    def e_bilans_this_month(self):
        emplo_depo = Operation.objects.filter(who_id=self.id).filter(date__month=datetime.date.today().month).filter(
            status_id=5).aggregate(Sum('cash')).get('cash__sum')
        emplo_wd = Operation.objects.filter(who_id=self.id).filter(date__month=datetime.date.today().month).filter(
            type_id=3).filter(status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if emplo_depo is None: emplo_depo = 0
        if emplo_wd is None: emplo_wd = 0

        emplo_all = emplo_depo - (2 * emplo_wd)

        return emplo_all

    @property
    def e_ftd_count(self):
        e_ftd_count = Operation.objects.filter(who_id=self.id).filter(type_id=4).filter(status_id=5).aggregate(
            Count('cash')).get('cash__count')
        return e_ftd_count

    @property
    def e_ftd_count_this_month(self):
        e_ftd_count = Operation.objects.filter(who_id=self.id).filter(date__month=datetime.date.today().month).filter(
            type_id=4).filter(status_id=5).aggregate(Count('cash')).get('cash__count')
        return e_ftd_count

    @property
    def e_wd_count(self):
        e_wd_count = Operation.objects.filter(who_id=self.id).filter(type_id=3).filter(status_id=5).aggregate(
            Count('cash')).get('cash__count')

        return e_wd_count

    @property
    def e_depo_count(self):
        e_depo_count = Operation.objects.filter(who_id=self.id).filter(type_id=2).filter(status_id=5).aggregate(
            Count('cash')).get('cash__count')

        return e_depo_count

    @property
    def office(self):
        office = Office.objects.get(assign_emplo_office__employee=self)
        return office

    @property
    def voip(self):
        voip = SipAccess.objects.get(worker=self)
        return voip

    @property
    def group(self):
        group = Group.objects.get(assign_emplo_group__employee=self)
        return group

    @property
    def menago(self):
        menago = Employee.objects.filter(rank=3).get(assign_emplo_manager__employee=self)
        if menago is None:
            menago = '1'
        return menago

    @property
    def manager_employee_count(self):
        employee_count = Employee.objects.filter(assign_employee__manager_id=self).count()
        if employee_count is None: employee_count = 0
        return employee_count

    @property
    def m_depo(self):
        m_depo = Operation.objects.filter(who__assign_employee__manager_id=self).filter(status_id=5).aggregate(
            Sum('cash')).get('cash__sum')
        if m_depo is None: m_depo = 0
        m_wd = Operation.objects.filter(who__assign_employee__manager_id=self).filter(
            type_id=3).filter(status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if m_wd is None: m_wd = 0
        return m_depo - m_wd

    @property
    def m_ftd(self):
        m_ftd = Operation.objects.filter(who__assign_employee__manager_id=self).filter(type_id=4).filter(
            status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if m_ftd is None: m_ftd = 0
        return m_ftd

    @property
    def m_ftd_this_month(self):
        m_ftd = Operation.objects.filter(who__assign_employee__manager_id=self).filter(type_id=4).filter(
            status_id=5).filter(date__month=datetime.date.today().month).aggregate(Sum('cash')).get('cash__sum')
        if m_ftd is None: m_ftd = 0
        return m_ftd

    @property
    def m_ftd_count(self):
        m_ftd_count = Operation.objects.filter(who__assign_employee__manager_id=self).filter(type_id=4).filter(
            status_id=5).count()
        if m_ftd_count is None: m_ftd_count = 0
        return m_ftd_count

    @property
    def m_redepo(self):
        m_redepo = Operation.objects.filter(who__assign_employee__manager_id=self).filter(
            status_id=5).filter(type_id=2).aggregate(Sum('cash')).get('cash__sum')
        if m_redepo is None: m_redepo = 0
        return m_redepo

    @property
    def m_redepo_count(self):
        m_redepo_count = Operation.objects.filter(who__assign_employee__manager_id=self).filter(
            status_id=5).filter(type_id=2).count()
        if m_redepo_count is None: m_redepo_count = 0
        return m_redepo_count

    def m_wd(self):
        m_wd = Operation.objects.filter(who__assign_employee__manager_id=self).filter(type_id=3).filter(
            status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if m_wd is None:
            m_wd = 0
        return m_wd

    def m_wd_this_month(self):
        m_wd = Operation.objects.filter(who__assign_employee__manager_id=self).filter(type_id=3).filter(
            status_id=5).filter(date__month=datetime.date.today().month).aggregate(Sum('cash')).get('cash__sum')
        if m_wd is None:
            m_wd = 0
        return m_wd

    def m_wd_count(self):
        m_wd_count = Operation.objects.filter(who__assign_employee__manager_id=self).filter(type_id=3).filter(
            status_id=5).count()
        if m_wd_count is None:
            m_wd_count = 0
        return m_wd_count

    @property
    def m_balance(self):
        m_depo = Operation.objects.filter(who__assign_employee__manager_id=self).filter(
            status_id=5).aggregate(Sum('cash')).get('cash__sum')
        m_wd = Operation.objects.filter(who__assign_employee__manager_id=self).filter(
            type_id=3).filter(status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if m_depo is None: m_depo = 0
        if m_wd is None: m_wd = 0
        m_balance = m_depo - (2 * m_wd)
        return m_balance

    @property
    def m_balance_this_month(self):
        m_depo = Operation.objects.filter(who__assign_employee__manager_id=self).filter(
            status_id=5).filter(date__month=datetime.date.today().month).aggregate(Sum('cash')).get('cash__sum')
        m_wd = Operation.objects.filter(who__assign_employee__manager_id=self).filter(
            type_id=3).filter(date__month=datetime.date.today().month).filter(
            status_id=5).aggregate(Sum('cash')).get('cash__sum')
        if m_depo is None: m_depo = 0
        if m_wd is None: m_wd = 0
        m_balance = m_depo - (2 * m_wd)
        return m_balance

this is a chart:

bar chart

CodePudding user response:

An example is:

These 22 lines of code in Views.py:

end_history_1 = today - relativedelta(days=1)
start_history1 = end_history_1.replace(day=1)

...
    
end_history_11 = today - relativedelta(days=1, months=10)
start_history11 = end_history_11.replace(day=1)

Can be replaced by these 5 lines:

    end_history = [0 for i in range(12)]
    start_history = [0 for i in range(12)]
    for i in range(1,12):
        end_history[i] = today - relativedelta(days=1, months=i-1)
        start_history[i] = end_history[i].replace(day=1)

After this you should no longer refer to start_history4, but to start_history[4], and end_history_8 should be replaced by end_history[8]

NOTE: You need to check if these variables are used in the rest of the code, and make the needed changes!

CodePudding user response:

Migrating solution from the question to an answer:

I changed thanks to the hint 85% of the code to a loop:

   end_history = [0 for i in range(12)]
   start_history = [0 for i in range(12)]
   total_chart = [0 for i in range(12)]
   wd_chart = [0 for i in range(12)]
    ftd_chart = [0 for i in range(12)]
    netto = [0 for i in range(12)]
    for i in range(1, 12):
        end_history[i] = today - relativedelta(days=1, months=i - 1)
        start_history[i] = end_history[i].replace(day=1)
        total_chart[i] = total.filter(date__range=(start_history[i], end_history[i])).aggregate(Sum('cash')).get(
            'cash__sum')
        wd_chart[i] = wd.filter(date__range=(start_history[i], end_history[i])).aggregate(Sum('cash')).get('cash__sum')
        ftd_chart[i] = ftd.filter(date__range=(start_history[i], end_history[i])).aggregate(Sum('cash')).get(
            'cash__sum')
        if total_chart[i] is None: total_chart[i] = 0
        if wd_chart[i] is None: wd_chart[i] = 0
        if ftd_chart[i] is None: ftd_chart[i] = 0
        netto[i] = total_chart[i] - (2 * wd_chart[i])
        values.append(end_history[i].strftime("%B"))
        dataTotal.append(int(netto[i]))
        dataFtd.append(int(ftd_chart[i]))
       dataWd.append(int(wd_chart[i]))
  • Related