class ProducerInvoicedForm(forms.Form): selected = forms.BooleanField(required=False) id = forms.IntegerField(label=EMPTY_STRING) short_profile_name = forms.CharField(label=_("Short name"), max_length=25, required=False) calculated_invoiced_balance = FormMoneyField( label=_("Amount due to the producer as calculated by Repanier"), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO, ) to_be_invoiced_balance = FormMoneyField( label=_("Amount claimed by the producer"), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO, ) invoice_reference = forms.CharField(label=_("Invoice reference"), max_length=100, required=False) producer_price_are_wo_vat = forms.BooleanField(required=False) def __init__(self, *args, **kwargs): super(ProducerInvoicedForm, self).__init__(*args, **kwargs) self.fields["id"].widget.attrs["readonly"] = True # self.fields["id"].widget.attrs["hidden"] = True self.fields["to_be_invoiced_balance"].widget.attrs[ "style"] = "width:100px !important" self.fields["invoice_reference"].widget.attrs[ "style"] = "width:250px !important" self.fields["calculated_invoiced_balance"].widget.attrs[ "readonly"] = True
class CustomerSendForm(forms.ModelForm): offer_purchase_price = FormMoneyField(label=_("Producer amount invoiced"), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) offer_selling_price = FormMoneyField( label=_("Invoiced to the consumer including tax"), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) rule_of_3 = forms.BooleanField(label=_("Apply the rule of 3"), required=False, initial=False) def __init__(self, *args, **kwargs): super(CustomerSendForm, self).__init__(*args, **kwargs) customer_producer_invoice = self.instance self.fields[ "offer_purchase_price"].initial = customer_producer_invoice.total_purchase_with_tax self.fields[ "offer_selling_price"].initial = customer_producer_invoice.total_selling_with_tax if customer_producer_invoice.producer.price_list_multiplier >= DECIMAL_ONE: self.fields["offer_selling_price"].widget = forms.HiddenInput() else: self.fields["offer_purchase_price"].widget = forms.HiddenInput()
class ProducerInvoicedForm(forms.Form): selected = forms.BooleanField(required=False) short_profile_name = forms.CharField(label=_("short_profile_name"), max_length=25, required=False) calculated_invoiced_balance = FormMoneyField( label=_("calculated balance to be invoiced"), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) to_be_invoiced_balance = FormMoneyField( label=_("balance to be invoiced"), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) invoice_reference = forms.CharField(label=_("invoice reference"), max_length=100, required=False) def __init__(self, *args, **kwargs): super(ProducerInvoicedForm, self).__init__(*args, **kwargs) self.fields["to_be_invoiced_balance"].widget.attrs['style'] = "width:100px !important" self.fields["invoice_reference"].widget.attrs['style'] = "width:250px !important" self.fields["calculated_invoiced_balance"].widget.attrs['readonly'] = True
class OfferItemPurchaseSendInlineForm(forms.ModelForm): previous_purchase_price = FormMoneyField( label=_("purchase price"), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) previous_customer = forms.ModelChoiceField( Customer.objects.none(), required=False) def __init__(self, *args, **kwargs): super(OfferItemPurchaseSendInlineForm, self).__init__(*args, **kwargs) if self.instance.id is not None: self.fields["previous_purchase_price"].initial = self.instance.purchase_price self.fields["previous_customer"].initial = self.instance.customer self.fields["customer"].widget.can_add_related = False self.fields["customer"].widget.can_delete_related = False if self.instance.offer_item.order_unit not in [PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG]: self.fields["purchase_price"].widget.attrs['readonly'] = True class Meta: widgets = { 'customer': Select2(select2attrs={'width': '450px'}) }
class CustomerPurchaseSendInlineForm(forms.ModelForm): previous_purchase_price = FormMoneyField( max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) previous_offer_item = forms.ModelChoiceField( OfferItemSend.objects.none(), required=False) def __init__(self, *args, **kwargs): super(CustomerPurchaseSendInlineForm, self).__init__(*args, **kwargs) purchase = self.instance self.fields["previous_purchase_price"].initial = purchase.purchase_price try: offer_item = purchase.offer_item except AttributeError: offer_item = None self.fields["previous_offer_item"].initial = offer_item class Meta: widgets = { 'offer_item': Select2(select2attrs={'width': '450px'}) }
class BankAccountDataForm(forms.ModelForm): customer = CustomerModelChoiceField( queryset=Customer.objects.filter(is_active=True), label=_("customer"), required=False, widget=apply_select2(forms.Select) ) producer = ProducerModelChoiceField( queryset=Producer.objects.filter( represent_this_buyinggroup=False, is_active=True ), label=_("producer"), required=False, widget=apply_select2(forms.Select) ) bank_amount_in = FormMoneyField( label=_("bank_amount_in"), help_text=_('payment_on_the_account'), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) bank_amount_out = FormMoneyField( label=_("bank_amount_out"), help_text=_('payment_from_the_account'), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) def __init__(self, *args, **kwargs): super(BankAccountDataForm, self).__init__(*args, **kwargs) bank_account = self.instance if bank_account.id is not None: self.fields["bank_amount_in"].initial = bank_account.bank_amount_in self.fields["bank_amount_out"].initial = bank_account.bank_amount_out self.fields["customer"].widget.attrs['readonly'] = True self.fields["customer"].disabled = True self.fields["producer"].widget.attrs['readonly'] = True self.fields["producer"].disabled = True if bank_account.customer is None: self.fields["customer"].choices = [('', _("---------"))] else: self.fields["customer"].empty_label = None self.fields["customer"].queryset = Customer.objects.filter(id=bank_account.customer_id) if bank_account.producer is None: self.fields["producer"].choices = [('', _("---------"))] else: self.fields["producer"].empty_label = None self.fields["producer"].queryset = Producer.objects.filter(id=bank_account.producer_id) if (bank_account.customer_invoice is not None or bank_account.producer_invoice is not None) or ( bank_account.customer is None and bank_account.producer is None): self.fields["operation_date"].widget.attrs['readonly'] = True self.fields["operation_date"].disabled = True self.fields["bank_amount_in"].widget.attrs['readonly'] = True self.fields["bank_amount_in"].disabled = True self.fields["bank_amount_out"].widget.attrs['readonly'] = True self.fields["bank_amount_out"].disabled = True if bank_account.customer is None and bank_account.producer is None: self.fields["operation_comment"].widget.attrs['readonly'] = True self.fields["operation_comment"].disabled = True else: self.fields["producer"].queryset = Producer.objects.filter( represent_this_buyinggroup=False, is_active=True ) self.fields["customer"].queryset = Customer.objects.filter( is_active=True ) def clean(self): if any(self.errors): # Don't bother validating the formset unless each form is valid on its own return customer = self.cleaned_data.get("customer") producer = self.cleaned_data.get("producer") initial_id = self.instance.id initial_customer = self.instance.customer initial_producer = self.instance.producer if customer is None and producer is None: if initial_id is not None: if initial_customer is None and initial_producer is None: raise forms.ValidationError( _('You may not update a balance.') ) else: self.add_error('customer', _('Either a customer or a producer must be given.')) self.add_error('producer', _('Either a customer or a producer must be given.')) else: bank_account = BankAccount.objects.filter(operation_status=BANK_LATEST_TOTAL).order_by('?').first() if bank_account: # You may only insert the first latest bank total at initialisation of the website self.add_error('customer', _('Either a customer or a producer must be given.')) self.add_error('producer', _('Either a customer or a producer must be given.')) else: if self.instance.customer_invoice is not None or self.instance.producer_invoice is not None: raise forms.ValidationError( _('You may not update a bank operation linked to an invoice.') ) if customer is not None and producer is not None: self.add_error('customer', _('Only one customer or one producer must be given.')) self.add_error('producer', _('Only one customer or one producer must be given.')) latest_total = BankAccount.objects.filter(operation_status=BANK_LATEST_TOTAL).order_by('?').first() if latest_total is not None: operation_date = self.cleaned_data.get("operation_date") if operation_date < latest_total.operation_date: self.add_error('operation_date', _('The operation date must be greater or equal to the latest total operation date.')) class Meta: model = BankAccount fields = "__all__"
class OfferItemSendDataForm(forms.ModelForm): offer_purchase_price = FormMoneyField( label=_("producer amount invoiced"), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) rule_of_3 = forms.BooleanField( label=_("apply rule of three"), required=False, initial=False) qty_delivered = forms.DecimalField( min_value=DECIMAL_ZERO, label=_("stock delivered"), max_digits=9, decimal_places=4, required=False, initial=0 ) qty_prepared = forms.DecimalField( label=_("qty prepared"), max_digits=9, decimal_places=4, required=False, initial=0) previous_producer_unit_price = FormMoneyField( max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) previous_customer_unit_price = FormMoneyField( max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) previous_unit_deposit = FormMoneyField( max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) def __init__(self, *args, **kwargs): getcontext().rounding = ROUND_HALF_UP super(OfferItemSendDataForm, self).__init__(*args, **kwargs) offer_item = self.instance self.fields["previous_producer_unit_price"].initial = offer_item.producer_unit_price self.fields["previous_customer_unit_price"].initial = offer_item.customer_unit_price self.fields["previous_unit_deposit"].initial = offer_item.unit_deposit if offer_item.manage_replenishment: invoiced_qty, taken_from_stock, customer_qty = offer_item.get_producer_qty_stock_invoiced() self.fields["offer_purchase_price"].initial = RepanierMoney( offer_item.total_purchase_with_tax.amount - ( (offer_item.producer_unit_price.amount + offer_item.unit_deposit.amount) * taken_from_stock ).quantize(TWO_DECIMALS), 2) self.fields["qty_delivered"].initial = invoiced_qty.quantize(FOUR_DECIMALS) self.fields["qty_prepared"].initial = customer_qty.quantize( FOUR_DECIMALS) self.fields["qty_prepared"].widget.attrs['readonly'] = True self.fields["qty_prepared"].disabled = True else: self.fields["offer_purchase_price"].initial = offer_item.total_purchase_with_tax if offer_item.wrapped or offer_item.order_unit not in [PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG] or offer_item.manage_replenishment: self.fields["offer_purchase_price"].widget.attrs['readonly'] = True self.fields["offer_purchase_price"].disabled = True if offer_item.producer_price_are_wo_vat: self.fields["offer_purchase_price"].label = _("producer amount invoiced wo tax") def get_readonly_fields(self, request, obj=None): if obj.order_unit in [PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG]: if obj.manage_replenishment: return ['product', 'qty_prepared'] else: return ['product'] else: if obj.manage_replenishment: return ['offer_purchase_price', 'product', 'qty_prepared'] else: return ['offer_purchase_price', 'product'] def save(self, *args, **kwargs): offer_item = super(OfferItemSendDataForm, self).save(*args, **kwargs) if offer_item.id is not None: previous_producer_unit_price = self.cleaned_data["previous_producer_unit_price"] previous_customer_unit_price = self.cleaned_data.get("previous_customer_unit_price", DECIMAL_ZERO) previous_unit_deposit = self.cleaned_data["previous_unit_deposit"] producer_unit_price = self.cleaned_data["producer_unit_price"] customer_unit_price = self.cleaned_data.get("customer_unit_price", offer_item.customer_unit_price) unit_deposit = self.cleaned_data["unit_deposit"] if offer_item.manage_replenishment \ or previous_producer_unit_price != producer_unit_price \ or previous_customer_unit_price != customer_unit_price \ or previous_unit_deposit != unit_deposit: offer_item.producer_unit_price = producer_unit_price offer_item.customer_unit_price = customer_unit_price offer_item.unit_deposit = unit_deposit # The previous save is called with "commit=False" or we need to update the producer # to recalculate the offer item prices. So a call to self.instance.save() is required offer_item.save() # Important : linked with vvvv return offer_item
class GroupWithUserDataForm(UserDataForm): customers = forms.ModelMultipleChoiceField( Customer.objects.filter( may_order=True, delivery_point__isnull=True, represent_this_buyinggroup=False ), label=_('Members'), widget=admin.widgets.FilteredSelectMultiple(_('Members'), False), required=False ) inform_customer_responsible = forms.BooleanField( label=_('Inform the group of orders placed by its members'), required=False ) transport = FormMoneyField( label=_("Delivery point shipping cost"), # help_text=_("This amount is added once for groups with entitled customer or at each customer for open groups."), max_digits=5, decimal_places=2, validators=[MinValueValidator(0)], initial=REPANIER_MONEY_ZERO, required=False ) min_transport = FormMoneyField( label=_("Minium order amount for free shipping cost"), # help_text=_("This is the minimum order amount to avoid shipping cost."), max_digits=5, decimal_places=2, validators=[MinValueValidator(0)], initial=REPANIER_MONEY_ZERO, required=False ) def __init__(self, *args, **kwargs): super(GroupWithUserDataForm, self).__init__(*args, **kwargs) if self.instance.id: delivery_point = LUT_DeliveryPoint.objects.filter( customer_responsible_id=self.instance.id ).order_by('?').first() if delivery_point is not None: self.fields['customers'].initial = Customer.objects.filter( delivery_point_id=delivery_point.id ) self.fields['customers'].queryset = Customer.objects.filter( Q(may_order=True, delivery_point__isnull=True) | Q(delivery_point_id=delivery_point.id) ).distinct() self.fields['inform_customer_responsible'].initial = delivery_point.inform_customer_responsible self.fields['transport'].initial = delivery_point.transport self.fields['min_transport'].initial = delivery_point.min_transport else: self.fields['customers'].initial = Customer.objects.none() self.fields['customers'].queryset = Customer.objects.filter( Q(may_order=True, delivery_point__isnull=True) ) # def save(self, *args, **kwargs): # super(GroupWithUserDataForm, self).save(*args, **kwargs) # self.selected_customers = self.cleaned_data['customers'] # self.selected_inform = self.cleaned_data['inform_customer_responsible'] # self.selected_transport = self.cleaned_data['transport'] # self.selected_min_transport = self.cleaned_data['min_transport'] # return self.instance class Meta: widgets = { 'address': Textarea(attrs={'rows': 4, 'cols': 80, 'style': 'height: 5em; width: 30em;'}), 'memo': Textarea(attrs={'rows': 4, 'cols': 160, 'style': 'height: 5em; width: 60em;'}), } model = Group fields = "__all__"
class OfferItemSendDataForm(forms.ModelForm): offer_purchase_price = FormMoneyField(label=_("Producer amount invoiced"), max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) rule_of_3 = forms.BooleanField(label=_("Apply the rule of 3"), required=False, initial=False) qty_delivered = forms.DecimalField(min_value=DECIMAL_ZERO, label=_("Stock delivered"), max_digits=9, decimal_places=4, required=False, initial=0) qty_prepared = forms.DecimalField(label=_("Qty prepared"), max_digits=9, decimal_places=4, required=False, initial=0) previous_producer_unit_price = FormMoneyField(max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) previous_customer_unit_price = FormMoneyField(max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) previous_unit_deposit = FormMoneyField(max_digits=8, decimal_places=2, required=False, initial=REPANIER_MONEY_ZERO) def __init__(self, *args, **kwargs): super(OfferItemSendDataForm, self).__init__(*args, **kwargs) offer_item = self.instance self.fields[ "previous_producer_unit_price"].initial = offer_item.producer_unit_price self.fields[ "previous_customer_unit_price"].initial = offer_item.customer_unit_price self.fields["previous_unit_deposit"].initial = offer_item.unit_deposit self.fields[ "offer_purchase_price"].initial = offer_item.total_purchase_with_tax if offer_item.wrapped or offer_item.order_unit not in [ PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG ]: self.fields["offer_purchase_price"].widget.attrs['readonly'] = True self.fields["offer_purchase_price"].disabled = True if offer_item.producer_price_are_wo_vat: self.fields["offer_purchase_price"].label = _( "Producer amount invoiced wo VAT") def get_readonly_fields(self, request, obj=None): if obj.order_unit in [PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG]: return ['product'] else: return ['offer_purchase_price', 'product'] def save(self, *args, **kwargs): offer_item = super(OfferItemSendDataForm, self).save(*args, **kwargs) if offer_item.id is not None: previous_producer_unit_price = self.cleaned_data[ "previous_producer_unit_price"] previous_customer_unit_price = self.cleaned_data.get( "previous_customer_unit_price", DECIMAL_ZERO) previous_unit_deposit = self.cleaned_data["previous_unit_deposit"] producer_unit_price = self.cleaned_data["producer_unit_price"] customer_unit_price = self.cleaned_data.get( "customer_unit_price", offer_item.customer_unit_price) unit_deposit = self.cleaned_data["unit_deposit"] if previous_producer_unit_price != producer_unit_price \ or previous_customer_unit_price != customer_unit_price \ or previous_unit_deposit != unit_deposit: offer_item.producer_unit_price = producer_unit_price offer_item.customer_unit_price = customer_unit_price offer_item.unit_deposit = unit_deposit # The previous save is called with "commit=False" or we need to update the producer # to recalculate the offer item prices. So a call to self.instance.save() is required offer_item.save() # Important : linked with vvvv return offer_item