Esempio n. 1
0
 /**
  * @abstract    审核文件
  * @return      null
  */
 public function reviewAction()
 {
     // 返回值数组
     $result = array('success' => true, 'result' => true, 'info' => '审批成功');
     $request = $this->getRequest()->getParams();
     $now = date('Y-m-d H:i:s');
     $user_session = new Zend_Session_Namespace('user');
     $user = $user_session->user_info['employee_id'];
     $val = (object) $request;
     $files = new Dcc_Model_Files();
     $record = new Dcc_Model_Record();
     $review = new Dcc_Model_Review();
     $formval = new Admin_Model_Formval();
     $employee = new Hra_Model_Employee();
     $upgrade = new Dcc_Model_Upgrade();
     $id = $val->id;
     $remark = $val->remark;
     $pass = $val->review_result;
     $review_id = $val->review_id;
     $publish = false;
     // 获取文件信息
     $filesData = $files->getOne($id);
     // 获取当前审核情况
     // 如果record记录被删除或状态已改变,报错
     $reviewWhere = "id = {$review_id}";
     $reviewRows = $review->getList($reviewWhere, "files");
     if (count($reviewRows) == 0) {
         $result['result'] = false;
         $result['info'] = "非法数据";
         echo Zend_Json::encode($result);
         exit;
     }
     $reviewRow = $reviewRows[0];
     if ($reviewRow['finish_flg'] != 0) {
         $result['result'] = false;
         $result['info'] = "数据状态已改变";
         echo Zend_Json::encode($result);
         exit;
     }
     // 处理记录
     $recordData = array("type" => "files", "table_name" => "oa_doc_files", "table_id" => $id, "handle_user" => $user, "handle_time" => $now, "action" => "审批", "result" => $pass == 1 ? "批准" : ($pass == 2 ? "拒绝" : "转审"), "ip" => $_SERVER['REMOTE_ADDR'], "remark" => $remark);
     // 增加记录
     $record->insert($recordData);
     // 通过方式
     $method = $reviewRow['method'];
     if ($pass == 1) {
         if ($method == 2) {
             // 任何一人处理即通过
             $finish_flg = 1;
             $actual_user = $user;
             $finish_time = $now;
         } else {
             // 所有人都需要审核,检查是否所有人都已经审核
             $plan_user = $reviewRow['plan_user'];
             $actual_user = $reviewRow['actual_user'];
             $actual_user = !$actual_user ? $user : $actual_user . "," . $user;
             // 检查计划审核人和实际审核人是否一致
             //                $plan_dept = $reviewRow['plan_dept'];
             //                if ($plan_dept) {
             //                    // 获取部门所有人员
             //                    $tmpUser = $employee->getAdapter()->query("select group_concat(id) as ids from oa_employee where dept_id in ( " . $plan_dept . ")")->fetchObject();
             //                    if ($tmpUser->ids) {
             //                        if ($plan_user)
             //                            $plan_user .= ",";
             //                        $plan_user .= $tmpUser->ids;
             //                    }
             //                }
             $planA = explode(',', $plan_user);
             $actualA = explode(',', $actual_user);
             $passFlg = true;
             foreach ($planA as $u) {
                 if ($u && !in_array($u, $actualA)) {
                     $passFlg = false;
                 }
             }
             if ($passFlg) {
                 $finish_flg = 1;
                 $finish_time = $now;
             } else {
                 $finish_flg = 0;
                 $finish_time = null;
             }
         }
         // 审核情况
         $reviewData = array("actual_user" => $actual_user, "finish_time" => $finish_time, "finish_flg" => $finish_flg);
     } else {
         if ($pass == 3) {
             // 转审
             $finish_flg = 0;
             if ($method == 2) {
                 // 处理方式为任意时,一个人转审之后其他人员也删除
                 $plan_user = str_replace('E', '', $val->transfer_id);
             } else {
                 // 更改审核情况中的审核人
                 $plan_users = explode(',', $reviewRow['plan_user']);
                 // 审核人不在审核人列表中,并且是管理员,则替换所有人
                 if (!in_array($user, $plan_users) && (Application_Model_User::checkPermissionByRoleName('文件管理员') || Application_Model_User::checkPermissionByRoleName('系统管理员'))) {
                     $plan_users = array(str_replace('E', '', $val->transfer_id));
                 } else {
                     for ($i = 0; $i < count($plan_users); $i++) {
                         if ($plan_users[$i] == $user) {
                             $plan_users[$i] = str_replace('E', '', $val->transfer_id);
                             break;
                         }
                     }
                 }
                 $plan_user = implode(',', $plan_users);
             }
             // 审核情况
             $reviewData = array("plan_user" => $plan_user, "method" => 1);
         } else {
             // 退回
             $actual_user = null;
             $finish_time = null;
             $finish_flg = 0;
             // 退回选项
             $return = $reviewRow['return'];
             if ($return == 2) {
                 // 退到初始状态
                 // 需更新的审核记录: 所有
                 $reviewWhere = "type = 'files' and file_id = {$id}";
                 // 审核情况更新数据
                 $reviewData = array("actual_user" => $actual_user, "finish_time" => $finish_time, "finish_flg" => $finish_flg);
                 // 文件状态不更新
             } else {
                 if ($return == 4) {
                     // 退到本阶段开始
                     // 需更新的审核记录
                     $reviewWhere = "type = 'files' and finish_flg = 0 and file_id = {$id}";
                     // 审核情况更新数据
                     $reviewData = array("actual_user" => $actual_user, "finish_time" => $finish_time, "finish_flg" => $finish_flg);
                     // 文件状态不更新
                 } else {
                     if ($return == 3) {
                         // 退到上一阶段
                         // 需更新的审核记录:最后一个finish_flg为1的数据和第一个finish_flg为0的数据
                         $last_1 = $first_0 = 0;
                         foreach ($reviewRows as $r) {
                             if ($r['finish_flg'] == 1) {
                                 $last_1 = $r['id'];
                             }
                             if ($r['finish_flg'] == 0 && $first_0 == null) {
                                 $first_0 = $r['id'];
                             }
                         }
                         $reviewWhere = "id = {$last_1} or id = {$first_0}";
                         // 审核情况更新数据
                         $reviewData = array("actual_user" => $actual_user, "finish_time" => $finish_time, "finish_flg" => $finish_flg);
                         // 文件状态不更新
                     } else {
                         $fileWhere = "id = {$id}";
                         // 更新文件状态为退回
                         $fileData = array("state" => "Return");
                         // 退到初始状态
                         // 更新所有record的finish_flg为0
                         $reviewWhere = "type = 'files' and file_id = {$id}";
                         // 审核情况
                         $reviewData = array("actual_user" => $actual_user, "finish_time" => $finish_time, "finish_flg" => $finish_flg);
                     }
                 }
             }
         }
     }
     $filesRow = $files->getOne($reviewRow['file_id']);
     // 判断是否需要更新文件
     // 如果所有record的记录的finish_flg 都为1,则发布
     if ($finish_flg == 1 && $review->fetchAll("type = 'files' and finish_flg = 0 and file_id = {$id}")->count() == 1) {
         $publish = true;
         $obsolute = false;
         // 如果是多个文件同时归档,需拆分
         if (strpos($filesRow['code'], ',') !== false) {
             $codes = explode(',', $filesRow['code']);
             $names = explode(',', $filesRow['name']);
             $file_ids = explode(',', $filesRow['file_ids']);
             $vers = explode(',', $filesRow['ver']);
             $description = explode('|', $filesRow['description']);
             $project_info = explode(',', $filesRow['project_info']);
             $ids = array();
             $k = 0;
             for ($i = 0; $i < count($names); $i++) {
                 $ids[] = "";
                 $length = substr_count($names[$i], '|');
                 for ($j = 0; $j <= $length; $j++) {
                     if (isset($ids[$i]) && $ids[$i]) {
                         $ids[$i] .= ',';
                     }
                     $ids[$i] .= $file_ids[$k++];
                 }
             }
             for ($i = 0; $i < count($codes); $i++) {
                 // 更新第一条
                 if ($i == 0) {
                     $data = array("state" => "Active", "code" => $codes[$i], "name" => $names[$i], "file_ids" => $ids[$i], "ver" => $vers[$i], "description" => $description[$i], "project_info" => $project_info[$i], "archive_time" => $now);
                     try {
                         $files->update($data, "id = {$id}");
                         // 如果是升版,登录升版信息
                         if ($vers[$i] > 1.0) {
                             $upgradeTmpData = array("project_no" => $project_info[$i], "description" => $description[$i]);
                             $upgrade->update($upgradeTmpData, "file_id=" . $id);
                         }
                     } catch (Exception $e) {
                         $result['result'] = false;
                         $result['info'] = $e->getMessage();
                         echo Zend_Json::encode($result);
                         exit;
                     }
                 } else {
                     $data = array("state" => "Active", "code" => $codes[$i], "ver" => $vers[$i], "tag" => $filesRow['tag'], "name" => $names[$i], "file_ids" => $ids[$i], "description" => $description[$i], "project_info" => $project_info[$i], "remark" => $filesRow['remark'], "create_time" => $filesRow['create_time'], "update_time" => $filesRow['update_time'], "create_user" => $filesRow['create_user'], "update_user" => $filesRow['update_user'], "archive_time" => $now, "add_flg" => $filesRow['add_flg']);
                     try {
                         $sid = $files->insert($data);
                         // 拆分智能表单数据
                         if ($sid) {
                             $sids[] = $sid;
                             $whereMenu = "oa_doc_files_" . $id;
                             $menus = $formval->getListByMenu($whereMenu);
                             foreach ($menus as $menu) {
                                 $menuData = array("attrid" => $menu['attrid'], "value" => $menu['value'], "menu" => "oa_doc_files_" . $sid);
                                 $formval->insert($menuData);
                             }
                             // 如果是升版,登录升版信息
                             if ($vers[$i] > 1.0) {
                                 $obsolute = true;
                                 $upgradeRow = $upgrade->fetchAll("file_id = {$id}")->toArray();
                                 foreach ($upgradeRow as $row) {
                                     $upgradeTmpData = array("file_id" => $sid, "reason" => $row['reason'], "reason_type" => $row['reason_type'], "project_no" => $project_info[$i], "description" => $description[$i], "create_user" => $row['create_user'], "create_time" => $row['create_time'], "update_user" => $row['update_user'], "update_time" => $row['update_time']);
                                     $upgrade->insert($upgradeTmpData);
                                 }
                             }
                             $recordRow = $record->fetchAll("type='files' and table_name='oa_doc_files' and table_id={$id}")->toArray();
                             foreach ($recordRow as $row) {
                                 // 增加记录
                                 $recordTmpData = array("type" => $row['type'], "table_name" => $row['table_name'], "table_id" => $sid, "handle_user" => $row['handle_user'], "handle_time" => $row['handle_time'], "action" => $row['action'], "result" => $row['result'], "ip" => $row['ip'], "remark" => $row['remark']);
                                 $record->insert($recordTmpData);
                             }
                         }
                     } catch (Exception $e) {
                         $result['result'] = false;
                         $result['info'] = $e->getMessage();
                         echo Zend_Json::encode($result);
                         exit;
                     }
                 }
             }
         }
         $fileData = array("state" => "Active", "archive_time" => $now);
         $fileWhere = "id = {$id}";
         // 更新文件状态为已归档
         $uploadData = array("archive" => 1, "archive_time" => $now);
         $upload = new Dcc_Model_Upload();
         // 获取上传文件id
         $ids = $filesRow['file_ids'];
         $uploadWhere = "id in ({$ids})";
         $upload->update($uploadData, $uploadWhere);
         // 更新旧版文件的状态为已作废
         if (strpos($filesData['ver'], '1.0') === false || $obsolute) {
             if (isset($sids) && count($sids) > 0) {
                 $obsoluteWhere = " id not in (" . implode(',', $sids) . ") and FIND_IN_SET(code, '" . $filesRow['code'] . "')";
             } else {
                 $obsoluteWhere = " FIND_IN_SET(code, '" . $filesRow['code'] . "')";
             }
             $obsoluteData = array("state" => "Obsolete");
         }
     }
     try {
         // 更新审核情况
         $review->update($reviewData, $reviewWhere);
         if (isset($obsoluteData) && isset($obsoluteWhere)) {
             $files->update($obsoluteData, $obsoluteWhere);
         }
         // 更新文件
         if (isset($fileWhere)) {
             $files->update($fileData, $fileWhere);
         }
         $this->operate("文件审批");
     } catch (Exception $e) {
         $result['result'] = false;
         $result['info'] = $e->getMessage();
         echo Zend_Json::encode($result);
         exit;
     }
     // 邮件任务
     // 文件提交者或更新人
     $owner = $filesData['create_user'];
     if ($filesData['create_user'] != $filesData['update_user']) {
         $owner .= "," . $filesData['update_user'];
     }
     $dev = false;
     $type = "新文件";
     if (stripos($filesData['ver'], '1.0') === false) {
         $dev = true;
         $type = "升版文件";
     }
     $content = "<p><b>文件号:</b>" . $filesData['code'] . "</p><p><b>版本:</b>" . $filesData['ver'] . "</p><p><b>文件描述:</b>" . $filesData['description'] . "</p><p><b>备注:</b>" . $filesData['remark'] . "</p><p><b>申请人:</b>" . $filesData['creater'] . "</p><p><b>申请时间:</b>" . $filesData['create_time'] . "</p><p>请登录系统查看详情!</p>";
     // 发邮件的情况:
     // 1、单站审核结束 $finish_flg = 1 && $publish = false
     if ($finish_flg == 1 && !$publish) {
         $subject = $type . "审批";
         // $to = 下一站审核人
         $current = $review->getFirstNoReview("files", $id);
         $to = $employee->getInfosByOneLine($current['plan_user']);
         //
         $cc = $employee->getInfosByOneLine($owner);
         $cc = $cc['email'];
         //$cc = "";
         $content = "你有一个" . $type . "需要审批," . $content;
     }
     // 2、所有审核结束  $publish = true
     if ($publish) {
         $subject = $type . "发布";
         $to = $employee->getInfosByOneLine($owner);
         $cc = $employee->getInfosByOneLine($record->getEmployeeIds($filesData['id'], 'files'));
         $cc = $cc['email'];
         //            $cc = "";
         $content .= "<p><b>审核记录:</b><br>" . str_replace(',', '<br>', $record->getHis($filesData['id'], 'files', 'oa_doc_files')) . "</p>";
         $content = "你申请的" . $type . "已通过审批," . $content;
     }
     // 3、退回 isset($return)
     if (isset($return)) {
         $subject = $type . "退回";
         $to = $employee->getInfosByOneLine($owner);
         $cc = "";
         // 原审核人
         if ($reviewRow['plan_user']) {
             $orgUser = $reviewRow['plan_user'];
             $cc = $employee->getInfosByOneLine($orgUser);
             $cc = $cc['email'];
         }
         $content = "你申请的" . $type . "已被退回,<p><b>退回原因:</b>" . $remark . "</p>" . $content;
     }
     // 4、转审 $pass == 3
     if ($pass == 3) {
         $subject = $type . "转审";
         $toUser = str_replace('E', '', $val->transfer_id);
         $to = $employee->getInfosByOneLine($toUser);
         // 原审核人
         if ($reviewRow['plan_user']) {
             $orgUser = $reviewRow['plan_user'];
             $owner .= "," . $orgUser;
         }
         $cc = $employee->getInfosByOneLine($owner);
         $cc = $cc['email'];
         //             $cc = "";
         $content = "有新的" . $type . "被转移到你处审批:" . $content;
     }
     if (isset($subject)) {
         $mailData = array('type' => $type, 'subject' => $subject, 'to' => $to['email'], 'cc' => $cc, 'content' => $content, 'send_time' => $now, 'add_date' => $now);
         $mail = new Application_Model_Log_Mail();
         try {
             $mailId = $mail->insert($mailData);
         } catch (Exception $e) {
             $result['result'] = false;
             $result['info'] = $e->getMessage();
             echo Zend_Json::encode($result);
             exit;
         }
         if ($mailId) {
             $mail->send($mailId);
         }
     }
     echo Zend_Json::encode($result);
     exit;
 }
