DeclarationLoopForm.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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->location = null;
  87. $model->decType = null;
  88. } else {
  89. $this->addError($attribute, Yii::t('app', 'reportFormatIncorrect'));
  90. }
  91. }
  92. }
  93. /**
  94. * 报单
  95. * @return bool
  96. * @throws \yii\db\Exception
  97. */
  98. public function add(){
  99. $startTime = microtime(true);
  100. if(!$this->validate()){
  101. return null;
  102. }
  103. $db = \Yii::$app->db;
  104. $transaction = $db->beginTransaction();
  105. try{
  106. // 所有的首购单会员ID以备点位绑定使用
  107. $allZcUserIds = [];
  108. $zcUserIdCard = null;
  109. $model = new DeclarationForm();
  110. $model->scenario = $this->scenario;
  111. $model->allData = $this->data;
  112. foreach ($this->data as $value){
  113. /**
  114. * 2022-04-29
  115. * York
  116. * 获取商品套餐信息
  117. */
  118. if (isset($value['packageId']) && $value['packageId']){
  119. $packagedata = DeclarationPackage::findOneAsArray('ID=:ID', [':ID' => $value['packageId']]);
  120. if (!$packagedata) {
  121. throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
  122. }
  123. //var_dump($packagedata['ID']);
  124. if($packagedata['STORE_NUMS']<=0){
  125. throw new Exception($packagedata['PACKAGE_NAME'] . Yii::t('app', 'insufficientInventory'));
  126. }
  127. }
  128. if (count($value['goodsId']) > 0 && (count($value['goodsId']) == count($value['goodsNum']))){
  129. for ($i=0;$i<count($value['goodsId']);$i++){
  130. $goods = ShopGoods::findOneAsArray('ID=:ID',[':ID'=> $value['goodsId'][$i]]);
  131. if (!$goods) {
  132. throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
  133. }
  134. $goodsNature = ShopGoodsNature::findOneAsArray('GOODS_ID=:GOODS_ID AND COUNTRY_ID=:COUNTRY_ID',
  135. [':GOODS_ID' => $value['goodsId'][$i], ':COUNTRY_ID' => $value['countryId']]);
  136. if (!$goodsNature) {
  137. throw new Exception(Yii::t('app', 'productsDoesSoldOut'));
  138. }
  139. if($goods['INSTALMENT']>0){ // 分期的商品
  140. if($value['goodsNum'][$i]>1){ // 只能购买一个
  141. throw new Exception(Yii::t('app', 'allowOnlyOne'));
  142. }
  143. // 分期的总期数
  144. $instalment = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
  145. // 分期商品的期数不能大于总分期数限制
  146. if (intval($goods['INSTALMENT']) > $instalment) {
  147. throw new Exception(Yii::t('app', 'instalmentGoodsNoError'));
  148. }
  149. }
  150. if($goods['INSTALMENT']>1){ // 不允许购买“非第一期”的商品
  151. throw new Exception(Yii::t('app', 'canNotBuy'));
  152. }
  153. if ($goods['STATUS'] == 1 ){
  154. if($goods['STORE_NUMS'] < $value['goodsNum'][$i]){
  155. throw new Exception($goods['GOODS_NAME'] . Yii::t('app', 'insufficientInventory'));
  156. }
  157. }else{
  158. throw new Exception($goods['GOODS_NAME'] . Yii::t('app', 'soldOut'));
  159. }
  160. }
  161. }
  162. if(is_array($value)){
  163. foreach($value as $key=>$decFormData){
  164. $model->$key = $decFormData;
  165. }
  166. // 把首购单的几个会员归集到一个数组里,将来绑定到一起
  167. if( $model->type == 'ZC'){
  168. $allZcUserIds[] = null;
  169. if($zcUserIdCard != null){
  170. if($model->insertUserIdCard != $zcUserIdCard){
  171. throw new Exception(Yii::t('app', 'bulkDeclarationNotSames'));
  172. }
  173. } else {
  174. $zcUserIdCard = $model->insertUserIdCard;
  175. }
  176. }
  177. if(!$model->add($this->data)){
  178. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  179. }
  180. } else {
  181. throw new Exception(Yii::t('app', 'reportFormatIncorrect'));
  182. }
  183. }
  184. $transaction->commit();
  185. } catch (\Exception $e){
  186. $transaction->rollBack();
  187. $this->addError('add', $e->getMessage());
  188. return null;
  189. }
  190. return true;
  191. }
  192. /**
  193. * RPC服务校验数据
  194. * @param $data
  195. * @return array
  196. */
  197. public static function rpcIsData($data){
  198. $result = [
  199. 'error' => false,
  200. 'message' => 'Data can be reported',//数据可报单
  201. ];
  202. $formModel = new self();
  203. $formModel->scenario = 'canDec';
  204. $formModel->data = $data;
  205. if(!$formModel->validate()){
  206. $result['error'] = true;
  207. $result['message'] = Form::formatErrorsForApi($formModel->getErrors());
  208. }
  209. return $result;
  210. }
  211. }