public function create()
 {
     $response = array('success' => false);
     if (Request::ajax()) {
         $postData = Input::all();
         $predictionId = $postData['predictionId'];
         $expenseValue = $postData['value'];
         $rules = array('predictionId' => array('required', 'not_in:0', 'exists:predictions,id'), 'value' => array('required', 'numeric', 'min:0'));
         $messages = array('predictionId.required' => 'Please select a category.', 'predictionId.not_in' => 'Please select a category.', 'predictionId.exists' => 'An error occured. Please try later.', 'value.required' => 'Please enter expense value.', 'value.numeric' => 'Expense must be a numeric value. Ex: 90, 3.42', 'value.min' => 'Value must be a positive number.');
         $validator = Validator::make($postData, $rules, $messages);
         if ($validator->fails()) {
             $messages = $validator->messages();
             $response['predictionMsg'] = $messages->first('predictionId');
             $response['valueMsg'] = $messages->first('value');
             return Response::json($response);
         }
         $expense = new Expense();
         $expense->prediction_id = $predictionId;
         $expense->value = $expenseValue;
         $expense->save();
         $response['expenseId'] = $expense->id;
         $response['success'] = true;
         Event::fire('expense.create.success', array($expense));
     }
     return Response::json($response);
 }
예제 #2
0
 /**
  * Store a newly created expense in storage.
  *
  * @return Response
  */
 public function store()
 {
     $validator = Validator::make($data = Input::all(), Expense::$rules, Expense::$messages);
     if ($validator->fails()) {
         return Redirect::back()->withErrors($validator)->withInput();
     }
     $expense = new Expense();
     $expense->name = Input::get('name');
     $expense->type = Input::get('type');
     $expense->account_id = Input::get('account');
     $expense->save();
     return Redirect::route('expenses.index')->withFlashMessage('Expense successfully created!');
 }
 public function store()
 {
     try {
         $expense = new \Expense();
         $expense->urbanism_id = Input::get('urbanism_id');
         $expense->sub_account_id = Input::get('sub_account_id');
         $expense->amount = Input::get('amount');
         $expense->coments = Input::get('coments');
         $expense->created_by = Auth::user()->id;
         $files = Input::file('file');
         if ($expense->save()) {
             $new_expense = Expense::orderBy('created_at', 'desc')->first();
             if ($files) {
                 foreach ($files as $file) {
                     if ($file) {
                         strip_tags($file);
                         $extension = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
                         $rules = ['file' => 'required|file|max:5120'];
                         $messages = ['file' => '¡Atención! Solo se permiten archivos de tipo: jpg,gif,jpeg,pdf,xml,png,doc,docx,xls y xlsx.', 'max' => '¡Atención! El tamaño máximo permitido para los archivos es de 5MB'];
                         $validator = \Validator::make(['file' => $extension], $rules, $messages);
                         if ($validator->fails()) {
                             $new_expense->delete();
                             return Redirect::back()->withInput()->withErrors($validator);
                             //var_dump($files);
                         } else {
                             //print_r($files);
                             $public_filename = $file->getClientOriginalName();
                             $filename = uniqid() . '_' . str_random(6) . '_' . $file->getClientOriginalName();
                             $destinationPath = public_path() . '/uploads/files/expenses/';
                             $uploadSuccess = $file->move($destinationPath, $filename);
                             $new_data = ['expense_id' => $new_expense->id, 'public_filename' => $public_filename, 'filename' => $filename];
                             $expenses_files = ExpenseFile::create($new_data);
                         }
                     }
                 }
             }
             return Redirect::route('report.expenses')->with('error', false)->with('msg', 'Gasto reportado con éxito')->with('class', 'info');
         }
     } catch (Exception $exc) {
         echo $exc->getMessage() . " " . $exc->getLine();
         return Redirect::back()->with('error', true)->with('msg', '¡Algo salió mal! Contacte con administrador')->with('class', 'danger');
     }
 }
