var result;
var formText;
var parentId;
var query;
var parent = ctx;
var find_up_hierarhy = true;
var idConverter = session.getIdConverter();
do {
formText = parent.getString('Form');
parentId = 'f3'.equals(formText) ? parent.getString('OName_1Id') : parent.getString('HeadOfficeId');
if (!find_up_hierarhy || (('f1'.equals(formText) || '1'.equals(parent.getString('Leader'))
|| '1'.equals(parent.getString('IsWho'))) && !''.equals(parent.getString('EPost_PersonId')))) {
result = parent.getString('docId');
break;
} else if ('f0'.equals(formText) || 'f2'.equals(formText)) {
find_up_hierarhy = false;
var id = idConverter.unid2PlatformId(parent.getString('docId'));
var boss_tabnum = parent.getString('struct_boss_ac');
if (boss_tabnum != null && !''.equals(boss_tabnum)) {
query = 'SELECT post.id, app.created_date
FROM SO_PersonSys sp
JOIN so_appointment app ON app.person = sp.id
LEFT JOIN (SELECT * FROM so_appointmentplain UNION SELECT * FROM so_appointmenthead) app1 ON app1.id = app.id
LEFT JOIN so_post post ON post.id = app1.post
JOIN so_beard brd ON brd.id = app.beard
WHERE sp.tabnum = \'' + boss_tabnum + '\' AND app.isprimary = \'1\'
AND brd.isactive = \'1\' ORDER BY post.isleader desc';
var collection = session.getCollectionService().findCollectionByQuery(query);
if (collection != null && collection.size() > 0 && collection.get(0) != null) {
parentId = idConverter.buildUnid(collection.get(0).getId(), collection.get(0).getTimestamp('created_date'));
} else {
find_up_hierarhy = true;
}
} else {
find_up_hierarhy = true;
}
if (find_up_hierarhy) {
query = 'SELECT post.id, app.created_date
FROM so_structureunit str
LEFT JOIN so_beard brd ON brd.id = str.beard
LEFT JOIN so_beard brd_chld ON brd_chld.hierparent = brd.id
LEFT JOIN so_appointment app ON app.beard = brd_chld.id
LEFT JOIN (select * from so_appointmentplain UNION select * from so_appointmenthead) app1 ON app1.id = app.id
LEFT JOIN so_post post ON post.id = app1.post
WHERE (post.isleader = \'1\' OR brd_chld.orig_type = \'1\')
AND brd_chld.isActive = \'1\' AND brd_chld.isNotVacancy = \'1\'
AND str.id = ' + id.getId() + ' AND str.id_type = ' + id.getTypeId() + ' ORDER BY post.isleader desc';
var collection = session.getCollectionService().findCollectionByQuery(query);
if (collection != null && collection.size() > 0 && collection.get(0) != null) {
parentId = idConverter.buildUnid(collection.get(0).getId(), collection.get(0).getTimestamp('created_date'));
} else if ('f0'.equals(formText)) {
result = '';
}
} else {}
}
parent = session.findByStrId(parentId);
} while (result == null && parent != null) ctx.setResult(result)