InventoryService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Depart;
  4. use App\Model\Employee;
  5. use App\Model\Inventory;
  6. use App\Model\InventorySub;
  7. use App\Model\OrderInventoryStock;
  8. use App\Model\Product;
  9. use App\Model\Storehouse;
  10. use Illuminate\Support\Facades\DB;
  11. class InventoryService extends Service
  12. {
  13. public function edit($data,$user){
  14. list($status,$msg) = $this->orderRule($data, $user,false);
  15. if(!$status) return [$status,$msg];
  16. try{
  17. DB::beginTransaction();
  18. $inventory_model = Inventory::where('order_number',$data['order_number'])->first();
  19. $inventory_model->counted_id = $data['counted_id'];
  20. $inventory_model->counted_time = $data['counted_time'];
  21. $inventory_model->mark = $data['mark'];
  22. $inventory_model->save();
  23. InventorySub::where('del_time',0)
  24. ->where('order_number',$data['order_number'])
  25. ->update(['del_time'=>time()]);
  26. if(! empty($data['sub'])){
  27. $sub = [];
  28. foreach ($data['sub'] as $value){
  29. $sub[] = [
  30. 'product_id' => $value['product_id'],
  31. 'storehouse_id' => $data['storehouse_id'],
  32. 'order_number' => $data['order_number'],
  33. 'book_num' => $value['book_num'],
  34. 'counted_num' => $value['counted_num'],
  35. 'final_num' => $value['final_num'],
  36. 'price' => $value['price'] ?? 0,
  37. 'inventory_id' => $inventory_model->id,
  38. 'final_amount' => $value['final_amount'] ?? 0,
  39. ];
  40. }
  41. InventorySub::insert($sub);
  42. $is_check_stock = OrderInventoryStock::where('order_number', $data['order_number'])
  43. ->where('del_time',0)
  44. ->value('is_check_stock') ?? 0;
  45. ProductInventoryService::changeLockNumber($user,$msg[0],$msg[1],$is_check_stock);
  46. }
  47. DB::commit();
  48. }catch (\Throwable $e){
  49. DB::rollBack();
  50. return [false,$e->getMessage()];
  51. }
  52. if(! empty($data['check'])) {
  53. list($status,$msg) = (new CheckService())->checkAll([
  54. "id" => $inventory_model->id,
  55. "order_number" => $data['order_number'],
  56. "opt_case" => CheckService::fourteen,
  57. "menu_id" => $data['menu_id']
  58. ],$user);
  59. // if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
  60. }
  61. return [true, ''];
  62. }
  63. public function add($data,$user){
  64. list($status,$msg) = $this->orderRule($data, $user);
  65. if(!$status) return [$status,$msg];
  66. try{
  67. DB::beginTransaction();
  68. $inventory_model = new Inventory();
  69. $inventory_model->order_number = $data['order_number'];
  70. $inventory_model->storehouse_id = $data['storehouse_id'];
  71. $inventory_model->counted_id = $data['counted_id'];
  72. $inventory_model->counted_time = $data['counted_time'];
  73. $inventory_model->mark = $data['mark'] ?? '';
  74. $inventory_model->depart_id = $data['depart_id'] ?? 0;
  75. $inventory_model->top_depart_id = $data['top_depart_id'] ?? 0;
  76. $inventory_model->crt_id = $user['id'];
  77. $inventory_model->save();
  78. if(! empty($data['sub'])){
  79. $sub = [];
  80. foreach ($data['sub'] as $value){
  81. $sub[] = [
  82. 'product_id' => $value['product_id'],
  83. 'storehouse_id' => $data['storehouse_id'],
  84. 'order_number' => $data['order_number'],
  85. 'book_num' => $value['book_num'],
  86. 'counted_num' => $value['counted_num'],
  87. 'final_num' => $value['final_num'],
  88. 'price' => $value['price'] ?? 0,
  89. 'inventory_id' => $inventory_model->id,
  90. 'final_amount' => $value['final_amount'] ?? 0,
  91. ];
  92. }
  93. InventorySub::insert($sub);
  94. ProductInventoryService::changeLockNumber($user,$msg[0],[]);
  95. }
  96. //单据创建时是否校验库存
  97. (new CheckService())->orderInventoryInsert(['order_number' => $data['order_number'], 'is_check_stock' => $data['is_check_stock']]);
  98. DB::commit();
  99. }catch (\Throwable $exception){
  100. DB::rollBack();
  101. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  102. return [false, '网络波动,请重新操作!'];
  103. }
  104. return [false,$exception->getMessage()];
  105. }
  106. if(! empty($data['check'])) {
  107. list($status,$msg) = (new CheckService())->checkAll([
  108. "id" => $inventory_model->id,
  109. "order_number" => $data['order_number'],
  110. "opt_case" => CheckService::fourteen,
  111. "menu_id" => $data['menu_id']
  112. ],$user);
  113. // if(! $status) return [true, '保存成功,施工单确认失败,异常信息:' . $msg];
  114. }
  115. return [true, ''];
  116. }
  117. public function detail($data,$user){
  118. if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
  119. if(! empty($data['id'])){
  120. $inventory = Inventory::where('del_time',0)
  121. ->where('id',$data['id'])
  122. ->first();
  123. }else{
  124. $inventory = Inventory::where('del_time',0)
  125. ->where('order_number',$data['order_number'])
  126. ->first();
  127. $data['id'] = empty($inventory->id) ? 0 : $inventory->id;
  128. }
  129. if(empty($inventory)) return [false,'盘点单不存在或已被删除'];
  130. $inventory = $inventory->toArray();
  131. $inventory['state_title'] = Inventory::$name[$inventory['state']] ?? '';
  132. $inventory['storehouse_title'] = Storehouse::where('id',$inventory['storehouse_id'])->value('title');
  133. $emp_map = Employee::whereIn('id', [$inventory['crt_id'], $inventory['counted_id']])
  134. ->pluck('emp_name','id')
  135. ->toArray();
  136. $inventory['crt_name'] = $emp_map[$inventory['crt_id']] ?? '';
  137. $inventory['counted_name'] = $emp_map[$inventory['counted_id']] ?? '';
  138. $inventory['counted_time'] = $inventory['counted_time'] ? date("Y-m-d", $inventory['counted_time']): '';
  139. $inventory['crt_time'] = $inventory['crt_time'] ? date("Y-m-d H:i:s", $inventory['crt_time']): '';
  140. $sub = InventorySub::where('del_time',0)
  141. ->where('inventory_id', $data['id'])
  142. ->select('product_id','counted_num','final_num','price','book_num','final_amount')
  143. ->get()->toArray();
  144. $map = (new ProductService())->getProductDetail(array_column($sub,'product_id'));
  145. foreach ($sub as $key => $value){
  146. $tmp = $map[$value['product_id']] ?? [];
  147. $sub[$key]['title'] = $tmp['title'] ?? "";
  148. $sub[$key]['code'] = $tmp['code'] ?? "";
  149. $sub[$key]['size'] = $tmp['size'] ?? "";
  150. }
  151. $inventory['sub'] = $sub;
  152. return [true, $inventory];
  153. }
  154. public function del($data,$user){
  155. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  156. $inventory = Inventory::where('del_time',0)->where('id',$data['id'])->first();
  157. if(empty($inventory)) return [false,'盘点单不存在或已被删除'];
  158. $inventory = $inventory->toArray();
  159. if($inventory['state'] > Inventory::STATE_ZERO) return [false,'请确认盘点单状态,操作失败'];
  160. $time = time();
  161. $product_save = $this->getSaveDetail($data['id']);
  162. try {
  163. DB::beginTransaction();
  164. Inventory::where('id',$data['id'])->update([
  165. 'del_time'=> $time
  166. ]);
  167. InventorySub::where('del_time',0)->where('inventory_id',$data['id'])->update([
  168. 'del_time'=> $time
  169. ]);
  170. $is_check_stock = OrderInventoryStock::where('order_number', $inventory['order_number'])
  171. ->where('del_time',0)
  172. ->value('is_check_stock') ?? 0;
  173. //锁定库存释放
  174. if(! empty($product_save)) ProductInventoryService::changeLockNumber($user,[],$product_save,$is_check_stock);
  175. DB::commit();
  176. }catch (\Exception $exception){
  177. DB::rollBack();
  178. return [false, $exception->getMessage()];
  179. }
  180. return [true, ''];
  181. }
  182. public function getList($data,$user){
  183. $model = Inventory::Clear($user,$data);
  184. $model = $model->where('del_time',0)
  185. ->select('order_number','id','storehouse_id','state','counted_id','counted_time','crt_time')
  186. ->orderby('id', 'desc');
  187. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) $model->whereBetween('crt_time',[$data['crt_time'][0],$data['crt_time'][1]]);
  188. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  189. if(! empty($data['storehouse_id'])) $model->where('storehouse_id', $data['storehouse_id']);
  190. if(! empty($data['counted_name'])) {
  191. $id = $this->forSearch($data);
  192. $model->whereIn('counted_id',$id);
  193. }
  194. if(isset($data['state'])) $model->where('state', $data['state']);
  195. if(! empty($data['product_code'])) {
  196. $id = $this->forSearch($data);
  197. $model->whereIn('id',$id);
  198. }
  199. $list = $this->limit($model,'',$data);
  200. $list = $this->fillListData($list);
  201. return [true, $list];
  202. }
  203. public function fillListData($data){
  204. if(empty($data['data'])) return $data;
  205. $storehouse_map = Storehouse::whereIn('id',array_column($data['data'],'storehouse_id'))
  206. ->pluck('title','id')
  207. ->toArray();
  208. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'counted_id'), array_column($data['data'],'crt_id'))))
  209. ->pluck('emp_name','id')
  210. ->toArray();
  211. foreach ($data['data'] as $key => $value){
  212. $data['data'][$key]['counted_time'] = $value['counted_time'] ? date("Y-m-d",$value['counted_time']) : '';
  213. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i:s",$value['crt_time']) : '';
  214. $data['data'][$key]['storehouse_name'] = $storehouse_map[$value['storehouse_id']] ?? '';
  215. $data['data'][$key]['counted_name'] = $emp_map[$value['counted_id']] ?? '';
  216. $data['data'][$key]['state_name'] = Inventory::$name[$value['state']] ?? '';
  217. }
  218. return $data;
  219. }
  220. public function forSearch($data){
  221. if(! empty($data['product_code'])){
  222. $product = Product::where('code', 'LIKE', '%'.$data['product_code'].'%')
  223. ->select('id')
  224. ->get()->toArray();
  225. $inventory_id = InventorySub::where('del_time',0)
  226. ->whereIn('product_id', array_column($product,'id'))
  227. ->select('inventory_id')
  228. ->get()->toArray();
  229. return array_column($inventory_id, 'inventory_id');
  230. }
  231. if(! empty($data['counted_name'])){
  232. $employee = Employee::where('emp_name', 'LIKE', '%'.$data['counted_name'].'%')
  233. ->select('id')
  234. ->get()->toArray();
  235. return array_column($employee, 'id');
  236. }
  237. return [];
  238. }
  239. public function orderRule(&$data, $user, $is_add = true){
  240. if(empty($data['counted_id'])) $data['counted_id'] = $user['id'];
  241. if(empty($data['counted_time'])) return [false,'盘点日期不能为空'];
  242. $data['counted_time'] = $this->changeDateToDate($data['counted_time']);
  243. if(empty($data['storehouse_id'])) return [false,'仓库不能为空'];
  244. if(empty($data['sub'])) return [false, '盘点明细不能为空'];
  245. $product_submit = $product_submit_reduce = $product_id = [];
  246. foreach ($data['sub'] as $value){
  247. if(empty($value['product_id'])) return [false,"盘点单产品ID不为空"];
  248. if(! is_numeric($value['book_num'])) return [false, '账面数量不合法'];
  249. $bool = $this->checkNumber($value['counted_num']);
  250. if(! $bool) return [false, '盘点数量不合法,请输入大于等于0的两位数值'];
  251. if(! is_numeric($value['final_num'])) return [false, '盈亏数量不合法'];
  252. $num = bcsub($value['counted_num'],$value['book_num'],2);
  253. $num = floatval($num);
  254. $final_num = floatval($value['final_num']);
  255. if($num != $final_num) return [false,'盈亏数量计算错误'];
  256. if(isset($value['price'])){
  257. $bool = $this->checkNumber($value['price']);
  258. if(! $bool) return [false, '盘点单价不合法,需是大于等于0的两位数值'];
  259. }
  260. if(isset($value['final_amount'])){
  261. $bool = $this->checkNumber($value['final_amount']);
  262. if(! $bool) return [false, '盈亏金额不合法,需是大于等于0的两位数值'];
  263. }
  264. $key = $value['product_id'] . ',' .$data['storehouse_id'];
  265. if(! isset($product_submit[$key])) $product_submit[$key] = 0;
  266. if($value['final_num'] < 0){
  267. if(isset($product_submit_reduce[$key])){
  268. $product_submit_reduce[$key] += $value['final_num'];
  269. }else{
  270. $product_submit_reduce[$key] = $value['final_num'];
  271. }
  272. $product_id[] = $value['product_id'];
  273. }
  274. }
  275. //所属部门 以及 顶级部门
  276. if(empty($data['depart_id'])) {
  277. $data['depart_id'] = $this->getDepart($user);
  278. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  279. }
  280. $data['is_check_stock'] = $user['is_check_stock'];
  281. if($is_add){
  282. $order_number = (new OrderNoService())->createOrderNumber(Inventory::prefix);
  283. if(empty($order_number)) return [false,'盘点单号生成失败'];
  284. $data['order_number'] = $order_number;
  285. }else{
  286. if(empty($data['order_number'])) return [false,'盘点单号不能为空'];
  287. $model = Inventory::where('order_number',$data['order_number'])->where('del_time',0)->first();
  288. if(empty($model)) return [false, '盘点单不存在或已被删除'];
  289. $order = $model->toArray();
  290. if($order['state'] != Inventory::STATE_ZERO) return [false, '请确认单据状态,编辑失败'];
  291. if($order['storehouse_id'] != $data['storehouse_id']) return [false, '仓库不允许编辑'];
  292. //订单编辑提交限制
  293. $current_top_depart_id = $this->getMyTopDepart($user);
  294. list($status, $msg) = $this->returnOrderEditErrorCommon($current_top_depart_id, $order['top_depart_id']);
  295. if(! $status) return [false, $msg];
  296. }
  297. //校验是否存在产品盘点
  298. list($status, $msg) = $this->issetProduct($data, $product_submit);
  299. if(! $status) return [false, $msg];
  300. $id = $data['id'] ?? 0;
  301. $product_save_reduce = $this->getSaveDetail($id);
  302. if(! empty($product_submit_reduce)){
  303. //校验库存
  304. list($status,$msg) = (new ProductInventoryService())->compareStock($user,$product_id, $product_submit_reduce, $product_save_reduce);
  305. if(! $status) return [false, $msg];
  306. }
  307. return [true, [$product_submit_reduce, $product_save_reduce]];
  308. }
  309. /**
  310. * 获取保存详情
  311. * @param $id
  312. * @return array
  313. */
  314. public function getSaveDetail($id){
  315. $product_save = [];
  316. if(empty($id)) return [];
  317. $sub = InventorySub::where('inventory_id',$id)
  318. ->where('del_time',0)
  319. ->get()->toArray();
  320. foreach ($sub as $value){
  321. $key = $value['product_id'] . ',' . $value['storehouse_id'];
  322. if($value['final_num'] < 0){//盘亏
  323. if(isset($product_save[$key])){
  324. $product_save[$key] += $value['final_num'];
  325. }else{
  326. $product_save[$key] = $value['final_num'];
  327. }
  328. }
  329. }
  330. return $product_save;
  331. }
  332. public function issetProduct($data, $product_submit){
  333. $id = $data['id'] ?? 0;
  334. $inventory = Inventory::where('del_time',0)
  335. ->when(! empty($id), function ($query) use ($id) {
  336. return $query->where('id','<>',$id);
  337. })
  338. ->where('top_depart_id',$data['top_depart_id'])
  339. ->where('state','<',Inventory::STATE_TWO)
  340. ->select('id')
  341. ->get()->toArray();
  342. $sub = InventorySub::where('del_time', 0)
  343. ->whereIn('inventory_id', array_column($inventory,'id'))
  344. ->select('product_id', 'storehouse_id', 'order_number')
  345. ->get()->toArray();
  346. $map = [];
  347. $product = Product::whereIn('id', array_unique(array_column($sub,'product_id')))
  348. ->select('id','title','code')
  349. ->get()->toArray();
  350. foreach ($product as $value){
  351. $map[$value['id']] = $value;
  352. }
  353. foreach ($sub as $value){
  354. $key = $value['product_id'] . ',' .$value['storehouse_id'];
  355. if(isset($product_submit[$key])){
  356. $pro_tmp = $map[$value['product_id']] ?? [];
  357. $pro_str = $pro_tmp['title'] . "( ". $pro_tmp['code'] .")";
  358. return [false, "产品:" . $pro_str . "在盘点单:" . $value['order_number'] . "已录入盘点"];
  359. }
  360. }
  361. return [true, ''];
  362. }
  363. }