Exemplo n.º 1
0
def zone(request,
         pk=None,
         region_pk=None,
         action=None,
         template_name="meter/location_form.html"):
    data = {}
    ZoneForm = ZoneConRegionForm if region_pk else ZoneSinRegionForm
    if request.method == "GET":
        if action in ("C", "U", "R"):
            if pk:
                data['zone'] = get_object_or_404(Zone, pk=pk)
                data['subzones'] = data['zone'].subzone_set.all()
                if action == "U":
                    zoneForm = ZoneForm(
                        instance=get_object_or_404(Zone, pk=pk))
            else:
                zoneForm = ZoneForm()
    if request.method == "POST":
        if action in ("C", "U", "D"):
            if action in ("C", "U"):
                if pk:
                    zoneForm = ZoneForm(request.POST,
                                        instance=get_object_or_404(Zone,
                                                                   pk=pk))
                else:
                    zoneForm = ZoneForm(request.POST)
                if zoneForm.is_valid():
                    zone = zoneForm.save(commit=False)
                    if region_pk:
                        zone.region = get_object_or_404(Region, id=region_pk)
                    zone.save()
                    return HttpResponseRedirect(
                        reverse("zone-details", args=[zone.pk]))
            else:
                if checkReceptionist(request):
                    return HttpResponseRedirect(reverse("subzone-list"))
                zone = get_object_or_404(Zone, pk=pk)
                zone.delete()
    if action in ("C", "U"):
        if checkReceptionist(request):
            return HttpResponseRedirect(reverse("zone-list"))
        if checkDirector(request):
            return HttpResponseRedirect(reverse("zone-list"))
        data['zoneForm'] = zoneForm
        data['locationForm'] = zoneForm
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name,
                                  data,
                                  context_instance=RequestContext(request))
Exemplo n.º 2
0
def meter_readings(request,
                   pk=None,
                   action=None,
                   template_name="meter/upload_readings.html"):
    data = {}
    if request.method == "GET":
        if action == 'C':
            form = MeterReadingsForm()
    if request.method == 'POST':
        form = MeterReadingsForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['meter_readings_file'])
            data = upload_readings(request.user.pk)
            form = MeterReadingsForm()
            #return HttpResponseRedirect(reverse("uploaded-readings"))
        else:
            form = MeterReadingsForm()
    if action == 'C':
        if checkReceptionist(request):
            return HttpResponseRedirect(reverse("subzone-list"))
        if checkDirector(request):
            return HttpResponseRedirect(reverse("home"))
        data['form'] = form
    return render_to_response(template_name,
                              data,
                              context_instance=RequestContext(request))
