feat: cashier/counter reports, service-order & remission flows, Rached migration utils
- Add "Mis cortes de caja" report for cashiers/counters with sales detail. - Cash register history scoped to own cuts for non-admin roles; new /register/<id>/sales endpoint. - Remove dashboard from cashier menu; add Reports to cashier/counter. - Service orders: assign mechanic, budget field, invoice flag, counter/cashier can add items/remissions, convert to remission. - Remission notes module (UI, CSS, courier, counter remissions). - Customer hard-delete and vehicle/customer linkage in workshop. - POS: always show search results, compact payment grid, credit validation, tier pricing (5%/10%), ticket with customer/folio. - Inventory: CSV template with sku_secondary, alias import. - Rached migration scripts and DB migrations. - Version-bump cached JS/CSS query strings. Excludes local Rached session tokens/captures (rached_*.json / rached_*.txt).
This commit is contained in:
@@ -8,15 +8,43 @@ from datetime import datetime
|
||||
|
||||
from services import inventory_engine
|
||||
|
||||
# Rached workshop statuses (applies to all tenants).
|
||||
ORDER_STATUSES = [
|
||||
'por_revisar',
|
||||
'en_revision',
|
||||
'revisada',
|
||||
'cotizada',
|
||||
'por_autorizar',
|
||||
'autorizada',
|
||||
'autorizacion_parcial',
|
||||
'en_reparacion',
|
||||
'reparada',
|
||||
'por_entregar',
|
||||
'entregado',
|
||||
'por_enviar',
|
||||
'enviado',
|
||||
'por_facturar',
|
||||
'facturada',
|
||||
'por_recolectar',
|
||||
'cancelada',
|
||||
]
|
||||
TERMINAL_STATUSES = {'entregado', 'facturada', 'cancelada'}
|
||||
|
||||
# Allow moving from any non-terminal status to any other non-terminal status,
|
||||
# plus cancellation. Terminal statuses cannot change.
|
||||
VALID_TRANSITIONS = {
|
||||
'received': ['diagnosis', 'cancelled'],
|
||||
'diagnosis': ['waiting_parts', 'repair', 'cancelled'],
|
||||
'waiting_parts': ['repair', 'cancelled'],
|
||||
'repair': ['ready', 'cancelled'],
|
||||
'ready': ['delivered', 'cancelled'],
|
||||
'delivered': [],
|
||||
'cancelled': [],
|
||||
status: [s for s in ORDER_STATUSES if s != status]
|
||||
for status in ORDER_STATUSES
|
||||
}
|
||||
for terminal in TERMINAL_STATUSES:
|
||||
VALID_TRANSITIONS[terminal] = []
|
||||
|
||||
# Legacy statuses (kept valid for imported/old data) cannot transition anywhere.
|
||||
_LEGACY_STATUSES = {
|
||||
'received', 'diagnosis', 'waiting_parts', 'repair', 'quality_check', 'ready', 'delivered'
|
||||
}
|
||||
for legacy in _LEGACY_STATUSES:
|
||||
VALID_TRANSITIONS.setdefault(legacy, [])
|
||||
|
||||
|
||||
def _generate_order_number(conn):
|
||||
@@ -45,6 +73,18 @@ def _generate_order_number(conn):
|
||||
return f"{prefix}{new_num}"
|
||||
|
||||
|
||||
_VALID_DELIVERY_METHODS = {'pickup', 'delivery', 'courier'}
|
||||
|
||||
|
||||
def _normalize_delivery(data):
|
||||
"""Restrict delivery_method to allowed values and clear courier when not applicable."""
|
||||
dm = data.get('delivery_method')
|
||||
if dm not in _VALID_DELIVERY_METHODS:
|
||||
data['delivery_method'] = None
|
||||
if data.get('delivery_method') not in ('delivery', 'courier'):
|
||||
data['courier_id'] = None
|
||||
|
||||
|
||||
def create_service_order(conn, data):
|
||||
"""Create a new service order.
|
||||
|
||||
@@ -52,9 +92,10 @@ def create_service_order(conn, data):
|
||||
customer_id, vehicle_id, branch_id, priority,
|
||||
reception_notes, estimated_cost, estimated_completion,
|
||||
employee_id, mileage_in, fuel_level, created_by,
|
||||
delivery_method, courier_id, is_direct
|
||||
delivery_method, courier_id, is_direct, requires_invoice
|
||||
}
|
||||
"""
|
||||
_normalize_delivery(data)
|
||||
cur = conn.cursor()
|
||||
order_number = _generate_order_number(conn)
|
||||
|
||||
@@ -62,19 +103,23 @@ def create_service_order(conn, data):
|
||||
INSERT INTO service_orders
|
||||
(tenant_id, branch_id, customer_id, vehicle_id, order_number, status,
|
||||
priority, reception_notes, estimated_cost, estimated_completion,
|
||||
employee_id, mileage_in, fuel_level, created_by,
|
||||
delivery_method, courier_id, is_direct)
|
||||
VALUES (%s, %s, %s, %s, %s, 'received', %s, %s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s)
|
||||
employee_id, mechanic_name, mileage_in, fuel_level, created_by,
|
||||
delivery_method, courier_id, is_direct, requires_invoice,
|
||||
workshop_name, customer_address, customer_phone, vehicle_description)
|
||||
VALUES (%s, %s, %s, %s, %s, 'por_revisar', %s, %s, %s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING id
|
||||
""", (
|
||||
data.get('tenant_id'), data.get('branch_id'), data.get('customer_id'),
|
||||
data.get('vehicle_id'), order_number,
|
||||
data.get('priority', 'normal'), data.get('reception_notes'),
|
||||
data.get('estimated_cost'), data.get('estimated_completion'),
|
||||
data.get('employee_id'), data.get('mileage_in'),
|
||||
data.get('fuel_level'), data.get('created_by'),
|
||||
data.get('delivery_method'), data.get('courier_id'), data.get('is_direct', False),
|
||||
data.get('employee_id'), data.get('mechanic_name'),
|
||||
data.get('mileage_in'), data.get('fuel_level'), data.get('created_by'),
|
||||
data.get('delivery_method'), data.get('courier_id'),
|
||||
data.get('is_direct', False), data.get('requires_invoice', False),
|
||||
data.get('workshop_name'), data.get('customer_address'),
|
||||
data.get('customer_phone'), data.get('vehicle_description'),
|
||||
))
|
||||
so_id = cur.fetchone()[0]
|
||||
|
||||
@@ -82,7 +127,7 @@ def create_service_order(conn, data):
|
||||
cur.execute("""
|
||||
INSERT INTO service_order_status_history
|
||||
(service_order_id, new_status, changed_by, notes)
|
||||
VALUES (%s, 'received', %s, 'Orden creada')
|
||||
VALUES (%s, 'por_revisar', %s, 'Orden creada')
|
||||
""", (so_id, data.get('created_by')))
|
||||
|
||||
conn.commit()
|
||||
@@ -95,7 +140,7 @@ def get_service_order(conn, so_id):
|
||||
cur.execute("""
|
||||
SELECT so.id, so.order_number, so.status, so.priority,
|
||||
so.customer_id, c.name as customer_name, c.phone as customer_phone,
|
||||
c.address as customer_address,
|
||||
c.address as customer_address, c.price_tier as customer_price_tier,
|
||||
so.vehicle_id, fv.plate as vehicle_plate, fv.make as vehicle_make, fv.model as vehicle_model,
|
||||
so.branch_id, b.name as branch_name, b.address as branch_address, b.phone as branch_phone,
|
||||
so.reception_notes, so.diagnosis_notes, so.repair_notes,
|
||||
@@ -103,10 +148,12 @@ def get_service_order(conn, so_id):
|
||||
so.estimated_completion, so.actual_completion, so.delivered_at,
|
||||
so.mileage_in, so.mileage_out, so.fuel_level,
|
||||
so.employee_id, e.name as employee_name,
|
||||
so.mechanic_name,
|
||||
so.created_by, creator.name as created_by_name,
|
||||
so.created_at, so.updated_at,
|
||||
so.delivery_method, so.courier_id, co.name as courier_name, so.is_direct,
|
||||
so.sale_id
|
||||
so.requires_invoice, so.sale_id,
|
||||
so.workshop_name, so.customer_address, so.customer_phone, so.vehicle_description
|
||||
FROM service_orders so
|
||||
LEFT JOIN customers c ON so.customer_id = c.id
|
||||
LEFT JOIN fleet_vehicles fv ON so.vehicle_id = fv.id
|
||||
@@ -124,28 +171,33 @@ def get_service_order(conn, so_id):
|
||||
so = {
|
||||
'id': row[0], 'order_number': row[1], 'status': row[2], 'priority': row[3],
|
||||
'customer_id': row[4], 'customer_name': row[5], 'customer_phone': row[6],
|
||||
'customer_address': row[7],
|
||||
'vehicle_id': row[8], 'vehicle_plate': row[9], 'vehicle_make': row[10], 'vehicle_model': row[11],
|
||||
'branch_id': row[12], 'branch_name': row[13], 'branch_address': row[14], 'branch_phone': row[15],
|
||||
'reception_notes': row[16], 'diagnosis_notes': row[17],
|
||||
'repair_notes': row[18], 'delivery_notes': row[19],
|
||||
'estimated_cost': float(row[20]) if row[20] else None,
|
||||
'final_cost': float(row[21]) if row[21] else None,
|
||||
'estimated_completion': str(row[22]) if row[22] else None,
|
||||
'actual_completion': str(row[23]) if row[23] else None,
|
||||
'delivered_at': str(row[24]) if row[24] else None,
|
||||
'mileage_in': row[25], 'mileage_out': row[26], 'fuel_level': row[27],
|
||||
'employee_id': row[28], 'employee_name': row[29],
|
||||
'created_by': row[30], 'created_by_name': row[31],
|
||||
'created_at': str(row[32]), 'updated_at': str(row[33]),
|
||||
'delivery_method': row[34], 'courier_id': row[35], 'courier_name': row[36],
|
||||
'is_direct': bool(row[37]) if row[37] is not None else False,
|
||||
'sale_id': row[38],
|
||||
'customer_address': row[7], 'customer_price_tier': row[8],
|
||||
'vehicle_id': row[9], 'vehicle_plate': row[10], 'vehicle_make': row[11], 'vehicle_model': row[12],
|
||||
'branch_id': row[13], 'branch_name': row[14], 'branch_address': row[15], 'branch_phone': row[16],
|
||||
'reception_notes': row[17], 'diagnosis_notes': row[18],
|
||||
'repair_notes': row[19], 'delivery_notes': row[20],
|
||||
'estimated_cost': float(row[21]) if row[21] else None,
|
||||
'final_cost': float(row[22]) if row[22] else None,
|
||||
'estimated_completion': str(row[23]) if row[23] else None,
|
||||
'actual_completion': str(row[24]) if row[24] else None,
|
||||
'delivered_at': str(row[25]) if row[25] else None,
|
||||
'mileage_in': row[26], 'mileage_out': row[27], 'fuel_level': row[28],
|
||||
'employee_id': row[29], 'employee_name': row[30],
|
||||
'mechanic_name': row[31],
|
||||
'created_by': row[32], 'created_by_name': row[33],
|
||||
'created_at': str(row[34]), 'updated_at': str(row[35]),
|
||||
'delivery_method': row[36], 'courier_id': row[37], 'courier_name': row[38],
|
||||
'is_direct': bool(row[39]) if row[39] is not None else False,
|
||||
'requires_invoice': bool(row[40]) if row[40] is not None else False,
|
||||
'sale_id': row[41],
|
||||
'workshop_name': row[42], 'customer_address': row[43],
|
||||
'customer_phone': row[44], 'vehicle_description': row[45],
|
||||
}
|
||||
|
||||
# Items
|
||||
cur.execute("""
|
||||
SELECT id, inventory_id, part_number, name, quantity, unit_cost, unit_price, status, notes
|
||||
SELECT id, inventory_id, part_number, name, quantity, unit_cost, unit_price, status, notes,
|
||||
mechanic_id, observations
|
||||
FROM service_order_items
|
||||
WHERE service_order_id = %s
|
||||
ORDER BY id
|
||||
@@ -161,6 +213,7 @@ def get_service_order(conn, so_id):
|
||||
'unit_cost': float(r[5]) if r[5] else None,
|
||||
'unit_price': price,
|
||||
'status': r[7], 'notes': r[8],
|
||||
'mechanic_id': r[9], 'observations': r[10],
|
||||
})
|
||||
total_parts += qty * price
|
||||
|
||||
@@ -238,8 +291,11 @@ def list_service_orders(conn, status=None, branch_id=None, customer_id=None,
|
||||
where_clauses.append("so.is_direct = %s")
|
||||
params.append(is_direct)
|
||||
if q:
|
||||
where_clauses.append("(so.order_number ILIKE %s OR c.name ILIKE %s OR fv.plate ILIKE %s)")
|
||||
params.extend([f'%{q}%', f'%{q}%', f'%{q}%'])
|
||||
where_clauses.append(
|
||||
"(so.order_number ILIKE %s OR c.name ILIKE %s OR fv.plate ILIKE %s OR "
|
||||
"so.workshop_name ILIKE %s OR so.vehicle_description ILIKE %s)"
|
||||
)
|
||||
params.extend([f'%{q}%', f'%{q}%', f'%{q}%', f'%{q}%', f'%{q}%'])
|
||||
|
||||
where_clauses.append("so.is_deleted = false")
|
||||
where = " AND ".join(where_clauses)
|
||||
@@ -259,14 +315,18 @@ def list_service_orders(conn, status=None, branch_id=None, customer_id=None,
|
||||
so.branch_id, b.name as branch_name,
|
||||
so.estimated_cost, so.final_cost,
|
||||
so.delivery_method, co.name as courier_name, so.is_direct,
|
||||
so.sale_id, so.created_at,
|
||||
creator.name as created_by_name
|
||||
so.requires_invoice, so.sale_id, so.created_at,
|
||||
creator.name as created_by_name,
|
||||
so.employee_id, mech.name as employee_name,
|
||||
so.mechanic_name,
|
||||
so.workshop_name, so.vehicle_description
|
||||
FROM service_orders so
|
||||
LEFT JOIN customers c ON so.customer_id = c.id
|
||||
LEFT JOIN fleet_vehicles fv ON so.vehicle_id = fv.id
|
||||
LEFT JOIN branches b ON so.branch_id = b.id
|
||||
LEFT JOIN couriers co ON so.courier_id = co.id
|
||||
LEFT JOIN employees creator ON so.created_by = creator.id
|
||||
LEFT JOIN employees mech ON so.employee_id = mech.id
|
||||
WHERE {where}
|
||||
ORDER BY
|
||||
CASE so.priority
|
||||
@@ -292,8 +352,12 @@ def list_service_orders(conn, status=None, branch_id=None, customer_id=None,
|
||||
'final_cost': final,
|
||||
'delivery_method': r[14], 'courier_name': r[15],
|
||||
'is_direct': bool(r[16]) if r[16] is not None else False,
|
||||
'sale_id': r[17], 'created_at': str(r[18]),
|
||||
'created_by_name': r[19],
|
||||
'requires_invoice': bool(r[17]) if r[17] is not None else False,
|
||||
'sale_id': r[18], 'created_at': str(r[19]),
|
||||
'created_by_name': r[20],
|
||||
'employee_id': r[21], 'employee_name': r[22],
|
||||
'mechanic_name': r[23],
|
||||
'workshop_name': r[24], 'vehicle_description': r[25],
|
||||
'total': round(final or estimated, 2),
|
||||
'paid': 0.0, # to be computed if needed
|
||||
})
|
||||
@@ -323,9 +387,9 @@ def update_status(conn, so_id, new_status, changed_by=None, notes=None):
|
||||
# Update status
|
||||
extra_sets = []
|
||||
extra_vals = []
|
||||
if new_status == 'ready':
|
||||
if new_status == 'reparada':
|
||||
extra_sets.append("actual_completion = NOW()")
|
||||
if new_status == 'delivered':
|
||||
if new_status == 'entregado':
|
||||
extra_sets.append("delivered_at = NOW()")
|
||||
extra_sets.append("delivered_by = %s")
|
||||
extra_vals.append(changed_by)
|
||||
@@ -354,14 +418,16 @@ def add_item(conn, so_id, item_data):
|
||||
cur = conn.cursor()
|
||||
cur.execute("""
|
||||
INSERT INTO service_order_items
|
||||
(service_order_id, inventory_id, part_number, name, quantity, unit_cost, unit_price, status, notes)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
(service_order_id, inventory_id, part_number, name, quantity, unit_cost, unit_price, status, notes,
|
||||
mechanic_id, observations)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING id
|
||||
""", (
|
||||
so_id, item_data.get('inventory_id'), item_data.get('part_number'),
|
||||
item_data.get('name'), item_data.get('quantity', 1),
|
||||
item_data.get('unit_cost'), item_data.get('unit_price'),
|
||||
item_data.get('status', 'pending'), item_data.get('notes'),
|
||||
item_data.get('mechanic_id'), item_data.get('observations'),
|
||||
))
|
||||
item_id = cur.fetchone()[0]
|
||||
conn.commit()
|
||||
@@ -371,7 +437,7 @@ def add_item(conn, so_id, item_data):
|
||||
|
||||
def update_item(conn, item_id, data):
|
||||
cur = conn.cursor()
|
||||
allowed = ['part_number', 'name', 'quantity', 'unit_cost', 'unit_price', 'status', 'notes']
|
||||
allowed = ['part_number', 'name', 'quantity', 'unit_cost', 'unit_price', 'status', 'notes', 'mechanic_id', 'observations']
|
||||
sets = []
|
||||
vals = []
|
||||
for field in allowed:
|
||||
@@ -453,12 +519,14 @@ def remove_labor(conn, labor_id):
|
||||
|
||||
def update_service_order(conn, so_id, data):
|
||||
"""Update general service order fields."""
|
||||
_normalize_delivery(data)
|
||||
cur = conn.cursor()
|
||||
allowed = ['customer_id', 'vehicle_id', 'branch_id', 'priority',
|
||||
'reception_notes', 'diagnosis_notes', 'repair_notes',
|
||||
'delivery_notes', 'estimated_cost', 'estimated_completion',
|
||||
'employee_id', 'mileage_in', 'mileage_out', 'fuel_level', 'final_cost',
|
||||
'delivery_method', 'courier_id', 'is_direct']
|
||||
'employee_id', 'mechanic_name', 'mileage_in', 'mileage_out', 'fuel_level', 'final_cost',
|
||||
'delivery_method', 'courier_id', 'is_direct', 'requires_invoice',
|
||||
'workshop_name', 'customer_address', 'customer_phone', 'vehicle_description']
|
||||
sets = []
|
||||
vals = []
|
||||
for field in allowed:
|
||||
@@ -491,15 +559,15 @@ def get_kanban_summary(conn, branch_id=None):
|
||||
GROUP BY status
|
||||
""", params)
|
||||
|
||||
summary = {status: 0 for status in VALID_TRANSITIONS if status != 'cancelled'}
|
||||
summary = {status: 0 for status in ORDER_STATUSES if status != 'cancelada'}
|
||||
for r in cur.fetchall():
|
||||
summary[r[0]] = r[1]
|
||||
|
||||
# Overdue orders (estimated_completion passed and not ready/delivered)
|
||||
# Overdue orders (estimated_completion passed and not delivered/invoiced)
|
||||
cur.execute(f"""
|
||||
SELECT count(*) FROM service_orders
|
||||
WHERE estimated_completion < NOW()
|
||||
AND status NOT IN ('ready', 'delivered', 'cancelled')
|
||||
AND status NOT IN ('entregado', 'facturada', 'cancelada')
|
||||
AND is_deleted = false
|
||||
{branch_filter}
|
||||
""", params)
|
||||
@@ -812,6 +880,152 @@ def convert_to_sale(conn, so_id, sale_data, employee_id=None):
|
||||
return {"sale_id": sale_id, "total": total, "items_count": len(sale_items)}
|
||||
|
||||
|
||||
def convert_to_remission(conn, so_id, sale_data, employee_id=None):
|
||||
"""Convert a service order into a counter remission note (pending payment).
|
||||
|
||||
sale_data keys:
|
||||
register_id: int (optional)
|
||||
notes: str (optional)
|
||||
|
||||
Returns dict with sale_id, total, items_count.
|
||||
"""
|
||||
cur = conn.cursor()
|
||||
so = get_service_order(conn, so_id)
|
||||
if not so:
|
||||
cur.close()
|
||||
raise ValueError("Service order not found")
|
||||
if so["status"] == "cancelled":
|
||||
cur.close()
|
||||
raise ValueError("Cannot convert a cancelled service order")
|
||||
if so.get("sale_id"):
|
||||
cur.close()
|
||||
raise ValueError("Service order already converted")
|
||||
|
||||
branch_id = so["branch_id"]
|
||||
customer_id = so["customer_id"]
|
||||
|
||||
# Build sale items from SO parts and labor
|
||||
sale_items = []
|
||||
for item in so.get("items", []):
|
||||
if item.get("status") == "cancelled":
|
||||
continue
|
||||
qty = int(item.get("quantity", 1))
|
||||
unit_price = float(item.get("unit_price") or 0)
|
||||
unit_cost = float(item.get("unit_cost") or 0)
|
||||
sale_items.append({
|
||||
"inventory_id": item.get("inventory_id"),
|
||||
"part_number": item.get("part_number") or "PART",
|
||||
"name": item.get("name") or "Refaccion",
|
||||
"quantity": qty,
|
||||
"unit_price": unit_price,
|
||||
"unit_cost": unit_cost,
|
||||
"tax_rate": 0.16,
|
||||
})
|
||||
|
||||
for labor in so.get("labor", []):
|
||||
if labor.get("status") == "cancelled":
|
||||
continue
|
||||
sale_items.append({
|
||||
"inventory_id": None,
|
||||
"part_number": "SERV",
|
||||
"name": labor.get("description") or "Mano de obra",
|
||||
"quantity": 1,
|
||||
"unit_price": float(labor.get("total_cost") or 0),
|
||||
"unit_cost": 0,
|
||||
"tax_rate": 0.16,
|
||||
})
|
||||
|
||||
if not sale_items:
|
||||
cur.close()
|
||||
raise ValueError("No items or labor to invoice")
|
||||
|
||||
subtotal = 0.0
|
||||
tax_total = 0.0
|
||||
for item in sale_items:
|
||||
item_subtotal = item["quantity"] * item["unit_price"]
|
||||
item_tax = item_subtotal * item["tax_rate"]
|
||||
item["subtotal"] = item_subtotal
|
||||
item["tax_amount"] = item_tax
|
||||
subtotal += item_subtotal
|
||||
tax_total += item_tax
|
||||
|
||||
total = subtotal + tax_total
|
||||
register_id = sale_data.get("register_id")
|
||||
notes = sale_data.get("notes") or f"Nota de remision desde orden {so['order_number']}"
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO sales
|
||||
(branch_id, customer_id, employee_id, register_id, sale_type,
|
||||
payment_method, subtotal, discount_total, tax_total, total,
|
||||
amount_paid, change_given, metodo_pago_sat, forma_pago_sat,
|
||||
status, notes)
|
||||
VALUES (%s, %s, %s, %s, 'counter_remission', 'remission', %s, %s, %s, %s, %s, %s, 'PPD', '99', 'pending_payment', %s)
|
||||
RETURNING id, created_at
|
||||
""",
|
||||
(
|
||||
branch_id,
|
||||
customer_id,
|
||||
employee_id,
|
||||
register_id,
|
||||
subtotal,
|
||||
0,
|
||||
tax_total,
|
||||
total,
|
||||
0,
|
||||
0,
|
||||
notes,
|
||||
),
|
||||
)
|
||||
sale_id, _created_at = cur.fetchone()
|
||||
|
||||
for item in sale_items:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO sale_items
|
||||
(sale_id, inventory_id, part_number, name, quantity,
|
||||
unit_price, unit_cost, tax_rate, tax_amount, subtotal)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
""",
|
||||
(
|
||||
sale_id,
|
||||
item["inventory_id"],
|
||||
item["part_number"],
|
||||
item["name"],
|
||||
item["quantity"],
|
||||
item["unit_price"],
|
||||
item["unit_cost"],
|
||||
item["tax_rate"],
|
||||
item["tax_amount"],
|
||||
item["subtotal"],
|
||||
),
|
||||
)
|
||||
|
||||
# Reserve inventory for parts
|
||||
for item in so.get("items", []):
|
||||
if item.get("status") == "cancelled":
|
||||
continue
|
||||
inventory_id = item.get("inventory_id")
|
||||
qty = int(item.get("quantity", 0))
|
||||
if inventory_id and qty > 0:
|
||||
inventory_engine.record_reservation(
|
||||
conn,
|
||||
inventory_id,
|
||||
branch_id,
|
||||
qty,
|
||||
sale_id=sale_id,
|
||||
cost_at_time=float(item.get("unit_cost") or 0),
|
||||
notes=f"Reserva nota de remision orden {so['order_number']}"
|
||||
)
|
||||
|
||||
# Link order to sale (the remission note)
|
||||
cur.execute("UPDATE service_orders SET sale_id = %s WHERE id = %s", (sale_id, so_id))
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
return {"sale_id": sale_id, "total": total, "items_count": len(sale_items)}
|
||||
|
||||
|
||||
def assign_mechanic(conn, so_id, employee_id):
|
||||
"""Assign a mechanic/technician to a service order."""
|
||||
cur = conn.cursor()
|
||||
|
||||
Reference in New Issue
Block a user