StockController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * 库存控制器
  4. * @author 2015-09-18
  5. */
  6. class StockController extends AdminController {
  7. // 显示库存修改信息首页
  8. public function actionIndex () {
  9. $operate_option = Stock::$operate_option;
  10. $operate = CommonFn::getComboboxData($operate_option, 100, true, 100);
  11. $objectName = Yii::app()->request->getParam('objectName', '');
  12. $stationName = Yii::app()->request->getParam('stationName', '');
  13. $fromStockView = (!empty($objectName) || !empty($stationName)) ? true : false;
  14. // 服务点信息
  15. $criteria = new EMongoCriteria();
  16. $cursor = Station::model()->findAll($criteria);
  17. $rows = CommonFn::getRowsFromCursor($cursor);
  18. $parsedRows = Station::model()->parse($rows);
  19. $station_data = array();
  20. foreach ($parsedRows as $key => $v) {
  21. $id = (string)$v['id'];
  22. $station_data = array_merge($station_data, array($id => array('name' => $v['name'])));
  23. }
  24. $station_data = array_merge($station_data, array('noStation' => array('name' => '无')));
  25. $station = CommonFn::getComboboxData($station_data, 'all', true, 'all');
  26. $data = array(
  27. 'operate' => $operate,
  28. 'objectName' => $objectName,
  29. 'stationName' => $stationName,
  30. 'fromStockView' => $fromStockView,
  31. 'station' => $station
  32. );
  33. $this->render('index', $data);
  34. }
  35. // 库存操作列表
  36. public function actionList () {
  37. $params = CommonFn::getPageParams();
  38. $operate = intval(Yii::app()->request->getParam('operate', 100));
  39. $s_mname = Yii::app()->request->getParam('s_mname', '');
  40. $s_user = Yii::app()->request->getParam('s_user', '');
  41. $date_start = Yii::app()->request->getParam('date_start', '');
  42. $date_end = Yii::app()->request->getParam('date_end', '');
  43. $station = Yii::app()->request->getParam('station', 'all');
  44. $criteria = new EMongoCriteria($params);
  45. // 时间处理
  46. if (!empty($date_start) && !empty($date_end)) {
  47. // 开始时间处理
  48. $timestamp_start = strtotime($date_start);
  49. // 结束时间处理,需通过strtotime()增加一天
  50. $timestamp_end = strtotime('+1 day', strtotime($date_end));
  51. $criteria->time('>=', $timestamp_start);
  52. $criteria->time('<=', $timestamp_end);
  53. }
  54. // 操作筛选
  55. if($operate != 100) {
  56. $criteria->operate('==', $operate);
  57. }
  58. // 根据物资名称搜索
  59. if ($s_mname != '') {
  60. if (CommonFn::isMongoId($s_mname)){
  61. $criteria->mid('==',new MongoId($s_mname));
  62. } else {
  63. $criteria->mname = new MongoRegex('/' . $s_mname . '/');
  64. }
  65. }
  66. // 根据目标用户名搜索
  67. if ($s_user != '') {
  68. if (CommonFn::isMongoId($s_user)){
  69. $criteria->object('==', new MongoId($s_user));
  70. } else {
  71. $criteria->objectName = new MongoRegex('/' . $s_user . '/');
  72. }
  73. }
  74. // 根据服务点搜索
  75. if ($station != 'all') {
  76. if ($station == 'noStation') {
  77. $criteria->stationName('==', '无');
  78. } else {
  79. $criteria->station('==', new MongoId($station));
  80. }
  81. }
  82. $cursor = Stock::model()->findAll($criteria);
  83. $total = $cursor->count();
  84. $rows = CommonFn::getRowsFromCursor($cursor);
  85. $parsedRows = Stock::model()->parse($rows);
  86. // $this->p($parsedRows);die;
  87. echo CommonFn::composeDatagridData($parsedRows, $total);
  88. }
  89. // 编辑库存操作备注
  90. public function actionEdit () {
  91. // echo "<pre>";
  92. // print_r($_POST);die;
  93. $id = Yii::app()->request->getParam('id', '');
  94. $objectName = Yii::app()->request->getParam('objectName', '');
  95. $station = Yii::app()->request->getParam('station', '');
  96. $remarks = Yii::app()->request->getParam('remarks', '');
  97. $criteria = new EMongoCriteria();
  98. $criteria->_id = new MongoId($id);
  99. $stock = Stock::model()->find($criteria);
  100. if (empty($stock)) {
  101. CommonFn::requestAjax(false, '该条操作不存在');
  102. }
  103. $arr_addStocks = array();
  104. // 获取目标用户信息
  105. if ($objectName != $stock->objectName) {
  106. $criteria = new EMongoCriteria();
  107. $criteria->name = $objectName;
  108. $object_info = User::model()->find($criteria);
  109. if (count($object_info) == 0) {
  110. CommonFn::requestAjax(false, '用户不存在,请检查用户名', array());
  111. die;
  112. }
  113. $stock->objectName = $objectName;
  114. $stock->object = $object_info->_id;
  115. $arr_addStocks[] = 'object';
  116. $arr_addStocks[] = 'objectName';
  117. }
  118. // 获取服务点信息
  119. if ((string)$station != (string)$stock->station) {
  120. $station_id = new MongoId($station);
  121. $criteria = new EMongoCriteria();
  122. $criteria->_id = $station_id;
  123. $station_info = Station::model()->find($criteria);
  124. $stock->stationName = $station_info->name;
  125. $stock->station = $station_id;
  126. $arr_addStocks[] = 'stationName';
  127. $arr_addStocks[] = 'station';
  128. }
  129. $arr_addStocks[] = 'remarks';
  130. $stock->remarks = $remarks;
  131. $success = $stock->save(true, $arr_addStocks);
  132. CommonFn::requestAjax($success, '', array());
  133. }
  134. // 自动填充服务点信息
  135. public function actionSelectStation () {
  136. $station = Yii::app()->request->getParam('station', '');
  137. $criteria = new EMongoCriteria();
  138. $criteria->name = new MongoRegex('/'.$station.'/');
  139. $cursor = Station::model()->findAll($criteria);
  140. $rows = CommonFn::getRowsFromCursor($cursor);
  141. if (empty($rows)) {
  142. $arr = array(
  143. 'id' => 0,
  144. 'data' => ''
  145. );
  146. } else {
  147. foreach ($rows as $key => $v) {
  148. $arr[] = array(
  149. 'id' => $key,
  150. 'data' => $v['name']
  151. );
  152. }
  153. }
  154. echo json_encode($arr);
  155. }
  156. /**
  157. * 几个私有方法
  158. * p 使用<pre>标签打印数据
  159. * checkAction 检查减小库存的操作是否超过最小库存量
  160. * checkStock 检查库存量所处的状态
  161. * unit 判断单位类型
  162. */
  163. protected function p($arr) {
  164. echo '<pre>';
  165. print_r($arr);
  166. echo '</pre>';
  167. }
  168. // 检查库存是否充足(针对减小库存的操作)
  169. protected function checkAction () {
  170. // TODO...
  171. }
  172. // 修改库存后检查库存的状态(针对所有操作)
  173. protected function checkStock () {
  174. // TODO...
  175. }
  176. // 判断单位类型
  177. protected function unit ($type) {
  178. switch ($type) {
  179. case 1:
  180. $unit = '瓶';
  181. break;
  182. case 2:
  183. $unit = '袋';
  184. break;
  185. case 3:
  186. $unit = '盒';
  187. break;
  188. case 4:
  189. $unit = '台';
  190. break;
  191. case 5:
  192. $unit = '件';
  193. break;
  194. default:
  195. $unit = '未找到相应单位';
  196. break;
  197. }
  198. return $unit;
  199. }
  200. }
  201. ?>