GhsController.php 5.7 KB

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