Manage.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Manage
  5. {
  6. public function sellOrderPs($id, $name, $data)
  7. {
  8. Dever::config('base')->hook = true;
  9. $order_id = Dever::param('order_id', $data);
  10. if ($order_id) {
  11. $info = Dever::db('shop/sell_order')->find(array('id' => $order_id));
  12. if ($info && $info['status'] < 3) {
  13. Dever::load('shop/lib/sell')->notice($info);
  14. }
  15. }
  16. }
  17. # 获取规格型号
  18. public function getSku($goods_id, $sku_id)
  19. {
  20. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($goods_id, $sku_id);
  21. if (isset($goods_info['sku'])) {
  22. $sku = $goods_info['sku']['string'];
  23. } else {
  24. $sku = '';
  25. }
  26. return $sku;
  27. }
  28. # 获取在途库存
  29. public function getGoodsTotal($type, $type_id, $goods_id, $sku_id)
  30. {
  31. $where['type'] = $type;
  32. $where['type_id'] = $type_id;
  33. $where['status'] = '2,3,4';
  34. $where['goods_id'] = $goods_id;
  35. $where['sku_id'] = $sku_id;
  36. $order = Dever::db('shop/buy_order_goods')->getGoodsTotal($where);
  37. if ($order && $order['total']) {
  38. return $order['total'];
  39. }
  40. return 0;
  41. }
  42. public function buyInfoRefundNum($id)
  43. {
  44. $where['status'] = 3;
  45. $where['order_id'] = $id;
  46. $order = Dever::db('shop/buy_order_goods')->getGoodsNum($where);
  47. if ($order && $order['total']) {
  48. return $order['total'];
  49. }
  50. return 0;
  51. }
  52. public function buyInfoRefundStatus($id, $table = 'buy_order')
  53. {
  54. $info = Dever::db('shop/' . $table)->find($id);
  55. if ($info['refund_cash'] > 0) {
  56. if ($info['status'] == 6 || $info['status'] == 8) {
  57. return '已退款';
  58. } else {
  59. return '有退款';
  60. }
  61. } elseif ($info['refund_status'] == 2) {
  62. return '已申请';
  63. } else {
  64. return '未申请';
  65. }
  66. }
  67. public function shopInfoType($type)
  68. {
  69. $config_type = Dever::db('shop/info')->config['config_type'];
  70. return $config_type[$type];
  71. }
  72. public function getTotalCash($id)
  73. {
  74. $order = Dever::db('shop/sell_order')->one($id);
  75. $cash = 0;
  76. $cash = $order['oprice'];
  77. return round($cash, 2);
  78. }
  79. public function getOrderUrl($source_type, $order_id, $order_num, $search)
  80. {
  81. //$url = Dever::load('manage/database')->url('list', $order_id, 'buy_order_goods&project=shop&order_id='.$order_id.'&page_type=1');
  82. if ($source_type == 4) {
  83. $url = Dever::url('project/database/list&project=shop&search_option_shop_type=1&page_type=2&menu_id=85&table=sell_order_goods&project=shop&order_id='.$order_id.'&page_type=&search_option_shop_type=1&menu=shop&menu_id=85&search_option_state=1&menu_id=85', 'manage');
  84. } else {
  85. $url = Dever::url('project/database/list?project=cash&search_option_search=1&search_option_type=1&page_type=2&table=buy_order_goods&project=shop&order_id='.$order_id.'&page_type=1&menu=cash&menu_id=124&search_option_state=1', 'manage');
  86. if ($search == 3) {
  87. $url .= '&type=2&search_option_type=2';
  88. } elseif ($search == 2) {
  89. $url .= '&type=1&search_option_type=3';
  90. } else {
  91. $url .= '&type=1&search_option_type=1';
  92. }
  93. }
  94. $url = '<a href="'.$url.'">'.$order_num.'</a>';
  95. return $url;
  96. }
  97. public function showCash($cash, $p_cash = 0)
  98. {
  99. if (!$cash) {
  100. $cash = 0;
  101. }
  102. if (!$p_cash) {
  103. $p_cash = 0;
  104. }
  105. return round($cash - $p_cash,2);
  106. }
  107. # 获取用户信息
  108. public function user($id, $type = 1)
  109. {
  110. $info = Dever::db('shop/sell_order')->one($id);
  111. if ($info['uid'] && $info['uid'] > 0) {
  112. $user = Dever::db('passport/user')->one($info['uid']);
  113. $result = $user['username'];
  114. if ($type == 1 && $info['mobile']) {
  115. $result .= '('.$info['mobile'].')';
  116. }
  117. } else {
  118. $result = $info['mobile'];
  119. }
  120. return $result;
  121. }
  122. # 获取订单信息
  123. public function sell_order($id)
  124. {
  125. $table = array();
  126. $info = Dever::db('shop/sell_order')->one($id);
  127. $goods = Dever::db('shop/sell_order_goods')->select(array('order_id' => $info['id']));
  128. foreach ($goods as $k => $v) {
  129. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  130. //print_r($goods_info);die;
  131. $table[$goods_info['name']] = $v['price'];
  132. }
  133. return Dever::table($table);
  134. }
  135. /**
  136. * 更新信息
  137. *
  138. * @return mixed
  139. */
  140. public function upInfo($id, $name, $data)
  141. {
  142. Dever::config('base')->hook = true;
  143. }
  144. /**
  145. * 更新信息
  146. *
  147. * @return mixed
  148. */
  149. public function printInsert($id, $name, $data)
  150. {
  151. Dever::config('base')->hook = true;
  152. $name = Dever::param('name', $data);
  153. $number = Dever::param('number', $data);
  154. $phonenum = Dever::param('phonenum', $data);
  155. $key = Dever::param('key', $data);
  156. if ($number && $key) {
  157. $data = array();
  158. $data['name'] = $name;
  159. $data['number'] = $number;
  160. $data['phonenum'] = $phonenum;
  161. $data['key'] = $key;
  162. $result = Dever::load('mshop/lib/feieyun')->add(array($data));
  163. if (isset($result['no'][0]) && $result['no'][0] && strstr($result['no'][0], '编号不合法')) {
  164. Dever::db('shop/print')->update(array('where_id' => $id, 'status' => 10));
  165. } else {
  166. Dever::db('shop/print')->update(array('where_id' => $id, 'status' => 1));
  167. }
  168. }
  169. }
  170. /**
  171. * 更新信息
  172. *
  173. * @return mixed
  174. */
  175. public function printUpdate($id, $name, $data)
  176. {
  177. Dever::config('base')->hook = true;
  178. $info = Dever::db('shop/print')->one($id);
  179. $name = Dever::param('name', $data);
  180. $phonenum = Dever::param('phonenum', $data);
  181. if ($info && $name) {
  182. if ($info['status'] == 10) {
  183. $this->printInsert($id, $name, $info);
  184. } else {
  185. Dever::load('mshop/lib/feieyun')->edit($info['number'], $name, $phonenum);
  186. }
  187. }
  188. }
  189. /**
  190. * 打印机状态
  191. *
  192. * @return mixed
  193. */
  194. public function printStatus($status, $sn)
  195. {
  196. return $status >= 10 ? '绑定失败' : Dever::load('mshop/lib/feieyun')->status($sn);
  197. }
  198. public function buyOrderUpdate($id, $name, $data)
  199. {
  200. Dever::config('base')->hook = true;
  201. $update = array();
  202. $audit = Dever::param('audit', $data);
  203. $info = Dever::db('shop/buy_order')->one($id);
  204. if ($audit > 1 && $info && $info['status'] == 2) {
  205. if ($audit == 2) {
  206. # 成功
  207. Dever::setInput('order_id', $id);
  208. Dever::load('mshop/lib/buy.audit_commit');
  209. } else {
  210. # 退款
  211. $shop = Dever::db('shop/info')->find($info['type_id']);
  212. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $id, false, 3, 0, '未通过审核');
  213. }
  214. }
  215. }
  216. public function sellOrderUpdate($id, $name, $data)
  217. {
  218. Dever::config('base')->hook = true;
  219. $update = array();
  220. $audit = Dever::param('audit', $data);
  221. $info = Dever::db('shop/sell_order')->one($id);
  222. if ($audit > 1 && $info && $info['status'] == 2) {
  223. if ($audit == 2) {
  224. # 成功
  225. Dever::setInput('order_id', $id);
  226. Dever::load('shop/lib/sell.audit_commit');
  227. } else {
  228. # 退款
  229. $shop = Dever::db('shop/info')->find($info['shop_id']);
  230. Dever::load('shop/lib/refund')->set('sell')->apply(1, $shop['id'], $id, false, 3, 0, '未通过审核');
  231. }
  232. }
  233. }
  234. /**
  235. * 更新信息
  236. *
  237. * @return mixed
  238. */
  239. public function couponUpdate($id, $name, $data)
  240. {
  241. Dever::config('base')->hook = true;
  242. $update = array();
  243. $coupon = Dever::param('coupon', $data);
  244. if ($coupon) {
  245. $temp = is_string($coupon) ? explode(',', $coupon) : $coupon;
  246. $update['method'] = $temp[0];
  247. $update['coupon_id'] = $temp[1];
  248. }
  249. $shop_id = Dever::param('shop_id', $data);
  250. if ($shop_id) {
  251. $shop = Dever::db('shop/info')->find($shop_id);
  252. $update['city'] = $shop['city'];
  253. }
  254. if (isset($update) && $update) {
  255. $update['where_id'] = $id;
  256. Dever::db('shop/coupon')->update($update);
  257. }
  258. }
  259. /**
  260. * 更新信息
  261. *
  262. * @return mixed
  263. */
  264. public function infoUpdate($id, $name, $data)
  265. {
  266. $update = array();
  267. $area = Dever::param('area', $data);
  268. if ($area) {
  269. $temp = is_string($area) ? explode(',', $area) : $area;
  270. $update['province'] = $temp[0];
  271. $update['city'] = $temp[1];
  272. if (isset($temp[2])) {
  273. $update['county'] = $temp[2];
  274. }
  275. if (isset($temp[3])) {
  276. $update['town'] = $temp[3];
  277. }
  278. if (is_array($area)) {
  279. $area = implode(',', $area);
  280. }
  281. $gup['option_shop_id'] = $id;
  282. $gup['set_area'] = $area;
  283. Dever::db('shop/goods_sku')->updates($gup);
  284. Dever::db('shop/sell_order')->updates($gup);
  285. unset($gup['option_shop_id']);
  286. $gup['option_type_id'] = $id;
  287. $gup['option_type'] = 1;
  288. Dever::db('shop/buy_order')->updates($gup);
  289. Dever::db('shop/out_order')->updates($gup);
  290. }
  291. $address = Dever::param('address', $data);
  292. if ($address && isset($update['city'])) {
  293. $geo = Dever::load('shop/lib/info')->geo($update['city'], $address);
  294. $update['lng'] = $geo[0];
  295. $update['lat'] = $geo[1];
  296. $update['map'] = $geo[2];
  297. $update['coord_address'] = $address;
  298. }
  299. /*
  300. $map = Dever::param('map', $data);
  301. if ($map) {
  302. $temp = is_string($map) ? explode(',', $map) : $map;
  303. if (isset($temp[1])) {
  304. $update['lng'] = $temp[1];
  305. $update['lat'] = $temp[2];
  306. $address = Dever::param('address', $data);
  307. $update['coord_address'] = Dever::load('shop/lib/info')->address($temp[1], $temp[2]);
  308. if (!$address && $update['coord_address']) {
  309. $update['address'] = $update['coord_address'];
  310. }
  311. }
  312. }
  313. */
  314. $act = Dever::param('act', $data);
  315. if ($act) {
  316. foreach ($act as $k => $v) {
  317. $w = array();
  318. $w['shop_id'] = $id;
  319. $w['act_id'] = $k;
  320. foreach ($v as $k1 => $v1) {
  321. $w['shop_coupon_id'] = $v1;
  322. $info = Dever::db('shop/coupon_act')->find($w);
  323. if (!$info) {
  324. Dever::db('shop/coupon_act')->insert($w);
  325. }
  326. }
  327. }
  328. }
  329. //Dever::upLinkage($update, $id, $data, 'goods', 'shop/goods', 'shop_id', 'goods_id', 'category_id');
  330. //Dever::upLinkage($update, $id, $data, 'factory', 'shop/factory', 'shop_id', 'factory_id', 'city');
  331. //Dever::upLinkage($update, $id, $data, 'store', 'shop/store', 'shop_id', 'store_id', 'city');
  332. if (isset($update) && $update) {
  333. $update['where_id'] = $id;
  334. Dever::db('shop/info')->update($update);
  335. }
  336. $truename = Dever::param('truename', $data);
  337. $mobile = Dever::param('mobile', $data);
  338. if ($mobile) {
  339. $match = Dever::rule('mobile');
  340. if (preg_match($match, $mobile)) {
  341. $member = Dever::db('shop/member')->one(array('shop_id' => $id, 'mobile' => $mobile));
  342. $update = array();
  343. $update['shop_id'] = $id;
  344. $update['name'] = $truename;
  345. $update['mobile'] = $mobile;
  346. if ($member) {
  347. $update['where_id'] = $member['id'];
  348. Dever::db('shop/member')->update($update);
  349. } else {
  350. Dever::db('shop/member')->insert($update);
  351. }
  352. }
  353. }
  354. }
  355. private function updateParam()
  356. {
  357. }
  358. /**
  359. * 更新商品信息
  360. *
  361. * @return mixed
  362. */
  363. public function goodsUpdate($id, $name, $data)
  364. {
  365. $update = array();
  366. $state = Dever::param('state', $data);
  367. if ($state) {
  368. Dever::config('base')->after = true;
  369. $info = Dever::db('shop/goods')->one($id);
  370. $shop_id = $info['shop_id'];
  371. $shop = Dever::db('shop/info')->one($shop_id);
  372. if ($shop && $shop['goods']) {
  373. $goods = Dever::json_decode($shop['goods']);
  374. if (isset($goods['level_' . $info['category_id']]['value']['id_' . $info['goods_id']])) {
  375. if ($state == 2) {
  376. unset($goods['level_' . $info['category_id']]['value']['id_' . $info['goods_id']]);
  377. }
  378. if (count($goods['level_' . $info['category_id']]['value']) <= 0) {
  379. unset($goods['level_' . $info['category_id']]);
  380. }
  381. } elseif ($state == 1) {
  382. $goods_info = Dever::db('goods/info')->one($info['goods_id']);
  383. $cate = Dever::load('category/api')->string($info['category_id']);
  384. $goods['level_' . $info['category_id']]['id'] = $info['category_id'];
  385. $goods['level_' . $info['category_id']]['name'] = $cate;
  386. $goods['level_' . $info['category_id']]['value']['id_' . $info['goods_id']] = array
  387. (
  388. 'id' => $info['goods_id'],
  389. 'name' => $goods_info['name'],
  390. 'state' => 1,
  391. );
  392. }
  393. $set['goods'] = Dever::json_encode($goods);
  394. $set['where_id'] = $shop_id;
  395. Dever::db('shop/info')->update($set);
  396. }
  397. }
  398. }
  399. /**
  400. * 更新库存信息
  401. *
  402. * @return mixed
  403. */
  404. public function skuUpdate($id, $name, $data)
  405. {
  406. $update = array();
  407. $add_num = Dever::param('add_num', $data);
  408. if ($add_num) {
  409. $state = Dever::db('shop/goods_sku')->updateTotal(array('where_id' => $id, 'total_num' => $add_num));
  410. if ($state) {
  411. $state = Dever::db('shop/goods_sku')->update(array('where_id' => $id, 'add_num' => 0));
  412. $info = Dever::db('shop/goods_sku')->one($id);
  413. $goods = Dever::db('shop/goods')->one(array('goods_id' => $info['goods_id'], 'shop_id' => $info['shop_id']));
  414. if ($goods) {
  415. Dever::db('shop/goods')->update(array('where_id' => $goods['id'], 'total_num' => $info['total_num'], 'add_num' => 0));
  416. }
  417. if ($add_num < 0) {
  418. # 记录出库日志
  419. } else {
  420. # 记录入库日志
  421. }
  422. }
  423. }
  424. }
  425. # 获取供货商信息
  426. public function buyInfo($type = '', $type_id = '')
  427. {
  428. if ($type == 1) {
  429. $type_info = Dever::db('shop/info')->one($type_id);
  430. return $type_info['name'] . '('.$type_info['mobile'].')';
  431. } elseif ($type == 2) {
  432. $type_info = Dever::db('store/info')->one($type_id);
  433. $sign = Dever::login($type_id);
  434. $link = Dever::url('home?sign=' . $sign, 'store');
  435. //return $type_info['name'] . '('.$type_info['mobile'].')<br /><a href="'.$link.'" target="_blank">进入仓库管理</a>';
  436. return $type_info['name'] . '('.$type_info['mobile'].')';
  437. } elseif ($type == 3) {
  438. $sign = Dever::login($type_id);
  439. $link = Dever::url('home?sign=' . $sign, 'factory');
  440. $type_info = Dever::db('factory/info')->one($type_id);
  441. //return $type_info['name'] . '('.$type_info['mobile'].')<br /><a href="'.$link.'" target="_blank">进入工厂管理</a>';
  442. return $type_info['name'] . '('.$type_info['mobile'].')';
  443. } else {
  444. return '无';
  445. }
  446. }
  447. /**
  448. * 更新信息
  449. *
  450. * @return mixed
  451. */
  452. public function buyOrderPs($id, $name, $data)
  453. {
  454. $order_id = Dever::param('order_id', $data);
  455. if ($order_id) {
  456. $update['where_id'] = $order_id;
  457. $update['set_status'] = 4;
  458. Dever::db('shop/buy_order')->update($update);
  459. }
  460. }
  461. # 获取门店
  462. public function search_api()
  463. {
  464. return Dever::search('shop/info');
  465. }
  466. # 获取环比增长
  467. public function getGoodsHb($num, $day, $goods_id)
  468. {
  469. $where = array('day' => $day, 'goods_id' => $goods_id);
  470. $search = Dever::search_button();
  471. if ($search) {
  472. $where['config']['group'] = $search[0];
  473. $where['config']['col'] = str_replace('|id', '', $search[1]);
  474. }
  475. if ($num < 0) {
  476. $num = 0;
  477. }
  478. $prev = Dever::db('shop/goods_stat')->prev($where);
  479. if ($prev && $num > 0 && $prev['num'] >= 0) {
  480. $n = $prev['num'];
  481. if ($n == 0) {
  482. $n = 1;
  483. }
  484. $hb = round(($num-$prev['num'])/$n, 2)*100;
  485. } else {
  486. $hb = 0;
  487. }
  488. return $hb . '%';
  489. }
  490. public function getUserHb($num, $total, $day)
  491. {
  492. $result = $this->getUserStat($num, $total, 0);
  493. $where = array('day' => $day);
  494. $search = Dever::search_button();
  495. if ($search) {
  496. $where['config']['group'] = $search[0];
  497. $where['config']['col'] = str_replace('|id', '', $search[1]);
  498. }
  499. $hb = 0;
  500. $prev = Dever::db('shop/user_stat')->prev($where);
  501. if ($prev) {
  502. $prev = $this->getUserStat($prev['num'], $prev['total'], $prev['order_num']);
  503. if ($result['fg'] > 0 && $prev['fg'] >= 0) {
  504. $fg = $prev['fg'];
  505. if ($fg == 0) {
  506. $fg = 1;
  507. }
  508. $hb = round(($result['fg']-$prev['fg'])/$fg, 2)*100;
  509. }
  510. }
  511. return $hb . '%';
  512. }
  513. public function getUserStat($num, $total, $order_num)
  514. {
  515. //print_r($info);die;
  516. if ($num && $total) {
  517. $result['fg'] = round($num/$total, 2);
  518. } else {
  519. $result['fg'] = 0;
  520. }
  521. if ($order_num) {
  522. $result['per_num'] = round($order_num/$total, 2);
  523. } else {
  524. $result['per_num'] = 0;
  525. }
  526. return $result;
  527. }
  528. # 获取环比增长
  529. public function getUserRankHb($num, $mobile, $day)
  530. {
  531. $where = array('day' => $day, 'mobile' => $mobile);
  532. $search = Dever::search_button();
  533. if ($search) {
  534. $where['config']['group'] = $search[0];
  535. $where['config']['col'] = str_replace('|id', '', $search[1]);
  536. }
  537. if ($num < 0) {
  538. $num = 0;
  539. }
  540. $prev = Dever::db('shop/user_rank_stat')->prev($where);
  541. if ($prev && $num > 0 && $prev['num'] >= 0) {
  542. $n = $prev['num'];
  543. if ($n == 0) {
  544. $n = 1;
  545. }
  546. $hb = round(($num-$prev['num'])/$n, 2)*100;
  547. } else {
  548. $hb = 0;
  549. }
  550. return $hb . '%';
  551. }
  552. # 确认收货
  553. public function setSellOrderStatus_api()
  554. {
  555. $order_id = Dever::input('order_id');
  556. $info = Dever::db('shop/sell_order')->find($order_id);
  557. if ($info) {
  558. Dever::load('shop/lib/sell')->finish($info, $info['shop_id']);
  559. return 'reload';
  560. } else {
  561. Dever::alert('错误的订单号');
  562. }
  563. }
  564. public function searchName($where)
  565. {
  566. $data = Dever::db('store/info')->like($where);
  567. if (!$data) {
  568. $data = Dever::db('shop/info')->like($where);
  569. }
  570. return $data;
  571. }
  572. public function searchFName($where)
  573. {
  574. $data = Dever::db('store/info')->like($where);
  575. if (!$data) {
  576. $data = Dever::db('factory/info')->like($where);
  577. }
  578. return $data;
  579. }
  580. }