feat(workshop): vehicles must be assigned to a customer
- Added customer_id column to fleet_vehicles with FK to customers. - Enforced customer_id on create_vehicle endpoints (workshop and fleet). - New vehicle modal now requires a selected customer and stores customer_id. - Migration v4.13 applied to all tenants.
This commit is contained in:
@@ -413,25 +413,36 @@ def create_customer_for_workshop():
|
||||
@service_order_bp.route('/vehicles', methods=['POST'])
|
||||
@require_auth('workshop.edit')
|
||||
def create_vehicle_for_workshop():
|
||||
"""Create a fleet vehicle directly from the workshop flow."""
|
||||
"""Create a fleet vehicle directly from the workshop flow.
|
||||
|
||||
Vehicles must be assigned to a customer.
|
||||
"""
|
||||
data = request.get_json() or {}
|
||||
if not data.get('plate') and not data.get('vin'):
|
||||
return jsonify({'error': 'Se requiere placa o VIN'}), 400
|
||||
if not data.get('customer_id'):
|
||||
return jsonify({'error': 'El vehiculo debe estar asignado a un cliente'}), 400
|
||||
|
||||
conn = get_tenant_conn(g.tenant_id)
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute("SELECT name FROM customers WHERE id = %s", (data['customer_id'],))
|
||||
cust = cur.fetchone()
|
||||
if not cust:
|
||||
cur.close(); conn.close()
|
||||
return jsonify({'error': 'Cliente no encontrado'}), 404
|
||||
|
||||
cur.execute("""
|
||||
INSERT INTO fleet_vehicles
|
||||
(branch_id, plate, vin, make, model, year,
|
||||
(branch_id, customer_id, plate, vin, make, model, year,
|
||||
current_mileage, fuel_type, color, owner_name, notes)
|
||||
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
|
||||
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
|
||||
RETURNING id
|
||||
""", (
|
||||
data.get('branch_id', g.branch_id), data.get('plate'), data.get('vin'),
|
||||
data.get('branch_id', g.branch_id), data['customer_id'], data.get('plate'), data.get('vin'),
|
||||
data.get('make'), data.get('model'), data.get('year'),
|
||||
data.get('current_mileage', 0), data.get('fuel_type', 'gasolina'),
|
||||
data.get('color'), data.get('owner_name'), data.get('notes'),
|
||||
data.get('color'), cust[0], data.get('notes'),
|
||||
))
|
||||
vehicle_id = cur.fetchone()[0]
|
||||
conn.commit()
|
||||
|
||||
Reference in New Issue
Block a user