123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- <?php
- namespace App\Service;
- use App\Model\BasicType;
- use App\Model\Construction;
- use App\Model\ConstructionProductInfo;
- use App\Model\Depart;
- use App\Model\InOutRecord;
- use App\Model\Inventory;
- use App\Model\InvoiceOrder;
- use App\Model\InvoiceOrderInfo;
- use App\Model\OrderOperation;
- use App\Model\OutBoundOrder;
- use App\Model\Product;
- use App\Model\ProductAdjustment;
- use App\Model\ProductCategory;
- use App\Model\ProductInventory;
- use App\Model\ProductInventorySet;
- use App\Model\PurchaseOrder;
- use App\Model\PurchaseOrderInfoForOutBound;
- use App\Model\ReturnExchangeOrder;
- use App\Model\SalesOrder;
- use App\Model\Setting;
- use App\Model\Storehouse;
- use Illuminate\Support\Facades\DB;
- class ProductInventoryService extends Service
- {
- //获取产品真实库存
- public static function getRealStock($product_id = [], $storehouse_id = 0){
- if(empty($product_id) || empty($storehouse_id)) return [];
- $array = ProductInventory::whereIn('product_id',$product_id)
- ->where('storehouse_id', $storehouse_id)
- ->get()->toArray();
- $return = [];
- if(! empty($array)){
- foreach ($array as $value){
- $num = bcsub($value['number'], $value['lock_number'],2);
- $return[] = [
- 'id' => $value['id'],
- 'product_id' => $value['product_id'],
- 'storehouse_id' => $value['storehouse_id'],
- 'number' => $value['number'],
- 'lock_number' => $value['lock_number'],
- 'real_number' => $num,
- ];
- }
- }
- return $return;
- }
- //暂时关闭 没用
- public static function is_check(&$user,$data){
- return;
- //是否校验库存
- $top_depart_id = $user['head']['id'] ?? 0;//总公司id
- if($data['top_depart_id'] == $top_depart_id) {
- $user['is_check_stock'] = false;
- }else{
- $set = ProductInventorySet::where('top_depart_id',$data['top_depart_id'])
- ->where('del_time',0)
- ->first();
- if(! empty($set) && ! $set->param_one) $user['is_check_stock'] = false;
- }
- }
- //更新锁定库存
- //$user 当前登录人的信息
- //$submit_total (提交的数据) $save_total(保存过的数据) todo
- public static function changeLockNumber($user, $submit_total = [], $save_total = [], $is_check_stock = 0){
- //如果外部传来则按照外部
- if($is_check_stock){
- if($is_check_stock == ProductInventorySet::type_two) return;
- }else{
- //外部没有传递 则根据当前用户
- if($user['is_check_stock'] == ProductInventorySet::type_two) return; //不校验库存
- }
- if(empty($submit_total) && empty($save_total)) return;
- //产品数扣减
- if(! empty($save_total)){
- foreach ($save_total as $key => $value){
- if(! isset($submit_total[$key])){
- $submit_total[$key] = - $value;
- }else{
- $submit_total[$key] = $submit_total[$key] - $value;
- }
- }
- }
- //更新锁定库存
- foreach ($submit_total as $key => $value){
- $tmp = explode(',',$key);
- ProductInventory::where('product_id',$tmp[0])
- ->where('storehouse_id',$tmp[1])
- ->update(['lock_number' => DB::raw('lock_number + ('. $value . ')')]);
- }
- }
- //比较库存
- public static function compareStock($user, $product_id = [],$product_submit = [], $product_save = []){
- if($user['is_check_stock'] == ProductInventorySet::type_two) return [true,'']; //不校验库存
- if(empty($product_id) || empty($product_submit)) return [false,'比较参数不能为空'];
- //库存
- $array = ProductInventory::whereIn('product_id', $product_id)
- ->select('id','product_id','number','crt_time','lock_number','storehouse_id')
- ->get()->toArray();
- if(empty($array)) return [false,'产品库存不存在'];
- //产品库存字典
- $stock = [];
- foreach ($array as $value){
- $key = $value['product_id'] . ',' . $value['storehouse_id'];
- $stock[$key] = $value;
- }
- //仓库
- $store_map = Storehouse::whereIn('id',array_unique(array_column($array,'storehouse_id')))
- ->pluck('title','id')
- ->toArray();
- //产品字典
- $map = [];
- $pro = Product::whereIn('id',$product_id)
- ->select('title','id','code')
- ->get()->toArray();
- foreach ($pro as $value){
- $map[$value['id']] = $value;
- }
- foreach ($product_submit as $key => $value){
- $tmp = explode(',',$key);
- //产品ID
- $product_id = $tmp[0];
- //仓库id
- $storehouse_id = $tmp[1];
- $storehouse_title = $store_map[$storehouse_id] ?? "";
- //产品名称
- $pro_tmp = $map[$product_id] ?? [];
- if(empty($pro_tmp)) return [false,'异常产品数据'];
- $pro_title = $pro_tmp['title'] . "( ". $pro_tmp['code'] .")";
- if(! isset($stock[$key])) return [false,'当前仓库' . $storehouse_title .' 的产品:'. $pro_title .'暂无库存'];
- //产品库存
- $stock_product = $stock[$key];
- //校验 =》 锁定库存
- $save_data = $product_save[$key] ?? 0;//已保存数量
- $tmp_lock = ($stock_product['lock_number'] > 0 ? $stock_product['lock_number'] : 0) - $save_data;
- $number = $stock_product['number'] - ($tmp_lock > 0 ? $tmp_lock : 0);
- if($value > $number) return [false, $pro_title. '数量不足,当前仓库:'. $storehouse_title . '的有效库存数量:' . $number];
- }
- return [true,''];
- }
- //现存量
- public function productInventoryListCommon($data,$user, $field = []){
- if(empty($field)){
- $field = ['a.product_id','a.id','a.number','b.title','b.code','b.product_category_id','b.unit','b.bar_code','a.crt_time','a.storehouse_id','a.top_depart_id'];
- }
- $model = ProductInventory::ATopClear($user,$data);
- $model = $model->from('product_inventory as a')
- ->join('product as b','b.id','a.product_id')
- ->select($field)
- ->orderby('a.crt_time', 'desc');
- if(! empty($data['product_name'])){
- $product_name = $data['product_name'];
- $id = Product::where('del_time',0)
- ->when(! empty($product_name), function ($query) use ($product_name) {
- return $query->where('title', 'LIKE', '%'.$product_name.'%');
- })
- ->select('id')
- ->get()->toArray();
- $model->whereIn('a.product_id',array_unique(array_column($id,'id')));
- }
- if(! empty($data['product_category_name'])){
- $product_category_name = $data['product_category_name'];
- $c = ProductCategory::where('del_time',0)
- ->where('title', 'LIKE', '%'.$product_category_name.'%')
- ->select('id')
- ->get()->toArray();
- $model->whereIn('b.product_category_id',array_unique(array_column($c,'id')));
- }
- if(!empty($data['code'])) $model->where('b.code', 'LIKE', '%'.$data['code'].'%');
- if(isset($data['is_stock'])){
- if($data['is_stock'] == 0){
- $model->where('a.number','>',0);
- }
- // else{
- // $model->where('a.number','<=',0);
- // }
- }else{
- $model->where('a.number','>',0);
- }
- if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- $model->whereBetween('a.crt_time',[$return[0],$return[1]]);
- }
- return $model;
- }
- //现存量
- public function productInventoryList($data,$user){
- $model = $this->productInventoryListCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->fillListData($list);
- return [true, $list];
- }
- public function fillListData($data){
- if(empty($data['data'])) return $data;
- $category = ProductCategory::whereIn('id',array_column($data['data'],'product_category_id'))
- ->pluck('title','id')
- ->toArray();
- $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
- ->pluck('title','id')
- ->toArray();
- $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
- ->pluck('title','id')
- ->toArray();
- $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'top_depart_id')))->pluck('title','id')->toArray();
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['unit_title'] = $basic_map[$value['unit']] ?? '';
- $data['data'][$key]['product_category_title'] = $category[$value['product_category_id']] ?? '';
- $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']):'';
- $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
- $data['data'][$key]['top_depart_title'] = $depart_map[$value['top_depart_id']] ?? '';
- }
- return $data;
- }
- //库存台账
- public function productInventoryStockListCommon($data,$user, $field = []){
- if(empty($field)){
- $field = ['a.id','a.product_id','a.number','a.order_type','a.crt_time','b.title','b.code','b.product_category_id','b.unit',DB::raw('SUM(CASE WHEN a.number < 0 THEN a.number ELSE 0 END) as out_number'),DB::raw('SUM(CASE WHEN a.number >= 0 THEN a.number ELSE 0 END) as in_number'),'a.order_number','a.crt_time','a.storehouse_id','a.top_depart_id'];
- }
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- $model = InOutRecord::ATopClear($user,$data);
- $model = $model->from('in_out_record as a')
- ->where('a.del_time',0)
- ->join('product as b','b.id','a.product_id')
- ->select($field)
- ->groupby('a.product_id','a.top_depart_id','a.order_number')
- ->orderBy('a.crt_time','asc');
- $model->whereBetween('a.crt_time',[$return[0],$return[1]]);
- if(! empty($data['product_name'])){
- $product_name = $data['product_name'];
- $id = Product::where('del_time',0)
- ->when(! empty($product_name), function ($query) use ($product_name) {
- return $query->where('title', 'LIKE', '%'.$product_name.'%');
- })
- ->select('id')
- ->get()->toArray();
- $model->whereIn('a.product_id',array_unique(array_column($id,'id')));
- }
- if(! empty($data['product_category_name'])){
- $product_category_name = $data['product_category_name'];
- $c = ProductCategory::where('del_time',0)
- ->where('title', 'LIKE', '%'.$product_category_name.'%')
- ->select('id')
- ->get()->toArray();
- $model->whereIn('b.product_category_id',array_unique(array_column($c,'id')));
- }
- if(! empty($data['code'])) $model->where('b.code', 'LIKE', '%'.$data['code'].'%');
- if(! empty($data['order_number'])) $model->where('a.order_number', 'LIKE', '%'.$data['order_number'].'%');
- return $model;
- }
- //库存台账
- public function productInventoryStockList($data,$user){
- if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false,'时间必须选择'];
- $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
- $day = $this->returnDays($return);
- if($day > 31) return [false, '库存台账查询时间仅支持范围区间在一月(31天)内'];
- $model = $this->productInventoryStockListCommon($data, $user);
- $list = $model->get()->toArray();
- $list = $this->fillData($list, $return);
- return [true, $list];
- }
- public function fillData($data,$time_arr){
- if (empty($data)) return $data;
- $start_data = InOutRecord::whereIn('product_id',array_unique(array_column($data,'product_id')))
- ->where('crt_time','<',$time_arr[0])
- ->where('del_time',0)
- ->select('product_id','number',DB::raw('sum(number) as number'),'top_depart_id')
- ->groupby('product_id','top_depart_id')
- ->get()->toArray();
- $start_map = [];
- foreach ($start_data as $value){
- $start_map[$value['product_id'].$value['top_depart_id']] = $value['number'];
- }
- $category = ProductCategory::whereIn('id',array_unique(array_column($data,'product_category_id')))
- ->pluck('title','id')
- ->toArray();
- $basic_map = BasicType::whereIn('id',array_unique(array_column($data,'unit')))
- ->pluck('title','id')
- ->toArray();
- $roll_tmp = [];
- $order_type = $this->getOrderType();
- $storehouse = Storehouse::whereIn('id',array_unique(array_column($data,'storehouse_id')))
- ->pluck('title','id')
- ->toArray();
- foreach ($data as $key => $value){
- $keys = $value['product_id'] . $value['top_depart_id'];
- if(! isset($roll_tmp[$keys])){
- if(isset($start_map[$keys])){
- $data[$key]['start_number'] = $start_map[$keys];
- }else{
- $data[$key]['start_number'] = 0;
- }
- $tmp = $data[$key]['start_number'] + $value['in_number'] + $value['out_number'];
- $data[$key]['end_number'] = round($tmp,2);
- $roll_tmp[$keys] = $data[$key]['end_number'];
- }else{
- $data[$key]['start_number'] = $roll_tmp[$keys];
- $tmp = $data[$key]['start_number'] + $value['in_number'] + $value['out_number'];
- $data[$key]['end_number'] = round($tmp,2);
- $roll_tmp[$keys] = $data[$key]['end_number'];
- }
- $data[$key]['out_number'] = abs($value['out_number']);
- $data[$key]['product_category_title'] = $category[$value['product_category_id']] ?? '';
- $data[$key]['order_name'] = $order_type[$value['order_type']] ?? '';
- $data[$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d",$value['crt_time']):'';
- $data[$key]['unit_title'] = $basic_map[$value['unit']] ?? '';
- $data[$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
- }
- return $data;
- }
- public function getOrderType(){
- $array = [];
- foreach (Construction::$prefix as $value){
- $array[$value] = '施工单';
- }
- foreach (ReturnExchangeOrder::$prefix as $key => $value){
- $array[$value] = ReturnExchangeOrder::$model_type_name[$key] ?? '';
- }
- foreach (SalesOrder::$prefix as $value){
- $array[$value] = '合同';
- }
- $array[InvoiceOrder::prefix] = '发货单';
- $array[PurchaseOrder::prefix] = '采购单';
- $array[Inventory::prefix] = '盘点单';
- $array[OutBoundOrder::prefix] = '出库单';
- $array[PurchaseOrderInfoForOutBound::prefix] = '平出库流水记录';
- $array[PurchaseOrderInfoForOutBound::prefix2] = '平库存流水记录';
- $array[ProductAdjustment::prefix] = '产品金额调整单';
- return $array;
- }
- //系统设置列表
- public function productInventorySetList($data, $user){
- $model = ProductInventorySet::TopClear($user,$data);
- $model = $model->where('del_time',0)
- ->select('id','top_depart_id','param_one','param_two','param_three')
- ->orderby('id', 'desc');
- $list = $model->get()->toArray();
- $map = Depart::whereIn('id',array_column($list,'top_depart_id'))->pluck('title','id')->toArray();
- foreach ($list as $key => $value){
- $list[$key]['top_depart_title'] = $map[$value['top_depart_id']] ?? "";
- }
- return [true, $list];
- }
- //库存设置
- public function productInventorySet($data, $user){
- if(empty($data['id'])) return [false, 'ID不能为空'];
- $set = ProductInventorySet::where('id',$data['id'])->first();
- if(empty($set)) return [false,'系统设置不存在或已被删除'];
- $msg = "";
- if(isset($data['param_one'])){
- if(! isset(ProductInventorySet::$type_name[$data['param_one']])) return [false,'系统设置参数错误'];
- if($set->param_one != $data['param_one']) {
- $set->param_one = $data['param_one'];
- $msg .= "库存校验状态变更为:" . ProductInventorySet::$type_name[$data['param_one']];
- }
- }
- if(isset($data['param_two'])){
- $res = $this->checkNumber($data['param_two']);
- if(! $res) return [false, '整单扣除率请输入不超过两位小数并且大于0的数值'];
- if($set->param_two != $data['param_two']) {
- $set->param_two = $data['param_two'];
- $msg .= ",整单扣除率变更为:" . $data['param_two'];
- }
- }
- if(isset($data['param_three'])){
- $res = $this->checkNumber($data['param_three']);
- if(! $res) return [false, '施工费用比例请输入不超过两位小数并且大于0的数值'];
- if($data['param_three'] < 3) return [false,'施工费用比例不能小于3%'];
- if($set->param_three != $data['param_three']) {
- $set->param_three = $data['param_three'];
- $msg .= ",施工费用比例变更为:" . $data['param_three'];
- }
- }
- if(! empty($msg)){
- $set->save();
- (new OrderOperationService())->add([
- 'order_number' => $data['id'],
- 'msg' => OrderOperation::$type[OrderOperation::zero] . $msg,
- 'type' => OrderOperation::zero
- ],$user);
- }
- return [true, ''];
- }
- //获取每个账号对应的系统设置
- public function getMySetting($data, $user){
- //顶级部门
- $depart = ! empty($user['depart_top'][0]) ? $user['depart_top'][0]: [];
- $depart_id = $depart['depart_id'] ?? 0;
- $model = new ProductInventorySet();
- $model = $model->where('del_time',0)
- ->select('id','top_depart_id','param_one','param_two','param_three')
- ->orderby('id', 'desc');
- if($depart_id) $model->where('top_depart_id',$depart_id);
- $list = $model->get()->toArray();
- $map = Depart::whereIn('id',array_column($list,'top_depart_id'))->pluck('title','id')->toArray();
- foreach ($list as $key => $value){
- $list[$key]['top_depart_title'] = $map[$value['top_depart_id']] ?? "";
- }
- return [true, $list];
- }
- }
|