public function getMSTaskChartData($id)
    {
        $criteria=new CDbCriteria;        
        $criteria->order='details asc';
        $criteria->compare('LCASE(order_id)',strtolower($id),true);
        
        $milestones = Milestone::model()->findAll($criteria);
        $chartData = array();
//        $chartRow = array('Ms/Task','Details','Start','End');
//        $chartData[] = $chartRow;
        $chartRow = array();
        $dateformat = Yii::app()->controller->datetimemysqlformatDMY;
        foreach($milestones as $milestone)
        {
            $start_at = Yii::app()->controller->getMysqlFormattedDatetime($milestone->start_at, $dateformat, false);
            //$end_at = Yii::app()->controller->getMysqlFormattedDatetime($milestone->end_at, $dateformat, false);            
            $chartRow[] = $milestone->details;
            $info = $start_at;
            $chartRow[] = $info;//this info field.
            $chartRow[] = $start_at;
            $chartRow[] = $start_at;
            $chartData[] = $chartRow;
            $chartRow = array();
        }
        
        $criteria=new CDbCriteria;
        $criteria->with = array('task');
        $criteria->order='task.name asc';
        $criteria->compare('LCASE(t.order_id)',strtolower($id),true);
        $ordertasks = Ordertask::model()->findAll($criteria);
        foreach($ordertasks as $ordertask)
        {
            $completed = $ordertask->completed;
            $start_at = Yii::app()->controller->getMysqlFormattedDatetime($ordertask->start_at, $dateformat, false);
            $end_at = Yii::app()->controller->getMysqlFormattedDatetime($ordertask->end_at, $dateformat, false);
            if($start_at === $end_at)
            {
                $datetime = new DateTime($ordertask->start_at);
                $datetime->modify('+24 hour');
                $end_at = $datetime->format($dateformat);
            }
            $info = ' [ ' . $completed . '% ] '. $start_at . ' - ' . $end_at;
            $chartRow[] = $ordertask->task->name;
            $chartRow[] = $info;//this info field.
            $chartRow[] = $start_at;
            $chartRow[] = $end_at;
            $chartData[] = $chartRow;
            $chartRow = array();
        }
//        $datetime = new DateTime('today');       
//        $datestr = $datetime->format($this->datetimemysqlformatDMY);
//        $low = $datestr;
//        $datetime->modify('+2 day');
//        $datestr = $datetime->format($this->datetimemysqlformatDMY);
//        $high = $datestr;
//        ["testms1", 
//            0, 
//            Object { date="2014-08-21 00:00:00", timezone_type=3,
//            timezone="Singapore"}, Object { date="2014-08-21 00:00:00", timezone_type=3,
//            timezone="Singapore"}                
//        ]
//        $chartData = array();
//        $chartRow = array();
//        $chartRow[] = 'MS-1';
//        $chartRow[] = $low;
//        $chartRow[] = $low;
//        $chartData[] = $chartRow;
//        $chartRow = array();
//        $chartRow[] = 'MS-2';
//        $chartRow[] = $low;
//        $chartRow[] = $high;
//        $chartData[] = $chartRow;
        return $chartData;
    }
 public function saveToDB($model, $oldpayment)
 {
     $rtn = false;
     $trans = Yii::app()->db->beginTransaction();
     try 
     {                                    
         $orderid = $model->order_or_ot_id;
         $ordertaskid = 0;
         $curdirection = $model->direction;
         $rtn = true;
         if(strtolower($curdirection) === strtolower(Helper::CONST_Outwards))
         {
             $ordertask = Ordertask::model()->findByPk($orderid);
             $rtn = !empty($ordertask);
             if($rtn)
             {
                 $ordertaskid = $ordertask->id;
                 $orderid = $ordertask->order->id;
             }
         }
         if($rtn)
         {
             $model->order_id = $orderid;
             $model->ordertask_id = $ordertaskid;
             $rtn = $model->save();
             if($rtn)
             {
                 $accountid = $model->account_id;
                 $rtn = $this->updateAccountPaid($oldpayment, $model->amount, $accountid);
             }
         }
     }
     catch (Exception $e) 
     {
         Yii::log("Error occurred while saving/activating order. Rolling back... . Failure reason as reported in exception: " . $e->getMessage(), CLogger::LEVEL_ERROR, __METHOD__);
         $rtn = false;
     }
     if($rtn)
     {
         $trans->commit();
     }
     else
     {
         $trans->rollback();
         Yii::app()->user->setFlash('error', "Error occurred");
     }
     return $rtn;
 }
    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete( $id )
    {
        if ( Yii::app()->request->isPostRequest )
        {
// we only allow deletion via POST request
            $model = $this->loadModel( $id );
            if(empty($model->payments))
            {
                foreach($model->accountorders as $single)
                {
                    $orderid = $single->order_id;
                    $ordertaskid = $single->ordertask_id;
                    $ordertask = Ordertask::model()->findByPk($ordertaskid);
                    if(!empty($ordertask))
                    {
                        $ordertask->invstatus = Helper::CONST_INVSTS_UNINVOICED;
                        $rtn = $ordertask->save();
                    }
                    else
                    {
                        $order = Order::model()->findByPk($orderid);
                        if(!empty($order))
                        {
                            $order->invstatus = Helper::CONST_INVSTS_UNINVOICED;
                            $rtn = $order->save();
                        }
                    }
                    if($rtn)
                    {
                        $rtn = $single->delete();
                    }                    
                    if(!$rtn) break;
                }
                if($rtn)
                {
                   foreach($model->accountpurchases as $single)
                    {
                        $rtn = $single->delete();
                        if(!$rtn) break;
                    }
                }
                if($rtn)
                {
                    $rtn = $model->delete();
                }
                if($rtn)
                {
                    $message = 'Invoice Deleted';                    
                }
                else
                {
                    $message = 'Invoice delete Failed';
                }
            }
            else
            {
                $message = 'Delete all Payments first';
            }
            if (Yii::app()->request->isAjaxRequest)
            {
                if ( !isset( $_GET['ajax'] ) )
                {
                    $RetCode = 0;
                    $data = Yii::app()->createUrl('account/admin');
                    $this->returnJsonResponse($RetCode, $data, $message);
                }
            }
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if ( !isset( $_GET['ajax'] ) )
                $this->redirect( isset( $_POST['returnUrl'] ) ? $_POST['returnUrl'] : array( 'admin' ) );
        }
        else
            throw new CHttpException( 400, 'Invalid request. Please do not repeat this request again.' );
    }
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer the ID of the model to be loaded
*/
public function loadModel($id)
{
$model=Ordertask::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}