Beispiel #1
0
def create_application_invoice(application, active_payment_type, action="send"):
    bill_series = application.competition.bill_series
    prefix = active_payment_type.payment_channel.erekins_url_prefix

    # Create new requests session with Auth keys and prepended url
    session = SessionWHeaders({'Authorization': 'ApiKey %s' % active_payment_type.payment_channel.erekins_auth_key}, url="https://%s.e-rekins.lv" % prefix)

    # Find series resource_uri from e-rekins
    series_obj = session.get("/api/v1/series/?prefix=%s" % bill_series)
    if series_obj.json().get('meta').get('total_count') == 0:
        series_obj = session.post("/api/v1/series/", data=json.dumps({
            'title': bill_series,
            'reset_period': 4,
            'prefix': bill_series,
            'formatting': '- #'
        }))
        series_obj.raise_for_status()
        series_resource_uri = series_obj.headers.get('Location')
    else:
        series_resource_uri = series_obj.json().get('objects')[0].get('resource_uri')

    client_data = {
        'name': application.company_name,
        'name_short': application.company_name,
        'integration_code': str(application.id),
        'number': application.company_regnr,
        'vat': application.company_vat,
        'email': application.email,
        'office_address': {
            'address': application.company_address,
            'country': '/api/v1/country/124/',  # Always default is Latvia
        },
        'juridical_address':  {
            'address': application.company_juridical_address,
            'country': '/api/v1/country/124/',  # Always default is Latvia
        },
        'form': 1,  # Juridical
    }

    # Find or Create customer
    client_obj = session.get("/api/v1/client/?number=%s" % application.company_regnr)
    if client_obj.json().get('meta').get('total_count') == 0:
        client_obj = session.post("/api/v1/client/", data=json.dumps(client_data))
        client_obj.raise_for_status()
        client_obj = session.get(client_obj.headers.get('Location'))
        client_obj.raise_for_status()
        client_resource_uri = client_obj.json().get('resource_uri')
    else:
        client_resource_uri = client_obj.json().get('objects')[0].get('resource_uri')
        client_obj = session.put(client_resource_uri, data=json.dumps(client_data))
        client_obj.raise_for_status()

    items = []
    for participant in application.participant_set.all():
        if application.invoice_show_names:
            description = "Dalības maksa %(competition)s - %(distance)s - %(full_name)s (%(year)i)" % {
                "competition": application.competition.get_full_name,
                "distance": unicode(participant.distance),
                "full_name": participant.full_name,
                "year": participant.birthday.year
            }
        else:
            description = "Dalības maksa %(competition)s " % {
                "competition": application.competition.get_full_name,
            }

        items.append({
            "description": description,
            "vat": getattr(settings, "EREKINS_%s_DEFAULT_VAT" % active_payment_type.payment_channel.payment_channel),
            "units": "gab.",
            "amount": "1",
            "price": get_participant_fee_from_price(participant.competition, participant.price)
        })
        if participant.insurance:
            items.append({
                "description": " - Apdrošināšana %(insurance)s" % {
                    "insurance": participant.insurance,
                },
                "vat": getattr(settings, "EREKINS_%s_DEFAULT_VAT" % active_payment_type.payment_channel.payment_channel),
                "units": "gab.",
                "amount": "1",
                "price": get_insurance_fee_from_insurance(participant.competition, participant.insurance)
            })
    if application.donation > 0:
        information = application.competition.params.get('donation', {}).get('bank_code', 'Ziedojums - %s') % application.donation
        items.append({
            "description": information,
            "vat": getattr(settings, "EREKINS_%s_DEFAULT_VAT" % active_payment_type.payment_channel.payment_channel),
            "units": "gab.",
            "amount": "1",
            "price": float(application.donation)
        })
    if not application.competition.complex_payment_enddate:
        competition_date = application.competition.competition_date
    else:
        competition_date = application.competition.get_children()[0].competition_date
    competition_datetime = datetime.datetime.combine(competition_date, datetime.time())
    now = datetime.datetime.now()
    if now + datetime.timedelta(days=7) > competition_datetime:
        due_date = now
    elif now + datetime.timedelta(days=14) > competition_datetime:
        due_date = competition_datetime - datetime.timedelta(days=6)
    else:
        due_date = now + datetime.timedelta(days=7)

    invoice_data = {
        "due_date": due_date.strftime('%Y-%m-%d'),
        "client": client_resource_uri,
        "currency": "/api/v1/currency/1/",
        "invoice_creator": getattr(settings, "EREKINS_%s_DEFAULT_CREATOR" % active_payment_type.payment_channel.payment_channel),
        "series": series_resource_uri,
        "language": getattr(settings, "EREKINS_%s_DEFAULT_LANGUAGE" % active_payment_type.payment_channel.payment_channel),
        "payment_type": "Pārskaitījums",
        "kind": "2",
        "items": items,
        "status": "10",
        "activity": 'Pakalpojumu sniegšana',
        "ad_integration_code": application.id,
        "comments": "Nesaņemot apmaksu līdz norādītajam termiņam, rēķins zaudē spēku un dalībnieki starta sarakstā neparādās, kā arī netiek pielaisti pie starta.",
        "action": action,
        "invoice_external_edit_url": ("%s%s" % (settings.MY_DEFAULT_DOMAIN, reverse('manager:application_pay', kwargs={'pk': application.competition_id, 'pk2': application.id}))).replace('http://', 'https://'),
    }

    # Create invoice
    invoice_obj = session.post("/api/v1/invoice/", data=json.dumps(invoice_data))
    invoice_obj.raise_for_status()
    invoice_obj = session.get(invoice_obj.headers.get('Location'))
    invoice_obj.raise_for_status()
    invoice = invoice_obj.json()
    return invoice.get('code'), invoice.get('invoice_nr')
