ApproachDeclarationLoopForm.php 8.6 KB

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