Machining.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Machining extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_machining','machining');
  7. $this->load->_model('Model_typeclass','typeclass');
  8. $this->load->_model('Model_customer','customer');
  9. }
  10. //定义方法的调用规则 获取URI第二段值
  11. public function _remap($arg,$arg_array)
  12. {
  13. if($arg == 'add')//添加
  14. {
  15. $this->_add();
  16. }
  17. else if($arg == 'edit')//修改
  18. {
  19. $this->_edit($arg_array);
  20. }
  21. else if($arg == 'del')//修改
  22. {
  23. $this->_del();
  24. }
  25. else if($arg == 'excel')
  26. {
  27. $this->_excel();
  28. }
  29. else if($arg == 'down')
  30. {
  31. $this->_down();
  32. }
  33. else if($arg == 'pindex')
  34. {
  35. $this->_pindex();
  36. }
  37. else
  38. {
  39. $this->_index();
  40. }
  41. }
  42. //管理
  43. public function _index()
  44. {
  45. $post = $this->input->post(NULL, TRUE);
  46. if(isset($post['page']))
  47. {
  48. $page = $this->input->post('page',true);
  49. $perpage = $this->input->post('perpage',true);
  50. $title = $this->input->post('title',true);
  51. $type = $this->input->post('type',true);
  52. $where = "1=1 ";
  53. if($type)
  54. {
  55. $where .= " and type = '$type'";
  56. }
  57. if($title)
  58. {
  59. $where .= " and title like '%$title%'";
  60. }
  61. //数据排序
  62. $order_str = "id asc";
  63. if(empty($page))
  64. {
  65. $start = 0;
  66. $perpage = 1;
  67. }
  68. else
  69. {
  70. $start = ($page - 1)*$perpage;
  71. }
  72. if($type)
  73. {
  74. //取得信息列表
  75. $info_list = $this->machining->find_all($where,'id,type,num,title,color',$order_str,$start,$perpage);
  76. foreach ($info_list as $k=>$v)
  77. {
  78. if($v['type'] == 1)
  79. {
  80. $info_list[$k]['type'] = '半成品';
  81. }
  82. else if($v['type'] == 2)
  83. {
  84. $info_list[$k]['type'] = '成品';
  85. }
  86. }
  87. $total = $this->machining->find_count($where);
  88. }
  89. else
  90. {
  91. $list = $this->machining->find_all($where,'id,type,num,title,color');
  92. $info = array();$data = array();
  93. foreach ($list as $k=>$v)
  94. {
  95. $key = preg_replace('/( | | |\s)/','',$v['title']).preg_replace('/( | | |\s)/','',$v['color']);
  96. if(isset($data[$key]))
  97. {
  98. $data[$key]['num'] += $v['num'];
  99. $data[$key]['type'] = '(半成品+成品)';
  100. }
  101. else
  102. {
  103. if($v['type'] == 1)
  104. {
  105. $v['type'] = '半成品';
  106. }
  107. else if($v['type'] == 2)
  108. {
  109. $v['type'] = '成品';
  110. }
  111. $data[$key] = $v;
  112. }
  113. }
  114. array_multisort(array_column($data,'type'),SORT_ASC,$data);
  115. foreach ($data as $k=>$v)
  116. {
  117. //if($v['type'] == '半成品+成品')
  118. //{
  119. $info[] = $v;
  120. //}
  121. }
  122. $info_list = array_slice($info,$start,$perpage);
  123. $total = count($info);
  124. }
  125. $pagenum = ceil($total/$perpage);
  126. $over = $total-($start+$perpage);
  127. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  128. echo json_encode($rows);exit;
  129. }
  130. $this->_Template('machining',$this->data);
  131. }
  132. //手机管理
  133. public function _pindex()
  134. {
  135. $post = $this->input->post(NULL, TRUE);
  136. if(isset($post['page']))
  137. {
  138. $page = $this->input->post('page',true);
  139. $perpage = $this->input->post('perpage',true);
  140. $title = $this->input->post('title',true);
  141. $type = $this->input->post('type',true);
  142. $where = "1=1 ";
  143. if($type)
  144. {
  145. $where .= " and type = '$type'";
  146. }
  147. if($title)
  148. {
  149. $where .= " and title like '%$title%'";
  150. }
  151. //数据排序
  152. $order_str = "id asc";
  153. if(empty($page))
  154. {
  155. $start = 0;
  156. $perpage = 1;
  157. }
  158. else
  159. {
  160. $start = ($page - 1)*$perpage;
  161. }
  162. if($type)
  163. {
  164. //取得信息列表
  165. $info_list = $this->machining->find_all($where,'id,type,num,title,color',$order_str,$start,$perpage);
  166. foreach ($info_list as $k=>$v)
  167. {
  168. if($v['type'] == 1)
  169. {
  170. $info_list[$k]['type'] = '半';
  171. }
  172. else if($v['type'] == 2)
  173. {
  174. $info_list[$k]['type'] = '成';
  175. }
  176. }
  177. $total = $this->machining->find_count($where);
  178. }
  179. else
  180. {
  181. $list = $this->machining->find_all($where,'id,type,num,title,color');
  182. $info = array();$data = array();
  183. foreach ($list as $k=>$v)
  184. {
  185. $key = preg_replace('/( | | |\s)/','',$v['title']).preg_replace('/( | | |\s)/','',$v['color']);
  186. if(isset($data[$key]))
  187. {
  188. $data[$key]['num'] += $v['num'];
  189. $data[$key]['type'] = '半+成';
  190. }
  191. else
  192. {
  193. if($v['type'] == 1)
  194. {
  195. $v['type'] = '半';
  196. }
  197. else if($v['type'] == 2)
  198. {
  199. $v['type'] = '成';
  200. }
  201. $data[$key] = $v;
  202. }
  203. }
  204. array_multisort(array_column($data,'type'),SORT_ASC,$data);
  205. foreach ($data as $k=>$v)
  206. {
  207. //if($v['type'] == '半成品+成品')
  208. //{
  209. $info[] = $v;
  210. //}
  211. }
  212. $info_list = array_slice($info,$start,$perpage);
  213. $total = count($info);
  214. }
  215. $total = $this->machining->find_count($where);
  216. $pagenum = ceil($total/$perpage);
  217. $over = $total-($start+$perpage);
  218. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  219. echo json_encode($rows);exit;
  220. }
  221. $this->_Template('phone/p_machining',$this->data);
  222. }
  223. //添加
  224. public function _add()
  225. {
  226. $post = $this->input->post(NULL, TRUE);
  227. if(isset($post['title']))
  228. {
  229. $post['time'] = time();
  230. if($this->machining->insert($post))
  231. {
  232. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  233. }
  234. else
  235. {
  236. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  237. }
  238. }
  239. $this->_Template('machining_add',$this->data);
  240. }
  241. //修改
  242. public function _edit($arg_array)
  243. {
  244. $post = $this->input->post(NULL, TRUE);
  245. if(isset($post['id']))
  246. {
  247. if($this->machining->save($post,$id))
  248. {
  249. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  250. }
  251. else
  252. {
  253. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  254. }
  255. }
  256. $machining = $this->machining->read($arg_array[0]);
  257. $this->data['machining'] = $machining;
  258. $this->_Template('machining_edit',$this->data);
  259. }
  260. //删除
  261. public function _del()
  262. {
  263. $post = $this->input->post(NULL, TRUE);
  264. if(isset($post['s']))
  265. {
  266. $id_arr = $this->input->post('s');
  267. $id_arr = explode(',',$id_arr);
  268. if(!$id_arr)
  269. {
  270. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  271. }
  272. //循环删除记录
  273. foreach ($id_arr as $v)
  274. {
  275. $this->machining->remove($v);
  276. }
  277. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  278. }
  279. }
  280. public function _excel()
  281. {
  282. $dir = '/data/excel/'.date('Ymd',time()).'/';
  283. $config['upload_path'] = '.'.$dir ;
  284. $config['file_name'] = date('Ymd_His_',time()).rand(1000,9999);
  285. $config['allowed_types'] = 'xls|xlsx';
  286. $config['max_size'] = 10240;
  287. $this->load->library('upload', $config);
  288. $this->upload->initialize($config);
  289. if ($this->upload->do_upload('userfile'))
  290. {
  291. $full_path = $dir.$this->upload->data('file_name');
  292. $fileName = '.' . $full_path;
  293. if (!file_exists($fileName))
  294. {
  295. echo json_encode(array('msg'=>"上传失败,请重试",'success'=>false));exit;
  296. }
  297. else
  298. {
  299. require_once "./data/excel/PHPExcel/IOFactory.php";
  300. @$phpExcel = PHPExcel_IOFactory::load($fileName);// 载入当前文件
  301. @$phpExcel->setActiveSheetIndex(0);// 设置为默认表
  302. $sheetCount = $phpExcel->getSheetCount();// 获取表格数量
  303. $row = $phpExcel->getActiveSheet()->getHighestRow();// 获取行数
  304. $column = $phpExcel->getActiveSheet()->getHighestColumn();// 获取列数
  305. ++$column;//如果列数大于26行
  306. $list = array();
  307. for ($i = 2; $i <= $row; $i++) // 行数循环
  308. {
  309. $data = array();
  310. for ($c = 'A'; $c != $column; $c++) // 列数循环
  311. {
  312. $data[] = $phpExcel->getActiveSheet()->getCell($c . $i)->getValue();
  313. }
  314. $list[] = $data;
  315. }
  316. }
  317. $i = 0;
  318. $this->db->trans_begin();
  319. foreach ($list as $key=>$value)
  320. {
  321. $value['2'] = $value['2']?str_replace(array('/( | | |\s)/'),array(' '),$value['2']):'';
  322. $value['3'] = $value['3']?str_replace(array('/( | | |\s)/'),array(' '),$value['3']):'';
  323. $post['type'] = $value['0'];
  324. $post['num'] = $value['1'];
  325. $post['title'] = $value['2'];
  326. $post['color'] = $value['3'];
  327. $post['time'] = time();
  328. $this->machining->insert($post);
  329. }
  330. if ($this->db->trans_status() === FALSE)
  331. {
  332. $this->db->trans_commit();
  333. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  334. }
  335. else
  336. {
  337. $this->db->trans_commit();
  338. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  339. }
  340. }
  341. else
  342. {
  343. echo json_encode(array('msg'=>'上传失败!','t'=>$this->upload->display_errors(),'success'=>false));exit;
  344. }
  345. }
  346. public function _down()
  347. {
  348. if(isset($_GET['excel']))
  349. {
  350. $title = "导入模板";
  351. $titledata = array(array('类型:1.半成品2.成品','数量','名称','蕾丝颜色'),array('1','20','4*4 body','black'));
  352. $filename = $title.".xls";
  353. $this->customer->get_excel($titledata,$filename);
  354. }
  355. }
  356. }