Forum

Why does WorkElemen...
 
Notifications
Clear all

Why does WorkElement save work for session user ?

8 Posts
2 Users
0 Reactions
3,703 Views
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Topic starter
 
[#7499]

Hello,

While developing my telegram bot plugin, I stumbled upon a problem which seemed really strange at first. I implemented a button to start/stop work on tickets through the bot. However, the WorkElement always saves the worktime to the session user (a user I created for the bot) instead of the user I set by manually changing its idUser value. I looked a bit further in the WorkElementMain.php and found this in the save function:
(lines 145 and 214 to 222, V9.1.2)

$user = getSessionUser();

...

// If realWork has changed (not through Dispatch screen), update the work
if ($diff != 0) {
  // Set work to Ticket
  $idx = - 1;
  // Will retrive work for current WorkElement, Current resource
  $crit = array (
      'idWorkElement' => $this->id,
      'idResource' => $user->id 
  );

This seems a bit strange to me. Why do you you use the session user instead of the user currently working on the ticket ? Is this intended or a bug ? If it is intended, do you know how I could manage what I want to do, that is starting and stopping work for another user ?

Thanks in advance for your reply


 
Posted : 17 Apr 2021 0H30
(@babynus)
Posts: 14952
Member Admin
 

No, it is normal if you concider Ticket screen.
There, you have visibility on WorkElement fields : estimated work, and real work.
If current user changes real work, it is automatically converted into real work (table work) and concidered it is some work now for current user.
Changing real work for another user, or another day requires to use Dispatch Work feature (see "saveDispatchWork.php)

 


 
Posted : 18 Apr 2021 22H23
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Topic starter
 

Good evening,

Thank you for your message.

I've looked at

saveDispatchWork.php

and it seems indeed to be what I need. I've tried to copy the important elements of the script to adapt it to my needs, but I still have some issues. I can succesfully create a new Work object and it updates the field in the "Dispatch" menu but I can't get it to update the total time on the ticket. I thought maybe

ProjectPlanningElement::updateSynthesis

was responsible for this but even after adding the line, it still didn't work. How is it supposed to update the total work time outside of the dispatch menu ?

Here are some screenshots to illustrate my problem:
Incorrect total work time displayed in the ticket
 

Correct work details in dispatch menu
 


 
Posted : 23 Apr 2021 23H49
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Topic starter
 

Here is what I've got to stop work and save the appropriate work time:

//$element is the element to which it should save work, for exemple a ticket
//$userId is the Resource/User id to which work should be attributed

$startTime = $element->WorkElement->ongoingStartDateTime;
$stopTime = date ( 'Y-m-d H:i:s' );
$workTime = workTimeDiffDateTime ( $startTime, $stopTime );

Sql::beginTransaction();
$saveDispatchMode=true;

$work = new Work();

$work->setDates(date("Y-m-d"));
$work->idResource=$userId;
$work->idProject=$element->idProject;

if ($element->idActivity) {
    $work->refType = "Activity";
    $work->refId=$element->idActivity;
} else {
    $work->refType = $class;
    $work->refId = $id;
}

$newWork=Work::convertImputation($workTime);
$diff=$newWork;
$work->work=$newWork;
$work->idWorkElement=$element->WorkElement->id;
$work->dailyCost=null; // set to null to force refresh 
$work->cost=null;

$ass=WorkElement::updateAssignment($work, $diff);
$work->idAssignment=($ass)?$ass->id:null;

$resWork = $work->save();

if ($resWork) {
    $status = getLastOperationStatus ( $resWork );
    if ($status=='OK') {
        error_log("OK");
        if ($ass) error_log($ass->saveWithRefresh());
    } else if ($status=='ERROR' or $status=='INVALID') {
        error_log("Error when stopping work: ".$resWork);
        break;
    }
}

ProjectPlanningElement::updateSynthesis('Project',$element->idProject);
Sql::commitTransaction();

$element->WorkElement->idUser = null;
$element->WorkElement->ongoing = 0;
$element->WorkElement->ongoingStartDateTime = null;

$element->WorkElement->save();

 
Posted : 23 Apr 2021 23H55
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Topic starter
 

I also looked at PlanningElement.php, Assignment.php, Work.php and projeqtorWork.js but I haven't found anything that would solve the problem. I suppose I must have forgotten a line or missed another part of the process but I don't know which.

Can you see what I'm doing wrong ? I would be very thankful because this has kept bothering for the last few days


 
Posted : 28 Apr 2021 17H55
(@babynus)
Posts: 14952
Member Admin
 

I don't get what's wrong now.
Please explain, what you now get, and what is still not working as expected.


 
Posted : 02 May 2021 13H49
Louis Heredero
(@baryhobal)
Posts: 49
Active Member
Topic starter
 

Ok, so, what I want to do is when I press the stop work button from my bot, it saves the worktime to the user who pressed the button and not the bot user. This means I can't just use the stop method from WorkElement.
I copied what Projeqtor does when you save imputations from the dispatch menu in hope it would work for me. Unfortunately, there is some strange behaviour.
Indeed, when stopping after some time, the amount displayed in the real work box is correct. However, if I open the dispatch menu, I can see a line for the user but with the wrong amount of work. I supsect the time is not converted properly and that time in work days is added to time in hours.
Perhaps it's because of WorkElement::updateAssignment. Does it need time in work days or in hours ?

I hope this explains a bit more my problem.


 
Posted : 02 May 2021 20H09
(@babynus)
Posts: 14952
Member Admin
 

This means I can't just use the stop method from WorkElement.

Exact !
 

I supsect the time is not converted properly and that time in work days is added to time in hours.
Perhaps it's because of WorkElement::updateAssignment. Does it need time in work days or in hours ?

Values stored in the DB must always be stored in days.
So possibly you need to add conversion. See Work::convertImputation()


 
Posted : 03 May 2021 14H52
Share:

Scroll to Top