Пример #1
0
def add(request):
    payroll_number = request.POST.get('payroll_number', '')

    if request.method == 'POST':
        form = PaylineForm(request.POST)
        if form.is_valid():
            payline = form.save()
            payline.last_altered_by = request.user.get('username')
            payline.location = payline.contractor.location
            if payline.contractor.pay_gst and payline.gst_value is None:
                payline.gst_value = float(payline.value)/10

            payline.save()

            messages.success(request, 'Payline added successfully.')
            return HttpResponseRedirect(reverse('direct.pays.views.pays.add', args=[request.info.get('current_projectname'),]))
        else:
            messages.error(request, 'Something went wrong.')
    else:
        form = PaylineForm()

    unlocked_cycles = Cycle.objects.values('id', 'cycle_date').order_by('-id').filter(locked=False, active=True)
    locations = Location.objects.values('id', 'location')
    finance_codes = FinanceCode.objects.extra(select={'display': "vcfinance_code+' - '+vcdescription", 'order': "cast(vcfinance_code as int)"}).values('id','display', 'order').extra(order_by=['order'])
    contractors = Contractor.objects.extra(select={'display': "upper(vcIDENTIFIER+' - '+vcNAME)"}).values('id', 'display').filter(referer=False).extra(order_by = ['display'])

    form.fields['cycle'].widget.choices = to_select(unlocked_cycles, 'id', 'cycle_date', None, 'date')
    form.fields['finance_code'].widget.choices = to_select(finance_codes, 'id', 'display', None)
    form.fields['contractor'].widget.choices = to_select(contractors, 'id', 'display', None)
    form.fields['location'].widget.choices = to_select(locations, 'id', 'location', None)

    return render(request, 'pays/pays/add.html', {'form': form, 'payroll_number': payroll_number})
Пример #2
0
def index(request):
    config = Config.objects.get(application='pays')
    failed = request.GET.get('failed', 'False')
    location = request.GET.get('l', '')
    if failed == 'True':
        messages.error(request, 'The last calculation failed. Please notify helpdesk to fix the calculation process.')

    cycles = Cycle.objects.values().order_by('-id').filter(active=True)
    locations = Location.objects.values().order_by('location')

    if not request.info.get('current_projectname') in request.user['perms'].get('direct.pays.admin', {}):
        cycles = cycles.filter(locked=True)

    form = CalculationForm(request.GET)
    form.fields['c'].widget.choices = to_select(cycles, 'id', 'cycle_date', None, 'date')
    form.fields['l'].widget.choices = to_select(locations, 'id', 'location')

    cycles = Cycle.objects.all().order_by('-id')
    if not request.info.get('current_projectname') in request.user['perms'].get('direct.pays.admin', {}):
        cycles = cycles.filter(locked=True)

    cycle_id = int(request.GET.get('c', cycles[0].id))
    current_cycle = Cycle.objects.get(pk=cycle_id)

    pay_runs = PayRun.objects.all().filter(cycle=cycle_id).order_by('-approved')
    try:
        pay_run_id = int(request.GET.get('pr', pay_runs[0].id))
    except:
        pay_run_id = 0

    try:
        pay_run = PayRun.objects.get(cycle=cycle_id, approved=True)
        if not pay_run.calculated:
            return HttpResponseRedirect(reverse('direct.pays.views.pays.calculating', args=[request.info.get('current_projectname'), cycle_id, pay_run.id]))
    except:
        pass

    pay_runs = pay_runs.order_by('id')

    if not pay_runs:
        pay_run = PayRun()
        pay_run.created_by = 'Noone'
        pay_run.created_date = datetime.datetime.now()
        pay_run.id = 0

        pay_runs = [pay_run]

    return render(request, 'pays/pays/index.html', {'current_cycle': current_cycle,
                                                    'pay_runs': pay_runs,
                                                    'pay_run_id': pay_run_id,
                                                    'form': form,
                                                    'config': config,
                                                    'location': location})
