Sales.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php namespace Pinterface\Api;
  2. use Dever;
  3. use Pinterface\Lib\Core;
  4. class Sales extends Core
  5. {
  6. protected $login = true;
  7. protected $entry = true;
  8. protected $info;
  9. protected $data = [];
  10. public function init()
  11. {
  12. $this->data['sales_type'] = Dever::input('sales_type', 'is_numeric', '销售渠道类型');
  13. $this->data['sales_id'] = Dever::input('sales_id', 'is_numeric', '销售渠道ID');
  14. $this->info = Dever::load(\Psales\Lib\Info::class)->get($this->data['sales_type'], $this->data['sales_id']);
  15. if ($this->info['uid'] != $this->place->uid) {
  16. Dever::error('您没有权限', -3);
  17. }
  18. $this->info['mobile'] = \Dever\Helper\Str::hide($this->info['mobile']);
  19. if (!$this->info['info']) {
  20. $this->info['info'] = $this->place->info['info'];
  21. }
  22. }
  23. # 销售渠道信息
  24. public function info()
  25. {
  26. $data['user'] = $this->place->user;
  27. $data['info'] = $this->info;
  28. $data['desc'] = $this->info['info'];
  29. # 获取权益
  30. $data['benefit'] = '';
  31. $benefit = Dever::db('pbenefit/sales')->find(['sales_cate_id' => $this->info['cate_id'], 'status' => 1]);
  32. if ($benefit && $benefit['desc']) {
  33. $data['benefit'] = htmlspecialchars_decode($benefit['desc']);
  34. }
  35. # 用户数
  36. $where = $this->data;
  37. $where['status'] = 1;
  38. $data['member'] = Dever::db('puser/info')->count($this->data);
  39. # 身份数
  40. $data['role'] = [];
  41. # 身份列表
  42. $role = Dever::db('prole/info')->select(['status' => 1]);
  43. if ($role) {
  44. $roleWhere = $this->data;
  45. foreach ($role as $k => $v) {
  46. $level = Dever::db('prole/level')->find(['info_id' => $v['id'], 'status' => 1]);
  47. if ($level) {
  48. $roleWhere['info_id'] = $v['id'];
  49. $roleWhere['status'] = 1;
  50. $num = Dever::db('prole/user')->count($roleWhere);
  51. $data['role'][] = ['id' => $v['id'], 'name' => $v['name'], 'num' => $num];
  52. }
  53. }
  54. }
  55. # 订单数
  56. unset($where['status']);
  57. $data['order'] = Dever::db('porder/source')->count($where);
  58. $money = $this->place->money();
  59. # 收益
  60. $data['profit'] = Dever::db('psales/profit')->sum($where, 'money') . $money['unit'];
  61. # 流水
  62. $where['status'] = ['in', '2,3,4,5,6'];
  63. $data['cash'] = Dever::db('porder/source')->sum($where, 'money_cash') . $money['unit'];
  64. # 获取待付款
  65. $where['status'] = 1;
  66. $data['order_1'] = Dever::db('porder/source')->count($where);
  67. # 待发货
  68. $where['status'] = 2;
  69. $data['order_2'] = Dever::db('porder/source')->count($where);
  70. # 待收货
  71. $where['status'] = ['in', '3,4'];
  72. $data['order_3'] = Dever::db('porder/source')->count($where);
  73. # 已完成
  74. $where['status'] = ['in', '5,6'];
  75. $data['order_4'] = Dever::db('porder/source')->count($where);
  76. # 有退款
  77. unset($where['status']);
  78. $where['refund_status'] = 1;
  79. $data['order_5'] = Dever::db('porder/source')->count($where);
  80. return $data;
  81. }
  82. # 店铺用户列表
  83. public function getUser()
  84. {
  85. $this->data['status'] = 1;
  86. $data['list'] = Dever::db('puser/info')->select($this->data, ['num' => 10, 'col' => 'id,name,avatar,num_order,cdate']);
  87. if ($data['list']) {
  88. foreach ($data['list'] as &$v) {
  89. $v['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']);
  90. $v['order'] = $v['num_order'] . '单';
  91. }
  92. }
  93. $data['page'] = Dever::page('total');
  94. return $data;
  95. }
  96. # 店铺身份用户列表
  97. public function getRoleUser()
  98. {
  99. $id = Dever::input('id', 'is_numeric', '身份');
  100. $this->data['info_id'] = $id;
  101. $this->data['status'] = 1;
  102. $data['info'] = $info = Dever::db('prole/info')->find(['id' => $id, 'status' => 1], ['col' => 'id,name,info,score_id']);
  103. $user = Dever::db('prole/user')->select($this->data, ['num' => 10, 'col' => '*']);
  104. $data['list'] = [];
  105. if ($user) {
  106. foreach ($user as $k => $v) {
  107. $data['list'][$k] = Dever::db('puser/info')->find($v['uid'], ['col' => 'id,name,avatar,num_order']);
  108. if ($data['list'][$k]) {
  109. $data['list'][$k]['cdate_str'] = date('Y-m-d H:i:s', $v['cdate']);
  110. # 获取订单数
  111. $data['list'][$k]['order'] = $data['list'][$k]['num_order'] . '单';
  112. }
  113. }
  114. }
  115. $data['page'] = Dever::page('total');
  116. return $data;
  117. }
  118. # 店铺订单列表
  119. public function getOrder()
  120. {
  121. $status = Dever::input('status');
  122. $data = Dever::load(\Porder\Lib\Source\Order::class)->getList($this->place->uid, $status, $this->data['sales_type'], $this->data['sales_id']);
  123. return ['list' => $data];
  124. }
  125. # 店铺订单详情
  126. public function getOrderView()
  127. {
  128. $info = $this->getOrderInfo();
  129. $info = Dever::load(\Porder\Lib\Source\Order::class)->getInfo($info, true);
  130. $data = ['info' => $info, 'refund_desc_type' => Dever::db('porder/source_refund')->value('desc_type'), 'refund_type' => Dever::db('porder/source_refund')->value('type')];
  131. $data['express'] = [Dever::call("Sector/Lib/Delivery.getList", 1)];
  132. $data['virtual'] = [Dever::call("Sector/Lib/Delivery.getList", 2)];
  133. # 获取退货地址
  134. if ($data['info']['refund_status'] == 1 && isset($data['info']['refund']) && $data['info']['refund'] && $data['info']['refund']['status'] == 1 && $data['info']['refund']['type'] == 1 && $data['info']['method'] > 1) {
  135. //$data['address'] = Dever::load('address', 'place')->getList();
  136. $name = $this->info['name'];
  137. $data['address'][] = ['id' => 'sales_' . $this->data['sales_type'] . '_' . $this->data['sales_id'], 'name' => $name];
  138. $data['address'] = [$data['address']];
  139. }
  140. return $data;
  141. }
  142. # 获取订单信息
  143. private function getOrderInfo()
  144. {
  145. $id = Dever::input('order_id');
  146. if (!$id) {
  147. Dever::error('订单信息错误');
  148. }
  149. $info = Dever::db('porder/source')->find(['id' => $id, 'sales_type' => $this->data['sales_type'], 'sales_id' => $this->data['sales_id']]);
  150. if (!$info) {
  151. Dever::error('订单信息有误');
  152. }
  153. return $info;
  154. }
  155. # 确认自提
  156. public function setPickup()
  157. {
  158. $info = $this->getOrderInfo();
  159. $code = Dever::input('code', 'is_numeric', '自提码');
  160. $info['code'] = substr($info['cdate'], -4);
  161. if ($info['code'] != $code) {
  162. Dever::error('自提码错误');
  163. }
  164. Dever::load(\Porder\Lib\Source\Order::class)->finish($this->data['sales_type'], $info, true, $this->data['sales_id']);
  165. return 'ok';
  166. }
  167. # 发货
  168. public function setDelivery()
  169. {
  170. $info = $this->getOrderInfo();
  171. $detail_id = Dever::input('detail_id', 'is_numeric', '订单信息');
  172. $delivery_id = Dever::input('delivery_id', 'is_numeric', '发货信息');
  173. $content = Dever::input('content', 'is_string', '发货信息');
  174. Dever::load(\Porder\Lib\Source\Delivery::class)->up($info['id'], $detail_id, $delivery_id, $content, $this->data['sales_type']+10, $this->data['sales_id']);
  175. return 'ok';
  176. }
  177. # 审核退款
  178. public function setRefund()
  179. {
  180. $data = $this->getOrderView();
  181. if ($data['info']['refund_status'] == 1 && isset($data['info']['refund']) && $data['info']['refund']) {
  182. $type = Dever::input('type', 'is_numeric', '审核类型');
  183. $desc = Dever::input('desc');
  184. $address_id = Dever::input('address_id');
  185. if ($type == 2 && !$desc) {
  186. Dever::error('审核备注不能为空');
  187. }
  188. if ($type == 1 && isset($data['address']) && $data['address']) {
  189. if (!$address_id) {
  190. Dever::error('请选择退货地址');
  191. }
  192. }
  193. Dever::load(\Porder\Lib\Source\Refund::class)->audit($data['info']['refund']['id'], $type, $desc, $address_id, $this->data['sales_type']+10, $this->data['sales_id']);
  194. return 'ok';
  195. } else {
  196. Dever::error('审核失败');
  197. }
  198. }
  199. # 审核退货
  200. public function setRefundDelivery()
  201. {
  202. $data = $this->getOrderView();
  203. if ($data['info']['refund_status'] == 1 && isset($data['info']['refund']) && $data['info']['refund']) {
  204. $type = Dever::input('type', 'is_numeric', '审核类型');
  205. $desc = Dever::input('desc');
  206. if ($type == 2 && !$desc) {
  207. Dever::error('审核备注不能为空');
  208. }
  209. Dever::load(\Porder\Lib\Source\Refund::class)->auditDelivery($data['info']['refund']['id'], $type, $desc, $this->data['sales_type']+10, $this->data['sales_id']);
  210. return 'ok';
  211. } else {
  212. Dever::error('审核失败');
  213. }
  214. }
  215. # 店铺订单发货、退款、代完成等
  216. # 店铺流水列表
  217. public function getOrderCash()
  218. {
  219. }
  220. # 店铺收益列表
  221. public function getProfit()
  222. {
  223. $where = $this->data;
  224. $data['money'] = $this->place->money();
  225. $data['cash'] = Dever::db('psales/profit')->sum($where, 'money') . $data['money']['unit'];
  226. $data['list'] = Dever::db('psales/profit')->select($where, ['num' => 10]);
  227. if ($data['list']) {
  228. foreach ($data['list'] as &$v) {
  229. $log = Dever::db('pscore/user_log')->find($v['user_log_id']);
  230. $v['desc'] = $log['desc'];
  231. $v['cdate'] = date('Y-m-d H:i:s', $v['cdate']);
  232. $info = Dever::db('pscore/action')->find($v['action_id']);
  233. $v['action_name'] = $info['name'];
  234. $v['amount'] = Dever::load(\Pscore\Lib\Info::class)->getText($v['amount'], $v['score_id']);
  235. $v['money'] .= $data['money']['unit'];
  236. }
  237. }
  238. return $data;
  239. }
  240. # 店铺权益列表 暂时不做
  241. public function getBenefit()
  242. {
  243. }
  244. # 获取店铺信息
  245. public function getInfo()
  246. {
  247. $this->info['content'] = htmlspecialchars_decode($this->info['content']);
  248. if (isset($this->info['area'])) {
  249. $this->info['area_string'] = Dever::load(\Area\Lib\Data::class)->string($this->info['area'], '-');
  250. $this->info['area'] = explode(',', 'CN,' . $this->info['area']);
  251. }
  252. return $this->info;
  253. }
  254. # 更新信息
  255. public function update()
  256. {
  257. $update = [];
  258. $name = Dever::input('name', 'is_string', '名称');
  259. $truename = Dever::input('truename', 'is_string', '联系人');
  260. $this->info['name'] = $update['name'] = $name;
  261. $this->info['truename'] = $update['truename'] = $truename;
  262. $logo = Dever::input('logo');
  263. if ($logo) {
  264. $this->info['logo'] = $update['logo'] = $logo;
  265. }
  266. $info = Dever::input('info');
  267. if ($info) {
  268. $this->info['info'] = $update['info'] = $info;
  269. }
  270. $content = Dever::input('content');
  271. if ($content) {
  272. $this->info['content'] = $update['content'] = $content;
  273. }
  274. $open = Dever::input('open');
  275. if ($open) {
  276. $this->info['open'] = $update['open'] = $open;
  277. }
  278. $worktime = Dever::input('worktime');
  279. if ($worktime) {
  280. $this->info['worktime'] = $update['worktime'] = $worktime;
  281. }
  282. if (isset($this->info['area'])) {
  283. $province = Dever::input('province_id', 'is_numeric', '省份');
  284. $city = Dever::input('city_id', 'is_numeric', '城市');
  285. $county = Dever::input('county_id', 'is_numeric', '区县');
  286. $town = Dever::input('town_id', 'is_numeric', '街镇');
  287. $address = Dever::input('address', 'is_string', '地址');
  288. $this->info['province_id'] = $update['province_id'] = $province;
  289. $this->info['city_id'] = $update['city_id'] = $city;
  290. $this->info['county_id'] = $update['county_id'] = $county;
  291. $this->info['town_id'] = $update['town_id'] = $town;
  292. if ($province && $city && $county && $town) {
  293. $update['area'] = $province . ',' . $city . ',' . $county . ',' . $town;
  294. }
  295. $this->info['address'] = $update['address'] = $address;
  296. }
  297. if ($update) {
  298. $table = Dever::load(\Psales\Lib\Info::class)->getType($this->data['sales_type'])['table'];
  299. Dever::load(\Place\Lib\Info::class)->update($this->place['id']);
  300. Dever::db('psales/' . $table)->update($this->data['sales_id'], $update);
  301. }
  302. return $this->getInfo();
  303. }
  304. }