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, })
def waybill_finalize_receipt(request, waybill_pk, template_name, queryset): """Signs reception""" waybill = get_object_or_404(queryset, pk = waybill_pk) waybill.receipt_sign() create_logentry(request, waybill, LOGENTRY_SIGN_RECEIVE) return render_to_pdf(request, template_name, { 'print_original': True, 'object': waybill, 'items': waybill.loading_details.select_related(), }, 'waybill-%s' % waybill.pk)
def waybill_finalize_dispatch(request, waybill_pk, template_name, queryset): """ called when user pushes Print Original on dispatch Redirects to order details """ waybill = get_object_or_404(queryset, pk = waybill_pk) waybill.dispatch_sign() create_logentry(request, waybill, LOGENTRY_SIGN_DISPATCH) return render_to_pdf(request, template_name, { 'print_original': True, 'object': waybill, 'items': waybill.loading_details.select_related(), }, 'waybill-%s' % waybill.pk)
def validate_receipt(request, waybill_pk, queryset): """Sets receipt 'validated' flag. It allows system to submit this waybill to COMPAS.""" waybill = get_object_or_404(queryset, pk = waybill_pk) waybill.receipt_validated = True waybill.save() create_logentry(request, waybill, LOGENTRY_VALIDATE_RECEIVE) message = _('eWaybill %(waybill)s has been validated. It will be sent in few minutes.') % { 'waybill': waybill.pk } if request.is_ajax(): return HttpResponse(message) messages.add_message(request, messages.INFO, message) return redirect('receipt_validates')
def waybill_delete(request, waybill_pk, queryset, redirect_to=''): """Deletes specific waybill""" waybill = get_object_or_404(queryset, pk = waybill_pk) create_logentry(request, waybill, LOGENTRY_DELETE_WAYBILL) waybill.delete() redirect_to = redirect_to or request.GET.get('redirect_to', '') messages.info(request, _('eWaybill %(number)s has now been Removed') % {"number": waybill.pk}) if redirect_to: return redirect(redirect_to) elif request.META.has_key('HTTP_REFERER'): return redirect(request.META['HTTP_REFERER']) else: return redirect('index')
def validate_dispatch(request, waybill_pk, queryset): """Sets dispatch 'validated' flag. It allows system to submit this waybill to COMPAS.""" waybill = get_object_or_404(queryset, pk = waybill_pk) waybill.validated = True waybill.save() create_logentry(request, waybill, LOGENTRY_VALIDATE_DISPATCH) messages.add_message(request, messages.INFO, _('eWaybill %(waybill)s has been validated. It will be sent in few minutes.') % { 'waybill': waybill.pk, }) if request.is_ajax(): return HttpResponse("") return redirect('dispatch_validates')
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, })