DeclarationLoopForm.php 7.9 KB

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