예제 #1
0
 /**
  * Displays a particular model.
  * @param integer $id the ID of the model to be displayed
  */
 public function actionView($id)
 {
     $viewModel = Expense::model()->findAll('employee_id = :id', array(':id' => $id));
     if (isset($_GET['asModal'])) {
         $this->renderPartial('view', array('model' => $this->loadModel($id), 'viewModel' => $viewModel));
     } else {
         $this->render('view', array('model' => $this->loadModel($id), 'viewModel' => $viewModel));
     }
 }
예제 #2
0
 public function Prod($depId, $dates)
 {
     $dishProd = Expense::model()->with('order.dish.dishStruct.Struct')->findAll('date(order_date) = :dates AND dish.department_id = :department_id', array(':dates' => $dates, ':department_id' => $depId));
     if (!empty($dishProd)) {
         foreach ($dishProd as $value) {
             foreach ($value->getRelated('order') as $val) {
                 foreach ($val->getRelated('dish')->getRelated('dishStruct') as $vals) {
                     $outProduct[$vals->prod_id] = $outProduct[$vals->prod_id] + $vals->amount / $val->getRelated('dish')->count * $val->count;
                 }
             }
         }
     }
     $Prod = Expense::model()->with('order.products')->findAll('date(order_date) = :dates AND products.department_id = :department_id', array(':dates' => $dates, ':department_id' => $depId));
     if (!empty($Prod)) {
         foreach ($Prod as $value) {
             foreach ($value->getRelated('order') as $val) {
                 $outProduct[$val->just_id] = $outProduct[$val->just_id] + $val->count;
             }
         }
     }
     return $outProduct;
 }
예제 #3
0
 public function actionMbalanceRefresh()
 {
     if (!empty($_POST)) {
         $from = $_POST['from'];
         $to = $_POST['to'];
         $days = strtotime($to) - strtotime($from);
         $newModel = Expense::model()->findAll(array('condition' => 'kind = 0', 'group' => 'date(order_date)'));
         $count = 0;
         $expense = new Expense();
         $summ = array();
         $summP = array();
         $dateList = array();
         for ($i = 0; $i < $days / (3600 * 24); $i++) {
             $mBalance = MBalance::model()->find('t.b_date = :dates', array(':dates' => date('Y-m-d', strtotime($from) + 3600 * 24 * $i)));
             $temp = $expense->getSum(date('Y-m-d', strtotime($from) + 3600 * 24 * $i));
             if (!empty($mBalance)) {
                 $mBalance->procProceeds = $temp[1];
                 $mBalance->proceeds = $temp[2];
                 $mBalance->cost = 0;
                 $mBalance->save();
             } else {
                 $mBalance = new MBalance();
                 $mBalance->b_date = date('Y-m-d', strtotime($from) + 3600 * 24 * $i);
                 $mBalance->procProceeds = $temp[1];
                 $mBalance->proceeds = $temp[2];
                 $mBalance->cost = 0;
                 $mBalance->save();
             }
         }
         $this->redirect('/');
     }
     $this->render('interval', array());
 }
예제 #4
0
 public function actionRefreshTable()
 {
     $dates = date('Y-m-d');
     $model = Expense::model()->with('employee')->findAll('date(order_date) = :dates', array(':dates' => $dates));
     $this->renderPartial('cook', array('model' => $model));
 }
