Beispiel #1
0
 public function savedraftAction()
 {
     // 返回值数组
     $result = array('success' => true, 'result' => true);
     $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;
     $upd_type = isset($val->upd_type) ? $val->upd_type : "";
     $id = "";
     if (isset($val->nid) && $val->nid) {
         $id = $val->nid;
     }
     $updbom = new Product_Model_Updbom();
     $record = new Dcc_Model_Record();
     $faModel = new Product_Model_Fa();
     $sonModel = new Product_Model_Son();
     $fadevModel = new Product_Model_Fadev();
     $sondevModel = new Product_Model_Sondev();
     $db = $updbom->getAdapter();
     // 保存数据
     $data = array("upd_type" => $upd_type, "replace_flg" => isset($val->replace_flg) ? 1 : 0, "replace" => isset($val->replacea) ? $val->replacea : "", "replaced" => isset($val->replaced) ? $val->replaced : "", "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" => 'Draft', "remark" => isset($val->remark) ? $val->remark : "", "create_time" => $now, "create_user" => $user, "update_time" => $now, "update_user" => $user);
     try {
         if (!$id) {
             $id = $updbom->insert($data);
             if ($id) {
                 $result['nid'] = $id;
                 /*// 操作记录
                   $data = array(
                           'type'             => "dev",
                           'table_name'       => "oa_product_bom_upd",
                           'table_id'         => $id,
                           'handle_user'      => $user,
                           'handle_time'      => $now,
                           'action'           => "新建",
                           'ip'               => $_SERVER['REMOTE_ADDR']
                   );
                   $record->insert($data);*/
                 // 自定义字段
                 $attrval = new Admin_Model_Formval();
                 if ($upd_type == 'ECO') {
                     $menu = 'oa_product_bom_eco_' . $id;
                 } else {
                     $menu = 'oa_product_bom_dev_' . $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);
                     }
                 }
             }
         }
         // 替代料
         if ($data['replace_flg'] && $data['replace'] && $data['replaced']) {
             $result['success'] = false;
             $result['info'] = "不存在以“" . $data['replaced'] . "”为子物料的BOM";
             // 删除bom表
             $delKeys = $db->query("select group_concat(recordkey) as recordkey from oa_product_bom_fa_dev where nid = {$id} and type='ECO'")->fetchObject();
             $fadevModel->delete("nid = {$id} and type='ECO'");
             if ($delKeys && $delKeys->recordkey) {
                 $sondevModel->delete("nid = {$id}");
             }
             $replaceids = $db->query("select id from oa_product_materiel where code = '" . $data['replace'] . "'")->fetchObject();
             $replaceid = $replaceids->id;
             // 查询fa
             $recordkeys = $db->query("select group_concat(recordkey) as recordkey from oa_product_bom_son where code = '" . $data['replaced'] . "'")->fetchObject();
             if ($recordkeys && $recordkeys->recordkey) {
                 $fas = $faModel->getFaList("(state = 'MBOM' or state = 'EBOM') and recordkey in ({$recordkeys->recordkey})");
                 if (count($fas) > 0) {
                     // 只取一次最大值,可能遇到重号
                     $maxkeys1 = $db->query("select ifnull(max(recordkey),0) + 1 as maxkey from oa_product_bom_fa_dev")->fetchObject();
                     $maxkeys2 = $db->query("select ifnull(max(recordkey),0) + 1 as maxkey from oa_product_bom_fa")->fetchObject();
                     $recordkey1 = $maxkeys1->maxkey;
                     $recordkey2 = $maxkeys2->maxkey;
                     $recordkey = max($recordkey1, $recordkey2);
                     $fainsert = array();
                     $soninsert = array();
                     foreach ($fas as $farow) {
                         $ver = $farow['ver'] + 0.1;
                         if (is_int($ver)) {
                             $ver = $ver . '.0';
                         }
                         $fadevdata = array("nid" => $id, "recordkey" => $recordkey, "id" => $farow['id'], "code" => $farow['code'], "project_no" => $farow['project_no'], "bom_file" => $farow['bom_file'], "qty" => $farow['qty'], "state" => $farow['state'], "ver" => $ver, "type" => $upd_type, "remark" => $farow['remark']);
                         $fainsert[] = $fadevdata;
                         $sons = $sonModel->getList("recordkey = '" . $farow['recordkey'] . "'");
                         foreach ($sons as $sonrow) {
                             $sondevdata = array("nid" => $id, "recordkey" => $recordkey, "pid" => $sonrow['pid'], "id" => $sonrow['id'], "code" => $sonrow['code'], "qty" => $sonrow['qty'], "partposition" => $sonrow['partposition'], "replace" => $sonrow['replace'], "remark" => $sonrow['remark']);
                             if ($sonrow['code'] == $data['replaced']) {
                                 $sondevdata['code'] = $data['replace'];
                                 $sondevdata['id'] = $replaceid;
                                 $sonreplaceArr = explode(',', $sonrow['replace']);
                                 if (in_array($data['replace'], $sonreplaceArr)) {
                                     for ($i = 0; $i < count($sonreplaceArr); $i++) {
                                         if ($sonreplaceArr[$i] == $data['replace']) {
                                             unset($sonreplaceArr[$i]);
                                         }
                                     }
                                 }
                                 $sondevdata['replace'] = implode(',', $sonreplaceArr);
                             }
                             $soninsert[] = $sondevdata;
                         }
                         $recordkey++;
                     }
                     foreach ($fainsert as $faData) {
                         $fadevModel->insert($faData);
                     }
                     foreach ($soninsert as $sonData) {
                         $sondevModel->insert($sonData);
                     }
                     $result['success'] = true;
                     $result['info'] = "";
                 }
             }
         }
     } catch (Exception $e) {
         $result['result'] = false;
         $result['info'] = $e->getMessage();
         echo Zend_Json::encode($result);
         exit;
     }
     echo Zend_Json::encode($result);
     exit;
 }
