from django.db import models
from django.urls import reverse
from Item.models import Item, UOM
from Customer.models import Customer

class Invoice(models.Model):
    CustomerRefFullName = models.ForeignKey(Customer, on_delete=models.PROTECT)
    # TxnNumber
    # ClassRefFullName
    # TemplateRefFullName
    TxnDate = models.DateField()
    RefNumber = models.CharField(max_length=30, verbose_name="S.O. No.")
    BillAddr1 = models.CharField(max_length=80, verbose_name="Name/Address")
    BillAddr2 = models.CharField(max_length=80, blank=True, null=True)
    BillAddr3 = models.CharField(max_length=80, blank=True, null=True)
    BillAddr4 = models.CharField(max_length=80, blank=True, null=True)
    BillAddr5 = models.CharField(max_length=80, blank=True, null=True)
    BillCity = models.CharField(max_length=80, blank=True, null=True)
    BillState = models.CharField(max_length=80, blank=True, null=True)
    BillPostalCode = models.CharField(max_length=80, blank=True, null=True)
    BillCountry = models.CharField(max_length=80, default="Indonesia", blank=True, null=True)
    BillNote = models.CharField(max_length=80, blank=True, null=True)
    # Addr1 = 
    # Addr2
    # Addr3
    ShipAddr1 = models.CharField(max_length=80, verbose_name="Ship To", blank=True, null=True)
    ShipAddr2 = models.CharField(max_length=80, blank=True, null=True)
    ShipAddr3 = models.CharField(max_length=80, blank=True, null=True)
    ShipAddr4 = models.CharField(max_length=80, blank=True, null=True)
    ShipAddr5 = models.CharField(max_length=80, blank=True, null=True)
    ShipCity = models.CharField(max_length=80, blank=True, null=True)
    ShipState = models.CharField(max_length=80, blank=True, null=True)
    ShipPostalCode = models.CharField(max_length=80, blank=True, null=True)
    ShipCountry = models.CharField(max_length=80, blank=True, null=True)
    ShipNote = models.CharField(max_length=200, blank=True, null=True)
    
    # ShipAddr1
    # ShipAddr2
    # ShipAddr3
    # ShipAddr4
    # ShipCity
    # ShipState
    # ShipPostalCode
    # ShipCountry
    # City
    # State
    # PostalCode
    # Country_Note
    PONumber = models.CharField(max_length=30, verbose_name="PO. No.", blank=True, null=True)
    TermsRefFullName = models.CharField(max_length=10, verbose_name="Terms", blank=True, null=True)
    SalesRepRefFullName = models.CharField(max_length=10, verbose_name="REP", blank=True, null=True)
    ShipDate = models.DateTimeField(blank=True, null=True)
    # ShipMethodRefFullName = 
    DueDate = models.DateField(blank=True, null=True)
    # Subtotal = models.DecimalField(max_digits=14, decimal_places=2, blank=True, null=True)

    # ItemSalesTaxRefFullName
    # SalesTaxPercentage
    # SalesTaxTotal
    # CurrencyRefFullName
    TotalAmount = models.DecimalField(max_digits=14, decimal_places=2, blank=True, null=True )
    # ExchangeRate
    # TotalAmountInHomeCurrency
    CustomerMsgRefFullName = models.CharField(max_length=120, blank=True, null=True)
    IsToBePrinted = models.BooleanField(default=False)
    IsToBeEmailed = models.BooleanField(default=False)
    # CustomerSalesTaxCodeRefFullName
    # Other
    # FOB
    IsManuallyClosed = models.BooleanField(default=False)
    IsFullyInvoiced = models.BooleanField(default=False)
    Memo = models.CharField(max_length=120, blank=True, null=True)
    # LinkedTxnID
    # TxnLineID = models.ForeignKey(InvoiceItemLine, ondelete=models.DO_NOTHING)
    
    NPWP = models.CharField(max_length=20, blank=True, null=True)
    KTP = models.CharField(max_length=16, blank=True, null=True)
    DMS_Cust_Name = models.CharField(max_length=80, blank=True, null=True)
    DMS_Cust_Code = models.CharField(max_length=80, blank=True, null=True)
    Special_Cust = models.BooleanField(default=False)
    EFaktur_Name = models.CharField(max_length=80, blank=True, null=True)
    Efaktur_Address = models.CharField(max_length=80, blank=True, null=True)
    Coordinates = models.CharField(max_length=30, blank=True, null=True)
    TimeCreated = models.DateTimeField(auto_now_add=True)
    TimeModified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.RefNumber
    
    def get_field_name(obj):
        # return [(f.name, f.value_to_string(obj)) for f in obj._meta.fields]   #get value convert it to string
        return [(f.verbose_name, f.name, f.value_from_object(obj)) for f in obj._meta.fields]
    
    def get_absolute_url(self):
        return reverse('Invoice:edit_so', args=[str(self.id)])
    
    def save(self, *args, **kwargs):
        
        super().save(*args, **kwargs)


class InvoiceItemLine(models.Model):
    Invoice = models.ForeignKey(Invoice, on_delete=models.PROTECT)
    ItemRefFullName = models.ForeignKey(Item, on_delete=models.PROTECT)
    Desc = models.CharField(max_length=80, blank=True, null=True)
    Quantity = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    UnitOfMeasure = models.ForeignKey(UOM, verbose_name="UOM", on_delete=models.DO_NOTHING, blank=True, null=True)
    # OverrideUOMFullName
    Rate = models.DecimalField(max_digits=11, decimal_places=2, blank=True, null=True)
    Amount = models.DecimalField(max_digits=14, decimal_places=2, blank=True, null=True)
    # InventorySiteRefFullName
    # SerialNumber
    # LotNumber
    # SalesTaxCodeRefFullName
    # Other1
    # Other2
    Invoiced = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)   #how many qty that has invoiced
    LineIsManuallyClosed = models.BooleanField(default=False, blank=True, null=True)
    # LineClass
    # GroupTxnLineID
    # GroupItemFullName
    # GroupDesc
    # GroupQuantity
    # IsPrintItemsInGroup
    # RatePercent
    # GroupLineTxnLineID
    # GroupLineItemFullName
    # GroupLineDesc
    # GroupLineQuantity
    # GroupLineUOM
    # GroupLineOverrideUOM
    # GroupLineRate
    # GroupLineAmount
    # GroupLineServiceDate
    # GroupLineSalesTaxCodeFullName
