OrderInvoiceRemarkForm.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /**
  11. * @inheritdoc
  12. */
  13. public function rules()
  14. {
  15. return [
  16. [['orderSn'], 'trim'],
  17. [['orderSn'], 'required'],
  18. ['invoiceRemark', 'string', 'max' => 50000],
  19. ];
  20. }
  21. public function attributeLabels()
  22. {
  23. return [
  24. 'orderSn' => '订单号',
  25. 'invoiceRemark' => '发票备注',
  26. ];
  27. }
  28. /**
  29. * 指定校验场景
  30. * @return array
  31. */
  32. public function scenarios()
  33. {
  34. $parentScenarios = parent::scenarios();
  35. $customScenarios = [];
  36. return array_merge($parentScenarios, $customScenarios);
  37. }
  38. /**
  39. * @throws Exception
  40. */
  41. public function remarkOrderInvoice()
  42. {
  43. if (!$this->validate()) {
  44. return null;
  45. }
  46. $sn = $this->orderSn;
  47. // 订单详情
  48. $orderInfo = Order::findOneAsArray(['SN' => $sn]);
  49. if (!$orderInfo) {
  50. throw new Exception('订单不存在');
  51. }
  52. $db = \Yii::$app->db;
  53. $transaction = $db->beginTransaction();
  54. try {
  55. Order::updateAll(
  56. ['INVOICE_REMARK' => $this->invoiceRemark],
  57. 'SN=:SN',
  58. ['SN' => $sn]
  59. );
  60. $transaction->commit();
  61. } catch(Exception $e) {
  62. $transaction->rollBack();
  63. $this->addError('add', $e->getMessage());
  64. return null;
  65. }
  66. return true;
  67. }
  68. }