OrderInvoiceRemarkForm.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\models\Order;
  5. use yii\base\Exception;
  6. class OrderInvoiceRemarkForm extends Model
  7. {
  8. public $orderSn;
  9. public $invoiceRemark;
  10. public $orderMethod;
  11. /**
  12. * @inheritdoc
  13. */
  14. public function rules()
  15. {
  16. return [
  17. [['orderSn'], 'trim'],
  18. [['orderSn'], 'required'],
  19. ['invoiceRemark', 'string', 'max' => 50000],
  20. ['orderMethod', 'string'],
  21. ['orderMethod', 'in', 'range' => ['Cash', 'Credit Card', 'Direct Banking', 'Cheque']],
  22. ];
  23. }
  24. public function attributeLabels()
  25. {
  26. return [
  27. 'orderSn' => '订单号',
  28. 'invoiceRemark' => '发票备注',
  29. 'orderMethod' => '支付方式',
  30. ];
  31. }
  32. /**
  33. * 指定校验场景
  34. * @return array
  35. */
  36. public function scenarios()
  37. {
  38. $parentScenarios = parent::scenarios();
  39. $customScenarios = [];
  40. return array_merge($parentScenarios, $customScenarios);
  41. }
  42. /**
  43. * @throws Exception
  44. */
  45. public function remarkOrderInvoice()
  46. {
  47. if (!$this->validate()) {
  48. return null;
  49. }
  50. $sn = $this->orderSn;
  51. // 订单详情
  52. $orderInfo = Order::findOneAsArray(['SN' => $sn]);
  53. if (!$orderInfo) {
  54. throw new Exception('订单不存在');
  55. }
  56. $db = \Yii::$app->db;
  57. $transaction = $db->beginTransaction();
  58. try {
  59. Order::updateAll(
  60. ['INVOICE_REMARK' => $this->invoiceRemark,'METHOD'=>$this->orderMethod],
  61. 'SN=:SN',
  62. ['SN' => $sn]
  63. );
  64. $transaction->commit();
  65. } catch(Exception $e) {
  66. $transaction->rollBack();
  67. $this->addError('add', $e->getMessage());
  68. return null;
  69. }
  70. return true;
  71. }
  72. }