예제 #4
0
 public function actionKindCreate()
 {
     $model = new Expense();
     $dates = date('Y-m-d H:i:s');
     if (isset($_POST['product'])) {
         $transaction = Yii::app()->db->beginTransaction();
         try {
             $messageType = 'warning';
             $message = "There are some errors ";
             $model->order_date = $dates;
             $model->employee_id = 1;
             $model->table = 0;
             $model->status = 0;
             $model->kind = 1;
             //$uploadFile=CUploadedFile::getInstance($model,'filename');
             if ($model->save()) {
                 $messageType = 'success';
                 $message = "<strong>Well done!</strong> You successfully create data ";
                 foreach ($_POST['product']['id'] as $key => $value) {
                     $newModel = new Orders();
                     $newModel->expense_id = $model->expense_id;
                     $newModel->just_id = $value;
                     $newModel->type = 3;
                     $newModel->count = $_POST['product']['count'][$key];
                     $newModel->save();
                 }
                 $transaction->commit();
                 Yii::app()->user->setFlash($messageType, $message);
                 //$this->redirect(array('create'));
             }
         } catch (Exception $e) {
             $transaction->rollBack();
             Yii::app()->user->setFlash('error', "{$e->getMessage()}");
             //$this->refresh();
         }
     }
     $this->render('kindCreate', array('model' => $model));
 }
예제 #5
0
 public function actionCreate()
 {
     $menuModel = Dishtype::model()->findAll('t.parent = :parent', array(':parent' => 0));
     $model = new Expense();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Expense'])) {
         $_POST['Expense']['order_date'] = date('Y-m-d H:i:s');
         if ($_POST['Expense']['debt'] == 0) {
             $_POST['Expense']['status'] = 0;
         } else {
             $_POST['Expense']['status'] = 1;
         }
         if ($_POST['Expense']['comment'] == '') {
             $_POST['Expense']['comment'] = '';
         }
         echo "<pre>";
         print_r($_POST);
         echo "</pre>";
         $transaction = Yii::app()->db->beginTransaction();
         try {
             $archive = new ArchiveOrder();
             $archive_message = '';
             $messageType = 'warning';
             $message = "There are some errors ";
             $model->attributes = $_POST['Expense'];
             if ($model->save()) {
                 $messageType = 'success';
                 $message = "<strong>Well done!</strong> You successfully create data ";
                 if (isset($_POST['dish'])) {
                     $archive_message .= '*dish=>';
                     foreach ($_POST['dish']['id'] as $key => $val) {
                         $prodModel = new Orders();
                         $prodModel->expense_id = $model->expense_id;
                         $prodModel->just_id = $val;
                         $prodModel->type = 1;
                         $prodModel->count = $this->changeToFloat($_POST['dish']['count'][$key]);
                         $prodModel->save();
                         $archive_message .= $val . ":" . $this->changeToFloat($_POST['dish']['count'][$key]) . ",";
                     }
                 }
                 if (isset($_POST['stuff'])) {
                     $archive_message .= '*stuff=>';
                     foreach ($_POST['stuff']['id'] as $key => $val) {
                         $prodModel = new Orders();
                         $prodModel->expense_id = $model->expense_id;
                         $prodModel->just_id = $val;
                         $prodModel->type = 2;
                         $prodModel->count = $this->changeToFloat($_POST['stuff']['count'][$key]);
                         $prodModel->save();
                         $archive_message .= $val . ":" . $this->changeToFloat($_POST['stuff']['count'][$key]) . ",";
                     }
                 }
                 if (isset($_POST['product'])) {
                     $archive_message .= '*prod=>';
                     foreach ($_POST['product']['id'] as $key => $val) {
                         $prodModel = new Orders();
                         $prodModel->expense_id = $model->expense_id;
                         $prodModel->just_id = $val;
                         $prodModel->type = 3;
                         $prodModel->count = $this->changeToFloat($_POST['product']['count'][$key]);
                         $prodModel->save();
                         $archive_message .= $val . ":" . $this->changeToFloat($_POST['product']['count'][$key]) . ",";
                     }
                 }
                 $archive->setArchive('create', $model->expense_id, $archive_message);
                 $transaction->commit();
                 Yii::app()->user->setFlash($messageType, $message);
                 //$this->redirect(array('view','id'=>$model->expense_id));
             }
         } catch (Exception $e) {
             $transaction->rollBack();
             Yii::app()->user->setFlash('error', "{$e->getMessage()}");
             //$this->refresh();
         }
     }
     $this->render('create', array('model' => $model, 'menuModel' => $menuModel));
 }
