Commit 1e8db8a9 authored by vipul.v's avatar vipul.v

mqtt and postgresql updated again

parent 696790ee
......@@ -24,53 +24,27 @@ class MqttConnectPostgresql:
print(
"Received message on topic '" + msg.topic + "' with message '" + msg.payload.decode('utf-8') + "'")
data = json.loads(msg.payload.decode('utf-8'))
print(data)
invoices = []
items = []
for invoice in data:
invoice_number = invoice['invoice_number']
client_name = invoice['client_name']
invoice_date = invoice['invoice_date']
due_date = invoice['due_date']
subtotal = invoice['subtotal']
tax_rate = invoice['tax_rate']
total = invoice['total']
paid = invoice['paid']
invoice_data = InvoiceDetails(
invoice_number=invoice['invoice_number'],
client_name=invoice['client_name'],
invoice_date=invoice['invoice_date'],
due_date=invoice['due_date'],
subtotal=invoice['subtotal'],
tax_rate=invoice['tax_rate'],
total=invoice['total'],
paid=invoice['paid'],
payment_method=invoice.get('payment_method'),
if paid:
payment_date = invoice['payment_date']
payment_method = invoice['payment_method']
billing_address = payment_method['billing_address']
invoice_data = InvoiceDetails(
invoice_number=invoice_number,
client_name=client_name,
invoice_date=invoice_date,
due_date=due_date,
subtotal=subtotal,
tax_rate=tax_rate,
total=total,
paid=paid,
payment_date=payment_date,
payment_method=payment_method,
billing_address=billing_address
)
else:
invoice_data = InvoiceDetails(
invoice_number=invoice_number,
client_name=client_name,
invoice_date=invoice_date,
due_date=due_date,
subtotal=subtotal,
tax_rate=tax_rate,
total=total,
paid=paid
)
)
invoices.append(invoice_data)
invoice_id = len(invoices)
with Session() as session:
session.add(invoice_data)
session.commit()
invoice_id = invoice_data.id
for item_data in invoice['items']:
item = Items(
invoice_id=invoice_id,
......@@ -81,7 +55,6 @@ class MqttConnectPostgresql:
items.append(item)
with Session() as session:
session.bulk_save_objects(invoices)
session.bulk_save_objects(items)
session.commit()
......@@ -92,4 +65,4 @@ class MqttConnectPostgresql:
client.loop_forever()
except Exception as e:
print(e, "Error detected in subscribe")
print(e, "Error detected in subscribe")
\ No newline at end of file
......@@ -21,7 +21,6 @@ class InvoiceDetails(Base):
paid = Column('paid', Boolean)
payment_date = Column('payment_date', Date)
payment_method = Column('payment_method', JSONB)
billing_address = Column('billing_address', JSONB)
class Items(Base):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment