CreateOrderForm.php 2.0 KB

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