CreateOrderForm.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace ghs\models\order;
  3. use common\components\dict;
  4. use ghs\models\BaseForm;
  5. class CreateOrderForm extends BaseForm
  6. {
  7. public $customId;
  8. public $modifyPrice;
  9. public $dealPrice;
  10. public $getStaffId;
  11. public $getStaffName;
  12. public $shopAdminId;
  13. public $shopAdminName;
  14. public $expressId;
  15. public $book;
  16. public $sendType;
  17. public $transType;
  18. public $sendCost;
  19. public $packCost;
  20. public $hasPay;
  21. public $xj;
  22. public $product;
  23. public $historyDate;
  24. public $callErrand;
  25. public $deliveryPlatform;
  26. public $deliveryPrice;
  27. public $deliveryDistance;
  28. public $deliveryBracketContent;
  29. public $deliveryAllParams;
  30. public $pickupTime;
  31. public $isCashier;
  32. public $payWay;
  33. public $weight;
  34. public $deliveryRemark;
  35. public $needPrint;
  36. // Additional fields that might be passed or processed
  37. public $bookSn;
  38. public function rules()
  39. {
  40. return [
  41. ['customId', 'required', 'message' => '请选择客户'],
  42. ['product', 'required', 'message' => '请选择花材'],
  43. [['customId', 'expressId', 'book', 'sendType', 'transType', 'hasPay', 'callErrand', 'isCashier', 'getStaffId', 'shopAdminId', 'bookSn', 'needPrint'], 'integer'],
  44. [['modifyPrice', 'dealPrice', 'sendCost', 'packCost', 'deliveryPrice', 'deliveryDistance', 'weight'], 'number'],
  45. [['getStaffName', 'shopAdminName', 'deliveryPlatform', 'deliveryBracketContent', 'pickupTime', 'historyDate', 'deliveryRemark'], 'string'],
  46. [['xj', 'product', 'payWay', 'deliveryAllParams'], 'safe'],
  47. ['modifyPrice', 'match', 'pattern' => '/^\d+(\.\d{1,2})?$/', 'message' => '金额不能超2位小数'],
  48. ['product', 'validateProduct'],
  49. ['historyDate', 'validateHistoryDate'],
  50. // 此规则依赖 Controller 上下文中的 shop->pfLevel 判断,可能不适合完全移入 Model,除非 Model 能获取 Shop 信息。
  51. // 暂时不在 Model 中校验 sendCost 与 sendType 的复杂业务逻辑,因为涉及到 Shop 属性判断。
  52. ];
  53. }
  54. public function attributeLabels()
  55. {
  56. return [
  57. 'customId' => '客户',
  58. 'modifyPrice' => '修改价格',
  59. 'product' => '花材',
  60. 'historyDate' => '订单日期',
  61. 'sendCost' => '运费',
  62. ];
  63. }
  64. public function validateProduct($attribute, $params)
  65. {
  66. if (!$this->hasErrors()) {
  67. $productJson = $this->$attribute;
  68. if (empty($productJson)) {
  69. $this->addError($attribute, '请选择花材');
  70. return;
  71. }
  72. $productList = json_decode($productJson, true);
  73. if (empty($productList)) {
  74. $this->addError($attribute, '请选择花材');
  75. }
  76. }
  77. }
  78. public function validateHistoryDate($attribute, $params)
  79. {
  80. if (!$this->hasErrors() && !empty($this->historyDate)) {
  81. if ($this->book == 1) {
  82. $this->addError($attribute, '预订单不能选订单日期');
  83. return;
  84. }
  85. // hasPay == 0 通常指 unPay / 扫码付?需确认 dict。
  86. // 假设 Controller 逻辑: if ($hasPay == 0) fail('扫码付不能选订单日期');
  87. // 这里 $hasPay 可能是 null,如果不传。
  88. // Controller 逻辑: $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  89. // Model 中如果 load 进来,hasPay 会有值。
  90. // 需要引入 dict 类。
  91. // 注意:Controller 中 hasPay 的默认值处理逻辑是在 validation 之后(或者中间)。
  92. // 我们可以尝试模拟这个默认值,或者只验证当 hasPay 明确为 0 时。
  93. // Controller logic:
  94. // $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  95. // if ($hasPay == 0) ...
  96. $hasPayVal = $this->hasPay ?? dict::getDict('hasPay', 'unPay');
  97. if ($hasPayVal == 0) {
  98. $this->addError($attribute, '扫码付不能选订单日期');
  99. return;
  100. }
  101. }
  102. }
  103. }