129 lines
3.9 KiB
Python
129 lines
3.9 KiB
Python
# -*- coding: utf-8 -*-
|
||
import odoo
|
||
from odoo import api, SUPERUSER_ID
|
||
from odoo.modules.registry import Registry
|
||
|
||
db_name = 'dev'
|
||
registry = Registry.new(db_name)
|
||
cr = registry.cursor()
|
||
env = api.Environment(cr, SUPERUSER_ID, {})
|
||
|
||
print("--- REpopulating Demo Drivers and Vehicles ---")
|
||
|
||
drivers_data = [
|
||
{
|
||
'name': 'Ahmet Yılmaz',
|
||
'plate': '34-AHM-101',
|
||
'model_name': 'Renault Clio 1.5 dCi',
|
||
'policy': 'limit',
|
||
'planned_km': 15.2,
|
||
'driven_km': 15.4,
|
||
'fuel': 42.0,
|
||
'score': 95,
|
||
'lat_offset': 0.0
|
||
},
|
||
{
|
||
'name': 'Ayşe Kaya',
|
||
'plate': '34-AYS-202',
|
||
'model_name': 'Fiat Egea 1.6 Multijet',
|
||
'policy': 'forbidden',
|
||
'planned_km': 12.0,
|
||
'driven_km': 12.3,
|
||
'fuel': 38.0,
|
||
'score': 92,
|
||
'lat_offset': -0.05
|
||
},
|
||
{
|
||
'name': 'Mehmet Demir',
|
||
'plate': '34-MHM-303',
|
||
'model_name': 'Ford Focus 1.5 EcoBlue',
|
||
'policy': 'limit',
|
||
'planned_km': 14.0,
|
||
'driven_km': 17.5,
|
||
'fuel': 28.0,
|
||
'score': 68,
|
||
'lat_offset': 0.03
|
||
},
|
||
{
|
||
'name': 'Canan Öztürk',
|
||
'plate': '34-CNN-404',
|
||
'model_name': 'Volkswagen Golf 1.0 TSI',
|
||
'policy': 'forbidden',
|
||
'planned_km': 25.0,
|
||
'driven_km': 28.8,
|
||
'fuel': 19.0,
|
||
'score': 60,
|
||
'lat_offset': -0.10
|
||
},
|
||
{
|
||
'name': 'Burak Arslan',
|
||
'plate': '34-BRK-505',
|
||
'model_name': 'BMW 320i Sedan',
|
||
'policy': 'unlimited',
|
||
'planned_km': 10.0,
|
||
'driven_km': 14.0,
|
||
'fuel': 45.0,
|
||
'score': 88,
|
||
'lat_offset': 0.02
|
||
}
|
||
]
|
||
|
||
model_obj = env['fleet.vehicle.model'].search([], limit=1)
|
||
if not model_obj:
|
||
brand = env['fleet.vehicle.model.brand'].create({'name': 'Generic Fleet'})
|
||
model_obj = env['fleet.vehicle.model'].create({'name': 'Filo Modeli', 'brand_id': brand.id})
|
||
|
||
for item in drivers_data:
|
||
partner = env['res.partner'].search([('name', '=', item['name'])], limit=1)
|
||
if not partner:
|
||
partner = env['res.partner'].create({
|
||
'name': item['name'],
|
||
'email': f"{item['name'].lower().replace(' ', '.')}@mymach.com",
|
||
'phone': '0532 000 00 00'
|
||
})
|
||
|
||
vehicle = env['fleet.vehicle'].search([('license_plate', '=', item['plate'])], limit=1)
|
||
if not vehicle:
|
||
vehicle = env['fleet.vehicle'].create({
|
||
'model_id': model_obj.id,
|
||
'license_plate': item['plate'],
|
||
'driver_id': partner.id,
|
||
'current_fuel_level': item['fuel'],
|
||
'tank_capacity': 50.0,
|
||
'odometer': 15000.0 + (item['driven_km'] * 10)
|
||
})
|
||
else:
|
||
vehicle.write({
|
||
'driver_id': partner.id,
|
||
'current_fuel_level': item['fuel'],
|
||
'odometer': 15000.0 + (item['driven_km'] * 10)
|
||
})
|
||
|
||
env['mymach.fleet.contract'].create({
|
||
'name': f"Tahsis Sözleşmesi - {item['name']}",
|
||
'driver_id': partner.id,
|
||
'personal_use_policy': item['policy'],
|
||
'driver_score': item['score'],
|
||
'max_km_limit': 300.0,
|
||
'personal_km_used': 50.0
|
||
})
|
||
|
||
deviation = item['driven_km'] - item['planned_km']
|
||
has_free_use = item['policy'] in ('allowed', 'unlimited', 'rental')
|
||
is_violation = (deviation > 1.0) and (not has_free_use)
|
||
|
||
status = 'exception' if is_violation else 'project_match'
|
||
log_name = f"ROTA SAPMA İHLALİ: {item['name']} rotadan {deviation:.1f} KM sapmıştır!" if is_violation else f"NORMAL SÜRÜŞ: {item['name']} rotaya uygun seyrediyor."
|
||
|
||
env['mymach.fleet.security.log'].create({
|
||
'vehicle_id': vehicle.id,
|
||
'status': status,
|
||
'name': log_name,
|
||
'latitude': 41.0766 + item['lat_offset'],
|
||
'longitude': 29.0124 + (item['lat_offset'] * 0.5),
|
||
'is_resolved': not is_violation
|
||
})
|
||
|
||
env.cr.commit()
|
||
print("--- DEMO DATA RE-POPULATED SUCCESSFULLY! ---")
|