예제 #6
0
    $prop_id = $_POST['prop'];
    $expense = $_POST['expense'];
    $start = $_POST['start'];
    $end = $_POST['end'];
    $amount = $_POST['amount'];
    if (empty($prop_id) || empty($expense) || empty($start) || empty($end) || empty($amount)) {
        $err = "Form fields marked with an asterix are required";
    } else {
        $exp = new Expense();
        $exp->setPropertyId($prop_id);
        $exp->setName($expense);
        $exp->setStartPeriod($start);
        $exp->setEndPeriod($end);
        $exp->setDatePaid();
        $exp->setPaymentAmount($amount);
        if ($exp->save()) {
            Logger::getInstance()->logAction("EXPENSE", $amount, $expense);
            /*$log = Logger::getInstance();
            		$log->logAction("EXPENSE", $amount, "Payment to security firm");*/
            //echo var_dump($log);
            $mesg = "Expense recorded";
            $session->message($mesg);
            redirect_to("expenses.php");
        } else {
            $err = "An error occured preventing the expense from being recorded";
        }
    }
}
include_layout_template('admin_header.php');
?>
예제 #7
0
        $expense = array_pop($expenses);
        $expense->delete();
    }
    for ($ii = 0; $ii < $numExpenseEntries; $ii++) {
        if ($ii <= $numExpenseUpdates - 1) {
            // update existing expense entry if it has changed
            $expense = $expenses[$ii];
            if ($expenseEntries[$ii]["desc"] !== $expense->description || intval($expenseEntries[$ii]["typeID"]) !== intval($expense->typeID) || floatval($expenseEntries[$ii]["amount"]) !== floatval($expense->amount)) {
                $expense->description = $expenseEntries[$ii]["desc"];
                $expense->amount = Expense::formatAmount($expenseEntries[$ii]["amount"]);
                $expense->typeID = $expenseEntries[$ii]["typeID"];
                $expense->save(array("amount", "description", "typeID"));
            }
        } else {
            // insert new expense entry
            $expense = new Expense();
            $expense->userID = $user->id;
            $expense->amount = Expense::formatAmount($expenseEntries[$ii]["amount"]);
            $expense->date = $mySqlFormatedDate;
            $expense->description = $expenseEntries[$ii]["desc"];
            $expense->typeID = $expenseEntries[$ii]["typeID"];
            $expense->save();
        }
    }
    // redirect back to the same day after processing the form
    header("Location: " . BASE_URL . "main/?month={$selectedMonth}&day={$selectedDay}&year={$selectedYear}");
    exit;
}
// Update $incomes and $expenses so that they contain the modified entries
$incomes = Income::findBySql('SELECT * FROM incomes WHERE userID = "' . $user->id . '" AND date = "' . $mySqlFormatedDate . '" ORDER BY id');
$expenses = Expense::findBySql('SELECT * FROM expenses WHERE userID = "' . $user->id . '" AND date = "' . $mySqlFormatedDate . '"ORDER BY id');
예제 #8
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'show' page.
  */
 public function actionCreate()
 {
     $model = new Expense($this->action->id);
     if (isset($_POST['Expense'])) {
         // collect user input data
         $model->attributes = $_POST['Expense'];
         if (!isset($_POST['Expense']['companyId'])) {
             // set company based on the project
             if ($model->projectId >= 1) {
                 $criteria = new CDbCriteria();
                 $criteria->order = "`t`.`companyPriority` ASC, `t`.`id` ASC";
                 if (($company2Project = Company2Project::model()->findByAttributes(array('projectId' => $model->projectId), $criteria)) !== null) {
                     $model->companyId = $company2Project->companyId;
                 } else {
                     $model->companyId = 0;
                 }
             } else {
                 $model->companyId = 0;
             }
         }
         if (!isset($_POST['Expense']['managerId'])) {
             // current user is considered to be manager
             $model->managerId = Yii::app()->user->id;
         }
         // validate with the current action as scenario and save without validation
         if (($validated = $model->validate()) !== false && ($saved = $model->save(false)) !== false) {
             // set success message
             MUserFlash::setTopSuccess(Yii::t('hint', 'The new expense record number "{expenseNumber}" has been successfully created.', array('{expenseNumber}' => MHtml::wrapInTag($model->id, 'strong'))));
             // go to the 'show' page
             $this->redirect(array('show', 'id' => $model->id));
         }
     } else {
         // pre-assigned attributes (default values for a new record)
         $model->billToCompany = Expense::BILL_TO_COMPANY;
         // current user is considered to be manager
         $model->managerId = Yii::app()->user->id;
         if (isset($_GET['projectId'])) {
             // project is known
             $model->projectId = $_GET['projectId'];
         }
     }
     $this->render($this->action->id, array('model' => $model));
 }
