summaryrefslogtreecommitdiff
path: root/app/unused_apps/income/models.py
blob: e5a351b59748f0e8ea6d47b8a53c75eafd0c9f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import datetime
from datetime import timedelta
from django.db import models
from django.utils import timezone
from django.urls import reverse
from django.utils.html import format_html

from resume.models import PubItem, Publisher


class Gig(models.Model):
    title = models.CharField(max_length=200)
    pitch = models.TextField(null=True, blank=True)
    created = models.DateTimeField(default=timezone.now)
    pub_date = models.DateTimeField(blank=True, null=True)
    due_date = models.DateField(blank=True, null=True)
    STATUS = (
        (0, "Pitched"),
        (1, "Accepted"),
        (2, "Submitted"),
        (3, "Published"),
        (4, "Rejected"),
        (5, "TO PITCH"),
    )
    status = models.IntegerField(choices=STATUS, default=1)
    invoice_date = models.DateTimeField(null=True, blank=True)
    payment = models.DecimalField(max_digits=10, decimal_places=2)
    PAY_STATUS = (
        (0, "NOT SUBMITTED"),
        (1, "Invoiced"),
        (2, "Paid"),
    )
    payment_status = models.IntegerField(choices=PAY_STATUS, default=1)
    PAY_TYPE = (
        (0, "Flat Rate"),
        (1, "Per Word"),
        (2, "Hourly"),
    )
    pay_type = models.IntegerField(choices=PAY_TYPE, default=1)
    word_count = models.DecimalField(max_digits=7, decimal_places=0, blank=True, null=True)
    publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE, blank=True, null=True)
    pub_item = models.ForeignKey(PubItem, on_delete=models.CASCADE, blank=True, null=True)

    def __str__(self):
        return self.title

    def get_pay_date(self):
        days = self.publisher.payment_time * 7
        if self.invoice_date:
            return self.invoice_date + datetime.timedelta(float(days))


class Invoice(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField()
    date_start = models.DateField(null=True, blank=True)
    date_end = models.DateField(null=True, blank=True)

    def __str__(self):
        return self.title

    def admin_link(self):
        return format_html('<a href="/admin/income/invoice/monthlyview/%s/">View Invoice</a>' % (self.slug))
    admin_link.short_description = 'Invoice'


class InvoiceItem(models.Model):
    time_start = models.DateTimeField(null=True, blank=True)
    time_end = models.DateTimeField(null=True, blank=True)
    work_done = models.TextField(null=True, blank=True)

    class Meta:
        ordering = ('time_start',)

    def __str__(self):
        return str(self.time_start)

    @property
    def total(self):
        return self.time_end - self.time_start

    @property
    def rounded_total(self):
        """
        Rounds the given timedelta by the given timedelta period
        :param td: `timedelta` to round
        :param period: `timedelta` period to round by.
        """
        period = timedelta(minutes=15)
        td = self.total
        period_seconds = period.total_seconds()
        half_period_seconds = period_seconds / 2
        remainder = td.total_seconds() % period_seconds
        if remainder >= half_period_seconds:
            tdr = timedelta(seconds=td.total_seconds() + (period_seconds - remainder))
            hours, remainder = divmod(tdr.total_seconds(), 3600)
            r = remainder/3600
            return float(hours)+r
        else:
            tdr = timedelta(seconds=td.total_seconds() - remainder)
            hours, remainder = divmod(tdr.total_seconds(), 3600)
            r = remainder/3600
            return float(hours)+r