GhsController.php 6.3 KB

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