diff --git a/pos/services/pos_engine.py b/pos/services/pos_engine.py index 619e579..dcc6962 100644 --- a/pos/services/pos_engine.py +++ b/pos/services/pos_engine.py @@ -18,6 +18,7 @@ from services.inventory_engine import ( record_operation, get_stock, ) +from services.accounting_engine import record_sale_entry, record_cancellation_entry def _to_dec(val): @@ -369,6 +370,20 @@ def process_sale(conn, sale_data): cur.close() + # Auto-generate accounting entry (non-blocking) + try: + record_sale_entry(conn, { + 'id': sale_id, + 'sale_type': sale_type, + 'total': totals['total'], + 'tax_total': totals['tax_total'], + 'subtotal': totals['subtotal'] - totals['discount_total'], + 'cost_total': sum(item.get('unit_cost', 0) * item['quantity'] for item in enriched_items), + 'payment_method': payment_method, + }) + except Exception: + pass # Accounting errors never block sales + return { 'id': sale_id, 'branch_id': branch_id, @@ -464,6 +479,19 @@ def cancel_sale(conn, sale_id, reason): WHERE id = %s """, (f"CANCELADA: {reason}", sale_id)) + # Reverse accounting entry (non-blocking) + try: + # Fetch tax_total for the reversal entry + cur.execute("SELECT tax_total FROM sales WHERE id = %s", (sale_id,)) + _tax_row = cur.fetchone() + record_cancellation_entry(conn, { + 'id': sale_id, + 'total': float(s_total), + 'tax_total': float(_tax_row[0]) if _tax_row else 0.0, + }) + except Exception: + pass # Accounting errors never block cancellations + # Reverse customer credit balance if credit sale if s_type == 'credit' and s_cust_id: cur.execute("""