Formula.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. $map['examine_status'] = 1;
  55. }
  56. $list = $this->model
  57. ->where($where)
  58. ->where($map)
  59. ->order($sort, $order)
  60. ->paginate($limit);
  61. $result = ['total' => $list->total(), 'rows' => $list->items()];
  62. return json($result);
  63. }
  64. /**
  65. * 添加
  66. *
  67. * @return string
  68. * @throws \think\Exception
  69. */
  70. public function add()
  71. {
  72. if (false === $this->request->isPost()) {
  73. return $this->view->fetch();
  74. }
  75. $base = $this->request->post('baseData/a');
  76. $formula = $this->request->post('formulaData/a');
  77. if (empty($base) || empty($formula)){
  78. $this->error('数据不能为空');
  79. }
  80. $params = [];
  81. $params['name'] = $base[0];
  82. $params['formula_no'] = $base[1];
  83. $params['charge_name'] = $base[2];
  84. $params['examine_name'] = $base[3];
  85. $params['remark'] = $base[4];
  86. $params['version'] = $base[5];
  87. $params['date'] = $base[6];
  88. if ($base[7] !== 99){
  89. $customer= explode(',',$base[7]);
  90. // print_r($base[7]);die;
  91. $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
  92. $usability = implode(',',$customer_id);
  93. }
  94. $params['usability'] = $usability;
  95. $params['create'] = date('Y-m-d H:i:s');
  96. $user_info = Session::get('admin');
  97. $params['user_id'] = $user_info['id'];
  98. $params['company_id'] = $user_info['company_id'];
  99. $result = false;
  100. Db::startTrans();
  101. try {
  102. $pid = Db::name('formula')->insertGetId($params);
  103. if (!$pid){
  104. Db::rollback();
  105. $this->error('数据未插入,请重新操作');
  106. }
  107. $data = [];
  108. $is_replace = 0;
  109. foreach($formula as $key=>$value){
  110. $data[$key]['material'] = $value[0];
  111. $data[$key]['percentage'] = $value[1];
  112. $data[$key]['gy_name'] = $value[2];
  113. $data[$key]['gy_num'] = $value[3];
  114. if (strpos($value[0],'/') === false){
  115. $data[$key]['is_replace'] = 0;
  116. }else{
  117. $data[$key]['is_replace'] = 1;
  118. $is_replace = 1;
  119. }
  120. $data[$key]['pid'] = $pid;
  121. $data[$key]['version'] = $params['version'];
  122. $data[$key]['create'] = $params['create'];
  123. }
  124. $result = Db::name('formula_detail')->insertAll($data);
  125. //有替代料的话 查出所有替代料 然后分解 插入到数据库
  126. if ($is_replace == 1){
  127. $replace = Db::name('formula_detail')->where('pid',$pid)->where('is_replace',1)->field('id,material')->select();
  128. $replaceData = [];
  129. $j = 0;
  130. foreach ($replace as $k=>$v){
  131. $material = explode('/',$v['material']);
  132. for ($i=0;$i<count($material);$i++){
  133. $replaceData[$j]['fid'] = $v['id'];
  134. $replaceData[$j]['material'] = $material[$i];
  135. $replaceData[$j]['create'] = date('Y-m-d H:i:s');
  136. $j++;
  137. }
  138. }
  139. $result = Db::name('formula_replace')->insertAll($replaceData);
  140. }
  141. Db::commit();
  142. } catch (Exception $e) {
  143. Db::rollback();
  144. $this->error($e->getMessage());
  145. }
  146. if ($result === false) {
  147. $this->error(__('No rows were inserted'));
  148. }
  149. $this->success();
  150. }
  151. /**
  152. * 编辑
  153. *
  154. * @param $ids
  155. * @return string
  156. * @throws DbException
  157. * @throws \think\Exception
  158. */
  159. public function edit($ids = null)
  160. {
  161. if (!$ids) {
  162. $this->error(__('No Results were found'));
  163. }
  164. if (false === $this->request->isPost()) {
  165. $list = Db::name('formula')->where('id',$ids)->find();
  166. //可用性,对应客户名称
  167. if ($list['usability'] != 99){
  168. $customer = explode(',',$list['usability']);
  169. $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
  170. }
  171. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
  172. $this->view->assign('ids',$ids);
  173. $this->view->assign('name',$name);
  174. $this->view->assign('row', $list);
  175. return $this->view->fetch();
  176. }
  177. /**
  178. * 厂家变更?
  179. * 原材料变更?
  180. * 百分比变更? ->工艺信息变更生成新配方
  181. * 工艺变更?
  182. * $change 0:未更改工艺信息 1:原材料更改 2:百分比更改 3:操作工艺更改 4:增加或减少工艺信息
  183. */
  184. $base = $this->request->post('baseData/a');
  185. $formula = $this->request->post('formulaData/a');
  186. if (empty($base) || empty($formula)){
  187. $this->error('数据不能为空');
  188. }
  189. $version = Db::name('formula')->where('name',$base[0])->order('id desc')->value('version');
  190. $version = substr($version,1);//获取当前最新版本号,后续看工艺情况是否更改
  191. //基础数据
  192. $params = [];
  193. $params['name'] = $base[0];
  194. $params['formula_no'] = $base[1];
  195. $params['charge_name'] = $base[2];
  196. $params['examine_name'] = $base[3];
  197. $params['remark'] = $base[4];
  198. $params['date'] = $base[6];
  199. //配方对应客户id(可用性)
  200. if ($base[7] !== 99){
  201. $customer= explode(',',$base[7]);
  202. $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
  203. $usability = implode(',',$customer_id);
  204. }
  205. $params['usability'] = $usability;
  206. $params['create'] = date('Y-m-d H:i:s');
  207. $user_info = Session::get('admin');
  208. $params['user_id'] = $user_info['id'];
  209. $params['company_id'] = $user_info['company_id'];
  210. $params['update'] = date('Y-m-d H:i:s');
  211. //数据库的工艺数据
  212. $gy_data = Db::name('formula_detail')->where('pid',$ids)->field('id,material,percentage,gy_name')->order('id asc')->select();
  213. //提交的工艺数据
  214. $data = [];
  215. $change = 0;
  216. $materialChange = 0; //原材料变更
  217. $percentageChange = 0; //百分比变更
  218. $gy_nameChange = 0; //工艺信息变更
  219. //总工艺已更改,版本号X位直接增加1
  220. if (count($gy_data) != count($formula)){
  221. $change = 1;
  222. }
  223. foreach($formula as $key=>$value){
  224. foreach ($gy_data as $k=>$v){
  225. //比对同一行原材料 百分比 工艺信息
  226. if ($key == $k){
  227. if ($value[0] != $gy_data[$k]['material']){
  228. $materialChange = 1;
  229. }
  230. if ($value[1] != $gy_data[$k]['percentage']){
  231. $percentageChange = 1;
  232. }
  233. if($value[2] != $gy_data[$k]['gy_name']){
  234. $gy_nameChange = 1;
  235. }
  236. }
  237. }
  238. }
  239. //更改版本号
  240. if ($change === 1){
  241. $version = intval($version) + 1;
  242. }else{
  243. if ($materialChange === 1){
  244. $version = intval($version) + 1;
  245. }else{
  246. if ($percentageChange === 1 || $gy_nameChange === 1){
  247. $version = $version + 0.1;
  248. }
  249. }
  250. }
  251. if (is_int($version)){
  252. $version = $version.'.0';
  253. }
  254. //根据版本号的变化来判断工艺有没有变化
  255. if ($version == substr($base[5],1)){
  256. $isChange = false;
  257. }else{
  258. $isChange = true;
  259. }
  260. $params['version'] = 'v'.$version;
  261. $result = true;
  262. Db::startTrans();
  263. try {
  264. if ($isChange === true){
  265. $ids = Db::name('formula')->insertGetId($params);
  266. }else{
  267. Db::name('formula')->where('id',$ids)->update($params);
  268. }
  269. //新工艺信息
  270. $is_replace = 0;
  271. for($i=0;$i<count($formula);$i++){
  272. $data[$i]['material'] = $formula[$i][0];
  273. $data[$i]['percentage'] = $formula[$i][1];
  274. $data[$i]['gy_name'] = $formula[$i][2];
  275. $data[$i]['gy_num'] = $formula[$i][3];
  276. if (strpos($formula[$i][0],'/') === false){
  277. $data[$i]['is_replace'] = 0;
  278. }else{
  279. $data[$i]['is_replace'] = 1;
  280. $is_replace = 1;
  281. }
  282. $data[$i]['pid'] = $ids;
  283. $data[$i]['version'] = 'v'.$version;
  284. $data[$i]['create'] = date('Y-m-d H:i:s');
  285. }
  286. if ($isChange === true){
  287. Db::name('formula_detail')->insertAll($data);
  288. }
  289. //有替代料的话 查出所有替代料 然后分解 插入到数据库
  290. if ($is_replace == 1 && ($materialChange == 1 || $change == 1)){
  291. $replace = Db::name('formula_detail')->where('pid',$ids)->where('is_replace',1)->field('id,material')->select();
  292. $replaceData = [];
  293. $j = 0;
  294. foreach ($replace as $k=>$v){
  295. $material = explode('/',$v['material']);
  296. for ($i=0;$i<count($material);$i++){
  297. $replaceData[$j]['fid'] = $v['id'];
  298. $replaceData[$j]['material'] = $material[$i];
  299. $replaceData[$j]['create'] = date('Y-m-d H:i:s');
  300. $j++;
  301. }
  302. }
  303. $result = Db::name('formula_replace')->insertAll($replaceData);
  304. }
  305. Db::commit();
  306. } catch (Exception $e) {
  307. Db::rollback();
  308. $this->error($e->getMessage());
  309. }
  310. if (false === $result) {
  311. $this->error(__('No rows were updated'));
  312. }
  313. $this->success();
  314. }
  315. //生成作业单
  316. public function task(){
  317. $ids = input('ids');
  318. if (!$ids) {
  319. $this->error(__('No Results were found'));
  320. }
  321. if (false === $this->request->isPost()) {
  322. $list = Db::name('formula')->where('id',$ids)->find();
  323. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  324. $this->view->assign('ids',$ids);
  325. $this->view->assign('row', $list);
  326. return $this->view->fetch();
  327. }
  328. $base = $this->request->post('baseData/a');
  329. if (empty($base)){
  330. $this->error('数据不能为空');
  331. }
  332. //基础数据
  333. $params = [];
  334. $params['fid'] = $base[0];
  335. $params['name'] = $base[1];
  336. $params['bach'] = $base[2];
  337. $params['drawer_name'] = $base[3];
  338. $params['examine_name'] = $base[4];
  339. $params['number'] = $base[5];
  340. $params['remark'] = $base[6];
  341. $params['create'] = date('Y-m-d H:i:s');
  342. $result = false;
  343. Db::startTrans();
  344. try {
  345. $result = Db::name('task')->insert($params);
  346. Db::commit();
  347. } catch (Exception $e) {
  348. Db::rollback();
  349. $this->error($e->getMessage());
  350. }
  351. if (false === $result) {
  352. $this->error(__('No rows were updated'));
  353. }
  354. $this->success();
  355. }
  356. //下拉选择获取客户列表
  357. public function getCustomer(){
  358. $user_info = Session::get('admin');
  359. $where = [];
  360. if ($user_info['id'] !== 1){
  361. $where['company_id'] =$user_info['company_id'];
  362. }
  363. $row = Db::name('customer')->where($where)->field('id,customer_name')->select();
  364. $result = ['total' => count($row), 'rows' => $row];
  365. return json($result);
  366. }
  367. //获取生产单应加量
  368. public function getNumber(){
  369. $params = input('');
  370. if ($params['ids'] == '' || $params['number'] == '' || !is_numeric($params['number'])){
  371. return array('status'=>0,'msg'=>'请求参数错误');
  372. }
  373. $list = Db::name('formula')->where('id',$params['ids'])->find();
  374. $gyinfo= Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->field('id,material,percentage')->select();
  375. if (empty($gyinfo)){
  376. return array('status'=>0,'msg'=>'数据错误');
  377. }
  378. $total = Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->sum('percentage');
  379. if (empty($total)){
  380. return array('status'=>0,'msg'=>'数据错误');
  381. }
  382. foreach ($gyinfo as $key=>$value){
  383. $gyinfo[$key]['num'] = '';
  384. if (!empty($value['percentage'])){
  385. $gyinfo[$key]['num'] = number_format($value['percentage'] / $total * $params['number'],3);
  386. }
  387. }
  388. $date = date('Y/m/d');
  389. return array('status'=>1,'data'=>$gyinfo,'formula_no'=>$list['formula_no'],'date'=>$date);
  390. }
  391. }