Ejemplo n.º 1
0
def index(request):
    if request.user.has_perms(["team.custom_view_teams", "budget.custom_view_budget"]):
        budget_lines = BudgetLine.objects.filter(is_active=True)
    elif request.user.has_perm("budget.custom_view_budget"):
        budget_lines = BudgetLine.objects.filter(is_active=True, team__in=[t.name for t in get_teams(request.user)])
    else:
        not_allowed_msg(request)
        return redirect("home")

    #
    # Filter budget_lines depending on received GET data
    form = BudgetLineFilterForm(user=request.user, data=request.GET)
    if len(request.GET.keys()) > 0 and form.is_valid():
        data = form.cleaned_data
        for key, value in data.items():
            if not value:
                del data[key]

        Q_obj = Q()
        Q_obj.connector = data.pop("connector")
        Q_obj.children = data.items()

        budget_lines = budget_lines.filter(Q_obj)

    budgets = list(set(budget_lines.values_list("budget_id", flat=True)))
    if len(budgets) == 1:
        budget = Budget.objects.get(id=budgets[0], is_active=True)
    else:
        budget = Budget.objects.none()

    return render(
        request,
        "budgetlines/index.html",
        {"budget": budget, "budget_lines": budget_lines, "filter_form": form, "url_args": request.GET.urlencode()},
    )
Ejemplo n.º 2
0
def index(request):
    if request.user.has_perms(['team.custom_view_teams','budget.custom_view_budget']):
        budget_lines = BudgetLine.objects.filter(is_active = True)
    elif request.user.has_perm('budget.custom_view_budget'):
        budget_lines = BudgetLine.objects.filter(
            is_active = True,
            team__in = [t.name for t in get_teams(request.user)]
    )
    else:
        not_allowed_msg(request)
        return redirect('home')
    
    # 
    # Filter budget_lines depending on received GET data
    form = BudgetLineFilterForm(user = request.user, data = request.GET)
    if len(request.GET.keys()) > 0 and form.is_valid():
        Q_obj = Q()
        Q_obj.connector = form.cleaned_data.pop("connector")
        Q_obj.children  = []
        for key, value in form.cleaned_data.items():
            if value:
                Q_obj.children.append((key,value))
        
        budget_lines = budget_lines.filter(Q_obj)
    
    budgets = list(set(budget_lines.values_list('budget_id',flat=True)))
    if len(budgets) == 1:
        budget = Budget.objects.get(id = budgets[0], is_active = True)
    else:
        budget = Budget.objects.none()
    
    return render(request, "budgetlines/index.html", {
        'budget': budget,
        'budget_lines' : budget_lines,
        'filter_form': form,
        'url_args': request.GET.urlencode()
    })
Ejemplo n.º 3
0
def export_to_xls(request):
    # 
    # Filter budget_lines depending on received GET data
    form = BudgetLineFilterForm(user = request.user, data = request.GET)
    if len(request.GET.keys()) > 0 and form.is_valid():
        Q_obj = Q()
        Q_obj.connector = form.cleaned_data.pop("connector")
        Q_obj.children  = []
        for key, value in form.cleaned_data.items():
            if value:
                Q_obj.children.append((key, value))

        budget_lines = BudgetLine.objects.filter(Q_obj)
    else:
        budget_lines = BudgetLine.objects.none()
    
    wb = xlwt.Workbook()
    ws = wb.add_sheet("export")
    
    header = [u"EQUIPE", u"BUDGET", u"N°CMDE",u"DATE", u"NATURE", 
    u"TUTELLE", u"FOURNISSEUR", u"COMMENTAIRE", u"DESIGNATION", 
    u"CREDIT", u"DEBIT", u"QUANTITE", u"TOTAL"]
    
    for col, title in enumerate(header): 
        ws.write(0, col, title)
    
    row = 1    
    total = 0
    for bl in budget_lines.filter(number__isnull = False).exclude(number = ''):
        total += bl.get_total()
        
        ws.write(row, 0, bl.team)
        ws.write(row, 1, bl.budget)
        ws.write(row, 2, bl.number)
        ws.write(row, 3, bl.date.strftime("%d/%m/%Y"))
        ws.write(row, 4, bl.nature)
        ws.write(row, 5, bl.get_budget_type_display())
        ws.write(row, 6, bl.provider)
        ws.write(row, 7, bl.offer)
        ws.write(row, 8, bl.product)
        ws.write(row, 9, bl.credit)
        ws.write(row, 10, bl.debit)
        ws.write(row, 11, bl.quantity)
        if bl.debit:
            ws.write(row, 12, "%s" % (bl.debit * bl.quantity * -1))
        else:
            ws.write(row, 12, "%s" % (bl.credit * bl.quantity))
        row += 1
    
    if row != 1:
        ws.write(row, 12, total)
        row += 2
    
    total = 0
    for bl in budget_lines.filter(Q(number__isnull = True) | Q(number = '')):
        total += bl.get_total()
        
        ws.write(row, 0, bl.team)
        ws.write(row, 1, bl.budget)
        ws.write(row, 2, bl.number)
        ws.write(row, 3, bl.date.strftime("%d/%m/%Y"))
        ws.write(row, 4, bl.nature)
        ws.write(row, 5, bl.get_budget_type_display())
        ws.write(row, 6, bl.provider)
        ws.write(row, 7, bl.offer)
        ws.write(row, 8, bl.product)
        ws.write(row, 9, bl.credit)
        ws.write(row, 10, bl.debit)
        ws.write(row, 11, bl.quantity)
        if bl.debit:
            ws.write(row, 12, "%s" % (bl.debit * bl.quantity * -1))
        else:
            ws.write(row, 12, "%s" % (bl.credit * bl.quantity))
        row += 1
    ws.write(row, 12, total)
    
    budget_ids = budget_lines.values_list('budget_id', flat = True).distinct()
    if budget_ids.count() == 1:
        budget = Budget.objects.get(id = budget_ids[0])
        ws.write(row + 1, 0, "MONTANT DISPONIBLE:")
        ws.write(row + 1, 1, budget.get_amount_left())
    
    response = HttpResponse()
    response['Content-Disposition'] = 'attachment; filename=export_budget.xls'
    response['Content-Type'] = 'application/vnd.ms-excel; charset=utf-8'
    wb.save(response)
    
    return response