# In __init__, call the setup method
self.setup_pos_ui()
self.current_sale_items = {} # Dictionary to store {drug_id: {data, quantity}}
def setup_pos_ui(self):
main_layout = QHBoxLayout()
# Left side: Sale and Barcode input
left_layout = QVBoxLayout()
barcode_group = QGroupBox("Scan Barcode")
barcode_layout = QVBoxLayout()
self.barcode_input = QLineEdit()
self.barcode_input.setPlaceholderText("Scan or type barcode and press Enter...")
self.barcode_input.returnPressed.connect(self.add_item_to_sale)
barcode_layout.addWidget(self.barcode_input)
barcode_group.setLayout(barcode_layout)
self.sales_table = QTableWidget()
self.sales_table.setColumnCount(5)
self.sales_table.setHorizontalHeaderLabels(['ID', 'Name', 'Quantity', 'Unit Price', 'Total Price'])
left_layout.addWidget(barcode_group)
left_layout.addWidget(self.sales_table)
# Right side: Totals and Actions
right_layout = QVBoxLayout()
total_group = QGroupBox("Sale Summary")
total_form = QFormLayout()
self.total_amount_label = QLabel("0.00")
total_form.addRow("Total Amount:", self.total_amount_label)
total_group.setLayout(total_form)
complete_sale_btn = QPushButton("Complete Sale")
complete_sale_btn.clicked.connect(self.complete_sale)
clear_sale_btn = QPushButton("Clear Sale")
clear_sale_btn.clicked.connect(self.clear_sale)
right_layout.addWidget(total_group)
right_layout.addWidget(complete_sale_btn)
right_layout.addWidget(clear_sale_btn)
right_layout.addStretch()
main_layout.addLayout(left_layout, stretch=3) # Left side takes 3/4 of space
main_layout.addLayout(right_layout, stretch=1) # Right side takes 1/4
self.pos_tab.setLayout(main_layout)
#Hashtags: #PointOfSale #BarcodeScanner #UIUX #PyQt5
---
#Step 4: Implementing the Sales LogicThis is the core logic that connects the barcode input to the sales table and the database. When a barcode is entered, we find the drug, add it to the current sale, and update the UI. The "Complete Sale" button will finalize the transaction by updating the database.
Add these methods to the
PharmacyApp class:def add_item_to_sale(self):
barcode = self.barcode_input.text()
if not barcode:
return
drug = db.find_drug_by_barcode(barcode)
if not drug:
QMessageBox.warning(self, "Not Found", "No drug found with this barcode.")
self.barcode_input.clear()
return
drug_id = drug[0]
if drug[3] <= 0: # Check quantity
QMessageBox.warning(self, "Out of Stock", f"{drug[1]} is out of stock.")
self.barcode_input.clear()
return
if drug_id in self.current_sale_items:
# Item already in sale, increment quantity
self.current_sale_items[drug_id]['quantity'] += 1
else:
# Add new item to sale
self.current_sale_items[drug_id] = {
'data': drug,
'quantity': 1
}
self.update_sales_table()
self.barcode_input.clear()
def update_sales_table(self):
self.sales_table.setRowCount(len(self.current_sale_items))
total_sale_amount = 0.0
for row, item in enumerate(self.current_sale_items.values()):
drug_data = item['data']
quantity = item['quantity']
unit_price = drug_data[4]
total_price = quantity * unit_price
self.sales_table.setItem(row, 0, QTableWidgetItem(str(drug_data[0]))) # ID
self.sales_table.setItem(row, 1, QTableWidgetItem(drug_data[1])) # Name
self.sales_table.setItem(row, 2, QTableWidgetItem(str(quantity)))
self.sales_table.setItem(row, 3, QTableWidgetItem(f"{unit_price:.2f}"))
self.sales_table.setItem(row, 4, QTableWidgetItem(f"{total_price:.2f}"))
total_sale_amount += total_price
self.total_amount_label.setText(f"{total_sale_amount:.2f}")
def complete_sale(self):
if not self.current_sale_items:
return
for drug_id, item in self.current_sale_items.items():
db.update_drug_quantity(drug_id, item['quantity'])
QMessageBox.information(self, "Success", f"Sale completed. Total: {self.total_amount_label.text()}")
self.clear_sale()
self.load_inventory_data() # Refresh inventory tab to show new quantities
def clear_sale(self):
self.current_sale_items.clear()
self.update_sales_table()
#Hashtags: #BusinessLogic #PointOfSale #PythonCode #Transaction
---
#Step 5: Results and DiscussionWith all the code in place, you have a fully functional pharmacy management system.
How to Use It:
• Run the
main.py script.• Go to the "Inventory Management" tab and add a few drugs with unique barcodes.
• Go to the "Point of Sale" tab. The cursor will be in the barcode input field.
• Type a barcode of a drug you added and press Enter. The drug will appear in the sales table.
• Scan the same barcode again. The quantity for that drug in the sales table will increase to 2.
• Click "Complete Sale". A success message will appear. The sales table will clear.
• Switch back to the "Inventory Management" tab. You will see that the quantity of the sold drugs has decreased accordingly.
❤2