ApproachDeclarationLoopForm.php 8.1 KB

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