Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -205,10 +205,20 @@ def handle_manual_entry(uid, user_ref, user_data):
|
|
| 205 |
|
| 206 |
def validate_and_save_transaction(uid, user_data, data, file_hash, image_bytes, manual=False):
|
| 207 |
transactions_ref = db.reference('transactions')
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
total = float(data.get('total', 0))
|
| 214 |
if total > user_data['remaining_cash']:
|
|
@@ -230,15 +240,15 @@ def validate_and_save_transaction(uid, user_data, data, file_hash, image_bytes,
|
|
| 230 |
transaction_data = {
|
| 231 |
'uid': uid,
|
| 232 |
'total': total,
|
| 233 |
-
'items':
|
| 234 |
'date': data.get('date'),
|
| 235 |
-
'receipt_number':
|
| 236 |
'timestamp': datetime.now(pytz.UTC).isoformat(),
|
| 237 |
'hash': file_hash,
|
| 238 |
'image_url': image_url,
|
| 239 |
'manual_entry': manual
|
| 240 |
}
|
| 241 |
-
|
| 242 |
new_transaction_ref = transactions_ref.push(transaction_data)
|
| 243 |
return jsonify({
|
| 244 |
'success': True,
|
|
|
|
| 205 |
|
| 206 |
def validate_and_save_transaction(uid, user_data, data, file_hash, image_bytes, manual=False):
|
| 207 |
transactions_ref = db.reference('transactions')
|
| 208 |
+
receipt_number = data.get('receipt_number')
|
| 209 |
+
items = data.get('items', [])
|
| 210 |
+
|
| 211 |
+
# Fetch existing transactions with the same receipt_number
|
| 212 |
+
existing_transactions_with_receipt = transactions_ref.order_by_child('receipt_number').equal_to(receipt_number).get()
|
| 213 |
+
|
| 214 |
+
if existing_transactions_with_receipt:
|
| 215 |
+
for transaction_id, existing_transaction_data in existing_transactions_with_receipt.items():
|
| 216 |
+
# Compare items lists - assuming order doesn't matter, so sorting for comparison
|
| 217 |
+
existing_items = sorted(existing_transaction_data.get('items', []))
|
| 218 |
+
current_items = sorted(items)
|
| 219 |
+
|
| 220 |
+
if existing_items == current_items:
|
| 221 |
+
return jsonify({'error': f"Transaction with Receipt #{receipt_number} and identical items already exists"}), 400
|
| 222 |
|
| 223 |
total = float(data.get('total', 0))
|
| 224 |
if total > user_data['remaining_cash']:
|
|
|
|
| 240 |
transaction_data = {
|
| 241 |
'uid': uid,
|
| 242 |
'total': total,
|
| 243 |
+
'items': items,
|
| 244 |
'date': data.get('date'),
|
| 245 |
+
'receipt_number': receipt_number,
|
| 246 |
'timestamp': datetime.now(pytz.UTC).isoformat(),
|
| 247 |
'hash': file_hash,
|
| 248 |
'image_url': image_url,
|
| 249 |
'manual_entry': manual
|
| 250 |
}
|
| 251 |
+
|
| 252 |
new_transaction_ref = transactions_ref.push(transaction_data)
|
| 253 |
return jsonify({
|
| 254 |
'success': True,
|