Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -225,12 +225,13 @@ def handle_manual_entry(uid, user_ref, user_data):
|
|
| 225 |
except Exception as e:
|
| 226 |
return jsonify({'error': str(e)}), 400
|
| 227 |
|
|
|
|
| 228 |
def validate_and_save_transaction(uid, user_data, data, file_hash, image_bytes, manual=False):
|
| 229 |
transactions_ref = db.reference('transactions')
|
| 230 |
receipt_number = data.get('receipt_number')
|
| 231 |
items = data.get('items', [])
|
| 232 |
|
| 233 |
-
# Check for duplicate transactions based on receipt number and items
|
| 234 |
existing_transactions_with_receipt = transactions_ref.order_by_child('receipt_number').equal_to(receipt_number).get()
|
| 235 |
|
| 236 |
if existing_transactions_with_receipt:
|
|
@@ -242,20 +243,21 @@ def validate_and_save_transaction(uid, user_data, data, file_hash, image_bytes,
|
|
| 242 |
|
| 243 |
total = float(data.get('total', 0))
|
| 244 |
|
| 245 |
-
#
|
|
|
|
| 246 |
|
| 247 |
-
# Upload image if
|
| 248 |
-
image_url = None
|
| 249 |
if image_bytes and not manual:
|
| 250 |
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 251 |
blob = bucket.blob(f'receipts/{uid}/{timestamp}_{file_hash}.jpg')
|
| 252 |
blob.upload_from_string(image_bytes, content_type='image/jpeg')
|
| 253 |
image_url = blob.public_url
|
| 254 |
|
| 255 |
-
# Update user cash -
|
| 256 |
new_remaining = user_data['remaining_cash'] - total
|
| 257 |
db.reference(f'users/{uid}').update({'remaining_cash': new_remaining})
|
| 258 |
|
|
|
|
| 259 |
transaction_data = {
|
| 260 |
'uid': uid,
|
| 261 |
'total': total,
|
|
@@ -264,10 +266,14 @@ def validate_and_save_transaction(uid, user_data, data, file_hash, image_bytes,
|
|
| 264 |
'receipt_number': receipt_number,
|
| 265 |
'timestamp': datetime.now(pytz.UTC).isoformat(),
|
| 266 |
'hash': file_hash,
|
| 267 |
-
'image_url': image_url,
|
| 268 |
'manual_entry': manual
|
| 269 |
}
|
| 270 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
new_transaction_ref = transactions_ref.push(transaction_data)
|
| 272 |
return jsonify({
|
| 273 |
'success': True,
|
|
|
|
| 225 |
except Exception as e:
|
| 226 |
return jsonify({'error': str(e)}), 400
|
| 227 |
|
| 228 |
+
|
| 229 |
def validate_and_save_transaction(uid, user_data, data, file_hash, image_bytes, manual=False):
|
| 230 |
transactions_ref = db.reference('transactions')
|
| 231 |
receipt_number = data.get('receipt_number')
|
| 232 |
items = data.get('items', [])
|
| 233 |
|
| 234 |
+
# Check for duplicate transactions based on receipt number and items
|
| 235 |
existing_transactions_with_receipt = transactions_ref.order_by_child('receipt_number').equal_to(receipt_number).get()
|
| 236 |
|
| 237 |
if existing_transactions_with_receipt:
|
|
|
|
| 243 |
|
| 244 |
total = float(data.get('total', 0))
|
| 245 |
|
| 246 |
+
# Initialize image_url from data (could be None)
|
| 247 |
+
image_url = data.get('image_url')
|
| 248 |
|
| 249 |
+
# Upload image if image_bytes are provided and not manual
|
|
|
|
| 250 |
if image_bytes and not manual:
|
| 251 |
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 252 |
blob = bucket.blob(f'receipts/{uid}/{timestamp}_{file_hash}.jpg')
|
| 253 |
blob.upload_from_string(image_bytes, content_type='image/jpeg')
|
| 254 |
image_url = blob.public_url
|
| 255 |
|
| 256 |
+
# Update user cash - allowing negative values
|
| 257 |
new_remaining = user_data['remaining_cash'] - total
|
| 258 |
db.reference(f'users/{uid}').update({'remaining_cash': new_remaining})
|
| 259 |
|
| 260 |
+
# Build transaction data without image_url initially
|
| 261 |
transaction_data = {
|
| 262 |
'uid': uid,
|
| 263 |
'total': total,
|
|
|
|
| 266 |
'receipt_number': receipt_number,
|
| 267 |
'timestamp': datetime.now(pytz.UTC).isoformat(),
|
| 268 |
'hash': file_hash,
|
|
|
|
| 269 |
'manual_entry': manual
|
| 270 |
}
|
| 271 |
|
| 272 |
+
# Only add image_url if it is not None
|
| 273 |
+
if image_url is not None:
|
| 274 |
+
transaction_data['image_url'] = image_url
|
| 275 |
+
|
| 276 |
+
# Save the transaction
|
| 277 |
new_transaction_ref = transactions_ref.push(transaction_data)
|
| 278 |
return jsonify({
|
| 279 |
'success': True,
|