123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- <?php
- namespace App\Service;
- use App\Model\Depart;
- use App\Model\Employee;
- use App\Model\Inventory;
- use App\Model\InventorySub;
- use App\Model\OrderInventoryStock;
- use App\Model\Product;
- use App\Model\Storehouse;
- use Illuminate\Support\Facades\DB;
- class InventoryService extends Service
- {
- public function edit($data,$user){
- list($status,$msg) = $this->orderRule($data, $user,false);
- if(!$status) return [$status,$msg];
- try{
- DB::beginTransaction();
- $inventory_model = Inventory::where('order_number',$data['order_number'])->first();
- $inventory_model->counted_id = $data['counted_id'];
- $inventory_model->counted_time = $data['counted_time'];
- $inventory_model->mark = $data['mark'];
- $inventory_model->save();
- InventorySub::where('del_time',0)
- ->where('order_number',$data['order_number'])
- ->update(['del_time'=>time()]);
- if(! empty($data['sub'])){
- $sub = [];
- foreach ($data['sub'] as $value){
- $sub[] = [
- 'product_id' => $value['product_id'],
- 'storehouse_id' => $data['storehouse_id'],
- 'order_number' => $data['order_number'],
- 'book_num' => $value['book_num'],
- 'counted_num' => $value['counted_num'],
- 'final_num' => $value['final_num'],
- 'price' => $value['price'] ?? 0,
- 'inventory_id' => $inventory_model->id,
- 'final_amount' => $value['final_amount'] ?? 0,
- ];
- }
- InventorySub::insert($sub);
- $is_check_stock = OrderInventoryStock::where('order_number', $data['order_number'])
- ->where('del_time',0)
- ->value('is_check_stock') ?? 0;
- ProductInventoryService::changeLockNumber($user,$msg[0],$msg[1],$is_check_stock);
- }
- DB::commit();
- }catch (\Throwable $e){
- DB::rollBack();
- return [false,$e->getMessage()];
- }
- if(! empty($data['check'])) {
- list($status,$msg) = (new CheckService())->checkAll([
- "id" => $inventory_model->id,
- "order_number" => $data['order_number'],
- "opt_case" => CheckService::fourteen,
- "menu_id" => $data['menu_id']
- ],$user);
- // if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
- }
- return [true, ''];
- }
- public function add($data,$user){
- list($status,$msg) = $this->orderRule($data, $user);
- if(!$status) return [$status,$msg];
- try{
- DB::beginTransaction();
- $inventory_model = new Inventory();
- $inventory_model->order_number = $data['order_number'];
- $inventory_model->storehouse_id = $data['storehouse_id'];
- $inventory_model->counted_id = $data['counted_id'];
- $inventory_model->counted_time = $data['counted_time'];
- $inventory_model->mark = $data['mark'] ?? '';
- $inventory_model->depart_id = $data['depart_id'] ?? 0;
- $inventory_model->top_depart_id = $data['top_depart_id'] ?? 0;
- $inventory_model->crt_id = $user['id'];
- $inventory_model->save();
- if(! empty($data['sub'])){
- $sub = [];
- foreach ($data['sub'] as $value){
- $sub[] = [
- 'product_id' => $value['product_id'],
- 'storehouse_id' => $data['storehouse_id'],
- 'order_number' => $data['order_number'],
- 'book_num' => $value['book_num'],
- 'counted_num' => $value['counted_num'],
- 'final_num' => $value['final_num'],
- 'price' => $value['price'] ?? 0,
- 'inventory_id' => $inventory_model->id,
- 'final_amount' => $value['final_amount'] ?? 0,
- ];
- }
- InventorySub::insert($sub);
- ProductInventoryService::changeLockNumber($user,$msg[0],[]);
- }
- //单据创建时是否校验库存
- (new CheckService())->orderInventoryInsert(['order_number' => $data['order_number'], 'is_check_stock' => $data['is_check_stock']]);
- DB::commit();
- }catch (\Throwable $exception){
- DB::rollBack();
- if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
- return [false, '网络波动,请重新操作!'];
- }
- return [false,$exception->getMessage()];
- }
- if(! empty($data['check'])) {
- list($status,$msg) = (new CheckService())->checkAll([
- "id" => $inventory_model->id,
- "order_number" => $data['order_number'],
- "opt_case" => CheckService::fourteen,
- "menu_id" => $data['menu_id']
- ],$user);
- // if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
- }
- return [true, ''];
- }
- public function detail($data,$user){
- if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
- if(! empty($data['id'])){
- $inventory = Inventory::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- }else{
- $inventory = Inventory::where('del_time',0)
- ->where('order_number',$data['order_number'])
- ->first();
- $data['id'] = empty($inventory->id) ? 0 : $inventory->id;
- }
- if(empty($inventory)) return [false,'盘点单不存在或已被删除'];
- $inventory = $inventory->toArray();
- $inventory['state_title'] = Inventory::$name[$inventory['state']] ?? '';
- $inventory['storehouse_title'] = Storehouse::where('id',$inventory['storehouse_id'])->value('title');
- $emp_map = Employee::whereIn('id', [$inventory['crt_id'], $inventory['counted_id']])
- ->pluck('emp_name','id')
- ->toArray();
- $inventory['crt_name'] = $emp_map[$inventory['crt_id']] ?? '';
- $inventory['counted_name'] = $emp_map[$inventory['counted_id']] ?? '';
- $inventory['counted_time'] = $inventory['counted_time'] ? date("Y-m-d", $inventory['counted_time']): '';
- $inventory['crt_time'] = $inventory['crt_time'] ? date("Y-m-d H:i:s", $inventory['crt_time']): '';
- $sub = InventorySub::where('del_time',0)
- ->where('inventory_id', $data['id'])
- ->select('product_id','counted_num','final_num','price','book_num','final_amount')
- ->get()->toArray();
- $map = (new ProductService())->getProductDetail(array_column($sub,'product_id'));
- foreach ($sub as $key => $value){
- $tmp = $map[$value['product_id']] ?? [];
- $sub[$key]['title'] = $tmp['title'] ?? "";
- $sub[$key]['code'] = $tmp['code'] ?? "";
- $sub[$key]['size'] = $tmp['size'] ?? "";
- }
- $inventory['sub'] = $sub;
- return [true, $inventory];
- }
- public function del($data,$user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $inventory = Inventory::where('del_time',0)->where('id',$data['id'])->first();
- if(empty($inventory)) return [false,'盘点单不存在或已被删除'];
- $inventory = $inventory->toArray();
- if($inventory['state'] > Inventory::STATE_ZERO) return [false,'请确认盘点单状态,操作失败'];
- $time = time();
- $product_save = $this->getSaveDetail($data['id']);
- try {
- DB::beginTransaction();
- Inventory::where('id',$data['id'])->update([
- 'del_time'=> $time
- ]);
- InventorySub::where('del_time',0)->where('inventory_id',$data['id'])->update([
- 'del_time'=> $time
- ]);
- $is_check_stock = OrderInventoryStock::where('order_number', $inventory['order_number'])
- ->where('del_time',0)
- ->value('is_check_stock') ?? 0;
- //锁定库存释放
- if(! empty($product_save)) ProductInventoryService::changeLockNumber($user,[],$product_save,$is_check_stock);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false, $exception->getMessage()];
- }
- return [true, ''];
- }
- public function getList($data,$user){
- $model = Inventory::Clear($user,$data);
- $model = $model->where('del_time',0)
- ->select('order_number','id','storehouse_id','state','counted_id','counted_time','crt_time')
- ->orderby('id', 'desc');
- 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['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
- if(! empty($data['storehouse_id'])) $model->where('storehouse_id', $data['storehouse_id']);
- if(! empty($data['counted_name'])) {
- $id = $this->forSearch($data);
- $model->whereIn('counted_id',$id);
- }
- if(isset($data['state'])) $model->where('state', $data['state']);
- if(! empty($data['product_code'])) {
- $id = $this->forSearch($data);
- $model->whereIn('id',$id);
- }
- $list = $this->limit($model,'',$data);
- $list = $this->fillListData($list);
- return [true, $list];
- }
- public function fillListData($data){
- if(empty($data['data'])) return $data;
- $storehouse_map = Storehouse::whereIn('id',array_column($data['data'],'storehouse_id'))
- ->pluck('title','id')
- ->toArray();
- $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'counted_id'), array_column($data['data'],'crt_id'))))
- ->pluck('emp_name','id')
- ->toArray();
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['counted_time'] = $value['counted_time'] ? date("Y-m-d",$value['counted_time']) : '';
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']) : '';
- $data['data'][$key]['storehouse_name'] = $storehouse_map[$value['storehouse_id']] ?? '';
- $data['data'][$key]['counted_name'] = $emp_map[$value['counted_id']] ?? '';
- $data['data'][$key]['state_name'] = Inventory::$name[$value['state']] ?? '';
- }
- return $data;
- }
- public function forSearch($data){
- if(! empty($data['product_code'])){
- $product = Product::where('code', 'LIKE', '%'.$data['product_code'].'%')
- ->select('id')
- ->get()->toArray();
- $inventory_id = InventorySub::where('del_time',0)
- ->whereIn('product_id', array_column($product,'id'))
- ->select('inventory_id')
- ->get()->toArray();
- return array_column($inventory_id, 'inventory_id');
- }
- if(! empty($data['counted_name'])){
- $employee = Employee::where('emp_name', 'LIKE', '%'.$data['counted_name'].'%')
- ->select('id')
- ->get()->toArray();
- return array_column($employee, 'id');
- }
- return [];
- }
- public function orderRule(&$data, $user, $is_add = true){
- if(empty($data['counted_id'])) $data['counted_id'] = $user['id'];
- if(empty($data['counted_time'])) return [false,'盘点日期不能为空'];
- $data['counted_time'] = $this->changeDateToDate($data['counted_time']);
- if(empty($data['storehouse_id'])) return [false,'仓库不能为空'];
- if(empty($data['sub'])) return [false, '盘点明细不能为空'];
- $product_submit = $product_submit_reduce = $product_id = [];
- foreach ($data['sub'] as $value){
- if(empty($value['product_id'])) return [false,"盘点单产品ID不为空"];
- if(! is_numeric($value['book_num'])) return [false, '账面数量不合法'];
- $bool = $this->checkNumber($value['counted_num']);
- if(! $bool) return [false, '盘点数量不合法,请输入大于等于0的两位数值'];
- if(! is_numeric($value['final_num'])) return [false, '盈亏数量不合法'];
- $num = bcsub($value['counted_num'],$value['book_num'],2);
- $num = floatval($num);
- $final_num = floatval($value['final_num']);
- if($num != $final_num) return [false,'盈亏数量计算错误'];
- if(isset($value['price'])){
- $bool = $this->checkNumber($value['price']);
- if(! $bool) return [false, '盘点单价不合法,需是大于等于0的两位数值'];
- }
- if(isset($value['final_amount'])){
- $bool = $this->checkNumber($value['final_amount']);
- if(! $bool) return [false, '盈亏金额不合法,需是大于等于0的两位数值'];
- }
- $key = $value['product_id'] . ',' .$data['storehouse_id'];
- if(! isset($product_submit[$key])) $product_submit[$key] = 0;
- if($value['final_num'] < 0){
- if(isset($product_submit_reduce[$key])){
- $product_submit_reduce[$key] += $value['final_num'];
- }else{
- $product_submit_reduce[$key] = $value['final_num'];
- }
- $product_id[] = $value['product_id'];
- }
- }
- //所属部门 以及 顶级部门
- if(empty($data['depart_id'])) {
- $data['depart_id'] = $this->getDepart($user);
- $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
- }
- $data['is_check_stock'] = $user['is_check_stock'];
- if($is_add){
- $order_number = (new OrderNoService())->createOrderNumber(Inventory::prefix);
- if(empty($order_number)) return [false,'盘点单号生成失败'];
- $data['order_number'] = $order_number;
- }else{
- if(empty($data['order_number'])) return [false,'盘点单号不能为空'];
- $model = Inventory::where('order_number',$data['order_number'])->where('del_time',0)->first();
- if(empty($model)) return [false, '盘点单不存在或已被删除'];
- $order = $model->toArray();
- if($order['state'] != Inventory::STATE_ZERO) return [false, '请确认单据状态,编辑失败'];
- if($order['storehouse_id'] != $data['storehouse_id']) return [false, '仓库不允许编辑'];
- //订单编辑提交限制
- $current_top_depart_id = $this->getMyTopDepart($user);
- list($status, $msg) = $this->returnOrderEditErrorCommon($current_top_depart_id, $order['top_depart_id']);
- if(! $status) return [false, $msg];
- }
- //校验是否存在产品盘点
- list($status, $msg) = $this->issetProduct($data, $product_submit);
- if(! $status) return [false, $msg];
- $id = $data['id'] ?? 0;
- $product_save_reduce = $this->getSaveDetail($id);
- if(! empty($product_submit_reduce)){
- //校验库存
- list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit_reduce, $product_save_reduce);
- if(! $status) return [false, $msg];
- }
- return [true, [$product_submit_reduce, $product_save_reduce]];
- }
- /**
- * 获取保存详情
- * @param $id
- * @return array
- */
- public function getSaveDetail($id){
- $product_save = [];
- if(empty($id)) return [];
- $sub = InventorySub::where('inventory_id',$id)
- ->where('del_time',0)
- ->get()->toArray();
- foreach ($sub as $value){
- $key = $value['product_id'] . ',' . $value['storehouse_id'];
- if($value['final_num'] < 0){//盘亏
- if(isset($product_save[$key])){
- $product_save[$key] += $value['final_num'];
- }else{
- $product_save[$key] = $value['final_num'];
- }
- }
- }
- return $product_save;
- }
- public function issetProduct($data, $product_submit){
- $id = $data['id'] ?? 0;
- $inventory = Inventory::where('del_time',0)
- ->when(! empty($id), function ($query) use ($id) {
- return $query->where('id','<>',$id);
- })
- ->where('top_depart_id',$data['top_depart_id'])
- ->where('state','<',Inventory::STATE_TWO)
- ->select('id')
- ->get()->toArray();
- $sub = InventorySub::where('del_time', 0)
- ->whereIn('inventory_id', array_column($inventory,'id'))
- ->select('product_id', 'storehouse_id', 'order_number')
- ->get()->toArray();
- $map = [];
- $product = Product::whereIn('id', array_unique(array_column($sub,'product_id')))
- ->select('id','title','code')
- ->get()->toArray();
- foreach ($product as $value){
- $map[$value['id']] = $value;
- }
- foreach ($sub as $value){
- $key = $value['product_id'] . ',' .$value['storehouse_id'];
- if(isset($product_submit[$key])){
- $pro_tmp = $map[$value['product_id']] ?? [];
- $pro_str = $pro_tmp['title'] . "( ". $pro_tmp['code'] .")";
- return [false, "产品:" . $pro_str . "在盘点单:" . $value['order_number'] . "已录入盘点"];
- }
- }
- return [true, ''];
- }
- }
|