Пример #3
0
def add(request):
    payroll_number = request.POST.get('payroll_number', '')

    if request.method == 'POST':
        form = PaylineForm(request.POST)
        if form.is_valid():
            payline = form.save()
            payline.last_altered_by = request.user.get('username')
            payline.location = payline.contractor.location
            if payline.contractor.pay_gst and payline.gst_value is None:
                payline.gst_value = float(payline.value) / 10

            payline.save()

            messages.success(request, 'Payline added successfully.')
            return HttpResponseRedirect(
                reverse('direct.pays.views.pays.add',
                        args=[
                            request.info.get('current_projectname'),
                        ]))
        else:
            messages.error(request, 'Something went wrong.')
    else:
        form = PaylineForm()

    unlocked_cycles = Cycle.objects.values(
        'id', 'cycle_date').order_by('-id').filter(locked=False, active=True)
    locations = Location.objects.values('id', 'location')
    finance_codes = FinanceCode.objects.extra(
        select={
            'display': "vcfinance_code+' - '+vcdescription",
            'order': "cast(vcfinance_code as int)"
        }).values('id', 'display', 'order').extra(order_by=['order'])
    contractors = Contractor.objects.extra(
        select={
            'display': "upper(vcIDENTIFIER+' - '+vcNAME)"
        }).values('id',
                  'display').filter(referer=False).extra(order_by=['display'])

    form.fields['cycle'].widget.choices = to_select(unlocked_cycles, 'id',
                                                    'cycle_date', None, 'date')
    form.fields['finance_code'].widget.choices = to_select(
        finance_codes, 'id', 'display', None)
    form.fields['contractor'].widget.choices = to_select(
        contractors, 'id', 'display', None)
    form.fields['location'].widget.choices = to_select(locations, 'id',
                                                       'location', None)

    return render(request, 'pays/pays/add.html', {
        'form': form,
        'payroll_number': payroll_number
    })
Пример #4
0
def fetch_held_paylines(request):
    id = request.GET.get('id')

    if request.GET.get('contractors'):
        held_paylines = Payline.objects.values_list('contractor__id').distinct().all().filter(Q(pay_run__approved=True) | Q(pay_run=0), cycle__locked=True, active=True, dummy=False, perm_held=False, held=True, reference_payline=None).order_by('-contractor__payroll')

        contractors = [p[0] for p in held_paylines]

        return HttpResponse(json.dumps(contractors), mimetype="application/json")

    unlocked_cycles = Cycle.objects.values('id', 'cycle_date').order_by('-id').filter(locked=False, active=True)
    form = CalculationForm(request.GET)
    form.fields['c'].widget.choices = to_select(unlocked_cycles, 'id', 'cycle_date', '----------------', 'date')
    form.fields['c'].widget.attrs = {'class':'release'}

    held_paylines = Payline.objects.select_related().all().filter(Q(pay_run__approved=True) | Q(pay_run=0), contractor=id, cycle__locked=True, active=True, dummy=False, perm_held=False, held=True, reference_payline=None)

    held_paylines_dict = OrderedDict()

    for payline in held_paylines:
        contractor_id = payline.contractor.id
        finance_code_id = payline.finance_code.id
        cycle_id = payline.cycle.id
        if not contractor_id in held_paylines_dict:
            held_paylines_dict[contractor_id] = {}
            held_paylines_dict[contractor_id]['contractor'] = payline.contractor
            held_paylines_dict[contractor_id]['total'] = 0.0
            held_paylines_dict[contractor_id]['total_gst'] = 0.0
            held_paylines_dict[contractor_id]['cycles'] = OrderedDict()
        if not cycle_id in held_paylines_dict[contractor_id]['cycles']:
            held_paylines_dict[contractor_id]['cycles'][cycle_id] = {}
            held_paylines_dict[contractor_id]['cycles'][cycle_id]['cycle'] = payline.cycle
            held_paylines_dict[contractor_id]['cycles'][cycle_id]['total'] = 0.0
            held_paylines_dict[contractor_id]['cycles'][cycle_id]['total_gst'] = 0.0
            held_paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'] = OrderedDict()
        if not finance_code_id in held_paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes']:
            held_paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id] = {}
            held_paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id]['finance_code'] = payline.finance_code
            held_paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id]['total'] = 0.0
            held_paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id]['total_gst'] = 0.0
            held_paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id]['paylines'] = []

        held_paylines_dict[contractor_id]['total'] += payline.value
        held_paylines_dict[contractor_id]['total_gst'] += float(payline.gst_value or 0)

        held_paylines_dict[contractor_id]['cycles'][cycle_id]['total'] += payline.value
        held_paylines_dict[contractor_id]['cycles'][cycle_id]['total_gst'] += float(payline.gst_value or 0)

        held_paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id]['total'] += payline.value
        held_paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id]['total_gst'] += float(payline.gst_value or 0)

        held_paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id]['paylines'].append(payline)

    return render(request, 'pays/ajax/held_paylines.html',   {'held_paylines': held_paylines_dict,
                                                                'form': form})
