Exemple #1
0
 /**
  * 导入供应商信息的函数
  * 基于 PHPExcel.php
  * @return  $result   $result > 0 成功,否则失败
  */
 public function act_importSave()
 {
     $uploadfile = date("Y") . date("m") . date("d") . rand(1, 3009) . ".xls";
     if (!move_uploaded_file($_FILES['upfile']['tmp_name'], WEB_PATH . 'upload_datas/' . $uploadfile)) {
         return false;
     }
     $fileName = WEB_PATH . 'upload_datas/' . $uploadfile;
     $filePath = $fileName;
     $PHPExcel = new PHPExcel();
     $PHPReader = new PHPExcel_Reader_Excel2007();
     if (!$PHPReader->canRead($filePath)) {
         $PHPReader = new PHPExcel_Reader_Excel5();
         if (!$PHPReader->canRead($filePath)) {
             echo 'no Excel';
             return;
         }
     }
     $PHPExcel = $PHPReader->load($filePath);
     $currentSheet = $PHPExcel->getSheet(0);
     /**取得一共有多少列*/
     $c = 2;
     while (true) {
         $aa = 'A' . $c;
         $bb = 'B' . $c;
         $cc = 'C' . $c;
         $dd = 'D' . $c;
         $ee = 'E' . $c;
         $ff = 'F' . $c;
         $gg = 'G' . $c;
         $hh = 'H' . $c;
         $ii = 'I' . $c;
         $jj = 'J' . $c;
         $kk = 'K' . $c;
         $ll = 'L' . $c;
         $mm = 'M' . $c;
         $nn = 'N' . $c;
         $oo = 'O' . $c;
         $pp = 'P' . $c;
         $qq = 'Q' . $c;
         $rr = 'R' . $c;
         $ss = 'S' . $c;
         $tt = 'T' . $c;
         $uu = 'U' . $c;
         $vv = 'V' . $c;
         $ww = 'W' . $c;
         $zz = 'Z' . $c;
         $xx = 'X' . $c;
         $c++;
         $company_name = str_rep(trim($currentSheet->getCell($aa)->getValue()));
         $username = str_rep(trim($currentSheet->getCell($bb)->getValue()));
         $category_name = str_rep(trim($currentSheet->getCell($cc)->getValue()));
         $tel = str_rep(trim($currentSheet->getCell($dd)->getValue()));
         $phone = str_rep(trim($currentSheet->getCell($ee)->getValue()));
         $fax = str_rep(trim($currentSheet->getCell($ff)->getValue()));
         $QQ = str_rep(trim($currentSheet->getCell($gg)->getValue()));
         $e_mail = str_rep(trim($currentSheet->getCell($hh)->getValue()));
         $AliIM = str_rep(trim($currentSheet->getCell($ii)->getValue()));
         $shoplink = str_rep(trim($currentSheet->getCell($jj)->getValue()));
         $city = str_rep(trim($currentSheet->getCell($kk)->getValue()));
         $address = str_rep(trim($currentSheet->getCell($ll)->getValue()));
         $status = str_rep(trim($currentSheet->getCell($mm)->getValue()));
         $email_status = str_rep(trim($currentSheet->getCell($nn)->getValue()));
         $sms_status = str_rep(trim($currentSheet->getCell($oo)->getValue()));
         $purchaser = str_rep(trim($currentSheet->getCell($pp)->getValue()));
         $company = str_rep(trim($currentSheet->getCell($qq)->getValue()));
         $note = str_rep(trim($currentSheet->getCell($rr)->getValue()));
         $partnerStr = "单位名称:{$company_name}, 姓名:{$username}, 电话:{$tel} ... ";
         //var_dump(self::check_input($company_name));
         //exit;
         if ($company_name != '' && !self::check_input($company_name)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '单位名称':'{$company_name}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($username != '' && !self::check_input($username)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '姓名':'{$username}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($category_name != '' && !self::check_input($category_name)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '单位类型':'{$category_name}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($tel != '' && !self::isTel($tel)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '电话号码':'{$tel}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($phone != '' && !self::isMobile($phone)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '手机号码':'{$phone}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($fax != '' && !self::isPhone($fax)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '传真号码':'{$fax}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($QQ != '' && !self::isQQ($QQ)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, 'QQ号码':'{$QQ}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($e_mail != '' && !self::isEmail($e_mail)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '邮件':'{$e_mail}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($AliIM != '' && !self::isNormalCharacter($AliIM)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '阿里旺旺':'{$AliIM}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($city != '' && !self::check_input($city)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '所属城市':'{$city}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($address != '' && !self::check_input($address)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '地址':'{$address}' 填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         $status = $status == '黑名单' ? 0 : $status == '正常' ? 1 : 2;
         if ($status < 0 || $status > 2) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '状态'填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         $email_status = $email_status == '是' ? 1 : 0;
         if ($email_status != 0 && $email_status != 1) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '支持邮件'填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         $sms_status = $sms_status == '是' ? 1 : 0;
         if ($sms_status != 0 && $sms_status != 1) {
             $errInfo = " -[<font color='#FF0000'>导入失败, '支持短信'填写有误!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         if ($company_name == '') {
             break;
         }
         //$c++;
         //To get type_id
         $where = " AND `category_name` = '{$category_name}' ";
         $field = " `id` ";
         $resultType = PartnerTypeAct::act_getPartnerTypeList($where, $field);
         $type_id = $resultType[0]['id'];
         if (!isset($type_id)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, 单位类型 '{$category_name}' 不存在!" . $partnerStr . "</font>]";
             continue;
         }
         //To get purchaser_id
         $where = " AND `global_user_name` = '{$purchaser}' ";
         $field = " `global_user_id` ";
         $resultPurchase = self::act_getPurchaserList($where, $field);
         $purchaser_id = $resultPurchase[0]['global_user_id'];
         if (!isset($purchaser_id)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, 采购员 '{$purchaser}' 不存在!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         //To get company_id
         $where = " AND `company` = '{$company}' ";
         $field = " `id` ";
         $resultCompany = self::act_getPartnerCompany($where, $field);
         //print_r($resultCompany);
         $company_id = $resultCompany[0]['id'];
         if (!isset($company_id)) {
             $errInfo = " -[<font color='#FF0000'>导入失败, 关联公司 '{$company}' 不存在!" . $partnerStr . "</font>]";
             echo $errInfo . '<br>';
             continue;
         }
         $data = array('company_name' => $company_name, 'username' => $username, 'type_id' => $type_id, 'tel' => $tel, 'phone' => $phone, 'fax' => $fax, 'QQ' => $QQ, 'AliIM' => $AliIM, 'e_mail' => $e_mail, 'shoplink' => $shoplink, 'city' => $city, 'address' => $address, 'note' => $note, 'status' => $status, 'sms_status' => $sms_status, 'email_status' => $email_status, 'purchaseuser_id' => $purchaser_id, 'company_id' => $company_id);
         //print_r($data);
         $result = PartnerModel::insertRow($data);
         if ($result) {
             $errInfo = " -[<font color='#33CC33'>导入成功, " . $partnerStr . "</font>]";
         } else {
             $errMsg = PartnerModel::$errMsg;
             $errInfo = " -[<font color='#FF0000'>导入失败, " . $partnerStr . $errMsg . "</font>]";
         }
         echo $errInfo . '<br>';
     }
 }