Exemplo n.º 3
0
def consumption(request, pk=None, action=None, consumer_type=None, boundary=None, template_name="consumer/consumption_form.html"):
    data = {}
    ConsumptionForm = BoundaryRateForm if boundary != None else NormConsumptionForm
    if request.method == "GET":
        if action in ("create", "read", "update"):
            if pk:
                data['consumption'] = get_object_or_404(Consumption, pk=pk)
                data['consumption_rates'] = previous_and_current(Consumption.objects.filter(consumer_type=data['consumption'].consumer_type))
                if action == "update":
                    consumptionForm = ConsumptionForm(instance=get_object_or_404(Consumption, pk=pk))
            elif consumer_type:
                data['consumertype'] = get_object_or_404(ConsumerType, pk=consumer_type)
                rates = data['consumertype'].consumption_set.all()
                data['consumption_rates'] = previous_and_current(list(rates))
                consumptionForm = ConsumptionForm()
        else:
            return HttpResponseForbidden()
    elif request.method == "POST":
        if action in ("create", "update"):
            if pk:
                consumptionForm = ConsumptionForm(request.POST, instance=get_object_or_404(Consumption, pk=pk))
            else:
                consumptionForm = ConsumptionForm(request.POST)
            if consumptionForm.is_valid():
                if action == "create":
                    rates = Consumption.objects.filter(consumer_type=ConsumerType.objects.filter( pk=consumer_type)).reverse()
                    if rates:
                        rate = rates[0]
                        consumption = consumptionForm.save(commit=False)
                        if rate.border_rate:
                            if rate.max_unit < consumption.max_unit:
                                rate.max_unit = consumption.max_unit +1
                                rate.save()
                        if boundary:
                            consumption.max_unit = rate.max_unit +1 
                            consumption.border_rate = True
                        if consumer_type:
                            consumption.consumer_type = get_object_or_404(ConsumerType, pk=consumer_type)
                        consumption.save()
                    else:
                        consumption = consumptionForm.save(commit=False)
                        if consumer_type:
                            consumption.consumer_type = get_object_or_404(ConsumerType, pk=consumer_type)
                        consumption.save()
                elif action == "update":
                    consumption = consumptionForm.save(commit=False)
                    consumption.save()
                return HttpResponseRedirect(reverse("consumer-type-details", args=[consumption.consumer_type.pk]))
        else:
            consumption = get_object_or_404(Consumption, pk=pk)
            consumption.delete()
    if action in ("create", "update"):
        if checkReceptionist(request):
            return HttpResponseRedirect(reverse("consumer-type-list"))
        data['consumptionForm'] = consumptionForm
    
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name, data, context_instance=RequestContext(request))
Exemplo n.º 4
0
def subzone(request, pk=None, zone_pk=None, action=None, template_name="meter/location_form.html"):
    data = {}
    SubZoneForm = SubZoneConForm if zone_pk else SubZoneSinForm
    if request.method == "GET":
        if action in ("C", "U", "R"):
            if pk:
                data['subzone'] = get_object_or_404(SubZone, pk=pk)
                data['plots'] = data['subzone'].plot_set.all()
                if action == "U":
                    subzoneForm = SubZoneForm(instance=get_object_or_404(SubZone, pk=pk))
            else:
                subzoneForm = SubZoneForm()
    if request.method == "POST":
        if action in ("C", "U", "D"):
            if action in ("C", "U"):
                if pk:
                    subzoneForm = SubZoneForm(request.POST, instance=get_object_or_404(SubZone, pk=pk))
                else:
                    subzoneForm = SubZoneForm(request.POST)
                if subzoneForm.is_valid():
                    subzone = subzoneForm.save(commit=False)
                    subzone.save()
                    return HttpResponseRedirect(reverse("subzone-details", args=[subzone.pk]))
            else:
                if checkReceptionist(request):
                    return HttpResponseRedirect(reverse("subzone-list"))
                subzone = get_object_or_404(SubZone, pk=pk)
                if zone_pk:
                    subzone.zone = get_object_or_404(Zone, id=region_pk)
                subzone.delete()
    if action in ("C", "U"):
        if checkReceptionist(request):
            return HttpResponseRedirect(reverse("subzone-list"))
        if checkDirector(request):
            return HttpResponseRedirect(reverse("subzone-list"))
        data['subzoneForm'] = subzoneForm
        data['locationForm'] = subzoneForm
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name, data, context_instance=RequestContext(request))
Exemplo n.º 5
0
def account(request,
            pk=None,
            action=None,
            template_name="consumer/consumer_account.html"):
    data = {}
    if request.method == "GET":
        if pk:
            data['account'] = get_object_or_404(Account, pk=pk)
            accountForm = AccountForm(
                instance=get_object_or_404(Account, pk=pk))
    if request.method == "POST":
        if action in ("C", "U", "R"):
            if action in ("U", "R"):
                if checkDirector(request):
                    return HttpResponseRedirect(reverse("account-list"))
            if pk:
                accountForm = AccountForm(request.POST,
                                          instance=get_object_or_404(Account,
                                                                     pk=pk))
            else:
                accountForm = AccountForm(request.POST)
            if accountForm.is_valid():
                account = accountForm.save(commit=False)
                account.date_activated = datetime.datetime.today()
                account.save()
                return HttpResponseRedirect(
                    reverse("consumer-details",
                            args=[account.application.consumer.pk]))
        elif action in ("activate", "close"):
            account = get_object_or_404(Account, pk=pk)
            if action == "activate":
                account.closed = False
                account.is_active = True
                account.date_activated = datetime.datetime.today()
            elif action == "close":
                account.closed = True
                account.is_active = False
                account.date_closed = datetime.datetime.today()
            account.save()
    if action in ("C", "U", "R"):
        if action in ("U", "R"):
            if checkDirector(request):
                return HttpResponseRedirect(reverse("account-list"))
        if action in ("C", "U"):
            if checkReceptionist(request):
                return HttpResponseRedirect(reverse("account-list"))
        data["accountForm"] = accountForm
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name,
                                  data,
                                  context_instance=RequestContext(request))
