MateApply.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\MateGoodsLog;
  4. use app\common\util\ExcelUtil;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use think\App;
  7. use think\Db;
  8. use think\Exception;
  9. class MateApply extends Auth
  10. {
  11. public function __construct(App $app = null) {
  12. parent::__construct($app);
  13. $this->model = new \app\common\model\MateApply();
  14. $this->table = 'mate_apply';
  15. }
  16. /**
  17. * 项目仓库出库记录
  18. *
  19. * @author wst
  20. * @date 2021/9/10 8:37
  21. */
  22. public function back(){
  23. if(request()->isAjax()){
  24. //分页参数
  25. $length = input('rows',10,'intval'); //每页条数
  26. $page = input('page',1,'intval'); //第几页
  27. $start = ($page - 1) * $length; //分页开始位置
  28. //排序
  29. $sortRow = input('sidx','id','trim'); //排序列
  30. $sort = input('sord','desc','trim'); //排序方式
  31. $order = $sortRow.' '.$sort;
  32. $title = input('title','','trim');
  33. if($title){
  34. $map[] = ['sn','like','%'.$title.'%'];
  35. }
  36. $enable = input('enable','','trim');
  37. if($enable != ''){
  38. $map[] = ['enable','=',$enable];
  39. }
  40. $map[] = ['org_id','=',$this->orgId];
  41. $map[] = ['type','=',2];
  42. $map= empty($map) ? true: $map;
  43. //数据查询
  44. $lists =db($this->table)
  45. ->where($map)->limit($start,$length)
  46. ->order($order)->select();
  47. foreach ($lists as $k=>$v){
  48. $lists[$k]['userName'] = db('user')
  49. ->where('id',$v['user_id'])
  50. ->value('real_name');
  51. }
  52. //数据返回
  53. $totalCount = db($this->table)->where($map)->count();
  54. $totalPage = ceil($totalCount/$length);
  55. $result['page'] = $page;
  56. $result['total'] = $totalPage;
  57. $result['records'] = $totalCount;
  58. $result['rows'] = $lists;
  59. return json($result);
  60. }else{
  61. $this->assign('meta_title','出库记录');
  62. return $this->fetch();
  63. }
  64. }
  65. /**
  66. * 新增出库
  67. */
  68. public function add(){
  69. if(request()->isPost()){
  70. $res = $this->model->updates($this->userId);
  71. if($res){
  72. $this->success('操作成功',url('back'));
  73. }else{
  74. $this->error($this->model->getError());
  75. }
  76. }else{
  77. $meta_title = '新增出库';
  78. $this->assign('meta_title',$meta_title);
  79. return $this->fetch();
  80. }
  81. }
  82. //出库详情
  83. public function detail($id){
  84. $info = (new \app\common\model\MateApply())->getInfo($id);
  85. if(!$info) $this->error('记录不存在');
  86. $this->assign('info',$info);
  87. return $this->fetch();
  88. }
  89. public function import(){
  90. return $this->fetch();
  91. }
  92. /**
  93. * 下载点模板
  94. */
  95. public function downloadtem(){
  96. set_time_limit(0);
  97. ini_set("memory_limit","512M");
  98. $fileName = '出库单模板.xlsx';
  99. $spreadsheet = new Spreadsheet();
  100. $worksheet = $spreadsheet->getActiveSheet();
  101. $worksheet->setTitle("出库单模板");
  102. $arr = array('A','B','C');
  103. foreach($arr as $k=>$v){
  104. $worksheet->getStyle($v)->getAlignment()->setWrapText(true);//换行
  105. $worksheet->getStyle($v.'1')->getFont()->setBold(true);
  106. $worksheet->getColumnDimension($v)->setWidth(18);
  107. $worksheet->getStyle($v.'3')->getFont()->setBold(true);
  108. }
  109. $worksheet->setCellValueByColumnAndRow(1,1,'出库人');
  110. $worksheet->setCellValueByColumnAndRow(2,1,'联系电话');
  111. $worksheet->setCellValueByColumnAndRow(3,1,'备注');
  112. $worksheet->setCellValueByColumnAndRow(1,3,'物品编号');
  113. $worksheet->setCellValueByColumnAndRow(2,3,'数量');
  114. ob_end_clean();//清除缓冲区,避免乱码
  115. header('Content-Type: application/vnd.ms-excel');
  116. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  117. header('Cache-Control: max-age=0');
  118. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  119. $writer->save('php://output'); //文件通过浏览器下载
  120. }
  121. public function importexcel(){
  122. set_time_limit(0);
  123. ini_set("memory_limit", -1);
  124. ob_flush();//清空缓存
  125. flush();//刷新缓存
  126. $orgId = $this->orgId;
  127. if(!request()->file()) {
  128. exit('请上传文件');
  129. }
  130. $file = request()->file('file');
  131. //获取文件后缀
  132. $e = explode('.',$_FILES['file']['name']);
  133. $ext = $e[count($e)-1];
  134. $newArr=['xls','xlsx'];
  135. if(!in_array($ext,$newArr)){
  136. exit('文件格式不正确');
  137. }
  138. // 移动到框架应用根目录/uploads/ 目录下
  139. $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
  140. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  141. if(!$info){
  142. exit('文件上传失败');
  143. }
  144. $img = './uploads/files/' . $info->getSaveName();
  145. $filePath = str_replace('\\', '/', $img);
  146. $data = ExcelUtil::read($filePath,['c1','c2','c3']);
  147. if(!$data && count($data) <= 2){
  148. exit('未上传数据');
  149. }
  150. $name = trim($data[0]['c1']);
  151. $phone = trim($data[0]['c2']);
  152. $remark = trim($data[0]['c3']);
  153. if(empty($name)){
  154. exit('未填写出库人');
  155. }
  156. if(empty($phone)){
  157. exit('未填写联系电话');
  158. }
  159. Db::startTrans();
  160. try{
  161. $curTime = date("Y-m-d H:i:s");
  162. $applyId = Db::name('mate_apply')->insertGetId([
  163. 'org_id' => $this->orgId,
  164. 'create_time' => $curTime,
  165. 'user_id' => $this->userId,
  166. 'sn' => get_unique_id(),
  167. 'remark' => $remark,
  168. 'name' => $name,
  169. 'phone' => $phone,
  170. 'type' => 2
  171. ]);
  172. if(!$applyId){
  173. \exception('操作失败');
  174. }
  175. $arr = [];
  176. foreach ($data as $k=>$v){
  177. if($k < 2){
  178. continue;
  179. }
  180. if(empty($v['c1'])){
  181. $msg = "第".($k+1)."行,物品编号未输入";
  182. \exception($msg);
  183. }
  184. if(empty($v['c2']) || $v['c2'] <= 0){
  185. $msg = "第".($k+1)."行,数量不正确";
  186. \exception($msg);
  187. }
  188. // 重新计算平均价
  189. $info = Db::name('mate_goods')->where('id',$v['c1'])->find();
  190. if(!$info){
  191. $msg = "第".($k+1)."行,物品编号不正确";
  192. \exception($msg);
  193. }
  194. // 重新计算平均价
  195. $info = Db::name('mate_goods')->where('id',$v['c1'])->find();
  196. if(!$info){
  197. $msg = "第".($k+1)."行,物品编号不正确";
  198. \exception($msg);
  199. }
  200. echo "<font color=\"green\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,物品信息正确</font><br />";
  201. $ret = Db::name('mate_goods')->where('id',$v['c1'])->update([
  202. 'nums' => $info['nums'] - $v['c2'],
  203. 'update_time' => $curTime
  204. ]);
  205. if(!$ret){
  206. \exception('操作失败');
  207. }
  208. $arr[] = [
  209. 'apply_id' => $applyId,
  210. 'goods_id' => $k,
  211. 'nums' => $v['c2'],
  212. 'price' => $info['price']
  213. ];
  214. }
  215. $ret = Db::name('mate_apply_goods')->insertAll($arr);
  216. if($ret != count($arr)){
  217. \exception('操作失败');
  218. }
  219. Db::commit();
  220. echo "<font color=\"green\" style='margin-left: 20px;font-size: 17px'>导入成功</font><br />";
  221. }catch (\Exception $e){
  222. Db::rollback();
  223. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>".$e->getMessage()."</font><br />";
  224. }
  225. }
  226. }