コード例 #1
0
ファイル: UpdbomController.php プロジェクト: xindalu/evolve
 public function importAction()
 {
     $result = array('success' => true, 'info' => '导入成功');
     $request = $this->getRequest()->getParams();
     $type = isset($request['type']) ? $request['type'] : null;
     $nid = isset($request['nid']) ? $request['nid'] : "";
     if (isset($_FILES['csv'])) {
         $now = date('Y-m-d H:i:s');
         $user_session = new Zend_Session_Namespace('user');
         $user_id = $user_session->user_info['user_id'];
         $file = $_FILES['csv'];
         $file_extension = strrchr($file['name'], ".");
         $h = new Application_Model_Helpers();
         $tmp_file_name = $h->getMicrotimeStr() . $file_extension;
         $savepath = "../temp/";
         $tmp_file_path = $savepath . $tmp_file_name;
         move_uploaded_file($file["tmp_name"], $tmp_file_path);
         $materiel = new Product_Model_Materiel();
         $db = $materiel->getAdapter();
         if ($type == 'dev') {
             $fa = new Product_Model_Fadev();
             $son = new Product_Model_Sondev();
             $table = "oa_product_bom_fa_dev";
         } else {
             if ($type == 'bom') {
                 $fa = new Product_Model_Fa();
                 $son = new Product_Model_Son();
                 $table = "oa_product_bom_fa";
             }
         }
         if ($type == 'dev' || $type == 'bom') {
             $file = fopen($tmp_file_path, "r");
             $data = array();
             while (!feof($file)) {
                 array_push($data, fgetcsv($file));
             }
             // 数据校验
             if (count($data) <= 1) {
                 $result['success'] = false;
                 $result['info'] = "文件中无数据!";
                 fclose($file);
                 echo Zend_Json::encode($result);
                 exit;
             }
             for ($i = 1; $i < count($data); $i++) {
                 $num = $i + 1;
                 $row = $data[$i];
                 if ($i == 1 && !$row[1]) {
                     $result['success'] = false;
                     $result['info'] = "请以上级物料开头:第" . $num . "行!";
                     fclose($file);
                     echo Zend_Json::encode($result);
                     exit;
                 }
                 if (!$row[1] && !$row[2]) {
                     continue;
                 }
                 if ($row[1] && $row[2]) {
                     $result['success'] = false;
                     $result['info'] = "上级物料和下级物料不能同时存在:第" . $num . "行!";
                     fclose($file);
                     echo Zend_Json::encode($result);
                     exit;
                 }
                 if ($row[1]) {
                     if (!$materiel->checkExist($row[1])) {
                         $result['success'] = false;
                         $result['info'] = "物料“" . $row[1] . "”不存在:第" . $num . "行!";
                         fclose($file);
                         echo Zend_Json::encode($result);
                         exit;
                     }
                     $m = $materiel->getMaterielByCode($row[1]);
                 }
                 if ($row[2]) {
                     if (!$materiel->checkExist($row[2])) {
                         $result['success'] = false;
                         $result['info'] = "物料“" . $row[2] . "”不存在:第" . $num . "行!";
                         fclose($file);
                         echo Zend_Json::encode($result);
                         exit;
                     }
                 }
                 if ($row[2] && $row[7]) {
                     $replace = explode(',', $row[7]);
                     foreach ($replace as $r) {
                         $r = iconv('utf-8', 'gbk//IGNORE', $r);
                         if (!$materiel->checkExist($r)) {
                             $result['success'] = false;
                             $result['info'] = "物料“" . $r . "”不存在:第" . $i . "行!";
                             fclose($file);
                             echo Zend_Json::encode($result);
                             exit;
                         }
                     }
                 }
                 if ($i > 1 && $row[1] && $data[$i - 1][1]) {
                     $result['success'] = false;
                     $result['info'] = "BOM“" . $data[$i - 1][1] . "”不存在子物料:第{$i行!}";
                     fclose($file);
                     echo Zend_Json::encode($result);
                     exit;
                 }
             }
             // 只取一次最大值,可能遇到重号
             $maxkeys = $fa->getAdapter()->query("select ifnull(max(recordkey),0) as maxkey from {$table}")->fetchObject();
             $recordkey = $maxkeys->maxkey;
             $pid = "";
             $faArr = array();
             $sonArr = array();
             $obsoleteWhere = "1=0";
             // 校验成功,开始保存数据
             for ($i = 1; $i < count($data); $i++) {
                 $row = $data[$i];
                 if ($row[1]) {
                     $m = $materiel->getMaterielByCode($row[1]);
                     $recordkey++;
                     $pid = $m['id'];
                     $faData = array('nid' => $nid, 'recordkey' => $recordkey, 'id' => $m['id'], 'code' => $row[1], 'qty' => 1, 'state' => $row[3] ? $row[3] : 'EBOM', 'ver' => $row[4] ? $row[4] : '1.0');
                     if ($faData['ver'] > '1.0') {
                         $obsoleteWhere .= " or (code='" . $row[1] . "' and ver < '" . $faData['ver'] . "')";
                     }
                     // 检查是否已经存在
                     if ($type == 'dev') {
                         $list = $fa->getFaList("nid = {$nid} and ver = '" . $faData['ver'] . "' and id = " . $m['id']);
                     } else {
                         $list = $fa->getFaList("ver = '" . $faData['ver'] . "' and id = " . $m['id']);
                     }
                     if ($list && count($list) > 0) {
                         $result['success'] = false;
                         $result['info'] = "数据已存在:" . $faData['code'] . " " . $faData['ver'] . "!";
                         fclose($file);
                         echo Zend_Json::encode($result);
                         exit;
                     }
                     $faArr[] = $faData;
                 }
                 if ($row[2]) {
                     $m = $materiel->getMaterielByCode($row[2]);
                     $sonData = array('nid' => $nid, 'recordkey' => $recordkey, 'pid' => $pid, 'id' => $m['id'], 'code' => $row[2], 'qty' => $row[5] ? $row[5] : 1, 'partposition' => $row[6] ? $row[6] : '', 'replace' => $row[7] ? $row[7] : '', 'remark' => $row[8] ? $row[8] : '');
                     // 检查是否已经存在
                     if ($type == 'dev') {
                         $list = $son->getList("nid = {$nid} and id = " . $m['id']);
                     } else {
                         $list = $son->getList("recordkey = {$recordkey} and id = " . $m['id']);
                     }
                     if ($list && count($list) > 0) {
                         $result['success'] = false;
                         $result['info'] = "数据已存在:" . $sonData['code'] . "!";
                         fclose($file);
                         echo Zend_Json::encode($result);
                         exit;
                     }
                     $sonArr[] = $sonData;
                 }
             }
             $db->beginTransaction();
             try {
                 foreach ($faArr as $faData) {
                     $fa->insert($faData);
                 }
                 foreach ($sonArr as $sonData) {
                     $son->insert($sonData);
                 }
                 // 旧版作废
                 if ($type == 'bom') {
                     $bosoleteData = array("state" => "Obsolete");
                     $fa->update($bosoleteData, $obsoleteWhere);
                 }
                 $db->commit();
                 //执行commit
             } catch (Exception $e) {
                 $db->rollBack();
                 //如果出现错误,执行回滚操作
                 $result['result'] = false;
                 $result['info'] = $e->getMessage();
                 fclose($file);
                 echo Zend_Json::encode($result);
                 exit;
             }
             fclose($file);
         }
     }
     echo Zend_Json::encode($result);
     exit;
 }
コード例 #2
0
ファイル: ListController.php プロジェクト: xindalu/evolve
 public function importAction()
 {
     $result = array('success' => true, 'info' => '导入成功');
     $request = $this->getRequest()->getParams();
     $now = date('Y-m-d H:i:s');
     if (isset($_FILES['csv'])) {
         $now = date('Y-m-d H:i:s');
         $user_session = new Zend_Session_Namespace('user');
         $user_id = $user_session->user_info['user_id'];
         $file = $_FILES['csv'];
         $file_extension = strrchr($file['name'], ".");
         $h = new Application_Model_Helpers();
         $tmp_file_name = $h->getMicrotimeStr() . $file_extension;
         $savepath = "../temp/";
         $tmp_file_path = $savepath . $tmp_file_name;
         move_uploaded_file($file["tmp_name"], $tmp_file_path);
         $materiel = new Product_Model_Materiel();
         $supply = new Product_Model_Bpartner();
         $db = $materiel->getAdapter();
         $file = fopen($tmp_file_path, "r");
         $data = array();
         while (!feof($file)) {
             array_push($data, fgetcsv($file));
         }
         // 数据校验
         if (count($data) <= 1) {
             $result['success'] = false;
             $result['info'] = "文件中无数据!";
             fclose($file);
             echo Zend_Json::encode($result);
             exit;
         }
         // 文件格式 # 物料号 MPQ MOQ LT 供应商1 供应商2 备注
         $errorCode = array();
         $okData = array();
         for ($i = 1; $i < count($data); $i++) {
             $num = $i + 1;
             $row = $data[$i];
             if (count($row) < 2 || !$row[1]) {
                 continue;
             }
             for ($k = 0; $k < count($row); $k++) {
                 $row[$k] = str_replace("\"", "", $row[$k]);
             }
             // check materiel code
             if ($materiel->checkExist($row[1])) {
                 // data
                 $code = $row[1];
                 $supply1Id = $supply2Id = '';
                 $mpq = $moq = $lt = 0;
                 $manufacturers = $remark = '';
                 if (isset($row[2]) && $row[2] && is_numeric($row[2])) {
                     $mpq = $row[2];
                 }
                 if (isset($row[3]) && $row[3] && is_numeric($row[3])) {
                     $moq = $row[3];
                 }
                 if (isset($row[4]) && $row[4] && is_numeric($row[4])) {
                     $lt = $row[4];
                 }
                 // supply
                 $supply1 = isset($row[5]) ? $row[5] : 0;
                 $supply2 = isset($row[6]) ? $row[6] : 0;
                 // check supply exists
                 if ($supply1) {
                     $supply1 = iconv('GBK', 'UTF-8', $supply1);
                     $where = "code = '{$supply1}' or cname like '{$supply1}' or ename like '{$supply1}' or concat(code, cname) like '{$supply1}' or concat(code, ename) like '{$supply1}'";
                     $supply1Data = $supply->getJoinList($where, array(), array('id'));
                     if (count($supply1Data) > 0) {
                         $supply1Id = $supply1Data[0]['id'];
                     }
                 }
                 if ($supply2) {
                     $supply2 = iconv('GBK', 'UTF-8', $supply2);
                     $where = "code = '{$supply2}' or cname like '{$supply2}' or ename like '{$supply2}' or concat(code, cname) like '{$supply2}' or concat(code, ename) like '{$supply2}'";
                     $supply2Data = $supply->getJoinList($where, array(), array('id'));
                     if (count($supply2Data) > 0) {
                         $supply2Id = $supply2Data[0]['id'];
                     }
                 }
                 $manufacturers = isset($row[7]) ? iconv('GBK', 'UTF-8', $row[7]) : '';
                 $remark = isset($row[8]) ? iconv('GBK', 'UTF-8', $row[8]) : '';
                 $profile = array('mpq' => $mpq, 'moq' => $moq, 'tod' => $lt, 'supply1' => $supply1Id, 'supply2' => $supply2Id, 'manufacturers' => $manufacturers, 'remark' => $remark);
                 $where = "code = '{$code}'";
                 try {
                     $materiel->update($profile, $where);
                 } catch (Exception $e) {
                     $errorCode[] = $code;
                 }
                 $okData[] = $row;
             } else {
                 $errorCode[] = $row[1];
             }
         }
         if (count($errorCode) > 0) {
             $result['error'] = $errorCode;
         }
         fclose($file);
     }
     echo Zend_Json::encode($result);
     exit;
 }