Formula.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Session;
  5. use think\Db;
  6. use think\Log;
  7. use pinyin\Pinyin;
  8. /**
  9. * 配方管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Formula extends Backend
  14. {
  15. /**
  16. * Formula模型对象
  17. * @var \app\admin\model\Formula
  18. */
  19. protected $model = null;
  20. protected $searchFields = "name";
  21. protected $noNeedLogin = ['gyName',"addFormulaChinese"];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\Formula;
  26. $this->view->assign("examineStatusList", $this->model->getExamineStatusList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. // $this->view->assign("gyNameList", \app\admin\model\GyName::select());
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. *
  38. * @return string|Json
  39. * @throws \think\Exception
  40. * @throws DbException
  41. */
  42. public function index()
  43. {
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags', 'trim']);
  46. if (false === $this->request->isAjax()) {
  47. return $this->view->fetch();
  48. }
  49. // 获取搜索框的值
  50. $map = [];
  51. $filter=input('filter');
  52. if($filter){
  53. $filter=urldecode($filter);
  54. $filter=json_decode($filter,TRUE);
  55. foreach($filter as $k=>$v){
  56. $map[$k]=['like',"%{$v}%"];
  57. }
  58. }
  59. //如果发送的来源是 Selectpage,则转发到 Selectpage
  60. if ($this->request->request('keyField')) {
  61. return $this->selectpage();
  62. }
  63. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  64. $user_info = Session::get('admin');
  65. if ($user_info['id'] !== 1){//管理员
  66. $map['company_id'] = $user_info['company_id'];
  67. $map['examine_status'] = 2;
  68. }
  69. if(!empty($map['name'])||!empty($map['material'])){
  70. $sort = 'f.id';
  71. $list = DB::name('formula')
  72. ->where($where)
  73. ->where($map)
  74. ->alias('f')
  75. ->group('f.id')
  76. ->field('f.id,f.name,f.version,f.examine_status,f.create')
  77. ->join('formula_detail fd','fd.pid=f.id','left')
  78. ->order($sort, $order)
  79. ->paginate($limit);
  80. }else{
  81. $list = $this->model
  82. ->where($where)
  83. ->where($map)
  84. ->order($sort, $order)
  85. ->paginate($limit);
  86. }
  87. $result = ['total' => $list->total(), 'rows' => $list->items()];
  88. return json($result);
  89. }
  90. /**
  91. * 添加
  92. *
  93. * @return string
  94. * @throws \think\Exception
  95. */
  96. public function add()
  97. {
  98. if (false === $this->request->isPost()) {
  99. //技术部=>担当人
  100. $jsdd = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jsdd")->order('name desc')->select();
  101. //技术部=>审核人
  102. $jssh = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jssh")->order('name desc')->select();
  103. $this->assign('jsdd',$jsdd);
  104. $this->assign('jssh',$jssh);
  105. return $this->view->fetch();
  106. }
  107. $base = $this->request->post('baseData/a');
  108. $formula = $this->request->post('formulaData/a');
  109. if (empty($base) || empty($formula)){
  110. $this->error('数据不能为空');
  111. }
  112. $params = [];
  113. $params['name'] = $base[0];
  114. $params['no'] = $base[1];
  115. $params['charge_name'] = $base[2];
  116. $params['examine_name'] = $base[3];
  117. $params['remark'] = $base[4];
  118. $params['version'] = $base[5];
  119. $params['date'] = $base[6];
  120. $params['model'] = $base[8];
  121. if ($base[7] !== 99){
  122. $customer= explode(',',$base[7]);
  123. // print_r($base[7]);die;
  124. $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
  125. $usability = implode(',',$customer_id);
  126. }
  127. // $order = Db::name('order')->order('desc id')->find();
  128. $params['usability'] = $usability;
  129. $params['create'] = date('Y-m-d H:i:s');
  130. $user_info = Session::get('admin');
  131. $params['user_id'] = $user_info['id'];
  132. $params['company_id'] = $user_info['company_id'];
  133. $result = false;
  134. Db::startTrans();
  135. try {
  136. $pid = Db::name('formula')->insertGetId($params);
  137. if (!$pid){
  138. Db::rollback();
  139. $this->error('数据未插入,请重新操作');
  140. }
  141. $data = [];
  142. $is_replace = 0;
  143. // $gy_num = 1;
  144. foreach($formula as $key=>$value){
  145. if (strpos($value[0],'/') === false){
  146. $data[$key]['is_replace'] = 0;
  147. }else{
  148. $data[$key]['is_replace'] = 1;
  149. $is_replace = 1;
  150. }
  151. $data[$key]['material'] = $value[0];
  152. $data[$key]['percentage'] = encrypt($value[1]);
  153. $data[$key]['gy_name'] = $value[2];
  154. // $data[$key]['gy_num'] = $gy_num;
  155. $data[$key]['gy_num'] = $value[3];
  156. $data[$key]['pid'] = $pid;
  157. $data[$key]['version'] = $params['version'];
  158. $data[$key]['create'] = $params['create'];
  159. // if(!$value[0]&&!$value[1]&&$value[2]){
  160. // $gy_num++;
  161. // }
  162. }
  163. $result = Db::name('formula_detail')->insertAll($data);
  164. //有替代料的话 查出所有替代料 然后分解 插入到数据库 2022年9月19日14:20:37 替代料功能改变
  165. // if ($is_replace == 1){
  166. // $replace = Db::name('formula_detail')->where('pid',$pid)->where('is_replace',1)->field('id,material')->select();
  167. // $replaceData = [];
  168. // $j = 0;
  169. // foreach ($replace as $k=>$v){
  170. // $material = explode('/',$v['material']);
  171. // for ($i=0;$i<count($material);$i++){
  172. // $replaceData[$j]['fid'] = $v['id'];
  173. // $replaceData[$j]['material'] = $material[$i];
  174. // $replaceData[$j]['create'] = date('Y-m-d H:i:s');
  175. // $j++;
  176. // }
  177. // }
  178. // $result = Db::name('formula_replace')->insertAll($replaceData);
  179. // }
  180. Db::commit();
  181. } catch (Exception $e) {
  182. Db::rollback();
  183. $this->error($e->getMessage());
  184. }
  185. if ($result === false) {
  186. $this->error(__('No rows were inserted'));
  187. }
  188. $this->success();
  189. }
  190. /**
  191. * 编辑
  192. *
  193. * @param $ids
  194. * @return string
  195. * @throws DbException
  196. * @throws \think\Exception
  197. */
  198. public function edit($ids = null)
  199. {
  200. if (!$ids) {
  201. $this->error(__('No Results were found'));
  202. }
  203. if (false === $this->request->isPost()) {
  204. $ids = input('ids');
  205. $formula = Db::name('formula')->field('charge_name,examine_name')->where('id',"=",$ids)->find();
  206. //查询公司
  207. $user_info = Session::get('admin');
  208. $map = [];
  209. if ($user_info['id'] !== 1){
  210. $map['company_id'] = $user_info['company_id'];
  211. }
  212. $customer_name = Db::name('customer')->field('id,customer_name')->where($map)->find();
  213. $this->assign('customer_name',$customer_name);
  214. $customer = Db::name('customer')->field('customer_name')->select();
  215. $this->assign('customer',$customer);
  216. //查询 经办 审核
  217. $jsdd = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jsdd")->order('name desc')->select();
  218. $jssh = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jssh")->order('name desc')->select();
  219. $personnel_jsdd = Db::name('personnel')->where('name','=',$formula['charge_name'])->find();
  220. $personnel_jssh = Db::name('personnel')->where('name','=',$formula['examine_name'])->find();
  221. $this->assign('jsdd',$jsdd);
  222. $this->assign('jssh',$jssh);
  223. $this->assign('personnel_jsdd',$personnel_jsdd);
  224. $this->assign('personnel_jssh',$personnel_jssh);
  225. $list = Db::name('formula')->where('id',$ids)->find();
  226. //可用性,对应客户名称
  227. if ($list['usability'] != 99){
  228. $customer = explode(',',$list['usability']);
  229. $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
  230. }
  231. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
  232. foreach ($list['gyinfo'] as $key=>$value){
  233. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  234. }
  235. // dump($list);die;
  236. $this->view->assign('ids',$ids);
  237. $this->view->assign('name',$name);
  238. $this->view->assign('row', $list);
  239. return $this->view->fetch();
  240. }
  241. /**
  242. * 厂家变更?
  243. * 原材料变更?
  244. * 百分比变更? ->工艺信息变更生成新配方
  245. * 工艺变更?
  246. * $change 0:未更改工艺信息 1:原材料更改 2:百分比更改 3:操作工艺更改 4:增加或减少工艺信息
  247. */
  248. $base = $this->request->post('baseData/a');
  249. $formula = $this->request->post('formulaData/a');
  250. if (empty($base) || empty($formula)){
  251. $this->error('数据不能为空');
  252. }
  253. $version = Db::name('formula')->where('name',$base[0])->order('id desc')->value('version');
  254. $version = substr($version,1);//获取当前最新版本号,后续看工艺情况是否更改
  255. //基础数据
  256. $params = [];
  257. $params['name'] = $base[0];
  258. $params['no'] = $base[1];
  259. $params['charge_name'] = $base[2];
  260. $params['examine_name'] = $base[3];
  261. $params['remark'] = $base[4];
  262. $params['date'] = $base[6];
  263. $params['model'] = $base[8];
  264. //配方对应客户id(可用性)
  265. if ($base[7] !== 99){
  266. $customer= explode(',',$base[7]);
  267. $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
  268. $usability = implode(',',$customer_id);
  269. }
  270. $params['usability'] = $usability;
  271. $params['create'] = date('Y-m-d H:i:s');
  272. $user_info = Session::get('admin');
  273. $params['user_id'] = $user_info['id'];
  274. $params['company_id'] = $user_info['company_id'];
  275. $params['update'] = date('Y-m-d H:i:s');
  276. //数据库的工艺数据
  277. $gy_data = Db::name('formula_detail')->where('pid',$ids)->field('id,material,percentage,gy_name')->order('id asc')->select();
  278. //提交的工艺数据
  279. $data = [];
  280. $change = 0;
  281. $materialChange = 0; //原材料变更
  282. $percentageChange = 0; //百分比变更
  283. $gy_nameChange = 0; //工艺信息变更
  284. //总工艺已更改,版本号X位直接增加1
  285. if (count($gy_data) != count($formula)){
  286. $change = 1;
  287. }
  288. foreach($formula as $key=>$value){
  289. foreach ($gy_data as $k=>$v){
  290. //比对同一行原材料 百分比 工艺信息
  291. if ($key == $k){
  292. if ($value[0] != $gy_data[$k]['material']){
  293. $materialChange = 1;
  294. }
  295. if ($value[1] != $gy_data[$k]['percentage']){
  296. $percentageChange = 1;
  297. }
  298. if($value[2] != $gy_data[$k]['gy_name']){
  299. $gy_nameChange = 1;
  300. }
  301. }
  302. }
  303. }
  304. //更改版本号
  305. if ($change === 1){
  306. $version = intval($version) + 1;
  307. }else{
  308. if ($materialChange === 1){
  309. $version = intval($version) + 1;
  310. }else{
  311. if ($percentageChange === 1 || $gy_nameChange === 1){
  312. $version = $version + 0.1;
  313. }
  314. }
  315. }
  316. if (is_int($version)){
  317. $version = $version.'.0';
  318. }
  319. //根据版本号的变化来判断工艺有没有变化
  320. if ($version == substr($base[5],1)){
  321. $isChange = false;
  322. }else{
  323. $isChange = true;
  324. }
  325. $params['version'] = 'v'.$version;
  326. $result = true;
  327. Db::startTrans();
  328. try {
  329. if ($isChange === true){
  330. $ids = Db::name('formula')->insertGetId($params);
  331. }else{
  332. Db::name('formula')->where('id',$ids)->update($params);
  333. }
  334. //新工艺信息
  335. $is_replace = 0;
  336. for($i=0;$i<count($formula);$i++){
  337. if (strpos($formula[$i][0],'/') === false){
  338. $data[$i]['is_replace'] = 0;
  339. }else{
  340. $data[$i]['is_replace'] = 1;
  341. $is_replace = 1;
  342. }
  343. $data[$i]['material'] = $formula[$i][0];
  344. $data[$i]['percentage'] = encrypt($formula[$i][1]);
  345. $data[$i]['gy_name'] = $formula[$i][2];
  346. $data[$i]['gy_num'] = $formula[$i][3];
  347. // $data[$i]['gy_num'] = $gy_num;
  348. $data[$i]['pid'] = $ids;
  349. $data[$i]['version'] = 'v'.$version;
  350. $data[$i]['create'] = date('Y-m-d H:i:s');
  351. }
  352. if ($isChange === true){
  353. Db::name('formula_detail')->insertAll($data);
  354. }
  355. //有替代料的话 查出所有替代料 然后分解 插入到数据库 //2022年9月19日14:19:09 替代料更改
  356. // if ($is_replace == 1 && ($materialChange == 1 || $change == 1)){
  357. // $replace = Db::name('formula_detail')->where('pid',$ids)->where('is_replace',1)->field('id,material')->select();
  358. // $replaceData = [];
  359. // $j = 0;
  360. // foreach ($replace as $k=>$v){
  361. // $material = explode('/',$v['material']);
  362. // for ($i=0;$i<count($material);$i++){
  363. // $replaceData[$j]['fid'] = $v['id'];
  364. // $replaceData[$j]['material'] = $material[$i];
  365. // $replaceData[$j]['create'] = date('Y-m-d H:i:s');
  366. // $j++;
  367. // }
  368. // }
  369. // $result = Db::name('formula_replace')->insertAll($replaceData);
  370. // }
  371. Db::commit();
  372. } catch (Exception $e) {
  373. Db::rollback();
  374. $this->error($e->getMessage());
  375. }
  376. if (false === $result) {
  377. $this->error(__('No rows were updated'));
  378. }
  379. $this->success();
  380. }
  381. //生成作业单
  382. public function task(){
  383. // $ceshi = Db::name('formula_detail')->where('id','>',52459)->limit(10000)->select();
  384. // foreach ($ceshi as $key=>$value){
  385. // $ceshi[$key]['percentage'] = encrypt($value['percentage']);
  386. // }
  387. // Db::name('formula_detail_c')->insertAll($ceshi);
  388. // print_r('-------------------------------');
  389. // die;
  390. $ids = input('ids');
  391. if (!$ids) {
  392. $this->error(__('No Results were found'));
  393. }
  394. if (false === $this->request->isPost()) {
  395. //生产部=>开票人
  396. $sckp = Db::name('personnel')->where('bid',"=",3)->where('position','=',"sckp")->order('name desc')->select();
  397. //生产部=>审核人
  398. $scsh = Db::name('personnel')->where('bid',"=",3)->where('position','=',"scsh")->order('name desc')->select();
  399. $this->assign('sckp',$sckp);
  400. $this->assign('scsh',$scsh);
  401. //批次号顺序新增
  402. $tastbach = Db::name('task')->order('bach desc')->find();
  403. $bach = $tastbach['bach'] +1;
  404. $this->view->assign('bach',$bach);
  405. $list = Db::name('formula')->where('id',$ids)->find();
  406. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  407. foreach ($list['gyinfo'] as $key=>$value){
  408. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  409. }
  410. //新增关联订单
  411. $order = Db::name('order')->where('status','neq',3)->field('id,customer,product,number,completed')->select();
  412. $uncompleted_order = array();
  413. if (!empty($order)){
  414. foreach($order as $k=>$v){
  415. $uncompleted_order[$k]['id'] = $v['id'];
  416. if (empty($v['completed'])){
  417. $completed = 0;
  418. }else{
  419. $completed = $v['completed'];
  420. }
  421. $uncompleted_order[$k]['str'] = $v['id'].'、'.$v['customer'].'-'.$v['product'].'-'.'总数量('.$v['number'].'kg)'.'-' .'已完成('.$completed.'kg)';
  422. }
  423. }
  424. $this->view->assign('ids',$ids);
  425. $this->view->assign('order',$uncompleted_order);
  426. $this->view->assign('machineList',\app\admin\model\Machine::select());
  427. $this->view->assign('row', $list);
  428. return $this->view->fetch();
  429. }
  430. $base = $this->request->post('baseData/a');
  431. // print_r($base);die;
  432. if (empty($base)){
  433. $this->error('数据不能为空');
  434. }
  435. //基础数据
  436. $params = [];
  437. $params['fid'] = $base[0];
  438. $params['name'] = $base[1];
  439. $params['bach'] = $base[2];
  440. $params['drawer_name'] = $base[3];
  441. $params['examine_name'] = $base[4];
  442. $params['number'] = $base[5];
  443. $params['remark'] = $base[6];
  444. $params['machine'] = $base[7];
  445. $params['oid'] = $base[8];
  446. $params['create'] = date('Y-m-d H:i:s');
  447. $result = false;
  448. Db::startTrans();
  449. try {
  450. // Db::name('order')->where('id',$params['oid'])->setField('status',2);
  451. $result = Db::name('task')->insert($params);
  452. //更改订单已完成数量,修改订单状态
  453. $order_info = Db::name('order')->where('id',$params['oid'])->find();
  454. if (empty($order_info['completed'])){
  455. $new_completed = $params['number'];
  456. $order_status = 2;//生产中
  457. Db::name('order')->where('id',$params['oid'])->setField('status',$order_status);
  458. }else{
  459. $new_completed = $params['number'] + $order_info['completed'];
  460. }
  461. Db::name('order')->where('id',$params['oid'])->setField('completed',$new_completed);
  462. Db::commit();
  463. } catch (Exception $e) {
  464. Db::rollback();
  465. $this->error($e->getMessage());
  466. }
  467. if (false === $result) {
  468. $this->error(__('No rows were updated'));
  469. }
  470. $this->success();
  471. }
  472. //配方审核列表
  473. public function examine(){
  474. //设置过滤方法
  475. $this->request->filter(['strip_tags', 'trim']);
  476. if (false === $this->request->isAjax()) {
  477. return $this->view->fetch();
  478. }
  479. //如果发送的来源是 Selectpage,则转发到 Selectpage
  480. if ($this->request->request('keyField')) {
  481. return $this->selectpage();
  482. }
  483. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  484. $user_info = Session::get('admin');
  485. $map = [];
  486. $map['examine_status'] = 1;
  487. if ($user_info['id'] !== 1){
  488. $map['company_id'] = $user_info['company_id'];
  489. }
  490. $list = $this->model
  491. ->where($where)
  492. ->where($map)
  493. ->order($sort, $order)
  494. ->paginate($limit);
  495. $result = ['total' => $list->total(), 'rows' => $list->items()];
  496. return json($result);
  497. }
  498. //审核操作
  499. public function status($ids=null){
  500. if (!$ids) {
  501. $this->error(__('No Results were found'));
  502. }
  503. if (false === $this->request->isPost()) {
  504. $list = Db::name('formula')->where('id',$ids)->find();
  505. //可用性,对应客户名称
  506. if ($list['usability'] != 99){
  507. $customer = explode(',',$list['usability']);
  508. $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
  509. }
  510. $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
  511. foreach ($list['gyinfo'] as $key=>$value){
  512. $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
  513. }
  514. $this->view->assign('ids',$ids);
  515. $this->view->assign('name',$name);
  516. $this->view->assign('row', $list);
  517. return $this->view->fetch();
  518. }
  519. $status = $this->request->post('status');
  520. if (!isset($status) || !isset($ids)){
  521. $this->error('审核失败');
  522. }
  523. $params = [];
  524. $params['examine_status'] = $status;
  525. $params['update'] = date('Y-m-d H:i:s');
  526. $result = false;
  527. Db::startTrans();
  528. try {
  529. $list = Db::name('formula')->where('id',$ids)->find();
  530. if ($list['version'] == 'v0.1'){
  531. Db::name('formula')->where('id',$ids)->setField('version','v1.0');
  532. Db::name('formula_detail')->where('pid',$ids)->setField('version','v1.0');
  533. }
  534. $result = Db::name('formula')->where('id',$ids)->update($params);
  535. Db::commit();
  536. } catch (Exception $e) {
  537. Db::rollback();
  538. $this->error($e->getMessage());
  539. }
  540. if ($result){
  541. $this->addFormulaChinese($ids);
  542. $this->success('更新成功');
  543. }else{
  544. $this->error('审核失败');
  545. }
  546. }
  547. //下拉选择获取客户列表
  548. public function getCustomer(){
  549. $user_info = Session::get('admin');
  550. $where = [];
  551. if ($user_info['id'] !== 1){
  552. $where['company_id'] =$user_info['company_id'];
  553. }
  554. $row = Db::name('customer')->where($where)->field('id,customer_name')->select();
  555. $result = ['total' => count($row), 'rows' => $row];
  556. return json($result);
  557. }
  558. //获取生产单应加量
  559. public function getNumber(){
  560. $params = input('');
  561. if ($params['ids'] == '' || $params['number'] == '' || !is_numeric($params['number'])){
  562. return array('status'=>0,'msg'=>'请求参数错误');
  563. }
  564. $list = Db::name('formula')->where('id',$params['ids'])->find();
  565. $gyinfo= Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  566. if (empty($gyinfo)){
  567. return array('status'=>0,'msg'=>'数据错误');
  568. }
  569. $total = Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->column('percentage');
  570. if (empty($total)){
  571. return array('status'=>0,'msg'=>'数据错误');
  572. }
  573. foreach ($total as $k=>$v){
  574. $total[$k] = decode($v);
  575. }
  576. $num = array_sum($total);
  577. foreach ($gyinfo as $key=>$value){
  578. if($gyinfo[$key]['gy_name'] == null){//
  579. $gyinfo[$key]['gy_name'] = '';//
  580. }//
  581. $gyinfo[$key]['num'] = '';
  582. $gyinfo[$key]['percentage'] = decode($value['percentage']);
  583. if (!empty($value['percentage'])){
  584. // $gyinfo[$key]['num'] = number_format($gyinfo[$key]['percentage'] / $num * $params['number'],3);
  585. $number = ceil($gyinfo[$key]['percentage'] / $num * $params['number'] *1000);
  586. $gyinfo[$key]['num'] = number_format($number/1000,3);
  587. }
  588. }
  589. $date = date('Y/m/d');
  590. return array('status'=>1,'data'=>$gyinfo,'formula_no'=>$list['formula_no'],'date'=>$date);
  591. }
  592. //获取工艺说明
  593. public function gyName(){
  594. $params = input('gy_name');
  595. // print_r($params);die;
  596. if ($params){
  597. $list = Db::name('formula_detail')->where('gy_name','like','%'.$params.'%')->field('id,gy_name')->limit(20)->select();
  598. }else{
  599. $list = Db::name('formula_detail')->where('gy_name','neq','')->field('id,gy_name')->limit(20)->select();
  600. }
  601. $total = count($list);
  602. $result = ['total'=>$total,'rows'=>$list];
  603. // return array('status'=>1,'rows'=>$list);
  604. return json($result);
  605. }
  606. public function addFormulaChinese($ids = null){
  607. $date = date('Ymd',time());
  608. if (!$ids){
  609. Log::mylog('formula_log','id='.$ids.'未获取到',$date.'_01');
  610. }
  611. $formula = Db::name('formula')->where('id',$ids)->field('name')->find();
  612. if (!$formula){
  613. Log::mylog('formula_log','id='.$ids.'未获取到配方数据',$date.'_01');
  614. }
  615. $material = Db::name('formula_detail')->where('pid',$ids)->field('id,material')->select();
  616. if (!$material){
  617. Log::mylog('formula_log','pid='.$ids.'未获取到配方详情数据',$date.'_01');
  618. }
  619. $list = array();
  620. $pinyin = new pinyin();
  621. foreach ($material as $key=>$value){
  622. if (preg_match("/[\x7f-\xff]/", $value['material'])){
  623. $str = $value['material'];
  624. $newStr = $pinyin->getpy($str,true,true);
  625. $chinese = array();
  626. for ($i=0;$i<strlen($newStr);$i++){
  627. $string = substr($newStr,$i,1);
  628. if (!preg_match_all('/\b([a-z]+)\b/', $string)){
  629. $chinese[$i] = $string;
  630. }
  631. }
  632. $list[$key]['f_id'] = $ids;
  633. $list[$key]['f_name'] = $formula['name'];
  634. $list[$key]['m_id'] = $value['id'];
  635. $list[$key]['m_name'] = $value['material'];
  636. $list[$key]['name'] = implode($chinese);
  637. }
  638. }
  639. // dump($list);die;
  640. if (!empty($list)){
  641. $result = true;
  642. $is_has = Db::name('formula_material')->where('f_id',$ids)->select();
  643. if ($is_has){
  644. Db::startTrans();
  645. try {
  646. Db::name('formula_material')->where('f_id',$ids)->delete();
  647. Db::name('formula_material')->insertAll($list);
  648. Db::commit();
  649. }catch (Exception $e){
  650. $result = false;
  651. Db::rollback();
  652. }
  653. }else{
  654. $res = Db::name('formula_material')->insertAll($list);
  655. if (!$res){
  656. $result = false;
  657. }
  658. }
  659. if (!$result){
  660. Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入失败',$date.'_01');
  661. }else{
  662. Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入成功',$date.'_01');
  663. }
  664. }
  665. }
  666. }