Cron.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Cron
  5. {
  6. # 定时获取银联提现信息
  7. public function yspay_tixian_api()
  8. {
  9. $num = Dever::input('num', 1);
  10. $start = Dever::input('start', date('Ymd', strtotime('-'.$num.' day')));
  11. $end = Dever::input('end', date('Ymd'));
  12. # 获取有银联商户号的门店
  13. $where = array();
  14. $mid = Dever::input('mid');
  15. if ($mid) {
  16. $where['mid'] = $mid;
  17. }
  18. $shop_id = Dever::input('shop_id');
  19. if ($shop_id) {
  20. $where['id'] = $shop_id;
  21. }
  22. $test = Dever::input('test');
  23. $shop = Dever::db('shop/info')->select($where);
  24. foreach ($shop as $k => $v) {
  25. if ($v['mid']) {
  26. $data = Dever::load('shop/yspay')->record_action($v['mid'], $start, $end);
  27. if ($data) {
  28. if ($test == 1) {
  29. print_r($data);die;
  30. }
  31. foreach ($data as $k1 => $v1) {
  32. $update = array();
  33. $update['shop_id'] = $v['id'];
  34. $update['type'] = $v1['trxTypeDtl'];
  35. $update['cash'] = round($v1['transAmt']/100, 2);
  36. $update['tdate'] = Dever::maketime($v1['trxTime']);
  37. $update['mid'] = $v1['mchntNo'];
  38. $update['cardNo'] = $v1['cardNo'];
  39. $update['order_num'] = $v1['sysOrderId'];
  40. $update['status'] = $v1['status'];
  41. $info = Dever::db('shop/yspay_tixian')->one($update);
  42. if (!$info) {
  43. Dever::db('shop/yspay_tixian')->insert($update);
  44. }
  45. }
  46. }
  47. }
  48. }
  49. return 'ok';
  50. }
  51. # 定时更新店铺经纬度
  52. public function geo_api()
  53. {
  54. Dever::config('base')->hook = true;
  55. $shop = Dever::db('shop/info')->select();
  56. if ($shop) {
  57. foreach ($shop as $k => $v) {
  58. $temp = explode(',', $v['area']);
  59. if (isset($temp[1]) && $v['address']) {
  60. $geo = Dever::load('shop/lib/info')->geo($temp[1], $v['address']);
  61. $update['lng'] = $geo[0];
  62. $update['lat'] = $geo[1];
  63. $update['map'] = $geo[2];
  64. $update['coord_address'] = $v['address'];
  65. $update['where_id'] = $v['id'];
  66. Dever::db('shop/info')->update($update);
  67. }
  68. }
  69. }
  70. return 'ok';
  71. }
  72. # 同步退款状态
  73. public function refund_status_api()
  74. {
  75. $order = Dever::db('shop/sell_order')->select();
  76. foreach ($order as $k => $v) {
  77. if ($v['refund_cash'] > 0) {
  78. if ($v['status'] == 6 || $v['status'] == 8) {
  79. $status = 4;
  80. } else {
  81. $status = 3;
  82. }
  83. } elseif ($v['refund_status'] == 2) {
  84. $status = 2;
  85. } else {
  86. $status = 1;
  87. }
  88. Dever::db('shop/sell_order')->update(array('where_id' => $v['id'], 'refund_status' => $status));
  89. }
  90. $order = Dever::db('shop/buy_order')->select();
  91. foreach ($order as $k => $v) {
  92. if ($v['refund_cash'] > 0) {
  93. if ($v['status'] == 6 || $v['status'] == 8) {
  94. $status = 4;
  95. } else {
  96. $status = 3;
  97. }
  98. } elseif ($v['refund_status'] == 2) {
  99. $status = 2;
  100. } else {
  101. $status = 1;
  102. }
  103. Dever::db('shop/buy_order')->update(array('where_id' => $v['id'], 'refund_status' => $status));
  104. }
  105. }
  106. # 获取7天后完成的订单 准备分账
  107. public function yspay_api()
  108. {
  109. $day = Dever::input('day', 7);
  110. $time = time();
  111. $where['status'] = '5,6';
  112. $where['withdraw'] = 1;
  113. $where['cdate'] = $time - $day*86400;
  114. $order = Dever::db('shop/sell_order')->getDataByTime($where);
  115. if ($order) {
  116. foreach ($order as $k => $v) {
  117. $this->yspayOne($v);
  118. }
  119. }
  120. }
  121. public function yspayOne($v)
  122. {
  123. $shop = Dever::db('shop/info')->find($v['shop_id']);
  124. if ($v['withdraw'] == 1 && $shop && $shop['mid'] && $shop['status'] == 1) {
  125. if ($v['refund_cash']) {
  126. $v['price'] -= $v['refund_cash'];
  127. $v['price'] = round($v['price'], 2);
  128. }
  129. if ($v['price'] > 0) {
  130. $cash = $v['price'] * 100;//之后还要加上礼品卡的钱
  131. Dever::load('shop/yspay')->action($shop['mid'], $v['order_num'], $v['id'], 2, $cash);
  132. sleep(1);
  133. } else {
  134. Dever::db('shop/sell_order')->update(array('where_id' => $v['id'], 'withdraw' => 2));
  135. }
  136. }
  137. }
  138. public function setGoodsStatus_api()
  139. {
  140. $goods = Dever::db('goods/info')->select();
  141. if ($goods) {
  142. foreach ($goods as $k => $v) {
  143. Dever::db('shop/goods')->updates(array('option_goods_id' => $v['id'], 'set_status' => $v['status']));
  144. Dever::db('store/goods')->updates(array('option_goods_id' => $v['id'], 'set_status' => $v['status']));
  145. Dever::db('factory/goods')->updates(array('option_goods_id' => $v['id'], 'set_status' => $v['status']));
  146. }
  147. }
  148. }
  149. /**
  150. * 处理优惠券到期时间
  151. *
  152. * @return mixed
  153. */
  154. public function coupon_api()
  155. {
  156. $coupon = Dever::db('shop/user_coupon')->getAll(array('status' => 1, 'edate' => time() - 86400));
  157. if ($coupon && Dever::project('message')) {
  158. foreach ($coupon as $k => $v) {
  159. $coupon_info = Dever::db('goods/coupon')->find($v['coupon_id']);
  160. $msg = $coupon_info['name'] . ",要到期啦!";
  161. $msg_param['type'] = 2;//消息类型2是优惠券
  162. $msg_param['id'] = $v['id'];
  163. $msg_param['coupon_id'] = $v['coupon_id'];
  164. $msg_param = Dever::json_encode($msg_param);
  165. Dever::load('message/lib/data')->push(-1, $v['uid'], '优惠劵到期提醒', $msg, 4, 1, false, $msg_param);
  166. if (Dever::load('wechat_applet') && $v['uid'] && $v['uid'] > 0) {
  167. $user = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'system_id' => 1, 'system_source' => 5));
  168. if ($user && $user['openid']) {
  169. $send = array
  170. (
  171. 'thing1' => array
  172. (
  173. 'value' => $coupon_info['name'],
  174. ),
  175. 'time2' => array
  176. (
  177. 'value' => date('Y-m-d H:i', $v['edate']),
  178. ),
  179. 'thing3' => array
  180. (
  181. 'value' => '您有一张优惠券即将到期,请尽快使用',
  182. ),
  183. );
  184. $send = Dever::json_encode($send);
  185. Dever::load('wechat_applet/subscribe')->sendOne('act_2', 1, $user['openid'], 'pages/my/coupon/coupon', $send, Dever::config('base')->wechat_applet);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. /**
  192. * 处理待支付订单提醒
  193. *
  194. * @return mixed
  195. */
  196. public function order_api()
  197. {
  198. # 获取超过5分钟未支付的订单
  199. $time = time();
  200. $where['status'] = 1;
  201. $where['cdate'] = $time - 300;
  202. $order = Dever::db('shop/sell_order')->getDataByTime($where);
  203. if ($order && Dever::project('message')) {
  204. $config = Dever::db('shop/sell_order')->config;
  205. foreach ($order as $k => $v) {
  206. if ($time - $v['cdate'] >= 900) {
  207. Dever::load('shop/lib/sell')->set(1, 2)->cancel($v['uid'], $v['id'], 11);
  208. Dever::db('shop/sell_order')->update(array('where_id' => $v['id'], 'status' => 11, 'notice' => 2));
  209. } elseif ($v['notice'] == 1) {
  210. Dever::db('shop/sell_order')->update(array('where_id' => $v['id'], 'notice' => 2));
  211. $msg = "您有一笔待付款订单,请及时付款。\r\n订单15分钟内未付款自动取消~";
  212. $shop = Dever::db('shop/info')->one($v['shop_id']);
  213. $msg_param = array();
  214. $msg_param['type'] = 1;//消息类型1是订单消息
  215. $msg_param['id'] = $v['id'];
  216. $msg_param['name'] = $shop['name'];
  217. $msg_param = Dever::json_encode($msg_param);
  218. Dever::load('message/lib/data')->push(-1, $v['uid'], '订单待支付提醒', $msg, 2, 1, false, $msg_param);
  219. if (Dever::load('wechat_applet') && $v['uid'] && $v['uid'] > 0) {
  220. $user = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'system_id' => 1, 'system_source' => 5));
  221. if ($user && $user['openid']) {
  222. $v['status_name'] = $config['status'][$v['status']];
  223. $send = array
  224. (
  225. 'character_string5' => array
  226. (
  227. 'value' => $v['order_num'],
  228. ),
  229. 'amount11' => array
  230. (
  231. 'value' => $v['price'] . '元',
  232. ),
  233. 'phrase13' => array
  234. (
  235. 'value' => $v['status_name'],
  236. ),
  237. 'thing12' => array
  238. (
  239. 'value' => '您有一笔待付款订单,请及时付款。',
  240. ),
  241. );
  242. $send = Dever::json_encode($send);
  243. Dever::load('wechat_applet/subscribe')->sendOne('order_pay', 1, $user['openid'], 'pages/app/order/order?id=' . $v['id'], $send, Dever::config('base')->wechat_applet);
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. /**
  251. * 处理待支付订单提醒
  252. *
  253. * @return mixed
  254. */
  255. public function buy_order_api()
  256. {
  257. $time = time();
  258. $where['status'] = 1;
  259. $where['cdate'] = $time - 7200;
  260. $order = Dever::db('shop/buy_order')->getDataByTime($where);
  261. if ($order) {
  262. foreach ($order as $k => $v) {
  263. Dever::load('mshop/lib/buy')->set(1, 1)->cancel($v['type_id'], $v['id']);
  264. Dever::db('shop/buy_order')->update(array('where_id' => $v['id'], 'status' => 11));
  265. }
  266. }
  267. }
  268. /**
  269. * 处理销售数据 生成每天的销量统计
  270. *
  271. * @return mixed
  272. */
  273. public function sell_api()
  274. {
  275. $num = Dever::input('num', 1);
  276. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  277. $end = Dever::input('end', date('Y-m-d'));
  278. $where['status'] = '2,3,4,5,6';
  279. $start = Dever::maketime($start . ' 00:00:00');
  280. $end = Dever::maketime($end . ' 23:59:59');
  281. $day = intval(($end - $start)/86400);
  282. $shop = Dever::db('shop/info')->select();
  283. foreach ($shop as $k => $v) {
  284. $where['shop_id'] = $v['id'];
  285. for($i=0; $i<=$day; $i++) {
  286. $where['start'] = $start + 86400*$i;
  287. $where['end'] = $start + 86400*$i + 86399;
  288. $data = array();
  289. $data['shop_id'] = $v['id'];
  290. $data['day'] = $where['start'];
  291. $info = Dever::db('shop/sell_stat')->find($data);
  292. $cash = Dever::db('shop/sell_order')->getCashNum($where);
  293. $data['cash'] = round($cash['total'], 2);
  294. $data['order'] = Dever::db('shop/sell_order')->getOrderNum($where);
  295. if ($data['order'] > 0) {
  296. $goods = Dever::db('shop/sell_order')->getGoodsNum($where);
  297. $data['goods'] = $goods['total'];
  298. $data['area'] = $v['area'];
  299. $data['province'] = $v['province'];
  300. $data['city'] = $v['city'];
  301. $data['county'] = $v['county'];
  302. $data['town'] = $v['town'];
  303. if (!$info) {
  304. Dever::db('shop/sell_stat')->insert($data);
  305. } else {
  306. $data['where_id'] = $info['id'];
  307. Dever::db('shop/sell_stat')->update($data);
  308. }
  309. }
  310. }
  311. }
  312. }
  313. public function buy_api()
  314. {
  315. $num = Dever::input('num', 30);
  316. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  317. $end = Dever::input('end', date('Y-m-d'));
  318. $where['status'] = '2,3,4,5,6';
  319. $start = Dever::maketime($start . ' 00:00:00');
  320. $end = Dever::maketime($end . ' 23:59:59');
  321. $day = intval(($end - $start)/86400);
  322. $shop = Dever::db('shop/info')->select();
  323. foreach ($shop as $k => $v) {
  324. $where['type'] = 1;
  325. $where['type_id'] = $v['id'];
  326. for($i=0; $i<=$day; $i++) {
  327. $where['start'] = $start + 86400*$i;
  328. $where['end'] = $start + 86400*$i + 86399;
  329. $data = array();
  330. $data['shop_id'] = $v['id'];
  331. $data['day'] = $where['start'];
  332. $info = Dever::db('shop/buy_stat')->find($data);
  333. $cash = Dever::db('shop/buy_order_goods')->getCashNum($where);
  334. $data['cash'] = round($cash['total'], 2);
  335. $cash = Dever::db('shop/buy_order_goods')->getPCashNum($where);
  336. $data['p_cash'] = round($cash['total'], 2);
  337. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  338. if ($data['order'] > 0) {
  339. $goods = Dever::db('shop/buy_order')->getGoodsNum($where);
  340. $data['goods'] = $goods['total'];
  341. $data['area'] = $v['area'];
  342. $data['province'] = $v['province'];
  343. $data['city'] = $v['city'];
  344. $data['county'] = $v['county'];
  345. $data['town'] = $v['town'];
  346. if (!$info) {
  347. Dever::db('shop/buy_stat')->insert($data);
  348. } else {
  349. $data['where_id'] = $info['id'];
  350. Dever::db('shop/buy_stat')->update($data);
  351. }
  352. }
  353. }
  354. }
  355. return 'ok';
  356. }
  357. /**
  358. * 门店数据统计
  359. *
  360. * @return mixed
  361. */
  362. public function shop_api()
  363. {
  364. $num = Dever::input('num', 1);
  365. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  366. $end = Dever::input('end', date('Y-m-d'));
  367. $start = Dever::maketime($start . ' 00:00:00');
  368. $end = Dever::maketime($end . ' 23:59:59');
  369. $day = intval(($end - $start)/86400);
  370. $shop = Dever::db('shop/info')->select();
  371. foreach ($shop as $k => $v) {
  372. $where = array();
  373. $where['status'] = '2,3,4,5,6';
  374. $where['shop_id'] = $v['id'];
  375. for($i=0; $i<=$day; $i++) {
  376. $where['start'] = $start + 86400*$i;
  377. $where['end'] = $start + 86400*$i + 86399;
  378. $data = array();
  379. $data['shop_id'] = $v['id'];
  380. $data['day'] = $where['start'];
  381. $info = Dever::db('shop/shop_stat')->find($data);
  382. $cash = Dever::db('shop/sell_order')->getCashNum($where);
  383. $data['sell_cash'] = round($cash['total'], 2);
  384. $data['sell_num'] = Dever::db('shop/sell_order')->getOrderNum($where);
  385. $where['type'] = 1;
  386. $where['type_id'] = $v['id'];
  387. $cash = Dever::db('shop/buy_order_goods')->getCashNum($where);
  388. $data['buy_cash'] = round($cash['total'], 2);
  389. $data['buy_num'] = Dever::db('shop/buy_order')->getOrderNum($where);
  390. $data['sl_num'] = 0;
  391. $data['area'] = $v['area'];
  392. $data['province'] = $v['province'];
  393. $data['city'] = $v['city'];
  394. $data['county'] = $v['county'];
  395. $data['town'] = $v['town'];
  396. if ($data['buy_num'] > 0) {
  397. if (!$info) {
  398. Dever::db('shop/shop_stat')->insert($data);
  399. } else {
  400. $data['where_id'] = $info['id'];
  401. Dever::db('shop/shop_stat')->update($data);
  402. }
  403. }
  404. }
  405. }
  406. }
  407. /**
  408. * 商品数据统计
  409. *
  410. * @return mixed
  411. */
  412. public function goods_api()
  413. {
  414. $num = Dever::input('num', 5);
  415. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  416. $end = Dever::input('end', date('Y-m-d'));
  417. $where['status'] = '2,3,4,5,6';
  418. $start = Dever::maketime($start . ' 00:00:00');
  419. $end = Dever::maketime($end . ' 23:59:59');
  420. $day = intval(($end - $start)/86400);
  421. for($i=0; $i<=$day; $i++) {
  422. $where['start'] = $start + 86400*$i;
  423. $where['end'] = $start + 86400*$i + 86399;
  424. $num = Dever::db('shop/sell_order_goods')->getNum($where);
  425. if ($num) {
  426. foreach ($num as $k => $v) {
  427. $data = array();
  428. $data['shop_id'] = $v['shop_id'];
  429. $data['goods_id'] = $v['goods_id'];
  430. $data['day'] = $where['start'];
  431. $info = Dever::db('shop/goods_stat')->find($data);
  432. $shop = Dever::db('shop/info')->find($data['shop_id']);
  433. if ($shop) {
  434. $data['area'] = $shop['area'];
  435. }
  436. $goods_info = Dever::db('goods/info')->find($data['goods_id']);
  437. if ($goods_info) {
  438. $data['category'] = $goods_info['category'];
  439. }
  440. $data['cash'] = round($v['cash'], 2);
  441. $data['num'] = round($v['num'], 2);
  442. if (!$info) {
  443. Dever::db('shop/goods_stat')->insert($data);
  444. } else {
  445. $data['where_id'] = $info['id'];
  446. Dever::db('shop/goods_stat')->update($data);
  447. }
  448. }
  449. }
  450. }
  451. return 'ok';
  452. }
  453. /**
  454. * 门店数据统计
  455. *
  456. * @return mixed
  457. */
  458. public function user_api()
  459. {
  460. $num = Dever::input('num', 1);
  461. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  462. $end = Dever::input('end', date('Y-m-d'));
  463. $start = Dever::maketime($start . ' 00:00:00');
  464. $end = Dever::maketime($end . ' 23:59:59');
  465. $day = intval(($end - $start)/86400);
  466. $shop = Dever::db('shop/info')->select();
  467. foreach ($shop as $k => $v) {
  468. $where = array();
  469. $where['status'] = '2,3,4,5,6';
  470. $where['shop_id'] = $v['id'];
  471. for($i=0; $i<=$day; $i++) {
  472. $where['start'] = $start + 86400*$i;
  473. $where['end'] = $start + 86400*$i + 86399;
  474. $data = array();
  475. $data['shop_id'] = $v['id'];
  476. $data['day'] = $where['start'];
  477. $info = Dever::db('shop/user_stat')->find($data);
  478. $total = Dever::db('shop/sell_order')->getUser($where);
  479. $data['total'] = count($total);
  480. foreach ($total as $k1 => $v1) {
  481. if ($v1['total'] <= 1) {
  482. unset($total[$k1]);
  483. }
  484. }
  485. $data['num'] = count($total);
  486. $data['order_num'] = Dever::db('shop/sell_order')->getOrderNum($where);
  487. if ($data['num'] > 0 || $data['order_num'] > 0) {
  488. $data['area'] = $v['area'];
  489. if (!$info) {
  490. Dever::db('shop/user_stat')->insert($data);
  491. } else {
  492. $data['where_id'] = $info['id'];
  493. Dever::db('shop/user_stat')->update($data);
  494. }
  495. }
  496. }
  497. }
  498. }
  499. /**
  500. * 用户排名统计
  501. *
  502. * @return mixed
  503. */
  504. public function user_rank_api()
  505. {
  506. $num = Dever::input('num', 1);
  507. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  508. $end = Dever::input('end', date('Y-m-d'));
  509. $start = Dever::maketime($start . ' 00:00:00');
  510. $end = Dever::maketime($end . ' 23:59:59');
  511. $day = intval(($end - $start)/86400);
  512. $w['start'] = $start;
  513. $w['end'] = $end;
  514. $w['status'] = '2,3,4,5,6';
  515. $data = Dever::db('shop/sell_order')->getAllByDate($w);
  516. if ($data) {
  517. $where = array();
  518. $result = array();
  519. foreach ($data as $k => $v) {
  520. $day = Dever::maketime(date('Y-m-d 00:00:00', $v['cdate']));
  521. $key = $v['shop_id'] . '_' . $day . '_' . $v['mobile'];
  522. if (!isset($result[$key])) {
  523. $result[$key] = array();
  524. $result[$key]['num'] = 0;
  525. $result[$key]['cash'] = 0;
  526. }
  527. $result[$key]['num'] += $v['num'];
  528. $result[$key]['cash'] = $v['total'];
  529. }
  530. if ($result) {
  531. foreach ($result as $k => $v) {
  532. $temp = explode('_', $k);
  533. $where['shop_id'] = $temp[0];
  534. $where['day'] = $temp[1];
  535. $where['mobile'] = $temp[2];
  536. $where['clear'] = true;
  537. $info = Dever::db('shop/user_rank_stat')->find($where);
  538. $up = array();
  539. if (!$info) {
  540. $up = $where;
  541. $shop = Dever::db('shop/info')->one($where['shop_id']);
  542. $up['area'] = $shop['area'];
  543. $up['num'] = $v['num'];
  544. $up['cash'] = $v['cash'];
  545. Dever::db('shop/user_rank_stat')->insert($up);
  546. } else {
  547. $up = array();
  548. $up['where_id'] = $info['id'];;
  549. $up['num'] = $v['num'];
  550. $up['cash'] = $v['cash'];
  551. Dever::db('shop/user_rank_stat')->update($up);
  552. }
  553. }
  554. }
  555. }
  556. }
  557. # 获取下单用户量
  558. public function order_user($where)
  559. {
  560. $where['status'] = '1,2,3,4,5,6';
  561. $user = Dever::db('shop/sell_order')->getUser($where);
  562. return $user;
  563. }
  564. # 处理月度对账数据 废弃,直接用结算单处理
  565. public function sell_month_api()
  566. {
  567. return;
  568. $num = Dever::input('num', -1);
  569. if ($num > 0) {
  570. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  571. } else {
  572. $month = Dever::input('start', date('Y-m'));
  573. }
  574. $where['status'] = 2;
  575. $start = Dever::maketime($month . '-01 00:00:00');
  576. $end = Dever::maketime($month . '-31 23:59:59');
  577. $shop = Dever::db('shop/info')->select();
  578. foreach ($shop as $k => $v) {
  579. $where['shop_id'] = $v['id'];
  580. $where['start'] = $start;
  581. $where['end'] = $end;
  582. $data = array();
  583. $data['shop_id'] = $v['id'];
  584. $data['month'] = $start;
  585. $info = Dever::db('shop/sell_stat_month')->find($data);
  586. $data['cash'] = Dever::db('shop/sell_order')->getCashNum($where);
  587. $data['order'] = Dever::db('shop/sell_order')->getOrderNum($where);
  588. $data['goods'] = Dever::db('shop/sell_order')->getGoodsNum($where);
  589. if (!$info) {
  590. Dever::db('shop/sell_stat_month')->insert($data);
  591. } else {
  592. $data['where_id'] = $info['id'];
  593. Dever::db('shop/sell_stat_month')->update($data);
  594. }
  595. }
  596. }
  597. # 处理月度对账数据:门店采购 废弃,直接用结算单处理
  598. public function buy_month_api()
  599. {
  600. return;
  601. $num = Dever::input('num', -1);
  602. if ($num > 0) {
  603. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  604. } else {
  605. $month = Dever::input('start', date('Y-m'));
  606. }
  607. $where['status'] = '5,6';
  608. $start = Dever::maketime($month . '-01 00:00:00');
  609. $end = Dever::maketime($month . '-31 23:59:59');
  610. $shop = Dever::db('shop/info')->select();
  611. foreach ($shop as $k => $v) {
  612. $where['type'] = 1;
  613. $where['type_id'] = $v['id'];
  614. $where['start'] = $start;
  615. $where['end'] = $end;
  616. $data = array();
  617. $data['shop_id'] = $v['id'];
  618. $data['month'] = $start;
  619. $info = Dever::db('shop/buy_stat_month')->find($data);
  620. $data['cash'] = Dever::db('shop/buy_order_goods')->getCashNum($where);
  621. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  622. $data['goods'] = Dever::db('shop/buy_order')->getGoodsNum($where);
  623. if (!$info) {
  624. Dever::db('shop/buy_stat_month')->insert($data);
  625. } else {
  626. $data['where_id'] = $info['id'];
  627. Dever::db('shop/buy_stat_month')->update($data);
  628. }
  629. }
  630. }
  631. # 处理月度对账数据:仓库对账 废弃,直接用结算单处理
  632. public function store_month_api()
  633. {
  634. return;
  635. $num = Dever::input('num', -1);
  636. if ($num > 0) {
  637. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  638. } else {
  639. $month = Dever::input('start', date('Y-m'));
  640. }
  641. $where['status'] = '5,6';
  642. $start = Dever::maketime($month . '-01 00:00:00');
  643. $end = Dever::maketime($month . '-31 23:59:59');
  644. $store = Dever::db('store/info')->select();
  645. foreach ($store as $k => $v) {
  646. $where['type'] = 2;
  647. $where['type_id'] = $v['id'];
  648. $where['start'] = $start;
  649. $where['end'] = $end;
  650. $data = array();
  651. $data['store_id'] = $v['id'];
  652. $data['month'] = $start;
  653. $info = Dever::db('shop/store_stat_month')->find($data);
  654. $data['cash'] = Dever::db('shop/buy_order_goods')->getCashNum($where);
  655. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  656. $data['goods'] = Dever::db('shop/buy_order')->getGoodsNum($where);
  657. if (!$info) {
  658. Dever::db('shop/store_stat_month')->insert($data);
  659. } else {
  660. $data['where_id'] = $info['id'];
  661. Dever::db('shop/store_stat_month')->update($data);
  662. }
  663. }
  664. }
  665. # 处理月度对账数据:工厂对账 废弃,直接用结算单处理
  666. public function factory_month_api()
  667. {
  668. return;
  669. $num = Dever::input('num', -1);
  670. if ($num > 0) {
  671. $month = Dever::input('start', date('Y-m', strtotime('-'.$num.' month')));
  672. } else {
  673. $month = Dever::input('start', date('Y-m'));
  674. }
  675. $where['status'] = '5,6';
  676. $start = Dever::maketime($month . '-01 00:00:00');
  677. $end = Dever::maketime($month . '-31 23:59:59');
  678. $factory = Dever::db('factory/info')->select();
  679. foreach ($factory as $k => $v) {
  680. $where['source_type'] = 3;
  681. $where['source_id'] = $v['id'];
  682. $where['start'] = $start;
  683. $where['end'] = $end;
  684. $data = array();
  685. $data['factory_id'] = $v['id'];
  686. $data['month'] = $start;
  687. $info = Dever::db('shop/factory_stat_month')->find($data);
  688. $data['cash'] = Dever::db('shop/buy_order_goods')->getCashNum($where);
  689. $data['order'] = Dever::db('shop/buy_order')->getOrderNum($where);
  690. $data['goods'] = Dever::db('shop/buy_order')->getGoodsNum($where);
  691. if (!$info) {
  692. Dever::db('shop/factory_stat_month')->insert($data);
  693. } else {
  694. $data['where_id'] = $info['id'];
  695. Dever::db('shop/factory_stat_month')->update($data);
  696. }
  697. }
  698. }
  699. # 校正出厂价
  700. public function setPrice_api()
  701. {
  702. $data = Dever::db('shop/buy_order')->select();
  703. if ($data) {
  704. foreach ($data as $k => $v) {
  705. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $v['id']));
  706. if ($goods) {
  707. $p_price = 0;
  708. foreach ($goods as $v1) {
  709. $p_price += ($v1['p_price'] * $v1['num']);
  710. }
  711. Dever::db('shop/buy_order')->update(array('where_id' => $v['id'], 'p_price' => $p_price));
  712. }
  713. }
  714. }
  715. return 'ok';
  716. }
  717. #新增
  718. #平台商城订单表同步发货时间sell_order
  719. public function tongSellOrder_api()
  720. {
  721. $data = Dever::db('shop/sell_order')->state();
  722. foreach ($data as $k => $v) {
  723. if (isset($v['id']) && $v['id'] ) {
  724. $ps = Dever::db('shop/sell_order_ps')->find(array('order_id'=>$v['id']));
  725. $where['where_id'] = $v['id'];
  726. if (isset($ps) && $ps && isset($ps['cdate']) && $ps['cdate']) {
  727. $where['fadate'] = $ps['cdate'];
  728. Dever::db('shop/sell_order')->update($where);
  729. }
  730. }
  731. }
  732. }
  733. #门店采购发货时间
  734. public function tongBuyOrder_api()
  735. {
  736. $data = Dever::db('shop/buy_order')->state();
  737. foreach ($data as $k => $v) {
  738. if (isset($v['id']) && $v['id']) {
  739. $ps = Dever::db('shop/buy_order_ps')->find(array('order_id'=>$v['id']));
  740. $where['where_id'] = $v['id'];
  741. if (isset($ps) && $ps && isset($ps['cdate']) && $ps['cdate']) {
  742. $where['fadate'] = $ps['cdate'];
  743. Dever::db('shop/buy_order')->update($where);
  744. if ($v['parent_type'] == 2 && $v['parent_order_id'] > 0) {
  745. Dever::db('shop/sell_order')->update(array('where_id' => $v['parent_order_id'], 'fadate' => $ps['cdate']));
  746. $sell_order_ps = Dever::db('shop/sell_order_ps')->select(array('order_id'=>$v['parent_order_id']));
  747. if ($sell_order_ps) {
  748. foreach ($sell_order_ps as $k1 => $v1) {
  749. Dever::db('shop/sell_order_ps')->update(array('where_id' => $v1['id'], 'cdate' => $ps['cdate']));
  750. }
  751. }
  752. }
  753. }
  754. }
  755. }
  756. }
  757. # 处理配送服务商和配送时间
  758. public function upPs_api()
  759. {
  760. $where = array();
  761. $order_id = Dever::input('order_id');
  762. if ($order_id) {
  763. $where['order_id'] = $order_id;
  764. }
  765. /*
  766. $data = Dever::db('shop/buy_order_ps')->state($where);
  767. if ($data) {
  768. $update = array();
  769. foreach ($data as $k => $v) {
  770. $service = Dever::array_decode($v['service']);
  771. if ($service) {
  772. $update['service'] = array();
  773. foreach ($service as $k1 => $v1) {
  774. $update['service'][$k1] = $v1;
  775. if (isset($v1['service_id']) && $v1['service_id']) {
  776. $service_id = $v1['service_id'];
  777. $service_info = Dever::db('shop/service')->one($service_id);
  778. if ($service_info) {
  779. $service_new_info = Dever::db('shop/service')->getOne(array('name' => $service_info['name']));
  780. if ($service_new_info && $service_new_info['id'] != $service_id) {
  781. $update['service'][$k1]['service_id'] = $service_new_info['id'];
  782. Dever::db('shop/service')->update(array('where_id' => $service_info['id'], 'state' => 2));
  783. }
  784. }
  785. }
  786. }
  787. if ($update['service']) {
  788. $update['service'] = Dever::array_encode($update['service']);
  789. $update['where_id'] = $v['id'];
  790. Dever::db('shop/buy_order_ps')->update($update);
  791. }
  792. }
  793. }
  794. }
  795. if ($order_id) {
  796. return 'ok';
  797. }
  798. $data = Dever::db('shop/sell_order_ps')->state();
  799. if ($data) {
  800. $update = array();
  801. foreach ($data as $k => $v) {
  802. $service = Dever::array_decode($v['service']);
  803. if ($service) {
  804. $update['service'] = array();
  805. foreach ($service as $k1 => $v1) {
  806. $update['service'][$k1] = $v1;
  807. if (isset($v1['service_id']) && $v1['service_id']) {
  808. $service_id = $v1['service_id'];
  809. $service_info = Dever::db('shop/service')->one($service_id);
  810. if ($service_info) {
  811. $service_new_info = Dever::db('shop/service')->getOne(array('name' => $service_info['name']));
  812. if ($service_new_info && $service_new_info['id'] != $service_id) {
  813. $update['service'][$k1]['service_id'] = $service_new_info['id'];
  814. Dever::db('shop/service')->update(array('where_id' => $service_info['id'], 'state' => 2));
  815. }
  816. }
  817. }
  818. }
  819. if ($update['service']) {
  820. $update['service'] = Dever::array_encode($update['service']);
  821. $update['where_id'] = $v['id'];
  822. Dever::db('shop/sell_order_ps')->update($update);
  823. }
  824. }
  825. }
  826. }
  827. */
  828. $data = Dever::db('agent/dh_order_ps')->state();
  829. if ($data) {
  830. $update = array();
  831. foreach ($data as $k => $v) {
  832. $service = Dever::array_decode($v['service']);
  833. if ($service) {
  834. $update['service'] = array();
  835. foreach ($service as $k1 => $v1) {
  836. $update['service'][$k1] = $v1;
  837. if (isset($v1['service_id']) && $v1['service_id']) {
  838. $service_id = $v1['service_id'];
  839. $service_info = Dever::db('shop/service')->one($service_id);
  840. if ($service_info) {
  841. $service_new_info = Dever::db('shop/service')->getOne(array('name' => $service_info['name']));
  842. if ($service_new_info && $service_new_info['id'] != $service_id) {
  843. $update['service'][$k1]['service_id'] = $service_new_info['id'];
  844. Dever::db('shop/service')->update(array('where_id' => $service_info['id'], 'state' => 2));
  845. }
  846. }
  847. }
  848. }
  849. if ($update['service']) {
  850. $update['service'] = Dever::array_encode($update['service']);
  851. $update['where_id'] = $v['id'];
  852. Dever::db('agent/dh_order_ps')->update($update);
  853. }
  854. }
  855. }
  856. }
  857. return 'ok';
  858. }
  859. }