def setUpClass(cls): print("Setting up") conn = mongoengine.connect(db=DB_NAME, host=DB_HOST) for n in range(1, 11): invoice = Invoice(x=n, invoice_number=2) position = Position(**POSITION).save() #invoice.positions = [InvoiceItem(position_id=str(position.id))] invoice.positions = [InvoiceItem(position=position)] invoice.save()
def post(self): new_invoice = Invoice(client_name=request.json['client_name']) db.session.add(new_invoice) for item in request.json['invoice_items']: new_invoice_item = InvoiceItem( units = item['units'], description = item['description'], amount = item['amount'], invoice = new_invoice ) db.session.add(new_invoice_item) db.session.commit() return '', 204
def add_invoice(): product = Product(name=f'Produkt testowy {random.randint(1, 1000)}') for _ in range(3): product.stocks.append( ProductStock(quantity=random.randint(1, 10), purchase_price=1)) invoice = Invoice(account=random.choice(accounts), invoice_amount=10, invoice_number=str( random.randint(1, 9) * 100 + random.randint(0, 9) * 10 + random.randint(0, 9)), invoice_date=datetime.datetime.today()) for _ in range(5): invoice_item = InvoiceItem(product=product, name=product.name, quantity=1, price=2, discount=0) invoice.invoice_items.append(invoice_item) session.add(invoice) session.commit()
def previous_months_invoices_october(modeladmin, request, queryset): # response = HttpResponse(content_type='text') previous_month_patients = Patient.objects.raw( "select p.id, p.name, p.first_name " + "from public.invoices_patient p, public.invoices_prestation prest " + "where p.id = prest.patient_id " + "and prest.date between '2014-10-01'::DATE and '2014-10-31'::DATE " + "and p.private_patient = 'f' " + "and (select count(inv.id) from public.invoices_invoiceitem inv " + "where inv.invoice_date between '2014-10-01'::DATE and '2014-10-31'::DATE " + "and inv.patient_id = p.id) = 0" + "group by p.id " + "order by p.name") invoice_counters = 0 for p in previous_month_patients: invoiceitem = InvoiceItem(patient=p, invoice_date=datetime.datetime(2014, 10, 31), invoice_sent=False, invoice_paid=False) invoiceitem.clean() invoiceitem.save() invoice_counters = invoice_counters + 1
def add_invoice(): account = session.query(Account).first() product = Product(name='Produkt testowy') for _ in range(3): product.stocks.append( ProductStock(quantity=random.randint(1, 10), purchase_price=1)) invoice = Invoice(account=account, invoice_amount=2, invoice_number='123', invoice_date=datetime.datetime.today()) for _ in range(5): invoice_item = InvoiceItem(product=product, name='Zakup: ' + product.name, quantity=1, price=2, discount=0) invoice.invoice_items.append(invoice_item) session.add(invoice) session.commit()
def create_invoice(modeladmin, request, queryset): # response = HttpResponse(content_type='text') not_invoiced_prestas = Prestation.objects.raw( "select p.id,p.patient_id,p.carecode_id,p.date" + " from invoices_prestation p " + "left join invoices_invoiceitem_prestations ip on p.id = ip.prestation_id " + "where ip.prestation_id is NULL") from collections import defaultdict prestations_to_invoice = defaultdict(list) invoices_created = [] invpks = [] for p in queryset: if p in not_invoiced_prestas: prestations_to_invoice[p.patient].append(p) for k, v in prestations_to_invoice.iteritems(): if (k.private_patient): invoiceitem = PrivateInvoiceItem( private_patient=k, invoice_date=datetime.datetime.now(), invoice_sent=False, invoice_paid=False) else: invoiceitem = InvoiceItem(patient=k, invoice_date=datetime.datetime.now(), invoice_sent=False, invoice_paid=False) invoices_created.append(invoiceitem) invpks.append(invoiceitem.pk) invoiceitem.save() for prestav in v: invoiceitem.prestations.add(prestav) #return HttpResponseRedirect("/admin/invoices/invoiceitem/?ct=%s&ids=%s" % (invoiceitem.pk, ",".join(invpks))) return HttpResponseRedirect("/admin/invoices/invoiceitem/")
import sqlalchemy as sa from sqlalchemy.orm import sessionmaker from helpers import Timer from models import Account, Invoice, Product, InvoiceItem, ProductStock engine = sa.engine.create_engine('sqlite:///litedb', echo=False) session = sessionmaker(bind=engine)() session.query(Invoice).delete() session.query(InvoiceItem).delete() session.query(Product).delete() session.query(ProductStock).delete() session.commit() account = session.query(Account).first() with Timer('Insert big invoice'): invoice = Invoice(account=account, invoice_number='test') for _ in range(10000): product = Product(name=random.choice(string.ascii_uppercase) + str(random.randint(1, 10000))) invoice_item = InvoiceItem(invoice=invoice, product=product, quantity=1, price=1) session.add(invoice) session.commit()
session.query(Invoice).delete() session.query(InvoiceItem).delete() session.query(Product).delete() session.query(ProductStock).delete() session.commit() account = session.query(Account).first() with Timer('Insert big invoice'): invoice = Invoice(account=account, invoice_number='test') session.add(invoice) session.flush() products = [] items = [] for _ in range(10000): product = Product(name=random.choice(string.ascii_uppercase) + str(random.randint(1, 10000))) products.append(product) # session.bulk_save_objects(products) # we need to fix it session.bulk_save_objects(products, return_defaults=True) session.flush() for row in products: invoice_item = InvoiceItem(invoice_id=invoice.id, product_id=row.id, quantity=1, price=1) items.append(invoice_item) session.bulk_save_objects(items) items = session.query(InvoiceItem) session.commit()
product=product, purchase_price=random.randint(1, 9) ) session.add(product) session.commit() accounts = session.query(Account).all() products = session.query(Product).all() items = [] for account in accounts: for _ in range(random.randint(1,5)): invoice = Invoice( account=account, invoice_number="".join(random.choices(string.ascii_uppercase, k=5)) + str(random.randint(0,9)) ) session.add(invoice) session.flush() for row in random.choices(products, k=random.randint(1,9)): invoice_item = InvoiceItem( invoice_id=invoice.id, product_id=row.id, quantity=1, name="Order: " + row.name, price=row.price * 2 ) items.append(invoice_item) session.bulk_save_objects(items) session.commit() # session.rollback()
import os from config import db from models import Invoice, InvoiceItem INVOICES = [ {'client_name': 'Eastridge', 'invoice_items': [{'units': 5, 'description': 'Some cool product #1', 'amount': 19.99}, {'units': 10, 'description': 'Some cool product #2', 'amount': 29.99}]}, {'client_name': 'Ilgor', 'invoice_items': [{'units': 20, 'description': 'Some cool product', 'amount': 39.99}]} ] if os.path.exists("invoices.db"): os.remove("invoices.db") db.create_all() for invoice in INVOICES: i = Invoice(client_name=invoice['client_name']) for item in invoice['invoice_items']: ii = InvoiceItem(units=item['units'], description=item['description'], amount=item['amount'], invoice=i) db.session.add(ii) db.session.add(i) db.session.commit()