OrderInvoiceRemarkForm.php 1.8 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. /**
  11. * @inheritdoc
  12. */
  13. public function rules()
  14. {
  15. return [
  16. [['orderSn'], 'trim'],
  17. [['orderSn'], 'required'],
  18. ['invoice_remark', 'string', 'max' => 50000],
  19. ];
  20. }
  21. public function attributeLabels()
  22. {
  23. return [
  24. 'orderSn' => '订单号',
  25. 'invoice_remark' => '发票备注',
  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::findUseDbCalc()
  49. ->select('ID,INVOICE_REMARK,SN,STATUS,COUNTRY_ID')
  50. ->where("SN=:SN", [':SN' => $sn])
  51. ->asArray()
  52. ->one();
  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],
  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. }