app->model('branch');
$branch=$oBranch->getList('branch_id,name');
foreach($branch as $k=>$v){
$tmp[] = $v;
}
$this->pagedata['branch']=$tmp;
$this->display("admin/stock/importfile.html");
}
/*以CSV导出*/
function gettypecsv(){
$filename = time();
$oExport= $this->app->model('system/export');
$branch=$_POST['branch'];
$str_array[]= array($branch,'bn','name','store','sku_property','weight','store_position');
$oExport->export_begin($str_array,'csv',$count);
$oExport->export_finish();
}
/*
* 以CSV文件导入商品库存
*
*/
function import(){
$oProducts=$this->app->model('products');//已不会调用该表和这段代码pdts,废弃 xiayuanjun
$oBranch= $this->app->model('branch');
$oPos = $this->app->model('branch_pos');
$time=time();
if(substr($_FILES['upload1']['name'],-4)!='.csv'){
echo "";
exit;
}
$tmp = $_FILES['upload1']['tmp_name'];
$content1 = file_get_contents($_FILES['upload1']['tmp_name']);
if(substr($content1,0,3)=="\xEF\xBB\xBF"){
$content1 = substr($content1,3); //去BOM头
}
$content1=mb_convert_encoding($content1,'UTF-8','GB2312');
$handle1 = fopen('storedata','wb');//订单信息文件
fwrite($handle1,$content1);
fclose($handle1);
$handle = fopen('storedata','r');
$i=0;
while($row1 = fgetcsv($handle, 1000, ",")){
$i++;
if($i == 1){
$branch_id = $row1[0];
$branch = $oBranch->dump($branch_id);
if(empty($branch)){
echo "";
exit;
}
echo "";
if($row1[1] != 'bn' || $row1[2] !='name' || $row1[3] !='store' || $row1[4] !='sku_property' || $row1[5] !='weight' || $row1[6]!='store_position'){
echo "";
exit;
}
continue;
}
$pos_id = $row1[6];
$bn = $row1[1];
$pro_s = array('bn'=>$bn);
$product = $oProducts->dump(array('bn'=>$bn),'product_id');
if(empty($product)){
echo "";
exit;
}
$pos = $oPos->dump(array('branch_id'=>$branch_id,'store_position'=>$pos_id),'*');
if(empty($pos)){
echo "";
exit;
}
}
$handle2 = fopen('storedata','r');
$j=0;
while($row = fgetcsv($handle2, 1000, ",")){
$j++;
if($j== 1){
$branch_id = $row[0];
continue;
}
$product_data=array('bn'=>$row[1],'name'=>$row[2],'store'=>$row[3],'sku_property'=>$row[4],'weight'=>$row[5],'branch_id'=>$branch_id,'type'=>$_POST['type'],'store_position'=>trim($row[6]));
$oProducts->import_product($product_data);
}
echo "";
}
}
?>