Formula.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Session;
  5. use think\Db;
  6. /**
  7. * 配方管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Formula extends Backend
  12. {
  13. /**
  14. * Formula模型对象
  15. * @var \app\admin\model\Formula
  16. */
  17. protected $model = null;
  18. protected $searchFields = "name";
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Formula;
  23. $this->view->assign("examineStatusList", $this->model->getExamineStatusList());
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. *
  34. * @return string|Json
  35. * @throws \think\Exception
  36. * @throws DbException
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if (false === $this->request->isAjax()) {
  43. return $this->view->fetch();
  44. }
  45. //如果发送的来源是 Selectpage,则转发到 Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  50. $user_info = Session::get('admin');
  51. $map = [];
  52. if ($user_info['id'] !== 1){
  53. $map['company_id'] = $user_info['company_id'];
  54. }
  55. $list = $this->model
  56. ->where($where)
  57. ->where($map)
  58. ->order($sort, $order)
  59. ->paginate($limit);
  60. $result = ['total' => $list->total(), 'rows' => $list->items()];
  61. return json($result);
  62. }
  63. /**
  64. * 添加
  65. *
  66. * @return string
  67. * @throws \think\Exception
  68. */
  69. public function add()
  70. {
  71. if (false === $this->request->isPost()) {
  72. return $this->view->fetch();
  73. }
  74. $base = $this->request->post('baseData/a');
  75. $formula = $this->request->post('formulaData/a');
  76. if (empty($base) || empty($formula)){
  77. $this->error('数据不能为空');
  78. }
  79. $params = [];
  80. $params['name'] = $base[0];
  81. $params['formula_no'] = $base[1];
  82. $params['charge_name'] = $base[2];
  83. $params['examine_name'] = $base[3];
  84. $params['remark'] = $base[4];
  85. $params['version'] = $base[5];
  86. $params['date'] = $base[6];
  87. if ($base[7] !== 99){
  88. $customer= explode(',',$base[7]);
  89. // print_r($base[7]);die;
  90. $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
  91. $usability = implode(',',$customer_id);
  92. }
  93. $params['usability'] = $usability;
  94. $params['create'] = date('Y-m-d H:i:s');
  95. $user_info = Session::get('admin');
  96. $params['user_id'] = $user_info['id'];
  97. $params['company_id'] = $user_info['company_id'];
  98. $result = false;
  99. Db::startTrans();
  100. try {
  101. $pid = Db::name('formula')->insertGetId($params);
  102. if (!$pid){
  103. Db::rollback();
  104. $this->error('数据未插入,请重新操作');
  105. }
  106. $data = [];
  107. $is_replace = 0;
  108. foreach($formula as $key=>$value){
  109. $data[$key]['material'] = $value[0];
  110. $data[$key]['percentage'] = $value[1];
  111. $data[$key]['gy_name'] = $value[2];
  112. $data[$key]['gy_num'] = $value[3];
  113. if (strpos($value[0],'/') === false){
  114. $data[$key]['is_replace'] = 0;
  115. }else{
  116. $data[$key]['is_replace'] = 1;
  117. $is_replace = 1;
  118. }
  119. $data[$key]['pid'] = $pid;
  120. $data[$key]['version'] = $params['version'];
  121. $data[$key]['create'] = $params['create'];
  122. }
  123. $result = Db::name('formula_detail')->insertAll($data);
  124. //有替代料的话 查出所有替代料 然后分解 插入到数据库
  125. if ($is_replace == 1){
  126. $replace = Db::name('formula_detail')->where('pid',$pid)->where('is_replace',1)->field('id,material')->select();
  127. $replaceData = [];
  128. $j = 0;
  129. foreach ($replace as $k=>$v){
  130. $material = explode('/',$v['material']);
  131. for ($i=0;$i<count($material);$i++){
  132. $replaceData[$j]['fid'] = $v['id'];
  133. $replaceData[$j]['material'] = $material[$i];
  134. $replaceData[$j]['create'] = date('Y-m-d H:i:s');
  135. $j++;
  136. }
  137. }
  138. $result = Db::name('formula_replace')->insertAll($replaceData);
  139. }
  140. Db::commit();
  141. } catch (Exception $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. }
  145. if ($result === false) {
  146. $this->error(__('No rows were inserted'));
  147. }
  148. $this->success();
  149. }
  150. /**
  151. * 编辑
  152. *
  153. * @param $ids
  154. * @return string
  155. * @throws DbException
  156. * @throws \think\Exception
  157. */
  158. public function edit($ids = null)
  159. {
  160. if (!$ids) {
  161. $this->error(__('No Results were found'));
  162. }
  163. if (false === $this->request->isPost()) {
  164. $list = Db::name('formula')->where('id',$ids)->find();
  165. //可用性,对应客户名称
  166. if ($list['usability'] != 99){
  167. $customer = explode(',',$list['usability']);
  168. $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
  169. }
  170. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
  171. $this->view->assign('ids',$ids);
  172. $this->view->assign('name',$name);
  173. $this->view->assign('row', $list);
  174. return $this->view->fetch();
  175. }
  176. /**
  177. * 厂家变更?
  178. * 原材料变更?
  179. * 百分比变更? ->工艺信息变更生成新配方
  180. * 工艺变更?
  181. * $change 0:未更改工艺信息 1:原材料更改 2:百分比更改 3:操作工艺更改 4:增加或减少工艺信息
  182. */
  183. $base = $this->request->post('baseData/a');
  184. $formula = $this->request->post('formulaData/a');
  185. if (empty($base) || empty($formula)){
  186. $this->error('数据不能为空');
  187. }
  188. $version = Db::name('formula')->where('name',$base[0])->order('id desc')->value('version');
  189. $version = substr($version,1);//获取当前最新版本号,后续看工艺情况是否更改
  190. //基础数据
  191. $params = [];
  192. $params['name'] = $base[0];
  193. $params['formula_no'] = $base[1];
  194. $params['charge_name'] = $base[2];
  195. $params['examine_name'] = $base[3];
  196. $params['remark'] = $base[4];
  197. $params['date'] = $base[6];
  198. //配方对应客户id(可用性)
  199. if ($base[7] !== 99){
  200. $customer= explode(',',$base[7]);
  201. $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
  202. $usability = implode(',',$customer_id);
  203. }
  204. $params['usability'] = $usability;
  205. $params['create'] = date('Y-m-d H:i:s');
  206. $user_info = Session::get('admin');
  207. $params['user_id'] = $user_info['id'];
  208. $params['company_id'] = $user_info['company_id'];
  209. $params['update'] = date('Y-m-d H:i:s');
  210. //数据库的工艺数据
  211. $gy_data = Db::name('formula_detail')->where('pid',$ids)->field('id,material,percentage,gy_name')->order('id asc')->select();
  212. //提交的工艺数据
  213. $data = [];
  214. $change = 0;
  215. $materialChange = 0; //原材料变更
  216. $percentageChange = 0; //百分比变更
  217. $gy_nameChange = 0; //工艺信息变更
  218. //总工艺已更改,版本号X位直接增加1
  219. if (count($gy_data) != count($formula)){
  220. $change = 1;
  221. }
  222. foreach($formula as $key=>$value){
  223. foreach ($gy_data as $k=>$v){
  224. //比对同一行原材料 百分比 工艺信息
  225. if ($key == $k){
  226. if ($value[0] != $gy_data[$k]['material']){
  227. $materialChange = 1;
  228. }
  229. if ($value[1] != $gy_data[$k]['percentage']){
  230. $percentageChange = 1;
  231. }
  232. if($value[2] != $gy_data[$k]['gy_name']){
  233. $gy_nameChange = 1;
  234. }
  235. }
  236. }
  237. }
  238. //更改版本号
  239. if ($change === 1){
  240. $version = intval($version) + 1;
  241. }else{
  242. if ($materialChange === 1){
  243. $version = intval($version) + 1;
  244. }else{
  245. if ($percentageChange === 1 || $gy_nameChange === 1){
  246. $version = $version + 0.1;
  247. }
  248. }
  249. }
  250. if (is_int($version)){
  251. $version = $version.'.0';
  252. }
  253. //根据版本号的变化来判断工艺有没有变化
  254. if ($version == substr($base[5],1)){
  255. $isChange = false;
  256. }else{
  257. $isChange = true;
  258. }
  259. $params['version'] = 'v'.$version;
  260. $result = true;
  261. Db::startTrans();
  262. try {
  263. if ($isChange === true){
  264. $ids = Db::name('formula')->insertGetId($params);
  265. }else{
  266. Db::name('formula')->where('id',$ids)->update($params);
  267. }
  268. //新工艺信息
  269. $is_replace = 0;
  270. for($i=0;$i<count($formula);$i++){
  271. $data[$i]['material'] = $formula[$i][0];
  272. $data[$i]['percentage'] = $formula[$i][1];
  273. $data[$i]['gy_name'] = $formula[$i][2];
  274. $data[$i]['gy_num'] = $formula[$i][3];
  275. if (strpos($formula[$i][0],'/') === false){
  276. $data[$i]['is_replace'] = 0;
  277. }else{
  278. $data[$i]['is_replace'] = 1;
  279. $is_replace = 1;
  280. }
  281. $data[$i]['pid'] = $ids;
  282. $data[$i]['version'] = 'v'.$version;
  283. $data[$i]['create'] = date('Y-m-d H:i:s');
  284. }
  285. if ($isChange === true){
  286. Db::name('formula_detail')->insertAll($data);
  287. }
  288. //有替代料的话 查出所有替代料 然后分解 插入到数据库
  289. if ($is_replace == 1 && ($materialChange == 1 || $change == 1)){
  290. $replace = Db::name('formula_detail')->where('pid',$ids)->where('is_replace',1)->field('id,material')->select();
  291. $replaceData = [];
  292. $j = 0;
  293. foreach ($replace as $k=>$v){
  294. $material = explode('/',$v['material']);
  295. for ($i=0;$i<count($material);$i++){
  296. $replaceData[$j]['fid'] = $v['id'];
  297. $replaceData[$j]['material'] = $material[$i];
  298. $replaceData[$j]['create'] = date('Y-m-d H:i:s');
  299. $j++;
  300. }
  301. }
  302. $result = Db::name('formula_replace')->insertAll($replaceData);
  303. }
  304. Db::commit();
  305. } catch (Exception $e) {
  306. Db::rollback();
  307. $this->error($e->getMessage());
  308. }
  309. if (false === $result) {
  310. $this->error(__('No rows were updated'));
  311. }
  312. $this->success();
  313. }
  314. //生成作业单
  315. public function task(){
  316. $ids = input('ids');
  317. if (!$ids) {
  318. $this->error(__('No Results were found'));
  319. }
  320. if (false === $this->request->isPost()) {
  321. $list = Db::name('formula')->where('id',$ids)->find();
  322. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
  323. $this->view->assign('ids',$ids);
  324. $this->view->assign('row', $list);
  325. return $this->view->fetch();
  326. }
  327. }
  328. public function getCustomer(){
  329. $user_info = Session::get('admin');
  330. $where = [];
  331. if ($user_info['id'] !== 1){
  332. $where['company_id'] =$user_info['company_id'];
  333. }
  334. $row = Db::name('customer')->where($where)->field('id,customer_name')->select();
  335. $result = ['total' => count($row), 'rows' => $row];
  336. return json($result);
  337. }
  338. }