2016-09-01 00:45:44 +02:00
# -*- coding: utf-8 -*-
2016-08-22 05:41:31 +02:00
from decimal import Decimal
2017-04-03 20:32:16 +02:00
2016-08-02 10:40:46 +02:00
from django import forms
2016-08-07 17:02:01 +02:00
from django . core . exceptions import ValidationError
2017-09-13 01:57:31 +02:00
from django . contrib . auth . models import User
from django . forms import modelformset_factory
2016-08-22 02:52:59 +02:00
from django . utils import timezone
2017-04-03 20:32:16 +02:00
2017-01-31 19:35:52 +01:00
from kfet . models import (
Account , Checkout , Article , OperationGroup , Operation ,
2017-04-03 20:32:16 +02:00
CheckoutStatement , ArticleCategory , AccountNegative , Transfer ,
2017-01-31 19:35:52 +01:00
TransferGroup , Supplier )
2016-08-02 10:40:46 +02:00
from gestioncof . models import CofProfile
2017-04-03 20:32:16 +02:00
2016-08-04 05:21:04 +02:00
# -----
# Widgets
# -----
class DateTimeWidget ( forms . DateTimeInput ) :
2017-06-12 01:51:10 +02:00
def __init__ ( self , * args , * * kwargs ) :
super ( ) . __init__ ( * args , * * kwargs )
2016-08-04 05:21:04 +02:00
self . attrs [ ' format ' ] = ' % Y- % m- %d % H: % M '
2017-06-12 01:51:10 +02:00
2016-08-04 05:21:04 +02:00
class Media :
css = {
2017-06-12 01:51:10 +02:00
' all ' : ( ' kfet/css/bootstrap-datetimepicker.min.css ' , )
}
2017-06-21 07:08:28 +02:00
js = ( ' kfet/js/bootstrap-datetimepicker.min.js ' , )
2017-06-12 01:51:10 +02:00
2016-08-04 05:21:04 +02:00
# -----
# Account forms
# -----
2016-08-03 04:38:54 +02:00
class AccountForm ( forms . ModelForm ) :
# Surcharge pour passer data à Account.save()
def save ( self , data = { } , * args , * * kwargs ) :
obj = super ( AccountForm , self ) . save ( commit = False , * args , * * kwargs )
obj . save ( data = data )
return obj
2016-08-02 10:40:46 +02:00
class Meta :
model = Account
2016-09-03 22:52:55 +02:00
fields = [ ' trigramme ' , ' promo ' , ' nickname ' , ' is_frozen ' ]
2016-08-02 10:40:46 +02:00
widgets = {
' trigramme ' : forms . TextInput ( attrs = { ' autocomplete ' : ' off ' } ) ,
}
2016-09-05 07:31:54 +02:00
class AccountBalanceForm ( forms . ModelForm ) :
class Meta :
model = Account
fields = [ ' balance ' ]
2016-08-03 04:38:54 +02:00
class AccountTriForm ( AccountForm ) :
2016-09-03 16:41:02 +02:00
def clean_trigramme ( self ) :
trigramme = self . cleaned_data [ ' trigramme ' ]
return trigramme . upper ( )
2016-08-03 04:38:54 +02:00
class Meta ( AccountForm . Meta ) :
fields = [ ' trigramme ' ]
class AccountNoTriForm ( AccountForm ) :
class Meta ( AccountForm . Meta ) :
exclude = [ ' trigramme ' ]
class AccountRestrictForm ( AccountForm ) :
class Meta ( AccountForm . Meta ) :
2016-09-03 22:52:55 +02:00
fields = [ ' is_frozen ' ]
2016-08-02 10:40:46 +02:00
2016-09-01 16:31:18 +02:00
class AccountPwdForm ( forms . Form ) :
pwd1 = forms . CharField (
2017-03-31 02:31:16 +02:00
label = " Mot de passe K-Fêt " ,
2017-04-02 04:25:47 +02:00
help_text = " Le mot de passe doit contenir au moins huit caractères " ,
2016-09-01 16:31:18 +02:00
widget = forms . PasswordInput )
pwd2 = forms . CharField (
2017-03-31 02:31:16 +02:00
label = " Confirmer le mot de passe " ,
2016-09-01 16:31:18 +02:00
widget = forms . PasswordInput )
def clean ( self ) :
2016-09-02 14:17:11 +02:00
pwd1 = self . cleaned_data . get ( ' pwd1 ' , ' ' )
pwd2 = self . cleaned_data . get ( ' pwd2 ' , ' ' )
2016-09-01 16:31:18 +02:00
if len ( pwd1 ) < 8 :
raise ValidationError ( " Mot de passe trop court " )
if pwd1 != pwd2 :
raise ValidationError ( " Les mots de passes sont différents " )
super ( AccountPwdForm , self ) . clean ( )
2016-08-02 10:40:46 +02:00
class CofForm ( forms . ModelForm ) :
def clean_is_cof ( self ) :
instance = getattr ( self , ' instance ' , None )
if instance and instance . pk :
return instance . is_cof
else :
return False
class Meta :
model = CofProfile
fields = [ ' login_clipper ' , ' is_cof ' , ' departement ' ]
2016-08-03 04:38:54 +02:00
class CofRestrictForm ( CofForm ) :
class Meta ( CofForm . Meta ) :
fields = [ ' departement ' ]
2016-08-31 00:36:23 +02:00
2017-09-04 13:25:09 +02:00
class UserForm ( forms . ModelForm ) :
2016-08-02 10:40:46 +02:00
class Meta :
2017-09-04 13:25:09 +02:00
model = User
2016-08-02 10:40:46 +02:00
fields = [ ' username ' , ' first_name ' , ' last_name ' , ' email ' ]
help_texts = {
' username ' : ' '
}
2017-09-04 13:25:09 +02:00
2016-08-03 04:38:54 +02:00
class UserRestrictForm ( UserForm ) :
2016-08-21 02:53:35 +02:00
class Meta ( UserForm . Meta ) :
fields = [ ' first_name ' , ' last_name ' ]
class UserRestrictTeamForm ( UserForm ) :
2016-08-03 04:38:54 +02:00
class Meta ( UserForm . Meta ) :
fields = [ ' first_name ' , ' last_name ' , ' email ' ]
2016-08-04 05:21:04 +02:00
2017-05-12 16:55:18 +02:00
2016-08-22 20:07:01 +02:00
class AccountNegativeForm ( forms . ModelForm ) :
class Meta :
model = AccountNegative
fields = [ ' authz_overdraft_amount ' , ' authz_overdraft_until ' ,
' balance_offset ' , ' comment ' ]
widgets = {
' authz_overdraft_until ' : DateTimeWidget ( ) ,
}
2016-08-04 05:21:04 +02:00
# -----
# Checkout forms
# -----
class CheckoutForm ( forms . ModelForm ) :
class Meta :
2016-08-04 08:23:34 +02:00
model = Checkout
fields = [ ' name ' , ' valid_from ' , ' valid_to ' , ' balance ' , ' is_protected ' ]
2016-08-04 05:21:04 +02:00
widgets = {
' valid_from ' : DateTimeWidget ( ) ,
' valid_to ' : DateTimeWidget ( ) ,
}
class CheckoutRestrictForm ( CheckoutForm ) :
class Meta ( CheckoutForm . Meta ) :
fields = [ ' name ' , ' valid_from ' , ' valid_to ' ]
2016-08-04 08:23:34 +02:00
2016-08-23 00:15:17 +02:00
class CheckoutStatementCreateForm ( forms . ModelForm ) :
2016-08-23 02:45:49 +02:00
balance_001 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_002 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_005 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_01 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_02 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_05 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_1 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_2 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_5 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_10 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_20 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_50 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_100 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_200 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
balance_500 = forms . IntegerField ( min_value = 0 , initial = 0 , required = False )
2016-08-23 00:15:17 +02:00
class Meta :
model = CheckoutStatement
exclude = [ ' by ' , ' at ' , ' checkout ' , ' amount_error ' , ' amount_taken ' ,
' balance_old ' , ' balance_new ' ]
2016-08-23 02:45:49 +02:00
def clean ( self ) :
not_count = self . cleaned_data [ ' not_count ' ]
if not not_count and (
self . cleaned_data [ ' balance_001 ' ] is None
or self . cleaned_data [ ' balance_002 ' ] is None
or self . cleaned_data [ ' balance_005 ' ] is None
or self . cleaned_data [ ' balance_01 ' ] is None
or self . cleaned_data [ ' balance_02 ' ] is None
or self . cleaned_data [ ' balance_05 ' ] is None
or self . cleaned_data [ ' balance_1 ' ] is None
or self . cleaned_data [ ' balance_2 ' ] is None
or self . cleaned_data [ ' balance_5 ' ] is None
or self . cleaned_data [ ' balance_10 ' ] is None
or self . cleaned_data [ ' balance_20 ' ] is None
or self . cleaned_data [ ' balance_50 ' ] is None
or self . cleaned_data [ ' balance_100 ' ] is None
or self . cleaned_data [ ' balance_200 ' ] is None
or self . cleaned_data [ ' balance_500 ' ] is None ) :
raise ValidationError ( " Y ' a un problème. Si tu comptes la caisse, mets au moins des 0 stp (et t ' as pas idée de comment c ' est long de vérifier que t ' as mis des valeurs de partout...) " )
super ( CheckoutStatementCreateForm , self ) . clean ( )
2016-08-23 00:15:17 +02:00
class CheckoutStatementUpdateForm ( forms . ModelForm ) :
2016-08-11 15:14:23 +02:00
class Meta :
model = CheckoutStatement
2016-08-23 00:15:17 +02:00
exclude = [ ' by ' , ' at ' , ' checkout ' , ' amount_error ' , ' amount_taken ' ]
2016-08-11 15:14:23 +02:00
2017-04-04 21:36:02 +02:00
# -----
# Category
# -----
class CategoryForm ( forms . ModelForm ) :
class Meta :
model = ArticleCategory
fields = [ ' name ' , ' has_addcost ' ]
2016-08-04 08:23:34 +02:00
# -----
# Article forms
# -----
class ArticleForm ( forms . ModelForm ) :
2016-08-21 18:10:35 +02:00
category_new = forms . CharField (
2017-03-31 00:32:11 +02:00
label = " Créer une catégorie " ,
2016-08-21 18:10:35 +02:00
max_length = 45 ,
required = False )
category = forms . ModelChoiceField (
2017-03-31 00:32:11 +02:00
label = " Catégorie " ,
2016-08-21 18:10:35 +02:00
queryset = ArticleCategory . objects . all ( ) ,
required = False )
2016-08-26 23:44:57 +02:00
suppliers = forms . ModelMultipleChoiceField (
2017-03-31 00:32:11 +02:00
label = " Fournisseurs " ,
2016-08-26 23:44:57 +02:00
queryset = Supplier . objects . all ( ) ,
required = False )
supplier_new = forms . CharField (
2017-03-31 00:32:11 +02:00
label = " Créer un fournisseur " ,
2016-08-26 23:44:57 +02:00
max_length = 45 ,
required = False )
def __init__ ( self , * args , * * kwargs ) :
super ( ArticleForm , self ) . __init__ ( * args , * * kwargs )
if self . instance . pk :
self . initial [ ' suppliers ' ] = self . instance . suppliers . values_list ( ' pk ' , flat = True )
2016-08-21 18:10:35 +02:00
def clean ( self ) :
category = self . cleaned_data . get ( ' category ' )
category_new = self . cleaned_data . get ( ' category_new ' )
if not category and not category_new :
raise ValidationError ( ' Sélectionnez une catégorie ou créez en une ' )
elif not category :
category , _ = ArticleCategory . objects . get_or_create ( name = category_new )
self . cleaned_data [ ' category ' ] = category
2016-08-26 23:44:57 +02:00
2016-08-21 18:10:35 +02:00
super ( ArticleForm , self ) . clean ( )
2016-08-04 08:23:34 +02:00
class Meta :
model = Article
2017-01-26 19:22:38 +01:00
fields = [ ' name ' , ' is_sold ' , ' hidden ' , ' price ' , ' stock ' , ' category ' , ' box_type ' ,
2016-08-28 05:39:34 +02:00
' box_capacity ' ]
2016-08-04 08:23:34 +02:00
class ArticleRestrictForm ( ArticleForm ) :
class Meta ( ArticleForm . Meta ) :
2017-01-26 19:22:38 +01:00
fields = [ ' name ' , ' is_sold ' , ' hidden ' , ' price ' , ' category ' , ' box_type ' ,
2016-08-28 05:39:34 +02:00
' box_capacity ' ]
2016-08-06 22:19:52 +02:00
# -----
# K-Psul forms
# -----
class KPsulOperationGroupForm ( forms . ModelForm ) :
2016-08-07 17:02:01 +02:00
checkout = forms . ModelChoiceField (
2016-08-22 02:52:59 +02:00
queryset = Checkout . objects . filter (
is_protected = False , valid_from__lte = timezone . now ( ) ,
valid_to__gte = timezone . now ( ) ) ,
2016-08-07 17:02:01 +02:00
widget = forms . HiddenInput ( ) )
2016-09-29 21:47:37 +02:00
on_acc = forms . ModelChoiceField (
queryset = Account . objects . exclude ( trigramme = ' GNR ' ) ,
widget = forms . HiddenInput ( ) )
2016-08-06 22:19:52 +02:00
class Meta :
model = OperationGroup
2016-08-23 15:43:16 +02:00
fields = [ ' on_acc ' , ' checkout ' , ' comment ' ]
2016-08-06 22:19:52 +02:00
class KPsulAccountForm ( forms . ModelForm ) :
class Meta :
model = Account
fields = [ ' trigramme ' ]
widgets = {
2016-08-16 03:36:14 +02:00
' trigramme ' : forms . TextInput (
attrs = {
' autocomplete ' : ' off ' ,
' spellcheck ' : ' false ' ,
} ) ,
2016-08-06 22:19:52 +02:00
}
2017-04-06 16:45:44 +02:00
2016-08-06 22:19:52 +02:00
class KPsulCheckoutForm ( forms . Form ) :
checkout = forms . ModelChoiceField (
2017-04-06 16:45:44 +02:00
queryset = (
Checkout . objects
. filter (
is_protected = False ,
valid_from__lte = timezone . now ( ) ,
valid_to__gte = timezone . now ( ) ,
)
) ,
widget = forms . Select ( attrs = { ' id ' : ' id_checkout_select ' } ) ,
)
2016-08-06 22:19:52 +02:00
class KPsulOperationForm ( forms . ModelForm ) :
2016-08-12 10:03:39 +02:00
article = forms . ModelChoiceField (
2016-08-14 19:59:36 +02:00
queryset = Article . objects . select_related ( ' category ' ) . all ( ) ,
2016-08-17 11:44:58 +02:00
required = False ,
widget = forms . HiddenInput ( ) )
2016-08-06 22:19:52 +02:00
class Meta :
model = Operation
2017-03-25 00:52:49 +01:00
fields = [ ' type ' , ' amount ' , ' article ' , ' article_nb ' ]
2016-08-17 11:44:58 +02:00
widgets = {
' type ' : forms . HiddenInput ( ) ,
' amount ' : forms . HiddenInput ( ) ,
' article_nb ' : forms . HiddenInput ( ) ,
}
2016-08-06 23:44:58 +02:00
2016-08-07 17:02:01 +02:00
def clean ( self ) :
super ( KPsulOperationForm , self ) . clean ( )
type_ope = self . cleaned_data . get ( ' type ' )
amount = self . cleaned_data . get ( ' amount ' )
article = self . cleaned_data . get ( ' article ' )
article_nb = self . cleaned_data . get ( ' article_nb ' )
if type_ope and type_ope == Operation . PURCHASE :
if not article or not article_nb :
raise ValidationError (
" Un achat nécessite un article et une quantité " )
if article_nb < 1 :
raise ValidationError ( " Impossible d ' acheter moins de 1 article " )
elif type_ope and type_ope in [ Operation . DEPOSIT , Operation . WITHDRAW ] :
if not amount or article or article_nb :
raise ValidationError ( " Bad request " )
if type_ope == Operation . DEPOSIT and amount < = 0 :
raise ValidationError ( " Charge non positive " )
if type_ope == Operation . WITHDRAW and amount > = 0 :
raise ValidationError ( " Retrait non négatif " )
2016-08-07 23:41:46 +02:00
self . cleaned_data [ ' article ' ] = None
self . cleaned_data [ ' article_nb ' ] = None
2016-08-07 17:02:01 +02:00
2016-08-06 23:44:58 +02:00
KPsulOperationFormSet = modelformset_factory (
2016-08-07 23:41:46 +02:00
Operation ,
form = KPsulOperationForm ,
2016-08-17 11:44:58 +02:00
can_delete = True ,
extra = 0 ,
2016-08-07 23:41:46 +02:00
min_num = 1 , validate_min = True )
2016-08-22 03:57:13 +02:00
2016-08-22 05:41:31 +02:00
class AddcostForm ( forms . Form ) :
trigramme = forms . CharField ( required = False )
amount = forms . DecimalField (
required = False ,
max_digits = 6 , decimal_places = 2 , min_value = Decimal ( 0 ) )
def clean ( self ) :
trigramme = self . cleaned_data . get ( ' trigramme ' )
if trigramme :
try :
Account . objects . get ( trigramme = trigramme )
except Account . DoesNotExist :
raise ValidationError ( ' Compte invalide ' )
else :
self . cleaned_data [ ' amount ' ] = 0
super ( AddcostForm , self ) . clean ( )
2017-04-03 20:32:16 +02:00
2016-08-24 19:52:07 +02:00
class FilterHistoryForm ( forms . Form ) :
2017-06-12 01:51:10 +02:00
checkouts = forms . ModelMultipleChoiceField ( queryset = Checkout . objects . all ( ) )
accounts = forms . ModelMultipleChoiceField ( queryset = Account . objects . all ( ) )
from_date = forms . DateTimeField ( widget = DateTimeWidget )
to_date = forms . DateTimeField ( widget = DateTimeWidget )
2016-08-26 15:30:40 +02:00
# -----
# Transfer forms
# -----
class TransferGroupForm ( forms . ModelForm ) :
class Meta :
model = TransferGroup
fields = [ ' comment ' ]
class TransferForm ( forms . ModelForm ) :
from_acc = forms . ModelChoiceField (
2016-09-29 21:47:37 +02:00
queryset = Account . objects . exclude ( trigramme__in = [ ' LIQ ' , ' #13 ' , ' GNR ' ] ) ,
2016-08-26 15:30:40 +02:00
widget = forms . HiddenInput ( )
)
to_acc = forms . ModelChoiceField (
2016-09-29 21:47:37 +02:00
queryset = Account . objects . exclude ( trigramme__in = [ ' LIQ ' , ' #13 ' , ' GNR ' ] ) ,
2016-08-26 15:30:40 +02:00
widget = forms . HiddenInput ( )
)
def clean_amount ( self ) :
amount = self . cleaned_data [ ' amount ' ]
if amount < = 0 :
raise forms . ValidationError ( " Montant invalide " )
return amount
class Meta :
model = Transfer
fields = [ ' from_acc ' , ' to_acc ' , ' amount ' ]
TransferFormSet = modelformset_factory (
Transfer ,
form = TransferForm ,
min_num = 1 , validate_min = True ,
extra = 9 ,
)
2016-08-27 14:12:01 +02:00
# -----
# Inventory forms
# -----
class InventoryArticleForm ( forms . Form ) :
article = forms . ModelChoiceField (
queryset = Article . objects . all ( ) ,
widget = forms . HiddenInput ( ) ,
)
2017-03-31 16:07:37 +02:00
stock_new = forms . IntegerField ( required = False )
2016-08-27 14:12:01 +02:00
def __init__ ( self , * args , * * kwargs ) :
super ( InventoryArticleForm , self ) . __init__ ( * args , * * kwargs )
if ' initial ' in kwargs :
self . name = kwargs [ ' initial ' ] [ ' name ' ]
self . stock_old = kwargs [ ' initial ' ] [ ' stock_old ' ]
self . category = kwargs [ ' initial ' ] [ ' category ' ]
self . category_name = kwargs [ ' initial ' ] [ ' category__name ' ]
2017-03-29 04:47:41 +02:00
self . box_capacity = kwargs [ ' initial ' ] [ ' box_capacity ' ]
2016-08-28 05:39:34 +02:00
# -----
# Order forms
# -----
2017-02-12 05:03:41 +01:00
2017-02-12 21:04:50 +01:00
2016-08-28 05:39:34 +02:00
class OrderArticleForm ( forms . Form ) :
article = forms . ModelChoiceField (
2017-02-12 05:03:41 +01:00
queryset = Article . objects . all ( ) ,
widget = forms . HiddenInput ( ) ,
2016-08-28 05:39:34 +02:00
)
2017-03-31 16:24:38 +02:00
quantity_ordered = forms . IntegerField ( required = False )
2016-08-28 05:39:34 +02:00
def __init__ ( self , * args , * * kwargs ) :
super ( OrderArticleForm , self ) . __init__ ( * args , * * kwargs )
if ' initial ' in kwargs :
self . name = kwargs [ ' initial ' ] [ ' name ' ]
self . stock = kwargs [ ' initial ' ] [ ' stock ' ]
self . category = kwargs [ ' initial ' ] [ ' category ' ]
self . category_name = kwargs [ ' initial ' ] [ ' category__name ' ]
self . box_capacity = kwargs [ ' initial ' ] [ ' box_capacity ' ]
2017-05-19 16:42:26 +02:00
self . v_all = kwargs [ ' initial ' ] [ ' v_all ' ]
2016-08-28 05:39:34 +02:00
self . v_moy = kwargs [ ' initial ' ] [ ' v_moy ' ]
self . v_et = kwargs [ ' initial ' ] [ ' v_et ' ]
self . v_prev = kwargs [ ' initial ' ] [ ' v_prev ' ]
self . c_rec = kwargs [ ' initial ' ] [ ' c_rec ' ]
2016-08-30 15:35:30 +02:00
class OrderArticleToInventoryForm ( forms . Form ) :
article = forms . ModelChoiceField (
queryset = Article . objects . all ( ) ,
widget = forms . HiddenInput ( ) ,
)
price_HT = forms . DecimalField (
max_digits = 7 , decimal_places = 4 ,
2017-03-31 16:24:38 +02:00
required = False )
2016-08-30 15:35:30 +02:00
TVA = forms . DecimalField (
max_digits = 7 , decimal_places = 2 ,
2017-03-31 16:24:38 +02:00
required = False )
2016-08-30 15:35:30 +02:00
rights = forms . DecimalField (
max_digits = 7 , decimal_places = 4 ,
2017-03-31 16:24:38 +02:00
required = False )
quantity_received = forms . IntegerField ( )
2016-08-30 15:35:30 +02:00
def __init__ ( self , * args , * * kwargs ) :
super ( OrderArticleToInventoryForm , self ) . __init__ ( * args , * * kwargs )
if ' initial ' in kwargs :
self . name = kwargs [ ' initial ' ] [ ' name ' ]
self . category = kwargs [ ' initial ' ] [ ' category ' ]
self . category_name = kwargs [ ' initial ' ] [ ' category__name ' ]
self . quantity_ordered = kwargs [ ' initial ' ] [ ' quantity_ordered ' ]