Beispiel #2
0
def create_team_invoice(team, active_payment_type, action="send"):
    bill_series = team.distance.competition.bill_series
    prefix = active_payment_type.payment_channel.erekins_url_prefix

    # Create new requests session with Auth keys and prepended url
    session = SessionWHeaders({'Authorization': 'ApiKey %s' % active_payment_type.payment_channel.erekins_auth_key}, url="https://%s.e-rekins.lv" % prefix)

    # Find series resource_uri from e-rekins
    series_obj = session.get("/api/v1/series/?prefix=%s" % bill_series)
    if series_obj.json().get('meta').get('total_count') == 0:
        series_obj = session.post("/api/v1/series/", data=json.dumps({
            'title': bill_series,
            'reset_period': 4,
            'prefix': bill_series,
            'formatting': '- #'
        }))
        series_obj.raise_for_status()
        series_resource_uri = series_obj.headers.get('Location')
    else:
        series_resource_uri = series_obj.json().get('objects')[0].get('resource_uri')

    client_data = {
        'name': team.company_name,
        'name_short': team.company_name,
        'integration_code': "team_%s" % str(team.id),
        'number': team.company_regnr,
        'vat': team.company_vat,
        'email': team.email,
        'office_address': {
            'address': team.company_address,
            'country': '/api/v1/country/124/',  # Always default is Latvia
        },
        'juridical_address':  {
            'address': team.company_juridical_address,
            'country': '/api/v1/country/124/',  # Always default is Latvia
        },
        'form': 1,  # Juridical
    }

    # Find or Create customer
    client_obj = session.get("/api/v1/client/?number=%s" % team.company_regnr)
    if client_obj.json().get('meta').get('total_count') == 0:
        client_obj = session.post("/api/v1/client/", data=json.dumps(client_data))
        client_obj.raise_for_status()
        client_obj = session.get(client_obj.headers.get('Location'))
        client_obj.raise_for_status()
        client_resource_uri = client_obj.json().get('resource_uri')
    else:
        client_resource_uri = client_obj.json().get('objects')[0].get('resource_uri')
        client_obj = session.put(client_resource_uri, data=json.dumps(client_data))
        client_obj.raise_for_status()

    items = [{
            "description": "Komandas %s profila apmaksa" % unicode(team),
            "vat": getattr(settings, "EREKINS_%s_DEFAULT_VAT" % active_payment_type.payment_channel.payment_channel),
            "units": "gab.",
            "amount": "1",
            "price": float(team.final_price)
        }, ]

    due_date = datetime.datetime.now() + datetime.timedelta(days=7)
    invoice_data = {
        "due_date": due_date.strftime('%Y-%m-%d'),
        "client": client_resource_uri,
        "currency": "/api/v1/currency/1/",
        "invoice_creator": getattr(settings, "EREKINS_%s_DEFAULT_CREATOR" % active_payment_type.payment_channel.payment_channel),
        "series": series_resource_uri,
        "language": getattr(settings, "EREKINS_%s_DEFAULT_LANGUAGE" % active_payment_type.payment_channel.payment_channel),
        "payment_type": "Pārskaitījums",
        "kind": "2",
        "items": items,
        "status": "10",
        "activity": 'Pakalpojumu sniegšana',
        "ad_integration_code": "team_%i" % team.id,
        "action": action,
    }

    # Create invoice
    invoice_obj = session.post("/api/v1/invoice/", data=json.dumps(invoice_data))
    invoice_obj.raise_for_status()
    invoice_obj = session.get(invoice_obj.headers.get('Location'))
    invoice_obj.raise_for_status()
    invoice = invoice_obj.json()
    return invoice.get('code'), invoice.get('invoice_nr')