CompanyDispatch.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 CompanyDispatch 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 = $this->model->table;
  15. }
  16. /**
  17. * 公司仓库入库记录
  18. *
  19. * @author wst
  20. * @date 2021/9/10 8:37
  21. */
  22. public function show(){
  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','=',1];
  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('show'));
  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 info($id){
  84. $info = $this->model->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. $worksheet->setCellValueByColumnAndRow(3,3,'数量');
  115. ob_end_clean();//清除缓冲区,避免乱码
  116. header('Content-Type: application/vnd.ms-excel');
  117. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  118. header('Cache-Control: max-age=0');
  119. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  120. $writer->save('php://output'); //文件通过浏览器下载
  121. }
  122. public function importexcel(){
  123. set_time_limit(0);
  124. ini_set("memory_limit", -1);
  125. ob_flush();//清空缓存
  126. flush();//刷新缓存
  127. $orgId = $this->orgId;
  128. if(!request()->file()) {
  129. exit('请上传文件');
  130. }
  131. $file = request()->file('file');
  132. //获取文件后缀
  133. $e = explode('.',$_FILES['file']['name']);
  134. $ext = $e[count($e)-1];
  135. $newArr=['xls','xlsx'];
  136. if(!in_array($ext,$newArr)){
  137. exit('文件格式不正确');
  138. }
  139. // 移动到框架应用根目录/uploads/ 目录下
  140. $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
  141. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  142. if(!$info){
  143. exit('文件上传失败');
  144. }
  145. $img = './uploads/files/' . $info->getSaveName();
  146. $filePath = str_replace('\\', '/', $img);
  147. $data = ExcelUtil::read($filePath,['c1','c2','c3']);
  148. if(!$data && count($data) <= 2){
  149. exit('未上传数据');
  150. }
  151. $name = trim($data[0]['c1']);
  152. $phone = trim($data[0]['c2']);
  153. $remark = trim($data[0]['c3']);
  154. if(empty($name)){
  155. exit('未填写入库人');
  156. }
  157. if(empty($phone)){
  158. exit('未填写联系电话');
  159. }
  160. Db::startTrans();
  161. try{
  162. $curTime = date("Y-m-d H:i:s");
  163. $applyId = Db::name('mate_apply')->insertGetId([
  164. 'org_id' => $this->orgId,
  165. 'create_time' => $curTime,
  166. 'user_id' => $this->userId,
  167. 'sn' => get_unique_id(),
  168. 'remark' => $remark,
  169. 'name' => $name,
  170. 'phone' => $phone,
  171. 'type' => 1
  172. ]);
  173. if(!$applyId){
  174. \exception('操作失败');
  175. }
  176. $arr = [];
  177. foreach ($data as $k=>$v){
  178. if($k < 2){
  179. continue;
  180. }
  181. if(empty($v['c1'])){
  182. $msg = "第".($k+1)."行,物品编号未输入";
  183. \exception($msg);
  184. }
  185. if(empty($v['c2']) || $v['c2'] < 0){
  186. $msg = "第".($k+1)."行,价格不正确";
  187. \exception($msg);
  188. }
  189. if(empty($v['c3']) || $v['c3'] <= 0){
  190. $msg = "第".($k+1)."行,数量不正确";
  191. \exception($msg);
  192. }
  193. // 重新计算平均价
  194. $info = Db::name('mate_goods')->where('id',$v['c1'])->find();
  195. if(!$info){
  196. $msg = "第".($k+1)."行,物品编号不正确";
  197. \exception($msg);
  198. }
  199. echo "<font color=\"green\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,物品信息正确</font><br />";
  200. $price = ($info['price'] *$info['nums'] + $v['c2']*$v['c3']) / ($info['nums']+$v['c3']);
  201. $ret = Db::name('mate_goods')->where('id',$v['c1'])->update([
  202. 'price' => round($price,2),
  203. 'nums' => $info['nums']+$v['c3'],
  204. 'update_time' => $curTime
  205. ]);
  206. if(!$ret){
  207. \exception('操作失败');
  208. }
  209. $arr[] = [
  210. 'apply_id' => $applyId,
  211. 'goods_id' => $v['c1'],
  212. 'nums' => $v['c3'],
  213. 'price' => $v['c2']
  214. ];
  215. }
  216. $ret = Db::name('mate_apply_goods')->insertAll($arr);
  217. if($ret != count($arr)){
  218. \exception('操作失败');
  219. }
  220. Db::commit();
  221. echo "<font color=\"green\" style='margin-left: 20px;font-size: 17px'>导入成功</font><br />";
  222. }catch (\Exception $e){
  223. Db::rollback();
  224. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>".$e->getMessage()."</font><br />";
  225. }
  226. }
  227. }