12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157 |
- <?php
- namespace App\Service;
- use App\Model\ApplyOrder;
- use App\Model\ApplyOrderDetail;
- use App\Model\Box;
- use App\Model\BoxDetail;
- use App\Model\DispatchSub;
- use App\Model\Employee;
- use App\Model\EmployeeTeamPermission;
- use App\Model\InOutRecord;
- use App\Model\Process;
- use App\Model\ReportWorking;
- use App\Model\ReportWorkingDetail;
- use App\Model\SaleOrdersProduct;
- use App\Model\Team;
- use Illuminate\Support\Facades\DB;
- class ApplyOrderService extends Service
- {
- public function MaterialEdit($data, $user){
- list($status,$msg) = $this->MaterialRule($data,false);
- if(!$status) return [$status,$msg];
- DB::beginTransaction();
- try{
- $time = time();
- $model = new ApplyOrder();
- $model = $model->where('id',$data['id'])->first();
- $model->apply_id = $data['apply_id'];
- $model->apply_time = $data['apply_time'] ;
- $model->mark = $data['mark'] ?? "";
- $model->storehouse_id = $data['storehouse_id'];
- $model->storehouse_title = $data['storehouse_title'] ?? "";
- $model->type = $data['type'];
- $model->save();
- $id = $model->id;
- ApplyOrderDetail::where('del_time',0)->where('apply_order_id',$id)->update([
- 'del_time' => $time
- ]);
- $detail_insert = [];
- foreach ($data['order_data'] as $v){
- $detail_insert[] = [
- 'apply_order_id' => $id,
- 'data_id' => $v['id'],
- 'quantity' => $v['quantity'] ?? 0,
- 'product_no' => $v['product_no'] ?? "",
- 'product_title' => $v['product_title'] ?? "",
- 'product_size' => $v['product_size'] ?? "",
- 'product_unit' => $v['product_unit'] ?? "",
- 'technology_name' => $v['technology_name'] ?? "", //颜色
- 'top_product_title' => $v['top_product_title'] ?? "",
- 'top_product_no' => $v['top_product_title'] ?? "",
- 'type' => $data['type'],
- 'storehouse_id' => $data['storehouse_id'],
- 'crt_time' => $time,
- ];
- }
- ApplyOrderDetail::insert($detail_insert);
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getMessage()];
- }
- return [true,''];
- }
- public function MaterialAdd($data,$user){
- list($status,$msg) = $this->MaterialRule($data);
- if(!$status) return [$status,$msg];
- DB::beginTransaction();
- try{
- $time = time();
- $model = new ApplyOrder();
- $model->order_number = $data['order_number'];
- $model->apply_id = $data['apply_id'];
- $model->apply_time = $data['apply_time'] ;
- $model->mark = $data['mark'] ?? "";
- $model->storehouse_id = $data['storehouse_id'];
- $model->storehouse_title = $data['storehouse_title'] ?? "";
- $model->type = $data['type'];
- $model->crt_id = $user['id'];
- $model->save();
- $id = $model->id;
- $detail_insert = [];
- foreach ($data['order_data'] as $v){
- $detail_insert[] = [
- 'apply_order_id' => $id,
- 'data_id' => $v['id'],
- 'quantity' => $v['quantity'] ?? 0,
- 'product_no' => $v['product_no'] ?? "",
- 'product_title' => $v['product_title'] ?? "",
- 'product_size' => $v['product_size'] ?? "",
- 'product_unit' => $v['product_unit'] ?? "",
- 'technology_name' => $v['technology_name'] ?? "", //颜色
- 'top_product_title' => $v['top_product_title'] ?? "",
- 'top_product_no' => $v['top_product_title'] ?? "",
- 'type' => $data['type'],
- 'storehouse_id' => $data['storehouse_id'],
- 'crt_time' => $time,
- ];
- }
- ApplyOrderDetail::insert($detail_insert);
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getMessage()];
- }
- return [true, ''];
- }
- public function MaterialDel($data){
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
- $apply = ApplyOrder::where('id',$data['id'])->first();
- if($apply->del_time > 0) return [false,'申请单不存在或已被删除'];
- if($apply->state != ApplyOrder::state_zero) return [false, '申请单已审核,删除失败'];
- $time = time();
- try {
- DB::beginTransaction();
- $apply->del_time = $time;
- $apply->save();
- ApplyOrderDetail::where('del_time', 0)->where('apply_order_id',$data['id'])->update([
- 'del_time'=>time()
- ]);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true,''];
- }
- public function MaterialList($data){
- $model = ApplyOrder::where('del_time',0)
- ->select('*')
- ->orderBy('id','desc');
- if(isset($data['status'])) $model->where('status', $data['status']);
- if(! empty($data['type'])) $model->where('type', $data['type']);
- if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
- if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
- if(! empty($data['apply_id'])) $model->where('apply_id', $data['apply_id']);
- if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
- if(! empty($data['apply_time'][0]) && ! empty($data['apply_time'][1])) $model->whereBetween('apply_time',[$data['apply_time'][0],$data['apply_time'][1]]);
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
- if(! empty($data['dispatch_no'])){
- $dispatch_id = DispatchSub::where('del_time',0)
- ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
- ->select('id')
- ->get()->toArray();
- $dispatch_id = array_column($dispatch_id,'id');
- $detail = ApplyOrderDetail::where('del_time',0)
- ->where('type', $data['type'])
- ->whereIn('data_id', $dispatch_id)
- ->select('apply_order_id')
- ->get()->toArray();
- $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
- }
- if(! empty($data['box_no'])){
- $box_id = Box::where('del_time',0)
- ->where('order_no', 'LIKE', '%'.$data['box_no'].'%')
- ->select('id')
- ->get()->toArray();
- $box_id = array_column($box_id,'id');
- $detail = ApplyOrderDetail::where('del_time',0)
- ->where('type', $data['type'])
- ->whereIn('data_id', $box_id)
- ->select('apply_order_id')
- ->get()->toArray();
- $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
- }
- if(! empty($data['sale_order_number'])){
- if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
- $dispatch_id = DispatchSub::where('del_time',0)
- ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
- ->select('id')
- ->get()->toArray();
- $dispatch_id = array_column($dispatch_id,'id');
- $detail = ApplyOrderDetail::where('del_time',0)
- ->where('type', $data['type'])
- ->whereIn('data_id', $dispatch_id)
- ->select('apply_order_id')
- ->get()->toArray();
- $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
- }else{
- $no = SaleOrdersProduct::where('del_time',0)
- ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
- ->select('order_no')
- ->get()->toArray();
- $no = array_unique(array_column($no,'order_no'));
- $box_id = Box::where('del_time',0)
- ->whereIn('top_order_no', $no)
- ->select('id')
- ->get()->toArray();
- $box_id = array_column($box_id,'id');
- $detail = ApplyOrderDetail::where('del_time',0)
- ->where('type', $data['type'])
- ->whereIn('data_id', $box_id)
- ->select('apply_order_id')
- ->get()->toArray();
- $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
- }
- }
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true,$list];
- }
- public function fillData($data){
- if(empty($data['data'])) return $data;
- $emp = Employee::whereIn('id',array_merge_recursive(array_unique(array_column($data['data'],'crt_id')), array_unique(array_column($data['data'],'apply_id'))))
- ->pluck('emp_name','id')
- ->toArray();
- $array1 = $array2 = [];
- foreach ($data['data'] as $value){
- if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
- $array1[] = $value['id'];
- }else{
- $array2[] = $value['id'];
- }
- }
- $order1 = ApplyOrderDetail::whereIn('type',[ApplyOrder::type_one,ApplyOrder::type_two])
- ->whereIn('apply_order_id', $array1)
- ->select('data_id','apply_order_id')
- ->get()->toArray();
- $order1_map = [];
- foreach ($order1 as $value){
- $order1_map[$value['apply_order_id']][] = $value['data_id'];
- }
- $order2 = ApplyOrderDetail::whereNotIn('type',[ApplyOrder::type_one,ApplyOrder::type_two])
- ->whereIn('apply_order_id', $array2)
- ->select('data_id','apply_order_id')
- ->get()->toArray();
- $order2_map = [];
- foreach ($order2 as $value){
- $order2_map[$value['apply_order_id']][] = $value['data_id'];
- }
- $map1 = DispatchSub::whereIn('id', array_unique(array_column($order1,'data_id')))
- ->pluck('out_order_no','id')
- ->toArray();
- $map2 = Box::whereIn('id', array_unique(array_column($order2,'data_id')))
- ->pluck('top_order_no','id')
- ->toArray();
- $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
- ->pluck('out_order_no','order_no')
- ->toArray();
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
- $data['data'][$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
- $data['data'][$key]['apply_name'] = $emp[$value['apply_id']] ?? '';
- if(in_array($value['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
- $order_tmp = $order1_map[$value['id']] ?? [];
- $order_tmp = array_unique($order_tmp);
- $sale_order_number = [];
- foreach ($order_tmp as $v){
- $str = $map1[$v] ?? '';
- if(empty($str)) continue;
- $sale_order_number[] = $str;
- }
- $sale_order_number = array_unique($sale_order_number);
- $sale_order_number = implode(',',$sale_order_number);
- $data['data'][$key]['sale_order_number'] = $sale_order_number;
- }else{
- $order_tmp = $order2_map[$value['id']] ?? [];
- $order_tmp = array_unique($order_tmp);
- $sale_order_number = [];
- foreach ($order_tmp as $v){
- $m2_t = $map2[$v] ?? '';
- $m2_t = $map2_fin[$m2_t] ?? "";
- if(empty($m2_t)) continue;
- $sale_order_number[] = $m2_t;
- }
- $sale_order_number = array_unique($sale_order_number);
- $sale_order_number = implode(',',$sale_order_number);
- $data['data'][$key]['sale_order_number'] = $sale_order_number;
- }
- }
- return $data;
- }
- public function MaterialDetail($data){
- if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
- $id = $data['id'];
- $detail = ApplyOrder::where('del_time',0)
- ->where('id',$id)
- ->first();
- if(empty($detail)) return [false,'申请单不存在或已被删除'];
- $detail = $detail->toArray();
- $detail['apply_title'] = Employee::where('id', $detail['apply_id'])->value('emp_name');
- $apply_d = ApplyOrderDetail::where('del_time', 0)
- ->where('apply_order_id', $id)
- ->get()->toArray();
- $apply_id = array_column($apply_d, 'data_id');
- $d = [];
- if($detail['type'] == ApplyOrder::type_one){
- $d = DispatchSub::where('del_time',0)
- ->whereIn('id', $apply_id)
- ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
- ->get()->toArray();
- $args = "";
- foreach ($d as $value){
- $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
- }
- $args = rtrim($args, 'OR ');
- $d_no_map = [];
- $d_no = DispatchSub::where('del_time',0)
- ->whereRaw($args)
- ->select('dispatch_no as order_no','order_product_id','crt_time')
- ->get()->toArray();
- foreach ($d_no as $value){
- if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
- if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
- }else{
- $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
- }
- }
- }elseif ($detail['type'] == ApplyOrder::type_two){
- $d = DispatchSub::where('del_time',0)
- ->whereIn('id', $apply_id)
- ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
- ->get()->toArray();
- $args = "";
- foreach ($d as $value){
- $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
- }
- $args = rtrim($args, 'OR ');
- $d_no_map = [];
- $d_no = DispatchSub::where('del_time',0)
- ->whereRaw($args)
- ->select('dispatch_no as order_no','order_product_id','crt_time')
- ->get()->toArray();
- foreach ($d_no as $value){
- if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
- if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
- }else{
- $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
- }
- }
- }elseif ($detail['type'] == ApplyOrder::type_three){
- $d = Box::where('del_time',0)
- ->whereIn('id', $apply_id)
- ->select('id', 'order_no')
- ->get()->toArray();
- }elseif ($detail['type'] == ApplyOrder::type_four){
- $d = Box::where('del_time',0)
- ->whereIn('id', $apply_id)
- ->select('id', 'order_no')
- ->get()->toArray();
- }
- $d_tmp = array_column($d,null,'id');
- $return = [];
- foreach ($apply_d as $t){
- $tmp = $d_tmp[$t['data_id']] ?? [];
- $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
- $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
- $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
- $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
- if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
- $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);
- }else{
- $order_no = $tmp['order_no'] ?? "";
- }
- if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
- $product_unit = "吨";
- }else{
- $product_unit = $t['product_unit'] ?? "";
- }
- $return[] = [
- 'id' => $t['data_id'] ?? 0,
- 'quantity' => $t['quantity'] ?? 0,
- 'product_no' => $t['product_no'] ?? "",
- 'product_title' => $t['product_title'] ?? "",
- 'product_size' => $t['product_size'] ?? "",
- 'product_unit' => $product_unit,
- 'top_product_title' => $top_product_title,
- 'top_product_no' => $top_product_no,
- 'technology_name' => $technology_name, //颜色
- 'wood_name' => $wood_name,
- 'order_no' => $order_no,
- ];
- }
- $detail['order_data'] = $return;
- return [true, $detail];
- }
- public function MaterialRule(&$data,$is_add = true){
- if($this->isEmpty($data,'apply_id')) return [false,'申请人不能为空'];
- if($this->isEmpty($data,'apply_time')) return [false,'申请时间不能为空'];
- if($this->isEmpty($data,'storehouse_id')) return [false,'仓库不能为空'];
- if($this->isEmpty($data,'type')) return [false,'申请单类型不能为空'];
- if(empty($data['order_data'])) return [false, '申请单详细信息不能为空'];
- foreach ($data['order_data'] as $value){
- if(empty($value['id'])) return [false, '申请单详细信息ID不能为空'];
- if($data['type'] != ApplyOrder::type_three){
- if(empty($value['quantity'])) return [false, '申请单详细信息数量不能为空'];
- }
- }
- $id = array_unique(array_column($data['order_data'],'id'));
- if($data['type'] == ApplyOrder::type_one){
- $dispatch = DispatchSub::where('del_time',0)
- ->whereIn('id', $id)
- ->select('id', 'dispatch_no', 'status')
- ->get()->toArray();
- if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
- }elseif ($data['type'] == ApplyOrder::type_two){
- $dispatch = DispatchSub::where('del_time',0)
- ->whereIn('id', $id)
- ->select('id', 'dispatch_no', 'status')
- ->get()->toArray();
- if(count($dispatch) != count($id)) return [false, '完工单信息错误,请重新选择'];
- }elseif ($data['type'] == ApplyOrder::type_three){
- $box = Box::where('del_time',0)
- ->whereIn('id', $id)
- ->select('id', 'order_no', 'status')
- ->get()->toArray();
- if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
- }elseif ($data['type'] == ApplyOrder::type_four){
- $box = Box::where('del_time',0)
- ->whereIn('id', $id)
- ->select('id', 'order_no', 'status')
- ->get()->toArray();
- if(count($box) != count($id)) return [false, '包装单信息错误,请重新选择'];
- }else{
- return [false, '申请单类型错误'];
- }
- if($is_add){
- $data['order_number'] = $this->setOrderNO($data['type']);
- if(empty($data['order_number'])) return [false, '申请单唯一标识生成失败'];
- }else{
- if(empty($data['id'])) return [false, '申请单唯一标识不能为空'];
- if(empty($data['order_number'])) return [false, '申请单唯一标识不能为空'];
- $apply = ApplyOrder::where('id', $data['id'])->where('del_time', 0)->first();
- if(empty($apply)) return [false, '申请单不存在或已被删除'];
- if($apply->status != 0) return [false, '申请单已审核,编辑失败'];
- }
- return [true,''];
- }
- public function setOrderNO($type = ""){
- $str = date('Ymd',time());
- $order_number = ApplyOrder::where('order_number','Like','%'. $str . '%')
- ->where('type', $type)
- ->max('order_number');
- if(empty($order_number)){
- $number = str_pad(1,3,'0',STR_PAD_LEFT);
- $number = $str . $number;
- }else{
- $tmp = substr($order_number, -3);
- $tmp = $tmp + 1;
- //超过999
- if(strlen($tmp) > 3) return '';
- $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
- $number = $str . $number;
- }
- return $number;
- }
- public function createSQ($data, $user, $type = 0, $status = 1){
- if(empty($data) || empty($type)) return [false, '自动生成申请单参数不能为空'];
- if($type == ApplyOrder::type_one){
- $storehouse_id = "001";
- $storehouse_title = "原料仓";
- }elseif ($type == ApplyOrder::type_two){
- $storehouse_id = "004";
- $storehouse_title = "待清洗原材料仓";
- }elseif ($type == ApplyOrder::type_three){
- $storehouse_id = "002";
- $storehouse_title = "产成品仓";
- }else{
- $storehouse_id = "003";
- $storehouse_title = "包材、辅料仓" ;
- }
- try{
- DB::beginTransaction();
- $order_number = $this->setOrderNO($type);
- // list($status,$msg) = $this->limitingSendRequestBackg("spAdd" . $order_number . $type);
- // if(! $status) return [false, $msg];
- $time = time();
- $model = new ApplyOrder();
- $model->order_number = $order_number;
- $model->apply_id = $user['id'];
- $model->apply_time = time();
- $model->storehouse_id = $storehouse_id;
- $model->storehouse_title = $storehouse_title;
- $model->type = $type;
- $model->status = $status; //是否审核
- $model->save();
- $id = $model->id;
- $detail_insert = [];
- foreach ($data as $v){
- $detail_insert[] = [
- 'apply_order_id' => $id,
- 'data_id' => $v['id'],
- 'quantity' => $v['quantity'] ?? 0,
- 'product_no' => $v['product_no'] ?? "",
- 'product_title' => $v['product_title'] ?? "",
- 'product_size' => $v['product_size'] ?? "",
- 'product_unit' => $v['product_unit'] ?? "",
- 'technology_name' => $v['technology_name'] ?? "",
- 'wood_name' => $v['wood_name'] ?? "",
- 'top_product_no' => $v['top_product_no'] ?? "",
- 'top_product_title' => $v['top_product_title'] ?? "",
- 'type' => $type,
- 'storehouse_id' => $storehouse_id,
- 'crt_time' => $time,
- ];
- }
- ApplyOrderDetail::insert($detail_insert);
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getMessage()];
- }
- return [true, $id];
- }
- //流水
- public function createRecord($id){
- $record = ApplyOrderDetail::where('apply_order_id', $id)
- ->where('del_time',0)
- ->get()->toArray();
- if(empty($record)) return [false, '申请单明细数据不存在或已被删除'];
- try{
- DB::beginTransaction();
- $time = time();
- $insert = [];
- foreach ($record as $value){
- if(empty($value['product_no'])) continue;
- if($value['type'] == ApplyOrder::type_one){
- $number = - $value['quantity'];
- }else{
- $number = $value['quantity'];
- }
- $insert[] = [
- 'apply_order_id' => $id,
- 'product_no' => $value['product_no'],
- 'number' => $number,
- 'type' => $value['type'],
- 'storehouse_id' => $value['storehouse_id'],
- 'crt_time' => $time,
- ];
- }
- if(! empty($insert)) InOutRecord::insert($insert);
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getMessage()];
- }
- return [true, ''];
- }
- //报工
- public function reportWorkingEdit($data, $user){
- list($status,$msg) = $this->reportWorkingRule($data,false);
- if(!$status) return [$status,$msg];
- DB::beginTransaction();
- try{
- $time = time();
- $model = new ReportWorking();
- $model = $model->where('id',$data['id'])->first();
- $model->report_time = $data['report_time'];
- $model->mark = $data['mark']?? "";
- $model->save();
- $id = $model->id;
- ReportWorkingDetail::where('del_time',0)->where('report_working_id',$id)->update([
- 'del_time' => $time
- ]);
- //班组下的人
- $team_man = [];
- $model_t = new EmployeeTeamPermission();
- $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();
- foreach ($team_array as $v){
- $team_man[$v['team_id']][] = $v['employee_id'];
- }
- $detail_insert = [];
- foreach ($data['order_data'] as $v){
- $t = $team_man[$v['team_id']] ?? [];
- foreach ($t as $t_v){
- $detail_insert[] = [
- 'report_working_id' => $id,
- 'data_id' => $v['id'],
- 'quantity' => $v['quantity'] ?? 0,
- 'process_id' => $v['process_id'] ?? 0,
- 'finished_id' => $t_v,
- 'team_id' => $v['team_id'],
- 'crt_time' => $time,
- ];
- }
- }
- ReportWorkingDetail::insert($detail_insert);
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getMessage()];
- }
- return [true,''];
- }
- public function reportWorkingAdd($data,$user){
- list($status,$msg) = $this->reportWorkingRule($data);
- if(!$status) return [$status,$msg];
- DB::beginTransaction();
- try{
- $time = time();
- $model = new ReportWorking();
- $model->order_number = $data['order_number'];
- $model->report_time = $data['report_time'];
- $model->mark = $data['mark']?? "";
- $model->crt_id = $user['id'];
- $model->save();
- $id = $model->id;
- //班组下的人
- $team_man = [];
- $model_t = new EmployeeTeamPermission();
- $team_array = $model_t->select('employee_id', 'team_id')->get()->toArray();
- foreach ($team_array as $v){
- $team_man[$v['team_id']][] = $v['employee_id'];
- }
- $detail_insert = [];
- foreach ($data['order_data'] as $v){
- $t = $team_man[$v['team_id']] ?? [];
- foreach ($t as $t_v){
- $detail_insert[] = [
- 'report_working_id' => $id,
- 'data_id' => $v['id'],
- 'quantity' => $v['quantity'] ?? 0,
- 'process_id' => $v['process_id'] ?? 0,
- 'finished_id' => $t_v,
- 'team_id' => $v['team_id'],
- 'crt_time' => $time,
- ];
- }
- }
- ReportWorkingDetail::insert($detail_insert);
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return [false,$e->getMessage()];
- }
- return [true, ''];
- }
- public function reportWorkingDel($data){
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
- $apply = ReportWorking::where('id',$data['id'])->first();
- if($apply->del_time > 0) return [false,'报工单不存在或已被删除'];
- if($apply->state != ReportWorking::state_zero) return [false, '报工单已审核,删除失败'];
- $time = time();
- try {
- DB::beginTransaction();
- $apply->del_time = $time;
- $apply->save();
- ReportWorkingDetail::where('del_time', 0)->where('report_working_id',$data['id'])->update([
- 'del_time'=>time()
- ]);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true,''];
- }
- public function reportWorkingList($data){
- $model = ReportWorking::where('del_time',0)
- ->select('*')
- ->orderBy('id','desc');
- if(isset($data['status'])) $model->where('status', $data['status']);
- if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
- if(! empty($data['crt_id'])) $model->where('crt_id', $data['crt_id']);
- if(! empty($data['report_time'][0]) && ! empty($data['report_time'][1])) $model->whereBetween('report_time',[$data['report_time'][0],$data['report_time'][1]]);
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
- if(! empty($data['dispatch_no1'])) {
- $dispatch_id = DispatchSub::where('del_time',0)
- ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
- ->select('id')
- ->get()->toArray();
- $dispatch_id = array_column($dispatch_id,'id');
- $detail = ReportWorkingDetail::where('del_time',0)
- ->whereIn('data_id', $dispatch_id)
- ->select('report_working_id')
- ->get()->toArray();
- $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
- }
- if(! empty($data['dispatch_no'])) {
- $dispatch_id = DispatchSub::where('del_time',0)
- ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
- ->select('id')
- ->get()->toArray();
- $dispatch_id = array_column($dispatch_id,'id');
- $detail = ReportWorkingDetail::where('del_time',0)
- ->whereIn('data_id', $dispatch_id)
- ->select('report_working_id')
- ->get()->toArray();
- $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
- }
- if(! empty($data['sale_order_number'])){
- $id = SaleOrdersProduct::where('del_time',0)
- ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
- ->select('id')
- ->get()->toArray();
- $dispatch_id = DispatchSub::where('del_time',0)
- ->whereIn('sale_orders_product_id',array_column($id,'id'))
- ->select('id')
- ->get()->toArray();
- $detail = ReportWorkingDetail::where('del_time',0)
- ->whereIn('data_id', $dispatch_id)
- ->select('report_working_id')
- ->get()->toArray();
- $model->whereIn('id', array_unique(array_column($detail,'report_working_id')));
- }
- $list = $this->limit($model,'',$data);
- $list = $this->reportWorkingfillData($list);
- return [true,$list];
- }
- public function reportWorkingfillData($data){
- if(empty($data['data'])) return $data;
- $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
- ->pluck('emp_name','id')
- ->toArray();
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['report_time'] = $value['report_time'] ? date('Y-m-d H:i:s',$value['report_time']) : '';
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
- $data['data'][$key]['status_title'] = ReportWorking::$state_name[$value['status']] ?? "";
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
- }
- return $data;
- }
- public function reportWorkingDetail($data){
- if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
- $id = $data['id'];
- $detail = ReportWorking::where('del_time',0)
- ->where('id',$id)
- ->first();
- if(empty($detail)) return [false,'报工单不存在或已被删除'];
- $detail = $detail->toArray();
- $apply_d = ReportWorkingDetail::where('del_time', 0)
- ->where('report_working_id', $id)
- ->get()->toArray();
- $apply_id = array_column($apply_d, 'data_id');
- //派工单
- $d = DispatchSub::where('del_time',0)
- ->whereIn('id', $apply_id)
- ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','process_id','product_unit','price','product_size')
- ->get()->toArray();
- $d_tmp = array_column($d,null,'id');
- $process_map = Process::whereIn('id',array_unique(array_column($d,'process_id')))
- ->pluck('title','id')
- ->toArray();
- $team_map = Team::pluck('title','id')->toArray();
- $return = [];
- foreach ($apply_d as $t){
- $tmp = $d_tmp[$t['data_id']] ?? [];
- $str = $t['data_id'] . $t['process_id'];
- if(! isset($return[$str])){
- $return[$str] = [
- 'id' => $t['data_id'] ?? 0,
- 'quantity' => $t['quantity'] ?? 0,
- 'product_no' => $tmp['product_no'] ?? "",
- 'product_title' => $tmp['product_title'] ?? "",
- 'product_size' => $tmp['product_size'] ?? "",
- 'product_unit' => $tmp['product_unit'] ?? "",
- 'technology_name' => $tmp['technology_name'] ?? "",
- 'wood_name' => $tmp['wood_name'] ?? "",
- 'order_no' => $tmp['order_no'] ?? "",
- 'process_id' => $tmp['process_id'] ?? 0,
- 'process_name' => $process_map[$tmp['process_id']] ?? "",
- 'team_id' => $t['team_id'] ?? 0,
- 'team_title' => $team_map[$t['team_id']] ?? '',
- ];
- }
- }
- $detail['order_data'] = array_values($return);
- return [true, $detail];
- }
- public function reportWorkingRule(&$data,$is_add = true){
- if($this->isEmpty($data,'report_time')) return [false,'报工时间不能为空'];
- if(empty($data['order_data'])) return [false, '报工单详细信息不能为空'];
- foreach ($data['order_data'] as $value){
- if(empty($value['id'])) return [false, '报工单详细信息ID不能为空'];
- if(empty($value['quantity'])) return [false, '报工单详细信息数量不能为空'];
- if(empty($value['process_id'])) return [false, '报工单详细信息工序不能为空'];
- if(empty($value['team_id'])) return [false, '报工单详细信息班组不能为空'];
- }
- $id = array_unique(array_column($data['order_data'],'id'));
- $dispatch = DispatchSub::where('del_time',0)
- ->whereIn('id', $id)
- ->select('id', 'dispatch_no', 'status')
- ->get()->toArray();
- if(count($dispatch) != count($id)) return [false, '派工单信息错误,请重新选择'];
- if($is_add){
- $data['order_number'] = $this->setOrderNO2();
- if(empty($data['order_number'])) return [false, '报工单唯一标识生成失败'];
- }else{
- if(empty($data['id'])) return [false, '报工单唯一标识不能为空'];
- if(empty($data['order_number'])) return [false, '报工单唯一标识不能为空'];
- $apply = ReportWorking::where('id', $data['id'])->where('del_time', 0)->first();
- if(empty($apply)) return [false, '报工单不存在或已被删除'];
- if($apply->status != 0) return [false, '报工单已审核,编辑失败'];
- }
- return [true,''];
- }
- public function setOrderNO2(){
- $str = date('Ymd',time());
- $model = new ReportWorking();
- $order_number = $model->where('order_number','Like','%'. $str . '%')
- ->max('order_number');
- if(empty($order_number)){
- $number = str_pad(1,3,'0',STR_PAD_LEFT);
- $number = $str . $number;
- }else{
- $tmp = substr($order_number, -3);
- $tmp = $tmp + 1;
- //超过999
- if(strlen($tmp) > 3) return '';
- $number = str_pad($tmp,3,'0',STR_PAD_LEFT);
- $number = $str . $number;
- }
- return $number;
- }
- public function applyOrderReportList($data){
- if(empty($data['apply_time'][0]) || empty($data['apply_time'][1])) return [false, '申请日期必须选择!'];
- if(empty($data['type'])) return [false, 'TYPE不能为空!'];
- $model = ApplyOrder::where('del_time',0)
- ->where('apply_time',">=",$data['apply_time'][0])
- ->where('apply_time',"<=",$data['apply_time'][1])
- ->where('type',$data['type'])
- ->select('id','order_number','apply_time','mark','status','storehouse_title')
- ->orderBy('id','desc');
- if(isset($data['status'])) $model->where('status', $data['status']);
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])){
- $model->where('crt_time',">=",$data['crt_time'][0]);
- $model->where('crt_time',"<=",$data['crt_time'][1]);
- }
- if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
- if(! empty($data['storehouse_title'])) $model->where('storehouse_title', 'LIKE', '%'.$data['storehouse_title'].'%');
- if(! empty($data['sale_order_number'])){
- if(in_array($data['type'], [ApplyOrder::type_one,ApplyOrder::type_two])){
- $dispatch_id = DispatchSub::where('del_time',0)
- ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
- ->select('id')
- ->get()->toArray();
- $dispatch_id = array_column($dispatch_id,'id');
- $detail = ApplyOrderDetail::where('del_time',0)
- ->where('type', $data['type'])
- ->whereIn('data_id', $dispatch_id)
- ->select('apply_order_id')
- ->get()->toArray();
- $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
- }else{
- $no = SaleOrdersProduct::where('del_time',0)
- ->where('out_order_no', 'LIKE', '%'.$data['sale_order_number'].'%')
- ->select('order_no')
- ->get()->toArray();
- $no = array_unique(array_column($no,'order_no'));
- $box_id = Box::where('del_time',0)
- ->whereIn('top_order_no', $no)
- ->select('id')
- ->get()->toArray();
- $box_id = array_column($box_id,'id');
- $detail = ApplyOrderDetail::where('del_time',0)
- ->where('type', $data['type'])
- ->whereIn('data_id', $box_id)
- ->select('apply_order_id')
- ->get()->toArray();
- $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
- }
- }
- if(! empty($data['dispatch_no'])){
- $dispatch_id = DispatchSub::where('del_time',0)
- ->where('dispatch_no', 'LIKE', '%'.$data['dispatch_no'].'%')
- ->select('id')
- ->get()->toArray();
- $dispatch_id = array_column($dispatch_id,'id');
- $detail = ApplyOrderDetail::where('del_time',0)
- ->where('type', $data['type'])
- ->whereIn('data_id', $dispatch_id)
- ->select('apply_order_id')
- ->get()->toArray();
- $model->whereIn('id', array_unique(array_column($detail,'apply_order_id')));
- }
- $list = $model->get()->toArray();
- if(empty($list)) return [true, []];
- $apply_id = array_column($list, 'id');
- //明细数据
- $result = ApplyOrderDetail::where('del_time',0)
- ->whereIn('apply_order_id', $apply_id)
- ->get()->toArray();
- if(empty($result)) return [true, []];
- //组织主表需要的数据
- $list_map = $this->makeListMap($list, $result, $data['type']);
- $data_id = array_unique(array_column($result, 'data_id'));
- list($d, $d_no_map) = $this->getDetailList($data_id, $data['type']);
- $return = [];
- $d_tmp = array_column($d,null,'id');
- foreach ($result as $t){
- $tmp = $d_tmp[$t['data_id']] ?? [];
- $technology_name = ! empty($t['technology_name']) ? $t['technology_name'] : $tmp['technology_name'] ?? "";
- $wood_name = ! empty($t['wood_name']) ? $t['wood_name'] : $tmp['wood_name'] ?? "";
- $top_product_title = ! empty($t['top_product_title']) ? $t['top_product_title'] : $tmp['product_title'] ?? "";
- $top_product_no = ! empty($t['top_product_no']) ? $t['top_product_no'] : $tmp['product_no'] ?? "";
- if(! empty($tmp['order_product_id']) && ! empty($tmp['crt_time']) && isset($d_no_map[$tmp['order_product_id'] . $tmp['crt_time']])){
- $order_no = implode(',', $d_no_map[$tmp['order_product_id'] . $tmp['crt_time']]);
- }else{
- $order_no = $tmp['order_no'] ?? "";
- }
- if($t['type'] == ApplyOrder::type_two || $t['type'] == ApplyOrder::type_three){
- $product_unit = "吨";
- }elseif($t['type'] == ApplyOrder::type_four){
- $product_unit = "只";
- }else{
- $product_unit = $t['product_unit'] ?? "";
- }
- $tmp = $list_map[$t['apply_order_id']] ?? [];
- $return[] = [
- 'order_number' => $tmp['order_number'],
- 'sale_order_number' => $tmp['sale_order_number'],
- 'apply_time' => $tmp['apply_time'],
- 'mark' => $tmp['mark'],
- 'status_title' => $tmp['status_title'],
- 'storehouse_title' => $tmp['storehouse_title'],
- 'id' => $t['data_id'] ?? 0,
- // 'main_id' => $t['id'],
- 'quantity' => $t['quantity'] ?? 0,
- 'product_no' => $t['product_no'] ?? "",
- 'product_title' => $t['product_title'] ?? "",
- 'product_size' => $t['product_size'] ?? "",
- 'product_unit' => $product_unit,
- 'top_product_title' => $top_product_title,
- 'top_product_no' => $top_product_no,
- 'technology_name' => $technology_name, //颜色
- 'wood_name' => $wood_name,
- 'order_no' => $order_no,
- ];
- }
- return [true, array_values($return)];
- }
- private function getDetailList($apply_id, $type){
- $d = $d_no_map = [];
- if($type == ApplyOrder::type_one){
- $d = DispatchSub::where('del_time',0)
- ->whereIn('id', $apply_id)
- ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','wood_name','product_no','order_product_id','crt_time')
- ->get()->toArray();
- $args = "";
- foreach ($d as $value){
- $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
- }
- $args = rtrim($args, 'OR ');
- $d_no_map = [];
- $d_no = DispatchSub::where('del_time',0)
- ->whereRaw($args)
- ->select('dispatch_no as order_no','order_product_id','crt_time')
- ->get()->toArray();
- foreach ($d_no as $value){
- if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
- if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
- }else{
- $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
- }
- }
- }elseif ($type == ApplyOrder::type_two){
- $d = DispatchSub::where('del_time',0)
- ->whereIn('id', $apply_id)
- ->select('id', 'dispatch_no as order_no','technology_name','wood_name','order_product_id','crt_time')
- ->get()->toArray();
- $args = "";
- foreach ($d as $value){
- $args = "(order_product_id = {$value['order_product_id']} and crt_time = {$value['crt_time']}) OR ";
- }
- $args = rtrim($args, 'OR ');
- $d_no_map = [];
- $d_no = DispatchSub::where('del_time',0)
- ->whereRaw($args)
- ->select('dispatch_no as order_no','order_product_id','crt_time')
- ->get()->toArray();
- foreach ($d_no as $value){
- if(isset($d_no_map[$value['order_product_id'] . $value['crt_time']])){
- if(! in_array($value['order_no'], $d_no_map[$value['order_product_id'] . $value['crt_time']])) $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
- }else{
- $d_no_map[$value['order_product_id'] . $value['crt_time']][] = $value['order_no'];
- }
- }
- }elseif ($type == ApplyOrder::type_three){
- $d = Box::where('del_time',0)
- ->whereIn('id', $apply_id)
- ->select('id', 'order_no')
- ->get()->toArray();
- }elseif ($type == ApplyOrder::type_four){
- $d = Box::where('del_time',0)
- ->whereIn('id', $apply_id)
- ->select('id', 'order_no')
- ->get()->toArray();
- }
- return [$d, $d_no_map];
- }
- private function makeListMap($data, $order, $type){
- if(empty($data)) return [];
- //申请单 所有的数据id
- $order_map = $data_id = [];
- foreach ($order as $value){
- $order_map[$value['apply_order_id']][] = $value['data_id'];
- $data_id[] = $value['data_id'];
- }
- if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
- $map1 = DispatchSub::whereIn('id', array_unique($data_id))
- ->pluck('out_order_no','id')
- ->toArray();
- }else{
- $map2 = Box::whereIn('id', array_unique($data_id))
- ->pluck('top_order_no','id')
- ->toArray();
- $map2_fin = SaleOrdersProduct::whereIn('order_no',array_unique(array_values($map2)))
- ->pluck('out_order_no','order_no')
- ->toArray();
- }
- foreach ($data as $key => $value){
- $data[$key]['apply_time'] = $value['apply_time'] ? date('Y-m-d',$value['apply_time']) : '';
- $data[$key]['status_title'] = ApplyOrder::$state_name[$value['status']] ?? "";
- $order_tmp = $order_map[$value['id']] ?? [];
- $order_tmp = array_unique($order_tmp);
- $sale_order_number = [];
- if(in_array($type, [ApplyOrder::type_one,ApplyOrder::type_two])){
- foreach ($order_tmp as $v){
- $str = $map1[$v] ?? '';
- if(empty($str)) continue;
- $sale_order_number[] = $str;
- }
- $sale_order_number = array_unique($sale_order_number);
- $sale_order_number = implode(',',$sale_order_number);
- $data[$key]['sale_order_number'] = $sale_order_number;
- }else{
- foreach ($order_tmp as $v){
- $m2_t = $map2[$v] ?? '';
- $m2_t = $map2_fin[$m2_t] ?? "";
- if(empty($m2_t)) continue;
- $sale_order_number[] = $m2_t;
- }
- $sale_order_number = array_unique($sale_order_number);
- $sale_order_number = implode(',',$sale_order_number);
- $data[$key]['sale_order_number'] = $sale_order_number;
- }
- }
- return array_column($data,null,'id');
- }
- }
|