OrderValidate.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\businessapi\validate;
  20. use app\common\enum\AfterSaleEnum;
  21. use app\common\enum\DeliveryEnum;
  22. use app\common\enum\OrderEnum;
  23. use app\common\enum\PayEnum;
  24. use app\common\enum\TeamEnum;
  25. use app\common\enum\YesNoEnum;
  26. use app\common\model\AfterSale;
  27. use app\common\model\Express;
  28. use app\common\model\Order;
  29. use app\common\model\OrderGoods;
  30. use app\common\service\ConfigService;
  31. use app\common\validate\BaseValidate;
  32. use think\facade\Validate;
  33. /**
  34. * 订单验证
  35. * Class OrderValidate
  36. * @package app\shopapi\validate
  37. */
  38. class OrderValidate extends BaseValidate
  39. {
  40. protected $rule = [
  41. 'id' => 'require|checkOrder',
  42. 'code' => 'require',
  43. 'province' => 'require',
  44. 'city' => 'require',
  45. 'distric' => 'require',
  46. 'address' => 'require',
  47. 'confirm' => 'require|in:0,1',
  48. 'delivery_address_id' => 'require',
  49. 'return_address_id' => 'require',
  50. 'parcel' => 'requireIf:send_type,1|array',
  51. 'order_goods_ids' => 'require|array'
  52. ];
  53. protected $message = [
  54. 'id.require' => '参数缺失',
  55. 'code.require' => '请输入核销码',
  56. 'province.require' => '所选地区不能为空',
  57. 'city.require' => '请选择完整地址',
  58. 'distric.require' => '请选择完整地址',
  59. 'address.require' => '详细地址不能为空',
  60. 'confirm.require' => '参数缺失',
  61. 'confirm.in' => '参数错误',
  62. 'delivery_address_id.require' => '请选择发货地址',
  63. 'return_address_id.require' => '请选择退货地址',
  64. 'parcel.requireIf' => '请输入包裹信息',
  65. 'parcel.array' => '包裹错误',
  66. 'order_goods_ids.require' => '请选择发货商品',
  67. 'order_goods_ids.array' => '发货商品错误',
  68. ];
  69. public function sceneId()
  70. {
  71. return $this->only(['id']);
  72. }
  73. public function sceneCancel()
  74. {
  75. return $this->only(['id'])
  76. ->remove('id','checkOrder')
  77. ->append('id','checkCancel');
  78. }
  79. public function sceneOrderTraces()
  80. {
  81. return $this->only(['id'])->append('id','checkTraces');
  82. }
  83. public function sceneCode()
  84. {
  85. return $this->only(['code']);
  86. }
  87. public function sceneConfirm()
  88. {
  89. return $this->only(['id'])
  90. ->append('id','checkConfirm');
  91. }
  92. public function sceneVerification()
  93. {
  94. return $this->only(['id','confirm'])
  95. ->append('id','checkVerification');
  96. }
  97. public function sceneDelivery()
  98. {
  99. return $this->only(['id'])
  100. ->append('id','checkDelivery');
  101. }
  102. public function sceneAddressEdit()
  103. {
  104. return $this->only(['id','province','city','district','address'])
  105. ->append('id','checkAddressEdit');
  106. }
  107. //验证订单
  108. public function checkOrder($value, $rule, $data)
  109. {
  110. $order = (new Order())->getOrderById($value);
  111. if ($order->isEmpty()) {
  112. return '订单不存在';
  113. }
  114. return true;
  115. }
  116. /**
  117. * @notes 检查订单是否可以发货
  118. * @param $value
  119. * @param $rule
  120. * @param $data
  121. * @return bool|string
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\DbException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. * @author ljj
  126. * @date 2021/8/10 5:59 下午
  127. */
  128. public function checkDelivery($value,$rule,$data)
  129. {
  130. $order = Order::where('id', $value)->findOrEmpty();
  131. if(OrderEnum::VIRTUAL_ORDER == $order['order_type']){
  132. if (empty($data['delivery_content']) && empty($data['delivery_content1'])) {
  133. return '发货内容不能为空';
  134. }
  135. if (! in_array($data['delivery_content_type'] ?? -111, [ 0, 1 ])) {
  136. return '发货类型不能为空';
  137. }
  138. return true;
  139. }
  140. $validate = Validate::rule([
  141. 'send_type|配送方式' => 'require|in:1,2',
  142. 'delivery_address_id|发货地址' => 'require',
  143. 'return_address_id|退货地址' => 'require',
  144. 'parcel|包裹' => 'requireIf:send_type,1|array',
  145. 'order_goods_ids|发货商品' => 'require|array'
  146. ]);
  147. if (!$validate->check($data)) {
  148. return $validate->getError();
  149. }
  150. if ($order['order_type'] == OrderEnum::TEAM_ORDER && $order['is_team_success'] != TeamEnum::TEAM_FOUND_SUCCESS) {
  151. return '该订单正在拼团中还不能发货';
  152. }
  153. if ($order['order_status'] == OrderEnum::STATUS_CLOSE) {
  154. return '订单已关闭';
  155. }
  156. if ($order['order_status'] != OrderEnum::STATUS_WAIT_DELIVERY || !in_array($order['express_status'],[DeliveryEnum::NOT_SHIPPED,DeliveryEnum::PART_SHIPPED])) {
  157. return '订单不允许发货';
  158. }
  159. if(!isset($data['send_type']) || !in_array($data['send_type'],[1,2])){
  160. return '发货类型错误';
  161. }
  162. if(1 == $data['send_type']){
  163. // if((!isset($data['invoice_no']) || empty($data['invoice_no']))) {
  164. // return '请输入单号';
  165. // }
  166. // if(!isset($data['express_id']) || empty($data['express_id'])){
  167. // return '请选择物流公司';
  168. // }
  169. // $this->checkExpressId($data['express_id'],[],[]);
  170. $invoiceNoArr = [];
  171. foreach ($data['parcel'] as $parcel) {
  172. if(!isset($parcel['invoice_no']) || empty($parcel['invoice_no']) || !isset($parcel['express_id']) || empty($parcel['express_id'])){
  173. return '包裹运单信息不完整,请检查后再发货';
  174. }
  175. if(!isset($parcel['order_goods_info']) || empty($parcel['order_goods_info']) || !is_array($parcel['order_goods_info'])){
  176. return '包裹商品信息错误,请检查后再发货';
  177. }
  178. foreach ($parcel['order_goods_info'] as $info) {
  179. if(!isset($info['order_goods_id']) || empty($info['order_goods_id']) || !isset($info['delivery_num']) || empty($info['delivery_num'])){
  180. return '包裹商品信息缺失,请检查后再发货';
  181. }
  182. }
  183. if (in_array($parcel['invoice_no'], $invoiceNoArr)) {
  184. return '多个包裹运单号重复';
  185. }
  186. $invoiceNoArr[] = $parcel['invoice_no'];
  187. $order_goods_ids = array_column($parcel['order_goods_info'],'order_goods_id');
  188. $delivery_num_arr = array_column($parcel['order_goods_info'],'delivery_num','order_goods_id');
  189. $order_goods = OrderGoods::where(['id'=>$order_goods_ids])->select()->toArray();
  190. foreach ($order_goods as $goods) {
  191. if ($goods['express_status'] == DeliveryEnum::SHIPPED) {
  192. return '存在已发货商品,无法发货';
  193. }
  194. if ($goods['goods_num'] - $goods['delivery_num'] < $delivery_num_arr[$goods['id']]) {
  195. return '超出商品剩余数量,无法发货';
  196. }
  197. }
  198. $after_sale = AfterSale::where(['order_goods_id' => $order_goods_ids, 'order_id' => $data['id']])->findOrEmpty();
  199. if (!$after_sale->isEmpty() && in_array($after_sale->status,[AfterSaleEnum::STATUS_ING,AfterSaleEnum::STATUS_SUCCESS])) {
  200. return '存在售后商品,无法发货';
  201. }
  202. }
  203. } else {
  204. $order_goods = OrderGoods::where(['id'=>$data['order_goods_ids']])->select()->toArray();
  205. foreach ($order_goods as $goods) {
  206. if ($goods['express_status'] == DeliveryEnum::SHIPPED) {
  207. return '存在已发货商品,无法发货';
  208. }
  209. }
  210. $after_sale = AfterSale::where(['order_goods_id' => $data['order_goods_ids'] ?? 0, 'order_id' => $data['id']])->findOrEmpty();
  211. if (!$after_sale->isEmpty() && in_array($after_sale->status,[AfterSaleEnum::STATUS_ING,AfterSaleEnum::STATUS_SUCCESS])) {
  212. return '存在售后商品,无法发货';
  213. }
  214. }
  215. // $order_goods = OrderGoods::where(['order_id'=>$order['id']])->select()->toArray();
  216. // foreach ($order_goods as $goods) {
  217. // $after_sale = AfterSale::where(['order_goods_id' => $goods['id'], 'order_id' => $goods['order_id']])->findOrEmpty();
  218. // if (!$after_sale->isEmpty() && $after_sale->status == AfterSaleEnum::STATUS_ING) {
  219. // return '订单商品:'.$goods['goods_name'].' 处于售后中,无法发货';
  220. // }
  221. // }
  222. return true;
  223. }
  224. /**
  225. * @notes 检查订单是否可以修改地址
  226. * @param $value
  227. * @param $rule
  228. * @param $data
  229. * @return bool|string
  230. * @author ljj
  231. * @date 2021/8/10 11:37 上午
  232. */
  233. public function checkAddressEdit($value,$rule,$data)
  234. {
  235. $order = Order::where('id', $value)->findOrEmpty();
  236. if ($order['express_status'] == 1) {
  237. return '订单已发货,不可以修改地址';
  238. }
  239. return true;
  240. }
  241. /**
  242. * @notes 检查物流公司是否存在
  243. * @param $value
  244. * @param $rule
  245. * @param $data
  246. * @return bool|string
  247. * @author ljj
  248. * @date 2021/8/10 5:29 下午
  249. */
  250. public function checkExpressId($value,$rule,$data)
  251. {
  252. $order = Express::where('id', $value)->findOrEmpty();
  253. if ($order->isEmpty()) {
  254. return '物流公司不存在';
  255. }
  256. return true;
  257. }
  258. /**
  259. * @notes 检查订单是否已发货
  260. * @param $value
  261. * @param $rule
  262. * @param $data
  263. * @return bool|string
  264. * @author ljj
  265. * @date 2021/8/13 11:20 上午
  266. */
  267. public function checkLogistics($value,$rule,$data)
  268. {
  269. $order = Order::where('id', $value)->findOrEmpty();
  270. if ($order['express_status'] == DeliveryEnum::NOT_SHIPPED) {
  271. return '订单未发货,暂无物流信息';
  272. }
  273. return true;
  274. }
  275. /**
  276. * @notes 验证订单能否取消
  277. * @param $value
  278. * @param $rule
  279. * @param $data
  280. * @return bool|string
  281. * @author 段誉
  282. * @date 2021/8/2 15:48
  283. */
  284. public function checkCancel($value, $rule, $data)
  285. {
  286. $order = Order::where('id', $value)->findOrEmpty();
  287. if ($order->isEmpty()) {
  288. return '订单不存在';
  289. }
  290. if ($order['order_status'] != OrderEnum::STATUS_WAIT_PAY && $order['order_status'] != OrderEnum::STATUS_WAIT_DELIVERY && ($order['order_status'] == OrderEnum::STATUS_WAIT_RECEIVE && $order['delivery_type'] != DeliveryEnum::SELF_DELIVERY)) {
  291. return '订单不允许取消';
  292. }
  293. return true;
  294. }
  295. /**
  296. * @notes 验证能否确认收货
  297. * @param $value
  298. * @param $rule
  299. * @param $data
  300. * @return bool|string
  301. * @author 段誉
  302. * @date 2021/8/2 15:26
  303. */
  304. public function checkConfirm($value, $rule, $data)
  305. {
  306. $order = (new Order())->getOrderById($value);
  307. if ($order->isEmpty()) {
  308. return '订单不存在';
  309. }
  310. if ($order['order_status'] < OrderEnum::STATUS_WAIT_RECEIVE) {
  311. return '订单未发货';
  312. }
  313. if ($order['order_status'] == OrderEnum::STATUS_FINISH) {
  314. return '订单已完成';
  315. }
  316. return true;
  317. }
  318. /**
  319. * @notes 检查订单是否有物流信息
  320. * @param $value
  321. * @param $rule
  322. * @param $data
  323. * @return bool|string
  324. * @author ljj
  325. * @date 2021/8/13 3:56 下午
  326. */
  327. public function checkTraces($value, $rule, $data)
  328. {
  329. $order = (new Order())->getOrderById($value);
  330. if ($order->isEmpty()) {
  331. return '订单不存在';
  332. }
  333. if ($order['express_status'] == DeliveryEnum::NOT_SHIPPED) {
  334. return '订单未发货,暂无物流信息';
  335. }
  336. return true;
  337. }
  338. /**
  339. * @notes 检查订单是否可以删除
  340. * @param $value
  341. * @param $rule
  342. * @param $data
  343. * @return bool|string
  344. * @throws \think\db\exception\DataNotFoundException
  345. * @throws \think\db\exception\DbException
  346. * @throws \think\db\exception\ModelNotFoundException
  347. * @author ljj
  348. * @date 2021/8/31 2:36 下午
  349. */
  350. public function checkDel($value, $rule, $data)
  351. {
  352. $order = Order::where('id',$value)->find()->toArray();
  353. if ($order['order_status'] != OrderEnum::STATUS_CLOSE) {
  354. return '订单无法删除';
  355. }
  356. //如果在售后中无法删除
  357. if(AfterSale::where(['order_id'=>$value,'status'=>AfterSaleEnum::STATUS_ING])->find()){
  358. return '订单正在售后中,无法删除';
  359. }
  360. return true;
  361. }
  362. /***
  363. * @notes 核销订单
  364. * @param $value
  365. * @param $rule
  366. * @param $data
  367. * @return bool|string
  368. * @author cjhao
  369. * @date 2023/2/17 11:55
  370. */
  371. public function checkVerification($value,$rule,$data)
  372. {
  373. $result = Order::where('id', $value)->findOrEmpty();
  374. if ($result['pay_status'] != PayEnum::ISPAID) {
  375. return '订单未支付,不允许核销';
  376. }
  377. if ($result['order_status'] == OrderEnum::STATUS_CLOSE) {
  378. return '订单已关闭';
  379. }
  380. if ($result['delivery_type'] != DeliveryEnum::SELF_DELIVERY) {
  381. return '非自提订单,不允许核销';
  382. }
  383. if ($result['verification_status'] == OrderEnum::WRITTEN_OFF) {
  384. return '订单已核销';
  385. }
  386. if ($result['order_type'] == OrderEnum::TEAM_ORDER){
  387. if ($result['is_team_success'] != TeamEnum::TEAM_FOUND_SUCCESS){
  388. return '拼团成功后才能核销';
  389. }
  390. }
  391. $order_goods = OrderGoods::where(['order_id'=>$result['id']])->select()->toArray();
  392. foreach ($order_goods as $goods) {
  393. $after_sale = AfterSale::where(['order_goods_id' => $goods['id'], 'order_id' => $goods['order_id']])->findOrEmpty();
  394. if (!$after_sale->isEmpty() && $after_sale->status == AfterSaleEnum::STATUS_ING) {
  395. return '订单商品:'.$goods['goods_name'].' 处于售后中,无法发货';
  396. }
  397. }
  398. return true;
  399. }
  400. }