Exemplo n.º 6
0
def region(request, pk=None, action=None, template_name="meter/location_form.html"):
    data = {}
    if request.method == "GET":
        if action in ("C", "U", "R"):
            if pk:
                data['region'] = get_object_or_404(Region, pk=pk)
                data['zones'] = data['region'].zone_set.all()
                if action == "U":
                    regionForm = RegionForm(instance=get_object_or_404(Region, pk=pk))
            else:
                regionForm = RegionForm()
    if request.method == "POST":
        if action in ("C", "U", "D"):
            if action in ("C", "U"):
                if pk:
                    regionForm = RegionForm(request.POST, instance=get_object_or_404(Region, pk=pk))
                else:
                    regionForm = RegionForm(request.POST)
                if regionForm.is_valid():
                    region= regionForm.save(commit=False)
                    region.save()
                    return HttpResponseRedirect(reverse("region-details", args=[region.pk]))
            else:
                if checkReceptionist(request):
                    return HttpResponseRedirect(reverse("subzone-list"))
                region = get_object_or_404(Region, pk=pk)
                region.delete()
    if action in ("C", "U"):
        if checkReceptionist(request):
            return HttpResponseRedirect(reverse("subzone-list"))
        if checkDirector(request):
            return HttpResponseRedirect(reverse("region-list"))
        data['regionForm'] = regionForm
        data['locationForm'] = regionForm
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name, data, context_instance=RequestContext(request))
Exemplo n.º 7
0
def consumertype(request,
                 pk=None,
                 action=None,
                 template_name="consumer/consumer_type_form.html"):
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        data = {}
        if request.method == "GET":
            if action in ("create", "read", "update"):
                if pk:
                    data['consumertype'] = get_object_or_404(ConsumerType,
                                                             pk=pk)
                    rates = data['consumertype'].consumption_set.all()
                    if rates:
                        data['last_rate'] = rates.reverse()[0]
                    data['rates'] = previous_and_current(list(rates))
                    if action == "update":
                        consumertypeForm = ConsumerTypeForm(
                            instance=get_object_or_404(ConsumerType, pk=pk))
                else:
                    consumertypeForm = ConsumerTypeForm()
            else:
                return HttpResponseForbidden()
        elif request.method == "POST":
            if action in ("create", "update"):
                if pk:
                    consumertypeForm = ConsumerTypeForm(
                        request.POST,
                        instance=get_object_or_404(ConsumerType, pk=pk))
                else:
                    consumertypeForm = ConsumerTypeForm(request.POST)
                if consumertypeForm.is_valid():
                    consumertype = consumertypeForm.save(commit=False)
                    consumertype.save()
                    return HttpResponseRedirect(
                        reverse("consumer-type-details",
                                args=[consumertype.pk]))
            else:
                consumertype = get_object_or_404(ConsumerType, pk=pk)
                consumertype.delete()
        if action in ("create", "update"):
            if checkReceptionist(request):
                return HttpResponseRedirect(reverse("consumer-type-list"))
            data['consumertypeForm'] = consumertypeForm
        return render_to_response(template_name,
                                  data,
                                  context_instance=RequestContext(request))
