12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571 |
- <?php
- namespace app\common\service;
- use app\common\constant\ApplyConstant;
- use app\common\constant\CommonConstant;
- use app\common\constant\OfferConstant;
- use app\common\model\Approve;
- use app\common\model\ApproveCopy;
- use app\common\model\ApproveApplyGoods;
- use app\common\model\ApproveMaintain;
- use app\common\model\ApproveMaintainUserLog;
- 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\cache\driver\Redis;
- 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'];
- }
- }
- // 重新发起
- 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($params['apply_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) {
- // 编辑对应模块
- $result = self::create_module($module, $params, $id, $info, 'update');
- // 编辑审批人
- self::create_approve($approve_user, $id, 'update', $userid);
- // 编辑抄送人
- self::create_approve_copy($copy_user, $id, 'update');
- // 编辑审批申请
- if ($module == CommonConstant::MODULE_9) {
- // 9=合同呈批 不编辑合同编号
- unset($data['reason'], $data['type']);
- }
- unset($data['order_no']);
- $data['create_at'] = date('Y-m-d H:i:s');
- $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, $info_id, 'create', $userid);
- // 添加抄送人
- self::create_approve_copy($copy_user, $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) {
- list($goodsStockSave, $goodsStockLog, $redisStock) = self::composition_data($result['other']['data'], CommonConstant::MODULE_4, CommonConstant::STATUS_2);
- if ($goodsStockSave) {
- // 批量更新商品库存
- (new GoodsStock)->saveAll($goodsStockSave);
- }
- if ($redisStock) {
- // 库存缓存记录
- $redis = new Redis(config('redis.'));
- foreach ($redisStock as $key => $value) {
- $redis->inc($value['id'], $value['stock']);
- }
- }
- }
- // 9=合同呈批申请 提交 生成合同编号
- if ($module == CommonConstant::MODULE_9 && $way == CommonConstant::create) {
- $contract_no = CommonService::get_contract_no($params['type']);
- $info->save(['reason' => $contract_no]);
- $models = CommonConstant::get_module_model_list();
- $model = $models[$module];
- $model::where('id', $result['module_id'])->update(['reason' => $contract_no]);
- }
- return true;
- }
- /**
- * 添加对应模块
- *
- * @param integer $module 模块类型
- * @param array $params
- * @param integer $info_id 申请ID
- * @param mixed $info 申请信息
- * @param string $type 类型:create=申请,update=重新发起,edit:审批人修改 只有module=4才传edit
- * @return array {"module_id":"3","module_info":{"info_id":"97","reason":"联想显示屏","document":"https:\/\/dingding.hdlkeji.com\/upload\/20240105\/202401051706366597c69c8800d.jpeg","remark":"领用电脑显示屏"},"other":{"total_amount":"0.00","data":[{"info_id":"97","goods_id":50,"goods_category_first":49,"goods_category_id":50,"goods_no":"jx105","goods_name":"联想 S22E","goods_brand":"联想","total_price":"0.00","goods_stock":[{"id":98,"goods_id":50,"name":"S22E","price":0,"stock":"10"}]}]}}
- **/
- 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($module, $params['apply_goods'], $info_id, $info, $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($module, $params['stock_goods'], $info_id, $info, $type);
- $total_amount = $other['total_amount'];
- $data['info_id'] = $info_id;
- $data['total_amount'] = $total_amount;
- $data['document'] = $params['document'];
- $data['images'] = $params['images'];
- $data['remark'] = $params['remark'];
- break;
- case CommonConstant::MODULE_4:
- // 添加领用物品
- $other = self::create_goods($module, $params['use_goods'], $info_id, $info, $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($module, $params['peer_user'], $info_id, $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['images'] = $params['images'];
- $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 != 'create') {
- unset($data['reason'], $data['type']);
- }
- 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 == 'create') {
- $models = CommonConstant::get_module_model_list();
- $model = $models[$module];
- $module_result = $model::create($data);
- $module_id = $module_result->id;
- $module_info = $data;
- } else {
- $info->module_info->save($data);
- $module_id = $info->module_info->id;
- $module_info = $info->module_info;
- }
- return compact("module_id", "module_info", "other");
- }
- /**
- * 添加申购商品/入库商品/领用商品
- *
- * @param integer $module 模块类型
- * @param array $params 数据
- * @param integer $info_id 申请ID
- * @param mixed $info 申请信息
- * @param string $type 类型:create=申请,update=重新发起,edit:审批人修改
- * @return array {"total_amount":"10","data":[{"info_id":"97","goods_id":50,"goods_category_first":49,"goods_category_id":50,"goods_no":"jx105","goods_name":"联想 S22E","goods_brand":"联想","total_price":"0.00","goods_stock":[{"id":98,"goods_id":50,"name":"S22E","price":0,"stock":"10"}]}]}
- **/
- public static function create_goods($module, $params, $info_id, $info, $type)
- {
- if (!$params) {
- return [];
- }
- $goods_list_new = [];
- $category_first_data = [];
- $category_data = [];
- $goods_ids = [];
- $goods_ids_new = [];
- $data = [];
- $total_amount = 0;
- 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 ($module == CommonConstant::MODULE_4 && $value['flag'] == '1') {
- // 商品库选择 TODO 这里修复 前端导入领用模块商品后,所返回的商品是过滤后的flag=3;然后,更改操作,对商品进行了规格库存修改,导致了flag=1
- $value['flag'] = '3';
- $goods_ids[$value['goods_id']] = $value;
- }
- }
- 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;
- if ($type == 'edit') {
- $old_goods_data = [];
- $old_goods_freeze_stock_data = []; // 旧数据的冻结库存
- foreach ($info['data'] as $key => $val) {
- $old_goods_data[$val['goods_id']] = $val;
- $goods_stock = $val['goods_stock_text'];
- foreach ($goods_stock as $k => $v) {
- $old_goods_freeze_stock_data[$v['id']] = [
- 'id' => $v['id'],
- 'stock' => $v['stock'],
- ];
- }
- }
- $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') : [];
- $error_data = '';
- $apply_goods_data = [];
- $add_data = [];
- $del_ids = [];
- $goodsStockSave = [];
- $redisStock = [];
- foreach ($goods_ids as $key => $value) {
- if (array_key_exists($value['goods_id'], $goods_object)) {
- // 商品库里有该商品
- $goods_info = $goods_object[$value['goods_id']];
- $goods_stock_object = 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_object)) {
- // 商品库里有该商品规格
- $goods_stock_info = $goods_stock_object[$val['id']];
- $total_stock = $goods_stock_info['stock'];
- $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
- $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
- $freeze_stock = array_key_exists($val['id'], $old_goods_freeze_stock_data) ? $old_goods_freeze_stock_data[$val['id']]['stock'] : 0;
- if ($stock > ($total_stock + $freeze_stock)) {
- // 商品规格库存不足
- $error_data .= $value['goods_name'] . '-' . $val['name'] . '的库存不足' . "\n";
- }
- $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,
- ];
- if ($freeze_stock > 0) {
- if ($stock > $freeze_stock) {
- // 扣除剩余库存 增加冻结库存
- $save_stock = $stock - $freeze_stock;
- $goodsStockSave[] = [
- 'id' => $val['id'],
- 'stock' => ['dec', $save_stock],
- 'freeze_stock' => ['inc', $save_stock],
- ];
- $redisStock[] = [
- 'id' => 'dingtalk_stock_' . $info_id . '_' . $value['goods_id'] . '_' . $val['id'],
- 'exp' => 'inc',
- 'stock' => $save_stock,
- ];
- }
- if ($stock < $freeze_stock) {
- // 增加剩余库存 扣除冻结库存
- $save_stock = $freeze_stock - $stock;
- $goodsStockSave[] = [
- 'id' => $val['id'],
- 'stock' => ['inc', $save_stock],
- 'freeze_stock' => ['dec', $save_stock],
- ];
- $redisStock[] = [
- 'id' => 'dingtalk_stock_' . $info_id . '_' . $value['goods_id'] . '_' . $val['id'],
- 'exp' => 'dec',
- 'stock' => $save_stock,
- ];
- }
- }
- } else {
- // 商品库里没有该商品规格
- $error_data .= $value['goods_name'] . '-' . $val['name'] . '规格商品库不存在' . "\n";
- }
- }
- $total_amount = bcadd($total_amount, $total_price, 2);
- $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);
- if (array_key_exists($value['goods_id'], $old_goods_data)) {
- // 修改
- $apply_goods_data[$value['goods_id']] = $goods_data;
- } else {
- // 添加
- $add_data[] = $goods_data;
- }
- } else {
- // 商品库里没有该商品
- $error_data .= $value['goods_name'] . '商品库不存在' . "\n";
- }
- }
- if ($error_data) {
- // 商品库选择的商品 商品库选择的商品的规格 在商品库没有匹配到 返回错误提示
- except($error_data);
- }
- foreach ($info['data'] as $index) {
- if (array_key_exists($index['goods_id'], $apply_goods_data)) {
- // 更新
- $save_data = $apply_goods_data[$index['goods_id']];
- $index->save($save_data);
- } else {
- // 删除
- $del_ids[] = $index['id'];
- }
- }
- if ($add_data) {
- $model::insertAll($add_data);
- }
- if ($del_ids) {
- $model::where(['id' => ['IN', $del_ids]])->delete();
- }
- return compact("total_amount", "data", "goodsStockSave", "redisStock");
- }
- break;
- }
- if ($type == 'update') {
- $model::where('info_id', $info_id)->delete();
- }
- // 商品库选择
- 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') : [];
- $error_data = '';
- $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_object = 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_object)) {
- // 商品库里有该商品规格
- $goods_stock_info = $goods_stock_object[$val['id']];
- $total_stock = $goods_stock_info['stock'];
- $stock = isset($val['stock']) && $val['stock'] > 0 ? $val['stock'] : 0;
- $price = isset($val['price']) && $val['price'] > 0 ? $val['price'] : 0;
- if ($stock > $total_stock) {
- // 商品规格库存不足
- $error_data .= $value['goods_name'] . '-' . $val['name'] . '的库存不足' . "\n";
- }
- $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 {
- // 商品库里没有该商品规格
- $error_data .= $value['goods_name'] . '-' . $val['name'] . '规格商品库不存在' . "\n";
- }
- }
- $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 {
- // 商品库里没有该商品
- $error_data .= $value['goods_name'] . '商品库不存在' . "\n";
- }
- }
- if ($error_data) {
- // 商品库选择的商品 商品库选择的商品的规格 在商品库没有匹配到 返回错误提示
- except($error_data);
- }
- $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 $module 模块类型
- * @param array $params 同行人员数据
- * @param integer $info_id 申请ID
- * @param string $type 类型:create=申请,update=重新发起
- **/
- public static function create_peer_user($module, $params, $info_id, $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 integer $info_id 申请ID
- * @param string $type 类型:create=申请,update=重新发起
- * @param string $userid 发起人ID
- **/
- public static function create_approve($approve_user, $info_id, $type, $userid)
- {
- 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]);
- }
- $approve_data = [];
- $flow_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,
- ];
- }
- if ($approve_data) {
- // 添加审批人
- $approve_data = array_merge([$apply_data], $approve_data);
- Approve::insertAll($approve_data);
- }
- return true;
- }
- /**
- * 添加抄送人
- *
- * @param array $copy_user 抄送ID
- * @param integer $info_id 申请ID
- * @param string $type 类型:create=申请,update=重新发起
- **/
- public static function create_approve_copy($copy_user, $info_id, $type)
- {
- if ($type == 'update') {
- // 删除掉抄送
- ApproveCopy::where('info_id', $info_id)->delete();
- }
- $copy_data = [];
- $copy_num = 0;
- // 抄送人
- foreach ($copy_user as $key => $value) {
- $copy_num++;
- $copy_data[] = [
- 'info_id' => $info_id,
- 'approve_user' => $value,
- 'approve_flow' => $copy_num,
- ];
- }
- if ($copy_data) {
- // 添加抄送人
- ApproveCopy::insertAll($copy_data);
- }
- return true;
- }
- /**
- * 入库商品/领用商品组成数据
- *
- * @param mixed $goods 商品
- * @param integer $module 模块类型
- * @param integer $status 状态:2=审批中,3=审批同意,4=审批驳回
- **/
- public static function composition_data($goods, $module, $status)
- {
- $goodsStockSave = [];
- $goodsStockLog = [];
- $redisStock = [];
- $type = 0;
- foreach ($goods as $key => $val) {
- $goods_stock = $status == CommonConstant::STATUS_2 ? $val['goods_stock'] : $val['goods_stock_text'];
- foreach ($goods_stock as $k => $v) {
- if ($module == CommonConstant::MODULE_3) {
- // 增加剩余库存
- $goodsStockSave[] = [
- 'id' => $v['id'],
- 'stock' => ['inc', $v['stock']],
- ];
- }
- if ($module == CommonConstant::MODULE_4) {
- if ($status == CommonConstant::STATUS_2) {
- // 扣除剩余库存 增加冻结库存
- $goodsStockSave[] = [
- 'id' => $v['id'],
- 'stock' => ['dec', $v['stock']],
- 'freeze_stock' => ['inc', $v['stock']],
- ];
- }
- if ($status == CommonConstant::STATUS_3) {
- // 扣除冻结库存
- $goodsStockSave[] = [
- 'id' => $v['id'],
- 'freeze_stock' => ['dec', $v['stock']],
- ];
- $type = 1;
- }
- if ($status == CommonConstant::STATUS_4) {
- // 增加剩余库存 扣除冻结库存
- $goodsStockSave[] = [
- 'id' => $v['id'],
- 'stock' => ['inc', $v['stock']],
- 'freeze_stock' => ['dec', $v['stock']],
- ];
- }
- $redisStock[] = [
- 'id' => 'dingtalk_stock_' . $val['info_id'] . '_' . $v['goods_id'] . '_' . $v['id'],
- 'stock' => $v['stock'],
- ];
- }
- if ($status == CommonConstant::STATUS_3) {
- $goodsStockLog[] = [
- 'type' => $type,
- 'info_id' => $val['info_id'],
- 'goods_id' => $v['goods_id'],
- 'stock_id' => $v['id'],
- 'name' => $v['name'],
- 'price' => $v['price'],
- 'stock' => $v['stock'],
- ];
- }
- }
- }
- return [
- $goodsStockSave,
- $goodsStockLog,
- $redisStock
- ];
- }
- /**
- * 列表
- *
- * @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,signature,signature_status');
- }
- ]);
- },
- '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=信息,edit=审批人修改
- **/
- public static function get_item($info, $type)
- {
- if ($info) {
- $module = $info['module'];
- switch ($module) {
- case CommonConstant::MODULE_1:
- if ($info['type'] == ApplyConstant::TYPE_1) {
- // $info['apply_goods'] = $info->applyGoods()->select();
- $data = $info->apply_goods;
- $info['data'] = $data;
- }
- $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:
- // $info['stock_goods'] = $info->stockGoods()->select();
- $data = $info->stock_goods;
- $info['data'] = $data;
- $apply = null;
- if ($type != 'edit') {
- // 关联的申购申请单
- 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:
- // $info['use_goods'] = $info->useGoods()->select();
- $data = $info->use_goods;
- $info['data'] = $data;
- break;
- case CommonConstant::MODULE_5:
- $info['peer_user'] = $info->peerUser()->field('id,info_id,name')->select();
- // $info->peer_user;
- $info['data'] = $info['peer_user'];
- break;
- case CommonConstant::MODULE_9:
- $apply = null;
- if ($type != 'edit') {
- // 关联的申购申请单
- 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,module,user_id,status,reason,maintain_user_id')
- ->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 没有判断评价状态
- Db::startTrans();
- try {
- // 添加评价记录
- $data = $params;
- $data['info_id'] = $id;
- $data['user_id'] = $info->maintain_user_id;
- ApproveMaintainUserLog::create($data);
- // 更新维修信息
- $params['comment_status'] = CommonConstant::IS_WHO_1;
- ApproveMaintain::where('info_id', $id)->update($params);
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- except('出现错误:' . $e->getMessage());
- }
- break;
- }
- return true;
- }
- /**
- * 下载文件pdf
- *
- * @param integer $id 申请ID
- **/
- public static function download($id)
- {
- $data = self::get_detail($id ?: 0, [], CommonConstant::IS_WHO_0, 'detail');
- if (!$data) {
- except('申请记录不存在或已删除');
- }
- $data = $data->toArray();
- unset($data['approve'][0]);// 去掉审批流程里面的创建人
- $data['approve_count'] = count($data['approve']) + count($data['approve_copy']);
- $data['subject'] = sysconf('subject');
- $vo = $data;
- $template = 'index_' . $data['module'];
- $get_module_list = CommonConstant::get_module_list();
- $get_approve_status_list = CommonConstant::get_approve_status_list();
- $get_approve_status_list_admin = CommonConstant::get_approve_status_list_admin();
- $get_is_who_list = CommonConstant::get_is_who_list();
- $get_pay_type_list = ApplyConstant::get_pay_type_list();
- $get_degree_list = OfferConstant::get_degree_list();
- $get_type_list = CommonService::get_type_list($data['module']);
- $data = compact('vo','template','get_module_list','get_approve_status_list','get_approve_status_list_admin','get_is_who_list','get_pay_type_list','get_degree_list','get_type_list');
- $html = self::get_html($data);
- $realpath = self::getPath();
- $pdf = new \TCPDF();
- $pdf = self::setPdfAttr($pdf);
- self::exportPdf($pdf,$html,$realpath);
- return ['url' => $realpath, 'fullurl' => 'http://'.$_SERVER['HTTP_HOST'].$realpath];
- }
- // 获取html
- public static function get_html($data){
- // $htmlUrl=app()->getRootPath() . 'public/upload/1.html';
- // $html=file_get_contents($htmlUrl);
- $html = view('common/'.$data['template'],$data)->getContent();
- return $html;
- }
- // 获取路径
- public static function getPath(){
- //保存文件
- $subPath = date('Ymd');
- $savePath = './upload/pdf/' . $subPath;
- // 检查上传目录
- if (!is_dir($savePath)) {
- // 检查目录是否编码后的
- if (is_dir(base64_decode($savePath))) {
- $savePath = base64_decode($savePath);
- } else {
- // 尝试创建目录
- if (!mkdir($savePath, 0755, true)) {
- except('上传目录' . $savePath . '不存在');
- return false;
- }
- }
- } else {
- if (!is_writeable($savePath)) {
- except('上传目录' . $savePath . '不可写');
- return false;
- }
- }
- $filename = date('Ymd') . sha1(date('YmdHis', time()) . uniqid());
- $filename = date('Ymd') . date('YmdHis', time()).time();
- $suffix = '.pdf';
- $realpath = substr($savePath, 1) . '/' . $filename . $suffix;
- return $realpath;
- }
- // 设置PDF参数
- public static function setPdfAttr($pdf)
- {
- // 设置页面信息
- $pdf->SetTitle("简历文件");
- $pdf->setAuthor('Dya');
- $pdf->setCreator(PDF_CREATOR);
- $pdf->SetSubject('TCPDF Tutorial');
- $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
- //页眉页脚
- $pdf->setPrintHeader(false);
- $pdf->setPrintFooter(false);
- // 自动分页 (第二个参数可以设置距离底部多少距离时分页)
- $pdf->setAutoPageBreak(true,15);
- // 设置边距(左 上 右 下) 右边距默认左侧值 下边距是bool值(是否覆盖默认页边距)
- $pdf->setMargins(26, 18, 26);
- // 定义默认的单间距字体 (设置为等宽字体)
- $pdf->SetDefaultMonospacedFont('courier');
- // 设置图像比例因子
- $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
- // 设置默认字体
- $pdf->SetFont('stsongstdlight', '', 10, '', true);
- return $pdf;
- }
- // 生成下载pdf
- public static function exportPdf($pdf,$html,$realpath)
- {
- // 新增页面
- $pdf->AddPage();
- $pdf->writeHTML($html, true, false, true, false, '');
- // $pdf->lastPage();
- $pdf->Output($_SERVER['DOCUMENT_ROOT'] . $realpath, 'F');
- }
- }
|