Пример #5
0
def index(request):
    config = Config.objects.get(application='pays')
    failed = request.GET.get('failed', 'False')
    location = request.GET.get('l', '')
    if failed == 'True':
        messages.error(
            request,
            'The last calculation failed. Please notify helpdesk to fix the calculation process.'
        )

    cycles = Cycle.objects.values().order_by('-id').filter(active=True)
    locations = Location.objects.values().order_by('location')

    if not request.info.get(
            'current_projectname') in request.user['perms'].get(
                'direct.pays.admin', {}):
        cycles = cycles.filter(locked=True)

    form = CalculationForm(request.GET)
    form.fields['c'].widget.choices = to_select(cycles, 'id', 'cycle_date',
                                                None, 'date')
    form.fields['l'].widget.choices = to_select(locations, 'id', 'location')

    cycles = Cycle.objects.all().order_by('-id')
    if not request.info.get(
            'current_projectname') in request.user['perms'].get(
                'direct.pays.admin', {}):
        cycles = cycles.filter(locked=True)

    cycle_id = int(request.GET.get('c', cycles[0].id))
    current_cycle = Cycle.objects.get(pk=cycle_id)

    pay_runs = PayRun.objects.all().filter(
        cycle=cycle_id).order_by('-approved')
    try:
        pay_run_id = int(request.GET.get('pr', pay_runs[0].id))
    except:
        pay_run_id = 0

    try:
        pay_run = PayRun.objects.get(cycle=cycle_id, approved=True)
        if not pay_run.calculated:
            return HttpResponseRedirect(
                reverse('direct.pays.views.pays.calculating',
                        args=[
                            request.info.get('current_projectname'), cycle_id,
                            pay_run.id
                        ]))
    except:
        pass

    pay_runs = pay_runs.order_by('id')

    if not pay_runs:
        pay_run = PayRun()
        pay_run.created_by = 'Noone'
        pay_run.created_date = datetime.datetime.now()
        pay_run.id = 0

        pay_runs = [pay_run]

    return render(
        request, 'pays/pays/index.html', {
            'current_cycle': current_cycle,
            'pay_runs': pay_runs,
            'pay_run_id': pay_run_id,
            'form': form,
            'config': config,
            'location': location
        })