Exemplo n.º 8
0
def plot(request, pk=None, landlord_id=None, action=None, template_name="consumer/plot_form.html", redirect_field_name = None):
    data = {}
    if request.method == "GET":
        if action in ("create", "read", "update"):
            if pk:
                data['plot'] = get_object_or_404(Plot, pk=pk)
                data['accounts'] = account_set(data['plot'].application_set.filter(reviewed=True, approved=True))
                data['landlord'] = get_object_or_404(Landlord, pk=data['plot'].landlord.id)
                if action == "update":
                    plotForm = PlotForm(instance=get_object_or_404(Plot, pk=pk))
            else:
                plotForm = PlotForm()
                if landlord_id:
                    data['landlord'] = get_object_or_404(Landlord, pk=landlord_id)
                else:
                    landlordForm = LandlordForm()
    elif request.method == "POST":
        if action in ("create", "read", "update"):
            if pk:
                plotForm = PlotForm(request.POST, instance=get_object_or_404(Plot, pk=pk))
            else:
                plotForm = PlotForm(request.POST)
            if not landlord_id:
                landlordForm = LandlordForm(request.POST)
            if plotForm.is_valid():
                plot = plotForm.save(commit=False)
                if not landlord_id:
                    if landlordForm.is_valid():
                        landlord = landlordForm.save()
                        plot.landlord = landlord
                    else:
                        landlordForm = LandlordForm()
                else:
                    plot.landlord = get_object_or_404(Landlord, pk=landlord_id)
                plot.save()
                return HttpResponseRedirect(plot.landlord.get_absolute_url())
    if action in ("create", "update"):
        if checkReceptionist(request):
            return HttpResponseRedirect(reverse("consumer-type-list"))
        if checkDirector(request):
            return HttpResponseRedirect(reverse("plot-list"))
        data['plotForm']=plotForm
        if not landlord_id and action=="create":
            data['landlordForm'] = landlordForm
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name, data, context_instance=RequestContext(request))
Exemplo n.º 9
0
def account(request, pk=None, action=None, template_name="consumer/consumer_account.html"):
    data = {}
    if request.method == "GET":
        if pk:
            data['account'] = get_object_or_404(Account, pk=pk)
            accountForm = AccountForm(instance=get_object_or_404(Account, pk=pk))
    if request.method == "POST":
        if action in ("C","U", "R"):
            if action in ("U", "R"):
                if checkDirector(request):
                    return HttpResponseRedirect(reverse("account-list"))
            if pk:
                accountForm = AccountForm(request.POST, instance=get_object_or_404(Account, pk=pk))
            else:
                accountForm = AccountForm(request.POST)
            if accountForm.is_valid():
                account = accountForm.save(commit=False)
                account.date_activated = datetime.datetime.today()
                account.save()
                return HttpResponseRedirect(reverse("consumer-details", args=[account.application.consumer.pk]))
        elif action in ("activate", "close"):
            account = get_object_or_404(Account, pk=pk)
            if action == "activate":
                account.closed = False
                account.is_active = True
                account.date_activated = datetime.datetime.today()
            elif action=="close":
                account.closed = True
                account.is_active = False
                account.date_closed = datetime.datetime.today()
            account.save()
    if action in ("C","U", "R"):
        if action in ("U", "R"):
            if checkDirector(request):
                return HttpResponseRedirect(reverse("account-list"))
        if action in ("C", "U"):
            if checkReceptionist(request):
                return HttpResponseRedirect(reverse("account-list"))
        data["accountForm"] = accountForm
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name, data, context_instance=RequestContext(request))
Exemplo n.º 10
0
def meter_readings(request, pk=None, action=None, template_name="meter/upload_readings.html"):
    data = {}
    if request.method == "GET":
        if action == 'C':
            form = MeterReadingsForm()
    if request.method == 'POST':
        form = MeterReadingsForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['meter_readings_file'])
            data = upload_readings(request.user.pk)
            form = MeterReadingsForm()
            #return HttpResponseRedirect(reverse("uploaded-readings"))
        else:
            form = MeterReadingsForm()
    if action == 'C':
        if checkReceptionist(request):
            return HttpResponseRedirect(reverse("subzone-list"))
        if checkDirector(request):
            return HttpResponseRedirect(reverse("home"))
        data['form'] = form
    return render_to_response(template_name, data, context_instance=RequestContext(request))
Exemplo n.º 11
0
def consumertype(request, pk=None, action=None, template_name="consumer/consumer_type_form.html"):
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        data = {}
        if request.method == "GET":
            if action in ("create", "read", "update"):
                if pk:
                    data['consumertype'] = get_object_or_404(ConsumerType, pk=pk)
                    rates = data['consumertype'].consumption_set.all()
                    if rates:
                        data['last_rate'] = rates.reverse()[0]
                    data['rates'] = previous_and_current(list(rates))
                    if action == "update":
                        consumertypeForm = ConsumerTypeForm(instance=get_object_or_404(ConsumerType, pk=pk))
                else:
                    consumertypeForm = ConsumerTypeForm()
            else:
                return HttpResponseForbidden()
        elif request.method == "POST":
            if action in ("create", "update"):
                if pk:
                    consumertypeForm = ConsumerTypeForm(request.POST, instance=get_object_or_404(ConsumerType, pk=pk))
                else:
                    consumertypeForm = ConsumerTypeForm(request.POST)
                if consumertypeForm.is_valid():
                    consumertype = consumertypeForm.save(commit=False)
                    consumertype.save()
                    return HttpResponseRedirect(reverse("consumer-type-details", args=[consumertype.pk]))
            else:
                consumertype = get_object_or_404(ConsumerType, pk=pk)
                consumertype.delete()
        if action in ("create", "update"):
            if checkReceptionist(request):
                return HttpResponseRedirect(reverse("consumer-type-list"))
            data['consumertypeForm'] = consumertypeForm
        return render_to_response(template_name, data, context_instance=RequestContext(request))
