AllocationService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace app\common\service;
  3. use Carbon\Carbon;
  4. use think\Validate;
  5. use think\facade\Db;
  6. use app\common\model\Io;
  7. use app\common\model\Good;
  8. use app\common\model\Admin;
  9. use app\common\model\IoDetail;
  10. use app\common\model\Allocation;
  11. use app\common\util\WhereBuilder;
  12. use app\common\model\AllocationDetail;
  13. class AllocationService extends Service
  14. {
  15. public function page($params = [])
  16. {
  17. // 自动处理传入的参数
  18. $this->autoParams($params);
  19. // 获取关键词
  20. $keyword = $this->pg('keyword');
  21. // 获取来源仓库ID
  22. $from_repo_id = $this->pg('from_repo_id');
  23. // 获取目标仓库ID
  24. $to_repo_id = $this->pg('to_repo_id');
  25. // 获取开始日期
  26. $begin_date = $this->pg('begin_date');
  27. // 获取结束日期
  28. $end_date = $this->pg('end_date');
  29. // 如果结束日期存在,则将其解析为 Carbon 对象并增加一天,并转换为日期字符串
  30. if ($end_date) {
  31. $end_date = Carbon::parse($end_date)->addDay()->toDateString();
  32. }
  33. // 构建查询条件
  34. $where = WhereBuilder::builder()
  35. ->like('a.id|from.name|to.name|a.remark', $keyword)
  36. ->in('a.from_repo_id', $from_repo_id)
  37. ->in('a.to_repo_id', $to_repo_id)
  38. ->between('a.date', $begin_date, $end_date)
  39. ->build();
  40. // 执行查询,并返回分页结果
  41. return (new Allocation)->alias('a')
  42. ->field('a.*, from.name as from_repo_name, to.name as to_repo_name')
  43. ->join('repo from', 'from.id = a.from_repo_id', 'LEFT')
  44. ->join('repo to', 'to.id = a.to_repo_id', 'LEFT')
  45. ->where($where)
  46. ->order('a.create_time desc')
  47. ->paginate($this->tp6Page());
  48. }
  49. public function info($params = [])
  50. {
  51. $this->autoParams($params);
  52. $alloc = $this->one(Allocation::class);
  53. $alloc->append(['from', 'to', 'details', 'details.good', 'details.good.goodClass']);
  54. return $alloc;
  55. }
  56. /**
  57. * 创建调拨单(事务处理)
  58. *
  59. * @param Admin $admin 管理员对象
  60. * @param array $params 参数数组
  61. * @return Allocation 创建的调拨单对象
  62. */
  63. public function createTrans(Admin $admin, $params = [])
  64. {
  65. // 使用事务处理创建调拨单
  66. return Db::transaction(fn() => $this->create($admin, $params));
  67. }
  68. /**
  69. * 创建调拨单
  70. *
  71. * @param Admin $admin 管理员对象
  72. * @param array $params 参数数组
  73. * @return Allocation 创建的调拨单对象
  74. */
  75. public function create(Admin $admin, $params = [])
  76. {
  77. // 自动处理参数
  78. $params = $this->autoParams($params);
  79. // 验证参数
  80. $this->validate($params, new AllocateValidate);
  81. // 获取调拨明细
  82. $details = $this->pg('details');
  83. // 设置管理员ID
  84. $params['admin_id'] = $admin->id;
  85. // 创建调拨单
  86. $allocation = Allocation::create($params);
  87. // 遍历调拨明细进行赋值和检查
  88. foreach ($details as &$detail) {
  89. // 设置调拨单ID和日期
  90. $detail['allocation_id'] = $allocation->id;
  91. $detail['date'] = isset($detail['date']) && $detail['date'] ? $detail['date'] : $allocation->date;
  92. // 检查出入库数量
  93. if ($detail['num'] == 0) {
  94. $good = Good::find($detail['good_id']);
  95. throw $this->exception("物品--{$good?->name}的出入库数量不能为0");
  96. }
  97. if ($detail['num'] > 0 && $allocation->type == Io::TYPE_OUT) {
  98. $good = Good::find($detail['good_id']);
  99. throw $this->exception("物品--{$good?->name}的出入库数量大于0但是类型为出库");
  100. }
  101. if ($detail['num'] < 0 && $allocation->type == Io::TYPE_IN) {
  102. $good = Good::find($detail['good_id']);
  103. throw $this->exception("物品--{$good?->name}的出入库数量小于0但是类型为入库");
  104. }
  105. }
  106. // 保存调拨明细
  107. $allocationDetails = (new AllocationDetail)->saveAll($details);
  108. // 出库
  109. $outIoData = [
  110. 'repo_id' => $allocation->from_repo_id,
  111. 'type' => Io::TYPE_OUT,
  112. 'change_type' => Io::CHANGE_TYPE_ALLOCATION,
  113. 'date' => $allocation->date,
  114. 'remark' => "自动生成自调拨单{$allocation->id}",
  115. 'sn' => "SYS_ALC_OUT_T" . hrtime(true) . '_SN' . rand(100000, 999999),
  116. 'admin_id' => $admin->id,
  117. 'source' => "调拨单{$allocation->id}"
  118. ];
  119. $outIo = Io::create($outIoData);
  120. // 出库详情
  121. foreach ($details as &$detail) {
  122. $detail['type'] = Io::TYPE_OUT;
  123. $detail['io_id'] = $outIo->id;
  124. $detail['repo_id'] = $allocation->from_repo_id;
  125. $detail['num'] = -$detail['num'];
  126. }
  127. $outDetails = (new IoDetail)->saveAll($details);
  128. // 入库
  129. $inIoData = [
  130. 'repo_id' => $allocation->to_repo_id,
  131. 'type' => Io::TYPE_IN,
  132. 'change_type' => Io::CHANGE_TYPE_ALLOCATION,
  133. 'date' => $allocation->date,
  134. 'remark' => "自动生成自调拨单{$allocation->id}",
  135. 'sn' => "SYS_ALC_IN_T" . hrtime(true) . '_SN' . rand(100000, 999999),
  136. 'admin_id' => $admin->id,
  137. 'source' => "调拨单{$allocation->id}"
  138. ];
  139. $inIo = Io::create($inIoData);
  140. // 入库详情
  141. foreach ($details as &$detail) {
  142. $detail['type'] = Io::TYPE_IN;
  143. $detail['io_id'] = $inIo->id;
  144. $detail['repo_id'] = $allocation->to_repo_id;
  145. $detail['num'] = -$detail['num'];
  146. }
  147. $inDetails = (new IoDetail)->saveAll($details);
  148. $allocation->out_io_id = $outIo->id;
  149. $allocation->in_io_id = $inIo->id;
  150. $allocation->save();
  151. // 返回创建的调拨单对象
  152. return $allocation;
  153. }
  154. public function revertTrans(Admin $admin, $params = [])
  155. {
  156. return Db::transaction(fn() => $this->revert($admin, $params));
  157. }
  158. public function revert(Admin $admin, $params = [])
  159. {
  160. $this->autoParams($params);
  161. $allc = $this->one(Allocation::class);
  162. if ($allc->revert_id) {
  163. return true;
  164. }
  165. $service = (new IoService($this->app))->exceptionClass($this->exceptionClass);
  166. $out_revert_to_in_io = $service->revertTrans($admin, ['id' => $allc->out_io_id]);
  167. $in_revert_to_out_io = $service->revertTrans($admin, ['id' => $allc->in_io_id]);
  168. // 创建调拨单
  169. $revertData = [
  170. 'from_repo_id' => $allc->to_repo_id,
  171. 'to_repo_id' => $allc->from_repo_id,
  172. 'date' => date('Ymd'),
  173. 'remark' => "回滚自$allc->id",
  174. 'admin_id' => $admin->id,
  175. 'out_io_id' => $in_revert_to_out_io->id,
  176. 'in_io_id' => $out_revert_to_in_io->id
  177. ];
  178. $revert = Allocation::create($revertData);
  179. $revertDetails = [];
  180. // 遍历调拨明细进行赋值和检查
  181. foreach ($allc->details as $detail) {
  182. // 设置调拨单ID和日期
  183. $revertDetail = [
  184. 'good_id' => $detail->good_id,
  185. 'num' => $detail->num,
  186. 'date' => date('Ymd'),
  187. 'remark' => '回滚',
  188. 'allocation_id' => $revert->id
  189. ];
  190. $revertDetails[] = $revertDetail;
  191. }
  192. // 保存调拨明细
  193. $allocationDetails = (new AllocationDetail)->saveAll($revertDetails);
  194. $allc->revert_id = $revert->id;
  195. $allc->save();
  196. return $revert;
  197. }
  198. }
  199. class AllocateValidate extends Validate
  200. {
  201. protected $rule = [
  202. 'from_repo_id' => 'require',
  203. 'to_repo_id' => 'require',
  204. 'date' => 'require',
  205. 'remark' => 'max:255',
  206. 'details' => 'require|array'
  207. ];
  208. }