Esempio n. 2
0
 /**
  * 获取文件编码及文件信息
  */
 public function getfilesbyidAction()
 {
     // 获取参数
     $request = $this->getRequest()->getParams();
     $id = $request['id'];
     $user_session = new Zend_Session_Namespace('user');
     $user = $user_session->user_info['employee_id'];
     $myDept = $user_session->user_info['dept_id'];
     $nowDate = date('Y-m-d');
     $files = new Dcc_Model_Files();
     $record = new Dcc_Model_Record();
     $upload = new Dcc_Model_Upload();
     $codeModel = new Dcc_Model_Code();
     $share = new Dcc_Model_Share();
     $review = new Dcc_Model_Review();
     $modelUser = new Application_Model_User();
     $data = $files->getDataById($id);
     // [id, code, ver, file_id, file_name, file_path, file_view_path]
     $result = array();
     $row0 = $data[0];
     $names = explode(',', $row0['name']);
     $codes = explode(',', $row0['code']);
     $vers = explode(',', $row0['ver']);
     $description = explode('|', $row0['description']);
     $project_info = explode(',', $row0['project_info']);
     $final_codes = array();
     $final_vers = array();
     $final_names = array();
     $final_descs = array();
     $final_projs = array();
     $k = 0;
     for ($i = 0; $i < count($names); $i++) {
         $sub_names = explode('|', $names[$i]);
         $final_codes[$k] = $codes[$i];
         $final_vers[$k] = $vers[$i];
         if (isset($description[$i]) && $description[$i]) {
             $final_descs[$k] = $description[$i];
         } else {
             $final_descs[$k] = "";
         }
         if (isset($project_info[$i]) && $project_info[$i]) {
             $final_projs[$k] = $project_info[$i];
         } else {
             $final_projs[$k] = "";
         }
         $final_names[$k] = $sub_names[0];
         $k++;
         for ($j = 1; $j < count($sub_names); $j++) {
             $final_codes[$k] = $codes[$i];
             $final_vers[$k] = $vers[$i];
             $final_names[$k] = $sub_names[$j];
             if (isset($description[$i]) && $description[$i]) {
                 $final_descs[$k] = $description[$i];
             } else {
                 $final_descs[$k] = "";
             }
             if (isset($project_info[$i]) && $project_info[$i]) {
                 $final_projs[$k] = $project_info[$i];
             } else {
                 $final_projs[$k] = "";
             }
             $k++;
         }
     }
     $catalog = new Product_Model_Catalog();
     for ($i = 0; $i < count($data); $i++) {
         $paths = $this->getPathByName($data, trim($final_names[$i]));
         $row = $data[$i];
         $result[$i]['id'] = $row['id'];
         $result[$i]['state'] = $row['state'];
         if ($final_descs[$i]) {
             $result[$i]['description'] = $final_descs[$i];
         } else {
             // 获取描述
             $descData = $codeModel->getJoinList("code = '{$final_codes[$i]}'", array(), array('description', 'project_no'));
             if (count($descData) > 0) {
                 $result[$i]['description'] = $descData[0]['description'];
                 if (!$final_projs[$i]) {
                     $final_projs[$i] = $descData[0]['project_no'];
                 }
             }
         }
         $result[$i]['project_no'] = $final_projs[$i];
         if ($final_projs[$i]) {
             $projData = $catalog->getById($final_projs[$i]);
             if ($projData && $projData['model_internal']) {
                 $result[$i]['project_name'] = $projData['model_internal'];
             }
         }
         $result[$i]['code'] = $final_codes[$i];
         $result[$i]['ver'] = $final_vers[$i];
         $result[$i]['file_id'] = $paths[0];
         $result[$i]['file_name'] = $final_names[$i];
         $result[$i]['file_path'] = $paths[1];
         $result[$i]['file_view_path'] = $paths[2];
         // 权限检查
         // 有权限的情况:1、本人上传的文件 2、本人提交的文件评审 3、本人参与审核的文件评审 4、共享给我的 5、将要审核的审核人 6、文件管理员、BOM管理员、物料管理员
         // 首先检查文件是否存在
         $result[$i]['exists'] = true;
         if (!file_exists($paths[1])) {
             $result[$i]['exists'] = false;
         }
         $role = false;
         if ($modelUser->checkPermissionByRoleName('物料管理员') || $modelUser->checkPermissionByRoleName('文件管理员') || $modelUser->checkPermissionByRoleName('系统管理员') || $modelUser->checkPermissionByRoleName('BOM管理员')) {
             $role = true;
         } else {
             if ($row0['create_user'] == $user || $row0['update_user'] == $user) {
                 $role = true;
             } else {
                 if ($record->fetchAll("table_id = {$id} and handle_user = {$user} and table_name = 'oa_doc_files' and action = '审批'")->count() > 0) {
                     $role = true;
                 } else {
                     if ($upload->fetchAll("id = " . $row['id'] . " and (create_user = {$user} or update_user = {$user})")->count() > 0) {
                         $role = true;
                     } else {
                         if ($share->fetchAll("shared_id = " . $row['id'] . " and type = 'upload' and share_time_begin <= '{$nowDate}' and share_time_end >= '{$nowDate}'  and (FIND_IN_SET({$user}, share_user) or FIND_IN_SET({$myDept}, share_dept))")->count() > 0) {
                             $role = true;
                         } else {
                             if ($review->fetchAll("file_id = {$id} and FIND_IN_SET({$user}, plan_user) and type = 'files'")->count() > 0) {
                                 $role = true;
                             }
                         }
                     }
                 }
             }
         }
         $result[$i]['role'] = $role;
     }
     echo Zend_Json::encode($result);
     exit;
 }