예제 #9
0
 /**
  * Migrate from old project management system.
  */
 public function actionIndex()
 {
     // check rights
     if (!Yii::app()->user->checkAccess(User::ADMINISTRATOR)) {
         throw new CHttpException(403, Yii::t('Yii', 'You are not authorized to perform this action.'));
     }
     // models to migrate
     $migrate = array('User' => false, 'Company' => false, 'CompanyPayment' => false, 'Project' => false, 'Task' => false, 'Time' => true, 'Invoice' => false, 'Expense' => false);
     // start
     $message = '';
     // we won't migrate unless form is submitted
     if (Yii::app()->request->isPostRequest) {
         // default criteria
         $findAllCriteria = new CDbCriteria();
         $findAllCriteria->order = "`id` ASC";
         if ($migrate['User']) {
             // user
             $mUsers = MUser::model()->findAll($findAllCriteria);
             if (is_array($mUsers)) {
                 $i = $j = $c = 0;
                 $accessType = array('customer' => 'client', 'consultant' => 'consultant', 'manager' => 'manager', 'admin' => 'administrator');
                 $accessLevel = array('customer' => 2, 'consultant' => 3, 'manager' => 4, 'admin' => 5);
                 foreach ($mUsers as $oldModel) {
                     if (($model = User::model()->findByPk($oldModel->id)) !== null) {
                         $model->delete();
                     }
                     // old model validation
                     if (User::model()->findByAttributes(array('email' => $oldModel->email))) {
                         $oldModel->email = rand(10, 99) . $oldModel->email;
                     }
                     $closeTime = strtotime($oldModel->close_date);
                     $isActive = empty($oldModel->close_date) || $oldModel->close_date === '0000-00-00' || $closeTime === false;
                     // new model
                     $model = new User('migrate');
                     $model->username = $oldModel->email;
                     $model->password = md5($oldModel->password);
                     $model->email = $oldModel->email;
                     $model->screenName = $oldModel->name;
                     $model->accessType = isset($accessType[$oldModel->class]) ? $accessType[$oldModel->class] : 'member';
                     $model->accessLevel = isset($accessLevel[$oldModel->class]) ? $accessLevel[$oldModel->class] : 1;
                     $model->isActive = $isActive ? '1' : '0';
                     $model->createTime = strtotime($oldModel->last);
                     $model->id = $oldModel->id;
                     if ($model->save()) {
                         $i++;
                         if (($userDetails = UserDetails::model()->findByPk($model->id)) !== null) {
                             $userDetails->delete();
                         }
                         $userDetails = new UserDetails('migrate');
                         $userDetails->emailConfirmationKey = md5(uniqid(rand(), true));
                         $userDetails->initials = $oldModel->inits;
                         $userDetails->occupation = $oldModel->title;
                         $userDetails->deactivationTime = $isActive ? null : $closeTime;
                         $userDetails->administratorNote = '[from migration]';
                         $userDetails->userId = $model->id;
                         $userDetails->save();
                         // relation between user and company
                         if ($oldModel->customer_id >= 1) {
                             $user2Company = new User2Company('migrate');
                             $user2Company->userId = $model->id;
                             $user2Company->companyId = $oldModel->customer_id;
                             $user2Company->position = 'owner';
                             //$oldModel->title
                             if ($user2Company->save()) {
                                 $c++;
                             }
                         }
                     }
                     $j++;
                 }
                 $message .= $i . ' of ' . $j . ' users' . ($i === $c ? '' : ' with ' . $c . ' company (relations)') . ' have been migrated.<br/>';
             }
         }
         if ($migrate['Company']) {
             // company
             $mCustomers = MCustomer::model()->findAll($findAllCriteria);
             if (is_array($mCustomers)) {
                 $i = $j = $l = 0;
                 foreach ($mCustomers as $oldModel) {
                     if (($model = Company::model()->findByPk($oldModel->id)) !== null) {
                         $model->delete();
                     }
                     $closeTime = strtotime($oldModel->close_date);
                     $isActive = empty($oldModel->close_date) || $oldModel->close_date === '0000-00-00' || $closeTime === false;
                     $model = new Company('migrate');
                     $model->title = $oldModel->name;
                     $model->titleAbbr = $oldModel->inits;
                     $model->contactName = $oldModel->contact;
                     $model->contactEmail = $oldModel->contact_email;
                     $model->content = '[from migration]';
                     $model->contentMarkup = 'text';
                     $model->invoiceDueDay = $oldModel->terms_days;
                     $model->isActive = $isActive ? '1' : '0';
                     $model->deactivationTime = $isActive ? null : $closeTime;
                     $model->createTime = strtotime($oldModel->last);
                     $model->id = $oldModel->id;
                     if ($model->save()) {
                         $i++;
                         // associated location
                         $location = new Location('migrate');
                         $location->address1 = $oldModel->addr;
                         $location->address2 = $oldModel->addr2;
                         $location->city = $oldModel->city;
                         $location->state = $oldModel->state;
                         $location->zipcode = $oldModel->zip;
                         $location->content = '[from migration]';
                         $location->contentMarkup = 'text';
                         $location->createTime = strtotime($oldModel->last);
                         if ($location->save()) {
                             // relation between company and location
                             $location2Record = new Location2Record('migrate');
                             $location2Record->locationId = $location->id;
                             $location2Record->record = get_class($model);
                             $location2Record->recordId = $model->id;
                             if ($location2Record->save()) {
                                 $l++;
                             }
                         }
                     }
                     $j++;
                 }
                 $message .= $i . ' of ' . $j . ' companies' . ($i === $l ? '' : ' with ' . $l . ' locations') . ' have been migrated.<br/>';
             }
         }
         if ($migrate['CompanyPayment']) {
             // company payment
             $mCustomerPayments = MCustomerPayment::model()->findAll($findAllCriteria);
             if (is_array($mCustomerPayments)) {
                 $i = $j = 0;
                 $paymentMethod = array('cash' => 'cash', 'check' => 'check', 'credit card' => 'creditCard');
                 foreach ($mCustomerPayments as $oldModel) {
                     if (($model = CompanyPayment::model()->findByPk($oldModel->id)) !== null) {
                         $model->delete();
                     }
                     $model = new CompanyPayment('migrate');
                     $model->companyId = $oldModel->id;
                     $model->paymentDate = MDate::formatToDb($oldModel->payment_date, 'date');
                     $model->amount = $oldModel->amount;
                     $model->paymentMethod = $paymentMethod[$oldModel->payment_method];
                     $model->paymentNumber = $oldModel->payment_number;
                     $model->content = $oldModel->note . "\n" . '[from migration]';
                     $model->contentMarkup = 'text';
                     $model->id = $oldModel->id;
                     if ($model->save()) {
                         $i++;
                     }
                     $j++;
                 }
                 $message .= $i . ' of ' . $j . ' company payments have been migrated.<br/>';
             }
         }
         if ($migrate['Project']) {
             // project
             $mProjects = MProject::model()->findAll($findAllCriteria);
             if (is_array($mProjects)) {
                 $i = $j = $c = $u = 0;
                 foreach ($mProjects as $oldModel) {
                     if (($model = Project::model()->findByPk($oldModel->id)) !== null) {
                         $model->delete();
                     }
                     $openDateNotSet = empty($oldModel->open_date) || $oldModel->open_date === '0000-00-00' || strtotime($oldModel->open_date) === false;
                     $closeDateNotSet = empty($oldModel->close_date) || $oldModel->close_date === '0000-00-00' || strtotime($oldModel->close_date) === false;
                     $model = new Project('migrate');
                     $model->title = $oldModel->name;
                     $model->content = $oldModel->description . "\n" . '[from migration]';
                     $model->contentMarkup = 'text';
                     $model->hourlyRate = $oldModel->rate;
                     $model->openDate = $openDateNotSet ? null : MDate::formatToDb($oldModel->open_date, 'date');
                     $model->closeDate = $closeDateNotSet ? null : MDate::formatToDb($oldModel->close_date, 'date');
                     $model->createTime = strtotime($oldModel->last);
                     $model->id = $oldModel->id;
                     if ($model->save()) {
                         $i++;
                         // relation between project and company
                         if ($oldModel->customer_id >= 1) {
                             $company2Project = new Company2Project('migrate');
                             $company2Project->companyId = $oldModel->customer_id;
                             $company2Project->projectId = $model->id;
                             if ($company2Project->save()) {
                                 $c++;
                             }
                         }
                         // relation between project and manager
                         if ($oldModel->manager_id >= 1) {
                             $user2Project = new User2Project('migrate');
                             $user2Project->userId = $oldModel->manager_id;
                             $user2Project->projectId = $model->id;
                             $user2Project->role = 'manager';
                             if ($user2Project->save()) {
                                 $u++;
                             }
                         }
                     }
                     $j++;
                 }
                 $message .= $i . ' of ' . $j . ' projects' . ($i === $c ? '' : ' with ' . $c . ' company (relations)') . ($i === $u ? '' : ' with ' . $u . ' manager (relations)') . ' have been migrated.<br/>';
             }
         }
         if ($migrate['Task']) {
             // task
             $mTasks = MTask::model()->findAll($findAllCriteria);
             if (is_array($mTasks)) {
                 $i = $j = $u = $m = 0;
                 $priority = array('A' => 2, 'B' => 3, 'C' => 4, '' => 3);
                 $status = array('' => 'completed', 'Open' => 'completed', '0' => 'notStarted', 'New' => 'notStarted', 'Done' => 'completed', 'In Progress' => 'inProgress', 'Ready to Test' => 'readyToTest');
                 foreach ($mTasks as $oldModel) {
                     if (($model = Task::model()->findByPk($oldModel->id)) !== null) {
                         $model->delete();
                     }
                     $hourlyRate = null;
                     if ($oldModel->project_id >= 1 && ($project = Project::model()->findByPk($oldModel->project_id)) !== null) {
                         $hourlyRate = $project->hourlyRate;
                     }
                     $dueDateNotSet = empty($oldModel->due_date) || $oldModel->due_date === '0000-00-00' || strtotime($oldModel->due_date) === false;
                     $openDateNotSet = empty($oldModel->open_date) || $oldModel->open_date === '0000-00-00' || strtotime($oldModel->open_date) === false;
                     $closeDateNotSet = empty($oldModel->close_date) || $oldModel->close_date === '0000-00-00' || strtotime($oldModel->close_date) === false;
                     $model = new Task('migrate');
                     $model->title = $oldModel->name;
                     $model->content = $oldModel->description . "\n" . '[from migration]';
                     $model->contentMarkup = 'text';
                     $model->companyId = $oldModel->customer_id;
                     $model->projectId = $oldModel->project_id;
                     $model->estimateMinute = (int) $oldModel->hours_estimate * 60;
                     $model->dueDate = $dueDateNotSet ? null : MDate::formatToDb($oldModel->due_date, 'date');
                     $model->priority = $priority[$oldModel->priority];
                     $model->openDate = $openDateNotSet ? null : MDate::formatToDb($oldModel->open_date, 'date');
                     $model->closeDate = $closeDateNotSet ? null : MDate::formatToDb($oldModel->close_date, 'date');
                     $model->status = $status[$oldModel->task_status];
                     $model->report = $oldModel->work_report;
                     $model->reportMarkup = 'text';
                     $model->hourlyRate = $hourlyRate;
                     $model->isConfirmed = '1';
                     $model->confirmationTime = strtotime($oldModel->last);
                     $model->createTime = strtotime($oldModel->last);
                     $model->id = $oldModel->id;
                     if ($model->save()) {
                         $i++;
                         // relation between task and consultant
                         if ($oldModel->leader_id >= 1) {
                             $user2Task = new User2Task('migrate');
                             $user2Task->userId = $oldModel->leader_id;
                             $user2Task->taskId = $model->id;
                             $user2Task->role = User2Task::CONSULTANT;
                             if ($user2Task->save()) {
                                 $u++;
                             }
                         }
                         // relation between task and manager
                         if ($model->projectId >= 1) {
                             $criteria = new CDbCriteria();
                             $criteria->order = "`" . User2Project::model()->tableName() . "`.`userPriority` ASC";
                             $criteria->order .= ",`" . User2Project::model()->tableName() . "`.`id` ASC";
                             if (($user2Project = User2Project::model()->findByAttributes(array('projectId' => $model->projectId, 'role' => 'manager'), $criteria)) !== null) {
                                 $user2Task = new User2Task('migrate');
                                 $user2Task->userId = $user2Project->userId;
                                 $user2Task->taskId = $model->id;
                                 $user2Task->role = 'manager';
                                 if ($user2Task->save()) {
                                     $m++;
                                 }
                             }
                         }
                     }
                     $j++;
                 }
                 $message .= $i . ' of ' . $j . ' tasks' . ($i === $u ? '' : ' with ' . $u . ' consultant (relations)') . ($i === $m ? '' : ' with ' . $m . ' manager (relations)') . ' have been migrated.<br/>';
             }
         }
         if ($migrate['Time']) {
             // time
             $mTime = MTime::model()->findAll($findAllCriteria);
             if (is_array($mTime)) {
                 $i = $j = $t = 0;
                 foreach ($mTime as $oldModel) {
                     if (($model = Time::model()->findByPk($oldModel->id)) !== null) {
                         $model->delete();
                     }
                     $taskId = $oldModel->task_id;
                     if (empty($taskId) && $oldModel->project_id >= 1) {
                         $criteria = new CDbCriteria();
                         $criteria->order = "`" . Task::model()->tableName() . "`.`id` ASC";
                         if (($task = Task::model()->findByAttributes(array('projectId' => $oldModel->project_id), $criteria)) !== null) {
                             $taskId = $task->id;
                         } else {
                             // auto-generate a task
                             $companyId = 0;
                             $criteria = new CDbCriteria();
                             $criteria->order = "`" . Company2Project::model()->tableName() . "`.`companyPriority` ASC";
                             $criteria->order .= ", `" . Company2Project::model()->tableName() . "`.`id` ASC";
                             if (($company2Project = Company2Project::model()->findByAttributes(array('projectId' => $oldModel->project_id), $criteria)) !== null) {
                                 $companyId = $company2Project->companyId;
                             }
                             $hourlyRate = null;
                             if (($project = Project::model()->findByPk($oldModel->project_id)) !== null) {
                                 $hourlyRate = $project->hourlyRate;
                             }
                             $task = new Task('migrate');
                             $task->projectId = $oldModel->project_id;
                             $task->companyId = $companyId;
                             $task->title = '[Auto Generated]';
                             $task->status = 'completed';
                             $task->dueDate = MDate::formatToDb($project === null ? 1234567890 : $project->createTime, 'date');
                             $task->openDate = MDate::formatToDb($project === null ? 1234567890 : $project->createTime, 'date');
                             $task->closeDate = MDate::formatToDb($project === null ? 1234567890 : $project->createTime, 'date');
                             $task->hourlyRate = $hourlyRate;
                             $task->isConfirmed = 1;
                             $task->confirmationTime = $project === null ? 1234567890 : $project->createTime;
                             if ($task->save()) {
                                 $t++;
                                 $taskId = $task->id;
                                 // assigned consultant
                                 if ($oldModel->user_id >= 1) {
                                     $consultant2Task = new User2Task('migrate');
                                     $consultant2Task->userId = $oldModel->user_id;
                                     $consultant2Task->taskId = $task->id;
                                     $consultant2Task->role = User2Task::CONSULTANT;
                                     $consultant2Task->save();
                                 }
                                 // assigned manager
                                 $criteria = new CDbCriteria();
                                 $criteria->order = "`" . User2Project::model()->tableName() . "`.`userPriority` ASC";
                                 $criteria->order .= ", `" . User2Project::model()->tableName() . "`.`id` ASC";
                                 if (($user2Project = User2Project::model()->findByAttributes(array('projectId' => $oldModel->project_id, 'role' => 'manager'), $criteria)) !== null) {
                                     $manager2Task = new User2Task('migrate');
                                     $manager2Task->userId = $user2Project->userId;
                                     $manager2Task->taskId = $task->id;
                                     $manager2Task->role = User2Task::MANAGER;
                                     $manager2Task->save();
                                 }
                             }
                         }
                     }
                     $managerId = null;
                     if (!empty($taskId)) {
                         $criteria = new CDbCriteria();
                         $criteria->order = "`" . User2Task::model()->tableName() . "`.`userPriority` ASC";
                         $criteria->order .= ",`" . User2Task::model()->tableName() . "`.`id` ASC";
                         if (($user2Task = User2Task::model()->findByAttributes(array('taskId' => $taskId, 'role' => 'manager'), $criteria)) !== null) {
                             $managerId = $user2Task->userId;
                         }
                     }
                     $timeDateNotSet = empty($oldModel->time_date) || $oldModel->time_date === '0000-00-00' || strtotime($oldModel->time_date) === false;
                     $model = new Time('migrate');
                     $model->consultantId = $oldModel->user_id;
                     $model->taskId = $taskId;
                     $model->spentMinute = (int) $oldModel->hours_spent * 60;
                     $model->timeDate = $timeDateNotSet ? null : MDate::formatToDb($oldModel->time_date, 'date');
                     $model->title = $oldModel->note;
                     $model->content = $oldModel->details . "\n" . '[from migration]';
                     $model->contentMarkup = 'text';
                     $model->managerId = $managerId;
                     $model->billedMinute = (int) $oldModel->hours_billed * 60;
                     $model->invoiceId = $oldModel->invoice_id;
                     $model->invoiceAmount = $oldModel->invoice_amount;
                     $model->isConfirmed = '1';
                     $model->confirmationTime = strtotime($oldModel->last);
                     $model->createTime = strtotime($oldModel->last);
                     $model->id = $oldModel->id;
                     if ($model->save()) {
                         $i++;
                     }
                     $j++;
                 }
                 $message .= $i . ' of ' . $j . ' time records' . ($i === $t ? '' : ' with ' . $t . ' tasks') . ' have been migrated.<br/>';
             }
         }
         if ($migrate['Invoice']) {
             // invoice
             $mInvoices = MInvoice::model()->findAll($findAllCriteria);
             if (is_array($mInvoices)) {
                 $i = $j = 0;
                 foreach ($mInvoices as $oldModel) {
                     if (($model = Invoice::model()->findByPk($oldModel->id)) !== null) {
                         $model->delete();
                     }
                     $invoiceDateNotSet = empty($oldModel->invoice_date) || $oldModel->invoice_date === '0000-00-00' || strtotime($oldModel->invoice_date) === false;
                     $startDateNotSet = empty($oldModel->start_date) || $oldModel->start_date === '0000-00-00' || strtotime($oldModel->start_date) === false;
                     $endDateNotSet = empty($oldModel->end_date) || $oldModel->end_date === '0000-00-00' || strtotime($oldModel->end_date) === false;
                     $dueDateNotSet = empty($oldModel->due_date) || $oldModel->due_date === '0000-00-00' || strtotime($oldModel->due_date) === false;
                     $model = new Invoice('migrate');
                     $model->invoiceDate = $invoiceDateNotSet ? null : MDate::formatToDb($oldModel->invoice_date, 'date');
                     $model->companyId = $oldModel->customer_id;
                     $model->billedMinute = (int) $oldModel->hours_billed * 60;
                     $model->amountTotal = number_format($oldModel->total, 2, '.', '');
                     $model->startDate = $startDateNotSet ? null : MDate::formatToDb($oldModel->start_date, 'date');
                     $model->endDate = $endDateNotSet ? null : MDate::formatToDb($oldModel->end_date, 'date');
                     $model->dueDate = $dueDateNotSet ? null : MDate::formatToDb($oldModel->due_date, 'date');
                     $model->amountTime = number_format($oldModel->total, 2, '.', '');
                     $model->amountExpense = 0;
                     $model->content = '[from migration]';
                     $model->contentMarkup = 'text';
                     $model->createTime = $invoiceDateNotSet ? null : strtotime($oldModel->invoice_date);
                     $model->id = $oldModel->id;
                     if ($model->save()) {
                         $i++;
                     }
                     $j++;
                 }
                 $message .= $i . ' of ' . $j . ' invoices have been migrated.<br/>';
             }
         }
         if ($migrate['Expense']) {
             // expense
             $mExpenses = MExpense::model()->findAll($findAllCriteria);
             if (is_array($mExpenses)) {
                 $i = $j = 0;
                 $billToCompany = array('Yes' => '1', 'No' => '0');
                 foreach ($mExpenses as $oldModel) {
                     if (($model = Expense::model()->findByPk($oldModel->id)) !== null) {
                         $model->delete();
                     }
                     $expenseDateNotSet = empty($oldModel->expense_date) || $oldModel->expense_date === '0000-00-00' || strtotime($oldModel->expense_date) === false;
                     $model = new Expense('migrate');
                     $model->managerId = $oldModel->user_id;
                     $model->companyId = $oldModel->customer_id;
                     $model->projectId = $oldModel->project_id;
                     $model->invoiceId = $oldModel->invoice_id;
                     $model->expenseDate = $expenseDateNotSet ? null : MDate::formatToDb($oldModel->expense_date, 'date');
                     $model->amount = $oldModel->amount;
                     $model->billToCompany = $billToCompany[$oldModel->bill_to_customer];
                     $model->content = $oldModel->note . "\n" . '[from migration]';
                     $model->contentMarkup = 'text';
                     $model->createTime = strtotime($oldModel->last);
                     $model->id = $oldModel->id;
                     if ($model->save()) {
                         $i++;
                     }
                     $j++;
                 }
                 $message .= $i . ' of ' . $j . ' expenses have been migrated.<br/>';
             }
         }
         // last message line
         $message .= 'done';
     }
     $this->render($this->action->id, array('message' => $message));
 }