ApproachDeclarationLoopForm.php 9.0 KB

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