/** * * @param array $result * @param string $title * @return array */ private function setRpt10Data($result, $title) { $colAry = array(); // 利用 title 陣列作為範本 foreach ($title as $key => $value) { $title[$key] = ''; } // 合計陣列 $sumAry = $title; $sumAry['area'] = ''; $sumAry['store'] = '合計'; if (isset($result) && count($result) > 0) { $com = new ComFunction(); $comArray = $com->getStoreArray(FALSE); $store = $comArray[0]; // 門市名 $area = $comArray[2]; // 區域名 $storecode = $result[0]['storecode']; $storeary = $title; $storeary['area'] = $area[$storecode]; $storeary['store'] = $store[$storecode]; // 門市該月合計 $sum = 0; for ($i = 0; $i < count($result); $i++) { // 相同的門市放同一列 if ($result[$i]['storecode'] == $storecode) { $total = round($result[$i]['total'], 0); $storeary[substr($result[$i]['pdate'], 6)] = $total; $sum = $sum + $total; // 合計 $sumAry[substr($result[$i]['pdate'], 6)] = $sumAry[substr($result[$i]['pdate'], 6)] + $total; $sumAry['total'] = $sumAry['total'] + $total; } else { // 門市不同後放入另一列 $storeary['total'] = $sum; $colAry[$storecode] = $storeary; $storeary = $title; $storecode = $result[$i]['storecode']; $storeary['area'] = $area[$storecode]; $storeary['store'] = $store[$storecode]; $total = round($result[$i]['total'], 0); $storeary[substr($result[$i]['pdate'], 6)] = $total; $sum = $total; // 合計 $sumAry[substr($result[$i]['pdate'], 6)] = $sumAry[substr($result[$i]['pdate'], 6)] + $total; $sumAry['total'] = $sumAry['total'] + $total; } } $storeary['total'] = $sum; $colAry[$storecode] = $storeary; } // 最後把合計陣列加入 $colAry['sum'] = $sumAry; return $colAry; }
/** * */ public function actionSettle() { //年月, 預設上個月 $tmp = strtotime("-1 month", time()); $qry_YM = date('Ym', $tmp); $qry_YM = '201405'; $com = new ComFunction(); //員編 $qry_empno = ''; //員工姓名 $qry_empname = ''; if (isset($_POST['qry_YM'])) { $qry_YM = $_POST['qry_YM']; } if (isset($_POST['qry_empno'])) { $qry_empno = $_POST['qry_empno']; } if (isset($_POST['qry_empname'])) { $qry_empname = $_POST['qry_empname']; } // 標題列 $title = array(); // 欄位順序 $col = array(); // 畫面輸出陣列 $output = array(); // 顯示差勤細項紀錄 $showTotal = TRUE; if ($_POST) { // 若員工編號或姓名不為空 if ($qry_empno != '') { $user = User::model()->findByPk($qry_empno); if (isset($user->emp)) { $qry_empname = $user->emp->empname; } } elseif ($qry_empname != '') { $emp = TbsEmp::model()->findByAttributes(array('empname' => $qry_empname)); if (isset($emp)) { $qry_empno = $emp->empno; } } // 勾選顯示差勤 // if(isset($_POST['showTotal']) && $_POST['showTotal'] == 1 ) // $showTotal = TRUE; // 差勤結算 // if(isset($_POST['settle'])) $showTotal = TRUE; // 取得此月份的國定假日 $holidays = $com->getHolidayByYearMonth($qry_YM); // 取得所有的差勤項目 $logitems = TbaLogItem::model()->findAllByAttributes(array('optshow' => 1)); $items = array(); // 所有差勤項目 $itemsettle = array(); // 差勤結算項目 $itemname = array(); // 差勤名稱 $itemdays = array(); // 以天計差勤項目 (公.婚.事.病.假.曠..) $itemmins = array(); // 以分計差勤項目 ( 遲到早退開小差 ) $itemeach = array(); // 以支計差勤項目 ( 小過, 大過 ) // 將資料分類 foreach ($logitems as $item) { array_push($items, $item->id); $itemname[$item->id] = $item->logname; if ($item->weight == 1) { if ($item->logtype == 1) { if ($item->opt2 == 0) { array_push($itemdays, $item->id); } else { array_push($itemmins, $item->id); } } else { array_push($itemeach, $item->id); } } if ($item->salaryitem != '') { $itemsettle[$item->id] = $item->salaryitem; } } // 輸出的表頭 $title = $this->getWeightTitle($showTotal, $items, $itemdays, $itemmins, $itemeach, $itemname); // 欄位順序 $col = array_keys($title); // 取得差勤權重 $logweights = TbaWeight::model()->findAllByAttributes(array('opt1' => 1)); $nweight = array(); // 平日權重 $hweight = array(); // 假日權重 // foreach ($logweights as $weight) { $nweight[$weight->logitem] = $weight->nweight; $hweight[$weight->logitem] = $weight->hweight; if ($weight->opt2 == 1) { $nweight['opt2'] = $weight->nweight; } } // 計算權重 // 查出目前所有的差勤紀錄 // 每一天都丟進 $holidays 來判斷是否為假日 // 再逐項去判斷是否有權重. 若有則再做計算. // 而權重要判斷成是以天計, 以分計, 還是以支計 $aStart = $com->getTheFirstDayByYearMonth($qry_YM, "Ymd"); $aEnd = $com->getTheLastDayByYearMonth($qry_YM, "Ymd"); // 查出區間內之差勤紀錄, 依員編. 差勤項目. 排序 $sql = "SELECT * \n FROM `tba_log` \n WHERE logday \n BETWEEN '{$aStart}'\n AND '{$aEnd}' "; if ($qry_empno != '') { $sql = $sql . "AND empno = '{$qry_empno}' "; } $sql = $sql . "ORDER BY empno, logitem, opt2"; $logs = TbaLog::model()->findAllBySql($sql); // 針對每一筆. 先判斷是否有權重. 再判斷是那一種 // logtype=1 => 天 => 再判斷平日.假日 // logtype=1 => => opt2 = 1 ==> 分 , 該月累積超過30分, 則有權重 // 支 => 每一筆皆有 $emp = $logs[0]->empno; $empAry = array(); $empAry['empname'] = $logs[0]->empname; for ($i = 0; $i < count($logs); $i++) { // 若符合要計算權重的項目才進行判斷 if (in_array($logs[$i]->logitem, $items)) { // 同一個人一個 array if ($logs[$i]->empno == $emp) { if (isset($empAry[$logs[$i]->logitem])) { $empAry[$logs[$i]->logitem] = $empAry[$logs[$i]->logitem] + $logs[$i]->num; } else { $empAry[$logs[$i]->logitem] = $logs[$i]->num; } $empAry = $this->setWeightValue($empAry, $logs[$i], $holidays, $nweight, $hweight); } else { // 寫入前, 把遲到分鐘數, 換算成權重 if (isset($empAry['opt2']) && isset($nweight['opt2'])) { $empAry['opt2'] = $this->setLateWeight($empAry['opt2'], $nweight['opt2']); } else { $empAry['opt2'] = 0; } $empAry['sum'] = $this->setSumWeight($col, $empAry); $output[$emp] = $empAry; $emp = $logs[$i]->empno; $empAry = array(); $empAry['empname'] = $logs[$i]->empname; if (isset($empAry[$logs[$i]->logitem])) { $empAry[$logs[$i]->logitem] = $empAry[$logs[$i]->logitem] + $logs[$i]->num; } else { $empAry[$logs[$i]->logitem] = $logs[$i]->num; } $empAry = $this->setWeightValue($empAry, $logs[$i], $holidays, $nweight, $hweight); } } // if(in_array($logs[$i]->logitem, $items)) } // for ($i = 0; $i < count($logs); $i++) if (isset($empAry['opt2']) && isset($nweight['opt2'])) { $empAry['opt2'] = $this->setLateWeight($empAry['opt2'], $nweight['opt2']); } else { $empAry['opt2'] = 0; } $empAry['sum'] = $this->setSumWeight($col, $empAry); $output[$emp] = $empAry; // 如果是結算, 則將結果寫入資料庫 if (isset($_POST['settle'])) { // 先刪除該年月之資料 $deleteAry = array(); $param = TbmParam::model()->findByAttributes(array('param' => 'attend_settle')); if ($param != NULL) { $deleteAry = explode(",", $param->pvalue); } // 先刪除此年月之資料 if ($qry_empno != '' or $qry_empname != '') { TbmEmpItem::model()->deleteAllByAttributes(array('daymonth' => $qry_YM, 'empno' => $qry_empno, 'itemno' => $deleteAry)); } else { TbmEmpItem::model()->deleteAllByAttributes(array('daymonth' => $qry_YM, 'itemno' => $deleteAry)); } $result = $this->createSettleItems($qry_YM, $itemsettle, $output); if (isset($result)) { $check = isset($result[0]) ? $result[0] : FALSE; $errmsg = isset($result[1]) ? $result[1] : "差勤結算失敗!"; if ($check) { Yii::app()->user->setFlash('success', "[差勤結算]成功!{$qry_YM} 共計有 " . count($output) . "筆 員工有差勤紀錄!"); } else { Yii::app()->user->setFlash('error', "[差勤結算]失敗!{$errmsg!}"); } } } else { Yii::app()->user->setFlash('success', "[畫面輸出]查詢成功!{$qry_YM} 共計有 " . count($output) . "筆 員工有差勤紀錄!"); } } else { Yii::app()->user->setFlash('notice', "[畫面輸出]可以只查看此年月之差勤權重, 不會結算至薪資。[差勤結算]會將此年月之差勤結果寫入薪資項目,供薪資計算用。"); } $this->render('settle', array('qry_YM' => $qry_YM, 'qry_empno' => $qry_empno, 'qry_empname' => $qry_empname, 'title' => $title, 'col' => $col, 'colAry' => $output)); }