OfficeItems.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class OfficeItems extends Model
  8. {
  9. public function add($userId){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'title' => input('title','','trim'),
  13. 'spec' => input('spec','','trim'),
  14. 'price' => input('price/f',0),
  15. 'org_id' => input('orgId/d',0),
  16. 'cate_id' => input('cateId/d',0),
  17. 'unit_id' => input('unitId/d',0),
  18. 'buy_time' => input('buyTime','','trim'),
  19. 'nums' => input('nums/d',0),
  20. 'last_nums' => input('lastNums/d',0),
  21. 'remark' => input('remark','','trim')
  22. ];
  23. $logdata = json_encode($data);
  24. $result = validate('OfficeItems')->check($data,[],'');
  25. if(true !== $result){
  26. HelpHander::error(validate('OfficeItems')->getError());
  27. }
  28. Db::startTrans();
  29. try{
  30. $id = $data['id'];
  31. unset($data['id']);
  32. if($id > 0){
  33. $data['update_time'] = date('Y-m-d H:i:s');
  34. unset($data['nums']);
  35. unset($data['last_nums']);
  36. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  37. }else{
  38. $data['create_time'] = date('Y-m-d H:i:s');
  39. $data['last_nums'] = $data['nums'];
  40. $ret = $this->allowField(true)->save($data);
  41. }
  42. if(!$ret){
  43. \exception('操作失败');
  44. }
  45. if($id <= 0){ // 添加一个初始入库单
  46. $res = Db::name('office_inreceipt')->insert([
  47. 'org_id' => $data['org_id'],
  48. 'user_id' => $userId,
  49. 'items_id' => $this->id,
  50. 'nums' => $data['nums'],
  51. 'remark' => '初始入库',
  52. 'create_time' => date('Y-m-d H:i:s')
  53. ]);
  54. if(!$res){
  55. \exception('操作失败');
  56. }
  57. }
  58. if($id > 0){
  59. $content = '修改办公物品';
  60. }else{
  61. $content = '添加办公物品';
  62. }
  63. model('ActionLog')->add(13,$content,0,$logdata);
  64. Db::commit();
  65. }catch (\Exception $e){
  66. $this->error = $e->getMessage();
  67. Db::rollback();
  68. return false;
  69. }
  70. return true;
  71. }
  72. public function info($id){
  73. $info = $this->where('id',$id)->find();
  74. if(!$info){
  75. HelpHander::error('数据不存在');
  76. }
  77. return $info->toArray();
  78. }
  79. public function lists($page,$size,$title,$cateId,$orgId,$type=0){ // type 0=全部 1=有库存
  80. $map[] = ['org_id','=',$orgId];
  81. $map[] = ['del','=',0];
  82. if($cateId > 0){
  83. $map[] = ['cate_id','=',$cateId];
  84. }
  85. if($title){
  86. $map[] = ['title','like','%'.$title.'%'];
  87. }
  88. if($type == 1){
  89. $map[] = ['last_nums','>',0];
  90. $map[] = ['enable','=',1];
  91. }
  92. $lists = $this
  93. ->where($map)
  94. ->page($page,$size)
  95. ->field('del,update_time',true)
  96. ->order('id desc')
  97. ->select();
  98. $lists = $lists?$lists->toArray():[];
  99. foreach ($lists as $k=>$v){
  100. $lists[$k]['cate_name'] = Db::name('office_cate')->where('id',$v['cate_id'])->value('title');
  101. $lists[$k]['unit_name'] = Db::name('office_unit')->where('id',$v['unit_id'])->value('title');
  102. }
  103. $total = $this->where($map)->count();
  104. $data = [
  105. 'total' => $total,
  106. 'list' => $lists
  107. ];
  108. return $data;
  109. }
  110. public function export($title,$cateId,$orgId,$type=0){ // type 0=全部 1=有库存
  111. $map[] = ['org_id','=',$orgId];
  112. $map[] = ['del','=',0];
  113. if($cateId > 0){
  114. $map[] = ['cate_id','=',$cateId];
  115. }
  116. if($title){
  117. $map[] = ['title','like','%'.$title.'%'];
  118. }
  119. if($type == 1){
  120. $map[] = ['last_nums','>',0];
  121. $map[] = ['enable','=',1];
  122. }
  123. $lists = $this
  124. ->where($map)
  125. ->field('del,update_time',true)
  126. ->order('id desc')
  127. ->select();
  128. $lists = $lists?$lists->toArray():[];
  129. foreach ($lists as $k=>$v){
  130. $lists[$k]['cate_name'] = Db::name('office_cate')->where('id',$v['cate_id'])->value('title');
  131. $lists[$k]['unit_name'] = Db::name('office_unit')->where('id',$v['unit_id'])->value('title');
  132. if($v['enable'] == 1){
  133. $lists[$k]['enable_text'] = '启用';
  134. }else{
  135. $lists[$k]['enable_text'] = '禁用';
  136. }
  137. }
  138. $columns = [
  139. ["title" => "名称","key" => "title"],
  140. ["title" => "规格","key" => "spec"],
  141. ["title" => "类型","key" => "cateName"],
  142. ["title" => "价格","key" => "price"],
  143. ["title" => "总数","key" => "nums"],
  144. ["title" => "剩余数量","key" => "lastNums"],
  145. ["title" => "单位","key" => "unitName"],
  146. ["title" => "购买日期","key" => "buyTime"],
  147. ["title" => "备注","key" => "remark"],
  148. ["title" => "状态","key" => "enableText"],
  149. ];
  150. $data = [
  151. 'columns' => $columns,
  152. 'list' => $lists
  153. ];
  154. return $data;
  155. }
  156. public function del($id){
  157. // 检查是否已被使用
  158. $info = Db::name('office_receive_items')
  159. ->where('items_id',$id)
  160. ->find();
  161. if($info){
  162. HelpHander::error('已被使用,无法删除');
  163. }
  164. $ret = $this->where('id',$id)->delete();
  165. if(!$ret){
  166. HelpHander::error('删除失败');
  167. }
  168. $logdata = json_encode(['id' => $id]);
  169. model('ActionLog')->add(13,'删除办公物品',0,$logdata);
  170. return true;
  171. }
  172. // 物品数量修改
  173. public function modifyNums($id,$nums,$reason,$type,$userId,$orgId){
  174. Db::startTrans();
  175. try{
  176. $info = Db::name('office_items')->where('id',$id)->where('del',0)->lock(true)->find();
  177. if(!$info){
  178. \exception('物品不存在');
  179. }
  180. if($type == 1 && $info['last_nums'] < $nums){ // 减少
  181. \exception('减少数量不能大于剩余数量');
  182. }
  183. if($type == 1){
  184. $nums = -$nums;
  185. }
  186. $before = $info['last_nums'];
  187. $after = $before + $nums;
  188. $ret = Db::name('office_items')->where('id',$id)->setField('last_nums',$after);
  189. if(!$ret){
  190. \exception('操作失败');
  191. }
  192. $res = Db::name('office_items_log')->insert([
  193. 'org_id' => $orgId,
  194. 'user_id' => $userId,
  195. 'items_id' => $id,
  196. 'before' => $before,
  197. 'after' => $after,
  198. 'nums' => $nums,
  199. 'reason' => $reason,
  200. 'create_time' => date('Y-m-d H:i:s')
  201. ]);
  202. if(!$res){
  203. \exception('操作失败');
  204. }
  205. Db::commit();
  206. }catch (Exception $e){
  207. Db::rollback();
  208. HelpHander::error($e->getMessage());
  209. }
  210. return true;
  211. }
  212. // 物品数量修改
  213. public function modifyNumsReceive($id,$nums,$remark,$userId,$orgId){
  214. Db::startTrans();
  215. try{
  216. $info = Db::name('office_items')->where('id',$id)->where('del',0)->lock(true)->find();
  217. if(!$info){
  218. \exception('物品不存在');
  219. }
  220. $total = $info['nums'] + $nums;
  221. $after = $info['last_nums'] + $nums;
  222. $ret = Db::name('office_items')->where('id',$id)->update(['last_nums'=>$after,'nums'=>$total]);
  223. if(!$ret){
  224. \exception('操作失败');
  225. }
  226. $res = Db::name('office_inreceipt')->insert([
  227. 'org_id' => $orgId,
  228. 'user_id' => $userId,
  229. 'items_id' => $id,
  230. 'nums' => $nums,
  231. 'remark' => $remark,
  232. 'create_time' => date('Y-m-d H:i:s')
  233. ]);
  234. if(!$res){
  235. \exception('操作失败');
  236. }
  237. Db::commit();
  238. }catch (Exception $e){
  239. Db::rollback();
  240. HelpHander::error($e->getMessage());
  241. }
  242. return true;
  243. }
  244. // 日志修改记录
  245. public function numsLog($page,$size,$title,$name,$orgId){
  246. $map[] = ['oil.org_id','=',$orgId];
  247. if($title){
  248. $map[] = ['oi.title','like','%'.$title.'%'];
  249. }
  250. if($name){
  251. $map[] = ['ui.name','like','%'.$name.'%'];
  252. }
  253. $lists = Db::name('office_items_log')
  254. ->alias('oil')
  255. ->join('user_info ui','ui.user_id = oil.user_id')
  256. ->join('office_items oi','oi.id = oil.items_id')
  257. ->field('oil.*,oi.title,ui.name')
  258. ->where($map)
  259. ->page($page,$size)
  260. ->order('oil.id desc')
  261. ->select();
  262. $total = Db::name('office_items_log')
  263. ->alias('oil')
  264. ->join('user_info ui','ui.user_id = oil.user_id')
  265. ->join('office_items oi','oi.id = oil.items_id')
  266. ->where($map)->count();
  267. $data = [
  268. 'total' => $total,
  269. 'list' => $lists?$lists:[]
  270. ];
  271. return $data;
  272. }
  273. public function inreceipt($page,$size,$id){
  274. $map[] = ['items_id','=',$id];
  275. $lists = Db::name('office_inreceipt')
  276. ->where($map)
  277. ->page($page,$size)
  278. ->order('id desc')
  279. ->select();
  280. foreach ($lists as $k=>$v){
  281. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  282. }
  283. $total = Db::name('office_inreceipt')
  284. ->where($map)->count();
  285. $data = [
  286. 'total' => $total,
  287. 'list' => $lists?$lists:[]
  288. ];
  289. return $data;
  290. }
  291. // 物品入库月统计
  292. public function numsTj($page,$size,$title,$month,$orgId){
  293. $map[] = ['a.org_id','=',$orgId];
  294. if($title){
  295. $map[] = ['b.title','like','%'.$title.'%'];
  296. }
  297. if($month){
  298. $map[] = ['a.create_time','like',$month.'%'];
  299. }else{
  300. $map[] = ['a.items_id','=',0];
  301. }
  302. $lists = Db::name('office_items_tj')
  303. ->alias('a')
  304. ->join('office_items b','b.id = a.items_id')
  305. ->field('sum(a.nums) as nums1,sum(a.nums2) as nums2,b.title')
  306. ->where($map)
  307. ->page($page,$size)
  308. ->order('a.create_time desc')
  309. ->group('a.items_id')
  310. // ->fetchSql(true)
  311. ->select();
  312. // halt($lists);
  313. $total = Db::name('office_items_tj')
  314. ->alias('a')
  315. ->join('office_items b','b.id = a.items_id')
  316. ->group('a.items_id')
  317. ->where($map)->count();
  318. $data = [
  319. 'total' => $total,
  320. 'list' => $lists?$lists:[]
  321. ];
  322. return $data;
  323. }
  324. public function tjexport($title,$month,$orgId){
  325. $map[] = ['a.org_id','=',$orgId];
  326. if($title){
  327. $map[] = ['b.title','like','%'.$title.'%'];
  328. }
  329. if($month){
  330. $map[] = ['a.create_time','like',$month.'%'];
  331. }else{
  332. $map[] = ['a.items_id','=',0];
  333. }
  334. $lists = Db::name('office_items_tj')
  335. ->alias('a')
  336. ->join('office_items b','b.id = a.items_id')
  337. ->field('sum(a.nums) as nums1,sum(a.nums2) as nums2,b.title')
  338. ->where($map)
  339. ->order('a.create_time desc')
  340. ->group('a.items_id')
  341. ->select();
  342. $lists = $lists?$lists:[];
  343. $columns = [
  344. ["title" => "名称","key" => "title"],
  345. ["title" => "入库数量","key" => "nums1"],
  346. ["title" => "修改数量","key" => "nums2"],
  347. ];
  348. $data = [
  349. 'columns' => $columns,
  350. 'list' => $lists
  351. ];
  352. return $data;
  353. }
  354. }