예제 #5
0
 public function actionToday()
 {
     $dates = date('Y-m-d');
     $startProducts = array();
     $inProducts = array();
     $outProducts = array();
     $inOutProducts = array();
     $endProducts = array();
     $prodModel = Products::model()->findAll();
     //Приход
     $fakturaProd = Faktura::model()->with('realize.products')->findAll('date(realize_date) = :realize_date', array('realize_date' => $dates));
     foreach ($fakturaProd as $value) {
         foreach ($value->getRelated('realize') as $key => $val) {
             $inProducts[$val->getRelated('products')->product_id] = $inProducts[$val->getRelated('products')->product_id] + $val->count;
         }
     }
     //Расход
     $Depfaktura = DepFaktura::model()->with('realizedProd')->findAll('date(real_date) = :real_date', array(':real_date' => $dates));
     foreach ($Depfaktura as $value) {
         foreach ($value->getRelated('realizedProd') as $val) {
             $outProducts[$val->prod_id] = $outProducts[$val->prod_id] + $val->count;
         }
     }
     $expense = Expense::model()->with('order.products')->findAll('date(order_date) = :dates AND t.kind = :kind', array(':kind' => 1, ':dates' => $dates));
     foreach ($expense as $value) {
         foreach ($value->getRelated('order') as $val) {
             $inOutProducts[$val->just_id] = $inOutProducts[$val->just_id] + $val->count;
         }
     }
     $curProd = Balance::model()->with('products')->findAll('b_date = :dates', array(':dates' => $dates), array('order' => 'products.name'));
     foreach ($curProd as $value) {
         $endProducts[$value->prod_id] = $endProducts[$value->prod_id] + $value->startCount + $inProducts[$value->prod_id] - $outProducts[$value->prod_id] - $inOutProducts[$value->prod_id];
     }
     $this->render('today', array('prodModel' => $prodModel, 'model' => $curProd, 'inProducts' => $inProducts, 'outProducts' => $outProducts, 'inOutProducts' => $inOutProducts, 'endProducts' => $endProducts));
 }
예제 #6
0
 public function actionUsedProdLists()
 {
     $dates = $_POST['dates'];
     $depId = $_POST['department_id'];
     $prodType = $_POST['prod_type'];
     $curDish = array();
     $products = array();
     $stuff = array();
     $dishCount = array();
     $prodList = DepBalance::model()->with('products')->findAll('t.b_date = :dates AND t.department_id = :depId AND t.type = :type', array(':dates' => $dates, ':depId' => $depId, ':type' => 1));
     $stuffList = DepBalance::model()->with('stuff')->findAll('t.b_date = :dates AND t.department_id = :depId AND t.type = :type', array(':dates' => $dates, ':depId' => $depId, ':type' => 2));
     if ($prodType == 1) {
         $model = Expense::model()->with('order.dish.dishStruct')->findAll('date(t.order_date) = :dates AND dish.department_id = :depId', array(':dates' => $dates, ':depId' => $depId));
         foreach ($model as $value) {
             foreach ($value->getRelated('order') as $val) {
                 $curDish[$val->getRelated('dish')->dish_id] = $val->getRelated('dish')->name;
                 $dishCount[$val->getRelated('dish')->dish_id] = $dishCount[$val->getRelated('dish')->dish_id] + $val->count;
                 foreach ($val->getRelated('dish')->getRelated('dishStruct') as $vals) {
                     $products[$val->getRelated('dish')->dish_id][$vals->prod_id] = $products[$val->getRelated('dish')->dish_id][$vals->prod_id] + $vals->amount / $val->getRelated('dish')->count * $val->count;
                 }
             }
         }
         $model2 = Expense::model()->with('order.dish.halfstuff')->findAll('date(t.order_date) = :dates AND dish.department_id = :depId', array(':dates' => $dates, ':depId' => $depId));
         foreach ($model2 as $value) {
             foreach ($value->getRelated('order') as $val) {
                 $curDish[$val->getRelated('dish')->dish_id] = $val->getRelated('dish')->name;
                 foreach ($val->getRelated('dish')->getRelated('halfstuff') as $vals) {
                     $stuff[$val->getRelated('dish')->dish_id][$vals->halfstuff_id] = $stuff[$val->getRelated('dish')->dish_id][$vals->halfstuff_id] + $vals->amount / $val->getRelated('dish')->count * $val->count;
                 }
             }
         }
         $this->renderPartial('usedDishProd', array('dishCount' => $dishCount, 'prodList' => $prodList, 'stuffList' => $stuffList, 'products' => $products, 'stuff' => $stuff, 'curDish' => $curDish));
     } elseif ($prodType == 2) {
         $model3 = Expense::model()->with('order.halfstuff.stuffStruct')->findAll('date(t.order_date) = :dates AND halfstuff.department_id = :depId', array(':dates' => $dates, ':depId' => $depId));
         foreach ($model3 as $value) {
             foreach ($value->getRelated('order') as $val) {
                 $curStuff[$val->getRelated('halfstuff')->halfstuff_id] = $val->getRelated('halfstuff')->name;
                 $stuffCount[$val->getRelated('halfstuff')->halfstuff_id] = $dishCount[$val->getRelated('halfstuff')->halfstuff_id] + $val->count;
                 foreach ($val->getRelated('halfstuff')->getRelated('stuffStruct') as $vals) {
                     if ($vals->types == 1) {
                         $products[$val->getRelated('halfstuff')->halfstuff_id][$vals->prod_id] = $products[$val->getRelated('halfstuff')->halfstuff_id][$vals->prod_id] + $vals->amount / $val->getRelated('halfstuff')->count * $val->count;
                     } elseif ($vals->types) {
                         $stuff[$val->getRelated('halfstuff')->halfstuff_id][$vals->prod_id] = $stuff[$val->getRelated('halfstuff')->halfstuff_id][$vals->prod_id] + $vals->amount / $val->getRelated('halfstuff')->count * $val->count;
                     }
                 }
             }
         }
         $this->renderPartial('usedStuffProd', array('stuffCount' => $stuffCount, 'prodList' => $prodList, 'stuffList' => $stuffList, 'products' => $products, 'stuff' => $stuff, 'curStuff' => $curStuff));
     }
 }
