OrderInvoiceRemarkForm.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. ];
  30. }
  31. /**
  32. * 指定校验场景
  33. * @return array
  34. */
  35. public function scenarios()
  36. {
  37. $parentScenarios = parent::scenarios();
  38. $customScenarios = [];
  39. return array_merge($parentScenarios, $customScenarios);
  40. }
  41. /**
  42. * @throws Exception
  43. */
  44. public function remarkOrderInvoice()
  45. {
  46. if (!$this->validate()) {
  47. return null;
  48. }
  49. $sn = $this->orderSn;
  50. // 订单详情
  51. $orderInfo = Order::findOneAsArray(['SN' => $sn]);
  52. if (!$orderInfo) {
  53. throw new Exception('订单不存在');
  54. }
  55. $db = \Yii::$app->db;
  56. $transaction = $db->beginTransaction();
  57. try {
  58. Order::updateAll(
  59. ['INVOICE_REMARK' => $this->invoiceRemark,'METHOD'=>$this->orderMethod],
  60. 'SN=:SN',
  61. ['SN' => $sn]
  62. );
  63. $transaction->commit();
  64. } catch(Exception $e) {
  65. $transaction->rollBack();
  66. $this->addError('add', $e->getMessage());
  67. return null;
  68. }
  69. return true;
  70. }
  71. }