Cron.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. <?php
  2. namespace Bill\Lib;
  3. use Dever;
  4. class Cron
  5. {
  6. # 校正金额和业绩
  7. public function jz_api()
  8. {
  9. $w['state'] = 1;
  10. $id = Dever::input('id');
  11. if ($id) {
  12. $w['id'] = $id;
  13. }
  14. $member = Dever::db('agent/member')->select($w);
  15. if ($member) {
  16. foreach ($member as $v) {
  17. $where = array();
  18. $where['mid'] = $v['id'];
  19. /*
  20. $data = Dever::db('bill/cash')->getCash($where);
  21. if ($data && $data['total']) {
  22. Dever::db('agent/member')->update(array('where_id' => $v['id'], 'cash' => $data['total']));
  23. }
  24. */
  25. $update = array();
  26. $where['type'] = 1;
  27. $sell = Dever::db('bill/sell')->getTotal($where);
  28. if ($sell && $sell['total']) {
  29. $update['sell'] = $sell['total'];
  30. }
  31. $where['type'] = 2;
  32. $group_sell = Dever::db('bill/sell')->getTotal($where);
  33. if ($group_sell && $group_sell['total']) {
  34. $update['group_sell'] = $group_sell['total'];
  35. }
  36. if ($update) {
  37. $update['where_id'] = $v['id'];
  38. Dever::db('agent/member')->update($update);
  39. }
  40. }
  41. }
  42. return 'ok';
  43. }
  44. public function sell_api()
  45. {
  46. # 按月份统计团队业绩和直推业绩
  47. $month = Dever::input('month', date('Y-m'));
  48. $start_time = $month .'-01 00:00:00';
  49. $end_time = date('Y-m-d', strtotime($start_time. ' +1 month -1 day')).' 23:59:59';
  50. // $where['type'] = '2';
  51. $where['start'] = Dever::maketime($start_time);
  52. $where['end'] = Dever::maketime($end_time);
  53. $data = Dever::db('bill/sell')->getNumByTime($where);
  54. if ($data) {
  55. foreach ($data as $k => $v) {
  56. if ($v['num'] > 0) {
  57. $w = array();
  58. $w['mid'] = $v['mid'];
  59. $w['month'] = $where['start'];
  60. $w['clear'] = true;
  61. $info = Dever::db('bill/sell_stat')->find($w);
  62. $where['mid'] = $v['mid'];
  63. $where['type'] = $v['type'];
  64. $total = Dever::db('bill/sell')->getNumByTimeOne($where);
  65. if ($v['type'] == 1) {
  66. $w['new_ztnum'] = $v['num'];
  67. if ($total) {
  68. $w['zt_num'] = $total['num'];
  69. }
  70. } else {
  71. $w['new_num'] = $v['num'];
  72. if ($total) {
  73. $w['num'] = $total['num'];
  74. }
  75. }
  76. if ($info) {
  77. $w['where_id'] = $info['id'];
  78. Dever::db('bill/sell_stat')->update($w);
  79. } else {
  80. Dever::db('bill/sell_stat')->insert($w);
  81. }
  82. }
  83. }
  84. }
  85. return 'ok';
  86. }
  87. # 权益使用情况统计:类型、商品名、总发放数量、总使用数量
  88. public function goods_api()
  89. {
  90. $num = Dever::input('num', 5);
  91. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  92. $end = Dever::input('end', date('Y-m-d'));
  93. $start = Dever::maketime($start . ' 00:00:00');
  94. $end = Dever::maketime($end . ' 23:59:59');
  95. $day = intval(($end - $start)/86400);
  96. for($i=0; $i<=$day; $i++) {
  97. $where['start'] = $start + 86400*$i;
  98. $where['end'] = $start + 86400*$i + 86399;
  99. $goods = Dever::db('agent/member_goods')->findAll($where);
  100. $dh_order = Dever::db('agent/dh_order')->getDhNum(array('status'=>3,'start'=>$where['start'],'end'=>$where['end'],'state'=>1));
  101. // print_R($dh_order);die;
  102. if ($goods) {
  103. foreach ($goods as $k => $v) {
  104. $data = array();
  105. $data['day'] = $where['start'];
  106. $data['type'] = $v['type'];
  107. $data['type_id'] = $v['type_id'];
  108. $data['sku_id'] = $v['sku_id'];
  109. $info = Dever::db('bill/goods_stat')->find($data);
  110. $data['total_num'] = $v['total_num'];
  111. $data['sell_num'] = $v['sell_num'];
  112. $data['cash'] = $v['cash'];
  113. $data['y_cash'] = $v['y_cash'];
  114. $data['w_cash'] = $v['cash'] - $v['y_cash'];
  115. $data['name'] = $v['name'];
  116. $data['sell_nonum'] = $dh_order;
  117. if ($info) {
  118. $data['where_id'] = $info['id'];
  119. Dever::db('bill/goods_stat')->update($data);
  120. } else {
  121. Dever::db('bill/goods_stat')->insert($data);
  122. }
  123. }
  124. }
  125. }
  126. return 'ok';
  127. }
  128. # 区域分润
  129. /*
  130. 区域毛利 = 区域内所有门店的采购商品的零售价总额 x 30%
  131. 城市分润 = 区域毛利 x 1%
  132. 区县分润 = 区域毛利 x 3%
  133. 街道分润 = 区域毛利 x 5%
  134. */
  135. public function area_api()
  136. {
  137. # 获取所有代理商
  138. $where['status'] = 2;
  139. $member = Dever::db('agent/member')->select($where);
  140. if ($member) {
  141. foreach ($member as $k => $v) {
  142. # 获取区域内门店的利润
  143. $role = Dever::db('setting/role')->find($v['role']);
  144. $profit = $role['profit'];
  145. if (!$profit) {
  146. continue;
  147. }
  148. $temp = explode(',', $v['area']);
  149. if ($role['type'] == 2) {
  150. $area = '';
  151. if ($role['area'] == 1 && isset($temp[1])) {
  152. # 城市
  153. $area = $temp[0] . ',' . $temp[1];
  154. } elseif ($role['area'] == 2 && isset($temp[2])) {
  155. # 区县
  156. $area = $temp[0] . ',' . $temp[1] . ',' . $temp[2];
  157. } elseif ($role['area'] == 3 && isset($temp[3])) {
  158. # 街道
  159. $area = $temp[0] . ',' . $temp[1] . ',' . $temp[2] . ',' . $temp[3];
  160. }
  161. if ($area) {
  162. }
  163. }
  164. }
  165. }
  166. }
  167. # 营业额报表:日期,订单数,确认订单数,确认订单额度,新增代理商数量
  168. public function order_api()
  169. {
  170. $num = Dever::input('num', 5);
  171. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  172. $end = Dever::input('end', date('Y-m-d'));
  173. $start = Dever::maketime($start . ' 00:00:00');
  174. $end = Dever::maketime($end . ' 23:59:59');
  175. $day = intval(($end - $start)/86400);
  176. for($i=0; $i<=$day; $i++) {
  177. $where = array();
  178. $where['fstart'] = $start + 86400*$i;
  179. $where['fend'] = $start + 86400*$i + 86399;
  180. $data = array();
  181. $data['day'] = $where['fstart'];
  182. $info = Dever::db('bill/order_stat')->find($data);
  183. $data['num'] = Dever::db('agent/order')->getNum($where);
  184. $where['status'] = 4;
  185. $data['yes_num'] = Dever::db('agent/order')->getNum($where);
  186. $cash = Dever::db('agent/order')->getCash($where);
  187. if ($cash) {
  188. $data['cash'] = $cash['total'];
  189. } else {
  190. $data['cash'] = 0;
  191. }
  192. $agent_cash = Dever::db('agent/order')->getDailiCash($where);
  193. if ($agent_cash) {
  194. $data['agent_cash'] = $agent_cash['total'];
  195. } else {
  196. $data['agent_cash'] = 0;
  197. }
  198. $where['status'] = 2;
  199. $data['daili_num'] = Dever::db('agent/member')->getNum($where);
  200. $where['status'] = '-2,1,2,3,6';//驳回不加入
  201. $where['start'] = $where['fstart'];
  202. $where['end'] = $where['fend'];
  203. unset($where['fstart']);
  204. unset($where['fend']);
  205. $data['no_num'] = Dever::db('agent/order')->getNum($where);
  206. $no_cash = Dever::db('agent/order')->getCash($where);
  207. if ($no_cash) {
  208. $data['no_cash'] = $no_cash['total'];
  209. } else {
  210. $data['no_cash'] = 0;
  211. }
  212. if (!$info) {
  213. Dever::db('bill/order_stat')->insert($data);
  214. } else {
  215. $data['where_id'] = $info['id'];
  216. Dever::db('bill/order_stat')->update($data);
  217. }
  218. }
  219. }
  220. # 代理商统计:日期,新增代理商,城市代理商,区县代理商,街道代理商,创客,消客
  221. public function member_api()
  222. {
  223. $num = Dever::input('num', 5);
  224. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  225. $end = Dever::input('end', date('Y-m-d'));
  226. $start = Dever::maketime($start . ' 00:00:00');
  227. $end = Dever::maketime($end . ' 23:59:59');
  228. $day = intval(($end - $start)/86400);
  229. for($i=0; $i<=$day; $i++) {
  230. $where = array();
  231. $where['status'] = 2;
  232. $where['fstart'] = $start + 86400*$i;
  233. $where['fend'] = $start + 86400*$i + 86399;
  234. $data = array();
  235. $data['day'] = $where['fstart'];
  236. $info = Dever::db('bill/member_stat')->find($data);
  237. $data['num'] = Dever::db('agent/member')->getNum($where);
  238. $where['role'] = 2;
  239. $data['city_num'] = Dever::db('agent/member')->getNum($where);
  240. $where['role'] = 3;
  241. $data['county_num'] = Dever::db('agent/member')->getNum($where);
  242. $where['role'] = 4;
  243. $data['town_num'] = Dever::db('agent/member')->getNum($where);
  244. $where['role'] = 5;
  245. $data['cf_num'] = Dever::db('agent/member')->getNum($where);
  246. $where['role'] = 6;
  247. $data['ck_num'] = Dever::db('agent/member')->getNum($where);
  248. $where['role'] = 8;
  249. $data['cv_num'] = Dever::db('agent/member')->getNum($where);
  250. if (!$info) {
  251. Dever::db('bill/member_stat')->insert($data);
  252. } else {
  253. $data['where_id'] = $info['id'];
  254. Dever::db('bill/member_stat')->update($data);
  255. }
  256. }
  257. }
  258. # 资金账户统计:日期,总收入,代理费,软件服务费,赠送商品价值,直推佣金,市场培训费
  259. # 总收入金额 = 代理费金额 + 软件服务费 - 赠送商品价值 - 直推分佣 - 市场培训费 - 活动分佣 - 手动发放
  260. public function cash_in_api()
  261. {
  262. $num = Dever::input('num', 5);
  263. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  264. $end = Dever::input('end', date('Y-m-d'));
  265. $start = Dever::maketime($start . ' 00:00:00');
  266. $end = Dever::maketime($end . ' 23:59:59');
  267. $day = intval(($end - $start)/86400);
  268. for($i=0; $i<=$day; $i++) {
  269. $where = array();
  270. $where['fstart'] = $start + 86400*$i;
  271. $where['fend'] = $start + 86400*$i + 86399;
  272. $data = array();
  273. $data['day'] = $where['fstart'];
  274. $info = Dever::db('bill/cash_in_stat')->find($data);
  275. $where['status'] = 4;
  276. $data['daili_cash'] = 0;
  277. $cash = Dever::db('agent/order')->getDailiCash($where);
  278. if ($cash) {
  279. $data['daili_cash'] = $cash['total'];
  280. }
  281. $data['soft_cash'] = 0;
  282. $cash = Dever::db('agent/order')->getSoftCash($where);
  283. if ($cash) {
  284. $data['soft_cash'] = $cash['total'];
  285. }
  286. $data['goods_cash'] = 0;
  287. $cash = Dever::db('agent/order')->getGoodsCash($where);
  288. if ($cash) {
  289. $data['goods_cash'] = $cash['total'];
  290. }
  291. unset($where['status']);
  292. $where['start'] = $where['fstart'];
  293. $where['end'] = $where['fend'];
  294. $where['type'] = 1;
  295. $data['zt_cash'] = 0;
  296. $cash = Dever::db('bill/cash')->getCash($where);
  297. if ($cash) {
  298. $data['zt_cash'] = $cash['total'];
  299. }
  300. $where['type'] = 2;
  301. $data['sc_cash'] = 0;
  302. $cash = Dever::db('bill/cash')->getCash($where);
  303. if ($cash) {
  304. $data['sc_cash'] = $cash['total'];
  305. }
  306. $where['type'] = 4;
  307. $data['hd_cash'] = 0;
  308. $cash = Dever::db('bill/cash')->getCash($where);
  309. if ($cash) {
  310. $data['hd_cash'] = $cash['total'];
  311. }
  312. $where['type'] = 10;
  313. $data['sd_cash'] = 0;
  314. $cash = Dever::db('bill/cash')->getCash($where);
  315. if ($cash) {
  316. $data['sd_cash'] = $cash['total'];
  317. }
  318. #间推
  319. $where['type'] =3;
  320. $data['jt_cash'] =0;
  321. $cash = Dever::db('bill/cash')->getCash($where);
  322. if ($cash) {
  323. $data['jt_cash'] = $cash['total'];
  324. }
  325. $data['cash'] = $data['daili_cash'] + $data['soft_cash'] - $data['goods_cash'] - $data['sc_cash'] - $data['zt_cash'] - $data['hd_cash'] - $data['sd_cash'] - $data['jt_cash'];
  326. if (!$info) {
  327. Dever::db('bill/cash_in_stat')->insert($data);
  328. } else {
  329. $data['where_id'] = $info['id'];
  330. Dever::db('bill/cash_in_stat')->update($data);
  331. }
  332. }
  333. return 'ok';
  334. }
  335. # 应付金额 = 现存余额 + 未审核提现 + 已审核待发放提现。
  336. /*public function cash_out_api()
  337. {
  338. $num = Dever::input('num', 1);
  339. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  340. $end = Dever::input('end', date('Y-m-d'));
  341. $start = Dever::maketime($start . ' 00:00:00');
  342. $end = Dever::maketime($end . ' 23:59:59');
  343. $day = intval(($end - $start)/86400);
  344. for($i=0; $i<=$day; $i++) {
  345. $where = array();
  346. $where['start'] = $start + 86400*$i;
  347. $where['end'] = $start + 86400*$i + 86399;
  348. $data = array();
  349. $data['day'] = $where['start'];
  350. $info = Dever::db('bill/cash_in_stat')->find($data);
  351. $where['status'] = 4;
  352. $data['daili_cash'] = 0;
  353. $cash = Dever::db('agent/order')->getDailiCash($where);
  354. if ($cash) {
  355. $data['daili_cash'] = $cash['total'];
  356. }
  357. $data['soft_cash'] = 0;
  358. $cash = Dever::db('agent/order')->getSoftCash($where);
  359. if ($cash) {
  360. $data['soft_cash'] = $cash['total'];
  361. }
  362. $data['goods_cash'] = 0;
  363. $cash = Dever::db('agent/order')->getGoodsCash($where);
  364. if ($cash) {
  365. $data['goods_cash'] = $cash['total'];
  366. }
  367. unset($where['status']);
  368. $where['type'] = 1;
  369. $data['zt_cash'] = 0;
  370. $cash = Dever::db('bill/cash')->getCash($where);
  371. if ($cash) {
  372. $data['zt_cash'] = $cash['total'];
  373. }
  374. $where['type'] = 2;
  375. $data['sc_cash'] = 0;
  376. $cash = Dever::db('bill/cash')->getCash($where);
  377. if ($cash) {
  378. $data['sc_cash'] = $cash['total'];
  379. }
  380. $data['cash'] = $data['daili_cash'] + $data['soft_cash'] - $data['goods_cash'] - $data['sc_cash'] - $data['zt_cash'];
  381. if (!$info) {
  382. Dever::db('bill/cash_in_stat')->insert($data);
  383. } else {
  384. $data['where_id'] = $info['id'];
  385. Dever::db('bill/cash_in_stat')->update($data);
  386. }
  387. }
  388. }
  389. */
  390. #财务应付报表:
  391. #应付金额 = 现存余额 + 未审核提现 + 已审核待发放提现
  392. public function cash_up_api()
  393. {
  394. $num = Dever::input('num', 5);
  395. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  396. $end = Dever::input('end', date('Y-m-d'));
  397. $start = Dever::maketime($start . ' 00:00:00');
  398. $end = Dever::maketime($end . ' 23:59:59');
  399. $day = intval(($end - $start)/86400);
  400. for($i=0; $i<=$day; $i++) {
  401. $where = array();
  402. $where['start'] = $start + 86400*$i;
  403. $where['end'] = $start + 86400*$i + 86399;
  404. $data = array();
  405. $data['day'] = $where['start'];
  406. $info = Dever::db('bill/cash_up_stat')->find($data);
  407. /*
  408. 现存余额=tixian.php下的cash
  409. 未审核提现=cash下的status=1,type=11
  410. 已审核待发放提现=tixian.php下的status=1,cash
  411. */
  412. #现存余额
  413. $data['xc_cash']=0;
  414. $where['status'] = 2;
  415. $cash=Dever::db('bill/tixian')->gettxCash($where);
  416. if($cash){
  417. $data['xc_cash']=$cash['total']*(-1);
  418. }
  419. #未审核提现
  420. $where['status']=-1;
  421. $data['ws_cash']=0;
  422. // print_R($where);die;
  423. $tixian=Dever::db('bill/tixian')->getwsCash($where);
  424. if($tixian){
  425. // $where['status']=1;
  426. // $where['type']=11;
  427. // $where['mid']=$tixian['mid'];
  428. // $cash=Dever::db('bill/cash')->getwsCash($where);
  429. $data['ws_cash']=$tixian['total']*(-1);
  430. }
  431. #已审核待发放提现
  432. $data['df_cash']=0;
  433. $where['status']=1;
  434. $where['state'] = 1;
  435. $cash=Dever::db('bill/tixian')->getdfCash($where);
  436. if($cash){
  437. $data['df_cash'] = $cash['total'] *(-1);
  438. }
  439. $data['cash'] = $data['xc_cash'] + $data['ws_cash'] + $data['df_cash'];
  440. if (!$info) {
  441. Dever::db('bill/cash_up_stat')->insert($data);
  442. } else {
  443. $data['where_id'] = $info['id'];
  444. Dever::db('bill/cash_up_stat')->update($data);
  445. }
  446. }
  447. }
  448. public function getShopInfo ($data, $name, $param)
  449. {
  450. $manage = array();
  451. $month = Dever::param('month', $param);
  452. if ($data) {
  453. $delete = array('一键生成归店信息');
  454. foreach ($data as $k => $v) {
  455. if ($v['cash_status'] == 1) {
  456. # 已发放
  457. $delete[] = '对账确认';
  458. $delete[] = '确认发放';
  459. $delete[] = '重新生成归店信息';
  460. break;
  461. } else {
  462. if ($v['status'] == 1) {
  463. # 已确认
  464. $delete[] = '对账确认';
  465. $delete[] = '重新生成归店信息';
  466. } else {
  467. $delete[] = '确认发放';
  468. }
  469. }
  470. }
  471. $log = Dever::db('bill/shop_stat_log')->find(array('month' => $month));
  472. if ($log) {
  473. $audit = array();
  474. if ($log['create_admin']) {
  475. $admin = Dever::db('manage/admin')->find($log['create_admin']);
  476. if ($admin) {
  477. $date = date('Y-m-d H:i', $log['create_date']);
  478. $audit[] = '创建人:' . $admin['username'] . ' 创建时间:' . $date;
  479. }
  480. }
  481. if ($log['confirm_admin']) {
  482. $admin = Dever::db('manage/admin')->find($log['confirm_admin']);
  483. if ($admin) {
  484. $date = date('Y-m-d H:i', $log['confirm_date']);
  485. $audit[] = '对账人:' . $admin['username'] . ' 对账时间:' . $date;
  486. }
  487. }
  488. if ($log['send_admin']) {
  489. $admin = Dever::db('manage/admin')->find($log['send_admin']);
  490. if ($admin) {
  491. $date = date('Y-m-d H:i', $log['send_date']);
  492. $audit[] = '发放人:' . $admin['username'] . ' 发放时间:' . $date;
  493. }
  494. }
  495. if ($audit) {
  496. $audit = '<br />' . implode('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $audit);
  497. } else {
  498. $audit = '';
  499. }
  500. $manage['info'] = '代理商总数=创V数+代理商数量,创V数包含名额外创V数,创V数减去名额外创V就是名额内创V数,代理商数量包含创客数'. $audit;
  501. }
  502. $manage['deleteButton'] = $delete;
  503. } else {
  504. Dever::setInput('day', Dever::input('search_option_month'));
  505. list($start, $end) = Dever::month();
  506. $manage['deleteButton'] = array('重新生成归店信息', '对账确认', '确认发放');
  507. }
  508. Dever::config('base')->manage = $manage;
  509. }
  510. #归店统计
  511. public function shop_stat_api(){
  512. // $month = Dever::input('month');
  513. // $y = date('Y');
  514. // if($month){
  515. // $month = $month;
  516. // }else{
  517. // $m = date('m')-1;
  518. // if ($m == 0) {
  519. // $y = $y - 1;
  520. // $m = 12;
  521. // }
  522. // $month = $y.'-'.$m;
  523. // }
  524. # 传入day,获取月开始和月结束日期
  525. list($where['start'], $where['end']) = Dever::month();
  526. $month = date('Y-m', $where['start']);
  527. // print_R($month);die;
  528. $start_time = $month .'-01 00:00:00';
  529. $end_time = date('Y-m-d', strtotime($start_time. ' +1 month -1 day')).' 23:59:59';
  530. $start = Dever::maketime($start_time);
  531. $end = Dever::maketime($end_time);
  532. $shops = Dever::db('shop/info')->getguiAll(array('type'=>2));
  533. foreach ($shops as $k => $v) {
  534. #业绩补贴:除了创V0.02,创V>=10个0.05,创V>=20个0.08
  535. // $subsidy = 0;
  536. $w = array('end' => $end,'new_shop_id'=> $v['id']);
  537. $member = Dever::db('agent/member_shop_log')->getAllByShop($w);
  538. if ($member) {
  539. $member_mid = implode(',', array_keys($member));
  540. $w['start'] = $start;
  541. $cmember = Dever::db('agent/member_shop_log')->getAllByShop($w);
  542. $insert = array();
  543. $insert['shop_id'] = $v['id'];
  544. $insert['month'] = $start;
  545. $insert['c_money'] = 0;
  546. $insert['mids'] = $member_mid;
  547. $insert['month_mids'] = array();
  548. $insert['num'] = 0;
  549. $insert['dl_num'] = 0;
  550. $insert['v_num'] = 0;
  551. $insert['ck_num'] = 0;
  552. $insert['out_num'] = 0;
  553. $insert['out_money'] = 0;
  554. $insert['nei_money'] = 0;
  555. $insert['subsidy'] = 0;
  556. $insert['dl_subsidy'] = 0;
  557. $insert['v_subsidy'] = 0;
  558. $out_member = array();
  559. $nei_member = array();
  560. if ($cmember) {
  561. foreach ($cmember as $k1 => $v1) {
  562. $vinfo = Dever::db('agent/member')->find($v1['mid']);
  563. if ($vinfo) {
  564. $insert['month_mids'][] = $v1['mid'];
  565. $insert['num'] += 1;
  566. if ($vinfo['role'] == 8) {
  567. $insert['v_num'] += 1;
  568. if ($vinfo['prize_type'] == 1) {
  569. $insert['out_num'] += 1;
  570. }
  571. } else {
  572. $insert['dl_num'] += 1;
  573. }
  574. if ($vinfo['role'] == 6) {
  575. $insert['ck_num'] += 1;
  576. }
  577. }
  578. }
  579. $insert['month_mids'] = implode(',', $insert['month_mids']);
  580. }
  581. foreach ($member as $k1 => $v1) {
  582. $vinfo = Dever::db('agent/order')->select(array('parent_mid' => $v1['mid'], 'role' => 8));
  583. if ($vinfo) {
  584. foreach ($vinfo as $v2) {
  585. if ($v2['prize_type'] == 1) {
  586. $out_member[] = $v2['mid'];
  587. } else {
  588. $nei_member[] = $v2['mid'];
  589. }
  590. }
  591. }
  592. }
  593. $c_money = Dever::db('shop/buy_order')->getCashNum(array('type'=>1,'type_id'=>$v['id'],'start' => $start, 'end' => $end, 'status' => '3,4,5,6'));
  594. if ($c_money) {
  595. $insert['c_money'] = $c_money['total'];
  596. }
  597. $where = array
  598. (
  599. 'parent_mid' => $member_mid,
  600. 'start' => $start,
  601. 'end' => $end
  602. );
  603. $money = Dever::db('agent/order')->getCount($where);
  604. if ($money) {
  605. $insert['money'] = $money['total'];
  606. }
  607. $where['role'] = '1,2,3,4,5,6';
  608. // $where['role'] = '1,2,3,4,5,6,7';
  609. $dl_money = Dever::db('agent/order')->getCount($where);
  610. if ($dl_money) {
  611. $insert['dl_money'] = $dl_money['total'];
  612. } else {
  613. $insert['dl_money'] = 0;
  614. }
  615. #除去创V的补贴
  616. if ($insert['dl_money']) {
  617. $insert['dl_subsidy'] = $insert['dl_money'] * 0.02;
  618. }
  619. $where['role'] = 8;
  620. unset($where['parent_mid']);
  621. if ($out_member) {
  622. $where['mid'] = $out_member;
  623. $out_money = Dever::db('agent/order')->getCount($where);
  624. if ($out_money) {
  625. $insert['out_money'] = $out_money['total'];
  626. }
  627. }
  628. if ($insert['out_money']) {
  629. if ($insert['out_money'] >= 100000 && $insert['out_money'] <200000) {
  630. $insert['v_subsidy'] = $insert['out_money'] * 0.05;
  631. } elseif ($insert['out_money'] >=200000) {
  632. $insert['v_subsidy'] = $insert['out_money'] * 0.08;
  633. }
  634. }
  635. if ($nei_member) {
  636. $where['mid'] = $nei_member;
  637. $nei_money = Dever::db('agent/order')->getCount($where);
  638. if ($nei_money) {
  639. $insert['nei_money'] = $nei_money['total'];
  640. }
  641. }
  642. // print_R($insert);die;
  643. $res = Dever::db('bill/shop_stat')->find(array('month'=>$insert['month'],'shop_id'=>$v['id']));
  644. if ($res) {
  645. $insert['where_id'] = $res['id'];
  646. Dever::db('bill/shop_stat')->update($insert);
  647. } else {
  648. Dever::db('bill/shop_stat')->insert($insert);
  649. }
  650. } else {
  651. $insert = array();
  652. $insert['shop_id'] = $v['id'];
  653. $insert['month'] = $start;
  654. $insert['c_money'] = 0;
  655. $insert['mids'] = '';
  656. $insert['month_mids'] = array();
  657. $insert['num'] = 0;
  658. $insert['dl_num'] = 0;
  659. $insert['v_num'] = 0;
  660. $insert['ck_num'] = 0;
  661. $insert['out_num'] = 0;
  662. $insert['out_money'] = 0;
  663. $insert['nei_money'] = 0;
  664. $insert['subsidy'] = 0;
  665. $insert['dl_subsidy'] = 0;
  666. $insert['v_subsidy'] = 0;
  667. $res = Dever::db('bill/shop_stat')->find(array('month'=>$insert['month'],'shop_id'=>$v['id']));
  668. // print_R($res);die;
  669. if ($res) {
  670. $insert['where_id'] = $res['id'];
  671. Dever::db('bill/shop_stat')->update($insert);
  672. } else {
  673. Dever::db('bill/shop_stat')->insert($insert);
  674. }
  675. // echo dever::sql();
  676. }
  677. }
  678. $this->addAreaLog('create', $month);
  679. return 'reload';
  680. }
  681. private function addAreaLog($col, $month)
  682. {
  683. $admin = Dever::load('manage/auth.info');
  684. $update['month'] = $month;
  685. $log = Dever::db('bill/shop_stat_log')->find(array('month' => $month));
  686. $update[$col . '_date'] = time();
  687. $update[$col . '_admin'] = $admin['id'];
  688. if ($log) {
  689. $update['where_id'] = $log['id'];
  690. Dever::db('bill/shop_stat_log')->update($update);
  691. } else {
  692. Dever::db('bill/shop_stat_log')->insert($update);
  693. }
  694. }
  695. # 确认对账
  696. public function shopYes_commit_api()
  697. {
  698. $month = Dever::input('month');
  699. if ($month) {
  700. $month = Dever::maketime($month);
  701. $state = Dever::db('bill/shop_stat')->updates(array('option_month' => $month, 'option_status' => 2, 'set_status' => 1));
  702. if ($state) {
  703. $this->addAreaLog('confirm', $month);
  704. }
  705. }
  706. return 'reload';
  707. }
  708. # 确认发放
  709. public function areaCash_commit_api()
  710. {
  711. $month = Dever::input('month');
  712. if ($month) {
  713. $month = Dever::maketime($month);
  714. $data = Dever::db('bill/shop_stat')->select(array('month' => $month, 'cash_status' => 2));
  715. if ($data) {
  716. $date = date('Y年m月', $month);
  717. foreach ($data as $k => $v) {
  718. if ($v['shop_id']) {
  719. $shop = Dever::db('shop/info')->find($v['shop_id']);
  720. if (isset($shop) && $shop && isset($shop['agent_mid']) && $shop['agent_mid']) {
  721. $member = Dever::db('agent/member')->find($shop['agent_mid']);
  722. $cash = $v['dl_subsidy'] + $v['v_subsidy'];
  723. if ($member && $member['status'] == 2) {
  724. if ($v['dl_subsidy'] > 0) {
  725. $desc = $shop['name'].$date.'区域招商补贴';
  726. $state = Dever::load('bill/lib/cash')->up($member['id'], 13, $v['dl_subsidy'], $member['role'], $v['id'], $desc, 2, true);
  727. }
  728. if ($v['v_subsidy'] >0) {
  729. $desc = $shop['name'].$date.'名额外创V补贴';
  730. $state = Dever::load('bill/lib/cash')->up($member['id'], 14, $v['v_subsidy'], $member['role'], $v['id'], $desc, 2, true);
  731. }
  732. if (isset($state) && $state) {
  733. // $where = array();
  734. // $where['where_id'] = $shop['id'];
  735. // $where['price'] = $shop['price'] + $cash;
  736. // $where['clear'] = true;
  737. // Dever::db('shop/info')->upCash($where);
  738. Dever::db('bill/shop_stat')->update(array('where_id' => $v['id'], 'cash_status' => 1));
  739. }
  740. }
  741. }
  742. }
  743. }
  744. $this->addAreaLog('send', $month);
  745. }
  746. }
  747. return 'reload';
  748. }
  749. # 角色统计
  750. public function role_api()
  751. {
  752. $num = Dever::input('num', 5);
  753. $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
  754. $end = Dever::input('end', date('Y-m-d'));
  755. $start = Dever::maketime($start . ' 00:00:00');
  756. $end = Dever::maketime($end . ' 23:59:59');
  757. $day = intval(($end - $start)/86400);
  758. $status = 4;
  759. $role = Dever::load('setting/role-state');
  760. for($i=0; $i<=$day; $i++) {
  761. $where = array();
  762. $where['fstart'] = $start + 86400*$i;
  763. $where['fend'] = $start + 86400*$i + 86399;
  764. $where['status'] = 4;
  765. foreach ($role as $k => $v) {
  766. $data = array();
  767. $data['day'] = $where['fstart'];
  768. $data['role'] = $v['id'];
  769. $data['clear'] = true;
  770. $where['role'] = $v['id'];
  771. $info = Dever::db('bill/role_stat')->find($data);
  772. $data['num'] = Dever::db('agent/order')->getNum($where);
  773. if ($data['num'] && $data['num'] > 0) {
  774. $cash = Dever::db('agent/order')->getCash($where);
  775. if ($cash) {
  776. $data['cash'] = $cash['total'];
  777. } else {
  778. $data['cash'] = 0;
  779. }
  780. $data['touru'] = $data['cash'];
  781. $cash = Dever::db('agent/member')->getCash($where);
  782. if ($cash) {
  783. $data['shouru'] = $cash['total'];
  784. } else {
  785. $data['shouru'] = 0;
  786. }
  787. if (!$info) {
  788. Dever::db('bill/role_stat')->insert($data);
  789. } else {
  790. $data['where_id'] = $info['id'];
  791. Dever::db('bill/role_stat')->update($data);
  792. }
  793. }
  794. }
  795. }
  796. return 'ok';
  797. }
  798. }