from django.db import models
from company.models import Company
from orders.models import Orders
from django.utils import timezone
import datetime

# Create your models here.


class Bills(models.Model):

    Generated = 'Generated'
    Bill_Paid = 'Bill Paid'

    BILL_STATUS_CHOICES = (
        (Generated, 'Generated'),
        (Bill_Paid, 'Bill_Paid'),
    )

    billNumber = models.CharField(max_length=256, verbose_name="Bill no")
    compName = models.ForeignKey(Company, on_delete=models.CASCADE, verbose_name="Company Name")
    orderId = models.ForeignKey(Orders, on_delete=models.CASCADE, verbose_name="Order ID")
    billDate = models.DateTimeField(auto_now=True, verbose_name="Bill Date")
    createdBy = models.CharField(max_length=150, verbose_name="Created by", default="Accountant")
    billAmount = models.FloatField(verbose_name="Bill Amount")
    billStatus = models.CharField(max_length=50, choices=BILL_STATUS_CHOICES,
                                  verbose_name="Bill Status", default="Generated")

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

    def generate_invoice_no(self):
        """
            This function will generate bill number from first 3 letters from company name and current date time.
            and the assign it to billNumber field.

        """
        part1 = self.compName
        print("CNAME:", str(part1))
        cname = str(part1)
        cnamehalf = cname[:3]
        t = datetime.datetime.now()
        t = t.strftime('%m%d%Y%H%M%S')
        par = str(t)
        part2 = str(par)
        aaa = cnamehalf.upper() + part2
        self.billNumber = aaa

    def save(self):

        # To save the bill number generated by the function.
        self.generate_invoice_no()
        super(Bills, self).save()
