def get(self, request, *args, **kwargs): template = get_template('sales_details_pdf.html') sales_details = get_object_or_404(Sales, pk=self.kwargs['pk2']) stocklist = Stock_Total_sales.objects.filter(sales=sales_details.pk) context = { "company_details": get_object_or_404(company, pk=self.kwargs['pk1']), "selectdatefield_details": get_object_or_404(selectdatefield, pk=self.kwargs['pk3']), "sales_details": sales_details, "stocklist": stocklist, } html = template.render(context) pdf = render_to_pdf('sales_details_pdf.html', context) if pdf: return HttpResponse(pdf, content_type='application/pdf') return HttpResponse("PDF Not Found")
def get(self, request, *args, **kwargs): id = request.session['contract_id'] customer_plan_data = CustomerServiceContract.objects.values( 'id', 'user_id', 'customerwithservice', 'service_plan_hardware', 'type', 'payment_status', 'user_id__account_id', 'user_id__first_name', 'user_id__last_name', 'user_id__email_address', 'user_id__phone', 'created_at').filter(pk=id) address = AccountAddressCustomer.objects.values( 'id', 'user_id', 'address_1', 'address_2', 'city__city_name', 'province__province_name', 'country__country_name').filter( user_id=customer_plan_data[0]['user_id']) n = random.randint(1000000, 9999999) total = 0 arr = customer_plan_data[0]['customerwithservice'] arr = arr.replace('"[', '') arr = arr.replace(']"', '') arr = arr.split(',') for ar in arr: service_plan = CustomerWithService.objects.values( 'id', 'service_plan_id__title', 'service_price_actual', 'service_price_retail', 'service_price_qty', 'plan_status').filter(service_plan_id=ar) ml = service_plan[0]['service_price_actual'] * service_plan[0][ 'service_price_qty'] total = total + ml # =========================================================================================================================# totalhw = 0 arrhwdata = [] for ar in arr: hardware = ServicePlanWithHardware.objects.values( 'id', 'hw_id', 'hw_qty', 'hw_status', 'hw_id__hw_title').filter(service_plan_id=ar) for hj in hardware: arrhwdata.append(hj['hw_id']) print(arrhwdata) array = [] for h in arrhwdata: vale = Hardware.objects.values('device_buy').filter(pk=h) array.append(vale[0]['device_buy']) for hw in array: totalhw = totalhw + float(hw) pdf = render_to_pdf( 'admin/customer/contract/mail/contract_attch.html', { 'id': id, 'customer_plan_data': customer_plan_data, 'hwDtata': json.loads(customer_plan_data[0]['service_plan_hardware']), 'contractData': arr, 'customerwithservicetotal': total, 'hw_total': totalhw, 'random_no': n, 'address': address }) return HttpResponse(pdf, content_type='application/pdf')
def get(self, request, *args, **kwargs): template = get_template('ledger_monthly_pdf.html') company_details = get_object_or_404(company, pk=self.kwargs['pk']) ledger1_details = get_object_or_404(ledger1, pk=self.kwargs['pk2']) selectdatefield_details = get_object_or_404(selectdatefield, pk=self.kwargs['pk3']) # opening balance qsob = journal.objects.filter( User=self.request.user, Company=company_details.pk, By=ledger1_details.pk, Date__gte=ledger1_details.Creation_Date, Date__lte=selectdatefield_details.Start_Date) qsob2 = journal.objects.filter( User=self.request.user, Company=company_details.pk, To=ledger1_details.pk, Date__gte=ledger1_details.Creation_Date, Date__lte=selectdatefield_details.Start_Date) total_debitob = qsob.aggregate( the_sum=Coalesce(Sum('Debit'), Value(0)))['the_sum'] total_creditob = qsob2.aggregate( the_sum=Coalesce(Sum('Credit'), Value(0)))['the_sum'] if (ledger1_details.Creation_Date != selectdatefield_details.Start_Date): if (ledger1_details.group1_Name.balance_nature == 'Debit'): opening_balance = abs(ledger1_details.Opening_Balance) + abs( total_debitob) - abs(total_creditob) else: opening_balance = abs(ledger1_details.Opening_Balance) + abs( total_creditob) - abs(total_debitob) else: opening_balance = abs(ledger1_details.Opening_Balance) results = collections.OrderedDict() qscb = journal.objects.filter( User=self.request.user, Company=company_details.pk, By=ledger1_details.pk, Date__gte=selectdatefield_details.Start_Date, Date__lte=selectdatefield_details.End_Date).annotate( real_total_debit=Case(When(Debit__isnull=True, then=0), default=F('Debit'))) qscb2 = journal.objects.filter( User=self.request.user, Company=company_details.pk, To=ledger1_details.pk, Date__gte=selectdatefield_details.Start_Date, Date__lte=selectdatefield_details.End_Date).annotate( real_total_credit=Case(When(Debit__isnull=True, then=0), default=F('Debit'))) date_cursor = selectdatefield_details.Start_Date z = 0 k = 0 while date_cursor < selectdatefield_details.End_Date: month_partial_total_debit = qscb.filter( Date__month=date_cursor.month).aggregate( partial_total_debit=Sum( 'real_total_debit'))['partial_total_debit'] month_partial_total_credit = qscb2.filter( Date__month=date_cursor.month).aggregate( partial_total_credit=Sum( 'real_total_credit'))['partial_total_credit'] if month_partial_total_debit == None: month_partial_total_debit = int(0) e = month_partial_total_debit else: e = month_partial_total_debit if month_partial_total_credit == None: month_partial_total_credit = int(0) f = month_partial_total_credit else: f = month_partial_total_credit if (ledger1_details.group1_Name.balance_nature == 'Debit'): z = z + e - f else: z = z + f - e k = z + opening_balance results[calendar.month_name[date_cursor.month]] = [e, f, k] date_cursor += dateutil.relativedelta.relativedelta(months=1) total_debit = qscb.aggregate( the_sum=Coalesce(Sum('Debit'), Value(0)))['the_sum'] total_credit = qscb2.aggregate( the_sum=Coalesce(Sum('Credit'), Value(0)))['the_sum'] if (ledger1_details.group1_Name.balance_nature == 'Debit'): total1 = total_debit - total_credit else: total1 = total_credit - total_debit total = total1 + opening_balance context = { 'company_details': company_details, 'ledger1_details': ledger1_details, 'selectdatefield_details': selectdatefield_details, 'total_debit': total_debit, 'total_credit': total_credit, 'total': total, 'data': results.items(), 'opening_balance': opening_balance, } html = template.render(context) pdf = render_to_pdf('ledger_monthly_pdf.html', context) if pdf: return HttpResponse(pdf, content_type='application/pdf') return HttpResponse("PDF Not Found")