| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Session;
- use think\Db;
- /**
- * 配方管理
- *
- * @icon fa fa-circle-o
- */
- class Formula extends Backend
- {
- /**
- * Formula模型对象
- * @var \app\admin\model\Formula
- */
- protected $model = null;
- protected $searchFields = "name";
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Formula;
- $this->view->assign("examineStatusList", $this->model->getExamineStatusList());
- $this->view->assign("statusList", $this->model->getStatusList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- *
- * @return string|Json
- * @throws \think\Exception
- * @throws DbException
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- //如果发送的来源是 Selectpage,则转发到 Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
- $user_info = Session::get('admin');
- $map = [];
- if ($user_info['id'] !== 1){
- $map['company_id'] = $user_info['company_id'];
- $map['examine_status'] = 2;
- }
- $list = $this->model
- ->where($where)
- ->where($map)
- ->order($sort, $order)
- ->paginate($limit);
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
- /**
- * 添加
- *
- * @return string
- * @throws \think\Exception
- */
- public function add()
- {
- if (false === $this->request->isPost()) {
- return $this->view->fetch();
- }
- // $ceshi = encrypt('111');
- // $ceshitwo = decode('eGWIu/HvS5g4CbzdItvkig==');
- // print_r($ceshi);
- // print_r('-------------------------------');
- // print_r($ceshitwo);die;
- $base = $this->request->post('baseData/a');
- $formula = $this->request->post('formulaData/a');
- if (empty($base) || empty($formula)){
- $this->error('数据不能为空');
- }
- $params = [];
- $params['name'] = $base[0];
- $params['formula_no'] = $base[1];
- $params['charge_name'] = $base[2];
- $params['examine_name'] = $base[3];
- $params['remark'] = $base[4];
- $params['version'] = $base[5];
- $params['date'] = $base[6];
- if ($base[7] !== 99){
- $customer= explode(',',$base[7]);
- // print_r($base[7]);die;
- $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
- $usability = implode(',',$customer_id);
- }
- $params['usability'] = $usability;
- $params['create'] = date('Y-m-d H:i:s');
- $user_info = Session::get('admin');
- $params['user_id'] = $user_info['id'];
- $params['company_id'] = $user_info['company_id'];
- $result = false;
- Db::startTrans();
- try {
- $pid = Db::name('formula')->insertGetId($params);
- if (!$pid){
- Db::rollback();
- $this->error('数据未插入,请重新操作');
- }
- $data = [];
- $is_replace = 0;
- foreach($formula as $key=>$value){
- if (strpos($value[0],'/') === false){
- $data[$key]['is_replace'] = 0;
- }else{
- $data[$key]['is_replace'] = 1;
- $is_replace = 1;
- }
- $data[$key]['material'] = $value[0];
- $data[$key]['percentage'] = encrypt($value[1]);
- $data[$key]['gy_name'] = $value[2];
- $data[$key]['gy_num'] = $value[3];
- $data[$key]['pid'] = $pid;
- $data[$key]['version'] = $params['version'];
- $data[$key]['create'] = $params['create'];
- }
- $result = Db::name('formula_detail')->insertAll($data);
- //有替代料的话 查出所有替代料 然后分解 插入到数据库
- if ($is_replace == 1){
- $replace = Db::name('formula_detail')->where('pid',$pid)->where('is_replace',1)->field('id,material')->select();
- $replaceData = [];
- $j = 0;
- foreach ($replace as $k=>$v){
- $material = explode('/',$v['material']);
- for ($i=0;$i<count($material);$i++){
- $replaceData[$j]['fid'] = $v['id'];
- $replaceData[$j]['material'] = $material[$i];
- $replaceData[$j]['create'] = date('Y-m-d H:i:s');
- $j++;
- }
- }
- $result = Db::name('formula_replace')->insertAll($replaceData);
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('No rows were inserted'));
- }
- $this->success();
- }
- /**
- * 编辑
- *
- * @param $ids
- * @return string
- * @throws DbException
- * @throws \think\Exception
- */
- public function edit($ids = null)
- {
- if (!$ids) {
- $this->error(__('No Results were found'));
- }
- if (false === $this->request->isPost()) {
- $list = Db::name('formula')->where('id',$ids)->find();
- //可用性,对应客户名称
- if ($list['usability'] != 99){
- $customer = explode(',',$list['usability']);
- $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
- }
- $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
- foreach ($list['gyinfo'] as $key=>$value){
- $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
- }
- $this->view->assign('ids',$ids);
- $this->view->assign('name',$name);
- $this->view->assign('row', $list);
- return $this->view->fetch();
- }
- /**
- * 厂家变更?
- * 原材料变更?
- * 百分比变更? ->工艺信息变更生成新配方
- * 工艺变更?
- * $change 0:未更改工艺信息 1:原材料更改 2:百分比更改 3:操作工艺更改 4:增加或减少工艺信息
- */
- $base = $this->request->post('baseData/a');
- $formula = $this->request->post('formulaData/a');
- if (empty($base) || empty($formula)){
- $this->error('数据不能为空');
- }
- $version = Db::name('formula')->where('name',$base[0])->order('id desc')->value('version');
- $version = substr($version,1);//获取当前最新版本号,后续看工艺情况是否更改
- //基础数据
- $params = [];
- $params['name'] = $base[0];
- $params['formula_no'] = $base[1];
- $params['charge_name'] = $base[2];
- $params['examine_name'] = $base[3];
- $params['remark'] = $base[4];
- $params['date'] = $base[6];
- //配方对应客户id(可用性)
- if ($base[7] !== 99){
- $customer= explode(',',$base[7]);
- $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id');
- $usability = implode(',',$customer_id);
- }
- $params['usability'] = $usability;
- $params['create'] = date('Y-m-d H:i:s');
- $user_info = Session::get('admin');
- $params['user_id'] = $user_info['id'];
- $params['company_id'] = $user_info['company_id'];
- $params['update'] = date('Y-m-d H:i:s');
- //数据库的工艺数据
- $gy_data = Db::name('formula_detail')->where('pid',$ids)->field('id,material,percentage,gy_name')->order('id asc')->select();
- //提交的工艺数据
- $data = [];
- $change = 0;
- $materialChange = 0; //原材料变更
- $percentageChange = 0; //百分比变更
- $gy_nameChange = 0; //工艺信息变更
- //总工艺已更改,版本号X位直接增加1
- if (count($gy_data) != count($formula)){
- $change = 1;
- }
- foreach($formula as $key=>$value){
- foreach ($gy_data as $k=>$v){
- //比对同一行原材料 百分比 工艺信息
- if ($key == $k){
- if ($value[0] != $gy_data[$k]['material']){
- $materialChange = 1;
- }
- if ($value[1] != $gy_data[$k]['percentage']){
- $percentageChange = 1;
- }
- if($value[2] != $gy_data[$k]['gy_name']){
- $gy_nameChange = 1;
- }
- }
- }
- }
- //更改版本号
- if ($change === 1){
- $version = intval($version) + 1;
- }else{
- if ($materialChange === 1){
- $version = intval($version) + 1;
- }else{
- if ($percentageChange === 1 || $gy_nameChange === 1){
- $version = $version + 0.1;
- }
- }
- }
- if (is_int($version)){
- $version = $version.'.0';
- }
- //根据版本号的变化来判断工艺有没有变化
- if ($version == substr($base[5],1)){
- $isChange = false;
- }else{
- $isChange = true;
- }
- $params['version'] = 'v'.$version;
- $result = true;
- Db::startTrans();
- try {
- if ($isChange === true){
- $ids = Db::name('formula')->insertGetId($params);
- }else{
- Db::name('formula')->where('id',$ids)->update($params);
- }
- //新工艺信息
- $is_replace = 0;
- for($i=0;$i<count($formula);$i++){
- if (strpos($formula[$i][0],'/') === false){
- $data[$i]['is_replace'] = 0;
- }else{
- $data[$i]['is_replace'] = 1;
- $is_replace = 1;
- }
- $data[$i]['material'] = $formula[$i][0];
- $data[$i]['percentage'] = encrypt($formula[$i][1]);
- $data[$i]['gy_name'] = $formula[$i][2];
- $data[$i]['gy_num'] = $formula[$i][3];
- $data[$i]['pid'] = $ids;
- $data[$i]['version'] = 'v'.$version;
- $data[$i]['create'] = date('Y-m-d H:i:s');
- }
- if ($isChange === true){
- Db::name('formula_detail')->insertAll($data);
- }
- //有替代料的话 查出所有替代料 然后分解 插入到数据库
- if ($is_replace == 1 && ($materialChange == 1 || $change == 1)){
- $replace = Db::name('formula_detail')->where('pid',$ids)->where('is_replace',1)->field('id,material')->select();
- $replaceData = [];
- $j = 0;
- foreach ($replace as $k=>$v){
- $material = explode('/',$v['material']);
- for ($i=0;$i<count($material);$i++){
- $replaceData[$j]['fid'] = $v['id'];
- $replaceData[$j]['material'] = $material[$i];
- $replaceData[$j]['create'] = date('Y-m-d H:i:s');
- $j++;
- }
- }
- $result = Db::name('formula_replace')->insertAll($replaceData);
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if (false === $result) {
- $this->error(__('No rows were updated'));
- }
- $this->success();
- }
- //生成作业单
- public function task(){
- $ids = input('ids');
- if (!$ids) {
- $this->error(__('No Results were found'));
- }
- if (false === $this->request->isPost()) {
- $list = Db::name('formula')->where('id',$ids)->find();
- $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
- foreach ($list['gyinfo'] as $key=>$value){
- $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
- }
- $this->view->assign('ids',$ids);
- $this->view->assign('row', $list);
- return $this->view->fetch();
- }
- $base = $this->request->post('baseData/a');
- if (empty($base)){
- $this->error('数据不能为空');
- }
- //基础数据
- $params = [];
- $params['fid'] = $base[0];
- $params['name'] = $base[1];
- $params['bach'] = $base[2];
- $params['drawer_name'] = $base[3];
- $params['examine_name'] = $base[4];
- $params['number'] = $base[5];
- $params['remark'] = $base[6];
- $params['create'] = date('Y-m-d H:i:s');
- $result = false;
- Db::startTrans();
- try {
- $result = Db::name('task')->insert($params);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if (false === $result) {
- $this->error(__('No rows were updated'));
- }
- $this->success();
- }
- //配方审核列表
- public function examine(){
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- //如果发送的来源是 Selectpage,则转发到 Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
- $user_info = Session::get('admin');
- $map = [];
- $map['examine_status'] = 1;
- if ($user_info['id'] !== 1){
- $map['company_id'] = $user_info['company_id'];
- }
- $list = $this->model
- ->where($where)
- ->where($map)
- ->order($sort, $order)
- ->paginate($limit);
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
- //审核操作
- public function status($ids=null){
- if (!$ids) {
- $this->error(__('No Results were found'));
- }
- if (false === $this->request->isPost()) {
- $list = Db::name('formula')->where('id',$ids)->find();
- //可用性,对应客户名称
- if ($list['usability'] != 99){
- $customer = explode(',',$list['usability']);
- $name = Db::name('customer')->where('id','in',$customer)->column('customer_name');
- }
- $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select();
- foreach ($list['gyinfo'] as $key=>$value){
- $list['gyinfo'][$key]['percentage'] = decode($value['percentage']);
- }
- $this->view->assign('ids',$ids);
- $this->view->assign('name',$name);
- $this->view->assign('row', $list);
- return $this->view->fetch();
- }
- $status = $this->request->post('status');
- if (!isset($status) || !isset($ids)){
- $this->error('审核失败');
- }
- $params = [];
- $params['examine_status'] = $status;
- $params['update'] = date('Y-m-d H:i:s');
- $result = false;
- Db::startTrans();
- try {
- $list = Db::name('formula')->where('id',$ids)->find();
- if ($list['version'] == 'v0.1'){
- Db::name('formula')->where('id',$ids)->setField('version','v1.0');
- Db::name('formula_detail')->where('pid',$ids)->setField('version','v1.0');
- }
- $result = Db::name('formula')->where('id',$ids)->update($params);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result){
- $this->success('更新成功');
- }else{
- $this->error('审核失败');
- }
- }
- //下拉选择获取客户列表
- public function getCustomer(){
- $user_info = Session::get('admin');
- $where = [];
- if ($user_info['id'] !== 1){
- $where['company_id'] =$user_info['company_id'];
- }
- $row = Db::name('customer')->where($where)->field('id,customer_name')->select();
- $result = ['total' => count($row), 'rows' => $row];
- return json($result);
- }
- //获取生产单应加量
- public function getNumber(){
- $params = input('');
- if ($params['ids'] == '' || $params['number'] == '' || !is_numeric($params['number'])){
- return array('status'=>0,'msg'=>'请求参数错误');
- }
- $list = Db::name('formula')->where('id',$params['ids'])->find();
- $gyinfo= Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select();
- if (empty($gyinfo)){
- return array('status'=>0,'msg'=>'数据错误');
- }
- $total = Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->column('percentage');
- if (empty($total)){
- return array('status'=>0,'msg'=>'数据错误');
- }
- foreach ($total as $k=>$v){
- $total[$k] = decode($v);
- }
- $num = array_sum($total);
- foreach ($gyinfo as $key=>$value){
- $gyinfo[$key]['num'] = '';
- $gyinfo[$key]['percentage'] = decode($value['percentage']);
- if (!empty($value['percentage'])){
- $gyinfo[$key]['num'] = number_format($gyinfo[$key]['percentage'] / $num * $params['number'],3);
- }
- }
- $date = date('Y/m/d');
- return array('status'=>1,'data'=>$gyinfo,'formula_no'=>$list['formula_no'],'date'=>$date);
- }
- }
|