# -*- coding: utf-8 -*-
from django import forms
from django.forms import ModelForm
from .models import *

from django.utils.translation import gettext_lazy as _
from datetime import datetime




class StorehouseForm(ModelForm):
    class Meta:
        model=Storehouse
        fields='__all__'
        widgets={
            'company':forms.HiddenInput(),                
        }


class BatchForm(ModelForm):
    class Meta:
        model=Batch
        fields='__all__'
        widgets={
            'company':forms.HiddenInput(),        
            'vtype':forms.HiddenInput(),   
            'user':forms.HiddenInput(),  
            'order':forms.HiddenInput(),   
            'customer':forms.HiddenInput(),  
            'vendors':forms.HiddenInput(), 
        }
    def __init__(self,* args,** kwargs):
        super(BatchForm,self).__init__(* args,** kwargs)  
        self.fields['dt_voucher'].widget.attrs['class'] = "datepicker"
    

class StkDetailsForm(ModelForm):
    class Meta:
        model=StkDetails
        fields='__all__'
        widgets={
            'batch':forms.HiddenInput(),                   
            'vtype':forms.HiddenInput(),   
            'user':forms.HiddenInput(),
            'materials':forms.HiddenInput(),
            'packaging':forms.HiddenInput(),
            'semifinishedgoods':forms.HiddenInput(),
            'product':forms.HiddenInput(),
            'company':forms.HiddenInput(), 
            'store_fr':forms.HiddenInput(),
            'store_to':forms.HiddenInput(),
            'qc':forms.HiddenInput(),
        }

    def __init__(self,* args,** kwargs):
        super(StkDetailsForm,self).__init__(* args,** kwargs)        
        self.fields['materials'].widget.attrs['class'] = "mat_id"
        self.fields['packaging'].widget.attrs['class'] = "pkg_id"  
        self.fields['semifinishedgoods'].widget.attrs['class']="sem_id"    
        self.fields['product'].widget.attrs['class']="fg_id" 
        self.fields['company'].widget.attrs['class'] = "company_id"
        self.fields['store_fr'].widget.attrs['class'] = "store_fr_id"
        self.fields['store_to'].widget.attrs['class'] = "store_to_id"
        self.fields['vtype'].widget.attrs['class']="vtype_id"
        self.fields['qty'].widget.attrs['class'] = "qty"
        self.fields['unit'].widget.attrs['class'] = "unit"

StkDetailsFormset=forms.models.inlineformset_factory(Batch, StkDetails, StkDetailsForm, extra=1, can_delete=True)  


class MaterialsForm(ModelForm):
    class Meta:
        model=Materials
        fields='__all__'
        widgets={
            'company':forms.HiddenInput(),
            'unit':forms.HiddenInput(),
        }


class BOMForm(ModelForm):
    class Meta:
        model=BOM
        fields='__all__'
        widgets={
            'company':forms.HiddenInput(),
            'product':forms.HiddenInput(),
        }


class BOMmaterialsForm(ModelForm):
    class Meta:
        model=BOMmaterials
        fields='__all__'
        widgets={
            #'bom': forms.HiddenInput(),
            'materials': forms.HiddenInput(),
            'unit': forms.HiddenInput(),            
        }
BOMmaterialsFormset=forms.models.inlineformset_factory(BOM, BOMmaterials,
        BOMmaterialsForm, extra=0, can_delete=True)    

class BOMpackagingForm(ModelForm):
    class Meta:
        model=BOMpackaging
        fields='__all__'
        widgets={
            'bom': forms.HiddenInput(),
            'packaging': forms.HiddenInput(),
            'unit': forms.HiddenInput(),            
        }
BOMpackagingFormset=forms.models.inlineformset_factory(BOM, BOMpackaging,
        BOMpackagingForm, extra=0, can_delete=True)   

class BOMsemifinishedgoodsForm(ModelForm):
    class Meta:
        model=BOMsemifinishedgoods
        fields='__all__'
        widgets={
            'bom': forms.HiddenInput(),
            'semifinishedgoods': forms.HiddenInput(),
            'unit': forms.HiddenInput(),            
        }
BOMsemifinishedgoodsFormset=forms.models.inlineformset_factory(BOM,
        BOMsemifinishedgoods, BOMsemifinishedgoodsForm, extra=0, can_delete=True)   


class OrderForm(ModelForm):
    class Meta:
        model=Order
        fields='__all__'
        widgets={
            'company':forms.HiddenInput(),
            'agent':forms.HiddenInput(),
            'user':forms.HiddenInput(),
        }
    def __init__(self,* args,** kwargs):
        super(OrderForm,self).__init__(* args,** kwargs)        
        self.fields['dt_delivery'].widget.attrs['class'] = "datepicker"

        
