Exemplo n.º 1
0
Arquivo: views.py Projeto: Arpaso/ETS
def _dispatching(request, waybill, template, success_message, created=False, form_class=DispatchWaybillForm, 
                formset_form=LoadingDetailDispatchForm, formset_class=BaseLoadingDetailFormSet):
    """Private function with common functionality for creating and editing dispatching waybill"""
    order = waybill.order
    
    class FormsetForm(formset_form):
        stock_item = forms.ModelChoiceField(queryset=order.get_stock_items(), label=_('Stock Item'), 
                                            empty_label=_("Choose stock item"))
        stock_item.choices = [(u"", stock_item.empty_label),]
        stock_item.choices+=[(item.pk, u"%s  - %s(kg)" % (unicode(item), item.unit_weight_net ))  
                             for item in order.get_stock_items().exclude(quantity_net=0)]
    
    loading_formset = inlineformset_factory(ets.models.Waybill, ets.models.LoadingDetail, 
                       form=FormsetForm, formset=formset_class, 
                       extra=1, max_num=5,
                       can_order=False, can_delete=True)\
        (request.POST or None, request.FILES or None, prefix='item', instance=waybill)
    
    form = form_class(data=request.POST or None, files=request.FILES or None, instance=waybill)
    
    #Filter choices
    #Warehouse
    warehouses = ets.models.Warehouse.get_warehouses(order.location, order.consignee).exclude(pk=order.warehouse.pk)
    
    form.fields['destination'].queryset = warehouses

    def get_transaction_type_choice(*args):
        return ((k, v) for k, v in form.fields['transaction_type'].choices if k in args)
    
    #Transaction type
    if order.consignee.pk == WFP_ORGANIZATION:
        form.fields['transaction_type'].choices = get_transaction_type_choice(ets.models.Waybill.INTERNAL_TRANSFER, 
                                                                              ets.models.Waybill.SHUNTING)
        form.fields['destination'].empty_label = None
    elif order.consignee.pk == WFP_DISTRUIBUTION:
        form.fields['transaction_type'].choices = get_transaction_type_choice(ets.models.Waybill.DISTIBRUTION)
    else:
        form.fields['transaction_type'].choices = get_transaction_type_choice(ets.models.Waybill.DELIVERY)
    
    if form.is_valid() and loading_formset.is_valid():
        waybill = form.save()
        loading_formset.save()
        
        if created:
            create_logentry(request, waybill, LOGENTRY_CREATE_WAYBILL)
        else:
            create_logentry(request, waybill, LOGENTRY_EDIT_DISPATCH, construct_change_message(request, form, [loading_formset]))
        
        messages.success(request, success_message)
        return redirect(waybill)
        
    return direct_to_template( request, template, {
        'form': form, 
        'formset': loading_formset,
        'object': order,
        'waybill': waybill,
    })
Exemplo n.º 2
0
Arquivo: views.py Projeto: Arpaso/ETS
def waybill_reception(request, waybill_pk, queryset, form_class=WaybillRecieptForm, 
                      formset_form = LoadingDetailReceiptForm,
                      template='waybill/receive.html'):
    """Waybill reception view""" 
    waybill = get_object_or_404(queryset, pk=waybill_pk)
    waybill.receipt_person = request.user.person
    
    loading_formset = inlineformset_factory(ets.models.Waybill, ets.models.LoadingDetail, 
                                            form=formset_form, extra=0, max_num=5,
                                            can_order=False, can_delete=False)\
                                (request.POST or None, request.FILES or None, instance=waybill, prefix='item')
    
    today = datetime.date.today()
    form = form_class(data=request.POST or None, files=request.FILES or None, initial = {
        'arrival_date': today,
        'start_discharge_date': today,
        'end_discharge_date': today,
        'receipt_warehouse': waybill.receipt_warehouse or waybill.destination,
    }, instance=waybill)
    
    form.fields['receipt_warehouse'].queryset = request.user.person.warehouses.all().exclude(pk=waybill.order.warehouse.pk)
    form.fields['receipt_warehouse'].empty_label = None
    
    if form.is_valid() and loading_formset.is_valid():
        waybill = form.save()
        loading_formset.save()
        create_logentry(request, waybill, LOGENTRY_EDIT_RECEIVE, construct_change_message(request, form, [loading_formset]))
        
        messages.add_message(request, messages.INFO, _('eWaybill has been discharged'))
        
        return redirect(waybill)
    
    return direct_to_template(request, template, {
        'form': form, 
        'formset': loading_formset,
        'waybill': waybill,
    })