Purchase.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Purchase extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_purchase','purchase');
  7. $this->load->_model('Model_country','country');
  8. $this->load->_model('Model_productprice','productprice');
  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 == 'rows')//修改
  26. {
  27. $this->_rows();
  28. }
  29. else
  30. {
  31. $this->_index();
  32. }
  33. }
  34. //管理
  35. public function _index()
  36. {
  37. $post = $this->input->post(NULL, TRUE);
  38. if(isset($post['page']))
  39. {
  40. $page = $this->input->post('page',true);
  41. $perpage = $this->input->post('perpage',true);
  42. $title = $this->input->post('title',true);
  43. $where = "1=1 ";
  44. if($title)
  45. {
  46. $where .= " and title like '%$title%'";
  47. }
  48. //数据排序
  49. $order_str = "px desc,id desc";
  50. if(empty($page))
  51. {
  52. $start = 0;
  53. $perpage = 1;
  54. }
  55. else
  56. {
  57. $start = ($page - 1)*$perpage;
  58. }
  59. //取得信息列表
  60. $info_list = $this->purchase->find_all($where,'id,title,bm,yyid,px',$order_str,$start,$perpage);
  61. foreach ($info_list as $key=>$value)
  62. {
  63. //$country = $this->country->read($value['country']);
  64. //$info_list[$key]['country'] = $country['name'];
  65. }
  66. $total = $this->purchase->find_count($where);
  67. $pagenum = ceil($total/$perpage);
  68. $over = $total-($start+$perpage);
  69. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  70. echo json_encode($rows);exit;
  71. }
  72. $this->_Template('purchase',$this->data);
  73. }
  74. //添加
  75. public function _add()
  76. {
  77. $post = $this->input->post(NULL, TRUE);
  78. if(isset($post['title']))
  79. {
  80. $bm = $this->input->post('bm',true);
  81. $n = $this->purchase->find_all("bm = '$bm'");
  82. if(count($n) > 0)
  83. {
  84. echo json_encode(array('msg'=>'供应商编码必须唯一','success'=>false));exit;
  85. }
  86. if($this->purchase->insert($post))
  87. {
  88. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  89. }
  90. else
  91. {
  92. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  93. }
  94. }
  95. $country = $this->country->find_all();
  96. $this->data['country'] = $country;
  97. $this->_Template('purchase_add',$this->data);
  98. }
  99. //修改
  100. public function _edit($arg_array)
  101. {
  102. $post = $this->input->post(NULL, TRUE);
  103. if(isset($post['id']))
  104. {
  105. $bm = $this->input->post('bm',true);
  106. $n = $this->purchase->find_all("bm = '$bm'");
  107. if(count($n) > 0 && $n[0]['id'] != $post['id'])
  108. {
  109. echo json_encode(array('msg'=>'供应商编码必须唯一','success'=>false));exit;
  110. }
  111. $id = $this->input->post('id',true);
  112. if($this->purchase->save($post,$id))
  113. {
  114. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  115. }
  116. else
  117. {
  118. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  119. }
  120. }
  121. $arg_array = $arg_array[0];
  122. $purchase = $this->purchase->read($arg_array);
  123. $country = $this->country->find_all();
  124. $this->data['country'] = $country;
  125. $this->data['purchase'] = $purchase;
  126. $this->_Template('purchase_edit',$this->data);
  127. }
  128. //删除
  129. public function _del()
  130. {
  131. $post = $this->input->post(NULL, TRUE);
  132. if(isset($post['s']))
  133. {
  134. $id_arr = $this->input->post('s');
  135. $id_arr = explode(',',$id_arr);
  136. if(!$id_arr)
  137. {
  138. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  139. }
  140. //循环删除记录
  141. foreach ($id_arr as $v)
  142. {
  143. $this->purchase->remove($v);
  144. }
  145. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  146. }
  147. }
  148. //获取数据
  149. public function _rows()
  150. {
  151. $post = $this->input->post(NULL, TRUE);
  152. if(isset($post['rows']))
  153. {
  154. $rows = $this->input->post('rows',true);
  155. $an = array();
  156. $data = $this->productprice->find_all();
  157. foreach ($data as $k=>$v)
  158. {
  159. $an[] = array('id'=>$v['id'],'title'=>$v['supplier']);
  160. }
  161. $price = array();
  162. if($rows != "null")
  163. {
  164. $a = $this->purchase->read($rows);//找出次ID的权限内容
  165. if($a['price'])
  166. {
  167. $price = explode('|',trim($a['price'],'|'));//数组化权限内容
  168. }
  169. }
  170. echo json_encode(array('msg'=>($an),'power'=>($price),'success'=>true));
  171. }
  172. }
  173. }