class OrderDetailsForm(ModelForm):
    class Meta:
        model=OrderDetails
        fields='__all__'
        widgets={
            'product':forms.HiddenInput(),
            #'unit':forms.HiddenInput(),
        }
    def __init__(self,* args,** kwargs):
        super(OrderDetailsForm,self).__init__(* args,** kwargs)        
        self.fields['product'].widget.attrs['class'] = "id_product"
OrderDetailsFormset=forms.models.inlineformset_factory(Order, OrderDetails,
        OrderDetailsForm, extra=1, can_delete=True)   




class VendorForm(ModelForm):
    class Meta:
        model=Vendor
        fields='__all__'
        widgets={
            'company':forms.HiddenInput(),            
        }

class VendorPhotosForm(ModelForm):
    class Meta:
        model=VendorPhotos
        fields='__all__'
        widgets={
            'vendor':forms.HiddenInput(),            
        }
  

 
class CustomerForm(ModelForm):
    class Meta:
        model=Customer
        fields='__all__'
        widgets={
            'company':forms.HiddenInput(),            
        }

#lisin 2024-08-22
class CompanyunitForm(ModelForm):
    class Meta:
        model = CompanyUnit 
        fields='__all__'
        widgets={
            'company':forms.HiddenInput(),
        }

# lisin
class PackagingForm(ModelForm):
    class Meta:
        model=Packaging
        fields='__all__'
        widgets={
            'company': forms.HiddenInput(),    
            # 'code': forms.TextInput(attrs={'readonly': 'readonly'}),         
        }
class CompanyForm(ModelForm):
    class Meta:
        model=Company
        fields='__all__'
        widgets={
            'company': forms.HiddenInput(),    
        }

# lisin
class PackagingVendorForm(ModelForm):
    class Meta:
        model=PackagingVendor
        fields='__all__'
        widgets = {
            'company': forms.HiddenInput(),
            'packaging': forms.HiddenInput(),
            'vendor': forms.HiddenInput(),  
        }
    def __init__(self, *args, **kwargs):
        super(PackagingVendorForm, self).__init__(*args, **kwargs)
        self.fields['vendor'].widget.attrs['class'] = "vid"
        
PackagingVendorFormset=forms.inlineformset_factory(Packaging, PackagingVendor,
        form=PackagingVendorForm, extra=0, can_delete=True)  

class PackagingPurchaseRecordForm(ModelForm):
    class Meta:
        model = PackagingPurchasRecord 
        fields='__all__'
        widgets={
            'code': forms.TextInput(attrs={'readonly': 'readonly'}),
            'company':forms.HiddenInput(),
        }

    def __init__(self, *args, **kwargs):
        self._user = kwargs.pop('user')
        super(PackagingPurchaseRecordForm, self).__init__(*args, **kwargs)
        self.fields['vendor'].queryset = Vendor.objects.filter(company_id=self._user.company.id)
        self.fields['packaging'].queryset = Packaging.objects.filter(company_id=self._user.company.id)

class PackagingInstoreForm(ModelForm):
    class Meta:
        model = PackagingInstore
        fields='__all__'
        widgets={
            'code': forms.TextInput(attrs={'readonly': 'readonly'}),
            'company':forms.HiddenInput(),
            'Packaging':forms.HiddenInput()
        }

    def __init__(self, *args, **kwargs):
        self._user = kwargs.pop('user')
        super(PackagingInstoreForm, self).__init__(*args, **kwargs)
        # 获取当前时间并格式化
        now = datetime.now()
        formatted_time = now.strftime("%Y%m%d%H%M%S")
        self.fields['code'].initial = f'RK{formatted_time}'
        print(f"Setting code initial value to: RK{formatted_time}")
        self.fields['packaging_purchas'].queryset = PackagingPurchasRecord.objects.filter(company_id=self._user.company.id)
        self.fields['packaging_purchas'].label_from_instance = lambda obj: obj.code

class PackagingReceiveForm(ModelForm):
    class Meta:
        model = PackagingReceive
        fields='__all__'
        widgets={
            'code': forms.TextInput(attrs={'readonly': 'readonly'}),
            'company':forms.HiddenInput(),
        }

    def __init__(self, *args, **kwargs):
        self._user = kwargs.pop('user')
        super(PackagingReceiveForm, self).__init__(*args, **kwargs)
        self.fields['packaging'].queryset = Packaging.objects.filter(company_id=self._user.company.id)