Formula.php 27 KB

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