예제 #7
0
 public function actionAjaxDishIncome()
 {
     $prices = new Prices();
     $dishCnt = array();
     $dCount = array();
     $prodCnt = array();
     $pCount = array();
     $stuffCnt = array();
     $sCount = array();
     $dish = new Dishes();
     $stuff = new Halfstaff();
     $prod = new Products();
     $model = Expense::model()->with('order.dish')->findAll('date(t.order_date) BETWEEN :from AND :to', array(':from' => $_POST['from'], ':to' => $_POST['to']));
     if (!empty($model)) {
         foreach ($model as $value) {
             foreach ($value->getRelated('order') as $val) {
                 $dishes[$val->just_id] = $val->getRelated('dish')->name;
                 $dishCnt[$val->just_id] = $dishCnt[$val->just_id] + $val->count * $prices->getPrice($val->just_id, $value->mType, $val->type, $value->order_date) - $dish->getCostPrice($val->just_id, $value->order_date);
                 $dCount[$val->just_id] = $dCount[$val->just_id] + $val->count;
             }
         }
     }
     $model2 = Expense::model()->with('order.halfstuff')->findAll('date(t.order_date) BETWEEN :from AND :to', array(':from' => $_POST['from'], ':to' => $_POST['to']));
     if (!empty($model2)) {
         foreach ($model2 as $value) {
             foreach ($value->getRelated('order') as $val) {
                 $halfstuff[$val->just_id] = $val->getRelated('halfstuff')->name;
                 $stuffCnt[$val->just_id] = $stuffCnt[$val->just_id] + $val->count * $prices->getPrice($val->just_id, $value->mType, $val->type, $value->order_date) - $stuff->getCostPrice($val->just_id, $value->order_date);
                 $sCount[$val->just_id] = $sCount[$val->just_id] + $val->count;
             }
         }
     }
     $model3 = Expense::model()->with('order.products')->findAll('date(t.order_date) BETWEEN :from AND :to', array(':from' => $_POST['from'], ':to' => $_POST['to']));
     if (!empty($model3)) {
         foreach ($model3 as $value) {
             foreach ($value->getRelated('order') as $val) {
                 $products[$val->just_id] = $val->getRelated('products')->name;
                 $prodCnt[$val->just_id] = $prodCnt[$val->just_id] + $val->count * $prices->getPrice($val->just_id, $value->mType, $val->type, $value->order_date) - $prod->getCostPrice($val->just_id, $value->order_date);
                 $pCount[$val->just_id] = $pCount[$val->just_id] + $val->count;
             }
         }
     }
     $this->renderPartial('ajaxDishIncome', array('dishes' => $dishes, 'dishCnt' => $dishCnt, 'halfstuff' => $halfstuff, 'stuffCnt' => $stuffCnt, 'products' => $products, 'prodCnt' => $prodCnt, 'dCount' => $dCount, 'sCount' => $sCount, 'pCount' => $pCount));
 }
