Manage.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. namespace Agent\Lib;
  3. use Dever;
  4. class Manage
  5. {
  6. # 后台设置区域
  7. public function updateArea($id, $name, $data)
  8. {
  9. Dever::config('base')->hook = true;
  10. $area = Dever::param('area', $data);
  11. $role = Dever::param('role', $data);
  12. if ($area) {
  13. $member = Dever::db('agent/member')->find($id);
  14. $role = Dever::db('setting/role')->one($member['role']);
  15. if ($role && $role['type'] == 2) {
  16. Dever::load('area/api')->upStatus($member['area'], 1);
  17. }
  18. $role = Dever::db('setting/role')->one($role);
  19. if ($role && $role['type'] == 2) {
  20. $temp = explode(',', $area);
  21. $num = count($temp);
  22. if ($role['area'] == 1 && $num < 2) {
  23. Dever::alert('请选择城市');
  24. } elseif ($role['area'] == 2 && $num < 3) {
  25. Dever::alert('请选择区县');
  26. } elseif ($role['area'] == 3 && $num < 4) {
  27. Dever::alert('请选择街道');
  28. }
  29. Dever::load('area/api')->upStatus($area, 2);
  30. }
  31. }
  32. }
  33. # 获取
  34. # 设置上级
  35. public function setParent_commit($id, $name, $data)
  36. {
  37. Dever::config('base')->hook = true;
  38. $mid = Dever::param('mid', $data);
  39. $parent_mobile = trim(Dever::param('parent_mobile', $data));
  40. if ($mid && $parent_mobile) {
  41. $member = Dever::db('agent/member')->find($mid);
  42. $parent = Dever::db('agent/member')->find(array('mobile' => $parent_mobile));
  43. if ($parent && $parent['status'] == 2) {
  44. if ($member['parent_mid'] == $parent['id']) {
  45. Dever::alert('上级代理商设置失败');
  46. return;
  47. }
  48. if ($member['parent_mid'] == $mid) {
  49. Dever::alert('上级代理商不能设置为自己');
  50. return;
  51. }
  52. # 删除之前关系
  53. Dever::load('invite/api')->dropRelation($mid, $parent['id']);
  54. # 获取所有下级
  55. $child = Dever::load('invite/api')->getChild($mid);
  56. if ($child) {
  57. foreach ($child as $k => $v) {
  58. Dever::load('invite/api')->dropRelation($v['to_uid'], $parent['id']);
  59. }
  60. }
  61. # 重新设置关系
  62. Dever::load('invite/api')->setRelation($mid, $parent['id']);
  63. Dever::db('agent/member')->update(array('id' => $mid, 'parent_mid' => $parent['id']));
  64. } else {
  65. Dever::alert('上级代理商还未注册或者已封禁');
  66. }
  67. }
  68. }
  69. public function orderUpdate($id, $name, $data)
  70. {
  71. Dever::config('base')->hook = true;
  72. $audit = Dever::param('audit', $data);
  73. $status = Dever::param('status', $data);
  74. $desc = Dever::param('desc', $data);
  75. $order_id = Dever::param('order_id', $data);
  76. if ($order_id && $status) {
  77. if ($status < 3) {
  78. if ($audit == 1) {
  79. $update = array('where_id' => $order_id, 'status' => $status+1);
  80. if ($status == 2) {
  81. $order = Dever::db('agent/order')->find($order_id);
  82. $price = Dever::param('price', $data);
  83. $prize_type = Dever::param('prize_type', $data);
  84. if ($prize_type == 2) {
  85. # 扣减已有名额,判断一下
  86. $prize = Dever::db('setting/prize')->find(array('type' => 1, 'price' => $order['price']));
  87. if ($prize) {
  88. $member_goods = Dever::db('agent/member_goods')->getOne(array('mid' => $order['mid'], 'type' => 3, 'type_id' => $prize['id']));
  89. if ($member_goods && $member_goods['num'] > 0) {
  90. # 有名额
  91. $price = 0;
  92. # 开始扣减
  93. $member = Dever::db('agent/member')->find($order['mid']);
  94. Dever::load('agent/lib/dhorder')->action($member, $prize['name'], $prize['num'], $prize['price'], array(), -1, 2, 3, $desc);
  95. }
  96. }
  97. }
  98. if ($price <= 0) {
  99. $update['get_type'] = 2;
  100. } else {
  101. $update['get_type'] = 1;
  102. }
  103. $update['price'] = $price;
  104. $update['agent_cash'] = $price;
  105. $update['soft_cash'] = 0;
  106. $cstatus = Dever::param('cstatus', $data);
  107. if ($cstatus && $cstatus == 2 && $price > 0) {
  108. # 拆分代理费
  109. $role = Dever::db('setting/role')->one($order['role']);
  110. if ($role && $role['soft']) {
  111. if (strstr($role['soft'], '%')) {
  112. $role['soft'] = str_replace('%', '', $role['soft']);
  113. $role['soft'] = $price * ($role['soft']/100);
  114. }
  115. $update['agent_cash'] = $price - $role['soft'];
  116. $update['soft_cash'] = $role['soft'];
  117. }
  118. }
  119. $update['pay_status'] = 2;
  120. # 财务终审之后,先生成代理商
  121. if ($order['mid'] <= 0) {
  122. # 生成
  123. $where['mobile'] = $order['mobile'];
  124. $member = Dever::db('agent/member')->find($where);
  125. if ($member) {
  126. $update['mid'] = $member['id'];
  127. } else {
  128. $data = $where;
  129. $data['name'] = $order['name'];
  130. $data['idcard'] = $order['idcard'];
  131. $data['idcard_front'] = $order['idcard_front'];
  132. $data['idcard_back'] = $order['idcard_back'];
  133. $data['company_name'] = $order['company_name'];
  134. $data['company_pic'] = $order['company_pic'];
  135. $data['company_number'] = $order['company_number'];
  136. $data['role'] = 1;
  137. $data['parent_mid'] = $order['parent_mid'];
  138. $data['source_id'] = $order['source_id'];
  139. $update['mid'] = Dever::db('agent/member')->insert($data);
  140. Dever::load('invite/api')->setRelation($update['mid'], $data['parent_mid']);
  141. }
  142. }
  143. }
  144. Dever::db('agent/order')->update($update);
  145. } else {
  146. Dever::db('agent/order')->update(array('where_id' => $order_id, 'status' => 5));
  147. }
  148. } elseif ($status == 3) {
  149. $info = Dever::db('agent/order_process')->find($id);
  150. if ($info && $info['status'] == 3) {
  151. $info['type'] = explode(',', $info['type']);
  152. }
  153. if (in_array(2, $info['type'])) {
  154. $goods_status = 3;
  155. } else {
  156. $goods_status = 2;
  157. }
  158. Dever::db('agent/order')->update(array('where_id' => $order_id, 'fdate' => time(), 'status' => 4, 'goods_status' => $goods_status));
  159. $order = Dever::db('agent/order')->find($order_id);
  160. if ($order && $order['mid']) {
  161. $member = Dever::db('agent/member')->find($order['mid']);
  162. $update['where_id'] = $order['mid'];
  163. if ($member && $member['status'] == 1) {
  164. $update['status'] = 2;
  165. $update['rdate'] = time();
  166. }
  167. $update['area'] = $order['area'];
  168. $update['role'] = $order['role'];
  169. Dever::db('agent/member')->update($update);
  170. # 对区域进行占用
  171. $role = Dever::db('setting/role')->one($order['role']);
  172. if ($role && $role['type'] == 2) {
  173. Dever::load('area/api')->upStatus($order['area'], 2);
  174. }
  175. $area_data = array('mid' => $order['mid'], 'order_id' => $order['id'], 'role' => $order['role'], 'area' => $order['area']);
  176. $area = Dever::db('agent/member_area')->find($area_data);
  177. if (!$area) {
  178. $area_data['type'] = $order['get_type'];
  179. $area_data['price'] = $order['price'];
  180. Dever::db('agent/member_area')->insert($area_data);
  181. } else {
  182. if ($role && $role['type'] == 2) {
  183. Dever::load('area/api')->upStatus($area['area'], 1);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. # 发放权益
  191. public function set_commit_api()
  192. {
  193. Dever::config('base')->hook = true;
  194. $type = Dever::input('type', 2);
  195. $order_id = Dever::input('order_id');
  196. # 检测是否发放过了
  197. $order = Dever::db('agent/order')->find($order_id);
  198. if ($order['status'] != 3) {
  199. Dever::alert('无发放权限');
  200. }
  201. if ($order['mid'] <= 0) {
  202. $update = array('where_id' => $order_id);
  203. $where['mobile'] = $order['mobile'];
  204. $member = Dever::db('agent/member')->find($where);
  205. if ($member) {
  206. $update['mid'] = $member['id'];
  207. } else {
  208. $data = $where;
  209. $data['name'] = $order['name'];
  210. $data['idcard'] = $order['idcard'];
  211. $data['idcard_front'] = $order['idcard_front'];
  212. $data['idcard_back'] = $order['idcard_back'];
  213. $data['company_name'] = $order['company_name'];
  214. $data['company_pic'] = $order['company_pic'];
  215. $data['company_number'] = $order['company_number'];
  216. $data['role'] = 1;
  217. $data['parent_mid'] = $order['parent_mid'];
  218. $data['source_id'] = $order['source_id'];
  219. $update['mid'] = Dever::db('agent/member')->insert($data);
  220. Dever::load('invite/api')->setRelation($update['mid'], $data['parent_mid']);
  221. }
  222. Dever::db('agent/order')->update($update);
  223. $order['mid'] = $update['mid'];
  224. }
  225. $order_process = Dever::db('agent/order_process')->find(array('order_id' => $order_id, 'status' => 3));
  226. $yes = -1;
  227. if (!$order_process) {
  228. # 可以发放
  229. $yes = 1;
  230. } elseif ($order_process) {
  231. $order_process['type'] = explode(',', $order_process['type']);
  232. if (!in_array($type, $order_process['type'])) {
  233. # 可以发放
  234. $yes = 2;
  235. }
  236. }
  237. if ($yes > 0) {
  238. if ($yes == 1) {
  239. $data['order_id'] = $order_id;
  240. $data['status'] = 3;
  241. $data['type'] = '1,' . $type;
  242. $state = Dever::db('agent/order_process')->insert($data);
  243. } else {
  244. $data['where_id'] = $order_process['id'];
  245. $order_process['type'][] = $type;
  246. $data['type'] = implode(',', $order_process['type']);
  247. $state = Dever::db('agent/order_process')->update($data);
  248. }
  249. if ($state) {
  250. if ($type == 2) {
  251. $this->setGoods($order);
  252. } elseif ($type == 3) {
  253. $this->setCash($order);
  254. } elseif ($type == 5) {
  255. $this->setAgreement($order);
  256. }
  257. }
  258. return 'reload';
  259. } else {
  260. Dever::alert('无发放权限');
  261. }
  262. }
  263. # 设置多个权益
  264. private function setGoods($order)
  265. {
  266. $goods = Dever::db('agent/order_goods')->getData(array('order_id' => $order['id']));
  267. if (!$goods) {
  268. $goods = Dever::db('setting/role_goods')->getData(array('role_id' => $order['role'], 'give' => $order['order_type']));
  269. }
  270. if ($goods) {
  271. $where = array();
  272. foreach ($goods as $k => $v) {
  273. $this->addGoods($order, $v);
  274. }
  275. }
  276. }
  277. # 新增权益
  278. private function addGoods($order, $v)
  279. {
  280. $where['mid'] = $order['mid'];
  281. $where['type'] = $v['type'];
  282. $where['type_id'] = $v['type_id'];
  283. $where['sku_id'] = $v['sku_id'];
  284. $info = Dever::db('agent/member_goods')->find($where);
  285. if ($info) {
  286. $up['where_id'] = $info['id'];
  287. $up['total_num'] = $info['total_num'] + $v['num'];
  288. $state = Dever::db('agent/member_goods')->update($up);
  289. if ($state) {
  290. Dever::load('agent/lib/member_set')->log($order['mid'], $order['order_num'], $v['type'], $v['type_id'], $v['sku_id'], $v['num'], $info['total_num'], $up['total_num']);
  291. }
  292. } else {
  293. $up = $where;
  294. $up['total_num'] = $v['num'];
  295. $state = Dever::db('agent/member_goods')->insert($up);
  296. if ($state) {
  297. Dever::load('agent/lib/member_set')->log($order['mid'], $order['order_num'], $v['type'], $v['type_id'], $v['sku_id'], $v['num'], 0, $up['total_num']);
  298. }
  299. }
  300. }
  301. private function setCash($order)
  302. {
  303. $param = Dever::input('param');
  304. if ($param) {
  305. $param = explode(',', $param);
  306. foreach ($param as $k => $v) {
  307. $t = explode('-', $v);
  308. $where['type'] = $t[0];
  309. $where['order_id'] = $order['id'];
  310. $where['mid'] = $order['mid'];
  311. $where['parent_mid'] = $t[2];
  312. $info = Dever::db('agent/order_cash')->find($where);
  313. if (!$info) {
  314. $insert = $where;
  315. $insert['cash'] = $t[3];
  316. $insert['per'] = $t[5];
  317. if (isset($t[6])) {
  318. $insert['prize_id'] = $t[6];
  319. }
  320. Dever::db('agent/order_cash')->insert($insert);
  321. } else {
  322. $update['where_id'] = $info['id'];
  323. $update['cash'] = $t[3];
  324. $update['per'] = $t[5];
  325. if (isset($t[6])) {
  326. $update['prize_id'] = $t[6];
  327. }
  328. Dever::db('agent/order_cash')->update($update);
  329. }
  330. if (isset($t[6]) && isset($t[7])) {
  331. $prize_info = Dever::db('setting/prize')->find($t[6]);
  332. if ($prize_info) {
  333. $prize_insert['order_id'] = $order['order_id'];
  334. $prize_insert['mid'] = $t[2];
  335. $prize_insert['child_num'] = $t[7];
  336. $prize_insert['prize_id'] = $prize_info['id'];
  337. Dever::db('agent/member_prize')->insert($prize_insert);
  338. $prize['type'] = 3;
  339. $prize['type_id'] = $prize_info['id'];
  340. $prize['sku_id'] = -1;
  341. $prize['num'] = $prize_info['num'];
  342. $this->addGoods($order, $prize);
  343. }
  344. }
  345. $member = Dever::db('agent/member')->find($where['mid']);
  346. $desc = $member['name'] . '成为代理商,佣金'.$t[5].'%';
  347. Dever::load('agent/lib/member')->up($member, $order['id'], $order['role'], $where['type'], $where['parent_mid'], $t[3], $order['agent_cash'], $t[4], $desc);
  348. }
  349. }
  350. }
  351. private function setAgreement($order)
  352. {
  353. Dever::load('agent/lib/agreement')->up($order);
  354. }
  355. public function sellOrderUpdate($id, $name, $data)
  356. {
  357. Dever::config('base')->hook = true;
  358. $update = array();
  359. $audit = Dever::param('audit', $data);
  360. $info = Dever::db('agent/dh_order')->one($id);
  361. if ($audit > 1 && $info && $info['status'] == 2) {
  362. if ($audit == 2) {
  363. # 成功
  364. Dever::setInput('order_id', $id);
  365. Dever::load('agent/lib/dhorder.audit_commit');
  366. } else {
  367. Dever::db('agent/dh_order')->update(array('where_id' => $id, 'status' => 7));
  368. }
  369. }
  370. }
  371. # 确认收货
  372. public function setSellOrderStatus_api()
  373. {
  374. $order_id = Dever::input('order_id');
  375. $info = Dever::db('agent/dh_order')->find($order_id);
  376. if ($info) {
  377. Dever::load('agent/lib/dhorder')->finish($info, $info['shop_id']);
  378. return 'reload';
  379. } else {
  380. Dever::alert('错误的订单号');
  381. }
  382. }
  383. # 获取权益名称
  384. public function getGoods($type, $type_id, $sku_id)
  385. {
  386. if ($type == 1) {
  387. if ($sku_id == -2) {
  388. return '商品';
  389. }
  390. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($type_id, $sku_id);
  391. if (isset($goods_info['sku'])) {
  392. $sku = '-' . $goods_info['sku']['string'];
  393. } else {
  394. $sku = '';
  395. }
  396. return array('name' => $goods_info['name'] . $sku, 'price' => $goods_info['price']);
  397. } elseif ($type == 2) {
  398. $card = Dever::db('card/info')->find($type_id);
  399. return array('name' => $card['name'], 'price' => $card['price']);
  400. } elseif ($type == 3) {
  401. $prize = Dever::db('setting/role_goods')->config['set']['prize'];
  402. return array('name' => $prize[$type_id], 'price' => 0);
  403. }
  404. }
  405. }