Files
erp-site/application/controllers/basedata/import.php
2026-04-19 01:39:41 +08:00

351 lines
19 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Import extends CI_Controller {
public function __construct(){
parent::__construct();
$this->common_model->checkpurview();
$this->load->helper('download');
$this->load->library('excel/excel_reader2');
$this->load->library('pinyin/getpinyin');
}
public function index() {
$dir = './data/upfile/' . date('Ymd') . '/';
//$path = '/data/upfile/' . date('Ymd') . '/';
$err = json_encode(array('url' => '', 'title' => '', 'state' => '请登录'));
$info = upload('resume_file', $dir);
if (is_array($info) && count($info) > 0) {
//$array = array('url' => $path . $info['file'], 'title' => $path . $info['file'], 'state' => 'SUCCESS');
print_r($info);
die();
} else {
die($err);
}
}
//客户
public function downloadtemplate1() {
$info = read_file('./data/download/customer.xls');
$this->common_model->logs('下载文件名:客户导入_'.date("YmdHis").'.xls');
force_download('客户导入_'.date("YmdHis").'.xls', $info);
}
//供应商
public function downloadtemplate2() {
$info = read_file('./data/download/vendor.xls');
$this->common_model->logs('下载文件名:供应商导入_'.date("YmdHis").'.xls');
force_download('供应商导入_'.date("YmdHis").'.xls', $info);
}
//商品
public function downloadtemplate3() {
$info = read_file('./data/download/goods.xls');
$this->common_model->logs('下载文件名:商品导入_'.date("YmdHis").'.xls');
force_download('商品导入_'.date("YmdHis").'.xls', $info);
}
//价格预警
public function downloadtemplate4() {
$info = read_file('./data/download/preprice.xls');
$this->common_model->logs('下载文件名:价格预警导入_'.date("YmdHis").'.xls');
force_download('价格预警导入_'.date("YmdHis").'.xls', $info);
}
//客户导入
public function findDataImporter() {
$fn = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : false);
print_r($fn);
die();
if ($fn) {
file_put_contents(
'upload/' . $fn,
file_get_contents('php://input')
);
echo "http://119.10.11.187:82/AAAUPIMG/1/uploads/".$fn;
exit();
}
print_r($_FILES);
die();
// $dir = './data/upfile/' . date('Ymd') . '/';
// //$path = '/data/upfile/' . date('Ymd') . '/';
// $err = json_encode(array('url' => '', 'title' => '', 'state' => '请登录'));
// $info = upload('resume_file', $dir);
// if (is_array($info) && count($info) > 0) {
// //$array = array('url' => $path . $info['file'], 'title' => $path . $info['file'], 'state' => 'SUCCESS');
// print_r($info);
// die();
// } else {
// die($err);
// }
die('{"status":200,"msg":"success","data":{"items":[{"id":1294598139109696,"date":"2015-04-25 14:41:35","uploadPath"
:"customer_20150425024011.xls","uploadName":"customer_20150425024011.xls","resultPath":"uploadfiles/88887901
/customer_20150425024011.xls","resultName":"customer_20150425024011.xls","resultInfo":"商品导入完毕。<br/>商
品一共0条数据成功导入0条数据失败0条数据。<br/>供应商导入完毕。<br/>供应商一共0条数据成功导入0条数据失败0条数据。<br/>客户导入完毕。<br/>客户一共10条数
成功导入10条数据失败0条数据。<br/>","status":2,"spendTime":0},{"id":1294598139109659,"date":"2015-04-25 14:40
:49","uploadPath":"customer_20150425024011.xls","uploadName":"customer_20150425024011.xls","resultPath"
:"uploadfiles/88887901/customer_20150425024011.xls","resultName":"customer_20150425024011.xls","resultInfo"
:"商品导入完毕。<br/>商品一共0条数据成功导入0条数据失败0条数据。<br/>供应商导入完毕。<br/>供应商一共0条数据成功导入0条数据失败0条数据。<br/>客户导入完毕
。<br/>客户一共10条数据成功导入10条数据失败0条数据。<br/>","status":2,"spendTime":0},{"id":1294597559113847,"date":"2015-04-17
16:54:39","uploadPath":"蓝港新系统xls.xls","uploadName":"蓝港新系统xls.xls","resultPath":"uploadfiles/88887901
/蓝港新系统xls.xls","resultName":"蓝港新系统xls.xls","resultInfo":"商品导入完毕。<br/>商品一共557条数据成功导入0条数据失败557条数据
。<br/>(请检查模板是否匹配,建议重新下载模板导入)<br/>供应商导入完毕。<br/>供应商一共0条数据成功导入0条数据失败0条数据。<br/>客户导入完毕。<br/>客户一共0条数
成功导入0条数据失败0条数据。<br/>","status":2,"spendTime":0}],"totalsize":3}}');
die('{"status":200,"msg":"success"}');
}
//上传文件
public function upload() {
die('{"status":200,"msg":"success","data":{"items":[{"id":1294598139109696,"date":"2015-04-25 14:41:35","uploadPath"
:"customer_20150425024011.xls","uploadName":"customer_20150425024011.xls","resultPath":"uploadfiles/88887901
/customer_20150425024011.xls","resultName":"customer_20150425024011.xls","resultInfo":"商品导入完毕。<br/>商
品一共0条数据成功导入0条数据失败0条数据。<br/>供应商导入完毕。<br/>供应商一共0条数据成功导入0条数据失败0条数据。<br/>客户导入完毕。<br/>客户一共10条数
成功导入10条数据失败0条数据。<br/>","status":2,"spendTime":0},{"id":1294598139109659,"date":"2015-04-25 14:40
:49","uploadPath":"customer_20150425024011.xls","uploadName":"customer_20150425024011.xls","resultPath"
:"uploadfiles/88887901/customer_20150425024011.xls","resultName":"customer_20150425024011.xls","resultInfo"
:"商品导入完毕。<br/>商品一共0条数据成功导入0条数据失败0条数据。<br/>供应商导入完毕。<br/>供应商一共0条数据成功导入0条数据失败0条数据。<br/>客户导入完毕
。<br/>客户一共10条数据成功导入10条数据失败0条数据。<br/>","status":2,"spendTime":0},{"id":1294597559113847,"date":"2015-04-17
16:54:39","uploadPath":"蓝港新系统xls.xls","uploadName":"蓝港新系统xls.xls","resultPath":"uploadfiles/88887901
/蓝港新系统xls.xls","resultName":"蓝港新系统xls.xls","resultInfo":"商品导入完毕。<br/>商品一共557条数据成功导入0条数据失败557条数据
。<br/>(请检查模板是否匹配,建议重新下载模板导入)<br/>供应商导入完毕。<br/>供应商一共0条数据成功导入0条数据失败0条数据。<br/>客户导入完毕。<br/>客户一共0条数
成功导入0条数据失败0条数据。<br/>","status":2,"spendTime":0}],"totalsize":3}}');
}
public function uploadExcel() {
$path=$_FILES['file'];
if($path['error']!= 0)
str_alert(-1,'文件上传失败!');
if($path['size']>20*1024*1024)
str_alert(-1,'上传文件大小超过限制!');
if($path['type']!='application/vnd.ms-excel' && strrchr($path['name'],'xls')!='xls'){
str_alert(200,'上传的文件不是excel类型');
}
//$filePath = "data/upload/".$path["name"];
//move_uploaded_file($path["tmp_name"],$filePath);
//$reader = new Excel_reader2(); // 实例化解析类Spreadsheet_Excel_Reader
$reader = $this->excel_reader2;
$reader->setOutputEncoding("utf-8"); // 设置编码方式
$reader->read("{$path['tmp_name']}");
$data = $reader->sheets[0]['cells'];
if(!isset($data[2])||!isset($data[2][1]))
str_alert(-1,'无可导入的数据!');
$first = array_shift($data);
$itype = "";
$this->db->trans_begin();
if($first[1]=='商品编号'){
$itype = "商品";
foreach ($data as $arr=>$row) {
if(empty($row[1]))
continue;
$good['number'] = $row[1];
$good['name'] = $row[2];
$good['barCode'] = $row[3];
$good['spec'] = $row[4];
//$good['categoryId'] = 1;
//$good['categoryName'] = $row[5];
//$good['locationId'] = $row[6];
//$good['baseUnitId'] = $row[9];
$good['purPrice'] = floatval($row[10]);
$good['wholesalePrice'] = floatval($row[12]);
$good['salePrice'] = floatval($row[11]);
$good['pinYin'] = $this->getpinyin->getFirstPY($row[2]);
$good['sonGoods'] = '[]';
$good['dopey'] = 0;
empty($row[5])&&str_alert(-1,'商品【'.$row[2].'】类别不能为空!');
empty($row[6])&&str_alert(-1,'商品【'.$row[2].'】仓库不能为空!');
empty($row[9])&&str_alert(-1,'商品【'.$row[2].'】计量单位不能为空!');
$list = $this->mysql_model->get_rows('storage',array('isDelete'=>0,'name'=>$row[6]));
if (count($list) > 0) {
$good['locationId']= $list['id'];
$good['locationName']= $row[6];
}else
str_alert(-1,'仓库【'.$row[6].'】不存在,请先添加仓库后再导入!');
$list = $this->mysql_model->get_rows('category',array('name'=>$row[5],'typeNumber'=>'trade'));
if (count($list) > 0) {
$good['categoryId']= $list['id'];
$good['categoryName']= $row[5];
}else{
str_alert(-1,'商品类别【'.$row[5].'】不存在,请先添加商品类别再导入!');
}
$list = $this->mysql_model->get_rows('unit',array('name'=>$row[9]));
if (count($list) > 0) {
$good['baseUnitId']= $list['id'];
$good['unitName']= $row[9];
}else{
str_alert(-1,'计量单位【'.$row[9].'】不存在,请先添加计量单位再导入!');
}
$info = array(
'number','name','barCode','spec','categoryId','locationId','baseUnitId','purPrice','salePrice',
'locationName','unitName','categoryName','pinYin','sonGoods','dopey','wholesalePrice'
);
$info = elements($info,$good,NULL);
$existgood = $this->mysql_model->get_rows('goods',array('isDelete'=>0,'number'=>$good['number']));
// if($this->mysql_model->get_count('goods',array('isDelete'=>0,'number'=>$good['number'])) <= 0){
if(count($existgood) <= 0){
$rtn['id'] = $this->mysql_model->insert('goods',$info);
}else {
$rtn['id'] = $existgood['id'];
$this->mysql_model->delete('invoice_info',array('billType'=>'INI','invId'=>$rtn['id']));
$this->mysql_model->update('goods',$info,array('number'=>$good['number']));
}
if(!empty($row[19])){
$list = $this->mysql_model->get_rows('storage',array('isDelete'=>0,'name'=>$row[19]));
if (count($list) > 0) {
$arr = 0;
$v[$arr]['invId'] = $rtn['id'];
$v[$arr]['locationId'] = $list['id'];
$v[$arr]['qty'] = floatval($row[21]);
$v[$arr]['price'] = floatval($row[22]);
$v[$arr]['amount'] = floatval($row[21])*floatval($row[22]);
$v[$arr]['skuId'] = 0;
$v[$arr]['billDate'] = date('Y-m-d');;
$v[$arr]['billNo'] = '期初数量';
$v[$arr]['billType'] = 'INI';
$v[$arr]['transTypeName'] = '期初数量';
if (isset($v)) {
$this->mysql_model->insert('invoice_info',$v);
}
}
}
}
}else if($first[1]=='客户编号'){
$itype = "客户";
foreach ($data as $arr=>$row) {
if(empty($row[1]))
continue;//add by michen 20170917 for
$cust['number'] = strval($row[1]);
$cust['name'] = $row[2];
//$cust['cCategory'] = $row[3];
//$cust['cCategoryName'] = $row[4];
$cust['cLevel'] = 0;
$cust['remark'] = $row[8];
$linkMan['linkName'] = $row[9];
$linkMan['linkMobile'] = $row[10];
$linkMan['linkPhone'] = $row[11];
$linkMan['linkPlace'] = $row[12];
$linkMan['linkIm'] = $row[13];
$linkMan['address'] = $row[14];
$linkMan['linkFirst'] = 1;
$linkMan['id'] = 0;
$linkMans[0] = $linkMan;
$cust['linkMans'] = json_encode($linkMans);
$cust['type'] = -10;
empty($row[1])&&str_alert(-1,'第'.$arr.'行客户编号不能为空!');
empty($row[2])&&str_alert(-1,'第'.$arr.'行客户名称不能为空!');
empty($row[3])&&str_alert(-1,'第'.$arr.'行客户类别不能为空!');
$list = $this->mysql_model->get_rows('category',array('name'=>$row[3],'typeNumber'=>'customertype'));
if (count($list) > 0) {
$cust['cCategory']= $list['id'];
$cust['cCategoryName']= $row[3];
}else{
str_alert(-1,'第'.$arr.'行客户类别【'.$row[3].'】不存在,请先添加客户类别再导入!');
}
$info = array(
'number','name','cLevel','remark','cCategory','cCategoryName','linkMans','type'
);
$info = elements($info,$cust,NULL);
//die(var_export($cust,true));
if($this->mysql_model->get_count('contact',array('isDelete'=>0,'number'=>$cust['number'],'type'=>-10)) <= 0){
$rtn['id'] = $this->mysql_model->insert('contact',$info);
}else {
$this->mysql_model->update('contact',$info,array('number'=>$cust['number'],'type'=>-10));
}
}
}else if($first[1]=='供应商编号'){
$itype = "供应商";
foreach ($data as $arr=>$row) {
if(empty($row[1]))
continue;
$sup['number'] = strval($row[1]);
$sup['name'] = $row[2];
//$cust['cCategory'] = $row[3];
//$cust['cCategoryName'] = $row[4];
$sup['cLevel'] = 0;
$sup['remark'] = $row[7];
$linkMan['linkName'] = $row[8];
$linkMan['linkMobile'] = $row[9];
$linkMan['linkPhone'] = $row[10];
$linkMan['linkPlace'] = $row[11];
$linkMan['linkIm'] = $row[12];
$linkMan['address'] = $row[13];
$linkMan['linkFirst'] = 1;
$linkMan['id'] = 0;
$linkMans[0] = $linkMan;
$sup['linkMans'] = json_encode($linkMans);
$sup['type'] = 10;
empty($row[1])&&str_alert(-1,'第'.$arr.'行供应商编号不能为空!');
empty($row[2])&&str_alert(-1,'第'.$arr.'行供应商名称不能为空!');
empty($row[3])&&str_alert(-1,'第'.$arr.'行供应商类别不能为空!');
$list = $this->mysql_model->get_rows('category',array('name'=>$row[3],'typeNumber'=>'supplytype'));
if (count($list) > 0) {
$sup['cCategory']= $list['id'];
$sup['cCategoryName']= $row[3];
}else{
str_alert(-1,'第'.$arr.'行供应商类别【'.$row[3].'】不存在,请先添加供应商类别再导入!');
}
$info = array(
'number','name','cLevel','remark','cCategory','cCategoryName','linkMans','type'
);
$info = elements($info,$sup,NULL);
if($this->mysql_model->get_count('contact',array('isDelete'=>0,'number'=>$sup['number'],'type'=>10)) <= 0){
$rtn['id'] = $this->mysql_model->insert('contact',$info);
}else {
$this->mysql_model->update('contact',$info,array('number'=>$sup['number'],'type'=>10));
}
}
}else if($first[1]=='商品编码'){
$itype = "价格预警";
foreach ($data as $arr=>$row) {
if(empty($row[1]))
continue;
$preprice['number'] = trim(strval($row[1]));
$preprice['supplier'] = trim(strval($row[2]));
$preprice['price'] = floatval($row[3]);
empty($row[1])&&str_alert(-1,'第'.$arr.'行商品编码不能为空!');
empty($row[2])&&str_alert(-1,'第'.$arr.'行供应商编码不能为空!');
empty($row[3])&&str_alert(-1,'第'.$arr.'行价格不能为空!');
$list = $this->mysql_model->get_rows('goods',array('number'=>$preprice['number'],'isDelete'=>0));
if (count($list) > 0) {
$preprice['invId'] = $list['id'];
}else{
str_alert(-1,'第'.$arr.'行商品编码【'.$row[1].'】不存在,请先添加商品再导入!');
}
$list = $this->mysql_model->get_rows('contact',array('number'=>$preprice['supplier'],'isDelete'=>0,'type'=>10));
if (count($list) > 0) {
$preprice['buId'] = $list['id'];
}else{
str_alert(-1,'第'.$arr.'行供应商编码【'.$row[2].'】不存在,请先添加供应商再导入!');
}
$preprice['billDate'] = date('Y-m-d');
$info = array(
'invId','buId','price','billDate'
);
$info = elements($info,$preprice,NULL);
$rtn['id'] = $this->mysql_model->insert('preprice',$info);
}
}
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
str_alert(-1,'SQL错误回滚');
} else {
$this->db->trans_commit();
str_alert(200,'恭喜您,导入'.$itype.'信息成功!');
}
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */