ProductInventoryService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Construction;
  5. use App\Model\ConstructionProductInfo;
  6. use App\Model\Depart;
  7. use App\Model\InOutRecord;
  8. use App\Model\Inventory;
  9. use App\Model\InvoiceOrder;
  10. use App\Model\InvoiceOrderInfo;
  11. use App\Model\MonthlyPerFormance;
  12. use App\Model\OrderOperation;
  13. use App\Model\OutBoundOrder;
  14. use App\Model\Product;
  15. use App\Model\ProductAdjustment;
  16. use App\Model\ProductCategory;
  17. use App\Model\ProductInventory;
  18. use App\Model\ProductInventorySet;
  19. use App\Model\PurchaseOrder;
  20. use App\Model\PurchaseOrderInfoForOutBound;
  21. use App\Model\ReturnExchangeOrder;
  22. use App\Model\SalesOrder;
  23. use App\Model\Setting;
  24. use App\Model\Storehouse;
  25. use Illuminate\Support\Facades\DB;
  26. class ProductInventoryService extends Service
  27. {
  28. //获取产品真实库存
  29. public static function getRealStock($product_id = [], $storehouse_id = 0){
  30. if(empty($product_id) || empty($storehouse_id)) return [];
  31. $array = ProductInventory::whereIn('product_id',$product_id)
  32. ->where('storehouse_id', $storehouse_id)
  33. ->get()->toArray();
  34. $return = [];
  35. if(! empty($array)){
  36. foreach ($array as $value){
  37. $num = bcsub($value['number'], $value['lock_number'],2);
  38. $return[] = [
  39. 'id' => $value['id'],
  40. 'product_id' => $value['product_id'],
  41. 'storehouse_id' => $value['storehouse_id'],
  42. 'number' => $value['number'],
  43. 'lock_number' => $value['lock_number'],
  44. 'real_number' => $num,
  45. ];
  46. }
  47. }
  48. return $return;
  49. }
  50. //暂时关闭 没用
  51. public static function is_check(&$user,$data){
  52. return;
  53. //是否校验库存
  54. $top_depart_id = $user['head']['id'] ?? 0;//总公司id
  55. if($data['top_depart_id'] == $top_depart_id) {
  56. $user['is_check_stock'] = false;
  57. }else{
  58. $set = ProductInventorySet::where('top_depart_id',$data['top_depart_id'])
  59. ->where('del_time',0)
  60. ->first();
  61. if(! empty($set) && ! $set->param_one) $user['is_check_stock'] = false;
  62. }
  63. }
  64. //更新锁定库存
  65. //$user 当前登录人的信息
  66. //$submit_total (提交的数据) $save_total(保存过的数据) todo
  67. public static function changeLockNumber($user, $submit_total = [], $save_total = [], $is_check_stock = 0){
  68. //如果外部传来则按照外部
  69. if($is_check_stock){
  70. if($is_check_stock == ProductInventorySet::type_two) return;
  71. }else{
  72. //外部没有传递 则根据当前用户
  73. if($user['is_check_stock'] == ProductInventorySet::type_two) return; //不校验库存
  74. }
  75. if(empty($submit_total) && empty($save_total)) return;
  76. //产品数扣减
  77. if(! empty($save_total)){
  78. foreach ($save_total as $key => $value){
  79. if(! isset($submit_total[$key])){
  80. $submit_total[$key] = - $value;
  81. }else{
  82. $submit_total[$key] = $submit_total[$key] - $value;
  83. }
  84. }
  85. }
  86. //更新锁定库存
  87. foreach ($submit_total as $key => $value){
  88. $tmp = explode(',',$key);
  89. ProductInventory::where('product_id',$tmp[0])
  90. ->where('storehouse_id',$tmp[1])
  91. ->update(['lock_number' => DB::raw('lock_number + ('. $value . ')')]);
  92. }
  93. }
  94. //比较库存
  95. public static function compareStock($user, $product_id = [],$product_submit = [], $product_save = []){
  96. if($user['is_check_stock'] == ProductInventorySet::type_two) return [true,'']; //不校验库存
  97. if(empty($product_id) || empty($product_submit)) return [false,'比较参数不能为空'];
  98. //库存
  99. $array = ProductInventory::whereIn('product_id', $product_id)
  100. ->select('id','product_id','number','crt_time','lock_number','storehouse_id')
  101. ->get()->toArray();
  102. if(empty($array)) return [false,'产品库存不存在'];
  103. //产品库存字典
  104. $stock = [];
  105. foreach ($array as $value){
  106. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  107. $stock[$key] = $value;
  108. }
  109. //仓库
  110. $store_map = Storehouse::whereIn('id',array_unique(array_column($array,'storehouse_id')))
  111. ->pluck('title','id')
  112. ->toArray();
  113. //产品字典
  114. $map = [];
  115. $pro = Product::whereIn('id',$product_id)
  116. ->select('title','id','code')
  117. ->get()->toArray();
  118. foreach ($pro as $value){
  119. $map[$value['id']] = $value;
  120. }
  121. foreach ($product_submit as $key => $value){
  122. $tmp = explode(',',$key);
  123. //产品ID
  124. $product_id = $tmp[0];
  125. //仓库id
  126. $storehouse_id = $tmp[1];
  127. $storehouse_title = $store_map[$storehouse_id] ?? "";
  128. //产品名称
  129. $pro_tmp = $map[$product_id] ?? [];
  130. if(empty($pro_tmp)) return [false,'异常产品数据'];
  131. $pro_title = $pro_tmp['title'] . "( ". $pro_tmp['code'] .")";
  132. if(! isset($stock[$key])) return [false,'当前仓库' . $storehouse_title .' 的产品:'. $pro_title .'暂无库存'];
  133. //产品库存
  134. $stock_product = $stock[$key];
  135. //校验 =》 锁定库存
  136. $save_data = $product_save[$key] ?? 0;//已保存数量
  137. $tmp_lock = ($stock_product['lock_number'] > 0 ? $stock_product['lock_number'] : 0) - $save_data;
  138. $number = $stock_product['number'] - ($tmp_lock > 0 ? $tmp_lock : 0);
  139. if($value > $number) return [false, $pro_title. '数量不足,当前仓库:'. $storehouse_title . '的有效库存数量:' . $number];
  140. }
  141. return [true,''];
  142. }
  143. //现存量
  144. public function productInventoryListCommon($data,$user, $field = []){
  145. if(empty($field)){
  146. $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'];
  147. }
  148. $model = ProductInventory::ATopClear($user,$data);
  149. $model = $model->from('product_inventory as a')
  150. ->join('product as b','b.id','a.product_id')
  151. ->select($field)
  152. ->orderby('a.crt_time', 'desc');
  153. if(! empty($data['product_name'])){
  154. $product_name = $data['product_name'];
  155. $id = Product::where('del_time',0)
  156. ->when(! empty($product_name), function ($query) use ($product_name) {
  157. return $query->where('title', 'LIKE', '%'.$product_name.'%');
  158. })
  159. ->select('id')
  160. ->get()->toArray();
  161. $model->whereIn('a.product_id',array_unique(array_column($id,'id')));
  162. }
  163. if(! empty($data['product_category_name'])){
  164. $product_category_name = $data['product_category_name'];
  165. $c = ProductCategory::where('del_time',0)
  166. ->where('title', 'LIKE', '%'.$product_category_name.'%')
  167. ->select('id')
  168. ->get()->toArray();
  169. $model->whereIn('b.product_category_id',array_unique(array_column($c,'id')));
  170. }
  171. if(!empty($data['code'])) $model->where('b.code', 'LIKE', '%'.$data['code'].'%');
  172. if(isset($data['is_stock'])){
  173. if($data['is_stock'] == 0){
  174. $model->where('a.number','>',0);
  175. }
  176. // else{
  177. // $model->where('a.number','<=',0);
  178. // }
  179. }else{
  180. $model->where('a.number','>',0);
  181. }
  182. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  183. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  184. $model->whereBetween('a.crt_time',[$return[0],$return[1]]);
  185. }
  186. return $model;
  187. }
  188. //现存量
  189. public function productInventoryList($data,$user){
  190. $model = $this->productInventoryListCommon($data, $user);
  191. $list = $this->limit($model,'',$data);
  192. $list = $this->fillListData($list);
  193. return [true, $list];
  194. }
  195. public function fillListData($data){
  196. if(empty($data['data'])) return $data;
  197. $category = ProductCategory::whereIn('id',array_column($data['data'],'product_category_id'))
  198. ->pluck('title','id')
  199. ->toArray();
  200. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  201. ->pluck('title','id')
  202. ->toArray();
  203. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data['data'],'storehouse_id')))
  204. ->pluck('title','id')
  205. ->toArray();
  206. $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'top_depart_id')))->pluck('title','id')->toArray();
  207. foreach ($data['data'] as $key => $value){
  208. $data['data'][$key]['unit_title'] = $basic_map[$value['unit']] ?? '';
  209. $data['data'][$key]['product_category_title'] = $category[$value['product_category_id']] ?? '';
  210. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']):'';
  211. $data['data'][$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  212. $data['data'][$key]['top_depart_title'] = $depart_map[$value['top_depart_id']] ?? '';
  213. }
  214. return $data;
  215. }
  216. //库存台账
  217. public function productInventoryStockListCommon($data,$user, $field = []){
  218. if(empty($field)){
  219. $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'];
  220. }
  221. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  222. $model = InOutRecord::ATopClear($user,$data);
  223. $model = $model->from('in_out_record as a')
  224. ->where('a.del_time',0)
  225. ->join('product as b','b.id','a.product_id')
  226. ->select($field)
  227. ->groupby('a.product_id','a.top_depart_id','a.order_number')
  228. ->orderBy('a.crt_time','asc');
  229. $model->whereBetween('a.crt_time',[$return[0],$return[1]]);
  230. if(! empty($data['product_name'])){
  231. $product_name = $data['product_name'];
  232. $id = Product::where('del_time',0)
  233. ->when(! empty($product_name), function ($query) use ($product_name) {
  234. return $query->where('title', 'LIKE', '%'.$product_name.'%');
  235. })
  236. ->select('id')
  237. ->get()->toArray();
  238. $model->whereIn('a.product_id',array_unique(array_column($id,'id')));
  239. }
  240. if(! empty($data['product_category_name'])){
  241. $product_category_name = $data['product_category_name'];
  242. $c = ProductCategory::where('del_time',0)
  243. ->where('title', 'LIKE', '%'.$product_category_name.'%')
  244. ->select('id')
  245. ->get()->toArray();
  246. $model->whereIn('b.product_category_id',array_unique(array_column($c,'id')));
  247. }
  248. if(! empty($data['code'])) $model->where('b.code', 'LIKE', '%'.$data['code'].'%');
  249. if(! empty($data['order_number'])) $model->where('a.order_number', 'LIKE', '%'.$data['order_number'].'%');
  250. return $model;
  251. }
  252. //库存台账
  253. public function productInventoryStockList($data,$user){
  254. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false,'时间必须选择'];
  255. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  256. $day = $this->returnDays($return);
  257. if($day > 31) return [false, '库存台账查询时间仅支持范围区间在一月(31天)内'];
  258. $model = $this->productInventoryStockListCommon($data, $user);
  259. $list = $model->get()->toArray();
  260. $list = $this->fillData($list, $return);
  261. return [true, $list];
  262. }
  263. public function fillData($data,$time_arr){
  264. if (empty($data)) return $data;
  265. $start_data = InOutRecord::whereIn('product_id',array_unique(array_column($data,'product_id')))
  266. ->where('crt_time','<',$time_arr[0])
  267. ->where('del_time',0)
  268. ->select('product_id','number',DB::raw('sum(number) as number'),'top_depart_id')
  269. ->groupby('product_id','top_depart_id')
  270. ->get()->toArray();
  271. $start_map = [];
  272. foreach ($start_data as $value){
  273. $start_map[$value['product_id'].$value['top_depart_id']] = $value['number'];
  274. }
  275. $category = ProductCategory::whereIn('id',array_unique(array_column($data,'product_category_id')))
  276. ->pluck('title','id')
  277. ->toArray();
  278. $basic_map = BasicType::whereIn('id',array_unique(array_column($data,'unit')))
  279. ->pluck('title','id')
  280. ->toArray();
  281. $roll_tmp = [];
  282. $order_type = $this->getOrderType();
  283. $storehouse = Storehouse::whereIn('id',array_unique(array_column($data,'storehouse_id')))
  284. ->pluck('title','id')
  285. ->toArray();
  286. foreach ($data as $key => $value){
  287. $keys = $value['product_id'] . $value['top_depart_id'];
  288. if(! isset($roll_tmp[$keys])){
  289. if(isset($start_map[$keys])){
  290. $data[$key]['start_number'] = $start_map[$keys];
  291. }else{
  292. $data[$key]['start_number'] = 0;
  293. }
  294. $tmp = $data[$key]['start_number'] + $value['in_number'] + $value['out_number'];
  295. $data[$key]['end_number'] = round($tmp,2);
  296. $roll_tmp[$keys] = $data[$key]['end_number'];
  297. }else{
  298. $data[$key]['start_number'] = $roll_tmp[$keys];
  299. $tmp = $data[$key]['start_number'] + $value['in_number'] + $value['out_number'];
  300. $data[$key]['end_number'] = round($tmp,2);
  301. $roll_tmp[$keys] = $data[$key]['end_number'];
  302. }
  303. $data[$key]['out_number'] = abs($value['out_number']);
  304. $data[$key]['product_category_title'] = $category[$value['product_category_id']] ?? '';
  305. $data[$key]['order_name'] = $order_type[$value['order_type']] ?? '';
  306. $data[$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d",$value['crt_time']):'';
  307. $data[$key]['unit_title'] = $basic_map[$value['unit']] ?? '';
  308. $data[$key]['storehouse_title'] = $storehouse[$value['storehouse_id']] ?? '';
  309. }
  310. return $data;
  311. }
  312. public function getOrderType(){
  313. $array = [];
  314. foreach (Construction::$prefix as $value){
  315. $array[$value] = '施工单';
  316. }
  317. foreach (ReturnExchangeOrder::$prefix as $key => $value){
  318. $array[$value] = ReturnExchangeOrder::$model_type_name[$key] ?? '';
  319. }
  320. foreach (SalesOrder::$prefix as $value){
  321. $array[$value] = '合同';
  322. }
  323. $array[InvoiceOrder::prefix] = '发货单';
  324. $array[PurchaseOrder::prefix] = '采购单';
  325. $array[Inventory::prefix] = '盘点单';
  326. $array[OutBoundOrder::prefix] = '出库单';
  327. $array[PurchaseOrderInfoForOutBound::prefix] = '平出库流水记录';
  328. $array[PurchaseOrderInfoForOutBound::prefix2] = '平库存流水记录';
  329. $array[ProductAdjustment::prefix] = '产品金额调整单';
  330. return $array;
  331. }
  332. //系统设置列表
  333. public function productInventorySetList($data, $user){
  334. $model = ProductInventorySet::TopClear($user,$data);
  335. $model = $model->where('del_time',0)
  336. ->select('id','top_depart_id','param_one','param_two','param_three')
  337. ->orderby('id', 'desc');
  338. $list = $model->get()->toArray();
  339. $map = Depart::whereIn('id',array_column($list,'top_depart_id'))->pluck('title','id')->toArray();
  340. foreach ($list as $key => $value){
  341. $list[$key]['top_depart_title'] = $map[$value['top_depart_id']] ?? "";
  342. }
  343. return [true, $list];
  344. }
  345. //库存设置
  346. public function productInventorySet($data, $user){
  347. if(empty($data['id'])) return [false, 'ID不能为空'];
  348. $set = ProductInventorySet::where('id',$data['id'])->first();
  349. if(empty($set)) return [false,'系统设置不存在或已被删除'];
  350. $msg = "";
  351. if(isset($data['param_one'])){
  352. if(! isset(ProductInventorySet::$type_name[$data['param_one']])) return [false,'系统设置参数错误'];
  353. if($set->param_one != $data['param_one']) {
  354. $set->param_one = $data['param_one'];
  355. $msg .= "库存校验状态变更为:" . ProductInventorySet::$type_name[$data['param_one']];
  356. }
  357. }
  358. if(isset($data['param_two'])){
  359. $res = $this->checkNumber($data['param_two']);
  360. if(! $res) return [false, '整单扣除率请输入不超过两位小数并且大于0的数值'];
  361. if($set->param_two != $data['param_two']) {
  362. $set->param_two = $data['param_two'];
  363. $msg .= ",整单扣除率变更为:" . $data['param_two'];
  364. }
  365. }
  366. if(isset($data['param_three'])){
  367. $res = $this->checkNumber($data['param_three']);
  368. if(! $res) return [false, '施工费用比例请输入不超过两位小数并且大于0的数值'];
  369. if($data['param_three'] < 3) return [false,'施工费用比例不能小于3%'];
  370. if($set->param_three != $data['param_three']) {
  371. $set->param_three = $data['param_three'];
  372. $msg .= ",施工费用比例变更为:" . $data['param_three'];
  373. }
  374. }
  375. if(! empty($msg)){
  376. $set->save();
  377. (new OrderOperationService())->add([
  378. 'order_number' => $data['id'],
  379. 'msg' => OrderOperation::$type[OrderOperation::zero] . $msg,
  380. 'type' => OrderOperation::zero
  381. ],$user);
  382. }
  383. return [true, ''];
  384. }
  385. //获取每个账号对应的系统设置
  386. public function getMySetting($data, $user){
  387. //顶级部门
  388. $depart = ! empty($user['depart_top'][0]) ? $user['depart_top'][0]: [];
  389. $depart_id = $depart['depart_id'] ?? 0;
  390. $model = new ProductInventorySet();
  391. $model = $model->where('del_time',0)
  392. ->select('id','top_depart_id','param_one','param_two','param_three')
  393. ->orderby('id', 'desc');
  394. if($depart_id) $model->where('top_depart_id',$depart_id);
  395. $list = $model->get()->toArray();
  396. $map = Depart::whereIn('id',array_column($list,'top_depart_id'))->pluck('title','id')->toArray();
  397. foreach ($list as $key => $value){
  398. $list[$key]['top_depart_title'] = $map[$value['top_depart_id']] ?? "";
  399. }
  400. return [true, $list];
  401. }
  402. public function manSetting($data, $user){
  403. if(empty($data['ym'])) return [false, '业绩目标年月不能为空'];
  404. $start_time = strtotime(date("Y-m-01 00:00:00",$data['ym']));
  405. $end_time = strtotime(date("Y-m-t 23:59:59",$data['ym']));
  406. if(empty($data['data'])) return [false, '人员业绩不能为空'];
  407. $insert = [];
  408. foreach ($data['data'] as $value){
  409. if(empty($value['employee_id'])) return [false, '人员Id不能为空'];
  410. if(! isset($value['sales_index'])) return [false, '人员业绩不能为空'];
  411. $insert[] = [
  412. 'employee_id' => $value['employee_id'],
  413. 'sales_index' => $value['sales_index'],
  414. 'crt_time' => $data['ym'],
  415. ];
  416. }
  417. $employee_id = array_column($data['data'],'employee_id');
  418. try {
  419. $time = time();
  420. MonthlyPerFormance::where('del_time',0)
  421. ->where('crt_time',">=",$start_time)
  422. ->where('crt_time','<=',$end_time)
  423. ->whereIn('employee_id',$employee_id)
  424. ->update(['del_time' => $time]);
  425. if(! empty($insert)) MonthlyPerFormance::insert($insert);
  426. }catch (\Throwable $exception){
  427. DB::rollBack();
  428. return [false, $exception->getMessage()];
  429. }
  430. return [true, ''];
  431. }
  432. }