PluginSettleController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/10/11
  6. * Time: 13:47
  7. */
  8. namespace app\frontend\modules\finance\controllers;
  9. use app\backend\modules\finance\models\Advertisement;
  10. use app\backend\modules\income\Income;
  11. use app\common\components\ApiController;
  12. use app\common\exceptions\AppException;
  13. use app\frontend\modules\finance\services\PluginSettleService;
  14. use Illuminate\Support\Facades\DB;
  15. use Yunshop\StoreCashier\common\services\AdvertisementService;
  16. class PluginSettleController extends ApiController
  17. {
  18. public function pluginList()
  19. {
  20. $list = [];
  21. $config = \app\backend\modules\income\Income::current()->getItems();
  22. foreach ($config as $key => $value) {
  23. $list[] = $this->available($key, $value);
  24. }
  25. $list = array_filter($list);
  26. sort($list);
  27. if ($list) {
  28. return $this->successJson('获取数据成功', $list);
  29. }
  30. return $this->errorJson('未开启手动结算');
  31. }
  32. protected function available($key, $value)
  33. {
  34. $lang = \Setting::get('shop.lang', ['lang' => 'zh_cn'])['zh_cn'];
  35. $arr = [];
  36. switch ($key) {
  37. case 'merchant':
  38. if (\Setting::get('plugin.merchant.settlement_model')) {
  39. $arr = [
  40. 'title' => $value['title'],
  41. 'type' => $value['type'],
  42. 'amount'=> $value['class']::getNotSettleAmount(\YunShop::app()->getMemberId()),
  43. 'api' => 'finance.plugin-settle.plugin-merchant',
  44. 'icon' => 'income_d',
  45. ];
  46. }
  47. break;
  48. case 'commission':
  49. if (\Setting::get('plugin.commission.settlement_model')) {
  50. $arr = [
  51. 'title' => $value['title'],
  52. 'type' => $value['type'],
  53. 'amount'=> $value['class']::getNotSettleAmount(\YunShop::app()->getMemberId()),
  54. 'api' => 'finance.plugin-settle.plugin-commission',
  55. 'icon' => 'income_a',
  56. ];
  57. }
  58. break;
  59. case 'areaDividend':
  60. if (\Setting::get('plugin.area_dividend.settlement_model')) {
  61. $arr = [
  62. 'title' => $lang['area_dividend']['title']?:$value['title'],
  63. 'type' => $value['type'],
  64. 'amount'=> $value['class']::getNotSettleAmount(\YunShop::app()->getMemberId()),
  65. 'api' => 'finance.plugin-settle.plugin-area-dividend',
  66. 'icon' => 'income_c',
  67. ];
  68. }
  69. break;
  70. case 'teamDividend':
  71. if (\Setting::get('plugin.team_dividend.settlement_model')) {
  72. $arr = [
  73. 'title' => $lang['team_dividend']['title']?:$value['title'],
  74. 'type' => $value['type'],
  75. 'amount'=> $value['class']::getNotSettleAmount(\YunShop::app()->getMemberId()),
  76. 'api' => 'finance.plugin-settle.plugin-team-dividend',
  77. 'icon' => 'income_b',
  78. ];
  79. }
  80. break;
  81. case 'weeklyRewards':
  82. $week = array_pluck(\Setting::getAllByGroup('weekly-rewards')->toArray(), 'value', 'key');
  83. if ($week['dividend_settle'] || $week['area_settle']) {
  84. $arr = [
  85. 'title' => $value['title'],
  86. 'type' => $value['type'],
  87. 'amount'=> $value['class']::getNotSettleAmount(\YunShop::app()->getMemberId()),
  88. 'api' => 'finance.plugin-settle.plugin-weekly-rewards',
  89. 'icon' => 'income_e',
  90. ];
  91. }
  92. break;
  93. default:
  94. $arr = [];
  95. }
  96. return $arr;
  97. }
  98. //获取插件可结算佣金列表
  99. public function getNotSettleList()
  100. {
  101. $type = \YunShop::request()->plugin_type;
  102. $member_id = \YunShop::app()->getMemberId();
  103. if (empty($type) || empty($member_id)) {
  104. throw new AppException('参数错误');
  105. }
  106. $plugin_income = Income::current()->getItem($type);
  107. $class = array_get($plugin_income,'class');
  108. $function = 'getNotSettleInfo';
  109. if(class_exists($class) && method_exists($class,$function) && is_callable([$class,$function])){
  110. $result = $class::$function(['member_id' => $member_id])->orderBy('id', 'desc')->paginate(20);
  111. } else {
  112. throw new AppException('不存在的类');
  113. }
  114. if ($result->isEmpty()) {
  115. return $this->errorJson('暂无数据', []);
  116. }
  117. $data_processing = PluginSettleService::create($type);
  118. if (is_null($data_processing)) {
  119. throw new AppException('数据处理出错');
  120. }
  121. $data = [
  122. 'type' => $type,
  123. 'total' => $result->total(),
  124. 'per_page' => $result->perPage(),
  125. 'last_page' => $result->lastPage(),
  126. 'data' => $data_processing->sameFormat($result),
  127. ];
  128. return $this->successJson('获取数据成功', $data);
  129. }
  130. public function incomeReceive()
  131. {
  132. $id = intval(\YunShop::request()->id);
  133. $type = \YunShop::request()->plugin_type;
  134. if (empty($type) || empty($id)) {
  135. throw new AppException('参数错误');
  136. }
  137. $plugin_income = Income::current()->getItem($type);
  138. $class = array_get($plugin_income,'class');
  139. $function = 'getNotSettleInfo';
  140. if(class_exists($class) && method_exists($class,$function) && is_callable([$class,$function])){
  141. $result = $class::$function(['id' => $id])->first();
  142. } else {
  143. throw new AppException('不存在的类');
  144. }
  145. if (empty($result)) {
  146. throw new AppException('记录不存在');
  147. }
  148. //修改为已结算,加入收入
  149. $data_processing = PluginSettleService::create($type);
  150. $income_data = $data_processing->getAdvFormat($result);
  151. if (app('plugins')->isEnabled('store-cashier')) {
  152. $info = AdvertisementService::getStoreAdv($income_data);
  153. if ($info['status'] == 1) {
  154. $info['income_data'] = $income_data;
  155. return $this->successJson('成功', $info);
  156. }
  157. }
  158. return $this->successJson('成功', $this->getShopAdv($income_data));
  159. }
  160. protected function getShopAdv($income_data)
  161. {
  162. $adv = Advertisement::getOneData()->first();
  163. $lat = \YunShop::request()->lat;
  164. $lng = \YunShop::request()->lng;
  165. if ((!empty($lat) && !is_numeric($lat)) || (!empty($lng) && !is_numeric($lng))) {
  166. throw new AppException('参数错误');
  167. }
  168. if ($lat && $lng) {
  169. $res = Advertisement::uniacid()
  170. ->select(DB::raw("id, thumb, adv_url, longitude, latitude,(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$lat} * pi()) / 180) / 2), 2) + cos(({$lat} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$lng} * pi()) / 180) / 2), 2))))) as distance"))
  171. ->Status()
  172. ->where('area_open',1)
  173. ->orderBy('distance', 'asc')
  174. ->orderBy('sort_by', 'desc')
  175. ->first();
  176. if ($res) $adv = $res;
  177. }
  178. return [
  179. 'status' => 1,
  180. 'income_data' => $income_data,
  181. 'adv_thumb' => $adv->thumb ? yz_tomedia($adv->thumb) : '',
  182. 'adv_url' => $adv->adv_url ? $adv->adv_url : '',
  183. 'type' => 'shop',
  184. ];
  185. }
  186. //招商分红
  187. public function pluginMerchant()
  188. {
  189. }
  190. //分销佣金
  191. public function pluginCommission()
  192. {
  193. }
  194. //经销商提成
  195. public function pluginTeamDividend()
  196. {
  197. // $member_id = \YunShop::app()->getMemberId();
  198. //
  199. // $config = \Config::get('income.merchant');
  200. }
  201. //区域分红
  202. public function pluginAreaDividend()
  203. {
  204. }
  205. //周度奖励
  206. public function pluginWeeklyRewards()
  207. {
  208. }
  209. }