Пример #6
0
def search(request):
    config = Config.objects.get(application='pays')

    paylines = {}
    messages.info(
        request,
        'To prevent timeouts search only returns the first 500 matching paylines.'
    )

    form = SearchForm(request.GET)
    cycles = Cycle.objects.values().order_by('-id')
    locations = Location.objects.values('id', 'location')
    finance_codes = FinanceCode.objects.extra(
        select={
            'display': "vcfinance_code+' - '+vcdescription",
            'order': "cast(vcfinance_code as int)"
        }).values('id', 'display', 'order').extra(order_by=['order'])

    form.fields['cycle'].widget.choices = to_select(cycles, 'id', 'cycle_date',
                                                    '------------', 'date')
    form.fields['finance_code'].widget.choices = to_select(
        finance_codes, 'id', 'display')
    form.fields['location'].widget.choices = to_select(locations, 'id',
                                                       'location')

    query_string = request.META['QUERY_STRING']
    if any([bool(request.GET.get(x))
            for x in request.GET]) and all(k not in form.errors
                                           for k in ('pay_id', 'payroll')):
        paylines = Payline.objects.select_related().all().filter(
            Q(pay_run__approved=True) | Q(pay_run=0), active=True, dummy=False)

        if request.GET.get('cycle'):
            paylines = paylines.filter(cycle=request.GET.get('cycle'))

        if request.GET.get('location'):
            paylines = paylines.filter(location=request.GET.get('location'))

        if request.GET.get('finance_code'):
            paylines = paylines.filter(
                finance_code=request.GET.get('finance_code'))

        if request.GET.get('sales_code'):
            paylines = paylines.filter(
                contractor__sales_code__contains=request.GET.get('sales_code'))

        if request.GET.get('payroll'):
            paylines = paylines.filter(
                contractor__payroll__contains=request.GET.get('payroll'))

        if request.GET.get('application_id'):
            paylines = paylines.filter(
                application_id__contains=request.GET.get('application_id'))

        if request.GET.get('pay_id'):
            paylines = paylines.filter(
                external_record_id=request.GET.get('pay_id'))

        paylines = paylines[:500]

        if not paylines:
            messages.warning(request,
                             'No paylines were found matching your criteria')

    if request.GET.get('fmt') == 'excel':
        head_row = ("Finance Code", "Location", "Contractor Id", "Sales Code",
                    "Contractor", "Payroll", "Amount", "GST", "Details",
                    "Pays Id", "Application Id", "Cycle")

        response = HttpResponse(mimetype='text/csv')
        response[
            'Content-Disposition'] = 'attachment; filename=Search Extract.csv'

        xstr = lambda s: s or ""

        writer = csv.writer(response, quoting=csv.QUOTE_ALL)
        writer.writerow(head_row)
        for payline in paylines:
            row = [
                payline.finance_code.description, payline.location,
                payline.contractor.id, payline.contractor.sales_code,
                payline.contractor.name, payline.contractor.payroll,
                payline.value, payline.gst_value,
                xstr(payline.details).encode('utf8'),
                payline.external_record_id, payline.application_id,
                payline.cycle.cycle_date
            ]
            if payline.perm_held:
                row.append("Perm Held")
            elif not payline.reference_payline == None:
                row.append("Held and Released")
            elif payline.held:
                row.append("Held")

            writer.writerow(row)

        return response

    else:
        paylines_dict = OrderedDict()

        for payline in paylines:
            contractor_id = payline.contractor.id
            cycle_id = payline.cycle.id
            finance_code_id = payline.finance_code.id
            if not contractor_id in paylines_dict:
                paylines_dict[contractor_id] = {}
                paylines_dict[contractor_id]['contractor'] = payline.contractor
                paylines_dict[contractor_id]['cycles'] = OrderedDict()

            if not cycle_id in paylines_dict[contractor_id]['cycles']:
                paylines_dict[contractor_id]['cycles'][cycle_id] = {}
                paylines_dict[contractor_id]['cycles'][cycle_id][
                    'cycle'] = payline.cycle
                paylines_dict[contractor_id]['cycles'][cycle_id][
                    'finance_codes'] = OrderedDict()

            if not finance_code_id in paylines_dict[contractor_id]['cycles'][
                    cycle_id]['finance_codes']:
                paylines_dict[contractor_id]['cycles'][cycle_id][
                    'finance_codes'][finance_code_id] = {}
                paylines_dict[contractor_id]['cycles'][cycle_id][
                    'finance_codes'][finance_code_id][
                        'finance_code'] = payline.finance_code
                paylines_dict[contractor_id]['cycles'][cycle_id][
                    'finance_codes'][finance_code_id]['paylines'] = []

            paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][
                finance_code_id]['paylines'].append(payline)

        unlocked_cycles = Cycle.objects.values(
            'id', 'cycle_date').order_by('-id').filter(locked=False,
                                                       active=True)
        release_form = CalculationForm(request.GET)
        release_form.fields['c'].widget.choices = to_select(
            unlocked_cycles, 'id', 'cycle_date', '----------------', 'date')
        release_form.fields['c'].widget.attrs = {'class': 'search_release'}

        pp(form.errors)
        return render(
            request, 'pays/pays/search.html', {
                'form': form,
                'release_form': release_form,
                'paylines': paylines_dict,
                'config': config,
                'query_string': query_string
            })
