예제 #1
0
파일: forms.py 프로젝트: 2Leadin/api-flask
    def validate_project_id(form, field):
        if not field.data:
            return

        if field.data:
            project = Project.find_by_id(field.data)
            if not project:
                raise ValidationError('Project not found.')
예제 #2
0
파일: views.py 프로젝트: Invoicy/invoicy
def create():
    body = request.get_json()

    if not 'invoice' in body:
        return '"invoice" parameter is missing', 400

    if not 'customer' in body['invoice']:
        return '"customer" parameter is missing', 400

    project = None
    if 'project' in body:
        project = Project.find_by_id(body['project'])

    customer = None
    if 'email' in body['invoice']['customer']:
        customer = Customer.find_by_email(body['invoice']['customer']['email'])
        if not customer and 'name' not in body['invoice']['customer']:
            return '"customer" not found', 404
    elif 'id' in body['invoice']['customer']:
        customer = Customer.find_by_id(body['invoice']['customer']['id'])
        if not customer:
            return '"customer" not found', 404

    if not customer:
        if not 'name' in body['invoice']['customer']:
            return '"customer.name" is required when creating one', 400

        customer = Customer.create(body['invoice']['customer'])

    invoice = Invoice('INVOICE', customer, project)
    invoice.currency = body['invoice']['currency'] if 'currency' in body['invoice'] else invoice.project.default_currency
    invoice.message = body['invoice']['message'] if 'message' in body['invoice'] else None
    invoice.due = body['invoice']['due'] if 'due' in body['invoice'] else None

    if 'reference' in body['invoice']:
        invoice.reference = body['invoice']['reference']
    else:
        try:
            invoice.set_reference()
        except IntegrityError:
            return 'Invalid Reference number given', 400

    if 'theme' in body:
        theme = Theme.find_by_id(body['theme'])
        if not theme:
            return "Invalid Theme ID : Theme not found", 404

        invoice.theme_id = theme.id
        invoice.theme = theme

    invoice.save()

    # Adding meta data
    for item in body['invoice']:
        if item in ['customer', 'currency', 'message', 'due', 'reference', 'entries', 'discounts', 'payments','refunds','notes','paid']:
            continue;
        InvoiceMeta.create(invoice, item, body['invoice'][item])


    if 'entries' in body['invoice']:
        entry_index = 1
        for entry in body['invoice']['entries']:
            if not 'description' in entry:
                return abort(400)

            if not 'amount' in entry:
                return abort(400)

            InvoiceEntry.create(invoice, entry, body['invoice']['tax'] if 'tax' in body['invoice'] else None, entry_index)
            entry_index += 1

    if 'discounts' in body['invoice']:
        for discount in body['invoice']['discounts']:
            if not 'amount' in discount:
                return abort(400)

            if not 'mode' in discount or discount['mode'] not in ['PERCENTAGE', 'FIXED']:
                return abort(400)

            if not 'option' in discount or discount['option'] not in ['SUBTOTAL', 'TOTAL']:
                discount['option'] = 'SUBTOTAL'

            InvoiceDiscount.create(invoice, discount)

    if 'payments' in body['invoice']:
        for payment in body['invoice']['payments']:
            if not 'amount' in payment:
                return abort(400)

            InvoicePayment.create(invoice, payment['amount'])

    if 'refunds' in body['invoice']:
        for refund in body['invoice']['refunds']:
            if not 'amount' in refund:
                return abort(400)

            InvoiceRefund.create(invoice, refund)

    if 'notes' in body['invoice']:
        for note in body['invoice']['notes']:
            if not 'description' in note:
                return abort(400)

            InvoiceNote.create(invoice, note['description'])

    if 'send' in body:
        # TODO later
        pass

    return jsonify(invoice.to_json()), 200