Ejemplo n.º 1
0
def update_airpurchases(request, id):
    if 'POST' == request.method:
        if ('-1' == id):
            form = PurchaseForm(request.POST)
        else:
            form = PurchaseForm(request.POST, instance=Purchase.objects.get(id=id))

        if form.is_valid():
            model = form.save();
            return HttpResponse(model.to_json(), content_type='application/json', status=200)

        else:
            return render(request, 'themetours/update_airpurchase.html', {'form' : form, 'id' : id}, status=302)
    else:
        if '-1' == id:
            form = PurchaseForm()
        else:
            form = PurchaseForm(instance=Purchase.objects.get(id=id))

    return render(request, 'themetours/update_airpurchase.html', {'form' : form, 'id' : id})
Ejemplo n.º 2
0
def update_airsale(request, id):
    PassengerFormSet = modelformset_factory(PassengerInfo,
                                            formset=PassengerInfoFormSet,
                                            can_delete=False,
                                            can_order=False,
                                            max_num=6,
                                            extra=0,
                                            form=PassengerInfoForm,
                                            exclude = ('date',
                                                       'is_deleted',
                                                       'sales_transaction_no',
                                                       'purchase_transaction_no')
                                            )
    if 'POST' == request.method:
        sale_instance = None
        if '-1' == id:
            salesForm = SaleForm(request.POST, prefix='s')
            passenger_info_formset = PassengerFormSet(request.POST)

        else:
            sale_instance = Sale.objects.get(id=id, is_deleted=False)
            salesForm = SaleForm(request.POST, instance=copy.deepcopy(sale_instance), prefix='s')
            passenger_info_formset = PassengerFormSet(request.POST)

        airpassengerInfo_ids = []
        purchase_model_ids = []

        if salesForm.is_valid() and passenger_info_formset.is_valid():
            salesModel = salesForm.save(commit=False)
            salesModel.sales_transaction_no = salesModel.service_type.name+ '/'+ str(salesModel.id)

            count = 0;
            for form in passenger_info_formset.forms:
                count = count + 1;
                try:
                    passenger_info = form.save(commit=False)
                except e:
                    raise form.ValidationError(e.__str__)

                try:
                    purchaseModel = Purchase.objects.get(supplier=passenger_info.supplier, sales=salesModel, is_deleted=False)
                except:
                    purchaseModel = Purchase(supplier=passenger_info.supplier, sales=salesModel)
                    purchaseModel.purchase_transaction_no = 'P/' +salesModel.service_type.name+ '/'+ str(purchaseModel.id)
                    purchaseModel.service_tax = 0.0
                    purchaseModel.education_cess = 0.0
                    purchaseModel.higher_secondary = 0.0
                    purchaseModel.tds = 0
                    purchaseModel.discount = 0
                    purchaseModel.round_off = 0
                    purchaseModel.purchase_actual = passenger_info.basic_fare + passenger_info.airline_taxes;
                    purchaseModel.total = 0.0

                if purchaseModel and sale_instance and sale_instance.total != salesModel.total:
                    purchaseModel.supplier = passenger_info.supplier
                    purchaseModel.service_tax = 0.0
                    purchaseModel.education_cess = 0.0
                    purchaseModel.higher_secondary = 0.0
                    purchaseModel.tds = 0
                    purchaseModel.discount = 0
                    purchaseModel.round_off = 0
                    purchaseModel.total = 0.0

                if 1 == count:
                    purchaseModel.purchase_actual = 0;
                    purchaseModel.purchase_actual = passenger_info.basic_fare + passenger_info.airline_taxes;

                else:
                    purchaseModel.purchase_actual = purchaseModel.purchase_actual + passenger_info.basic_fare + passenger_info.airline_taxes;

                purchaseForm = PurchaseForm(instance=purchaseModel)
                pmodel = purchaseForm.save(commit=False)

                try:
                    passenger_info.purchase_transaction_no = pmodel
                    passenger_info.sales_transaction_no = salesModel
                    form.save_m2m()

                    salesModel.save()
                    salesModel.sales_transaction_no = salesModel.service_type.code+'/'+str(salesModel.id)
                    salesModel.save()

                    purchaseModel.sales = salesModel
                    purchaseModel.save()

                    purchaseModel.purchase_transaction_no = 'P-'+purchaseModel.sales.service_type.code+'/'+str(purchaseModel.id)
                    purchaseModel.save()
                    purchase_model_ids.append(purchaseModel.id);

                    passenger_info.purchase_transaction_no = purchaseModel
                    passenger_info.sales_transaction_no = salesModel
                    passenger_info.save()

                    airpassengerInfo_ids.append(passenger_info.id)

                except ValueError as e:
                    raise form.ValidationError(e.__str__)

            deletedModels = PassengerInfo.objects.filter(sales_transaction_no=salesModel).exclude(id__in=airpassengerInfo_ids)
            for dPmodels in deletedModels:
                dPmodels.is_deleted = True;
                dPmodels.save()

            deletedPurchaseModels = Purchase.objects.filter(sales=salesModel).exclude(id__in=purchase_model_ids)
            for deletedPurchaseModel in deletedPurchaseModels:
                deletedPurchaseModel.is_deleted = True;
                deletedPurchaseModel.save()

            return HttpResponse(salesModel.to_json(), content_type='application/json', status=200)

        else:
            return render(request, 'themetours/update_airsales.html', {'formset' : passenger_info_formset,
                                                                       'salesForm' : salesForm,
                                                                      'id' : id},
                          status=302)
    else:
        if '-1' == id:
            service = Service.objects.all()[0]
            salesForm = SaleForm(prefix='s', initial={'service_tax_per':service.service_tax, 'education_cess_per': service.education_cess, 'higher_secondary_per':service.higher_secondary})
            PassengerFormSet = modelformset_factory(PassengerInfo,
                                                    formset=PassengerInfoFormSet,
                                                    can_delete=False,
                                                    can_order=False,
                                                    max_num=6,
                                                    extra=1,
                                                    form=PassengerInfoForm,
                                                    exclude = ('date',
                                                               'sales_transaction_no',
                                                               'purchase_transaction_no',
                                                               'is_deleted',
                                                               'sales_transaction_no',
                                                               'purchase_transaction_no')
                                                    )
            passenger_info_formset = PassengerFormSet(queryset=PassengerInfo.objects.none())

        else:
            saleModel = Sale.objects.get(id=id)
            service = Service.objects.get(id=saleModel.service_type.id)

            salesForm = SaleForm(instance=saleModel,
                                 prefix='s',
                                 initial={'service_tax_per':service.service_tax,
                                          'education_cess_per': service.education_cess,
                                          'higher_secondary_per': service.higher_secondary})
            passenger_info_formset = PassengerFormSet(queryset=PassengerInfo.objects.filter(sales_transaction_no=id, is_deleted=False))

    return render(request, 'themetours/update_airsales.html', {'salesForm' : salesForm,
                                                               'formset': passenger_info_formset,
                                                               'id' : id})