Exemplo n.º 12
0
def plot(request,
         pk=None,
         landlord_id=None,
         action=None,
         template_name="consumer/plot_form.html",
         redirect_field_name=None):
    data = {}
    if request.method == "GET":
        if action in ("create", "read", "update"):
            if pk:
                data['plot'] = get_object_or_404(Plot, pk=pk)
                data['accounts'] = account_set(
                    data['plot'].application_set.filter(reviewed=True,
                                                        approved=True))
                data['landlord'] = get_object_or_404(
                    Landlord, pk=data['plot'].landlord.id)
                if action == "update":
                    plotForm = PlotForm(
                        instance=get_object_or_404(Plot, pk=pk))
            else:
                plotForm = PlotForm()
                if landlord_id:
                    data['landlord'] = get_object_or_404(Landlord,
                                                         pk=landlord_id)
                else:
                    landlordForm = LandlordForm()
    elif request.method == "POST":
        if action in ("create", "read", "update"):
            if pk:
                plotForm = PlotForm(request.POST,
                                    instance=get_object_or_404(Plot, pk=pk))
            else:
                plotForm = PlotForm(request.POST)
            if not landlord_id:
                landlordForm = LandlordForm(request.POST)
            if plotForm.is_valid():
                plot = plotForm.save(commit=False)
                if not landlord_id:
                    if landlordForm.is_valid():
                        landlord = landlordForm.save()
                        plot.landlord = landlord
                    else:
                        landlordForm = LandlordForm()
                else:
                    plot.landlord = get_object_or_404(Landlord, pk=landlord_id)
                plot.save()
                return HttpResponseRedirect(plot.landlord.get_absolute_url())
    if action in ("create", "update"):
        if checkReceptionist(request):
            return HttpResponseRedirect(reverse("consumer-type-list"))
        if checkDirector(request):
            return HttpResponseRedirect(reverse("plot-list"))
        data['plotForm'] = plotForm
        if not landlord_id and action == "create":
            data['landlordForm'] = landlordForm
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name,
                                  data,
                                  context_instance=RequestContext(request))
