123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace app\admin\controller;
- use app\common\model\MateGoodsLog;
- use app\common\util\ExcelUtil;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use think\App;
- use think\Db;
- use think\Exception;
- class CompanyDispatch extends Auth
- {
- public function __construct(App $app = null) {
- parent::__construct($app);
- $this->model = new \app\common\model\MateApply();
- $this->table = $this->model->table;
- }
- /**
- * 公司仓库入库记录
- *
- * @author wst
- * @date 2021/9/10 8:37
- */
- public function show(){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','id','trim'); //排序列
- $sort = input('sord','desc','trim'); //排序方式
- $order = $sortRow.' '.$sort;
- $title = input('title','','trim');
- if($title){
- $map[] = ['sn','like','%'.$title.'%'];
- }
- $enable = input('enable','','trim');
- if($enable != ''){
- $map[] = ['enable','=',$enable];
- }
- $map[] = ['org_id','=',$this->orgId];
- $map[] = ['type','=',1];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists =db($this->table)
- ->where($map)->limit($start,$length)
- ->order($order)->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['userName'] = db('user')
- ->where('id',$v['user_id'])
- ->value('real_name');
- }
- //数据返回
- $totalCount = db($this->table)->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- $this->assign('meta_title','入库记录');
- return $this->fetch();
- }
- }
- /**
- * 新增入库
- */
- public function add(){
- if(request()->isPost()){
- $res = $this->model->updates($this->userId);
- if($res){
- $this->success('操作成功',url('show'));
- }else{
- $this->error($this->model->getError());
- }
- }else{
- $meta_title = '新增入库';
- $this->assign('meta_title',$meta_title);
- return $this->fetch();
- }
- }
- //详情
- public function info($id){
- $info = $this->model->getInfo($id);
- if(!$info) $this->error('记录不存在');
- $this->assign('info',$info);
- return $this->fetch();
- }
- public function import(){
- return $this->fetch();
- }
- /**
- * 下载点模板
- */
- public function downloadtem(){
- set_time_limit(0);
- ini_set("memory_limit","512M");
- $fileName = '入库单模板.xlsx';
- $spreadsheet = new Spreadsheet();
- $worksheet = $spreadsheet->getActiveSheet();
- $worksheet->setTitle("入库单模板");
- $arr = array('A','B','C');
- foreach($arr as $k=>$v){
- $worksheet->getStyle($v)->getAlignment()->setWrapText(true);//换行
- $worksheet->getStyle($v.'1')->getFont()->setBold(true);
- $worksheet->getColumnDimension($v)->setWidth(18);
- $worksheet->getStyle($v.'3')->getFont()->setBold(true);
- }
- $worksheet->setCellValueByColumnAndRow(1,1,'入库人');
- $worksheet->setCellValueByColumnAndRow(2,1,'联系电话');
- $worksheet->setCellValueByColumnAndRow(3,1,'备注');
- $worksheet->setCellValueByColumnAndRow(1,3,'物品编号');
- $worksheet->setCellValueByColumnAndRow(2,3,'单价');
- $worksheet->setCellValueByColumnAndRow(3,3,'数量');
- ob_end_clean();//清除缓冲区,避免乱码
- header('Content-Type: application/vnd.ms-excel');
- header('Content-Disposition: attachment;filename="'.$fileName.'"');
- header('Cache-Control: max-age=0');
- $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
- $writer->save('php://output'); //文件通过浏览器下载
- }
- public function importexcel(){
- set_time_limit(0);
- ini_set("memory_limit", -1);
- ob_flush();//清空缓存
- flush();//刷新缓存
- $orgId = $this->orgId;
- if(!request()->file()) {
- exit('请上传文件');
- }
- $file = request()->file('file');
- //获取文件后缀
- $e = explode('.',$_FILES['file']['name']);
- $ext = $e[count($e)-1];
- $newArr=['xls','xlsx'];
- if(!in_array($ext,$newArr)){
- exit('文件格式不正确');
- }
- // 移动到框架应用根目录/uploads/ 目录下
- $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
- ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
- if(!$info){
- exit('文件上传失败');
- }
- $img = './uploads/files/' . $info->getSaveName();
- $filePath = str_replace('\\', '/', $img);
- $data = ExcelUtil::read($filePath,['c1','c2','c3']);
- if(!$data && count($data) <= 2){
- exit('未上传数据');
- }
- $name = trim($data[0]['c1']);
- $phone = trim($data[0]['c2']);
- $remark = trim($data[0]['c3']);
- if(empty($name)){
- exit('未填写入库人');
- }
- if(empty($phone)){
- exit('未填写联系电话');
- }
- Db::startTrans();
- try{
- $curTime = date("Y-m-d H:i:s");
- $applyId = Db::name('mate_apply')->insertGetId([
- 'org_id' => $this->orgId,
- 'create_time' => $curTime,
- 'user_id' => $this->userId,
- 'sn' => get_unique_id(),
- 'remark' => $remark,
- 'name' => $name,
- 'phone' => $phone,
- 'type' => 1
- ]);
- if(!$applyId){
- \exception('操作失败');
- }
- $arr = [];
- foreach ($data as $k=>$v){
- if($k < 2){
- continue;
- }
- if(empty($v['c1'])){
- $msg = "第".($k+1)."行,物品编号未输入";
- \exception($msg);
- }
- if(empty($v['c2']) || $v['c2'] < 0){
- $msg = "第".($k+1)."行,价格不正确";
- \exception($msg);
- }
- if(empty($v['c3']) || $v['c3'] <= 0){
- $msg = "第".($k+1)."行,数量不正确";
- \exception($msg);
- }
- // 重新计算平均价
- $info = Db::name('mate_goods')->where('id',$v['c1'])->find();
- if(!$info){
- $msg = "第".($k+1)."行,物品编号不正确";
- \exception($msg);
- }
- echo "<font color=\"green\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,物品信息正确</font><br />";
- $price = ($info['price'] *$info['nums'] + $v['c2']*$v['c3']) / ($info['nums']+$v['c3']);
- $ret = Db::name('mate_goods')->where('id',$v['c1'])->update([
- 'price' => round($price,2),
- 'nums' => $info['nums']+$v['c3'],
- 'update_time' => $curTime
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- $arr[] = [
- 'apply_id' => $applyId,
- 'goods_id' => $v['c1'],
- 'nums' => $v['c3'],
- 'price' => $v['c2']
- ];
- }
- $ret = Db::name('mate_apply_goods')->insertAll($arr);
- if($ret != count($arr)){
- \exception('操作失败');
- }
- Db::commit();
- echo "<font color=\"green\" style='margin-left: 20px;font-size: 17px'>导入成功</font><br />";
- }catch (\Exception $e){
- Db::rollback();
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>".$e->getMessage()."</font><br />";
- }
- }
- }
|