예제 #8
0
 public function actionAjaxEmpExpense()
 {
     $model = Expense::model()->with('employee')->findAll('date(t.order_date) = :dates AND t.employee_id = :empId', array(':dates' => $_POST['dates'], ':empId' => $_POST['empId']));
     $percent = new Percent();
     $this->renderPartial('ajaxEmpExpense', array('newModel' => $model, 'percent' => $percent->getPercent($_POST['dates'])));
 }
예제 #9
0
 public function actionUpdate($id)
 {
     $menuModel = Dishtype::model()->findAll('t.parent = :parent', array(':parent' => 0));
     $updateDish = Expense::model()->with('order.dish', 'employee')->findByPk($id);
     $updateStuff = Expense::model()->with('order.halfstuff', 'employee')->findByPk($id);
     $updateProd = Expense::model()->with('order.products', 'employee')->findByPk($id);
     if (!empty($updateDish)) {
         $empId = $updateDish->employee_id;
         $table = $updateDish->table;
         $status = $updateDish->debt;
     }
     if (!empty($updateStuff)) {
         $empId = $updateStuff->employee_id;
         $table = $updateStuff->table;
         $status = $updateStuff->debt;
     }
     if (!empty($updateProd)) {
         $empId = $updateProd->employee_id;
         $table = $updateProd->table;
         $status = $updateProd->debt;
     }
     $orders = new Orders();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['expense_id'])) {
         $model = Expense::model()->findByPk($_POST['expense_id']);
         $_POST['Expense']['order_date'] = date('Y-m-d H:i:s');
         if (isset($_POST['debt'])) {
             if ($_POST['debt'] == 1) {
                 $_POST['status'] = 1;
             }
         } else {
             $_POST['debt'] = 0;
             $_POST['status'] = 0;
         }
         $transaction = Yii::app()->db->beginTransaction();
         try {
             $archive = new ArchiveOrder();
             $archive_message = '';
             $messageType = 'warning';
             $message = "There are some errors ";
             $model->table = $_POST['table'];
             $model->employee_id = $_POST['employee_id'];
             $model->status = $_POST['status'];
             $model->debt = $_POST['debt'];
             $model->comment = $_POST['comment'];
             if ($model->save()) {
                 $orders->model()->deleteAll('expense_id = :expId', array(':expId' => $_POST['expense_id']));
                 $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 = $_POST['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 = $_POST['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 = $_POST['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('update', $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('update', array('model' => $model, 'menuModel' => $menuModel, 'updateDish' => $updateDish, 'updateStuff' => $updateStuff, 'updateProd' => $updateProd, 'empId' => $empId, 'table' => $table, 'debt' => $status, 'expense_id' => $id));
 }
예제 #10
0
 public function getExpenseProcSum($id, $dates)
 {
     $percent = new Percent();
     $summa = 0;
     $summaP = 0;
     $curPercent = 0;
     $summ = 0;
     $model = Expense::model()->with('employee')->findByPk($id);
     if (!empty($model)) {
         if ($model->getRelated('employee')->check_percent == 1) {
             $curPercent = $percent->getPercent(date('Y-m-d', strtotime($model->order_date)));
         } else {
             $curPercent = 0;
         }
         $summ = $summ + $this->getExpenseSum($model->expense_id, $dates);
     }
     $summaP = $summ / 100 * $curPercent + $summ + $summaP;
     $summa = $summ + $summa;
     return array(1 => $summaP, 2 => $summa);
 }
예제 #11
0
 /**
  * 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 = Expense::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
예제 #12
0
 public function actionProdlist()
 {
     $dates = date('Y:m:d');
     $halfstuff = array();
     $products = array();
     $inProducts = array();
     $inHalfstuff = array();
     $depId = $_POST['depId'];
     $model = DepBalance::model()->with('products')->findAll('t.department_id = :depId AND t.type = :type', array(':depId' => $depId, 'type' => 1));
     foreach ($model as $val) {
         $products[$val->prod_id] = $val->getRelated('products')->name;
     }
     //$model = new Products();
     //$products = $model->getProdName($depId);
     $departMoveOut = DepFaktura::model()->with('realizedProd')->findAll('date(t.real_date) = :dates AND t.department_id = :depId AND t.fromDepId <> :fromDepId', array(':dates' => $dates, ':depId' => $_POST['depsId'], ':fromDepId' => 0));
     foreach ($departMoveOut as $key => $val) {
         foreach ($val->getRelated('realizedProd') as $value) {
             $depIn[$value->prod_id] = $depIn[$value->prod_id] + $value->count;
         }
     }
     $departMoveIn = DepFaktura::model()->with('realizedProd')->findAll('date(t.real_date) = :dates AND t.fromDepId = :depId', array(':dates' => $dates, ':depId' => $_POST['depsId']));
     foreach ($departMoveIn as $value) {
         foreach ($value->getRelated('realizedProd') as $val) {
             $depOut[$val->prod_id] = $depOut[$val->prod_id] + $val->count;
         }
     }
     $balance = DepBalance::model()->with('products')->findAll('t.b_date = :dates AND t.department_id = :depId AND t.type = :types', array(':dates' => $dates, ':depId' => $_POST['depsId'], ':types' => 1));
     $model = DepFaktura::model()->with('realizedProd')->findAll('t.department_id = :depId AND date(t.real_date) = :dates', array(':depId' => $_POST['depsId'], ':dates' => $dates));
     if (!empty($model)) {
         foreach ($model as $value) {
             foreach ($value->getRelated('realizedProd') as $val) {
                 $inProducts[$val->prod_id] = $inProducts[$val->prod_id] + $val->count;
             }
         }
     }
     $dishProd = Expense::model()->with('order.dish.dishStruct.Struct')->findAll('date(order_date) = :dates AND dish.department_id = :department_id', array(':dates' => $dates, ':department_id' => $_POST['depsId']));
     if (!empty($dishProd)) {
         foreach ($dishProd as $value) {
             foreach ($value->getRelated('order') as $val) {
                 foreach ($val->getRelated('dish')->getRelated('dishStruct') as $vals) {
                     $outProduct[$vals->prod_id] = $outProduct[$vals->prod_id] + $vals->amount / $val->getRelated('dish')->count * $val->count;
                 }
             }
         }
     }
     $Prod = Expense::model()->with('order.products')->findAll('date(order_date) = :dates AND products.department_id = :department_id', array(':dates' => $dates, ':department_id' => $_POST['depsId']));
     if (!empty($Prod)) {
         foreach ($Prod as $value) {
             foreach ($value->getRelated('order') as $val) {
                 $outProduct[$val->just_id] = $outProduct[$val->just_id] + $val->count;
             }
         }
     }
     foreach ($balance as $value) {
         $endCount[$value->prod_id] = $value->startCount + $inProducts[$value->prod_id] - $outProduct[$value->prod_id] + $depIn[$value->prod_id] - $depOut[$value->prod_id];
     }
     $this->renderPartial('lists', array('endCount' => $endCount, 'products' => $products, 'halfstuff' => $halfstuff, 'inHalfstuff' => $inHalfstuff));
 }
예제 #13
0
<!--<div ><?php 
$expense = new Expense();
$this->widget('application.extensions.print.printWidget', array('printedElement' => '#datas-' . $empId, 'htmlOptions' => array('id' => 'print')));
?>
</div>-->
<?php 
foreach ($empId as $key => $val) {
    $model = Expense::model()->with('order')->findAll('date(t.order_date) = :dates AND t.employee_id = :empId AND t.kind = :kind', array(':dates' => $dates, ':empId' => $val, ':kind' => 0));
    $counting = 1;
    $count = 0;
    ?>
    <?php 
    if (!empty($model)) {
        ?>
        <h3><?php 
        echo Employee::model()->findByPk($val)->name;
        ?>
</h3>
<table class="table table-bordered">
    <thead>
    <tr>
        <th></th>
        <th>Дата и время</th>
        <th>Стол №</th>
        <th>Сумма счета</th>
        <th></th>
    </tr>
    </thead>
    <tbody>
    <?php 
        foreach ($model as $value) {
예제 #14
0
 /**
  * Print out array of models for the jqGrid rows.
  */
 public function actionGridData()
 {
     if (!Yii::app()->request->isPostRequest) {
         throw new CHttpException(400, Yii::t('http', 'Invalid request. Please do not repeat this request again.'));
         exit;
     }
     // specify request details
     $jqGrid = $this->processJqGridRequest();
     // specify filter parameters
     $company = isset($_GET['company']) ? $_GET['company'] : null;
     if ($company !== 'all' && !ctype_digit($company)) {
         $company = 'all';
     }
     $project = isset($_GET['project']) ? $_GET['project'] : null;
     if ($project !== 'all' && !ctype_digit($project)) {
         $project = 'all';
     }
     $billToCompany = isset($_GET['billToCompany']) ? $_GET['billToCompany'] : null;
     if ($billToCompany !== 'all' && $billToCompany !== (string) Expense::BILL_TO_COMPANY && $billToCompany !== (string) Expense::DO_NOT_BILL_TO_COMPANY) {
         $billToCompany = 'all';
     }
     // criteria
     $criteria = new CDbCriteria();
     $criteria->group = "`t`.`id`";
     // required by together()
     $criteria->select = "`t`.amount, `t`.billToCompany, `t`.expenseDate";
     //$criteria->select="`t`.`amount`, `t`.`billToCompany`, `t`.`expenseDate`"; // uncomment in yii-1.1.2
     if ($jqGrid['searchField'] !== null && $jqGrid['searchString'] !== null && $jqGrid['searchOper'] !== null) {
         $field = array('amount' => "`t`.`amount`", 'billToCompany' => "`t`.`billToCompany`", 'expenseDate' => "`t`.`expenseDate`", 'company' => "`Expense_Company`.`title`", 'project' => "`Expense_Project`.`title`");
         $operation = $this->getJqGridOperationArray();
         $keywordFormula = $this->getJqGridKeywordFormulaArray();
         if (isset($field[$jqGrid['searchField']]) && isset($operation[$jqGrid['searchOper']])) {
             $criteria->condition = '(' . $field[$jqGrid['searchField']] . ' ' . $operation[$jqGrid['searchOper']] . ' :keyword)';
             $criteria->params = array(':keyword' => str_replace('keyword', $jqGrid['searchString'], $keywordFormula[$jqGrid['searchOper']]));
             // search by special field types
             if ($jqGrid['searchField'] === 'createTime' && ($keyword = strtotime($jqGrid['searchString'])) !== false) {
                 $criteria->params = array(':keyword' => str_replace('keyword', $keyword, $keywordFormula[$jqGrid['searchOper']]));
                 if (date('H:i:s', $keyword) === '00:00:00') {
                     // visitor is looking for a precision by day, not by second
                     $criteria->condition = '(TO_DAYS(FROM_UNIXTIME(' . $field[$jqGrid['searchField']] . ',"%Y-%m-%d")) ' . $operation[$jqGrid['searchOper']] . ' TO_DAYS(FROM_UNIXTIME(:keyword,"%Y-%m-%d")))';
                 }
             }
         }
     }
     if ($company !== 'all') {
         $criteria->addCondition("`Expense_Company`.`id`=:companyId");
         $criteria->params[':companyId'] = $company;
     }
     if ($project !== 'all') {
         $criteria->addCondition("`Expense_Project`.`id`=:projectId");
         $criteria->params[':projectId'] = $project;
     }
     if ($billToCompany === (string) Expense::BILL_TO_COMPANY) {
         $criteria->addCondition("`t`.`billToCompany`=:billToCompany");
         $criteria->params[':billToCompany'] = Expense::BILL_TO_COMPANY;
     } else {
         if ($billToCompany === (string) Expense::DO_NOT_BILL_TO_COMPANY) {
             $criteria->addCondition("`t`.`billToCompany`=:doNotBillToCompany");
             $criteria->params[':doNotBillToCompany'] = Expense::DO_NOT_BILL_TO_COMPANY;
         }
     }
     if (User::isClient()) {
         $criteria->addCondition("`Company_User2Company`.`userId`=:clientId AND `Company_User2Company`.`position`=:clientPosition");
         $criteria->params[':clientId'] = Yii::app()->user->id;
         $criteria->params[':clientPosition'] = Company::OWNER;
     }
     // pagination
     $with = array();
     if (strpos($criteria->condition, 'Expense_Company') !== false) {
         $with[] = 'company';
     }
     if (strpos($criteria->condition, 'Expense_Project') !== false) {
         $with[] = 'project';
     }
     if (strpos($criteria->condition, 'Company_User2Company') !== false) {
         $with[] = 'company.allUser2Company';
     }
     if (count($with) >= 1) {
         $pages = new CPagination(Expense::model()->with($with)->count($criteria));
     } else {
         $pages = new CPagination(Expense::model()->count($criteria));
     }
     $pages->pageSize = $jqGrid['pageSize'] !== null ? $jqGrid['pageSize'] : self::GRID_PAGE_SIZE;
     $pages->applyLimit($criteria);
     //sort
     $sort = new CSort('Expense');
     $sort->attributes = array('amount' => array('asc' => "`t`.`amount`", 'desc' => "`t`.`amount` desc", 'label' => Expense::model()->getAttributeLabel('amount')), 'billToCompany' => array('asc' => "`t`.`billToCompany`", 'desc' => "`t`.`billToCompany` desc", 'label' => Expense::model()->getAttributeLabel('Bill')), 'expenseDate' => array('asc' => "`t`.`expenseDate`", 'desc' => "`t`.`expenseDate` desc", 'label' => Expense::model()->getAttributeLabel('Date')), 'company' => array('asc' => "`Expense_Company`.`title`", 'desc' => "`Expense_Company`.`title` desc", 'label' => Expense::model()->getAttributeLabel('companyId')), 'project' => array('asc' => "`Expense_Project`.`title`", 'desc' => "`Expense_Project`.`title` desc", 'label' => Expense::model()->getAttributeLabel('projectId')));
     $sort->defaultOrder = "`t`.`expenseDate` DESC, `t`.`id` DESC";
     $sort->applyOrder($criteria);
     // find all
     $with = array('company' => array('select' => 'title'), 'project' => array('select' => 'title'));
     if (strpos($criteria->condition, 'Company_User2Company') !== false) {
         $with['company.allUser2Company'] = array('select' => 'id');
     }
     $together = strpos($criteria->condition, 'Company_User2Company') !== false;
     if ($together) {
         $models = Expense::model()->with($with)->together()->findAll($criteria);
     } else {
         $models = Expense::model()->with($with)->findAll($criteria);
     }
     // create resulting data array
     $data = array('page' => $pages->getCurrentPage() + 1, 'total' => $pages->getPageCount(), 'records' => $pages->getItemCount(), 'rows' => array());
     foreach ($models as $model) {
         $data['rows'][] = array('id' => $model->id, 'cell' => array(isset($model->company->id) ? CHtml::link(CHtml::encode($model->company->title), array('company/show', 'id' => $model->company->id)) : '', isset($model->project->id) ? CHtml::link(CHtml::encode($model->project->title), array('project/show', 'id' => $model->project->id)) : '', CHtml::encode(MDate::format($model->expenseDate, 'medium', null)), CHtml::encode($model->amount), CHtml::encode($model->getAttributeView('billToCompany', 'grid')), User::isManager() && empty($model->invoiceId) || User::isAdministrator() ? CHtml::link('<span class="ui-icon ui-icon-zoomin"></span>', array('show', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent w3-first ui-corner-all', 'title' => Yii::t('link', 'Show'))) . CHtml::link('<span class="ui-icon ui-icon-pencil"></span>', array('update', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent ui-corner-all', 'title' => Yii::t('link', 'Edit'))) . CHtml::link('<span class="ui-icon ui-icon-trash"></span>', array('delete', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent w3-last ui-corner-all', 'title' => Yii::t('link', 'Delete the record number {id}', array('{id}' => $model->id)))) : CHtml::link('<span class="ui-icon ui-icon-zoomin"></span>', array('show', 'id' => $model->id), array('class' => 'w3-ig w3-link-icon w3-border-1px-transparent w3-first w3-last ui-corner-all', 'title' => Yii::t('link', 'Show')))));
     }
     $this->printJson($data);
 }
예제 #15
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));
 }