DeclarationLoopForm.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Cache;
  5. use common\helpers\Form;
  6. use common\models\UserBind;
  7. use Yii;
  8. use yii\base\Exception;
  9. use yii\helpers\Json;
  10. use common\models\DeclarationPackage;
  11. use common\helpers\Date;
  12. use common\models\ShopGoods;
  13. /**
  14. * Login form
  15. */
  16. class DeclarationLoopForm extends Model
  17. {
  18. public $data;
  19. /**
  20. * @inheritdoc
  21. */
  22. public function rules()
  23. {
  24. return [
  25. [['data'], 'required'],
  26. [['data'], 'formatData'],
  27. [['data'], 'isData', 'on'=>['canDec', 'notFull']],
  28. ];
  29. }
  30. public function attributeLabels()
  31. {
  32. return [
  33. 'data' => 'Data',// 数据
  34. ];
  35. }
  36. /**
  37. * 指定场景
  38. * @return array
  39. */
  40. public function scenarios()
  41. {
  42. $parentScenarios = parent::scenarios();
  43. $customScenarios = [
  44. 'userDec' => ['data'],
  45. 'canDec' => ['data'],
  46. 'notFull' => ['data'],
  47. ];
  48. return array_merge($parentScenarios, $customScenarios);
  49. }
  50. /**
  51. * 格式化提交的数据
  52. * @param $attribute
  53. */
  54. public function formatData($attribute){
  55. //$this->data = Json::decode($this->data);
  56. if(!is_array($this->data)){
  57. $this->addError($attribute, 'Data format error');// 数据格式错误
  58. }
  59. }
  60. /**
  61. * 循环校验数据是否合格
  62. * @param $attribute
  63. */
  64. public function isData($attribute){
  65. $model = new DeclarationForm();
  66. $model->scenario = $this->scenario;
  67. $model->allData = $this->data;
  68. foreach ($this->data as $value){
  69. if(is_array($value)){
  70. foreach($value as $key=>$decFormData){
  71. $model->$key = $decFormData;
  72. }
  73. if(!$model->validate()){
  74. $this->addErrors($model->getErrors());
  75. }
  76. $model->type = null;
  77. $model->decSn = null;
  78. $model->userId = null;
  79. $model->toUserId = null;
  80. $model->decPv = null;
  81. $model->insertUserName = null;
  82. $model->insertUserIdCard = null;
  83. $model->conUserName = null;
  84. $model->recUserName = null;
  85. $model->location = null;
  86. $model->decType = null;
  87. } else {
  88. $this->addError($attribute, Yii::t('app', 'reportFormatIncorrect'));
  89. }
  90. }
  91. }
  92. /**
  93. * 报单
  94. * @return bool
  95. * @throws \yii\db\Exception
  96. */
  97. public function add(){
  98. $startTime = microtime(true);
  99. if(!$this->validate()){
  100. return null;
  101. }
  102. $db = \Yii::$app->db;
  103. $transaction = $db->beginTransaction();
  104. try{
  105. // 所有的首购单会员ID以备点位绑定使用
  106. $allZcUserIds = [];
  107. $zcUserIdCard = null;
  108. $model = new DeclarationForm();
  109. $model->scenario = $this->scenario;
  110. $model->allData = $this->data;
  111. foreach ($this->data as $value){
  112. /**
  113. * 2022-04-29
  114. * York
  115. * 获取商品套餐信息
  116. */
  117. if (isset($value['packageId']) && $value['packageId']){
  118. $packagedata = DeclarationPackage::findOneAsArray('ID=:ID', [':ID' => $value['packageId']]);
  119. if (!$packagedata) {
  120. throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
  121. }
  122. //var_dump($packagedata['ID']);
  123. if($packagedata['STORE_NUMS']<=0){
  124. throw new Exception($packagedata['PACKAGE_NAME'] . Yii::t('app', 'insufficientInventory'));
  125. }
  126. }
  127. if (count($value['goodsId']) > 0 && (count($value['goodsId']) == count($value['goodsNum']))){
  128. for ($i=0;$i<count($value['goodsId']);$i++){
  129. $goods = ShopGoods::findOneAsArray('ID=:ID',[':ID'=> $value['goodsId'][$i]]);
  130. if (!$goods) {
  131. throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
  132. }
  133. if($goods['INSTALMENT']>0){ // 分期的商品
  134. if($value['goodsNum'][$i]>1){ // 只能购买一个
  135. throw new Exception(Yii::t('app', 'allowOnlyOne'));
  136. }
  137. // 分期的总期数
  138. $instalment = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
  139. // 分期商品的期数不能大于总分期数限制
  140. if (intval($goods['INSTALMENT']) > $instalment) {
  141. throw new Exception(Yii::t('app', 'instalmentGoodsNoError'));
  142. }
  143. }
  144. if($goods['INSTALMENT']>1){ // 不允许购买“非第一期”的商品
  145. throw new Exception(Yii::t('app', 'canNotBuy'));
  146. }
  147. if ($goods['STATUS'] == 1 ){
  148. if($goods['STORE_NUMS'] < $value['goodsNum'][$i]){
  149. throw new Exception($goods['GOODS_NAME'] . Yii::t('app', 'insufficientInventory'));
  150. }
  151. }else{
  152. throw new Exception($goods['GOODS_NAME'] . Yii::t('app', 'soldOut'));
  153. }
  154. }
  155. }
  156. if(is_array($value)){
  157. foreach($value as $key=>$decFormData){
  158. $model->$key = $decFormData;
  159. }
  160. // 把首购单的几个会员归集到一个数组里,将来绑定到一起
  161. if( $model->type == 'ZC'){
  162. $allZcUserIds[] = null;
  163. if($zcUserIdCard != null){
  164. if($model->insertUserIdCard != $zcUserIdCard){
  165. throw new Exception(Yii::t('app', 'bulkDeclarationNotSames'));
  166. }
  167. } else {
  168. $zcUserIdCard = $model->insertUserIdCard;
  169. }
  170. }
  171. if(!$model->add($this->data)){
  172. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  173. }
  174. } else {
  175. throw new Exception(Yii::t('app', 'reportFormatIncorrect'));
  176. }
  177. }
  178. $transaction->commit();
  179. } catch (\Exception $e){
  180. $transaction->rollBack();
  181. $this->addError('add', $e->getMessage());
  182. return null;
  183. }
  184. return true;
  185. }
  186. /**
  187. * RPC服务校验数据
  188. * @param $data
  189. * @return array
  190. */
  191. public static function rpcIsData($data){
  192. $result = [
  193. 'error' => false,
  194. 'message' => 'Data can be reported',//数据可报单
  195. ];
  196. $formModel = new self();
  197. $formModel->scenario = 'canDec';
  198. $formModel->data = $data;
  199. if(!$formModel->validate()){
  200. $result['error'] = true;
  201. $result['message'] = Form::formatErrorsForApi($formModel->getErrors());
  202. }
  203. return $result;
  204. }
  205. }