Beispiel #2
0
 public function autosaveAction()
 {
     // 返回值数组
     $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;
     $fas = array();
     if ($val->fa) {
         $fas = Zend_Json::decode($val->fa);
     }
     $sons = array();
     if ($val->son) {
         $sons = Zend_Json::decode($val->son);
     }
     $type = $val->type;
     if ($type == 'edit') {
         $fadev = new Product_Model_Fa();
         $sondev = new Product_Model_Son();
     } else {
         $fadev = new Product_Model_Fadev();
         $sondev = new Product_Model_Sondev();
     }
     $materiel = new Product_Model_Materiel();
     $db = $fadev->getAdapter();
     // 保存数据到临时表
     $id = $val->nid;
     $recordkey = "";
     if (isset($val->recordkey)) {
         $recordkey = $val->recordkey;
     }
     if ($type == 'edit' && !$recordkey) {
         $result['result'] = false;
         $result['info'] = "参数错误,保存失败";
         echo Zend_Json::encode($result);
         exit;
     }
     if (!$id && $type != 'edit') {
         // 如果不存在表单数据,则创建一条
         if ($type == 'new') {
             $bomModel = new Product_Model_Newbom();
         } else {
             $bomModel = new Product_Model_Updbom();
         }
         // 保存数据
         $data = array("state" => 'Draft', "create_time" => $now, "create_user" => $user, "update_time" => $now, "update_user" => $user);
         try {
             $id = $bomModel->insert($data);
         } catch (Exception $e) {
             $result['result'] = false;
             $result['info'] = $e->getMessage();
             echo Zend_Json::encode($result);
             exit;
         }
     }
     if ($type == 'edit') {
         // 2 保存fa数据
         $faDatas = array();
         $keyArr = array();
         // 清除原先的数据
         $where = "recordkey=" . $recordkey;
         $sondev->delete($where);
         // 3 保存son数据
         $fa = $fas[0];
         $faData = array("project_no" => isset($fa['project_no']) ? $fa['project_no'] : 0, "bom_file" => isset($fa['bom_file']) ? $fa['bom_file'] : "", "remark" => isset($fa['remark']) ? $fa['remark'] : "");
         $sonDatas = array();
         foreach ($sons as $son) {
             if (!$son['id']) {
                 continue;
             }
             $row = $materiel->getById($son['id']);
             if (!$row['id']) {
                 continue;
             }
             $sonData = array("nid" => 0, "recordkey" => $recordkey, "pid" => $son['pid'], "id" => $row['id'], "code" => $row['code'], "qty" => isset($son['qty']) ? $son['qty'] : 1, "remark" => isset($son['remark']) ? $son['remark'] : "", "partposition" => isset($son['partposition']) ? $son['partposition'] : "", "replace" => isset($son['replace']) ? $son['replace'] : "");
             $sonDatas[] = $sonData;
         }
         $db->beginTransaction();
         try {
             $fadev->update($faData, $where);
             foreach ($sonDatas as $sonData) {
                 $sondev->insert($sonData);
             }
             $db->commit();
             //执行commit
             $result['info'] = "编辑成功";
         } catch (Exception $e) {
             $db->rollBack();
             //如果出现错误,执行回滚操作
             $result['result'] = false;
             $result['info'] = $e->getMessage();
             echo Zend_Json::encode($result);
             exit;
         }
     } else {
         // 2 保存fa数据
         $faDatas = array();
         $keyArr = array();
         // 清除原先的数据
         $delWhere = "nid=" . $id;
         $fadev->delete($delWhere);
         $sondev->delete($delWhere);
         // 只取一次最大值,可能遇到重号
         $maxkeys1 = $db->query("select ifnull(max(recordkey),0) + 1 as maxkey from oa_product_bom_fa_dev")->fetchObject();
         $maxkeys2 = $db->query("select ifnull(max(recordkey),0) + 1 as maxkey from oa_product_bom_fa")->fetchObject();
         $recordkey1 = $maxkeys1->maxkey;
         $recordkey2 = $maxkeys2->maxkey;
         $recordkey = max($recordkey1, $recordkey2);
         $faArr = array();
         foreach ($fas as $fa) {
             $fastate = "EBOM";
             if (!$fa['id']) {
                 continue;
             }
             $row = $materiel->getById($fa['id']);
             if (!$row['id']) {
                 continue;
             }
             $ver = "";
             if ($type == 'new') {
                 $ver = "1.0";
             } else {
                 // 获取最新版本
                 $vers = $db->query("select (max(ver) + 0.1) as ver, state from oa_product_bom_fa where code = '" . $row['code'] . "'")->fetchObject();
                 if ($vers && $vers->ver) {
                     $ver = round($vers->ver, 1);
                 }
                 if ($vers && $vers->state && ($vers->state == 'EBOM' || $vers->state == 'MBOM')) {
                     $fastate = $vers->state;
                 }
                 if (!$ver) {
                     $result['result'] = false;
                     $result['info'] = "BOM:" . $row['code'] . "版本错误!";
                     echo Zend_Json::encode($result);
                     exit;
                 }
             }
             $keyArr[$row['id']] = $recordkey;
             $faArr[] = $row['id'];
             if ($type == 'MBOM') {
                 $fastate = 'MBOM';
             }
             if (!$fastate) {
                 $fastate = 'EBOM';
             }
             $faData = array("nid" => $id, "recordkey" => $recordkey, "id" => $row['id'], "code" => $row['code'], "project_no" => isset($fa['project_no']) ? $fa['project_no'] : 0, "bom_file" => isset($fa['bom_file']) ? $fa['bom_file'] : "", "qty" => 1, "state" => $fastate, "ver" => $ver, "type" => $type, "remark" => isset($fa['remark']) ? $fa['remark'] : "");
             $faDatas[] = $faData;
             $recordkey++;
         }
         // 3 保存son数据
         $sonDatas = array();
         foreach ($sons as $son) {
             if (!$son['id'] || !$son['pid']) {
                 continue;
             }
             $row = $materiel->getById($son['id']);
             if (!$row['id']) {
                 continue;
             }
             if (!in_array($son['pid'], $faArr) || !$keyArr[$son['pid']]) {
                 continue;
             }
             $sonData = array("nid" => $id, "recordkey" => $keyArr[$son['pid']], "pid" => $son['pid'], "id" => $row['id'], "code" => $row['code'], "qty" => isset($son['qty']) ? $son['qty'] : 1, "remark" => isset($son['remark']) ? $son['remark'] : "", "partposition" => isset($son['partposition']) ? $son['partposition'] : "", "replace" => isset($son['replace']) ? $son['replace'] : "");
             $sonDatas[] = $sonData;
         }
         $db->beginTransaction();
         try {
             foreach ($faDatas as $faData) {
                 $fadev->insert($faData);
             }
             foreach ($sonDatas as $sonData) {
                 $sondev->insert($sonData);
             }
             $db->commit();
             //执行commit
             $result['info'] = $id;
         } catch (Exception $e) {
             $db->rollBack();
             //如果出现错误,执行回滚操作
             $result['result'] = false;
             $result['info'] = $e->getMessage();
             echo Zend_Json::encode($result);
             exit;
         }
     }
     echo Zend_Json::encode($result);
     exit;
 }