load->library('session'); $this->load->_model('Model_warehousefactory','warehousefactory'); $this->load->_model('Model_warehousefactoryout','warehousefactoryout'); $this->load->_model('Model_excel','excel'); $this->load->_model('Model_typeclass','typeclass'); } //定义方法的调用规则 获取URI第二段值 public function _remap($arg,$arg_array) { if($arg == 'add')//添加 { $this->_add(); } else if($arg == 'edit')//修改 { $this->_edit($arg_array); } else if($arg == 'editnum')//修改 { $this->_editnum($arg_array); } else if($arg == 'ck')//修改 { $this->_ck($arg_array); } else if($arg == 'del')//修改 { $this->_del(); } else if($arg == 'enter') { $this->_enter($arg_array); } else if($arg == 'out') { $this->_out($arg_array); } else if($arg == 'outxq') { $this->_outxq($arg_array); } else if($arg == 'presetedit')//修改 { $this->_presetedit($arg_array); } else if($arg == 'dcexcel') { $this->_dcexcel(); } else if($arg == 'drmb') { $this->_drmb(); } else if($arg == 'pd') { $this->_pd(); } else if($arg == 'outexcel') { $this->_outexcel($arg_array); } else if($arg == 'ckmb') { $this->_ckmb(); } else { $this->_index(); } } //管理 public function _index() { $t = array(); $typeclass = $this->typeclass->find_all(); foreach ($typeclass as $v) { $t[$v['id']] = $v['title']; } $post = $this->input->post(NULL, TRUE); if(isset($post['page'])) { $page = $this->input->post('page',true); $perpage = $this->input->post('perpage',true); $warehouse = $this->input->post('warehouse',true); $sku = $this->input->post('sku',true); $title = $this->input->post('title',true); $lowe = $this->input->post('lowe',true); $color = $this->input->post('color',true); $size = $this->input->post('size',true); $specs = $this->input->post('specs',true); $tax = $this->input->post('tax',true); $contract = $this->input->post('contract',true); $ktime = $this->input->post('ktime',true); $jtime = $this->input->post('jtime',true); $ktime = strtotime($ktime); $jtime = strtotime($jtime); $where = "1=1 "; //数据排序 $order_str = "id desc"; if(empty($page)) { $start = 0; $perpage = 1; } else { $start = ($page - 1)*$perpage; } if($sku) { $where .= " and sku like '%$sku%'"; } if($title) { $where .= " and title like '%$title%'"; } if($lowe) { $where .= " and lowe like '%$lowe%'"; } if($color) { $where .= " and color like '%$color%'"; } if($size) { $where .= " and size like '%$size%'"; } if($specs) { $where .= " and specs like '%$specs%'"; } if($tax) { $where .= " and tax = '$tax'"; } if($contract) { $where .= " and contract = '$contract'"; } //取得信息列表 $info_list = $this->warehousefactory->find_pc($where,'warehouse,sku,lowe,color,size,specs','id,warehouse,sku,title,lowe,color,size,specs,synum,price',$order_str,$start,$perpage); //格式化数据 foreach ($info_list as $key=>$value) { if($value['warehouse'] == '1') { $info_list[$key]['warehouse'] = '原材料仓'; } else if($value['warehouse'] == '2') { $info_list[$key]['warehouse'] = '半成品仓'; } else if($value['warehouse'] == '3') { $info_list[$key]['warehouse'] = '成品仓'; } else if($value['warehouse'] == '4') { $info_list[$key]['warehouse'] = '辅料仓'; } else if($value['warehouse'] == '5') { $info_list[$key]['warehouse'] = '普通仓'; } $info_list[$key]['lowe'] = $t[$value['lowe']]; $info_list[$key]['color'] = $t[$value['color']]; $info_list[$key]['size'] = $t[$value['size']]; $m = 0; $r = $this->warehousefactory->find_all($where." and sku = '".$value['sku']."' and warehouse = '".$value['warehouse']."' and lowe = '".$value['lowe']."' and color = '".$value['color']."' and size = '".$value['size']."' and specs = '".$value['specs']."' and review = '2'"); foreach ($r as $v) { $m += $v['synum']*$v['price']; } $c = $this->warehousefactoryout->find_all($where." and sku = '".$value['sku']."' and warehouse = '".$value['warehouse']."' and lowe = '".$value['lowe']."' and color = '".$value['color']."' and size = '".$value['size']."' and specs = '".$value['specs']."' and review = '2'"); $info_list[$key]['synum'] = array_sum(array_column($r,'num')); $info_list[$key]['price'] = array_sum(array_column($c,'num')); $info_list[$key]['jy'] = sprintf("%01.2f",$info_list[$key]['synum'] - $info_list[$key]['price']); $synum = array_sum(array_column($r,'synum')); if($synum > 0) { $info_list[$key]['jj'] = sprintf("%01.2f",$m/$synum); } else { $info_list[$key]['jj'] = 0; } $info_list[$key]['syzj'] = sprintf("%01.2f",$m); } $total = $this->warehousefactory->find_count($where,'warehouse,sku,lowe,color,size,specs','id,warehouse,sku,title,lowe,color,size,specs'); $pagenum = ceil($total/$perpage); $over = $total-($start+$perpage); $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list)); echo json_encode($rows);exit; } $this->_Template('warehousefactory',$this->data); } public function _enter($arg_array) { $t = array(); $typeclass = $this->typeclass->find_all(); foreach ($typeclass as $v) { $t[$v['id']] = $v['title']; } $post = $this->input->post(NULL, TRUE); if(isset($post['page'])) { $page = $this->input->post('page',true); $perpage = $this->input->post('perpage',true); $warehouse = $this->input->post('warehouse',true); $entertype = $this->input->post('entertype',true); $sku = $this->input->post('sku',true); $title = $this->input->post('title',true); $agent = $this->input->post('agent',true); $supply = $this->input->post('supply',true); $enterorder = $this->input->post('enterorder',true); $tax = $this->input->post('tax',true); $contract = $this->input->post('contract',true); $ktime = $this->input->post('ktime',true); $jtime = $this->input->post('jtime',true); $ktime = strtotime($ktime); $jtime = strtotime($jtime); $where = "1=1"; //数据排序 $order_str = "id desc"; if(empty($page)) { $start = 0; $perpage = 1; } else { $start = ($page - 1)*$perpage; } if($sku) { $where .= " and sku like '%$sku%'"; } if($title) { $where .= " and title like '%$title%'"; } if($agent) { $where .= " and agent like '%$agent%'"; } if($supply) { $where .= " and supply like '%$supply%'"; } if($enterorder) { $where .= " and enterorder like '%$enterorder%'"; } if($warehouse) { $where .= " and warehouse = '$warehouse'"; } if($entertype) { $where .= " and entertype = '$entertype'"; } if($tax) { $where .= " and tax = '$tax'"; } if($contract) { $where .= " and contract = '$contract'"; } if($ktime && $jtime) { $where .= " and rtime > '$ktime' and rtime < '$jtime'"; } //取得信息列表 $info_list = $this->warehousefactory->find_all($where,'id,warehouse,entertype,enterorder,contract,sku,title,lowe,color,size,specs,num,money,rtime',$order_str,$start,$perpage); $sl = 0;$jg = 0; //格式化数据 foreach ($info_list as $key=>$value) { if($value['warehouse'] == '1') { $info_list[$key]['warehouse'] = '原材料仓'; } else if($value['warehouse'] == '2') { $info_list[$key]['warehouse'] = '半成品仓'; } else if($value['warehouse'] == '3') { $info_list[$key]['warehouse'] = '成品仓'; } else if($value['warehouse'] == '4') { $info_list[$key]['warehouse'] = '辅料仓'; } else if($value['warehouse'] == '5') { $info_list[$key]['warehouse'] = '普通仓'; } if($value['entertype'] == '1') { $info_list[$key]['entertype'] = '采购入库单'; } else if($value['entertype'] == '2') { $info_list[$key]['entertype'] = '生产入库单'; } else if($value['entertype'] == '3') { $info_list[$key]['entertype'] = '其他入库单'; } $info_list[$key]['lowe'] = $t[$value['lowe']]; $info_list[$key]['color'] = $t[$value['color']]; $info_list[$key]['size'] = $t[$value['size']]; $info_list[$key]['rtime'] = date('Y-m-d H:i',$value['rtime']); $sl += $value['num']; $jg += $value['money']; } $total = $this->warehousefactory->find_count($where); $pagenum = ceil($total/$perpage); $over = $total-($start+$perpage); $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list),'wf'=>(array(sprintf("%01.2f",$sl),sprintf("%01.2f",$jg)))); echo json_encode($rows);exit; } $this->_Template('warehousefactory_enter',$this->data); } public function _out($arg_array) { $t = array(); $typeclass = $this->typeclass->find_all(); foreach ($typeclass as $v) { $t[$v['id']] = $v['title']; } $post = $this->input->post(NULL, TRUE); if(isset($post['page'])) { $page = $this->input->post('page',true); $perpage = $this->input->post('perpage',true); $warehouse = $this->input->post('warehouse',true); $outtype = $this->input->post('outtype',true); $sku = $this->input->post('sku',true); $title = $this->input->post('title',true); $cagent = $this->input->post('cagent',true); $department = $this->input->post('department',true); $customer = $this->input->post('customer',true); $outorder = $this->input->post('outorder',true); $tax = $this->input->post('tax',true); $contract = $this->input->post('contract',true); $ktime = $this->input->post('ktime',true); $jtime = $this->input->post('jtime',true); $ktime = strtotime($ktime); $jtime = strtotime($jtime); $where = "1=1"; //数据排序 $order_str = "id desc"; if(empty($page)) { $start = 0; $perpage = 1; } else { $start = ($page - 1)*$perpage; } if($sku) { $where .= " and sku like '%$sku%'"; } if($title) { $where .= " and title like '%$title%'"; } if($cagent) { $where .= " and cagent like '%$cagent%'"; } if($department) { $where .= " and department like '%$department%'"; } if($customer) { $where .= " and customer like '%$customer%'"; } if($outorder) { $where .= " and outorder like '%$outorder%'"; } if($warehouse) { $where .= " and warehouse = '$warehouse'"; } if($outtype) { $where .= " and outtype = '$outtype'"; } if($tax) { $where .= " and tax = '$tax'"; } if($contract) { $where .= " and contract = '$contract'"; } if($ktime && $jtime) { $where .= " and ctime > '$ktime' and ctime < '$jtime'"; } //取得信息列表 $info_list = $this->warehousefactoryout->find_all($where,'id,warehouse,outtype,outorder,sku,title,lowe,color,size,specs,num,jj,ctime',$order_str,$start,$perpage); //格式化数据 foreach ($info_list as $key=>$value) { if($value['warehouse'] == '1') { $info_list[$key]['warehouse'] = '原材料仓'; } else if($value['warehouse'] == '2') { $info_list[$key]['warehouse'] = '半成品仓'; } else if($value['warehouse'] == '3') { $info_list[$key]['warehouse'] = '成品仓'; } else if($value['warehouse'] == '4') { $info_list[$key]['warehouse'] = '辅料仓'; } else if($value['warehouse'] == '5') { $info_list[$key]['warehouse'] = '普通仓'; } if($value['outtype'] == '1') { $info_list[$key]['outtype'] = '材料出库单'; } else if($value['outtype'] == '2') { $info_list[$key]['outtype'] = '销售出库单'; } else if($value['outtype'] == '3') { $info_list[$key]['outtype'] = '其他出库单'; } $info_list[$key]['lowe'] = $t[$value['lowe']]; $info_list[$key]['color'] = $t[$value['color']]; $info_list[$key]['size'] = $t[$value['size']]; $info_list[$key]['ctime'] = date('Y-m-d H:i',$value['ctime']); } $total = $this->warehousefactoryout->find_count($where); $pagenum = ceil($total/$perpage); $over = $total-($start+$perpage); $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list)); echo json_encode($rows);exit; } $this->_Template('warehousefactory_out',$this->data); } public function _outxq($arg_array) { $arg_array = $arg_array[0];$m=0; $warehousefactory = $this->warehousefactoryout->read($arg_array); $r = $this->warehousefactory->find_all("sku = '".$warehousefactory['sku']."' and warehouse = '".$warehousefactory['warehouse']."' and lowe = '".$warehousefactory['lowe']."' and color = '".$warehousefactory['color']."' and size = '".$warehousefactory['size']."' and specs = '".$warehousefactory['specs']."' and review = '2'"); foreach ($r as $v) { if($v['synum'] > 0) { $m += $v['synum']*$v['price']; } } $c = $this->warehousefactoryout->find_all("sku = '".$warehousefactory['sku']."' and warehouse = '".$warehousefactory['warehouse']."' and lowe = '".$warehousefactory['lowe']."' and color = '".$warehousefactory['color']."' and size = '".$warehousefactory['size']."' and specs = '".$warehousefactory['specs']."' and review = '2'"); $warehousefactory['r'] = array_sum(array_column($r,'num')); $warehousefactory['c'] = array_sum(array_column($c,'num')); $warehousefactory['j'] = array_sum(array_column($r,'synum')); $this->data['warehousefactory'] = $warehousefactory; $this->_Template('warehousefactory_outxq',$this->data); } //添加 public function _add() { $post = $this->input->post(NULL, TRUE); if(isset($post['sku'])) { $time = time(); $num = $this->input->post('num',true); if($num < 1) { echo json_encode(array('msg'=>'添加数量不正确!','success'=>false));exit; } $post['time'] = $time; $post['rtime'] = $time; $post['grtime'] = date('Y-m-d',$time); $post['review'] = 2;//需要审核改为1 $post['synum'] = $post['num']; if (!$this->warehousefactory->insert($post)) { echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit; } else { echo json_encode(array('msg'=>'添加成功','success'=>true));exit; } } $this->data['order'] = time().rand(10,99); $this->_Template('warehousefactory_add',$this->data); } public function _edit($arg_array) { $post = $this->input->post(NULL, TRUE); if(isset($post['id'])) { $time = time(); $id = $this->input->post('id',true); $d = $this->warehousefactory->read($id); if($post['num'] < 1 || $d['synum']-($d['num']-$post['num']) < 0) { echo json_encode(array('msg'=>'可用数量小于1或剩余数量不足扣减','success'=>false));exit; } if($d['synum'] != $d['num']) { echo json_encode(array('msg'=>'已有出库记录,无法修改!','success'=>false));exit; } if($post['num'] > $d['num']) { $post['synum'] = $post['num']-$d['num']+$d['synum']; } else { $post['synum'] = $d['synum']-($d['num']-$post['num']); } if (!$this->warehousefactory->save($post,$id)) { echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit; } else { echo json_encode(array('msg'=>'操作成功','success'=>true));exit; } } $arg_array = $arg_array[0]; $warehousefactory = $this->warehousefactory->read($arg_array); $this->data['warehousefactory'] = $warehousefactory; $this->_Template('warehousefactory_edit',$this->data); } public function _ck($arg_array) { $post = $this->input->post(NULL, TRUE); if(isset($post['id'])) { $id = $this->input->post('id',true); $wyl = $this->warehousefactory->read($id); $warehousefactory = $this->warehousefactory->find_all("sku = '".$wyl['sku']."' and warehouse = '".$wyl['warehouse']."' and lowe = '".$wyl['lowe']."' and color = '".$wyl['color']."' and size = '".$wyl['size']."' and specs = '".$wyl['specs']."' and review = '2' and state = '0'"); $time = time(); $num = $this->input->post('num',true); $k = 0;$sl = 0;$money = 0;$x = 0; foreach ($warehousefactory as $v) { $k++; if($v['synum'] != 0) { $sl += $v['synum']; $money += $v['synum']*$v['price']; } if($x == '0' && $sl >= $num) { $x = $k; } } if($sl < abs($num)) { echo json_encode(array('msg'=>'扣减数量大于库存剩余数量!','success'=>false));exit; } $post = $warehousefactory[0]; $post['jj'] = sprintf("%01.2f",$money/$sl); $post['num'] = $this->input->post('num',true); $post['outtype'] = $this->input->post('outtype',true); $post['department'] = $this->input->post('department',true); $post['customer'] = $this->input->post('customer',true); $post['cagent'] = $this->input->post('cagent',true); $post['ctext'] = $this->input->post('ctext',true); $post['outorder'] = time().rand(10,99); $post['ctime'] = $time; $post['time'] = $time; $post['gctime'] = date('Ymd',$time); unset($post['id']); $this->db->trans_begin(); $kcjl = ''; for($i=0;$i<$x;$i++) { if($warehousefactory[$i]['synum'] <= $post['num']) { $this->warehousefactory->save(array('synum'=>0),$warehousefactory[$i]['id']); $post['num'] = $post['num'] - $warehousefactory[$i]['synum']; $kcjl .= $warehousefactory[$i]['id'].'-'.$warehousefactory[$i]['synum'].'|'; } else { $this->warehousefactory->save(array('synum'=>$warehousefactory[$i]['synum']-$post['num']),$warehousefactory[$i]['id']); $kcjl .= $warehousefactory[$i]['id'].'-'.$post['num'].'|'; } } $post['kcjl'] = $kcjl; $this->warehousefactoryout->insert($post); if ($this->db->trans_status() === TRUE) { $this->db->trans_commit(); echo json_encode(array('msg'=>'操作成功','success'=>true));exit; } else { $this->db->trans_rollback(); echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit; } } $m = 0; $arg_array = $arg_array[0]; $warehousefactory = $this->warehousefactory->read($arg_array); $r = $this->warehousefactory->find_all("sku = '".$warehousefactory['sku']."' and warehouse = '".$warehousefactory['warehouse']."' and lowe = '".$warehousefactory['lowe']."' and color = '".$warehousefactory['color']."' and size = '".$warehousefactory['size']."' and specs = '".$warehousefactory['specs']."' and review = '2'"); foreach ($r as $v) { if($v['synum'] > 0) { $m += $v['synum']*$v['price']; } } $c = $this->warehousefactoryout->find_all("sku = '".$warehousefactory['sku']."' and warehouse = '".$warehousefactory['warehouse']."' and lowe = '".$warehousefactory['lowe']."' and color = '".$warehousefactory['color']."' and size = '".$warehousefactory['size']."' and specs = '".$warehousefactory['specs']."' and review = '2'"); $warehousefactory['r'] = array_sum(array_column($r,'num')); $warehousefactory['c'] = array_sum(array_column($c,'num')); $warehousefactory['j'] = array_sum(array_column($r,'synum')); $warehousefactory['jj'] = sprintf("%01.2f",$m/$warehousefactory['j']); $this->data['warehousefactory'] = $warehousefactory; $this->_Template('warehousefactory_ck',$this->data); } //删除 public function _del() { $post = $this->input->post(NULL, TRUE); if(isset($post['s'])) { $id_arr = $this->input->post('s'); $id_arr = explode(',',$id_arr); if(!$id_arr) { echo json_encode(array('msg'=>'参数错误!','success'=>false));exit; } //循环删除记录 foreach ($id_arr as $v) { $d = $this->warehousefactory->read($v); if($d['num'] == $d['synum'])//只能删除未使用的库存 { $this->warehousefactory->remove($v); } } echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true)); } } public function _outdel() { $post = $this->input->post(NULL, TRUE); if(isset($post['s'])) { $id_arr = $this->input->post('s'); $id_arr = explode(',',$id_arr); if(!$id_arr) { echo json_encode(array('msg'=>'参数错误!','success'=>false));exit; } $this->db->trans_begin(); foreach ($id_arr as $v) { $d = $this->warehousefactoryout->read($v); $data = explode('|',rtrim($d['kcjl'],'|')); if(count($data) > 0) { foreach ($data as $vv) { $u = explode('-',$vv); $y = $this->warehousefactory->read($u[0]); if($y) { $this->warehousefactory->save(array('synum'=>$y['synum']+$u[1]),$u[0]); } } } $this->warehousefactory->remove($v); } if ($this->db->trans_status() === TRUE) { $this->db->trans_commit(); echo json_encode(array('msg'=>'操作成功','success'=>true));exit; } else { $this->db->trans_rollback(); echo json_encode(array('msg'=>'操作失败,请重试','success'=>false));exit; } } } public function _presetedit($arg_array) { $t = array();$bs = array(); $typeclass = $this->typeclass->find_all(); foreach ($typeclass as $v) { $t[$v['title']] = $v['id']; $bs[$v['bqsku']] = $v['id']; } $dir = '/data/excel/'.date('Ymd',time()).'/'; $config['upload_path'] = '.'.$dir ; $config['file_name'] = date('Ymd_His_',time()).rand(1000,9999); $config['allowed_types'] = 'xls|xlsx|csv'; $config['max_size'] = 10240; $this->load->library('upload', $config); $this->upload->initialize($config); if ($this->upload->do_upload('userfile')) { $full_path = $dir.$this->upload->data('file_name'); $fileName = '.' . $full_path; if (!file_exists($fileName)) { echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit; } else { libxml_use_internal_errors(true); require_once "./data/excel/PHPExcel/IOFactory.php"; $phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件 $phpExcel->setActiveSheetIndex(0);// 设置为默认表 $sheetCount = $phpExcel->getSheetCount();// 获取表格数量 $row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数 $column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数 ++$column;//如果列数大于26行 $list = array(); for ($i = 2; $i <= $row; $i++) // 行数循环 { $data = array(); for ($c = 'A'; $c != $column; $c++) // 列数循环 { $data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue(); } $list[] = $data; } } $i = 0;$j = 0;$ed = array();$x=0;$cs = ''; foreach ($list as $key=>$value) { $h = $key+2; $x++; $time = time(); if($value['1'] == "" || $value['1'] == null) { continue; } $value8=''; if(preg_match('/\d/',$value['8'])) { if(stripos($value['8'],'以上') !== false) { for($i=0;$iwarehousefactory->get_sku($sku); if($nbgl == '' || $bgd == '' || $hl == '' || $bz == '') { $ed[] = array('第'.$h.'行添加失败,请查看是否缺:内部关联号、少报关单号、汇率、币种'); $j++; continue; } if($num > 0) { $post['state'] = 0; $post['warehouse'] = $warehouse; $post['entertype'] = $entertype; $post['sku'] = $sku; $post['title'] = $title; $post['agent'] = $agent; $post['supply'] = $supply; $post['lowe']= $lowe; $post['color'] = $color; $post['size']= $size; $post['specs'] = $specs; $post['company'] = $company; $post['num']= $num; $post['synum']= $num; $post['yprice'] = $yprice; $post['ymoney'] = $ymoney; $post['price'] = $price; $post['money'] = $money; $post['tax'] = $tax; $post['contract'] = $contract; $post['nbgl'] = $nbgl; $post['bgd'] = $bgd; $post['hl'] = $hl; $post['bz'] = $bz; $post['text'] = $text; $post['enterorder'] = $enterorder; $post['rtime'] =$time+$x; $post['grtime'] = date('Ymd',$time+$x); $post['time'] = $time+$x; $post['review'] = 2;//需要审核改为1 $this->warehousefactory->insert($post); } //防止入库时间一样,需要延迟1秒 } if($j > 0) { $tt = date('Ymd',time()); $title = '导入错误信息-'.$tt; $titlename = "
错误详情
"; $tail = "\n"; $filename = $title.".xls"; $ecl = $this->excel->get_fz3($ed,$titlename,$filename,$tail); $dir = '/data/excel/'.$time.'/'; $file_name = 'error_'.$time.rand(1000,9999); if(!is_dir('.'.$dir))mkdir('.'.$dir,0777); $myfile = fopen(".".$dir.$file_name.".xls", "w") or die(); fwrite($myfile,$ecl); fclose($myfile); $error = $dir.$file_name.'.xls'; echo json_encode(array('msg'=>'导入成功,'.$j.'条异常,','error'=>$error,'success'=>true));exit; } else { echo json_encode(array('msg'=>'导入成功!','error'=>1,'success'=>true));exit; } } } public function _outexcel($arg_array) { $dir = '/data/excel/'.date('Ymd',time()).'/'; $config['upload_path'] = '.'.$dir ; $config['file_name'] = date('Ymd_His_',time()).rand(1000,9999); $config['allowed_types'] = 'xls|xlsx|csv'; $config['max_size'] = 10240; $this->load->library('upload', $config); $this->upload->initialize($config); if ($this->upload->do_upload('userfile')) { $full_path = $dir.$this->upload->data('file_name'); $fileName = '.' . $full_path; if (!file_exists($fileName)) { echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit; } else { libxml_use_internal_errors(true); require_once "./data/excel/PHPExcel/IOFactory.php"; $phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件 $phpExcel->setActiveSheetIndex(0);// 设置为默认表 $sheetCount = $phpExcel->getSheetCount();// 获取表格数量 $row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数 $column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数 ++$column;//如果列数大于26行 $list = array(); for ($i = 2; $i <= $row; $i++) // 行数循环 { $data = array(); for ($c = 'A'; $c != $column; $c++) // 列数循环 { $data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue(); } $list[] = $data; } } $i = 0;$j = 0;$ed = array();$x=0; foreach ($list as $key=>$value) { $x++; $time = time(); if($value['0'] == "") { continue; } $outdata['warehouse'] = $value['0']; $outdata['sku'] = $value['1']; $outdata['outtype'] = $value['2']; $outdata['department'] = $value['3']; $outdata['customer'] = $value['4']; $outdata['cagent'] = $value['5']; $outdata['num'] = $value['6']; $outdata['ctext'] = $value['7']; $outdata['outorder'] = time().rand(10,999); $outdata['ctime'] = $time; $outdata['time'] = $time; $outdata['gctime'] = date('Ymd',$time); $outdata['state'] = 1; if($outdata['warehouse'] == '' || $outdata['sku'] == '' || $outdata['outtype'] == '' || $outdata['department'] == '' || $outdata['customer'] == '' || $outdata['cagent'] == '' || $outdata['num'] == '' || $outdata['ctext'] == '') { $ed[] = array($outdata['sku'].'信息不完整'); $j++; continue; } $data = $this->warehousefactory->find_all('sku = "'.$outdata['sku'].'" and warehouse = "'.$outdata['warehouse'].'" and state = "0" and synum != "0" and review = "2"','*','id asc'); if(!$data)//如果没有这个SKU { $ed[] = array($outdata['sku'].'-库存中不存在此编码'); $j++; continue; } $sl = 0;$money = 0;$k=0;$x=0; foreach ($data as $v) { $k++; if($v['synum'] != 0) { $sl += $v['synum']; $money += $v['synum']*$v['price']; } if($x == '0' && $sl >= $outdata['num']) { $x = $k; } } if($sl < abs($outdata['num'])) { $ed[] = array($outdata['sku'].'-扣减数量大于库存剩余数量!'); $j++; continue; } $outdata = $data[0]; $outdata['jj'] = sprintf("%01.2f",$money/$sl); $outdata['outtype'] = $value['2']; $outdata['department'] = $value['3']; $outdata['customer'] = $value['4']; $outdata['cagent'] = $value['5']; $outdata['num'] = $value['6']; $outdata['ctext'] = $value['7']; $outdata['outorder'] = time().rand(10,999); $outdata['ctime'] = $time; $outdata['time'] = $time; $outdata['gctime'] = date('Ymd',$time); $outdata['state'] = 1; unset($outdata['id']); $this->db->trans_begin(); $kcjl = ''; $this->warehousefactoryout->insert($outdata); for($i=0;$i<$x;$i++) { if($data[$i]['synum'] <= $outdata['num']) { $this->warehousefactory->save(array('synum'=>0),$data[$i]['id']); $outdata['num'] = $outdata['num'] - $data[$i]['synum']; $kcjl .= $data[$i]['id'].'-'.$data[$i]['synum'].'|'; } else { $this->warehousefactory->save(array('synum'=>$data[$i]['synum']-$outdata['num']),$data[$i]['id']); $kcjl .= $data[$i]['id'].'-'.$outdata['num'].'|'; } } $outdata['kcjl'] = $kcjl; $this->warehousefactoryout->insert($outdata); if ($this->db->trans_status() === TRUE) { $this->db->trans_commit(); } else { $this->db->trans_rollback(); $ed[] = array($outdata['sku'].'-操作失败,需要重试!'); $j++; } sleep(1);//防止入库时间一样,需要延迟1秒 } if($j > 0) { $tt = date('Ymd',time()); $title = '导入错误信息-'.$tt; $titlename = "
错误详情
"; $tail = "\n"; $filename = $title.".xls"; $ecl = $this->excel->get_fz3($ed,$titlename,$filename,$tail); $dir = '/data/excel/'.$time.'/'; $file_name = 'error_'.$time.rand(1000,9999); if(!is_dir('.'.$dir))mkdir('.'.$dir,0777); $myfile = fopen(".".$dir.$file_name.".xls", "w") or die(); fwrite($myfile,$ecl); fclose($myfile); $error = $dir.$file_name.'.xls'; echo json_encode(array('msg'=>'导入成功,'.$j.'条异常,','error'=>$error,'success'=>true));exit; } else { echo json_encode(array('msg'=>'导入成功!','error'=>1,'success'=>true));exit; } } } public function _dcexcel() { if(isset($_GET['excel'])) { $ktime = $this->input->get('ktime',true); $jtime = $this->input->get('jtime',true); $tax = $this->input->get('tax',true); $contract = $this->input->get('contract',true); $ktime = strtotime($ktime); $jtime = strtotime($jtime); $where = "1=1"; if($ktime && $jtime) { $where .= " and time > '$ktime' and time < '$jtime'"; } if($tax) { $where .= " and tax = '$tax'"; } if($contract) { $where .= " and contract = '$contract'"; } //数据排序 $order_str = "time desc"; $info_list = $this->warehousefactory->find_all($where,'sku,title,specs,size,text',$order_str); $rows = array();$list = array(); foreach ($info_list as $key=>$value) { $rows[strtolower($value['sku'])] = $value;//所有数据sku } $rowsdq = $rows; //格式化数据 foreach ($rowsdq as $key=>$value) { $r = $this->warehousefactory->find_count($where.' and sku = "'.$value['sku'].'" and state = "0"'); $c = $this->warehousefactory->find_count($where.' and sku = "'.$value['sku'].'" and state = "1"'); $rowsdq[$key]['r'] = $r+$c; $rowsdq[$key]['c'] = $c; $rowsdq[$key]['j'] = $r; } $title = date('Y-m-d',$ktime).'至'.date('Y-m-d',$jtime).'详情'; $titlename = "
编码 产品名称 规格 尺寸 描述 入库 出库 结余
"; $filename = $title.".xls"; $tail = ""; $this->excel->get_fz2($rowsdq,$titlename,$filename,$tail); } } public function _drmb() { $title = '导入模板'; $titlename = "
仓库1原材料仓,2半成品仓,3成品仓,4辅料仓,5普通仓 入库类别1采购入库单,2生产入库单,3其他入库单 存货编码 存货名称 经办人 供应单位 曲度 颜色 尺寸 其它规格 单位(KG) 数量 单价 金额 1保税/2非保税: 合同号 内部关联号 报关单号 汇率(数字,如:6.41) 币种(USD,CNY,EUR,GBP) 备注
2 2 1001 缅甸发 小明 龙盈实业 Straight Natural Black 20 8A KG 10 35 350 2 HT123456 2022032801 123456 6.41 USD 质量上乘
"; $filename = $title.".xls"; $tail = "\n"; $this->excel->get_fz2(array(),$titlename,$filename,$tail); } public function _ckmb() { $title = '批量出库模板'; $titlename = "
仓库1原材料仓,2半成品仓,3成品仓,4辅料仓,5普通仓 存货编码 出库类别1材料出库单,2销售出库单,3其他出库单 领用部门 客户 经办人 出库数量 备注
"; $filename = $title.".xls"; $tail = "\n"; $this->excel->get_fz2(array(),$titlename,$filename,$tail); } public function _pd() { $t = array(); $typeclass = $this->typeclass->find_all(); foreach ($typeclass as $v) { $t[$v['id']] = $v['title']; } $post = $this->input->get(NULL, TRUE); if(isset($post['page'])) { $warehouse = $this->input->get('warehouse',true); $sku = $this->input->get('sku',true); $title = $this->input->get('title',true); $lowe = $this->input->get('lowe',true); $color = $this->input->get('color',true); $size = $this->input->get('size',true); $specs = $this->input->get('specs',true); $tax = $this->input->get('tax',true); $contract = $this->input->get('contract',true); $ktime = $this->input->get('ktime',true); $jtime = $this->input->get('jtime',true); $ktime = strtotime($ktime); $jtime = strtotime($jtime); $where = "1=1 "; //数据排序 $order_str = "id desc"; if(empty($page)) { $start = 0; $perpage = 1; } else { $start = ($page - 1)*$perpage; } if($sku) { $where .= " and sku like '%$sku%'"; } if($title) { $where .= " and title like '%$title%'"; } if($lowe) { $where .= " and lowe like '%$lowe%'"; } if($color) { $where .= " and color like '%$color%'"; } if($size) { $where .= " and size like '%$size%'"; } if($specs) { $where .= " and specs like '%$specs%'"; } if($tax) { $where .= " and tax = '$tax'"; } if($contract) { $where .= " and contract = '$contract'"; } $zrk = 0;$zck = 0;$zjy = 0;$zjg = 0; //取得信息列表 $info_list = $this->warehousefactory->find_pc($where,'warehouse,sku,lowe,color,size,specs','warehouse,sku,nbgl,bgd,contract,title,lowe,color,size,specs,synum,price,'); //格式化数据 foreach ($info_list as $key=>$value) { if($value['warehouse'] == '1') { $info_list[$key]['warehouse'] = '原材料仓'; } else if($value['warehouse'] == '2') { $info_list[$key]['warehouse'] = '半成品仓'; } else if($value['warehouse'] == '3') { $info_list[$key]['warehouse'] = '成品仓'; } else if($value['warehouse'] == '4') { $info_list[$key]['warehouse'] = '辅料仓'; } else if($value['warehouse'] == '5') { $info_list[$key]['warehouse'] = '普通仓'; } $info_list[$key]['lowe'] = $t[$value['lowe']]; $info_list[$key]['color'] = $t[$value['color']]; $info_list[$key]['size'] = $t[$value['size']]; $m = 0;$ym = 0; $r = $this->warehousefactory->find_all($where." and sku = '".$value['sku']."' and warehouse = '".$value['warehouse']."' and lowe = '".$value['lowe']."' and color = '".$value['color']."' and size = '".$value['size']."' and specs = '".$value['specs']."' and review = '2'"); foreach ($r as $v) { $m += $v['synum']*$v['price']; $ym += $v['synum']*$v['yprice']; } $c = $this->warehousefactoryout->find_all($where." and sku = '".$value['sku']."' and warehouse = '".$value['warehouse']."' and lowe = '".$value['lowe']."' and color = '".$value['color']."' and size = '".$value['size']."' and specs = '".$value['specs']."' and review = '2'"); $info_list[$key]['synum'] = array_sum(array_column($r,'num')); $info_list[$key]['price'] = array_sum(array_column($c,'num')); $info_list[$key]['jy'] = sprintf("%01.2f",$info_list[$key]['synum'] - $info_list[$key]['price']); $synum = array_sum(array_column($r,'synum')); if($synum > 0) { $info_list[$key]['yjj'] = sprintf("%01.2f",$ym/$synum); $info_list[$key]['jj'] = sprintf("%01.2f",$m/$synum); } else { $info_list[$key]['yjj'] = 0; $info_list[$key]['jj'] = 0; } $info_list[$key]['ysyzj'] = $ym; $info_list[$key]['syzj'] = $m; $info_list[$key]['spsl'] = ''; $info_list[$key]['bz'] = ''; $zrk += $info_list[$key]['synum']; $zck += $info_list[$key]['price']; $zjy += $info_list[$key]['jy']; $yzjg += $ym; $zjg += $m; } } $title = '盘点'; $titlename = "
所属仓库 编码 内部关联号 报关单号 合同号 产品名称 曲度 颜色 尺寸 规格 入库 出库 结余 均价(原) 均价(CNY) 总价(原) 总价(CNY) 实盘数量 备注
"; $filename = $title.".xls"; $tail = "
汇总入库:".$zrk."汇总出库:".$zck."汇总结余:".sprintf("%01.2f",$zjy)."汇总价(原)".$yzjg."汇总价(CNY)".$zjg."
"; $this->excel->get_fz2($info_list,$titlename,$filename,$tail); } }