ApproachDeclarationLoopForm.php 9.1 KB

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