CloudStockTwoGoodsStockLog.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. namespace addons\CloudStockTwo\common\models;
  3. use app\plugins\cloud_stock\models\StockDeductionDetail;
  4. use common\enums\StatusEnum;
  5. use addons\CloudStockTwo\common\enums\CloudStockTwoEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use common\models\goods\Goods;
  8. use common\models\user\User;
  9. use common\models\user\UserPartitionRelationship;
  10. use Yii;
  11. /**
  12. * This is the model class for table "{{%addons_cloud_stock_goods_stock_log".
  13. *
  14. * @property int $id 自增id
  15. * @property int $mall_id 商城id
  16. * @property int $user_id 用户id
  17. * @property int $goods_id 商品id
  18. * @property int $changes_dir 库存变动方向;1增加;2减少
  19. * @property int $order_id 相关的订单id
  20. * @property int $order_type order类型,1:会员购买订单,2:补货订单;
  21. * @property int $changes_type 库存变动类型;100向上级补货;101上级给自己配货;102退货退库存;103系统添加;104平台补货赠送;105购买礼包赠送;200会员买货;201给下级配货;202下级向自己补货;203自提货到线下;204系统扣减;
  22. * @property int $num 数量
  23. * @property int $status 状态[-1:删除;0:禁用;1启用]
  24. * @property int $created_at
  25. * @property int $updated_at
  26. */
  27. class CloudStockTwoGoodsStockLog extends BaseActiveRecord
  28. {
  29. private static $stock_good = null;
  30. private static $good = null;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_cloud_stock_two_goods_stock_log}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['mall_id', 'user_id','goods_id', 'changes_dir', 'order_id', 'order_type', 'changes_type', 'num', 'status', 'created_at', 'updated_at'], 'integer'],
  45. [['changes_dir', 'changes_type', 'num'], 'required'],
  46. ];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => '自增id',
  55. 'mall_id' => '商城id',
  56. 'user_id' => '用户id',
  57. 'goods_id' => '商品id',
  58. 'changes_dir' => '库存变动方向;1增加;2减少',
  59. 'order_id' => '相关的订单id',
  60. 'order_type' => 'order类型,1:会员购买订单,2:补货订单;',
  61. 'changes_type' => '库存变动类型;100向上级补货;101上级给自己配货;102退货退库存;103系统添加;104平台补货赠送;105购买礼包赠送;200会员买货;201给下级配货;202下级向自己补货;203自提货到线下;204系统扣减;',
  62. 'num' => '数量',
  63. 'status' => '状态[-1:删除;0:禁用;1启用]',
  64. 'created_at' => 'Created At',
  65. 'updated_at' => 'Updated At',
  66. ];
  67. }
  68. public function getStockDeductionDetail()
  69. {
  70. return $this->hasOne(CloudStockTwoDeductionDetail::class, ['stock_log_id' => 'id'])->select(['*']);
  71. }
  72. public function getStockIncreaseDetail()
  73. {
  74. return $this->hasOne(CloudStockTwoDeductionDetail::class, ['stock_log_id' => 'id'])->select(['*']);
  75. }
  76. public function getUser()
  77. {
  78. return $this->hasOne(User::class, ['id' => 'user_id'])->select(['*']);
  79. }
  80. public function getGoods()
  81. {
  82. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select(['id', 'goods_name', 'cover_pic']);
  83. }
  84. public static function setData(int $mall_id, array $data)
  85. {
  86. if (!empty($data['id'])) {
  87. $model = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['id']]);
  88. } else {
  89. $model = new self();
  90. $model->loadDefaultValues();
  91. $model->mall_id = $mall_id;
  92. }
  93. foreach ($data as $key => $val) {
  94. $model->$key = $val;
  95. }
  96. // dd($model);
  97. if (!$model->save()) {
  98. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  99. return false;
  100. }
  101. return $model;
  102. }
  103. /**
  104. * @name:
  105. * @msg: 库存处理
  106. * @param {int} $mall_id
  107. * @param {array} $data number:商品变动数量;goods_id:商品id,user_id:需要处理的会员id;target_user_id:目标会员id
  108. * @param {int} $changes_type 库存变动类型,具体参考 CloudStockTwoEnum::getStockChangeTypeMap()
  109. * @return bool
  110. */
  111. public static function stockHandle(int $mall_id, array $data, int $changes_type): bool
  112. {
  113. if (empty($data['target_user_id'])) {
  114. $target_user_id = 0;
  115. $is_adjoint = 0;
  116. } else {
  117. $target_user_id = $data['target_user_id'];
  118. $is_adjoint = 1;
  119. }
  120. $transaction = \Yii::$app->db->beginTransaction();
  121. try {
  122. $rules = [
  123. [['goods_id', 'number', 'user_id'], 'required'],
  124. [['goods_id', 'number', 'user_id', 'target_user_id', 'order_id', 'order_type'], 'integer'],
  125. [['remark'], 'safe'],
  126. [['order_id', 'order_type'], 'required', 'when' => function($model) use($changes_type) {
  127. if (
  128. ($changes_type >= CloudStockTwoEnum::STOCK_CHANGE_TYPE100 &&
  129. $changes_type <= CloudStockTwoEnum::STOCK_CHANGE_TYPE105 &&
  130. in_array($changes_type, [CloudStockTwoEnum::STOCK_CHANGE_TYPE100, CloudStockTwoEnum::STOCK_CHANGE_TYPE102])) && // 增/减库存分界线,上分下减
  131. $changes_type >= CloudStockTwoEnum::STOCK_CHANGE_TYPE200 &&
  132. $changes_type <= CloudStockTwoEnum::STOCK_CHANGE_TYPE204
  133. ) {
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }],
  139. ];
  140. $res = \Yii::$app->services->validate->validate($data, $rules);
  141. if ($res === false) throw new \Exception(\Yii::$app->services->validate->getErrorMessage());
  142. if ($changes_type >= CloudStockTwoEnum::STOCK_CHANGE_TYPE100 && $changes_type <= CloudStockTwoEnum::STOCK_CHANGE_TYPE105) {
  143. // 加库存
  144. $changes_dir = CloudStockTwoEnum::STOCK_CHANGE_ADD;
  145. } else if ($changes_type >= CloudStockTwoEnum::STOCK_CHANGE_TYPE200 && $changes_type <= CloudStockTwoEnum::STOCK_CHANGE_TYPE204) {
  146. // 减库存
  147. $changes_dir = CloudStockTwoEnum::STOCK_CHANGE_SUB;
  148. } else {
  149. throw new \Exception('不支持的库存变动类型');
  150. }
  151. $data['target_user_id'] = $target_user_id;
  152. // 检查会员两方是否符合条件
  153. self::checkAgenUser($mall_id, $data, $is_adjoint);
  154. // 检查商品
  155. self::checkAgentGood($mall_id, $data,$changes_dir);
  156. // dd($changes_dir, $changes_type);
  157. // 处理当前会员库存增减并记录log
  158. self::agentUserSaveLog($mall_id, $data, $changes_dir, $changes_type);
  159. $adjoint_change_type = self::adjointChangeType($changes_type);
  160. if (0 == $adjoint_change_type) {
  161. // 没有共轭变动明细需要记录
  162. $transaction->commit();
  163. return true;
  164. }
  165. // 处理共轭变动会员的库存并记录log
  166. if (empty($data['target_user_id'])) throw new \Exception('目标会员id不能为空');
  167. $adjoint_change_dir = self::adjointChangeDir($changes_dir);
  168. $data['user_id'] = $data['target_user_id'];
  169. $configs = \Yii::$app->services->setting->addons->get($mall_id, CloudStockTwoEnum::ADDONS_NAME, 'basic');
  170. $is_can_pick_free = $configs['is_can_pick_free'] ?? 0;
  171. if (1 == $is_can_pick_free) {
  172. // 允许自由配货,下级没有的商品也可以直接配货
  173. // 检查商品
  174. self::checkAgentGood($mall_id, $data,$adjoint_change_dir);
  175. }
  176. self::agentUserSaveLog($mall_id, $data, $adjoint_change_dir, $adjoint_change_type);
  177. $transaction->commit();
  178. return true;
  179. } catch (\Exception $e) {
  180. \Yii::error($e->getMessage() . ', file: ' . $e->getFile() . ', line: ' . $e->getLine());\Yii::getLogger()->flush(true);
  181. self::$static_error = $e->getMessage();
  182. $transaction->rollBack();
  183. return false;
  184. }
  185. }
  186. /**
  187. * @name:
  188. * @msg: 检查会员两方是否符合条件
  189. * @param {int} $mall_id
  190. * @param {array} $data user_id target_user_id
  191. * @return {*}
  192. */
  193. private static function checkAgenUser(int $mall_id, array $data, int $is_adjoint = 1)
  194. {
  195. $agent = CloudStockTwoAgent::findOne(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'status' => StatusEnum::ENABLED]);
  196. if (empty($agent)) {
  197. throw new \Exception('当前会员不是代理商');
  198. }
  199. if (!empty($is_adjoint)) {
  200. $child_user_ids = UserPartitionRelationship::getChildIdArr($data['user_id']);
  201. if (!in_array($data['target_user_id'], $child_user_ids)) {
  202. throw new \Exception('对方不是你的下级会员,不能配货');
  203. }
  204. $sub_agent = CloudStockTwoAgent::findOne(['mall_id' => $mall_id, 'user_id' => $data['target_user_id'], 'status' => StatusEnum::ENABLED]);
  205. if (empty($sub_agent)) {
  206. throw new \Exception('对方不是代理商');
  207. }
  208. }
  209. return true;
  210. }
  211. /**
  212. * @name:
  213. * @msg: 检查商品状态
  214. * @param {int} $mall_id
  215. * @param {array} $data goods_id:商品id,user_id:当前会员id
  216. * @return {*}
  217. */
  218. private static function checkAgentGood(int $mall_id, array $data, $changes_dir)
  219. {
  220. $good = Goods::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $data['goods_id'], 'is_on_sale' => 1, 'is_attr' => 0]);
  221. if (empty($good)) throw new \Exception('商品数据异常');
  222. $stock_good = CloudStockTwoAgentGoods::findOne(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'goods_id' => $data['goods_id'], 'status' => StatusEnum::ENABLED]);
  223. if (empty($stock_good)) {
  224. // throw new \Exception('未找到代理商品,请先补货');
  225. $stock_good = new CloudStockTwoAgentGoods();
  226. $stock_good->loadDefaultValues();
  227. $stock_good->mall_id = $mall_id;
  228. $stock_good->goods_id = $data['goods_id'];
  229. $stock_good->user_id = $data['user_id'];
  230. $stock_good->num = 0;
  231. $stock_good->save();
  232. }
  233. if ($changes_dir == CloudStockTwoEnum::STOCK_CHANGE_SUB) {
  234. if ($data['number'] > $stock_good['num']) {
  235. throw new \Exception('商品库存不足');
  236. }
  237. }
  238. self::$good = $good;
  239. self::$stock_good = $stock_good;
  240. return true;
  241. }
  242. /**
  243. * @name:
  244. * @msg: 获取共轭改变类型。例如:当前会员的改变类型是给下级配货(201),则其共轭改变类型就是 上级给自己配货(101)
  245. * @param {int} $change_type
  246. * @return {*}
  247. */
  248. private static function adjointChangeType(int $changes_type = null)
  249. {
  250. $adjoint_change_type_map = [
  251. CloudStockTwoEnum::STOCK_CHANGE_TYPE100 => CloudStockTwoEnum::STOCK_CHANGE_TYPE202,
  252. CloudStockTwoEnum::STOCK_CHANGE_TYPE101 => CloudStockTwoEnum::STOCK_CHANGE_TYPE201,
  253. CloudStockTwoEnum::STOCK_CHANGE_TYPE102 => 0,
  254. CloudStockTwoEnum::STOCK_CHANGE_TYPE103 => 0,
  255. CloudStockTwoEnum::STOCK_CHANGE_TYPE104 => 0,
  256. CloudStockTwoEnum::STOCK_CHANGE_TYPE105 => 0,
  257. CloudStockTwoEnum::STOCK_CHANGE_TYPE200 => 0,
  258. CloudStockTwoEnum::STOCK_CHANGE_TYPE201 => CloudStockTwoEnum::STOCK_CHANGE_TYPE101,
  259. CloudStockTwoEnum::STOCK_CHANGE_TYPE202 => CloudStockTwoEnum::STOCK_CHANGE_TYPE100,
  260. CloudStockTwoEnum::STOCK_CHANGE_TYPE203 => 0,
  261. CloudStockTwoEnum::STOCK_CHANGE_TYPE204 => 0,
  262. ];
  263. if (!empty($changes_type)) $adjoint_change_type_map = $adjoint_change_type_map[$changes_type];
  264. return $adjoint_change_type_map;
  265. }
  266. /**
  267. * @name:
  268. * @msg: 获取共轭变动方向
  269. * @param {int} $changes_dir 当前变动方向
  270. * @return mixed
  271. */
  272. private static function adjointChangeDir(int $changes_dir = null)
  273. {
  274. $adjoint_change_dir_map = [
  275. CloudStockTwoEnum::STOCK_CHANGE_ADD => CloudStockTwoEnum::STOCK_CHANGE_SUB,
  276. CloudStockTwoEnum::STOCK_CHANGE_SUB => CloudStockTwoEnum::STOCK_CHANGE_ADD,
  277. ];
  278. if (!empty($changes_dir)) $adjoint_change_dir_map = $adjoint_change_dir_map[$changes_dir];
  279. return $adjoint_change_dir_map;
  280. }
  281. /**
  282. * @name:
  283. * @msg: 代理商库存处理并记录明细
  284. * @param {int} $mall_id
  285. * @param {array} $data goods_id:商品id,user_id:需要处理的会员id;order_id:订单id,非必需;order_type:订单类型,非必需;number:商品变动数量;
  286. * @param {int} $changes_dir
  287. * @param {int} $changes_type
  288. * @return {*}
  289. */
  290. private static function agentUserSaveLog(int $mall_id, array $data, int $changes_dir, int $changes_type)
  291. {
  292. // 当前会员库存明细记录 --- start ----
  293. $params_sub = [
  294. 'goods_id' => $data['goods_id'],
  295. 'changes_dir' => $changes_dir,
  296. 'user_id' => $data['user_id'],
  297. 'order_id' => $data['order_id'] ?? 0,
  298. 'order_type' => $data['order_type'] ?? 0,
  299. 'changes_type' => $changes_type,
  300. 'num' => $data['number'],
  301. 'status' => StatusEnum::ENABLED,
  302. ];
  303. $res_save_log = self::setData($mall_id, $params_sub);
  304. if (false === $res_save_log) {
  305. throw new \Exception(self::$static_error);
  306. }
  307. // 当前会员减库存明细记录 --- end ----
  308. // 当前会员库存增减明细表数据记录 --- start ---
  309. // unset($stock_good);
  310. $stock_good = CloudStockTwoAgentGoods::findOne(['mall_id' => $mall_id, 'user_id' => $data['user_id'], 'goods_id' => $data['goods_id'], 'status' => StatusEnum::ENABLED]);
  311. if (empty($stock_good)) {
  312. $msg = '代理商商品未找到';
  313. if ($data['user_id'] == $data['target_user_id']) {
  314. $msg = '需要下级代理商进货过该商品之后才可以配货';
  315. }
  316. // throw new \Exception($msg);
  317. }
  318. // $msg = __METHOD__ . ' ======= mall_id => ' . $mall_id . ', data => ' . var_export($data, true) . ', changes_dir => ' . $changes_dir . ', changes_type => ' . $changes_type . ', stock_good->num => ' . $stock_good->num . PHP_EOL;
  319. // \Yii::error($msg);\Yii::getLogger()->flush(true);
  320. if (CloudStockTwoEnum::STOCK_CHANGE_SUB == $changes_dir) {
  321. // 减库存
  322. $good_change_number = max($stock_good->num - $data['number'], 0);
  323. $deduction_params = [
  324. 'user_id' => $data['user_id'],
  325. 'num' => $data['number'],
  326. 'mall_id'=>$mall_id,
  327. 'goods_id' => $data['goods_id'],
  328. 'order_id' => $data['order_id'] ?? 0,
  329. 'order_type' => $data['order_type'] ?? 0,
  330. 'changes_type' => $changes_type,
  331. 'not_yet_num' => $good_change_number,
  332. 'stock_log_id' => $res_save_log->id,
  333. 'status' => StatusEnum::ENABLED,
  334. 'remark' => $data['remark'] ?? '',
  335. ];
  336. $res_deduct = CloudStockTwoDeductionDetail::setData($deduction_params);
  337. if (false === $res_deduct) {
  338. throw new \Exception(self::$static_error);
  339. }
  340. } else {
  341. // 加库存
  342. $good_change_number = $stock_good->num + $data['number'];
  343. $add_params = [
  344. 'user_id' => $data['user_id'],
  345. 'num' => $data['number'],
  346. 'mall_id'=>$mall_id,
  347. 'goods_id' => $data['goods_id'],
  348. 'order_id' => $data['order_id'] ?? 0,
  349. 'order_type' => $data['order_type'] ?? 0,
  350. 'changes_type' => $changes_type,
  351. 'stock_log_id' => $res_save_log->id,
  352. 'status' => StatusEnum::ENABLED,
  353. 'remark' => $data['remark'] ?? '',
  354. ];
  355. $res_add = CloudStockTwoIncreaseDetail::setData($add_params);
  356. if (false === $res_add) {
  357. throw new \Exception(self::$static_error);
  358. }
  359. }
  360. // 当前会员库存增减明细表数据记录 --- end ---
  361. // 当前会员代理商品记录表相应商品数量变动更新 ---- start -----
  362. if (empty($stock_good)) {
  363. $update_stock_good_params = [
  364. 'user_id' => $data['user_id'],
  365. 'goods_id' => $data['goods_id'],
  366. 'num' => $data['number'],
  367. ];
  368. } else {
  369. $update_stock_good_params = [
  370. 'id' => $stock_good->id,
  371. 'num' => $good_change_number,
  372. ];
  373. }
  374. $res_agenuser_update = CloudStockTwoAgentGoods::setData($mall_id, $update_stock_good_params);
  375. if (false === $res_agenuser_update) {
  376. throw new \Exception(self::$static_error);
  377. }
  378. // 当前会员代理商品记录表相应商品数量变动更新 ---- end -----
  379. unset($res_save_log);
  380. return true;
  381. }
  382. }