Пример #7
0
                [('upload_file.csv', failed_file.getvalue(), 'text/csv')])

        messages.success(
            request,
            'Paylines added successfully. Any failures are being emailed to you.'
        )
        return HttpResponseRedirect(
            reverse('direct.pays.views.pays.upload',
                    args=[
                        request.info.get('current_projectname'),
                    ]))

    unlocked_cycles = Cycle.objects.values(
        'id', 'cycle_date').order_by('-id').filter(locked=False, active=True)
    form = UploadForm()
    form.fields['c'].widget.choices = to_select(unlocked_cycles, 'id',
                                                'cycle_date', None, 'date')

    return render(request, 'pays/pays/upload.html', {'form': form})


@access_required('direct.pays.admin')
def calculate(request, cycle_id):
    #log = logging.getLogger('pays')

    cycle = Cycle.objects.get(pk=cycle_id)
    cycle.calc_date = datetime.datetime.now()
    cycle.save()

    try:
        current_pay_run = PayRun.objects.get(cycle=cycle_id, approved=True)
        current_pay_run.approved = False
Пример #8
0
def search(request):
    config = Config.objects.get(application='pays')

    paylines = {}
    messages.info(request, 'To prevent timeouts search only returns the first 500 matching paylines.')

    form = SearchForm(request.GET)
    cycles = Cycle.objects.values().order_by('-id')
    locations = Location.objects.values('id', 'location')
    finance_codes = FinanceCode.objects.extra(select={'display': "vcfinance_code+' - '+vcdescription", 'order': "cast(vcfinance_code as int)"}).values('id','display', 'order').extra(order_by=['order'])

    form.fields['cycle'].widget.choices = to_select(cycles, 'id', 'cycle_date', '------------', 'date')
    form.fields['finance_code'].widget.choices = to_select(finance_codes, 'id', 'display')
    form.fields['location'].widget.choices = to_select(locations, 'id', 'location')

    
    query_string = request.META['QUERY_STRING']
    if any([bool(request.GET.get(x)) for x in request.GET]) and all(k not in form.errors for k in ('pay_id', 'payroll')):
        paylines = Payline.objects.select_related().all().filter(Q(pay_run__approved=True) | Q(pay_run=0), active=True, dummy=False)

        if request.GET.get('cycle'):
            paylines = paylines.filter(cycle=request.GET.get('cycle'))

        if request.GET.get('location'):
            paylines = paylines.filter(location=request.GET.get('location'))

        if request.GET.get('finance_code'):
            paylines = paylines.filter(finance_code=request.GET.get('finance_code'))

        if request.GET.get('sales_code'):
            paylines = paylines.filter(contractor__sales_code__contains=request.GET.get('sales_code'))

        if request.GET.get('payroll'):
            paylines = paylines.filter(contractor__payroll__contains=request.GET.get('payroll'))

        if request.GET.get('application_id'):
            paylines = paylines.filter(application_id__contains=request.GET.get('application_id'))

        if request.GET.get('pay_id'):
            paylines = paylines.filter(external_record_id=request.GET.get('pay_id'))

        paylines = paylines[:500]

        if not paylines:
            messages.warning(request, 'No paylines were found matching your criteria')


    if request.GET.get('fmt') == 'excel':
        head_row = (
            "Finance Code", "Location", "Contractor Id", "Sales Code", "Contractor", "Payroll",
            "Amount", "GST", "Details", "Pays Id", "Application Id", "Cycle"
        )

        response = HttpResponse(mimetype='text/csv')
        response['Content-Disposition'] = 'attachment; filename=Search Extract.csv'

        xstr = lambda s: s or ""


        writer = csv.writer(response, quoting=csv.QUOTE_ALL)
        writer.writerow(head_row)
        for payline in paylines:
            row = [payline.finance_code.description, payline.location, payline.contractor.id, payline.contractor.sales_code,
                    payline.contractor.name, payline.contractor.payroll, payline.value, payline.gst_value,
                    xstr(payline.details).encode('utf8'),
                    payline.external_record_id, payline.application_id, payline.cycle.cycle_date]
            if payline.perm_held:
                row.append("Perm Held")
            elif not payline.reference_payline == None:
                row.append("Held and Released")
            elif payline.held:
                row.append("Held")

            writer.writerow(row)

        return response

    else:
        paylines_dict = OrderedDict()

        for payline in paylines:
            contractor_id = payline.contractor.id
            cycle_id = payline.cycle.id
            finance_code_id = payline.finance_code.id
            if not contractor_id in paylines_dict:
                paylines_dict[contractor_id] = {}
                paylines_dict[contractor_id]['contractor'] = payline.contractor
                paylines_dict[contractor_id]['cycles'] = OrderedDict()

            if not cycle_id in paylines_dict[contractor_id]['cycles']:
                paylines_dict[contractor_id]['cycles'][cycle_id] = {}
                paylines_dict[contractor_id]['cycles'][cycle_id]['cycle'] = payline.cycle
                paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'] = OrderedDict()

            if not finance_code_id in paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes']:
                paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id] = {}
                paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id]['finance_code'] = payline.finance_code
                paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id]['paylines'] = []

            paylines_dict[contractor_id]['cycles'][cycle_id]['finance_codes'][finance_code_id]['paylines'].append(payline)

        unlocked_cycles = Cycle.objects.values('id', 'cycle_date').order_by('-id').filter(locked=False, active=True)
        release_form = CalculationForm(request.GET)
        release_form.fields['c'].widget.choices = to_select(unlocked_cycles, 'id', 'cycle_date', '----------------', 'date')
        release_form.fields['c'].widget.attrs = {'class':'search_release'}


        pp(form.errors)
        return render(request, 'pays/pays/search.html', {'form': form, 'release_form': release_form, 'paylines': paylines_dict, 'config': config, 'query_string': query_string})
Пример #9
0
        if failed:
            failed_file = StringIO()
            writer = csv.writer(failed_file)

            for row in failed:
                writer.writerow(row)

            send_email('Upload failures', 'Failed', [request.user.get('email')], '*****@*****.**', [('upload_file.csv', failed_file.getvalue(), 'text/csv')])

        messages.success(request, 'Paylines added successfully. Any failures are being emailed to you.')
        return HttpResponseRedirect(reverse('direct.pays.views.pays.upload', args=[request.info.get('current_projectname'),]))

    unlocked_cycles = Cycle.objects.values('id', 'cycle_date').order_by('-id').filter(locked=False, active=True)
    form = UploadForm()
    form.fields['c'].widget.choices = to_select(unlocked_cycles, 'id', 'cycle_date', None, 'date')

    return render(request, 'pays/pays/upload.html', {'form': form})

@access_required('direct.pays.admin')
def calculate(request, cycle_id):
    #log = logging.getLogger('pays')

    cycle = Cycle.objects.get(pk=cycle_id)
    cycle.calc_date = datetime.datetime.now()
    cycle.save()

    try:
        current_pay_run = PayRun.objects.get(cycle=cycle_id, approved=True)
        current_pay_run.approved = False
        current_pay_run.save()