Exemplo n.º 13
0
def consumption(request,
                pk=None,
                action=None,
                consumer_type=None,
                boundary=None,
                template_name="consumer/consumption_form.html"):
    data = {}
    ConsumptionForm = BoundaryRateForm if boundary != None else NormConsumptionForm
    if request.method == "GET":
        if action in ("create", "read", "update"):
            if pk:
                data['consumption'] = get_object_or_404(Consumption, pk=pk)
                data['consumption_rates'] = previous_and_current(
                    Consumption.objects.filter(
                        consumer_type=data['consumption'].consumer_type))
                if action == "update":
                    consumptionForm = ConsumptionForm(
                        instance=get_object_or_404(Consumption, pk=pk))
            elif consumer_type:
                data['consumertype'] = get_object_or_404(ConsumerType,
                                                         pk=consumer_type)
                rates = data['consumertype'].consumption_set.all()
                data['consumption_rates'] = previous_and_current(list(rates))
                consumptionForm = ConsumptionForm()
        else:
            return HttpResponseForbidden()
    elif request.method == "POST":
        if action in ("create", "update"):
            if pk:
                consumptionForm = ConsumptionForm(request.POST,
                                                  instance=get_object_or_404(
                                                      Consumption, pk=pk))
            else:
                consumptionForm = ConsumptionForm(request.POST)
            if consumptionForm.is_valid():
                if action == "create":
                    rates = Consumption.objects.filter(
                        consumer_type=ConsumerType.objects.filter(
                            pk=consumer_type)).reverse()
                    if rates:
                        rate = rates[0]
                        consumption = consumptionForm.save(commit=False)
                        if rate.border_rate:
                            if rate.max_unit < consumption.max_unit:
                                rate.max_unit = consumption.max_unit + 1
                                rate.save()
                        if boundary:
                            consumption.max_unit = rate.max_unit + 1
                            consumption.border_rate = True
                        if consumer_type:
                            consumption.consumer_type = get_object_or_404(
                                ConsumerType, pk=consumer_type)
                        consumption.save()
                    else:
                        consumption = consumptionForm.save(commit=False)
                        if consumer_type:
                            consumption.consumer_type = get_object_or_404(
                                ConsumerType, pk=consumer_type)
                        consumption.save()
                elif action == "update":
                    consumption = consumptionForm.save(commit=False)
                    consumption.save()
                return HttpResponseRedirect(
                    reverse("consumer-type-details",
                            args=[consumption.consumer_type.pk]))
        else:
            consumption = get_object_or_404(Consumption, pk=pk)
            consumption.delete()
    if action in ("create", "update"):
        if checkReceptionist(request):
            return HttpResponseRedirect(reverse("consumer-type-list"))
        data['consumptionForm'] = consumptionForm

    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name,
                                  data,
                                  context_instance=RequestContext(request))
Exemplo n.º 14
0
def index(request):
    if checkReceptionist(request):
        return HttpResponseRedirect(reverse("consumer-list"))
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    return HttpResponseRedirect(reverse('home'))
Exemplo n.º 15
0
def consumer(request, pk=None, action=None, cons_type=None, template_name="consumer/consumer_details.html"):
    data = {}
    if cons_type:
        type_id = get_object_or_404(ConsumerType, pk=cons_type)
        ConsumerForm = DomesticConsumerForm if type_id.consumer_type.lower() == 'domestic' else CorporateConsumerForm
    else:
        customer = get_object_or_404(Consumer, pk=pk)
        ConsumerForm = DomesticConsumerForm if customer.consumer_type.consumer_type.lower() == 'domestic' else CorporateConsumerForm
    all_accounts = []
    
    if request.method == "GET":
        if action in ("C", "R", "U"):
            if pk:
                consumer = get_object_or_404(Consumer, pk=pk)
                data['consumer'] = consumer
                data['applications'] = consumer.application_set.all()
                data['unreviewed_applications'] = consumer.application_set.filter(reviewed=False)
                data['active_accounts'] = account_set(data['applications'])
                all_accounts.extend(data['active_accounts'])
                data['inactive_accounts'] = account_set(data['applications'], active=False)
                all_accounts.extend(data['inactive_accounts'])
                data['suspended_accounts'] = account_set(data['applications'], active=False, closed=True)
                all_accounts.extend(data['suspended_accounts'])
                
                #Consumer Financial data
                data['invoices'] =[]
                data['payments'] =[]
                invoice_total = 0
                payment_total = 0
                for account in all_accounts:
                    data['invoices'].extend(list(account.invoice_set.all()))
                for invoice in data['invoices']:
                    invoice_total += invoice.total
                for account in all_accounts:
                    data['payments'].extend(list(account.payment_set.all()))
                for payment in data['payments']:
                    payment_total += payment.amount_paid
                data['invoice_total'] = invoice_total
                data['payment_total'] = payment_total
                data['balance'] = invoice_total-payment_total
                if action in "U":
                    consumerForm = ConsumerForm(instance=get_object_or_404(Consumer, pk=pk))
            else:
                consumerForm = ConsumerForm()
        else:
            return HttpResponseForbidden()
    elif request.method == "POST":
        if action in ("C","D","U"):
            if action in ("C","U"):
                if pk:
                    consumerForm = ConsumerForm(request.POST, instance=get_object_or_404(Consumer, pk=pk))
                else:
                    consumerForm = ConsumerForm(request.POST)
                if consumerForm.is_valid():
                    consumer = consumerForm.save(commit=False)
                    consumer.consumer_type = get_object_or_404(ConsumerType, pk=cons_type)
                    consumer.save()
                    return HttpResponseRedirect(reverse("application-add", args=[consumer.pk]) if action=="C" else consumer.get_absolute_url())
            else:
                if checkReceptionist(request):
                    return HttpResponseRedirect(reverse("consumer-details", args=[consumer.pk]))
                consumer = get_object_or_404(Consumer, pk=pk)
                consumer.delete()
        else:
            return HttpResponseForbidden()
    if action in ("C","U"):
        if checkDirector(request):
            return HttpResponseRedirect(reverse("consumer-list"))
        data["consumerForm"] = consumerForm
    
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name, data, context_instance=RequestContext(request))
Exemplo n.º 16
0
def index(request):
    if checkReceptionist(request):
        return HttpResponseRedirect(reverse("consumer-list"))
    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    return HttpResponseRedirect(reverse('home'))
