CreateOrderForm.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace hd\models\delivery;
  3. use bizHd\order\classes\OrderClass;
  4. use hd\models\BaseForm;
  5. use Yii;
  6. /**
  7. * 创建配送订单表单模型
  8. * 用于 actionCreateOrder 的参数验证
  9. */
  10. class CreateOrderForm extends BaseForm
  11. {
  12. public $orderId; // xhGhsOrder.id
  13. public $remark; // 订单备注
  14. public $price; // 配送费(单位:分)
  15. public $pickupTime; // 取件时间
  16. public $allParams; // 配送平台所需的所有参数(透传平台参数)
  17. public function rules()
  18. {
  19. return [
  20. [['orderId', 'price'], 'required'],
  21. [['orderId', 'price'], 'integer'],
  22. //['orderId', 'validateOrderId'],
  23. ['pickupTime', 'safe'],
  24. ['allParams', 'safe'],
  25. ['remark', 'string', 'max' => 500],
  26. ];
  27. }
  28. public function attributeLabels()
  29. {
  30. return [
  31. 'orderId' => '订单ID',
  32. 'remark' => '备注',
  33. 'price' => '配送费用',
  34. 'pickupTime' => '取件时间',
  35. 'allParams' => '配送参数',
  36. ];
  37. }
  38. /**
  39. * 验证订单ID是否存在且属于当前商户
  40. * @param $attribute
  41. */
  42. public function validateOrderId($attribute)
  43. {
  44. if (!$this->hasErrors()) {
  45. $order = OrderClass::getById($this->orderId, false, 'id, mainId, sendStatus');
  46. if (empty($order)) {
  47. $this->addError($attribute, '订单不存在');
  48. return;
  49. }
  50. if ($order['mainId'] != Yii::$app->controller->mainId) {
  51. $this->addError($attribute, '订单不属于当前商户');
  52. return;
  53. }
  54. // 检查订单是否已发过跑腿
  55. if (!in_array($order['sendStatus'], [-4, -2, -1])) {
  56. $this->addError($attribute, '订单已经发过跑腿');
  57. }
  58. }
  59. }
  60. }