123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197 |
- <?php
- namespace app\common\service;
- use app\common\constant\ApplyConstant;
- use app\common\constant\CommonConstant;
- use app\common\model\Approve;
- use app\common\model\ApproveCopy;
- use app\common\model\ApproveApplyGoods;
- use app\common\model\ApproveMaintain;
- use app\common\model\ApproveStockGoods;
- use app\common\model\ApproveEvectionPeerUser;
- use app\common\model\ApproveInfo;
- use app\common\model\ApproveUseGoods;
- use app\common\model\Goods;
- use app\common\model\GoodsCategory;
- use app\common\model\GoodsStock;
- use app\common\model\User;
- use think\Db;
- use think\Exception;
- /**
- * 审批申请服务类
- */
- class ApproveInfoService
- {
- /**
- * 申请/重新发起
- *
- * @param integer $id 申请ID
- * @param integer $module 模块类型
- * @param array $params
- * @param mixed $user 用户信息
- * @param string $way 方式:create=申请,update=重新发起
- **/
- public static function create($id, $module, $params, $user, $way)
- {
- $userid = $user['userid'];
- $apply_user_id = $userid;
- $department = $user['department'];
- // 5=出差申请,6=请假申请,7=用车申请 申请人信息 提交人和申请人不是同一人
- if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
- if($userid != $params['apply_user_id']){
- $apply_user = User::field('userid,department')
- ->where('userid', $params['apply_user_id'])
- ->find();
- if (!$apply_user) {
- except('申请人信息不存在或已删除');
- }
- $apply_user_id = $apply_user['userid'];
- $department = $apply_user['department'];
- }
- }
- // 重新发起
- if ($id > 0) {
- $info = ApproveInfo::field('is_deleted', true)
- ->where('module', $module)
- ->where('user_id', $userid)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->with([
- 'moduleInfo'
- ])
- ->find($id);
- if (!$info) {
- except('申请记录不存在或已删除');
- }
- if (!$info->module_info) {
- except(CommonConstant::get_module_list()[$module] . '记录不存在或已删除');
- }
- if ($info->status != CommonConstant::STATUS_4) {
- except('非审批驳回状态无法操作');
- }
- } else {
- // 3=入库申请,9=合同呈批申请
- if (in_array($module, [CommonConstant::MODULE_3, CommonConstant::MODULE_9])) {
- // 存在申购申请单 判断申购申请单
- if ($params['apply_id'] > 0) {
- $apply = ApproveInfo::field('id')
- ->where('module', CommonConstant::MODULE_1)
- ->where('status', CommonConstant::STATUS_3)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($id);
- if (!$apply) {
- except('申购申请单不存在或已删除');
- }
- }
- }
- }
- $order_no = get_order_sn($user['id']);
- $approve_user = $params['approve_user'] ? explode(',', $params['approve_user']) : [];
- $copy_user = $params['copy_user'] ? explode(',', $params['copy_user']) : [];
- $approve_num = $approve_user ? count($approve_user) : 0;
- $data = [
- 'module' => $module,
- 'user_id' => $userid,
- 'apply_user_id' => $apply_user_id,
- 'department' => $department,
- 'status' => CommonConstant::STATUS_2,
- 'approve_num' => $approve_num,
- 'cur_num' => 0,
- 'order_no' => $order_no,
- 'apply_date' => date("Y-m-d"),
- 'reason' => $params['reason'],
- 'type' => $params['type'],
- 'desc' => $params['desc'],
- 'start_time' => $module == CommonConstant::MODULE_6 ? $params['start_time'] . ' ' . $params['start_am'] : $params['start_time'],
- 'end_time' => $module == CommonConstant::MODULE_6 ? $params['end_time'] . ' ' . $params['end_am'] : $params['end_time'],
- 'apply_id' => $params['apply_id'],
- 'maintain_user_id' => 0,
- ];
- Db::startTrans();
- try {
- if ($id > 0) {
- // 编辑对应模块
- self::create_module($module, $params, $id, $info, 'update');
- // 编辑审批抄送
- self::create_approve($approve_user, $copy_user, $userid, $id, 'update');
- // 编辑审批申请
- if ($module == CommonConstant::MODULE_9) {
- unset($data['reason']);
- }
- unset($data['order_no']);
- $save_data = [
- 'create_at' => date('Y-m-d H:i:s'),
- ];
- $data = array_merge($data, $save_data);
- $info->save($data);
- } else {
- // 添加审批申请
- $info = ApproveInfo::create($data);
- $info_id = $info->id;
- // 添加对应模块
- $result = self::create_module($module, $params, $info_id, [], 'create');
- $info->module_id = $result['module_id'];
- $info->save();
- // 添加审批抄送
- self::create_approve($approve_user, $copy_user, $userid, $info_id, 'create');
- user_log('approve',json_encode($result,JSON_UNESCAPED_UNICODE));
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- // except('出现错误:' . $e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine());
- except('出现错误:' . $e->getMessage());
- }
- // 4=领用申请 冻结库存
- if ($module == CommonConstant::MODULE_4) {
- }
- if ($way == CommonConstant::create) {
- // 9=合同呈批申请 生成合同编号
- if ($module == CommonConstant::MODULE_9) {
- $contract_no = CommonService::get_contract_no($params['type']);
- $info->save(['reason' => $contract_no]);
- }
- }
- return true;
- }
- /**
- * 添加对应模块
- *
- * @param integer $module 模块类型
- * @param array $params
- * @param integer $info_id 申请ID
- * @param mixed $info 申请信息
- * @param string $type 类型:create=申请,update=重新发起
- **/
- public static function create_module($module, $params, $info_id, $info, $type)
- {
- $other = [];
- switch ($module) {
- case CommonConstant::MODULE_1:
- // 添加申购物品
- $total_amount = 0;
- if ($params['type'] == ApplyConstant::TYPE_1) {
- $other = self::create_goods($info_id, $module, $params['apply_goods'], $type);
- $total_amount = $other['total_amount'];
- }
- $data['info_id'] = $info_id;
- $data['reason'] = $params['reason'];
- $data['type'] = $params['type'];
- $data['total_amount'] = $params['total_amount']; // $total_amount
- $data['start_time'] = $params['start_time'];
- $data['document'] = $params['document'];
- $data['images'] = $params['images'];
- $data['pay_type'] = $params['pay_type'];
- break;
- case CommonConstant::MODULE_2:
- $data['info_id'] = $info_id;
- $data['type'] = $params['type'];
- $data['word_size'] = $params['word_size'];
- $data['desc'] = $params['desc'];
- $data['number'] = $params['number'];
- $data['reason'] = $params['reason'];
- $data['remark'] = $params['remark'];
- $data['document'] = $params['document'];
- break;
- case CommonConstant::MODULE_3:
- // 添加入库物品
- $other = self::create_goods($info_id, $module, $params['stock_goods'], $type);
- $data['info_id'] = $info_id;
- $data['total_amount'] = $other['total_amount'];
- $data['document'] = $params['document'];
- $data['images'] = $params['images'];
- $data['remark'] = $params['remark'];
- break;
- case CommonConstant::MODULE_4:
- // 添加领用物品
- $other = self::create_goods($info_id, $module, $params['use_goods'], $type);
- $data['info_id'] = $info_id;
- $data['reason'] = $params['reason'];
- $data['document'] = $params['document'];
- $data['remark'] = $params['remark'];
- break;
- case CommonConstant::MODULE_5:
- // 添加同行人员
- $other = self::create_peer_user($info_id, $params['peer_user'], $type);
- $data['info_id'] = $info_id;
- $data['reason'] = $params['reason'];
- $data['start_time'] = $params['start_time'];
- $data['end_time'] = $params['end_time'];
- $data['document'] = $params['document'];
- $data['images'] = $params['images'];
- $data['type'] = $params['type'];
- $data['is_who'] = $params['is_who'];
- $data['remark'] = $params['remark'];
- break;
- case CommonConstant::MODULE_6:
- $data['info_id'] = $info_id;
- $data['type'] = $params['type'];
- $data['start_time'] = $params['start_time'];
- $data['end_time'] = $params['end_time'];
- $data['start_am'] = $params['start_am'];
- $data['end_am'] = $params['end_am'];
- $data['time'] = $params['time'];
- $data['reason'] = $params['reason'];
- $data['document'] = $params['document'];
- $data['remark'] = $params['remark'];
- break;
- case CommonConstant::MODULE_7:
- $data['info_id'] = $info_id;
- $data['reason'] = $params['reason'];
- $data['start_time'] = $params['start_time'];
- $data['reach_address'] = $params['reach_address'];
- $data['end_time'] = $params['end_time'];
- $data['end_address'] = $params['end_address'];
- $data['document'] = $params['document'];
- $data['images'] = $params['images'];
- break;
- case CommonConstant::MODULE_8:
- $data['info_id'] = $info_id;
- $data['type'] = $params['type'];
- $data['reason'] = $params['reason'];
- $data['desc'] = $params['desc'];
- $data['images'] = $params['images'];
- break;
- case CommonConstant::MODULE_9:
- $data['info_id'] = $info_id;
- $data['reason'] = $params['reason'];
- $data['type'] = $params['type'];
- $data['desc'] = $params['desc'];
- $data['number'] = $params['number'];
- $data['scope'] = $params['scope'];
- $data['legal_opinion'] = $params['legal_opinion'];
- $data['document'] = $params['document'];
- $data['remark'] = $params['remark'];
- if ($type == 'update') {
- unset($data['reason']);
- }
- break;
- case CommonConstant::MODULE_10:
- $data['info_id'] = $info_id;
- $data['founder'] = $params['founder'];
- $data['desc'] = $params['desc'];
- $data['serial_number'] = $params['serial_number'];
- $data['reason'] = $params['reason'];
- $data['start_time'] = $params['start_time'];
- $data['remark'] = $params['remark'];
- $data['degree'] = $params['degree'];
- $data['document'] = $params['document'];
- break;
- case CommonConstant::MODULE_11:
- $data['info_id'] = $info_id;
- $data['department'] = $params['department'];
- $data['reason'] = $params['reason'];
- $data['department_sign'] = $params['department_sign'];
- $data['remark'] = $params['remark'];
- $data['document'] = $params['document'];
- $data['desc'] = $params['desc'];
- $data['start_time'] = $params['start_time'];
- $data['serial_number'] = $params['serial_number'];
- break;
- }
- if ($type == 'update') {
- $info->module_info->save($data);
- $module_id = $info->module_info->id;
- } else {
- $models = CommonConstant::get_module_model_list();
- $model = $models[$module];
- $module_result = $model::create($data);
- $module_id = $module_result->id;
- }
- $module_info = $data;
- return compact("module_id", "module_info", "other");
- }
- /**
- * 添加申购物品/入库物品/领用物品
- *
- * @param integer $info_id 申请ID
- * @param integer $module 模块类型
- * @param array $params 数据
- * @param string $type 类型:create=申请,update=重新发起
- * @return array
- **/
- public static function create_goods($info_id, $module, $params, $type)
- {
- if (!$params) {
- return [];
- }
- $goods_list_new = [];
- $category_first_data = [];
- $category_data = [];
- $goods_ids = [];
- $goods_ids_new = [];
- $data = [];
- $total_amount = 0;
- switch ($module) {
- case CommonConstant::MODULE_1:
- $model = ApproveApplyGoods::class;
- break;
- case CommonConstant::MODULE_3:
- $model = ApproveStockGoods::class;
- break;
- case CommonConstant::MODULE_4:
- $model = ApproveUseGoods::class;
- break;
- }
- if ($type == 'update') {
- $model::where('info_id', $info_id)->delete();
- }
- foreach ($params as $value) {
- if ($value['flag'] == '2') {
- // 批量导入
- $category_first_data[$value['goods_category_first']][$value['goods_category_id']] = $value['goods_category_id'];
- }
- if ($value['flag'] == '3') {
- // 商品库选择
- $goods_ids[$value['goods_id']] = $value;
- }
- }
- // 商品库选择
- if ($goods_ids) {
- $goods_list = Goods::field('status,is_deleted,create_at', true)
- ->where('id', 'in', array_keys($goods_ids))
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->with([
- 'goodsStock',
- ])
- ->select();
- $goods_object = $goods_list ? array_column($goods_list->toArray(), null, 'id') : [];
- $goods_name = '';
- $goods_stock_name = '';
- $apply_goods_data = [];
- foreach ($goods_ids as $key => $value) {
- if (array_key_exists($value['goods_id'], $goods_object)) {
- // 商品库里有该商品
- $goods_info = $goods_object[$value['goods_id']];
- $goods_stock_info = array_column($goods_info['goods_stock'], null, 'id');
- $goods_stock_data = [];
- $total_price = 0;
- foreach ($value['goods_stock'] as $val) {
- if (array_key_exists($val['id'], $goods_stock_info)) {
- // 商品库里有该商品规格
- $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
- $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
- $total_price = bcadd($total_price, $stock * $price, 2);
- $goods_stock_data[] = [
- 'id' => $val['id'],
- 'goods_id' => $goods_info['id'],
- 'name' => $val['name'],
- 'price' => $price,
- 'stock' => $stock,
- ];
- } else {
- // 商品库里没有该商品规格
- $goods_stock_name .= $value['goods_name'] . '-' . $val['name'] . '、';
- }
- }
- $goods_data = [
- 'info_id' => $info_id,
- 'goods_id' => $goods_info['id'],
- 'goods_category_first' => $goods_info['goods_category_first'],
- 'goods_category_id' => $goods_info['goods_category_id'],
- 'goods_no' => $goods_info['goods_no'],
- 'goods_name' => $goods_info['goods_name'],
- 'goods_brand' => $goods_info['goods_brand'],
- 'total_price' => $total_price,
- 'goods_stock' => $goods_stock_data,
- ];
- $data[] = $goods_data;
- $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
- $apply_goods_data[] = $goods_data;
- $total_amount = bcadd($total_amount, $total_price, 2);
- } else {
- // 商品库里没有该商品
- $goods_name .= $value['goods_name'] . '、';
- }
- }
- if ($goods_name) {
- // 商品库选择的商品 在商品库没有匹配到 返回错误提示
- except($goods_name . '等商品在商品库里不存在或已删除');
- }
- if ($goods_stock_name) {
- // 商品库选择的商品的规格 在商品库没有匹配到 返回错误提示
- except($goods_stock_name . '等商品规格在商品库里不存在或已删除');
- }
- $model::insertAll($apply_goods_data);
- }
- // 批量导入
- if ($category_first_data) {
- $category_first_list = GoodsCategoryService::get_list([['name', 'in', array_keys($category_first_data)]], 1);
- $category_first_object = $category_first_list ? array_column($category_first_list->toArray(), null, 'name') : [];
- foreach ($category_first_data as $key => $value) {
- if (array_key_exists($key, $category_first_object)) {
- // 一级里有该商品分类
- $category_first_info = $category_first_object[$key];
- $goods_category_first_id = $category_first_info['id'];
- $category_second_object = $category_first_info['childlist'] ? array_column($category_first_info['childlist'], null, 'name') : [];
- } else {
- // 一级里没有该商品分类 创建
- $goods_category_first = GoodsCategory::create(['name' => $key]);
- $goods_category_first_id = $goods_category_first->id;
- $category_second_object = [];
- }
- $childlist = [];
- foreach ($value as $kk => $vv) {
- if (array_key_exists($kk, $category_second_object)) {
- // 二级里有该商品分类
- $category_second_info = $category_second_object[$kk];
- $goods_category_id = $category_second_info['id'];
- } else {
- // 二级里没有该商品分类 创建
- $goods_category = GoodsCategory::create(['pid' => $goods_category_first_id, 'name' => $kk]);
- $goods_category_id = $goods_category->id;
- }
- $childlist[] = [
- 'id' => $goods_category_id,
- 'name' => $kk,
- ];
- }
- $category_data[] = [
- 'id' => $goods_category_first_id,
- 'name' => $key,
- 'childlist' => $childlist
- ];
- }
- }
- $category_object = $category_data ? array_column($category_data, null, 'name') : [];
- foreach ($params as $key => $value) {
- if ($value['flag'] == '1') {
- // 添加新商品
- $goods_ids_new[$key] = $value;
- $goods_info = Goods::field('status,is_deleted,create_at', true)
- ->where('goods_category_first', $value['goods_category_first'])
- ->where('goods_category_id', $value['goods_category_id'])
- ->where('goods_name', $value['goods_name'])
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->with([
- 'goodsStock',
- ])
- ->find();
- if ($goods_info) {
- $goods_list_new[] = $goods_info->toArray();
- }
- }
- if ($value['flag'] == '2') {
- // 批量导入
- if (array_key_exists($value['goods_category_first'], $category_object)) {
- $category_first_info = $category_object[$value['goods_category_first']];
- $goods_category_first_id = $category_first_info['id'];
- $category_second_object = array_column($category_first_info['childlist'], null, 'name');
- $category_second_info = $category_second_object[$value['goods_category_id']];
- $goods_category_id = $category_second_info['id'];
- $goods_ids_new[$key] = $value;
- $goods_ids_new[$key]['goods_category_first'] = $goods_category_first_id;
- $goods_ids_new[$key]['goods_category_id'] = $goods_category_id;
- $goods_info = Goods::field('status,is_deleted,create_at', true)
- ->where('goods_category_first', $goods_category_first_id)
- ->where('goods_category_id', $goods_category_id)
- ->where('goods_name', $value['goods_name'])
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->with([
- 'goodsStock',
- ])
- ->find();
- if ($goods_info) {
- $goods_list_new[] = $goods_info->toArray();
- }
- }
- }
- }
- // 添加新商品
- if ($goods_ids_new) {
- $goods_object = $goods_list_new ? array_column($goods_list_new, null, 'goods_name') : [];
- $apply_goods_data = [];
- foreach ($goods_ids_new as $value) {
- $goods_data = [
- 'goods_category_first' => $value['goods_category_first'],
- 'goods_category_id' => $value['goods_category_id'],
- 'goods_no' => $value['goods_no'],
- 'goods_name' => $value['goods_name'],
- 'goods_brand' => isset($value['goods_brand']) ? $value['goods_brand'] : '',
- ];
- if (array_key_exists($value['goods_name'], $goods_object)) {
- // 商品库里有该商品
- $goods_info = $goods_object[$value['goods_name']];
- $goods_id = $goods_info['id'];
- $goods_stock_info = array_column($goods_info['goods_stock'], null, 'name');
- } else {
- // 商品库里没有该商品 创建
- $goods = Goods::create($goods_data);
- $goods_id = $goods->id;
- $goods_stock_info = [];
- }
- $goods_stock_data = [];
- $total_price = 0;
- foreach ($value['goods_stock'] as $val) {
- if (array_key_exists($val['name'], $goods_stock_info)) {
- // 商品库里有该商品规格
- $stock_id = $goods_stock_info[$val['name']]['id'];
- } else {
- // 商品库里没有该商品规格 创建
- $stock_data = [
- 'goods_id' => $goods_id,
- 'name' => $val['name'],
- ];
- $stock = GoodsStock::create($stock_data);
- $stock_id = $stock->id;
- }
- $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
- $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
- $total_price = bcadd($total_price, $stock * $price, 2);
- $goods_stock_data[] = [
- 'id' => $stock_id,
- 'goods_id' => $goods_id,
- 'name' => $val['name'],
- 'price' => $price,
- 'stock' => $stock,
- ];
- }
- $goods_data['info_id'] = $info_id;
- $goods_data['goods_id'] = $goods_id;
- $goods_data['total_price'] = $total_price;
- $goods_data['goods_stock'] = $goods_stock_data;
- $data[] = $goods_data;
- $goods_data['goods_stock'] = json_encode($goods_stock_data, JSON_UNESCAPED_UNICODE);
- $apply_goods_data[] = $goods_data;
- $total_amount = bcadd($total_amount, $total_price, 2);
- }
- $model::insertAll($apply_goods_data);
- }
- return compact("total_amount", "data");
- }
- /**
- * 添加同行人员
- *
- * @param integer $info_id 申请ID
- * @param array $params 同行人员数据
- * @param string $type 类型:create=申请,update=重新发起
- **/
- public static function create_peer_user($info_id, $params, $type)
- {
- if ($type == 'update') {
- ApproveEvectionPeerUser::where('info_id', $info_id)->delete();
- }
- $data = [];
- if (!$params) {
- return $data;
- }
- foreach ($params as $value) {
- if (isset($value['name']) && !empty($value['name'])) {
- $data[] = [
- 'info_id' => $info_id,
- 'is_who' => $value['is_who'],
- 'user_id' => isset($value['user_id']) ? $value['user_id'] : '',
- 'name' => $value['name'],
- 'desc' => isset($value['desc']) ? $value['desc'] : '',
- ];
- }
- }
- if ($data) {
- ApproveEvectionPeerUser::insertAll($data);
- }
- return $data;
- }
- /**
- * 添加审批抄送
- *
- * @param array $approve_user 审批ID
- * @param array $copy_user 抄送ID
- * @param string $userid 发起人ID
- * @param integer $info_id 申请ID
- * @param string $type 类型:create=申请,update=重新发起
- **/
- public static function create_approve($approve_user, $copy_user, $userid, $info_id, $type)
- {
- if ($type == 'update') {
- // 审批全部改为历史记录
- Approve::where('info_id', $info_id)->where('status','in',[CommonConstant::STATUS_3,CommonConstant::STATUS_4])->update(['state' => CommonConstant::IS_WHO_1]);
- Approve::where('info_id', $info_id)->where('status','in',[CommonConstant::STATUS_1,CommonConstant::STATUS_2])->update(['state' => CommonConstant::IS_WHO_0]);
- // 删除掉抄送
- ApproveCopy::where('info_id', $info_id)->delete();
- }
- $approve_data = [];
- $copy_data = [];
- $flow_num = 0;
- $copy_num = 0;
- // 发起人
- $apply_data = [
- 'info_id' => $info_id,
- 'status' => 0,
- 'group' => CommonConstant::IS_WHO_1,
- 'approve_user' => $userid,
- 'approve_flow' => 1,
- ];
- // 审批人
- foreach ($approve_user as $key => $value) {
- $flow_num++;
- $approve_data[] = [
- 'info_id' => $info_id,
- 'status' => $flow_num == 1 ? CommonConstant::STATUS_2 : CommonConstant::STATUS_1,
- 'group' => CommonConstant::IS_WHO_0,
- 'approve_user' => $value,
- 'approve_flow' => $flow_num,
- ];
- }
- // 抄送人
- foreach ($copy_user as $key => $value) {
- $copy_num++;
- $copy_data[] = [
- 'info_id' => $info_id,
- 'approve_user' => $value,
- 'approve_flow' => $copy_num,
- ];
- }
- if ($approve_data) {
- // 添加审批
- $approve_data = array_merge([$apply_data], $approve_data);
- Approve::insertAll($approve_data);
- }
- if ($copy_data) {
- // 添加抄送
- ApproveCopy::insertAll($copy_data);
- }
- }
- /**
- * 列表
- *
- * @param integer $group 类别:list=我的申请记录,form=采购审批单
- * @param integer $module 模块类型
- * @param integer $status 审批状态
- * @param string $search 搜索
- * @param string $start_time 申请开始时间
- * @param string $end_time 申请结束时间
- * @param integer $offset 起始位置
- * @param integer $length 查询数量
- * @param mixed $user 用户信息
- **/
- public static function get_list($group, $module, $status, $search, $start_time, $end_time,$offset, $length, $user)
- {
- if ($group == 'list') {
- if (!array_key_exists($module, CommonConstant::get_module_list())) {
- return [];
- }
- if (!array_key_exists($status, CommonConstant::get_approve_status_list())) {
- return [];
- }
- $type = 0;
- } else {
- $module = CommonConstant::MODULE_1;
- $status = CommonConstant::STATUS_3;
- $type = ApplyConstant::TYPE_1;
- }
- $userid = $user['userid'];
- $list = ApproveInfo::field('module_id,user_id,apply_user_id,department,is_deleted', true)
- ->where('module', $module)
- ->where(function ($query) use ($module, $userid) {
- if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
- $query->where('user_id', $userid)->whereOr('apply_user_id', $userid);
- } else {
- $query->where('user_id', $userid);
- }
- })
- ->where('status', $status)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->when(!empty($search), function ($query) use ($search) {
- $query->where('order_no|reason', 'like', '%' . $search . '%');
- })
- ->when(!empty($start_time) && !empty($end_time), function ($query) use ($start_time, $end_time) {
- $query->where('create_at', 'BETWEEN', [$start_time, $end_time]);
- })
- ->when($type > 0, function ($query) use ($type) {
- $query->where('type', $type);
- });
- $list = self::get_with($list, $module, $status);
- $list = $list->order('id desc')
- ->limit($offset, $length)
- ->select();
- return $list;
- }
- /**
- * 详情/信息
- *
- * @param integer $id 申请ID
- * @param mixed $user 用户信息
- * @param string $group 类别:0=审批人/后台,1=提交人申请人 审批详情,我的申请详情/信息
- * @param string $type 类型:detail=详情,info=信息
- **/
- public static function get_detail($id, $user, $group, $type)
- {
- $info = ApproveInfo::field('is_deleted', true)
- ->where(function ($query) use ($user, $group) {
- if ($group == CommonConstant::IS_WHO_1) {
- $query->where('user_id', $user['userid'])->whereOr('apply_user_id', $user['userid']);
- }
- })
- ->where('is_deleted', CommonConstant::IS_DELETED_0);
- if ($type == 'detail') {
- $info->with([
- 'moduleInfo',
- 'approve' => function ($query) {
- $query->field('is_deleted,create_at', true)
- ->where('state', 'in', [CommonConstant::IS_WHO_0, CommonConstant::IS_WHO_1])
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name,avatar');
- }
- ]);
- },
- 'approveCopy' => function ($query) {
- $query->field('is_deleted,create_at', true)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name,avatar');
- }
- ]);
- }
- ]);
- $info = $info->find($id);
- if ($info) {
- // 部门列表
- $department_data = UserService::get_user_department_list($info['department']);
- $info['department_data'] = $department_data;
- }
- } else {
- // 信息
- $info->with([
- 'moduleInfo'
- ]);
- $info = $info->find($id);
- }
- if ($info) {
- $module = $info['module'];
- if ($group == CommonConstant::IS_WHO_1) {
- // 先默认当前是提交人
- $create_user = [
- 'id' => $user['id'],
- 'userid' => $user['userid'],
- 'name' => $user['name'],
- 'avatar' => $user['avatar'],
- ];
- $apply_user = $create_user;
- if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
- if($user['userid'] == $info['user_id']){
- // 当前是提交人
- if ($info['user_id'] != $info['apply_user_id']) {
- $apply_user_info = User::field('id,userid,name,avatar')
- ->where('userid', $info['apply_user_id'])
- ->find();
- $apply_user = [
- 'id' => $apply_user_info['id'],
- 'userid' => $apply_user_info['userid'],
- 'name' => $apply_user_info['name'],
- 'avatar' => $apply_user_info['avatar'],
- ];
- }
- }
- if($user['userid'] == $info['apply_user_id']){
- // 当前是申请人
- if ($info['user_id'] != $info['apply_user_id']) {
- $apply_user_info = User::field('id,userid,name,avatar')
- ->where('userid', $info['user_id'])
- ->find();
- $create_user = [
- 'id' => $apply_user_info['id'],
- 'userid' => $apply_user_info['userid'],
- 'name' => $apply_user_info['name'],
- 'avatar' => $apply_user_info['avatar'],
- ];
- }
- }
- }
- }
- if ($group == CommonConstant::IS_WHO_0) {
- // 审批人
- $apply_user = User::field('id,userid,name,avatar')
- ->where('userid', $info['apply_user_id'])
- ->find();
- $create_user = [
- 'id' => $apply_user['id'],
- 'userid' => $apply_user['userid'],
- 'name' => $apply_user['name'],
- 'avatar' => $apply_user['avatar'],
- ];
- $apply_user = $create_user;
- if (in_array($module, [CommonConstant::MODULE_5, CommonConstant::MODULE_6, CommonConstant::MODULE_7])) {
- if ($info['user_id'] != $info['apply_user_id']) {
- $apply_user_info = User::field('id,userid,name,avatar')
- ->where('userid', $info['user_id'])
- ->find();
- $create_user = [
- 'id' => $apply_user_info['id'],
- 'userid' => $apply_user_info['userid'],
- 'name' => $apply_user_info['name'],
- 'avatar' => $apply_user_info['avatar'],
- ];
- }
- }
- }
- $info['create_user'] = $create_user;
- $info['apply_user'] = $apply_user;
- }
- $info = self::get_item($info, $type);
- return $info;
- }
- /**
- * 列表-关联数据
- *
- * @param mixed $list
- * @param integer $module 模块类型
- * @param integer $status 审批状态
- **/
- public static function get_with($list, $module, $status)
- {
- $field = 'id,info_id,status,approve_user';
- switch ($module) {
- case CommonConstant::MODULE_1:
- // 申领商品列表
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- },
- 'applyGoods' => function ($query) {
- $query->field('create_at', true);
- },
- ]);
- } else {
- $list = $list->with([
- 'applyGoods' => function ($query) {
- $query->field('create_at', true);
- },
- ]);
- }
- break;
- case CommonConstant::MODULE_2:
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- }
- ]);
- }
- break;
- case CommonConstant::MODULE_3:
- // 入库商品列表
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- },
- 'stockGoods' => function ($query) {
- $query->field('id,info_id,goods_name');
- },
- ]);
- } else {
- $list = $list->with([
- 'stockGoods' => function ($query) {
- $query->field('id,info_id,goods_name');
- },
- ]);
- }
- break;
- case CommonConstant::MODULE_4:
- // 领用商品列表
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- },
- 'useGoods' => function ($query) {
- $query->field('id,info_id,goods_name');
- },
- ]);
- } else {
- $list = $list->with([
- 'useGoods' => function ($query) {
- $query->field('id,info_id,goods_name');
- },
- ]);
- }
- break;
- case CommonConstant::MODULE_5:
- // 出差同行人员列表
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- },
- 'peerUser' => function ($query) {
- $query->field('id,info_id,name');
- },
- ]);
- } else {
- $list = $list->with([
- 'peerUser' => function ($query) {
- $query->field('id,info_id,name');
- },
- ]);
- }
- break;
- case CommonConstant::MODULE_6:
- case CommonConstant::MODULE_7:
- case CommonConstant::MODULE_8:
- case CommonConstant::MODULE_9:
- case CommonConstant::MODULE_10:
- case CommonConstant::MODULE_11:
- if (in_array($status, [CommonConstant::STATUS_2, CommonConstant::STATUS_4])) {
- // 审批中或审批驳回 才关联 审批人信息
- $list = $list->with([
- 'approveOne' => function ($query) use ($status, $field) {
- $query->field($field)
- ->where('status', $status)
- ->where('group', CommonConstant::IS_WHO_0)
- ->where('state', CommonConstant::IS_WHO_0)
- ->with([
- 'user' => function ($query) {
- $query->field('userid,name');
- }
- ]);
- }
- ]);
- }
- break;
- }
- return $list;
- }
- /**
- * 详情/信息-关联数据
- *
- * @param mixed $info
- * @param string $type 类型:detail=详情,info=信息
- **/
- public static function get_item($info, $type)
- {
- if ($info) {
- $module = $info['module'];
- switch ($module) {
- case CommonConstant::MODULE_1:
- if ($info['type'] == ApplyConstant::TYPE_1) {
- if ($type == 'detail') {
- $info['apply_goods'] = $info->applyGoods()->select();
- } else {
- $info->apply_goods;
- }
- }
- $contract = [];
- if ($type == 'detail') {
- // 关联的合同呈批申请
- $contract = ApproveInfo::field('id,reason')
- ->where('module', CommonConstant::MODULE_9)
- ->where('status', CommonConstant::STATUS_3)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->where('apply_id', $info['id'])
- ->select();
- }
- $info['contract'] = $contract;
- break;
- case CommonConstant::MODULE_3:
- if ($type == 'detail') {
- $info['stock_goods'] = $info->stockGoods()->select();
- } else {
- $info->stock_goods;
- }
- // 关联的申购申请单
- $apply = null;
- if($info['apply_id'] > 0){
- $apply = ApproveInfo::field('id,order_no')
- ->where('module', CommonConstant::MODULE_1)
- ->where('status', CommonConstant::STATUS_3)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($info['apply_id']);
- }
- $info['apply'] = $apply;
- break;
- case CommonConstant::MODULE_4:
- if ($type == 'detail') {
- $info['use_goods'] = $info->useGoods()->select();
- } else {
- $info->use_goods;
- }
- break;
- case CommonConstant::MODULE_5:
- if ($type == 'detail') {
- $info['peer_user'] = $info->peerUser()->field('id,info_id,name')->select();
- } else {
- $info->peer_user;
- }
- break;
- case CommonConstant::MODULE_9:
- // 关联的申购申请单
- $apply = null;
- if($info['apply_id'] > 0){
- $apply = ApproveInfo::field('id,order_no')
- ->where('module', CommonConstant::MODULE_1)
- ->where('status', CommonConstant::STATUS_3)
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($info['apply_id']);
- }
- $info['apply'] = $apply;
- break;
- }
- }
- return $info;
- }
- /**
- * 操作方法
- *
- * @param integer $id 申请ID
- * @param array $params 数组
- * @param mixed $user 用户信息
- * @param string $type 类型:urging=催办,cancel=撤销,comment=评分
- */
- public static function make($id, $params, $user, $type)
- {
- $userid = $user['userid'];
- $info = ApproveInfo::field('id,user_id,module,status,reason')
- ->where('is_deleted', CommonConstant::IS_DELETED_0)
- ->find($id);
- if (!$info) {
- except('申请记录不存在或已删除');
- }
- if ($info->user_id != $userid) {
- except('您没有权限操作');
- }
- switch ($type) {
- case 'urging':
- if ($info->status != CommonConstant::STATUS_2) {
- except('其他人已操作');
- }
- // TODO 待对接钉钉接口
- break;
- case 'cancel':
- if ($info->status != CommonConstant::STATUS_4) {
- except('非审批驳回状态无法操作');
- }
- $info->status = CommonConstant::STATUS_5;
- $info->save();
- if ($info->module == CommonConstant::MODULE_9) {
- // 9=合同呈批 释放合同编号
- CommonService::set_contract_no($info->reason);
- }
- break;
- case 'comment':
- if ($info->status != CommonConstant::STATUS_3) {
- except('非审批同意状态无法操作');
- }
- if ($info->module != CommonConstant::MODULE_8) {
- // 8=维修申请
- except(CommonConstant::get_module_list()[CommonConstant::MODULE_8] . '记录不存在或已删除');
- }
- // TODO 没有判断评价状态
- // 更新维修信息
- $params['comment_status'] = CommonConstant::IS_WHO_1;
- try {
- ApproveMaintain::where('info_id', $info->id)->update($params);
- } catch (Exception $e) {
- except('出现错误:' . $e->getMessage());
- }
- break;
- }
- return true;
- }
- }
|