3

I have to save In and Out timing of customer and their In and Out location, I want to save all the data in one row.

I am getting status 1/0 1== In DATA, 0== Out DATA, but how to save it in the same row?

Here are both my In and Out functions:

public function ws_pushPunchInData($custid,$intime,$inlocation,$device_id,$status,$create_at)

{
    //Mage::log("intime: ".$intime.", outTime: ".$outtime.", create_at: ".$create_at, null, 'punchinout.log', true);
    $res = $data = array();
    $res['code'] = 400;
    $res['msg'] = "Invalid request";
    if ($custid)
    {
        $current = date('Y-m-d H:i:s');
        try
        {
            $data = array(
            'manager_id' => strval($custid),
            'punch_in_time' =>$intime,
            'in_coordinates' =>strval($inlocation),
            'device_id' => strval($device_id),
            'status' => strval($status),
            'create_at' => $current;     

            $smodel = Mage::getModel("manager/asmtracker")->setData($data)->save();

            $res['code'] = 200;
            $res['msg'] = 'Successfully updated';

        }catch(exception $exception)
        {
            $res['msg'] = 'Unable to process';
        }  
    }
    return $res;
}
 public function ws_pushPunchOutData($custid,$outtime,$outlocation,$device_id,$status,$updated_at)

{
    //Mage::log("intime: ".$intime.", outTime: ".$outtime.", create_at: ".$create_at, null, 'punchinout.log', true);
    $res = $data = array();
    $res['code'] = 400;
    $res['msg'] = "Invalid request";
    if ($custid)
    {
        $current = date('Y-m-d H:i:s');
        try
        {
            $data = array(
            'manager_id' => strval($custid),
            'punch_out_time' =>$outtime,
            'out_coordinates' =>$outlocation,
            'device_id' => strval($device_id),
            'status' => strval($status),
            'updated_at' => $current);     

            $smodel = Mage::getModel("manager/asmtracker")->setData($data)->save();

            $res['code'] = 200;
            $res['msg'] = 'Successfully updated';

        }catch(exception $exception)
        {
            $res['msg'] = 'Unable to process';
        }  
    }
    return $res;
}
  • create one array save in out data with key and convert it into json and save it – Gopal Patel Mar 21 '17 at 6:47
  • You mean in single record against each entry ? Your code is saving data twice for in and out ? – Arunendra Mar 21 '17 at 6:50
3

Save those data in array and convert that array to json and you can save that Json in database

<?php 
    $jason_data =  json_encode($array);
?>

after that Save that $jason_data in database

Edit part after @Arunendra suggestion

 public function ws_pushPunchOutData($custid,$outtime,$outlocation,$device_id,$status,$updated_at)

{
    //Mage::log("intime: ".$intime.", outTime: ".$outtime.", create_at: ".$create_at, null, 'punchinout.log', true);
    $res = $data = array();
    $res['code'] = 400;
    $res['msg'] = "Invalid request";
    if ($custid)
    {
        $current = date('Y-m-d H:i:s');
        try
        {

           $smodel =  Mage::getModel("manager/asmtracker")->load($custid,'manager_id');
            $data = array(
            //'manager_id' => strval($custid),
            'punch_out_time' =>$outtime,
            'out_coordinates' =>$outlocation,
            'device_id' => strval($device_id),
            'status' => strval($status),
            'updated_at' => $current);     
             $smodel->setData($data);  
             $smodel->setId($smodel->getId());
             $smodel->save();     
            //$smodel = Mage::getModel("manager/asmtracker")->setData($data)->save();

            $res['code'] = 200;
            $res['msg'] = 'Successfully updated';

        }catch(exception $exception)
        {
            $res['msg'] = 'Unable to process';
        }  
    }
    return $res;
}
  • yeah again same timing (y) – Gopal Patel Mar 21 '17 at 6:48
  • Yeah, Do you have same solution or different one ??? @GopalPatel – Murtuza Zabuawala Mar 21 '17 at 6:50
  • Exactly same solution. I gave solution in comment and you gave it into answer on same time – Gopal Patel Mar 21 '17 at 6:51
  • She asked for same row not column. I believe she want to update the record – Arunendra Mar 21 '17 at 6:52
  • @GopalPatel May be fraction seconds would be differ – Murtuza Zabuawala Mar 21 '17 at 6:53
2

i think you want to update the second time here is logic

public function ws_pushPunchOutData($custid,$outtime,$outlocation,$device_id,$status,$updated_at)

    {
        //Mage::log("intime: ".$intime.", outTime: ".$outtime.", create_at: ".$create_at, null, 'punchinout.log', true);
        $res = $data = array();
        $res['code'] = 400;
        $res['msg'] = "Invalid request";
        if ($custid)
        {
            $current = date('Y-m-d H:i:s');
            try
            {
                $data = array(
                'manager_id' => strval($custid),
                'punch_out_time' =>$outtime,
                'out_coordinates' =>$outlocation,
                'device_id' => strval($device_id),
                'status' => strval($status),
                'updated_at' => $current);     
    $smodelitem = Mage::getModel("manager/asmtracker")->getCollection()->addFieldToFilter('manager_id',$custid)->addFieldToFilter('device_id',$device_id)->getFristItem();
     if($smodelitem):
                $smodel = Mage::getModel("manager/asmtracker")->setId($smodelitem->getId())->setData($data)->save();
    endif;
                $res['code'] = 200;
                $res['msg'] = 'Successfully updated';

            }catch(exception $exception)
            {
                $res['msg'] = 'Unable to process';
            }  
        }
        return $res;
    }
1
  1. You will need unique record id when you store your first IN record. Udated code below

    public function ws_pushPunchInData($custid,$intime,$inlocation,$device_id,$status,$create_at)
    
    {
      //Mage::log("intime: ".$intime.", outTime: ".$outtime.", create_at: ".$create_at, null, 'punchinout.log', true);
      $res = $data = array();
      $res['code'] = 400;
      $res['msg'] = "Invalid request";
      if ($custid)
      {
          $current = date('Y-m-d H:i:s');
         try
        {
        $data = array(
        'manager_id' => strval($custid),
        'punch_in_time' =>$intime,
        'in_coordinates' =>strval($inlocation),
        'device_id' => strval($device_id),
        'status' => strval($status),
        'create_at' => $current;  
    
         //updated code
        $manager_model = Mage::getModel("manager/asmtracker");
        $manager_model->setData($data)->save();
        $last_inserted_id = manager_model->getId(); 
    
        $res['code'] = 200;
        //send this IN record unique auto generated id in response
        $res['In_Record_Id'] = $last_inserted_id;
        $res['msg'] = 'Successfully updated';
    
    }catch(exception $exception)
    {
        $res['msg'] = 'Unable to process';
        }  
        }
        return $res;
     }
    

Store "In_Record_Id" in your app from above response. When OUT request sent then pass this "In_Record_Id" id from your app to your server and you can get that id in your code using GET or POST. check below updated code

 //added one parameter here in function
public function ws_pushPunchOutData($custid,$outtime,$outlocation,$device_id,$status,$updated_at,$last_in_id)

    {
       //Mage::log("intime: ".$intime.", outTime: ".$outtime.", create_at: ".$create_at, null, 'punchinout.log', true);
$res = $data = array();
$res['code'] = 400;
$res['msg'] = "Invalid request";
if ($custid)
{
    $current = date('Y-m-d H:i:s');
    try
    {
        $data = array(
        'manager_id' => strval($custid),
        'punch_out_time' =>$outtime,
        'out_coordinates' =>$outlocation,
        'device_id' => strval($device_id),
        'status' => strval($status),
        'updated_at' => $current);     
        //assuming in your table unique key column name is Id

        //updated code
        $manager_model = Mage::getModel("manager/asmtracker")->setId($last_in_id);
        $smodel = $manager_model->setData($data)->save();

        $res['code'] = 200;
        $res['msg'] = 'Successfully updated';

    }catch(exception $exception)
    {
        $res['msg'] = 'Unable to process';
    }  
}
return $res;
   }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.