ProductInventoryService.php 19 KB

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