GhsController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\sj\classes\SjClass;
  4. use bizGhs\ghs\classes\GhsClass;
  5. use bizHd\saas\classes\ApplyClass;
  6. use bizHd\saas\services\ApplyService;
  7. use common\components\dict;
  8. use common\components\imgUtil;
  9. use common\components\orderSn;
  10. use common\components\stringUtil;
  11. use Yii;
  12. use common\components\util;
  13. use bizGhs\order\classes\PurchaseOrderClass;
  14. class GhsController extends BaseController
  15. {
  16. public function actionModifyName()
  17. {
  18. $get = Yii::$app->request->get();
  19. $id = $get['id'] ?? 0;
  20. $name = $get['name'] ?? '';
  21. $ghs = GhsClass::getById($id, true);
  22. if (empty($ghs)) {
  23. util::fail('没有找到供货商');
  24. }
  25. if ($ghs->ownMainId != $this->mainId) {
  26. util::fail('不是你的供货商');
  27. }
  28. if (empty($name)) {
  29. util::fail('请填写名称');
  30. }
  31. if (stringUtil::getWordNum($name) > 27) {
  32. util::fail('名称不能超过27个汉字');
  33. }
  34. $ghs->name = $name;
  35. $ghs->save();
  36. util::complete('修改成功');
  37. }
  38. //添加供货商 ssh 20210611
  39. public function actionAdd()
  40. {
  41. $post = Yii::$app->request->post();
  42. $location = $post['location'] ?? '';
  43. $mobile = $post['mobile'] ?? '';
  44. $name = $post['name'] ?? '';
  45. if (empty($name)) {
  46. util::fail('名称不能为空');
  47. }
  48. if (stringUtil::getWordNum($name) > 27) {
  49. util::fail('名称不能超过27个汉字');
  50. }
  51. $hasGhsList = GhsClass::getAllByCondition(['ownShopId' => $this->shopId], null, '*', null, true);
  52. if (!empty($hasGhsList)) {
  53. foreach ($hasGhsList as $hasGhs) {
  54. $hasGhsName = $hasGhs->name ?? '';
  55. if ($hasGhsName == $name) {
  56. util::fail('名称已经存在了');
  57. }
  58. }
  59. }
  60. if (!empty($mobile)) {
  61. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  62. if (!empty($has)) {
  63. $connection = Yii::$app->db;
  64. $transaction = $connection->beginTransaction();
  65. try {
  66. $shopId = $this->shopId;
  67. $currentShopId = $has['shopId'] ?? 0;
  68. if (empty($currentShopId)) {
  69. util::fail('添加失败');
  70. }
  71. $hasGhs = GhsClass::getByCondition(['ownShopId' => $this->shopId, 'shopId' => $currentShopId], true);
  72. if (!empty($hasGhs)) {
  73. $name = $hasGhs->name ?? '';
  74. util::fail("手机已添加过了,供货商名称:【{$name}】");
  75. }
  76. $ghs = \biz\ghs\classes\GhsClass::build($currentShopId, $shopId, 1);
  77. $ghs['avatar'] = imgUtil::groupImg($ghs['avatar']);
  78. $transaction->commit();
  79. util::success($ghs);
  80. } catch (\Exception $exception) {
  81. $transaction->rollBack();
  82. Yii::info("添加供货商没有成功:" . $exception->getMessage());
  83. util::fail('添加供货商没有成功');
  84. }
  85. util::fail('添加失败,请联系管理员');
  86. }
  87. }
  88. $connection = Yii::$app->db;
  89. $transaction = $connection->beginTransaction();
  90. try {
  91. if (empty($mobile)) {
  92. //获取虚拟手机号
  93. $mobile = orderSn::getMobileSn();
  94. }
  95. $sjId = $this->sjId;
  96. $shopId = $this->shopId;
  97. $sjName = $this->sj->name ?? '';
  98. $address = $post['address'] ?? '';
  99. $applyData = [
  100. 'name' => $name,
  101. 'licenseNo' => $mobile,
  102. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  103. 'mobile' => $mobile,
  104. 'introSjId' => $sjId,
  105. 'introSjName' => $sjName,
  106. 'introShopId' => $shopId,
  107. 'introStyle' => SjClass::STYLE_SUPPLIER,
  108. 'from' => ApplyClass::FROM_GHS,
  109. 'style' => dict::getDict('ptStyle', 'kmGhs'),
  110. 'address' => $address,
  111. 'status' => ApplyClass::STATUS_PASS,
  112. ];
  113. $apply = ApplyService::replaceKmGhs($applyData);
  114. $id = $apply['id'] ?? 0;
  115. $respond = \bizJd\apply\services\ApplyService::pass($id);
  116. $shop = $respond['shop'] ?? [];
  117. $sj = $respond['sj'] ?? [];
  118. $currentSjId = $sj['id'] ?? 0;
  119. $currentShopId = $shop->id ?? 0;
  120. ApplyClass::updateById($id, ['sjId' => $currentSjId, 'shopId' => $currentShopId]);
  121. if (empty($currentShopId)) {
  122. util::fail('供货商没有添加成功!');
  123. }
  124. $ghs = \biz\ghs\classes\GhsClass::build($currentShopId, $shopId, 1);
  125. $ghs['avatar'] = imgUtil::groupImg($ghs['avatar']);
  126. if (is_numeric($location)) {
  127. //本地批发商的标记
  128. $ghsId = $ghs['id'] ?? 0;
  129. \biz\ghs\classes\GhsClass::updateById($ghsId, ['location' => $location]);
  130. }
  131. $transaction->commit();
  132. util::success($ghs);
  133. } catch (\Exception $exception) {
  134. $transaction->rollBack();
  135. Yii::info("添加供货商没有成功:" . $exception->getMessage());
  136. util::fail('添加供货商没有成功');
  137. }
  138. }
  139. //供货商列表 ssh 2021.1.18
  140. public function actionList()
  141. {
  142. $get = Yii::$app->request->get();
  143. $name = isset($get['name']) && !empty($get['name']) ? $get['name'] : '';
  144. $where = ['ownShopId' => $this->shopId];
  145. if (!empty($name)) {
  146. if (stringUtil::isLetter($name)) {
  147. $where['py'] = ['like', $name];
  148. } else {
  149. $where['name'] = ['like', $name];
  150. }
  151. }
  152. $location = $get['location'] ?? '';
  153. if (is_numeric($location)) {
  154. $where['location'] = $location;
  155. }
  156. $list = GhsClass::getGhsList($where);
  157. //累计总欠款
  158. $totalDebtAmount = PurchaseOrderClass::sum(['mainId' => $this->mainId, 'status' => 4, 'debt' => PurchaseOrderClass::DEBT_YES], 'actPrice');
  159. $list['totalDebtAmount'] = $totalDebtAmount;
  160. util::success($list);
  161. }
  162. }