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