Exemplo n.º 17
0
def consumer(request,
             pk=None,
             action=None,
             cons_type=None,
             template_name="consumer/consumer_details.html"):
    data = {}
    if cons_type:
        type_id = get_object_or_404(ConsumerType, pk=cons_type)
        ConsumerForm = DomesticConsumerForm if type_id.consumer_type.lower(
        ) == 'domestic' else CorporateConsumerForm
    else:
        customer = get_object_or_404(Consumer, pk=pk)
        ConsumerForm = DomesticConsumerForm if customer.consumer_type.consumer_type.lower(
        ) == 'domestic' else CorporateConsumerForm
    all_accounts = []

    if request.method == "GET":
        if action in ("C", "R", "U"):
            if pk:
                consumer = get_object_or_404(Consumer, pk=pk)
                data['consumer'] = consumer
                data['applications'] = consumer.application_set.all()
                data[
                    'unreviewed_applications'] = consumer.application_set.filter(
                        reviewed=False)
                data['active_accounts'] = account_set(data['applications'])
                all_accounts.extend(data['active_accounts'])
                data['inactive_accounts'] = account_set(data['applications'],
                                                        active=False)
                all_accounts.extend(data['inactive_accounts'])
                data['suspended_accounts'] = account_set(data['applications'],
                                                         active=False,
                                                         closed=True)
                all_accounts.extend(data['suspended_accounts'])

                #Consumer Financial data
                data['invoices'] = []
                data['payments'] = []
                invoice_total = 0
                payment_total = 0
                for account in all_accounts:
                    data['invoices'].extend(list(account.invoice_set.all()))
                for invoice in data['invoices']:
                    invoice_total += invoice.total
                for account in all_accounts:
                    data['payments'].extend(list(account.payment_set.all()))
                for payment in data['payments']:
                    payment_total += payment.amount_paid
                data['invoice_total'] = invoice_total
                data['payment_total'] = payment_total
                data['balance'] = invoice_total - payment_total
                if action in "U":
                    consumerForm = ConsumerForm(
                        instance=get_object_or_404(Consumer, pk=pk))
            else:
                consumerForm = ConsumerForm()
        else:
            return HttpResponseForbidden()
    elif request.method == "POST":
        if action in ("C", "D", "U"):
            if action in ("C", "U"):
                if pk:
                    consumerForm = ConsumerForm(request.POST,
                                                instance=get_object_or_404(
                                                    Consumer, pk=pk))
                else:
                    consumerForm = ConsumerForm(request.POST)
                if consumerForm.is_valid():
                    consumer = consumerForm.save(commit=False)
                    consumer.consumer_type = get_object_or_404(ConsumerType,
                                                               pk=cons_type)
                    consumer.save()
                    return HttpResponseRedirect(
                        reverse("application-add", args=[consumer.pk])
                        if action == "C" else consumer.get_absolute_url())
            else:
                if checkReceptionist(request):
                    return HttpResponseRedirect(
                        reverse("consumer-details", args=[consumer.pk]))
                consumer = get_object_or_404(Consumer, pk=pk)
                consumer.delete()
        else:
            return HttpResponseForbidden()
    if action in ("C", "U"):
        if checkDirector(request):
            return HttpResponseRedirect(reverse("consumer-list"))
        data["consumerForm"] = consumerForm

    if checkCredentials(request):
        return HttpResponseRedirect(reverse('meter-readings-add'))
    else:
        return render_to_response(template_name,
                                  data,
                                  context_instance=RequestContext(request))