Пример #1
0
def create_invoice():
    data = request.get_json()

    if data and data.get('customerId') is not None \
            and data.get('discount') is not None \
            and data.get('total') is not None \
            and len(data.get('invoiceItems')) is not 0:
        invoice = models.Invoice(
            public_id=str(uuid.uuid4()),
            customer_id=data['customerId'],
            discount=data['discount'],
            total=data['total']
        )
        db.session.add(invoice)
        db.session.commit()

        for item in data['invoiceItems']:
            if item and item['productId'] and item['quantity']:
                item = models.InvoiceItem(
                    public_id=str(uuid.uuid4()),
                    product_id=item['productId'],
                    invoice=invoice,
                    quantity=item['quantity']
                )
                db.session.add(item)
                db.session.commit()
            else:
                return jsonify({'message': 'Product and quantity for each invoice item is required!'}), 503

        return jsonify({'message': 'Invoice created successfully!'}), 201

    return jsonify({'message': 'All fields for invoice is required!'}), 503
Пример #2
0
    def generate_invoice(self, request, extra_context=None):
        '''Create an invoice from selected TimeEntry records.'''
        HOUR = Decimal(60 * 60)
        entries = request.POST.getlist('timeentry')
        entries = models.TimeEntry.objects.filter(
            id__in=entries).order_by('project')

        line_items = []
        back_refs = []
        if len(entries):
            customer = None
            cur_project = None
            cur_item = None
            cur_list = None
            for entry in entries:
                if customer is None:
                    customer = entry.project.customer
                # prevent an invoice with multiple customers.
                if customer != entry.project.customer:
                    continue

                if cur_project is None or cur_project != entry.project:
                    if cur_item is not None:
                        back_refs.append((cur_item, cur_list))
                    cur_list = []
                    cur_project = entry.project
                    cur_item = models.InvoiceLineItem()
                    cur_item.description = cur_project.title
                    cur_item.price = cur_project.rate
                    cur_item.quantity = Decimal(0)
                    line_items.append(cur_item)

                cur_item.quantity += Decimal(
                    entry.time_to_invoice().total_seconds()) / HOUR
                cur_list.append(entry)

        if cur_item is not None:
            back_refs.append((cur_item, cur_list))

        invoice = models.Invoice()
        invoice.customer = customer
        invoice.invoice_number = 'NEW'
        invoice.invoice_date = datetime.date.today()
        invoice.invoice_total = Decimal(0)
        invoice.save()

        for item in line_items:
            item.invoice = invoice
            item.save()

        for ref in back_refs:
            for entry in ref[1]:
                entry.invoice_line_item = ref[0]
                entry.save()

        return HttpResponseRedirect(
            reverse('admin:cm_invoice_change', args=[invoice.id]))
Пример #3
0
def add_invoice(date=date.today()):
    """ Add an invoice into the (DB) session

    :param date: date of the invoice or today if none is given
    :return: None
    """
    invoice = models.Invoice(date=date)
    db_session.add(invoice)
    db_session.commit()
    invoice_id = invoice.id
    return invoice_id
Пример #4
0
def create_invoice():
    session = models.get_session()
    particulars = request.form['particulars']
    amount = request.form['amount']
    cid = request.form['cid']
    invoice = models.Invoice(date=datetime.date.today(),
                             particulars=particulars,
                             customer_id=int(cid),
                             amount=amount)

    session.add(invoice)
    session.commit()
    return redirect(url_for('invoices', cid=cid, info="added"))
Пример #5
0
def invoices():
    cid = request.args.get('cid')
    session = models.get_session()
    if request.method == 'POST':
        date = request.form['date']
        particulars = request.form['particulars']
        amount = request.form['amount']
        invoice = models.Invoice(particulars=particulars,
                                 date=date,
                                 amount=amount,
                                 customer_id=cid)
        session.add(invoice)
        session.commit()

    customer = session.query(
        models.Customer).filter(models.Customer.id == cid).first()
    invoices = session.query(
        models.Invoice).filter(models.Invoice.customer_id == cid).all()

    return render_template("invoices.html",
                           customer=customer,
                           invoices=invoices)
Пример #6
0
def create_customer():
    session = models.get_session()

    name = request.form['name']
    address = request.form['address']
    email = request.form['email']

    new_customer = models.Customer(name=name,
                                   jdate=datetime.date.today(),
                                   address=address,
                                   email=email)
    session.add(new_customer)
    f = faker.Faker()
    for i in range(5):
        invoice = models.Invoice(particulars=f.sentence(),
                                 date=f.date(),
                                 amount=random.randint(10, 99),
                                 customer=new_customer)
        session.add(invoice)

    session.commit()
    return redirect(url_for('hello', info="added"))