OrderInvoiceRemarkForm.php 2.0 KB

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