Changeset 1945 for trunk/LATMOS-Accounts/lib
- Timestamp:
- 02/14/17 18:32:09 (7 years ago)
- Location:
- trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Employment.pm
r1922 r1945 45 45 46 46 sub _has_extended_attributes { 1 } 47 48 sub _reported_atributes { qw(contratType endcircuit hosted company employer) }49 47 50 48 sub _get_attr_schema { … … 318 316 my $user = $base->get_object('user', $data{user}); 319 317 $user or return; 320 $class->SUPER::_create($base, $id, %data); 318 my $res = $class->SUPER::_create($base, $id, %data); 319 $user->applyCurrentEmployment or return; 320 $res; 321 321 } 322 322 … … 332 332 if ($res) { 333 333 my $ouser = $base->get_object('user', $user); 334 $ouser-> computeEmploymentDate;334 $ouser->applyCurrentEmployment or return; 335 335 } 336 336 337 337 $res 338 } 339 340 sub set_fields { 341 my ($self, %data) = @_; 342 343 my $res = $self->SUPER::set_fields(%data); 344 345 $self->applyToUser or return; 346 return $res; 338 347 } 339 348 … … 354 363 $user->base->log(LA_DEBUG, "Applying Employement %s to user %s", $self->id, $user->id); 355 364 356 $user->computeEmploymentDate;357 358 365 my $currentemployment = $user->get_attributes('currentEmployment') || ''; 359 366 360 367 if (!$currentemployment) { 361 return _resetUser($user);362 } elsif ($currentemployment ne$self->id) {368 return $user->_resetEmployment 369 } elsif ($currentemployment eq $self->id) { 363 370 # No sync to do if this employment is not currently applied 364 return; 371 return $user->applyCurrentEmployment; 372 } else { 373 return $user->computeEmploymentDate; 365 374 } 366 367 my %attrsets = (368 appliedEmployement => $self->id,369 );370 foreach my $attr (_reported_atributes(), qw(department managerContact)) {371 my $uval = $user->get_attributes($attr) || '';372 my $cval = $self->get_attributes($attr) || '';373 374 for ($attr) {375 if ($attr eq 'managerContact') {376 if (!$cval) {377 my $dpmt = $self->get_attributes('department') or last;378 my $odmpt = $self->base->get_object('group', $dpmt) or last;379 $cval = $odmpt->get_attributes('managedBy');380 }381 }382 }383 384 if ($uval ne $cval) {385 my $oattr = $self->base->attribute('user', $attr);386 $attrsets{$oattr->iname} = $cval;387 }388 }389 390 if (keys %attrsets) {391 if (my $res = $user->set_fields(%attrsets)) {392 $user->ReportChange('Update', 'Attr %s updated to match Employment %s', join(', ', sort keys %attrsets), $self->id);393 return $res;394 }395 } else {396 return 1;397 }398 }399 400 sub _resetUser {401 my ($ouser) = @_;402 403 $ouser->computeEmploymentDate;404 405 my %changes = (406 appliedEmployement => undef,407 );408 409 my @attributesToReset = _reported_atributes;410 if (!$ouser->_get_attributes('_startEmployment')) {411 push(@attributesToReset, qw(department));412 }413 414 foreach my $attr (@attributesToReset) {415 my $default = $ouser->base->config("unemployment.$attr") || '';416 my $old = $ouser->_get_attributes($attr) || '';417 if ($old ne $default) {418 $changes{$attr} = $default || undef;419 }420 }421 if ($ouser->set_fields(%changes)) {422 $ouser->base->log(LA_NOTICE, "Updating user %s to match unemployment", $ouser->id);423 $ouser->ReportChange('Update', 'Update %s to match unemployment', join(', ', sort keys %changes));424 return 1;425 }426 427 return 0;428 375 } 429 376 … … 474 421 } 475 422 476 sub ReportChange {477 my ($self, $changetype, $message, @args) = @_;478 479 $self->applyToUser();480 481 $self->SUPER::ReportChange($changetype, $message, @args);482 }483 484 423 1; 485 424 -
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm
r1941 r1945 1519 1519 } 1520 1520 1521 sub _reported_atributes { qw(contratType endcircuit hosted company employer) } 1522 1523 =head2 applyCurrentEmployment 1524 1525 Search the current employment is any and apply paramter to user 1526 1527 =cut 1528 1529 sub applyCurrentEmployment { 1530 my ($self) = @_; 1531 1532 my $currentemployment = $self->get_attributes('currentEmployment') || ''; 1533 1534 $self->base->log( 1535 LA_DEBUG, 1536 "Applying Employement %s to user %s", 1537 $currentemployment ? $currentemployment->id : '(none)', 1538 $self->id 1539 ); 1540 1541 if (!$currentemployment) { 1542 return $self->_resetEmployment; 1543 } 1544 1545 $self->computeEmploymentDate; 1546 1547 my %attrsets = ( 1548 appliedEmployement => $currentemployment->id, 1549 ); 1550 foreach my $attr (_reported_atributes(), qw(department managerContact)) { 1551 my $uval = $self->get_attributes($attr) || ''; 1552 my $cval = $currentemployment->get_attributes($attr) || ''; 1553 1554 for ($attr) { 1555 if ($attr eq 'managerContact') { 1556 if (!$cval) { 1557 my $dpmt = $currentemployment->get_attributes('department') or last; 1558 my $odmpt = $currentemployment->base->get_object('group', $dpmt) or last; 1559 $cval = $odmpt->get_attributes('managedBy'); 1560 } 1561 } 1562 } 1563 1564 if ($uval ne $cval) { 1565 my $oattr = $currentemployment->base->attribute('self', $attr); 1566 $attrsets{$oattr->iname} = $cval; 1567 } 1568 } 1569 1570 if (keys %attrsets) { 1571 if (my $res = $self->set_fields(%attrsets)) { 1572 $self->ReportChange('Update', 'Attr %s updated to match Employment %s', join(', ', sort keys %attrsets), $currentemployment->id); 1573 return $res; 1574 } 1575 } else { 1576 return 1; 1577 } 1578 } 1579 1580 sub _resetEmployment { 1581 my ($self) = @_; 1582 1583 $self->computeEmploymentDate; 1584 1585 my %changes = ( 1586 appliedEmployement => undef, 1587 ); 1588 1589 my @attributesToReset = _reported_atributes; 1590 if (!$self->_get_attributes('_startEmployment')) { 1591 push(@attributesToReset, qw(department)); 1592 } 1593 1594 foreach my $attr (@attributesToReset) { 1595 my $default = $self->base->config("unemployment.$attr") || ''; 1596 my $old = $self->_get_attributes($attr) || ''; 1597 if ($old ne $default) { 1598 $changes{$attr} = $default || undef; 1599 } 1600 } 1601 if ($self->set_fields(%changes)) { 1602 $self->base->log(LA_NOTICE, "Updating user %s to match unemployment", $self->id); 1603 $self->ReportChange('Update', 'Update %s to match unemployment', join(', ', sort keys %changes)); 1604 return 1; 1605 } 1606 1607 return 0; 1608 } 1609 1610 1521 1611 =head2 computeEmploymentDate 1522 1612
Note: See TracChangeset
for help on using the changeset viewer.