Manage.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. <?php
  2. namespace Stats\Lib;
  3. use Dever;
  4. Class Manage
  5. {
  6. public function index_api()
  7. {
  8. $data = array();
  9. $start = Dever::input('start');
  10. $end = Dever::input('end');
  11. if (!$start && !$end) {
  12. $start=Dever::maketime(date('Y-m-01') . ' 00:00:00');
  13. $end = strtotime(date('Y-m-d 23:59:59',
  14. strtotime(date('Y-m-01')."+1 month - 1 day")));
  15. $data['start'] = date('Y-m-d',$start);
  16. $data['end'] = date('Y-m-d',$end);
  17. } else {
  18. $data['start'] = date('Y-m-d',$start);
  19. $data['end'] = date('Y-m-d',$end);
  20. }
  21. $fastart = Dever::input('fastart');
  22. $faend = Dever::input('faend');
  23. if (!$fastart && !$faend) {
  24. $fastart=Dever::maketime(date('Y-m-01') . ' 00:00:00');
  25. $faend = strtotime(date('Y-m-d 23:59:59',
  26. strtotime(date('Y-m-01')."+1 month - 1 day")));
  27. $data['fastart'] = date('Y-m-d',$fastart);
  28. $data['faend'] = date('Y-m-d',$faend);
  29. } else {
  30. $data['fastart'] = date('Y-m-d',$fastart);
  31. $data['faend'] = date('Y-m-d',$faend);
  32. }
  33. $fstart = Dever::input('fstart');
  34. $fend = Dever::input('fend');
  35. if (!$fstart && !$fend) {
  36. $fstart=Dever::maketime(date('Y-m-01') . ' 00:00:00');
  37. $fend = strtotime(date('Y-m-d 23:59:59',
  38. strtotime(date('Y-m-01')."+1 month - 1 day")));
  39. $data['fstart'] = date('Y-m-d',$fstart);
  40. $data['fend'] = date('Y-m-d',$fend);
  41. } else {
  42. $data['fstart'] = date('Y-m-d',$fstart);
  43. $data['fend'] = date('Y-m-d',$fend);
  44. }
  45. $data['type'] = array(1 => '体验店',2 => '零售店',10 => '平台商城');
  46. return Dever::render('out', $data);
  47. }
  48. public function list_api()
  49. {
  50. $status = Dever::db('shop/sell_order')->config['status_manage'];
  51. $start = Dever::input('start');
  52. $end = Dever::input('end');
  53. $fastart = Dever::input('fastart');
  54. $faend = Dever::input('faend');
  55. $fstart = Dever::input('fstart');
  56. $fend = Dever::input('fend');
  57. $cate = Dever::input('cate');
  58. $name = Dever::input('goods_name');
  59. $shop_name = Dever::input('shop_name');
  60. $order_num = Dever::input('order_num');
  61. if ($start) {
  62. $where['start'] = strtotime($start);
  63. }
  64. if ($end) {
  65. $where['end'] = strtotime($end);
  66. }
  67. if ($fastart) {
  68. $where['fastart'] = strtotime($fastart);
  69. }
  70. if ($faend) {
  71. $where['faend'] = strtotime($faend);
  72. }
  73. if ($fstart) {
  74. $where['fstart'] = strtotime($fstart);
  75. }
  76. if ($fend) {
  77. $where['fend'] = strtotime($fend);
  78. }
  79. if ($cate && $cate>0) {
  80. $where['shop_type'] = $cate;
  81. }
  82. if ($name) {
  83. $where['name'] = $name;
  84. }
  85. if ($shop_name) {
  86. $shop = Dever::db('shop/info')->find(array('name'=>$name));
  87. if ($shop) {
  88. $where['shop_id'] = $shop['id'];
  89. }
  90. }
  91. if ($order_num) {
  92. $where['order_num'] = $order_num;
  93. }
  94. $where['state'] = 1;
  95. $data = Dever::db('shop/sell_order')->getExcel($where);
  96. $header = array('门店类型', '门店编号', '门店名称', '订单号', '商品编码', '存货编码', '商品名称', '规格型号', '税率', '商品单价', '购买数量', '商品合计金额', '代理优惠金额', '商品实付金额', '礼品卡金额', '钱包金额','商品支付金额', '礼品卡名称', '礼品卡卡号', '代理商注册时间', '下单时间', '付款时间', '发货时间', '完成时间', '订单总数量', '订单总金额', '订单实付金额', '礼品卡金额', '钱包金额', '优惠券金额', '代理优惠金额', '订单状态', '退款状态', '退款时间', '退款金额','退卡金额','退现金金额', '收件人', '收件人手机', '收件人地址');
  97. $body = array();
  98. if ($data) {
  99. foreach ($data as $k => $v) {
  100. $shop_type = '';
  101. if ($v['shop_type'] == 1) {
  102. $shop_type = '体验店';
  103. } elseif ($v['shop_type'] == 2) {
  104. $shop_type = '平台商城';
  105. }
  106. $shop = Dever::db('shop/info')->find($v['shop_id']);
  107. $sid = '';
  108. $shop_name = '';
  109. if ($shop) {
  110. if (isset($shop['sid']) && $shop['sid'] ) {
  111. $sid = $shop['sid'];
  112. }
  113. if (isset($shop['name']) && $shop['name']) {
  114. $shop_name = $shop['name'];
  115. }
  116. }
  117. $v['card'] = '';
  118. if ($v['card_code_id']) {
  119. $code = Dever::db('card/code')->find($v['card_code_id']);
  120. if ($code) {
  121. $card_info = Dever::db('card/info')->find($code['card_id']);
  122. if ($card_info) {
  123. if (!$code['total_cash']) {
  124. $code['total_cash'] = $card_info['value'];
  125. }
  126. $v['card'] = $card_info['name'];
  127. $v['card_code'] = $code['card'];
  128. }
  129. }
  130. }
  131. $zhuce = '';
  132. $address_contact = '';
  133. $address_mobile = '';
  134. $address_info = '';
  135. if ($v['address_id'] > 0) {
  136. $address = Dever::load('passport/address')->getOne($v['uid'], $v['address_id']);
  137. if($address) {
  138. $address_contact = $address['contact'];
  139. $address_mobile = $address['mobile'];
  140. $address_info = $address['address_info'];
  141. $member = Dever::db('agent/member')->find(array('mobile'=>$address['mobile']));
  142. if ($member) {
  143. $zhuce = date('Y-m-d H:i',$member['cdate']);
  144. }
  145. }
  146. }
  147. $ps = Dever::db('shop/sell_order_ps')->find(array('order_id' => $v['id']));
  148. $cdate = strstr($v['cdate'], '-') ? $v['cdate'] : date('Y-m-d H:i', $v['cdate']);
  149. $paydate = isset($v['paydate']) && $v['paydate'] ? date('Y-m-d H:i', $v['paydate']) : '-';
  150. $operdate = ($ps && $ps['cdate']) ? date('Y-m-d H:i', $ps['cdate']) : '-';
  151. $fdate = '-';
  152. if ($v['fdate']) {
  153. $fdate = date('Y-m-d H:i',$v['fdate']);
  154. }
  155. $refund_status = '';
  156. $refund_cdate = '';
  157. $refund_cash = '';
  158. $ka = 0;
  159. $money = 0;
  160. $goods = Dever::db('shop/sell_order_goods')->select(Array('order_id'=>$v['id']));
  161. foreach($goods as $k1 => $v1) {
  162. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  163. if (isset($goods_info['sku'])) {
  164. $sku = $goods_info['sku']['string'];
  165. } else {
  166. $sku = '';
  167. }
  168. if ($k1 == 0) {
  169. $total_cash = Dever::load("shop/lib/manage.getTotalCash", $v['id']);
  170. } else {
  171. $total_cash = 0;
  172. }
  173. if ($v['status'] == 8 || $v['status'] == 6) {
  174. $refund_status = '已退款';
  175. $refund_cash = $v1['price']*$v1['num'];
  176. if ($v['status'] == 8) {
  177. if ($v['refund_cash'] && $v['refund_cash']>0) {
  178. $money = $v['refund_cash']+$v['wallet_cash'];
  179. }
  180. if ($v['card_code_cash'] && $v['card_code_cash'] >0) {
  181. $ka = $v['card_code_cash'];
  182. }
  183. if ($k1 == 0) {
  184. $ka = $ka;
  185. $money = $money;
  186. } else {
  187. $ka = 0;
  188. $money = 0;
  189. }
  190. } elseif ($v['status'] == 6) {
  191. if ($v1['status'] == 3) {
  192. if ($v['card_code_id'] && $v['card_code_id'] >0) {
  193. $ka = $v['refund_cash'];
  194. } else {
  195. $money = $v['refund_cash'];
  196. }
  197. $refund_status = '已退款';
  198. } else {
  199. $refund_status = '正常';
  200. $refund_cash = 0;
  201. $ka = 0;
  202. $money = 0;
  203. }
  204. }
  205. // print_R($ka);die;
  206. $refund_cdate = '';
  207. if ($v['shop_type'] == 1) {
  208. $refund = Dever::db('shop/sell_order_refund')->find(array('order_id'=>$v['id']));
  209. if ($refund) {
  210. $refund_cdate = date('Y-m-d H:i',$refund['cdate']);
  211. }
  212. }
  213. } else {
  214. if ($v['refund_status'] == 3 && $v1['status'] == 3) {
  215. $refund_cash = $v1['num']*$v1['price'];
  216. if ($v['card_code_id'] && $v['card_code_id'] >0) {
  217. $ka = $v['refund_cash'];
  218. } else {
  219. $money = $v['refund_cash'];
  220. }
  221. // $refund_status = '已退款';
  222. $refund_status = '有退款';
  223. $refund_cdate = '';
  224. if ($v['shop_type'] == 1) {
  225. $refund = Dever::db('shop/sell_order_refund')->find(array('order_id'=>$v['id']));
  226. if ($refund) {
  227. $refund_cdate = date('Y-m-d H:i',$refund['cdate']);
  228. }
  229. }
  230. } else {
  231. $refund_cdate = '';
  232. $refund_cash = '0';
  233. $refund_status = '未退款';
  234. }
  235. }
  236. // print_R($goods);die;
  237. // print_R($goods_info);
  238. if ($goods_info['price_type'] > 2 && isset($goods_info['goods']) && is_array($goods_info['goods'])) {
  239. $heji = 0;
  240. $heji = $this->total($goods);
  241. // print_R($heji);die;
  242. foreach ($goods_info['goods'] as $key => $val) {
  243. // $val['price']-($val['price']*)
  244. $youhui = ($val['price']*($val['num']+$v1['num'])-($val['price']*($val['num']+$v1['num'])/$heji)*$v['price'])*$v1['num'];
  245. $card_price = ($v['card_code_cash']/$heji*($val['price']*($val['num']+$v1['num'])))*$v1['num'];
  246. $wallet_price = ($v['wallet_cash']/$heji*($val['price']*($val['num']+$v1['num'])))*$v1['num'];
  247. $goodsfu = $val['price']*($val['num']+$v1['num'])-$youhui-$card_price-$wallet_price;
  248. if ($key == 0) {
  249. $price = round($v1['price']*$v1['num']-$v1['discount_cash'],2);
  250. $shifu = round($v['price'],2);//实付金额
  251. $order_num = $v1['num'];#订单总数量
  252. $oprice = round($v1['price']*$v1['num'],2);#订单总金额
  253. $coupon_cash = round($v1['coupon_cash'],2);
  254. $discount_cash = round($v1['discount_cash'],2);
  255. $card_code_cash = round($v['card_code_cash'],2);
  256. $wallet_cash = round($v['wallet_cash'],2);
  257. } else {
  258. $price = 0;
  259. $shifu = 0;
  260. $order_num = 0;
  261. $oprice = 0;#订单总金额
  262. $coupon_cash = 0;
  263. $discount_cash = 0;
  264. $card_code_cash = 0;
  265. $wallet_cash = 0;
  266. }
  267. if($v1['num']>1) {
  268. $dnum = round($val['num']+$v1['num'],2);
  269. } else {
  270. $dnum = round($val['num'],2);
  271. }
  272. $d = array
  273. (
  274. $shop_type,
  275. $sid,
  276. $shop_name,
  277. $v['order_num'],
  278. $val['code']."\t",
  279. $goods_info['cunhuo_code']."\t",
  280. $val['name'].'('.$goods_info['name'].')',
  281. $sku,
  282. $goods_info['tax'],
  283. round($val['price'],2),
  284. $dnum,
  285. round(($val['price']*($val['num']+$v1['num'])),2),
  286. round($youhui,2),
  287. $goodsfu,
  288. $card_price,
  289. $wallet_price,
  290. $price,
  291. $v['card'],
  292. $v['card_code_card']."\t",
  293. $zhuce,
  294. $cdate,
  295. $paydate,
  296. $operdate,
  297. $fdate,
  298. $order_num,
  299. $oprice,
  300. $price,
  301. $card_code_cash,
  302. $wallet_cash,
  303. $coupon_cash,
  304. $discount_cash,
  305. $status[$v['status']],
  306. $refund_status,
  307. $refund_cdate,
  308. round($refund_cash, 2),
  309. round($ka,2),
  310. round($money,2),
  311. $address_contact,
  312. $address_mobile,
  313. $address_info,
  314. );
  315. $body[] = $d;
  316. }
  317. } else {
  318. if ($k1 == 0) {
  319. $discount_cash = round($v['discount_cash'],2);
  320. $card_code_cash = round($v['card_code_cash'],2);
  321. $wallet_cash = round($v['wallet_cash'],2);
  322. $zhifu = $v['price'];
  323. $total = $v['oprice']-($card_code_cash + $wallet_cash + $discount_cash+$zhifu);
  324. $order_num = $v['num'];//订单总数量
  325. $oprice = round($v['oprice'],2);#订单总金额
  326. $coupon_cash = round($v['coupon_cash'],2);
  327. } else {
  328. $discount_cash = 0;
  329. $card_code_cash = 0;
  330. $wallet_cash = 0;
  331. $total = 0;
  332. $zhifu = 0;
  333. $order_num = 0;
  334. $oprice = 0;
  335. $coupon_cash = 0;
  336. }
  337. // print_R($v1['price']*$v1['num']/$v['oprice']*$v['price']);die;
  338. $dyouhui = round($v1['discount_cash'],2);
  339. // ($v1['price']*$v1['num']/$v['oprice']*$v['price']);
  340. // print_R($dyouhui);die;
  341. $dcard_price = $v['card_code_cash']/$v['oprice']*($v1['price']*$v1['num']);
  342. $dwallet_price = $v['wallet_cash']/$v['oprice']*($v1['price']*$v1['num']);
  343. $dgoodsfu = $v1['price']*$v1['num']-$dyouhui-$dcard_price-$dwallet_price;
  344. if ($dgoodsfu<0){
  345. $dgoodsfu = 0;
  346. }
  347. $d = array
  348. (
  349. $shop_type,
  350. $sid,
  351. $shop_name,
  352. $v['order_num'],
  353. $goods_info['code']."\t",
  354. $goods_info['cunhuo_code']."\t",
  355. $goods_info['name'],
  356. $sku,
  357. $goods_info['tax'],
  358. round($v1['price'],2),
  359. round($v1['num'],2),
  360. round(($v1['price']*$v1['num']),2),
  361. round($dyouhui,2),
  362. $dgoodsfu,
  363. $dcard_price,
  364. $dwallet_price,
  365. $zhifu,
  366. $v['card'],
  367. $v['card_code_card']."\t",
  368. $zhuce,
  369. $cdate,
  370. $paydate,
  371. $operdate,
  372. $fdate,
  373. $order_num,
  374. sprintf('%01.2f',$total_cash),
  375. $zhifu,
  376. $card_code_cash,
  377. $wallet_cash,
  378. $coupon_cash,//优惠券
  379. $discount_cash,//代理优惠
  380. $status[$v['status']],
  381. $refund_status,
  382. $refund_cdate,
  383. round($refund_cash, 2),
  384. round($ka,2),
  385. round($money,2),
  386. $address_contact,
  387. $address_mobile,
  388. $address_info,
  389. );
  390. $body[] = $d;
  391. }
  392. }
  393. }
  394. }
  395. if (!$body) {
  396. Dever::alert('此时段没有数据,请重新选择条件');
  397. }
  398. $file = '商品销售相关数据';
  399. return Dever::excelExport($body, $header, $file);
  400. }
  401. public function total($goods){
  402. // print_R($goods);die;
  403. if ($goods) {
  404. $price = 0;
  405. foreach ($goods as $k => $v) {
  406. $price += $v['price'] * $v['num'];
  407. }
  408. return $price;
  409. }
  410. }
  411. public function memberGoods_api()
  412. {
  413. $name = Dever::input('name');
  414. $member_name = Dever::input('member_name');
  415. $card_num = Dever::input('card_num');
  416. $mobile = Dever::input('mobile');
  417. $start = Dever::input('start');
  418. $end = Dever::input('end');
  419. $rstart = Dever::input('rstart');
  420. $rend = Dever::input('end');
  421. $cstart = Dever::input('cstart');
  422. $cend = Dever::input('cend');
  423. if ($card_num) {
  424. $code = Dever::db('card/code')->find(array('card'=>$card_num));
  425. if ($code){
  426. $where['sku_id'] = "t_1.sku_id=".$code['id'];
  427. }
  428. }
  429. if ($name) {
  430. $where['name'] = "t_1.name like '%$name%'";
  431. }
  432. if ($cstart) {
  433. $where['cstart'] = 't_1.cdate+(360*86400)>='. strtotime($cstart);
  434. }
  435. if ($cend) {
  436. $where['cend'] = 't_1.cdate+(360*86400)<='.strtotime($cend);
  437. }
  438. if ($start) {
  439. $where['start'] = 't_1.cdate>='.strtotime($start);
  440. }
  441. if ($end) {
  442. $where['end'] = 't_1.cdate<='.strtotime($end);
  443. }
  444. if ($member_name) {
  445. $where['member_name'] = "t_2.name like '%$member_name%'";
  446. }
  447. if ($mobile) {
  448. $where['mobile'] = 't_2.mobile='.$mobile;
  449. }
  450. if ($rstart) {
  451. $where['rstart'] = 't_2.cdate>='.strtotime($rstart);
  452. }
  453. if ($rend) {
  454. $where['rend'] = 't_2.cdate<='.strtotime($rend);
  455. }
  456. $where['t_1.state'] = 't_1.state=1';
  457. $where['t_1.type'] = "t_1.type in (1,2)";
  458. $header = array('权益名称','会员等级','姓名','手机号','卡号','类型','数量','单件价值','总价值','已兑换价值','已使用价值','未兑换价值','未使用价值','备注','获取方式','获取时间','代理商注册时间','礼品卡有效期');
  459. $body = array();
  460. if ($where) {
  461. $where = implode(' and ', $where);
  462. $data = Dever::db('agent/member_goods')->fetchAll("select t_1.*,t_2.id as aid from churen_agent_member_goods as t_1 left join churen_agent_member as t_2 on t_1.mid = t_2.id where " . $where,array(),array(20, 'list'));
  463. if ($data) {
  464. foreach ($data as $k => $v) {
  465. $role_name = '';
  466. $member_name = '';
  467. $member_mobile = '';
  468. $member = Dever::db('agent/member')->find($v['mid']);
  469. if ($member) {
  470. $member_cdate = '-';
  471. if($member['cdate']){
  472. $member_cdate = date('Y-m-d H:i',$member['cdate']);
  473. }
  474. $member_name = $member['name'];
  475. $member_mobile = $member['mobile'];
  476. $role = Dever::db('setting/role')->find($member['role']);
  477. if ($role) {
  478. $role_name = $role['name'];
  479. }
  480. }
  481. $wd = ($v['total_num'] - $v['sell_num'])*$v['price'];
  482. $yd = $v['sell_num']*$v['price'];
  483. $card = '-';
  484. $mianzhi = '0';
  485. $money = '-';
  486. $youxiao = '';
  487. $leitype = '发放权益';
  488. if($v['type'] == 2){
  489. $code = Dever::db('card/code')->find($v['sku_id']);
  490. if($code){
  491. $card_info = Dever::db('card/info')->find($code['card_id']);
  492. if ($card_info && $card_info['end_dh_day']) {
  493. $youxiao = date('Y-m-d H:i',strtotime(+ $card_info['end_dh_day'].'day',$code['bdate']));
  494. }
  495. $card = $code['card']."\t";
  496. $mianzhi = $code['use_cash'];
  497. }
  498. }
  499. $wy = $v['cash'] - $mianzhi;
  500. $desc = '';
  501. $member_goods_log = Dever::db('agent/member_goods_log')->getOne(array('type'=>$v['type'],'type_id'=>$v['type_id'],'sku_id'=>$v['sku_id'],'state'=>1));
  502. if($member_goods_log && $member_goods_log['desc']){
  503. $desc = $member_goods_log['desc'];
  504. }
  505. $d = array
  506. (
  507. $v['name'],
  508. $role_name,
  509. $member_name,
  510. $member_mobile,
  511. $card,
  512. $leitype,
  513. $v['total_num'],
  514. $v['price'],
  515. $v['cash'],
  516. $yd,
  517. $mianzhi,
  518. $wd,
  519. $wy,
  520. $desc,
  521. '',
  522. date('Y-m-d H:i',$v['cdate']),
  523. $member_cdate,
  524. $youxiao,
  525. );
  526. $body[] = $d;
  527. }
  528. }
  529. }
  530. if (!$body) {
  531. Dever::alert('此时段没有数据,请重新选择条件');
  532. }
  533. $file = '代理商权益';
  534. return Dever::excelExport($body, $header, $file);
  535. }
  536. #代理商权益发放使用兑换明细
  537. public function mingxi_api()
  538. {
  539. $name = Dever::input('name');
  540. $mobile = Dever::input('mobile');
  541. $card_num = Dever::input('card_num');
  542. $shop_name = Dever::input('shop_name');
  543. $shop_num = Dever::input('shop_num');
  544. $order_num = Dever::input('order_num');
  545. $start = Dever::input('start');
  546. $end = Dever::input('end');
  547. $type = Dever::input('type');
  548. if ($name) {
  549. $where['name'] = $name;
  550. }
  551. if ($mobile) {
  552. $where['mobile'] = $mobile;
  553. }
  554. if ($card_num) {
  555. $code = Dever::db('card/code')->find(array('card'=>$card_num));
  556. if ($code){
  557. $where['sku_id'] = $code['id'];
  558. }
  559. }
  560. if ($shop_name) {
  561. $shop = Dever::db('shop/info')->find(array('name'=>$shop_name));
  562. $where['shop_id'] = $shop['id'];
  563. }
  564. if ($shop_num) {
  565. $shop = Dever::db('shop/info')->fetch('select * from churen_shop_info where sid = '.$shop_num.' and state = 1');
  566. $where['shop_id'] = $shop['id'];
  567. }
  568. // print_R($where['shop_id']);die;
  569. if ($order_num) {
  570. $sell_order = Dever::db('shop/sell_order')->find(array('order_num'=>$order_num));
  571. $where['sku_id'] = $sell_order['card_code_id'];
  572. }
  573. if ($start) {
  574. $where['start'] = strtotime($start);
  575. }
  576. if ($end) {
  577. $where['end'] = strtotime($end);
  578. }
  579. if ($type) {
  580. $where['types'] = $type;
  581. }
  582. $header = array('日期','权益名称','会员等级','姓名','手机号','卡号','类型','数量','单件价值','总价值','门店类型','门店编号','门店名称','关联平台商城订单号','备注','获取方式','获取时间','代理商注册时间','礼品卡有效期');
  583. $body = array();
  584. $where['type'] = '1,2';
  585. $data = Dever::db('agent/member_goods')->getMingxi($where);
  586. $res = array('发放权益','兑换权益','使用权益');
  587. // print_R($data);die;
  588. $rest = array();
  589. if ($where['types'] == -1) {
  590. foreach ($res as $a => $b ){
  591. foreach ($data as $k => $v) {
  592. $v['leixing'] = $b;
  593. $rest[] = $v;
  594. }
  595. }
  596. } elseif ($where['types'] == 1) {
  597. foreach ($data as $k => $v) {
  598. $v['leixing'] = '发放权益';
  599. $rest[] = $v;
  600. }
  601. } elseif ($where['types'] == 2) {
  602. foreach ($data as $k => $v) {
  603. $v['leixing'] = '兑换权益';
  604. $rest[] = $v;
  605. }
  606. } elseif ($where['types'] == 3) {
  607. foreach ($data as $k => $v) {
  608. $v['leixing'] = '使用权益';
  609. $rest[] = $v;
  610. }
  611. }
  612. foreach ($rest as $k => $v) {
  613. $data[$k]['cdate'] = date('Y-m-d H:i',$v['cdate']);
  614. // foreach ($res as $a => $b) {
  615. // $data[$k]['leixing'] = $b;
  616. $member_goods_name = '';
  617. if (isset($v['name']) && $v['name']) {
  618. $member_goods_name = $v['name'];
  619. }
  620. $total_num = '';
  621. if(isset($v['total_num']) && $v['total_num']) {
  622. $total_num = $v['total_num'];
  623. }
  624. $price = '';
  625. if (isset($v['price']) && $v['price']) {
  626. $price = $v['price'];
  627. }
  628. $cash = '';
  629. if (isset($v['cash']) && $v['cash']) {
  630. $cash = $v['cash'];
  631. }
  632. $role_name = '';
  633. $member_name = '';
  634. $member_mobile = '';
  635. $member_cdate = '-';
  636. $member = Dever::db('agent/member')->find($v['mid']);
  637. if ($member) {
  638. if($member['cdate']){
  639. $member_cdate = date('Y-m-d H:i',$member['cdate']);
  640. }
  641. $member_name = $member['name'];
  642. $member_mobile = $member['mobile'];
  643. $role = Dever::db('setting/role')->find($member['role']);
  644. if ($role) {
  645. $role_name = $role['name'];
  646. }
  647. }
  648. $card = '';
  649. $youxiao = '';
  650. if ($v['type'] == 2){
  651. $code = Dever::db('card/code')->find($v['sku_id']);
  652. if($code){
  653. $card_info = Dever::db('card/info')->find($code['card_id']);
  654. if ($card_info && $card_info['end_dh_day']) {
  655. $youxiao = date('Y-m-d H:i',strtotime(+ $card_info['end_dh_day'].'day',$code['bdate']));
  656. }
  657. $card = $code['card']."\t";
  658. }
  659. }
  660. $desc = '';
  661. $member_goods_log = Dever::db('agent/member_goods_log')->getOne(array('type'=>$v['type'],'type_id'=>$v['type_id'],'sku_id'=>$v['sku_id'],'state'=>1));
  662. if($member_goods_log && $member_goods_log['desc']){
  663. $desc = $member_goods_log['desc'];
  664. }
  665. $dui['mid'] = $v['mid'];
  666. if (isset($where['start']) && $where['start']) {
  667. $dui['start'] = $where['start'];
  668. }
  669. if (isset($where['end']) && $where['end']) {
  670. $dui['end'] = $where['end'];
  671. }
  672. $dh_order_ps = Dever::db('agent/dh_order_ps')->getDui($dui);
  673. if ($where['types'] == 2 || $where['types'] == -1) {
  674. // $v['leixing'] = ''
  675. if ($dh_order_ps) {
  676. // $data[$k]['leixing'] = '兑换权益';
  677. foreach ($dh_order_ps as $k =>$v) {
  678. $dh_order = Dever::db('agent/dh_order')->find($v['order_id']);
  679. if ($dh_order && isset($dh_order['info']) && $dh_order['info']){
  680. $desc = $dh_order['info'];
  681. }
  682. }
  683. }
  684. }
  685. $shop_name = '';
  686. $shop_type = '';
  687. $shop_sid = '';
  688. $order_num = '';
  689. if (isset($where['shop_id']) && $where['shop_id']) {
  690. $where['types'] = 3;
  691. $w2['shop_id'] = $where['shop_id'];
  692. if (isset($where['start']) && $where['start']) {
  693. $w2['start'] = $where['start'];
  694. }
  695. if (isset($where['end']) && $where['end']) {
  696. $w2['end'] = $where['end'];
  697. }
  698. $sell_order = Dever::db('shop/sell_order')->getOutOne($w2);
  699. if ($sell_order) {
  700. if ($where['types'] == 3 || $where['types'] == -1){
  701. $w = $this->common($sell_order);
  702. if ($w) {
  703. $shop_name = $w['shop_name'];
  704. $shop_type = $w['shop_type'];
  705. $shop_sid = $w['shop_sid'];
  706. $order_num = $w['order_num'];
  707. }
  708. }
  709. }
  710. } else {
  711. if (isset($v['sku_id']) && $v['sku_id']){
  712. $w1['card_code_id'] = $v['sku_id'];
  713. if (isset($where['start']) && $where['start']) {
  714. $w1['start'] = $where['start'];
  715. }
  716. if (isset($where['end']) && $where['end']) {
  717. $w1['end'] = $where['end'];
  718. }
  719. $sell_order = Dever::db('shop/sell_order')->getOutOne($w1);
  720. if ($sell_order) {
  721. if ($where['types'] == 3 || $where['types'] == -1) {
  722. $w = $this->common($sell_order);
  723. if ($w) {
  724. $shop_name = $w['shop_name'];
  725. $shop_type = $w['shop_type'];
  726. $shop_sid = $w['shop_sid'];
  727. $order_num = $w['order_num'];
  728. }
  729. }
  730. }
  731. }
  732. }
  733. $d = array
  734. (
  735. $data[$k]['cdate'],
  736. $member_goods_name,
  737. $role_name,
  738. $member_name,
  739. $member_mobile,
  740. $card,
  741. $v['leixing'],
  742. $total_num,
  743. $price,
  744. $cash,
  745. $shop_type,
  746. $shop_sid,
  747. $shop_name,
  748. $order_num,
  749. $desc,
  750. '',
  751. $data[$k]['cdate'],
  752. $member_cdate,
  753. $youxiao,
  754. );
  755. $body[] = $d;
  756. }
  757. if (!$body) {
  758. Dever::alert('此时段没有数据,请重新选择条件');
  759. }
  760. $file = '代理商权益明细';
  761. return Dever::excelExport($body, $header, $file);
  762. }
  763. public function common($sell_order)
  764. {
  765. $w = array();
  766. $w['order_num'] = $sell_order['order_num'];
  767. $shop = Dever::db('shop/info')->find($sell_order['shop_id']);
  768. if ($shop) {
  769. $w['shop_name'] = $shop['name'];
  770. $w['shop_sid'] = $shop['sid'];
  771. if ($shop['type'] = 1) {
  772. $w['shop_type'] = '体验店';
  773. } elseif ($shop['type'] = 2) {
  774. $w['shop_type'] = '零售店';
  775. } elseif ($shop['type'] = 10) {
  776. $w['shop_type'] = '平台商城';
  777. }
  778. }
  779. return $w;
  780. }
  781. }