Esempio n. 3
0
 /**
  * @abstract    保存
  * @return      null
  */
 public function saveAction()
 {
     // 返回值数组
     $result = array('success' => true, 'result' => true, 'info' => '提交成功');
     $request = $this->getRequest()->getParams();
     $now = date('Y-m-d H:i:s');
     $user_session = new Zend_Session_Namespace('user');
     $user = $user_session->user_info['employee_id'];
     $user_name = $user_session->user_info['user_name'];
     $val = (object) $request;
     $materiel = new Product_Model_Materiel();
     $record = new Dcc_Model_Record();
     $review = new Dcc_Model_Review();
     $employee = new Hra_Model_Employee();
     $mail = new Application_Model_Log_Mail();
     $member = new Admin_Model_Member();
     $fadev = new Product_Model_Fadev();
     $faModel = new Product_Model_Fa();
     $sondev = new Product_Model_Sondev();
     $flow = new Admin_Model_Flow();
     $step = new Admin_Model_Step();
     $db = $fadev->getAdapter();
     $type = $val->upd_type;
     if ($type == 'new') {
         $bomModel = new Product_Model_Newbom();
     } else {
         $bomModel = new Product_Model_Updbom();
     }
     // 检查BOM信息是否完整
     if (!isset($val->id) || !$val->id) {
         $result['result'] = false;
         $result['info'] = "BOM信息不完整,请重新编辑";
         echo Zend_Json::encode($result);
         exit;
     }
     $id = $val->id;
     $ismanager = "";
     if (isset($val->ismanager) && $val->ismanager == '1') {
         $ismanager = "1";
         $managerState = $val->state;
     }
     // BOM完整:存在上级物料和下级物料,每个上级物料都有下级物料,替代料都存在
     $bomFinishFlg = true;
     $facount = 0;
     $faData = $fadev->fetchAll("type = '{$type}' and nid = " . $id)->toArray();
     if (count($faData) > 0) {
         $files = new Dcc_Model_Files();
         foreach ($faData as $farow) {
             if ($farow['bom_file']) {
                 $fileArr = explode(',', $farow['bom_file']);
                 foreach ($fileArr as $filecode) {
                     if ($files->fetchAll("code='{$filecode}' and state ='Active'")->count() == 0) {
                         $result['result'] = false;
                         $result['info'] = "BOM " . $farow['code'] . "的关联文件:“<b>" . $filecode . "</b>”不存在!";
                         echo Zend_Json::encode($result);
                         exit;
                     }
                 }
             }
         }
     }
     $sonData = $sondev->fetchAll("nid = " . $id)->toArray();
     if (count($faData) > 0 && count($sonData) > 0) {
         foreach ($sonData as $son) {
             if ($son['replace'] == "") {
                 continue;
             }
             $replace = explode(',', $son['replace']);
             foreach ($replace as $r) {
                 if ($materiel->fetchAll("code='{$r}'")->count() == 0) {
                     $result['result'] = false;
                     $result['info'] = "物料" . $son['code'] . "的替代料:<b>" . $r . "</b>不存在!";
                     echo Zend_Json::encode($result);
                     exit;
                 }
             }
         }
         foreach ($faData as $fa) {
             foreach ($sonData as $son) {
                 if ($fa['recordkey'] == $son['recordkey']) {
                     $facount++;
                     break;
                 }
             }
         }
         if ($facount != count($faData)) {
             $bomFinishFlg = false;
         }
     } else {
         $bomFinishFlg = false;
     }
     if (!$bomFinishFlg) {
         $result['result'] = false;
         $result['info'] = "BOM信息不完整,请重新编辑";
         echo Zend_Json::encode($result);
         exit;
     }
     // 下级BOM未归档
     $materielType = new Product_Model_Type();
     $bomArr = array();
     foreach ($faData as $fa) {
         if ($type == 'new') {
             // 检查上级bom是否已经存在,或者正在申请
             $join = array(array('type' => INNERJOIN, 'table' => $bomModel->getName(), 'condition' => $fadev->getName() . '.nid = ' . $bomModel->getName() . '.id'));
             $jwhere = $fadev->getName() . ".code = '" . $fa['code'] . "' and (" . $bomModel->getName() . ".state = 'Active' or " . $bomModel->getName() . ".state = 'Reviewing')";
             if ($fadev->getJoinCount($jwhere, $join)) {
                 $result['result'] = false;
                 $result['info'] = "BOM“" . $fa['code'] . "”已存在或正在申请";
                 echo Zend_Json::encode($result);
                 exit;
             }
             // 检查bom的状态,不能是作废或者删除
             $bomMateriel = $materiel->getMaterielByCode($fa['code']);
             if (!$bomMateriel) {
                 $result['result'] = false;
                 $result['info'] = "物料“" . $fa['code'] . "”不存在";
                 echo Zend_Json::encode($result);
                 exit;
             }
             if ($bomMateriel['state'] == 'Obsolete' || $bomMateriel['state'] == 'Deleted') {
                 $result['result'] = false;
                 $result['info'] = "物料“" . $fa['code'] . "”已作废";
                 echo Zend_Json::encode($result);
                 exit;
             }
         }
         // 检查下级bom是否归档
         foreach ($sonData as $son) {
             if (isset($bomArr[$son['id']]) && $bomArr[$son['id']]) {
                 $isbom = true;
             } else {
                 if (isset($bomArr[$son['id']]) && !$bomArr[$son['id']]) {
                     $isbom = false;
                 } else {
                     $d = $materiel->fetchRow('id = ' . $son['id']);
                     $d = $materielType->fetchRow('id = ' . $d['type']);
                     $bomArr[$son['id']] = $d['bom'];
                     $isbom = false;
                 }
             }
             if ($isbom) {
                 if ($faModel->fetchAll("id = '" . $son['id'] . "'")->count() == 0) {
                     $result['result'] = false;
                     $result['info'] = "子BOM“" . $son['code'] . "”未归档";
                     echo Zend_Json::encode($result);
                     exit;
                 }
             }
         }
     }
     // 根据BOM获取审批流
     $flow_id = "";
     if (isset($val->review_flow)) {
         $flow_id = $val->review_flow;
         $flowRow = $db->query("select t1.* from oa_admin_flow t1 where t1.id='{$flow_id}'")->fetchObject();
     }
     if (!$flow_id) {
         $flowRow = $db->query("select t1.* from oa_admin_flow t1 inner join oa_product_bom_config t2 on t1.flow_name=t2.flow where t2.type='{$type}'")->fetchObject();
         if ($flowRow) {
             $flow_id = $flowRow->id;
         }
     }
     if (!$flow_id) {
         $result['result'] = false;
         $result['info'] = "还没有配置BOM审核流程,请联系管理员配置";
         echo Zend_Json::encode($result);
         exit;
     }
     // 根据流程ID获取阶段
     $step_ids = $flowRow->step_ids;
     if ($step_ids) {
         $stepRows = $step->getListByFlow($step_ids);
         $state = "Reviewing";
     }
     $state = "Reviewing";
     if (isset($managerState) && $managerState) {
         $state = $managerState;
     }
     if ($type == 'new') {
         $data = array("description" => isset($val->description) ? $val->description : "", "state" => $state, "remark" => isset($val->remark) ? $val->remark : "", "update_time" => $now, "update_user" => $user);
     } else {
         $data = array("upd_type" => isset($val->upd_type) ? $val->upd_type : "", "replace_flg" => isset($val->replace_flg) ? 1 : 0, "description" => isset($val->description) ? $val->description : "", "upd_reason" => isset($val->upd_reason) ? $val->upd_reason : "", "reason_type" => isset($val->reason_type) ? $val->reason_type : "", "state" => $state, "remark" => isset($val->remark) ? $val->remark : "", "update_time" => $now, "update_user" => $user);
     }
     if (isset($managerState) && $managerState == 'Active') {
         $data['archive_time'] = $now;
     }
     try {
         $bomModel->update($data, "id=" . $id);
         // 自定义字段
         $attrval = new Admin_Model_Formval();
         if ($type == 'new') {
             $table = "oa_product_bom_new";
             $recordType = "bom";
         } else {
             if ($type == 'ECO') {
                 $table = "oa_product_bom_eco";
                 $recordType = "ecobom";
             } else {
                 if ($type == 'DEV') {
                     $table = "oa_product_bom_dev";
                     $recordType = "devbom";
                 }
             }
         }
         $menu = $table . '_' . $id;
         $attrval->delete("menu = '" . $menu . "'");
         foreach ($request as $field => $value) {
             if (stripos($field, "intelligenceField") !== false && $value) {
                 $attrId = str_replace("intelligenceField", "", $field);
                 $formval = array('attrid' => $attrId, 'value' => $value, 'menu' => $menu);
                 $attrval->insert($formval);
             }
         }
         $action = "申请";
         if ($record->fetchAll("type='{$recordType}' and table_id={$id}")->count() > 0) {
             $action = "编辑";
         }
         // 操作记录
         $data = array('type' => $recordType, 'table_name' => $table, 'table_id' => $id, 'handle_user' => $user, 'handle_time' => $now, 'action' => $action, 'ip' => $_SERVER['REMOTE_ADDR']);
         $record->insert($data);
         // 管理员将状态改为审核中也会触发审批流程
         if (!$ismanager || isset($managerState) && $managerState == 'Reviewing') {
             // 审核流程
             // 把阶段信息插入review记录
             // 删除已有流程
             $review->delete("type='{$recordType}' and file_id = {$id}");
             $first = true;
             foreach ($stepRows as $s) {
                 $plan_user = $s['user'];
                 if ($s['dept']) {
                     $tmpUser = array();
                     $plan_dept = $s['dept'];
                     foreach (explode(',', $plan_dept) as $role) {
                         $tmpRole = $member->getMemberWithNoManager($role);
                         foreach ($tmpRole as $m) {
                             $tmpUser[] = $m['user_id'];
                         }
                     }
                     if (count($tmpUser) > 0) {
                         $tmpUser = $employee->getAdapter()->query("select group_concat(employee_id) as users from oa_user where active = 1 and id in ( " . implode(',', $tmpUser) . ")")->fetchObject();
                         $users = $tmpUser->users;
                     }
                     if (isset($users) && $users) {
                         if ($plan_user) {
                             $plan_user .= ",";
                         }
                         $plan_user .= $users;
                     }
                 }
                 $repeatUser = explode(',', $plan_user);
                 $repeatUser = array_unique($repeatUser);
                 $plan_user = implode(',', $repeatUser);
                 $reviewData = array('type' => "{$recordType}", 'file_id' => $id, 'plan_user' => $plan_user, 'method' => $s['method'], 'return' => $s['return'], 'step_name' => $s['step_name'], 'step_ename' => $s['step_ename']);
                 $review->insert($reviewData);
                 // 邮件任务
                 if ($first) {
                     $to = $employee->getAdapter()->query("select group_concat(email) as mail_to from oa_employee where id in ( " . $plan_user . ")")->fetchObject();
                     $boms = array();
                     foreach ($faData as $fa) {
                         if ($type == 'new') {
                             $boms[] = $fa['code'];
                         } else {
                             $boms[] = $fa['code'] . " V" . $fa['ver'];
                         }
                     }
                     if ($type == 'new') {
                         $content = "你有新BOM归档申请需要审核,<p>" . "<b>BOM号:</b>" . implode(',', $boms) . "</p>" . "<p><b>描述:</b>" . $val->description . "</p>" . "<p><b>备注:</b>" . $val->remark . "</p>" . "<p><b>申请人:</b>" . $user_name . "</p>" . "<p><b>申请时间:</b>" . $now . "</p>" . "<p>请登录系统查看详情!</p>";
                     } else {
                         $reson_type = "";
                         if ($val->reason_type) {
                             $codemaster = new Admin_Model_Codemaster();
                             $mstData = $codemaster->getList("type=6 and code='" . $val->reason_type . "'");
                             if ($mstData && count($mstData) > 0) {
                                 $reson_type = $mstData[0]['text'];
                             }
                         }
                         $content = "你有新BOM升版申请需要审核,<p>" . "<b>BOM号:</b>" . implode(',', $boms) . "</p>" . "<p><b>升版类型:</b>" . $val->upd_type . "</p>" . "<p><b>升版原因分类:</b>" . $reson_type . "</p>" . "<p><b>升版原因:</b>" . $val->upd_reason . "</p>" . "<p><b>升版描述:</b>" . $val->description . "</p>" . "<p><b>备注:</b>" . $val->remark . "</p>" . "<p><b>申请人:</b>" . $user_name . "</p>" . "<p><b>申请时间:</b>" . $now . "</p>" . "<p>请登录系统查看详情!</p>";
                     }
                     $mailData = array('type' => 'BOM归档审批', 'subject' => 'BOM归档审批', 'to' => $to->mail_to, 'cc' => '', 'content' => $content, 'send_time' => $now, 'add_date' => $now);
                     $mailId = $mail->insert($mailData);
                     if ($mailId) {
                         $mail->send($mailId);
                     }
                 }
                 $first = false;
             }
         } else {
             if (isset($managerState) && $managerState == 'Active') {
                 $bomfaData = $fadev->fetchAll("type = '{$type}' and nid=" . $id)->toArray();
                 // 如果是多个BOM,需要拆分
                 foreach ($bomfaData as $bomfa) {
                     // 升版的情况,旧版作废
                     $facode = $bomfa['code'];
                     if ($type != 'new') {
                         $obsoleteData = array("state" => "Obsolete");
                         $obsoleteWhere = "code = '{$facode}'";
                         $faModel->update($obsoleteData, $obsoleteWhere);
                     }
                     $recordkey = $bomfa['recordkey'];
                     $sql = "insert into oa_product_bom_fa (nid, recordkey, id, code, project_no, bom_file, qty, state, ver, type, remark) select nid, recordkey, id, code, project_no, bom_file, qty, 'EBOM', ver, type, remark from oa_product_bom_fa_dev where recordkey = {$recordkey}";
                     $db->query($sql);
                     $sql = "insert into oa_product_bom_son (nid, recordkey, pid, id, code, qty, partposition, `replace`, remark) select nid, recordkey, pid, id, code, qty, partposition, `replace`, remark from oa_product_bom_son_dev where recordkey = {$recordkey}";
                     $db->query($sql);
                 }
                 // 更新所有record的finish_flg为0
                 $reviewWhere = "type = '{$recordType}' and file_id = {$id}";
                 // 审核情况
                 $reviewData = array("actual_user" => $user, "finish_time" => $now, "finish_flg" => '1');
                 // 更新审核情况
                 $review->update($reviewData, $reviewWhere);
             }
         }
     } catch (Exception $e) {
         $result['result'] = false;
         $result['info'] = $e->getMessage();
         echo Zend_Json::encode($result);
         exit;
     }
     echo Zend_Json::encode($result);
     exit;
 }