OrderValidate.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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\adminapi\validate\order;
  20. use app\common\enum\AfterSaleEnum;
  21. use app\common\enum\DeliveryEnum;
  22. use app\common\enum\OrderEnum;
  23. use app\common\enum\TeamEnum;
  24. use app\common\enum\YesNoEnum;
  25. use app\common\model\AfterSale;
  26. use app\common\model\Express;
  27. use app\common\model\Order;
  28. use app\common\model\OrderGoods;
  29. use app\common\model\Printer;
  30. use app\common\validate\BaseValidate;
  31. use think\facade\Validate;
  32. class OrderValidate extends BaseValidate
  33. {
  34. protected $rule = [
  35. 'id' => 'require|checkId',
  36. 'contact' => 'require',
  37. 'mobile' => 'require|mobile',
  38. 'province_id' => 'require',
  39. 'city_id' => 'require',
  40. 'district_id' => 'require',
  41. 'address' => 'require',
  42. 'order_goods_id' => 'require|checkOrderGoodsId',
  43. 'change_price' => 'require|float',
  44. 'express_price' => 'require|float',
  45. 'send_type' => 'require|in:1,2',
  46. 'express_id' => 'requireIf:send_type,1|checkExpressId',
  47. 'invoice_no' => 'requireIf:send_type,1|alphaNum',
  48. ];
  49. protected $field = [
  50. 'change_price' => '修改价格',
  51. ];
  52. protected $message = [
  53. 'contact.require' => '请输入收货人',
  54. 'mobile.require' => '请输入收货电话',
  55. 'mobile.mobile' => '收货电话错误',
  56. 'province_id.require' => '所选地区不能为空',
  57. 'city_id.require' => '请选择完整地址',
  58. 'district_id.require' => '请选择完整地址',
  59. 'change_price.gt' => '修改的价格不能为负数',
  60. 'address.require' => '详细地址不能为空',
  61. 'express_id.requireIf' => '物流公司不能为空',
  62. 'invoice_no.requireIf' => '快递单号不能为空',
  63. 'invoice_no.alphaNum' => '快递单号只能是字母和数字',
  64. ];
  65. public function sceneDetail()
  66. {
  67. return $this->only(['id'])->remove('id', 'checkId');
  68. }
  69. public function sceneAddressEdit()
  70. {
  71. return $this->only(['id','contact','mobile','province_id','city_id','district_id','address'])
  72. ->append('id','checkAddressEdit');
  73. }
  74. public function sceneOrderRemarks()
  75. {
  76. return $this->only(['id'])
  77. ->append('id','array')
  78. ->remove('id','checkId');
  79. }
  80. public function sceneChangePrice()
  81. {
  82. return $this->only(['order_goods_id','change_price']);
  83. }
  84. public function sceneChangeExpressPrice()
  85. {
  86. return $this->only(['order_goods_id','express_price']);
  87. }
  88. public function sceneCancel()
  89. {
  90. return $this->only(['id'])
  91. ->append('id','checkCancel');
  92. }
  93. public function sceneDelivery()
  94. {
  95. return $this->only(['id'])
  96. ->append('id','checkDelivery');
  97. }
  98. public function sceneDeliveryInfo()
  99. {
  100. return $this->only(['id']);
  101. }
  102. public function sceneConfirm()
  103. {
  104. return $this->only(['id'])
  105. ->append('id','checkConfirm');
  106. }
  107. public function sceneLogistics()
  108. {
  109. return $this->only(['id'])
  110. ->append('id','checkLogistics');
  111. }
  112. public function sceneOrderPrint()
  113. {
  114. return $this->only(['id'])
  115. ->remove('id','checkId')
  116. ->append('id','checkPrint');
  117. }
  118. /**
  119. * @notes 检查订单ID是否存在
  120. * @param $value
  121. * @param $rule
  122. * @param $data
  123. * @return bool|string
  124. * @author ljj
  125. * @date 2021/8/6 11:36 上午
  126. */
  127. public function checkId($value,$rule,$data)
  128. {
  129. $order = Order::where('id', $value)->findOrEmpty();
  130. if ($order->isEmpty()) {
  131. return '订单不存在';
  132. }
  133. return true;
  134. }
  135. /**
  136. * @notes 检查订单是否可以修改地址
  137. * @param $value
  138. * @param $rule
  139. * @param $data
  140. * @return bool|string
  141. * @author ljj
  142. * @date 2021/8/10 11:37 上午
  143. */
  144. public function checkAddressEdit($value,$rule,$data)
  145. {
  146. $order = Order::where('id', $value)->findOrEmpty();
  147. if ($order['express_status'] != DeliveryEnum::NOT_SHIPPED) {
  148. return '订单已发货,不可以修改地址';
  149. }
  150. return true;
  151. }
  152. /**
  153. * @notes 检查订单商品id是否存在
  154. * @param $value
  155. * @param $rule
  156. * @param $data
  157. * @return bool|string
  158. * @author ljj
  159. * @date 2021/8/10 2:00 下午
  160. */
  161. public function checkOrderGoodsId($value,$rule,$data)
  162. {
  163. $order_goods = OrderGoods::where('id', $value)->findOrEmpty();
  164. if ($order_goods->isEmpty()) {
  165. return '订单商品不存在';
  166. }
  167. return true;
  168. }
  169. /**
  170. * @notes 检查订单是否可以取消
  171. * @param $value
  172. * @param $rule
  173. * @param $data
  174. * @return bool|string
  175. * @author ljj
  176. * @date 2021/8/10 4:18 下午
  177. */
  178. public function checkCancel($value,$rule,$data)
  179. {
  180. $order = Order::where('id', $value)->findOrEmpty();
  181. if ($order->isEmpty()) {
  182. return '订单不存在';
  183. }
  184. 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)) {
  185. return '订单不允许取消';
  186. }
  187. return true;
  188. }
  189. /**
  190. * @notes 检查物流公司是否存在
  191. * @param $value
  192. * @param $rule
  193. * @param $data
  194. * @return bool|string
  195. * @author ljj
  196. * @date 2021/8/10 5:29 下午
  197. */
  198. public function checkExpressId($value,$rule,$data)
  199. {
  200. $order = Express::where('id', $value)->findOrEmpty();
  201. if ($order->isEmpty()) {
  202. return '物流公司不存在';
  203. }
  204. return true;
  205. }
  206. /**
  207. * @notes 检查订单是否可以发货
  208. * @param $value
  209. * @param $rule
  210. * @param $data
  211. * @return bool|string
  212. * @throws \think\db\exception\DataNotFoundException
  213. * @throws \think\db\exception\DbException
  214. * @throws \think\db\exception\ModelNotFoundException
  215. * @author ljj
  216. * @date 2021/8/10 5:59 下午
  217. */
  218. public function checkDelivery($value,$rule,$data)
  219. {
  220. $order = Order::where('id', $value)->findOrEmpty();
  221. if(OrderEnum::VIRTUAL_ORDER == $order['order_type']){
  222. if (empty($data['delivery_content']) && empty($data['delivery_content1'])) {
  223. return '发货内容不能为空';
  224. }
  225. if (! in_array($data['delivery_content_type'] ?? -111, [ 0, 1 ])) {
  226. return '发货类型不能为空';
  227. }
  228. return true;
  229. }
  230. $validate = Validate::rule([
  231. 'send_type|配送方式' => 'require|in:1,2',
  232. 'delivery_address_id|发货地址' => 'require',
  233. 'return_address_id|退货地址' => 'require',
  234. 'parcel|包裹' => 'requireIf:send_type,1|array',
  235. 'order_goods_ids|发货商品' => 'require|array'
  236. ]);
  237. if (!$validate->check($data)) {
  238. return $validate->getError();
  239. }
  240. if ($order['order_type'] == OrderEnum::TEAM_ORDER && $order['is_team_success'] != TeamEnum::TEAM_FOUND_SUCCESS) {
  241. return '该订单正在拼团中还不能发货';
  242. }
  243. if ($order['order_status'] == OrderEnum::STATUS_CLOSE) {
  244. return '订单已关闭';
  245. }
  246. if ($order['order_status'] != OrderEnum::STATUS_WAIT_DELIVERY || !in_array($order['express_status'],[DeliveryEnum::NOT_SHIPPED,DeliveryEnum::PART_SHIPPED])) {
  247. return '订单不允许发货';
  248. }
  249. if(!isset($data['send_type']) || !in_array($data['send_type'],[1,2])){
  250. return '发货类型错误';
  251. }
  252. if(1 == $data['send_type']){
  253. // if((!isset($data['invoice_no']) || empty($data['invoice_no']))) {
  254. // return '请输入单号';
  255. // }
  256. // if(!isset($data['express_id']) || empty($data['express_id'])){
  257. // return '请选择物流公司';
  258. // }
  259. // $this->checkExpressId($data['express_id'],[],[]);
  260. $invoiceNoArr = [];
  261. foreach ($data['parcel'] as $parcel) {
  262. if(!isset($parcel['invoice_no']) || empty($parcel['invoice_no']) || !isset($parcel['express_id']) || empty($parcel['express_id'])){
  263. return '包裹运单信息不完整,请检查后再发货';
  264. }
  265. if(!isset($parcel['order_goods_info']) || empty($parcel['order_goods_info']) || !is_array($parcel['order_goods_info'])){
  266. return '包裹商品信息错误,请检查后再发货';
  267. }
  268. foreach ($parcel['order_goods_info'] as $info) {
  269. if(!isset($info['order_goods_id']) || empty($info['order_goods_id']) || !isset($info['delivery_num']) || empty($info['delivery_num'])){
  270. return '包裹商品信息缺失,请检查后再发货';
  271. }
  272. }
  273. if (in_array($parcel['invoice_no'], $invoiceNoArr)) {
  274. return '多个包裹运单号重复';
  275. }
  276. $invoiceNoArr[] = $parcel['invoice_no'];
  277. $order_goods_ids = array_column($parcel['order_goods_info'],'order_goods_id');
  278. $delivery_num_arr = array_column($parcel['order_goods_info'],'delivery_num','order_goods_id');
  279. $order_goods = OrderGoods::where(['id'=>$order_goods_ids])->select()->toArray();
  280. foreach ($order_goods as $goods) {
  281. if ($goods['express_status'] == DeliveryEnum::SHIPPED) {
  282. return '存在已发货商品,无法发货';
  283. }
  284. if ($goods['goods_num'] - $goods['delivery_num'] < $delivery_num_arr[$goods['id']]) {
  285. return '超出商品剩余数量,无法发货';
  286. }
  287. }
  288. $after_sale = AfterSale::where(['order_goods_id' => $order_goods_ids, 'order_id' => $data['id']])->findOrEmpty();
  289. if (!$after_sale->isEmpty() && in_array($after_sale->status,[AfterSaleEnum::STATUS_ING,AfterSaleEnum::STATUS_SUCCESS])) {
  290. return '存在售后商品,无法发货';
  291. }
  292. }
  293. } else {
  294. $order_goods = OrderGoods::where(['id'=>$data['order_goods_ids']])->select()->toArray();
  295. foreach ($order_goods as $goods) {
  296. if ($goods['express_status'] == DeliveryEnum::SHIPPED) {
  297. return '存在已发货商品,无法发货';
  298. }
  299. }
  300. $after_sale = AfterSale::where(['order_goods_id' => $data['order_goods_ids'] ?? 0, 'order_id' => $data['id']])->findOrEmpty();
  301. if (!$after_sale->isEmpty() && in_array($after_sale->status,[AfterSaleEnum::STATUS_ING,AfterSaleEnum::STATUS_SUCCESS])) {
  302. return '存在售后商品,无法发货';
  303. }
  304. }
  305. // $order_goods = OrderGoods::where(['order_id'=>$order['id']])->select()->toArray();
  306. // foreach ($order_goods as $goods) {
  307. // $after_sale = AfterSale::where(['order_goods_id' => $goods['id'], 'order_id' => $goods['order_id']])->findOrEmpty();
  308. // if (!$after_sale->isEmpty() && $after_sale->status == AfterSaleEnum::STATUS_ING) {
  309. // return '订单商品:'.$goods['goods_name'].' 处于售后中,无法发货';
  310. // }
  311. // }
  312. return true;
  313. }
  314. /**
  315. * @notes 检查订单是否可以确认收货
  316. * @param $value
  317. * @param $rule
  318. * @param $data
  319. * @return bool|string
  320. * @author ljj
  321. * @date 2021/8/11 10:19 上午
  322. */
  323. public function checkConfirm($value,$rule,$data)
  324. {
  325. $order = Order::where('id', $value)->findOrEmpty();
  326. if ($order['order_status'] != OrderEnum::STATUS_WAIT_RECEIVE) {
  327. return '订单不允许确认收货';
  328. }
  329. return true;
  330. }
  331. /**
  332. * @notes 检查订单是否已发货
  333. * @param $value
  334. * @param $rule
  335. * @param $data
  336. * @return bool|string
  337. * @author ljj
  338. * @date 2021/8/13 11:20 上午
  339. */
  340. public function checkLogistics($value,$rule,$data)
  341. {
  342. $order = Order::where('id', $value)->findOrEmpty();
  343. if ($order['express_status'] == DeliveryEnum::NOT_SHIPPED) {
  344. return '订单未发货,暂无物流信息';
  345. }
  346. return true;
  347. }
  348. /**
  349. * @notes 校验打印机
  350. * @return bool|string
  351. * @author Tab
  352. * @date 2021/11/16 9:56
  353. */
  354. public function checkPrint()
  355. {
  356. $printer = Printer::where('status', YesNoEnum::YES)->findOrEmpty();
  357. if ($printer->isEmpty()) {
  358. return '请先添加打印机并设置为启动状态';
  359. }
  360. return true;
  361. }
  362. }