PinModel.class.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. <?php
  2. namespace Home\Model;
  3. use Think\Model;
  4. /**
  5. * 拼团模型模型
  6. * @author fish
  7. *
  8. */
  9. class PinModel extends Model{
  10. /**
  11. * 检测拼团状态并返回可使用的拼团id
  12. * @param unknown $pin_id
  13. * @return unknown|number
  14. */
  15. function checkPinState($pin_id){
  16. $pin_info = M('lionfish_comshop_pin')->where( array('pin_id' => $pin_id) )->find();
  17. if($pin_info['state'] == 0 && $pin_info['end_time'] > time()) {
  18. return $pin_id;
  19. } else {
  20. return 0;
  21. }
  22. }
  23. /***
  24. 机器人成团
  25. **/
  26. public function jia_over_order( $pin_id )
  27. {
  28. $pin_info = M('lionfish_comshop_pin')->where( array('pin_id' => $pin_id ) )->find();
  29. $buy_count = $this->get_tuan_buy_count($pin_id);
  30. $del_count = $pin_info['need_count'] - $buy_count;
  31. if($del_count > 0)
  32. {
  33. $jia_list = M('lionfish_comshop_jiauser')->order('rand()')->limit($del_count)->select();
  34. foreach($jia_list as $jia_member)
  35. {
  36. $tmp_arr = array();
  37. //jiapinorder
  38. $tmp_arr['uniacid'] = 0;
  39. $tmp_arr['pin_id'] = $pin_id;
  40. $tmp_arr['uname'] = $jia_member['username'];
  41. $tmp_arr['avatar'] = $jia_member['avatar'];
  42. $tmp_arr['order_sn'] = build_order_no($pin_id);;
  43. $tmp_arr['mobile'] = $jia_member['mobile'];
  44. $tmp_arr['addtime'] = time() + mt_rand(60,120);
  45. M('lionfish_comshop_jiapinorder')->add( $tmp_arr );
  46. }
  47. M('lionfish_comshop_pin')->where( array('pin_id' => $pin_id ) )->save( array('is_jiqi' => 1) );
  48. $this->updatePintuanSuccess($pin_id);
  49. return true;
  50. }else{
  51. return false;
  52. }
  53. }
  54. /**
  55. * 检测拼团当前真实状态
  56. * 因为拼团时间是可变,过期拼团的订单状态可能是拼团中
  57. */
  58. public function getNowPinState($pin_id)
  59. {
  60. $pin_info = M('lionfish_comshop_pin')->where( array('pin_id' => $pin_id) )->find();
  61. if($pin_info['state'] == 0 && $pin_info['end_time'] <= time()) {
  62. return 2;
  63. } else {
  64. return $pin_info['state'];
  65. }
  66. }
  67. /**
  68. * 获取拼团已成功购买价数量
  69. */
  70. public function get_tuan_buy_count($pin_id=0 , $where =' and o.order_status_id = 2 ')
  71. {
  72. $buy_count_sql = "select count(o.order_id) as count from ".C('DB_PREFIX'). "lionfish_comshop_pin as p,".C('DB_PREFIX'). "lionfish_comshop_pin_order as po,"
  73. .C('DB_PREFIX'). "lionfish_comshop_order_goods as og,
  74. ".C('DB_PREFIX'). "lionfish_comshop_order as o
  75. where p.pin_id = po.pin_id and po.order_id=o.order_id and og.order_id = o.order_id {$where} and p.pin_id={$pin_id} ";
  76. $count_tuan =M()->query($buy_count_sql);
  77. return $count_tuan[0]['count'];
  78. }
  79. /**
  80. * 获取商品正在进行中的团
  81. */
  82. public function get_goods_pintuan($goods_id,$limit =8)
  83. {
  84. $hashids = new \Lib\Hashids(C('PWD_KEY'), C('URL_ID'));
  85. $fujin_sql = "select distinct(p.pin_id) as pin_id,p.need_count,o.order_id,o.shipping_city_id,p.end_time,m.name,m.avatar from ".C('DB_PREFIX'). "lionfish_comshop_pin as p,".C('DB_PREFIX'). "lionfish_comshop_order_goods as og,".C('DB_PREFIX'). "lionfish_comshop_pin_order as po,
  86. ".C('DB_PREFIX'). "lionfish_comshop_order as o,".C('DB_PREFIX'). "lionfish_comshop_member as m
  87. where p.pin_id = po.pin_id and po.order_id = o.order_id and og.order_id=o.order_id and o.member_id = m.member_id and o.order_status_id =2 and og.goods_id={$goods_id} and p.end_time>".time()." group by po.pin_id order by p.end_time asc limit {$limit}";
  88. $fujin_countsql = "select distinct(p.pin_id) as pin_id,p.need_count,o.order_id,o.shipping_city_id,p.end_time,m.name,m.avatar from ".C('DB_PREFIX'). "lionfish_comshop_pin as p,".C('DB_PREFIX'). "lionfish_comshop_order_goods as og,".C('DB_PREFIX'). "lionfish_comshop_pin_order as po,
  89. ".C('DB_PREFIX'). "lionfish_comshop_order as o,".C('DB_PREFIX'). "lionfish_comshop_member as m
  90. where p.pin_id = po.pin_id and po.order_id=o.order_id and og.order_id=o.order_id and o.member_id = m.member_id and o.order_status_id =2 and og.goods_id={$goods_id} and p.end_time>".time()." group by po.pin_id order by p.end_time asc ";
  91. $fujin_tuan_arr_count =M()->query($fujin_countsql);
  92. $fujin_tuan_count = count($fujin_tuan_arr_count);
  93. // /and o.order_status_id =2
  94. $fujin_tuan =M()->query($fujin_sql);
  95. //var_dump($fujin_tuan);die();
  96. $result = array();
  97. $weixin_notify_model = D('Home/Weixinnotify');
  98. if(!empty($fujin_tuan))
  99. {
  100. foreach($fujin_tuan as $pintuan)
  101. {
  102. $buy_count = $this->get_tuan_buy_count($pintuan['pin_id']);
  103. $pintuan['buy_count'] =$buy_count;
  104. $pintuan['re_need_count'] = $pintuan['need_count'] - $buy_count;
  105. //shipping_city_id
  106. $area_info = M('area')->where( array('area_id' => $pintuan['shipping_city_id']) )->find();
  107. $pintuan['area_name'] = $area_info['area_name'];
  108. $order_id = $hashids->encode($pintuan['order_id']);
  109. $url = $weixin_notify_model->getSiteUrl()."/index.php?s=/Group/info/group_order_id/{$order_id}";
  110. $pintuan['url'] = $url;
  111. if($buy_count > 0)
  112. {
  113. //存在进行中的
  114. $result[] = $pintuan;
  115. }
  116. }
  117. }
  118. //fujin_tuan_count
  119. return array('list' => $result, 'count' => $fujin_tuan_count);
  120. }
  121. /**
  122. 检测订单是否团长订单
  123. **/
  124. public function checkOrderIsTuanzhang($order_id)
  125. {
  126. $order_info = M('lionfish_comshop_order')->where( array('order_id' => $order_id) )->find();
  127. $pin_order = M('lionfish_comshop_pin_order')->where( array('order_id' => $order_id) )->find();
  128. $pin_id = $pin_order['pin_id'];
  129. if($order_info['is_pin'] == 0)
  130. {
  131. return false;
  132. }
  133. $pin_info = M('lionfish_comshop_pin')->where( array('pin_id' => $pin_id ) )->find();
  134. $is_tuan = false;
  135. if( $pin_info['order_id'] == $order_id)
  136. {
  137. $is_tuan = true;
  138. }
  139. return $is_tuan;
  140. }
  141. /**
  142. * 检测拼团是否成功
  143. */
  144. public function checkPinSuccess($pin_id)
  145. {
  146. $pin_info = M('lionfish_comshop_pin')->where( array('pin_id' => $pin_id) )->find();
  147. if(empty($pin_info)) {
  148. return false;
  149. }
  150. $pin_order_sql = "select count(po.id) as count from ".C('DB_PREFIX'). "lionfish_comshop_pin_order as po,".C('DB_PREFIX'). "lionfish_comshop_order as o,
  151. ".C('DB_PREFIX'). "lionfish_comshop_order_goods as og
  152. where po.pin_id = ".$pin_id." and o.order_status_id in(1,2,4,6)
  153. and og.order_id = po.order_id and o.order_id = po.order_id order by po.add_time asc ";
  154. $pin_order_arr_count = M()->query($pin_order_sql);
  155. $pin_order_count = $pin_order_arr_count[0]['count'];
  156. if($pin_order_count >= $pin_info['need_count'])
  157. {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. /**
  164. * 拼团成功
  165. */
  166. public function updatePintuanSuccess($pin_id)
  167. {
  168. $pin_up_sql = array();
  169. $pin_up_sql['state'] = 1;
  170. $pin_up_sql['success_time'] = time();
  171. M('lionfish_comshop_pin')->where( array('pin_id' => $pin_id ) )->save( $pin_up_sql );
  172. $pin_order_sql = "select po.order_id,og.order_goods_id,m.openid,m.we_openid,m.member_id,o.type,o.head_id,o.from_type,o.order_num_alias,o.delivery,og.name,og.price,og.store_id
  173. from ".C('DB_PREFIX'). "lionfish_comshop_pin_order as po,".C('DB_PREFIX'). "lionfish_comshop_order as o,
  174. ".C('DB_PREFIX'). "lionfish_comshop_order_goods as og,".C('DB_PREFIX'). "lionfish_comshop_member as m
  175. where po.pin_id = ".$pin_id." and o.order_status_id in(2)
  176. and og.order_id = po.order_id and o.order_id = po.order_id and o.member_id= m.member_id order by po.add_time asc ";
  177. $pin_order_arr = M()->query($pin_order_sql);
  178. $i = 0;
  179. $order_model = D('Home/Frontorder');
  180. $template_id = D('Home/Front')->get_config_by_name('weprogram_template_pin_tuansuccess' );
  181. $pintuan_model_buy = D('Home/Front')->get_config_by_name('pintuan_model_buy');
  182. if( empty($pintuan_model_buy) )
  183. {
  184. $pintuan_model_buy = 0;
  185. }
  186. $url = D('Home/Front')->get_config_by_name('shop_domain');
  187. $community_model = D('Seller/Community');
  188. $fenxiao_model = D('Home/Commission');//D('Home/Fenxiao');
  189. $template_order_success_notice= D('Home/Front')->get_config_by_name('template_order_success_notice');
  190. $tuanzhang_member_id = 0;
  191. //拼团信息
  192. $pin_info = M('lionfish_comshop_pin')->where( array('pin_id' => $pin_id ) )->find();
  193. //拼团返利订单设置是否发货
  194. $pin_order_arr = $this->setPintuanRebateOrderSuccess($pin_info, $pin_order_arr);
  195. foreach($pin_order_arr as $pin_order)
  196. {
  197. //拼团订单发货
  198. if($pin_order['is_send'] == 1){
  199. $order_model->change_order_status($pin_order['order_id'],1);
  200. $oh = array();
  201. $oh['order_id']=$pin_order['order_id'];
  202. $oh['order_status_id']=1;
  203. $oh['comment']='拼团成功';
  204. $oh['date_added']=time();
  205. $oh['notify']=1;
  206. M('lionfish_comshop_order_history')->add( $oh );
  207. //拼团成功加入缓存S中
  208. $day_time = strtotime( date('Y-m-d '.'00:00:00') );
  209. $day_key = 'new_ordernotice_'.$day_time;
  210. $day_arr = S( $day_key );
  211. if( empty($day_arr) )
  212. {
  213. $day_arr = array();
  214. $day_arr[] = $oh['order_id'];
  215. }else{
  216. $day_arr[] = $oh['order_id'];
  217. }
  218. S($day_key, $day_arr );
  219. //判断是否要插入到团长配送的订单中去,兼容团长逻辑 TODO...
  220. //if( $pintuan_model_buy == 1 && $pin_order['type'] != 'ignore' && $pin_order['delivery'] != 'express' && $pin_order['head_id'] > 0 )
  221. if( $pintuan_model_buy == 1 && $pin_order['type'] != 'ignore' && $pin_order['head_id'] > 0 )
  222. {
  223. $shipping_money = 0;
  224. if($pin_order['delivery'] == 'tuanz_send')
  225. {
  226. $shipping_money = $pin_order['shipping_fare'];
  227. }
  228. $community_model->ins_head_commiss_order($pin_order['order_id'],$pin_order['order_goods_id'], $shipping_money);
  229. }
  230. //发送拼团成功begin
  231. $pagepath = 'lionfish_comshop/moduleA/pin/share?id='.$pin_order['order_id'];
  232. //发送拼团成功end
  233. $member_formid_info = M('lionfish_comshop_member_formid')->where(" member_id=".$pin_order['member_id']." and formid != '' ")->order('id desc')->find();
  234. $member_info = M('lionfish_comshop_member')->where( array('member_id' => $pin_order['member_id'] ) )->find();
  235. $mb_subscribe = M('lionfish_comshop_subscribe')->where( array('member_id' => $pin_order['member_id'], 'type' => 'pin_tuansuccess' ) )->find();
  236. //...todo
  237. if( !empty($mb_subscribe) )
  238. {
  239. $template_id = D('Home/Front')->get_config_by_name('weprogram_subtemplate_pin_tuansuccess' );
  240. $pin_order_name = mb_substr( $pin_order['name'],0,10,'utf-8');
  241. $template_data = array();
  242. $template_data['number3'] = array('value' => $pin_order['order_num_alias'] );
  243. $template_data['thing1'] = array('value' => $pin_order_name );
  244. $template_data['amount7'] = array('value' => sprintf("%.2f",$pin_order['price']) );
  245. D('Seller/User')->send_subscript_msg( $template_data,$url,$pagepath,$member_info['we_openid'],$template_id );
  246. M('lionfish_comshop_subscribe')->where( array('id' => $mb_subscribe['id'] ) )->delete();
  247. }
  248. $wx_template_data = array();
  249. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  250. if( !empty($weixin_appid) && !empty($template_id) )
  251. {
  252. $template_data = array();
  253. $template_data['keyword1'] = array('value' => $pin_order['order_num_alias'], 'color' => '#030303');
  254. $template_data['keyword2'] = array('value' => $pin_order['name'], 'color' => '#030303');
  255. $template_data['keyword3'] = array('value' => sprintf('%.2f', $pin_order['price'] ) , 'color' => '#030303');
  256. if( $pin_order['type'] == 'ignore' )
  257. {
  258. $template_data['keyword3'] = array('value' => '0元开团', 'color' => '#030303');
  259. }
  260. $res = D('Seller/User')->send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid'], 0);
  261. $data = array();
  262. $data['username'] = $member_formid_info['formid'];
  263. $data['member_id'] = $order_info['member_id'];
  264. $data['avatar'] = json_encode($res );
  265. $data['order_time'] = time();
  266. $data['order_id'] = $order_id;
  267. $data['state'] = 0;
  268. $data['order_url'] = $member_info['we_openid'];
  269. $data['add_time'] = time();
  270. M('lionfish_comshop_notify_order')->add( $data );
  271. //会员下单成功发送公众号提醒给团长 weixin_template_order_buy
  272. }
  273. if($i == 0)
  274. {
  275. //暂时关闭发送拼团成功通知
  276. $tuanzhang_member_id = $pin_order['member_id'];
  277. } else {
  278. //插入佣金团分佣
  279. $this->ins_member_commiss_order($tuanzhang_member_id,$pin_order['order_id'],$pin_order['store_id'],$pin_order['order_goods_id']);
  280. //发送拼团成功
  281. }
  282. if($pin_order['delivery'] == 'pickup' && $pin_order['type'] != 'lottery')
  283. { //如果订单是抽奖类型,那么久暂时不修改订单的发货状态
  284. //$order_model->change_order_status($pin_order['order_id'],4);
  285. //暂时关闭自提发货信息发送
  286. //$weixin_notify->sendPickupMsg($pin_order['order_id']);
  287. }
  288. //暂时关闭商品分佣
  289. //$fenxiao_model->ins_member_commiss_order($pin_order['member_id'],$pin_order['order_id'],$pin_order['store_id'],$pin_order['order_goods_id']);
  290. $fenxiao_model->ins_member_commiss_order($pin_order['member_id'],$pin_order['order_id'],$pin_order['store_id'], $pin_order['order_goods_id'] );
  291. //暂时关闭分享列表分佣
  292. //$share_model->send_order_commiss_money( $pin_order['order_id'] );
  293. //暂时关闭有新订单来的通知
  294. //$weixin_notify->send_neworder_template_msg($pin_order['store_id'],$pin_order['order_num_alias']);
  295. $i++;
  296. //通知开关状态 0为关,1为开
  297. if(!empty($template_order_success_notice))
  298. {
  299. //判断是否关联团长
  300. $weixin_template_order =array();
  301. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  302. $weixin_template_order_buy = D('Home/Front')->get_config_by_name('weixin_template_order_buy');
  303. if( !empty($weixin_appid) && !empty($weixin_template_order_buy) )
  304. {
  305. $head_pathinfo = "lionfish_comshop/moduleA/groupCenter/groupDetail?groupOrderId=".$pin_order['order_id'];
  306. $weixin_template_order = array(
  307. 'appid' => $weixin_appid,
  308. 'template_id' => $weixin_template_order_buy,
  309. 'pagepath' => $head_pathinfo,
  310. 'data' => array(
  311. 'first' => array('value' => '您好团长,您收到了一个新订单,请尽快接单处理','color' => '#030303'),
  312. 'tradeDateTime' => array('value' => date('Y-m-d H:i:s'),'color' => '#030303'),
  313. 'orderType' => array('value' => '用户购买','color' => '#030303'),
  314. 'customerInfo' => array('value' => $member_info['username'],'color' => '#030303'),
  315. 'orderItemName' => array('value' => '订单编号','color' => '#030303'),
  316. 'orderItemData' => array('value' => $pin_order['order_num_alias'],'color' => '#030303'),
  317. 'remark' => array('value' => '点击查看订单详情','color' => '#030303'),
  318. )
  319. );
  320. }
  321. //$pin_order['head_id'] > 0
  322. $headid = $pin_order['head_id'];
  323. if($headid > 0)
  324. {
  325. $head_info = M('lionfish_community_head')->where( array('id' => $headid ) )->find();
  326. $head_weopenid = M('lionfish_comshop_member')->where( array('member_id' => $head_info['member_id'] ) )->find();
  327. $mnzember_formid_info = M('lionfish_comshop_member_formid')->where( "member_id=".$head_info['member_id']." and formid != ''" )->order('id desc')->find();
  328. $template_ids = D('Home/Front')->get_config_by_name('weprogram_template_pay_order');
  329. $sd_result = D('Seller/User')->send_wxtemplate_msg(array(),$url,$head_pathinfo,$head_weopenid['we_openid'],$template_ids,$mnzember_formid_info['formid'], 0,$weixin_template_order);
  330. }
  331. }
  332. $platform_send_info_member= D('Home/Front')->get_config_by_name('platform_send_info_member');
  333. if($platform_send_info_member){
  334. $template_ids = D('Home/Front')->get_config_by_name('weprogram_template_pay_order');
  335. $platform_send_info =array();
  336. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  337. $weixin_template_order_buy = D('Home/Front')->get_config_by_name('weixin_template_order_buy');
  338. if( !empty($weixin_appid) && !empty($weixin_template_order_buy) )
  339. {
  340. $head_pathinfo = "lionfish_comshop/moduleA/groupCenter/groupDetail?groupOrderId=".$pin_order['order_id'];
  341. $platform_send_info = array(
  342. 'appid' => $weixin_appid,
  343. 'template_id' => $weixin_template_order_buy,
  344. 'pagepath' => $head_pathinfo,
  345. 'data' => array(
  346. 'first' => array('value' => '您好平台,您收到了一个新订单,请尽快接单处理','color' => '#030303'),
  347. 'tradeDateTime' => array('value' => date('Y-m-d H:i:s'),'color' => '#030303'),
  348. 'orderType' => array('value' => '用户购买','color' => '#030303'),
  349. 'customerInfo' => array('value' => $member_info['username'],'color' => '#030303'),
  350. 'orderItemName' => array('value' => '订单编号','color' => '#030303'),
  351. 'orderItemData' => array('value' => $pin_order['order_num_alias'],'color' => '#030303'),
  352. 'remark' => array('value' => '点击查看订单详情','color' => '#030303'),
  353. )
  354. );
  355. }
  356. $memberid = $platform_send_info_member;
  357. $result = explode(",", $memberid);
  358. foreach($result as $re){
  359. $pingtai = M('lionfish_comshop_member')->where( array('member_id' => $re ) )->find();
  360. $mnzember_formid_info = M('lionfish_comshop_member_formid')->where( " member_id={$re} and formid != '' " )->order('id desc')->find();
  361. $sd_result = D('Seller/User')->send_wxtemplate_msg(array(),$url,$head_pathinfo,$pingtai['we_openid'],$template_ids,$mnzember_formid_info['formid'], 0,$platform_send_info);
  362. }
  363. }
  364. }else{
  365. //拼团返利订单 不发货退款,进行返利
  366. if($pin_order['is_pintuan_rebate'] == 1) {
  367. $res = D('Home/Weixin')->refundOrder($pin_order['order_id'], 0);
  368. if( $res['code'] == 0 )
  369. {
  370. //拼团失败,订单退款
  371. $history_data = array();
  372. $history_data['order_id'] = $pin_order['order_id'];
  373. $history_data['order_status_id'] = 7;
  374. $history_data['notify'] = 0;
  375. $history_data['comment'] = '拼团失败,订单退款失败,错误原因: '.$res['msg'];
  376. $history_data['date_added'] = time();
  377. M('lionfish_comshop_order_history')->add( $history_data );
  378. }else{
  379. M('lionfish_comshop_order')->where( array('order_id' => $pin_order['order_id']) )->save( array('order_status_id' => 7) );
  380. //拼团失败,订单退款
  381. $history_data = array();
  382. $history_data['order_id'] = $pin_order['order_id'];
  383. $history_data['order_status_id'] = 7;
  384. $history_data['notify'] = 0;
  385. $history_data['comment'] = '拼团失败,订单退款';
  386. $history_data['date_added'] = time();
  387. M('lionfish_comshop_order_history')->add( $history_data );
  388. D('Seller/Commonorder')->refund_one_order( $pin_order['order_id'] , 1);
  389. //拼团返利订单 退款返利
  390. $this->pintuanRebateAmount($pin_info,$pin_order);
  391. }
  392. }
  393. }
  394. }
  395. //拼团打印权限
  396. $is_open_pintuan_orderprint = D('Home/Front')->get_config_by_name('is_open_pintuan_orderprint');
  397. $is_print_auto = D('Home/Front')->get_config_by_name('is_print_auto');
  398. if($is_open_pintuan_orderprint == 1 && (empty($is_print_auto) || $is_print_auto == 0 )){
  399. foreach($pin_order_arr as $pin_order)
  400. {
  401. //小票打印
  402. D('Seller/Printaction')->check_print_order($pin_order['order_id']);
  403. D('Seller/Printaction')->check_print_order2($pin_order['order_id']);
  404. }
  405. }
  406. }
  407. /**
  408. * @author cy 2021-04-23
  409. * @desc 拼团返利订单 退款返利
  410. * @param $pin_info
  411. * @param $pin_order
  412. */
  413. public function pintuanRebateAmount($pin_info, $pin_order){
  414. $rebate_reward = $pin_info['rebate_reward'];//返利奖励:1、送积分,2、送余额
  415. $reward_amount = $pin_info['reward_amount'];//返利赠送的积分或余额数
  416. if($rebate_reward == 1){//送积分
  417. D('Seller/Order')->charge_member_score( $pin_order['member_id'] , $reward_amount, 'in', 'pintuan_rebate', $pin_order['order_id']);
  418. }else if($rebate_reward == 2){//送余额
  419. //赠送余额
  420. M('lionfish_comshop_member')->where( array('member_id' => $pin_order['member_id']) )->setInc('account_money',$reward_amount);
  421. $account_money_info = M('lionfish_comshop_member')->field('account_money')->where( array('member_id' =>$pin_order['member_id'] ) )->find();
  422. $account_money = $account_money_info['account_money'];
  423. $member_charge_flow_data = array();
  424. $member_charge_flow_data['member_id'] = $pin_order['member_id'];
  425. $member_charge_flow_data['money'] = $reward_amount;
  426. $member_charge_flow_data['operate_end_yuer'] = $account_money;
  427. $member_charge_flow_data['state'] = 21;
  428. $member_charge_flow_data['trans_id'] = $pin_order['order_id'];
  429. $member_charge_flow_data['order_goods_id'] = $pin_order['order_goods_id'];
  430. $member_charge_flow_data['charge_time'] = time();
  431. $member_charge_flow_data['remark'] = "拼团返利赠送余额:".$reward_amount."元";
  432. $member_charge_flow_data['add_time'] = time();
  433. M('lionfish_comshop_member_charge_flow')->add($member_charge_flow_data);
  434. }
  435. }
  436. /**
  437. * @author cy 2021-04-22
  438. * @desc 拼团返利设置订单是否发货,普通拼团全部发货
  439. * @param $pin_id
  440. * @param $pin_order_arr
  441. * @return mixed
  442. */
  443. public function setPintuanRebateOrderSuccess($pin_info, $pin_order_arr){
  444. $pin_id = $pin_info['pin_id'];
  445. $is_pintuan_rebate = $pin_info['is_pintuan_rebate'];//是否拼团返利,1、是,0、否
  446. //新人参团 0、无限制,1、必中,2、必不中
  447. $new_member_group_reward_type = D('Home/Front')->get_config_by_name('new_member_group_reward_type');
  448. if($is_pintuan_rebate == 1){//拼团返利订单
  449. $random_delivery_count = $pin_info['random_delivery_count'];//随机发货人数
  450. //拼团订单数
  451. $pin_count = count($pin_order_arr);
  452. if($pin_count <= $random_delivery_count){//拼团订单不足随机发货人数,全部订单发货
  453. foreach($pin_order_arr as $k=>$v){
  454. $member_id = $v['member_id'];
  455. //判断是否新人参团
  456. $pin_order_arr[$k]['is_new'] = $this->checkIsNewPintuanRebate($member_id,$pin_id);
  457. if($new_member_group_reward_type == 2 && $pin_order_arr[$k]['is_new'] == 1){
  458. $pin_order_arr[$k]['is_send'] = 0;
  459. }else{
  460. $pin_order_arr[$k]['is_send'] = 1;
  461. }
  462. $pin_order_arr[$k]['is_pintuan_rebate'] = 1;
  463. }
  464. }else{
  465. $new_orderid_array = array();//新人订单id数组
  466. $all_array = array();//所有订单id数组
  467. foreach($pin_order_arr as $k=>$v){
  468. $all_array[] = $v['order_id'];
  469. $member_id = $v['member_id'];
  470. //判断是否新人参团
  471. $pin_order_arr[$k]['is_new'] = $this->checkIsNewPintuanRebate($member_id,$pin_id);
  472. if($pin_order_arr[$k]['is_new'] == 1){
  473. $new_orderid_array[] = $v['order_id'];
  474. }
  475. $pin_order_arr[$k]['is_pintuan_rebate'] = 1;
  476. }
  477. //设置拼团返利订单是否发货
  478. $pin_order_arr = $this->setPintuanRebateOrderSend($pin_order_arr,$random_delivery_count, $new_orderid_array,$all_array);
  479. }
  480. }else{//普通拼团订单
  481. foreach($pin_order_arr as $k=>$v){
  482. $pin_order_arr[$k]['is_send'] = 1;
  483. $pin_order_arr[$k]['is_pintuan_rebate'] = 0;
  484. }
  485. }
  486. return $pin_order_arr;
  487. }
  488. /**
  489. * @author cy 2021-04-22
  490. * @desc 判断会员是否新人(新人指未付款过的会员)
  491. * @param $member_id
  492. * @return int
  493. */
  494. public function checkIsNewPintuanRebate($member_id){
  495. $is_new = 0;
  496. $sql = " SELECT count(1) as count FROM ".C('DB_PREFIX')."lionfish_comshop_order as o "
  497. . " where o.member_id=".$member_id
  498. . " and o.order_status_id not in (3,5)";
  499. $pin_arr = M()->query($sql);
  500. if(empty($pin_arr) || $pin_arr[0]['count'] <= 1){
  501. $is_new = 1;
  502. }
  503. return $is_new;
  504. }
  505. /**
  506. * @author cy 2021-04-22
  507. * @desc 拼团返利订单设置发货
  508. * @param $pin_order_arr 拼团订单信息
  509. * @param $random_delivery_count 随机发货数
  510. * @param $new_orderid_array 新人订单id数组
  511. * @param $all_orderid_array 订单id数组
  512. */
  513. public function setPintuanRebateOrderSend($pin_order_arr,$random_delivery_count, $new_orderid_array,$all_array){
  514. $order_ids_array = array();
  515. //新人参团 0、无限制,1、必中,2、必不中
  516. $new_member_group_reward_type = D('Home/Front')->get_config_by_name('new_member_group_reward_type');
  517. if($new_member_group_reward_type == 0){//新人无限制
  518. if($random_delivery_count >= count($all_array)){
  519. $order_ids_array = $all_array;
  520. }else{
  521. //获取随机订单id
  522. $order_ids_array = $this->getRandomArray($all_array, $random_delivery_count);
  523. }
  524. }else if($new_member_group_reward_type == 1){//新人必中
  525. if($random_delivery_count >= count($all_array)){
  526. $order_ids_array = $all_array;
  527. }else{
  528. if($random_delivery_count > count($new_orderid_array)){
  529. //剩余订单数
  530. $sy_count = $random_delivery_count - count($new_orderid_array);
  531. //去除新人订单
  532. $order_ids = $this->removeArrayVal($all_array, $new_orderid_array);
  533. //随机获取剩余订单
  534. $sy_orderids_array = $this->getRandomArray($order_ids, $sy_count);
  535. //合并新人订单和随机剩余订单
  536. $order_ids_array = array_merge($sy_orderids_array,$new_orderid_array);
  537. $order_ids_array = array_unique($order_ids_array);
  538. }else if($random_delivery_count <= count($new_orderid_array)){
  539. $order_ids_array = $new_orderid_array;
  540. }
  541. }
  542. }else if($new_member_group_reward_type == 2){//新人必不中
  543. //去除新人订单
  544. $order_ids = $this->removeArrayVal($all_array, $new_orderid_array);
  545. //随机获取订单
  546. $order_ids_array = $this->getRandomArray($order_ids, $random_delivery_count);
  547. }
  548. foreach($pin_order_arr as $k=>$v){
  549. if (in_array($v['order_id'], $order_ids_array)){
  550. $pin_order_arr[$k]['is_send'] = 1;
  551. }else{
  552. $pin_order_arr[$k]['is_send'] = 0;
  553. }
  554. }
  555. return $pin_order_arr;
  556. }
  557. /**
  558. * @author 去除前一个数组包含后面一个数组的值
  559. * @param $order_ids
  560. * @param $other_ids
  561. * @return mixed
  562. */
  563. public function removeArrayVal($order_ids, $other_ids){
  564. foreach($other_ids as $k=>$v){
  565. foreach( $order_ids as $ak=>$av) {
  566. if($av == $v){
  567. unset($order_ids[$ak]);
  568. }
  569. }
  570. }
  571. return $order_ids;
  572. }
  573. /**
  574. * @author cy 2021-04-23
  575. * @desc 获取数组随机值
  576. * @param $array 数组
  577. * @param $num 随机个数
  578. * @return array
  579. */
  580. public function getRandomArray($array, $num){
  581. if($num == 1){
  582. $key = array_rand($array , $num);
  583. $random_keys[] = $key;
  584. }else{
  585. $random_keys = array_rand($array, $num);
  586. }
  587. $result = array();
  588. foreach($random_keys as $k=>$v){
  589. $result[] = $array[$v];
  590. }
  591. return $result;
  592. }
  593. public function get_area_info($id)
  594. {
  595. $area_info = M('lionfish_comshop_area')->where( array('id' => $id ) )->find();
  596. return $area_info;
  597. }
  598. /**
  599. * 开新团
  600. */
  601. function openNewTuan($order_id,$goods_id,$member_id){
  602. $goods_detail = M('lionfish_comshop_goods')->where( array('id' => $goods_id ) )->find();
  603. $pin_data = array();
  604. $pin_data['user_id'] = $member_id;
  605. $pin_data['order_id'] = $order_id;
  606. $pin_data['state'] = 0;
  607. $pin_data['is_commiss_tuan'] = 0;
  608. $pin_data['is_newman_takein'] = 0;
  609. $pin_data['begin_time'] = time();
  610. $pin_data['add_time'] = time();
  611. if($goods_detail['type'] == 'pin')
  612. {
  613. $pin_data['is_lottery'] = 0;
  614. $pin_data['lottery_state'] = 0;
  615. $goods_info = M('lionfish_comshop_good_pin')->where( array('goods_id' => $goods_id ) )->find();
  616. $pin_data['end_time'] = time() + intval(60*60 * $goods_info['pin_hour']);
  617. if($pin_data['end_time'] > $goods_info['end_time'])
  618. {
  619. $pin_data['end_time'] = $goods_info['end_time'];
  620. }
  621. $pin_data['need_count'] = $goods_info['pin_count'];
  622. //order_id
  623. $order = M('lionfish_comshop_order')->field('type')->where( array('order_id' => $order_id ) )->find();
  624. //if( $order['type'] == 'ignore' )
  625. //{
  626. $goods_pin = M('lionfish_comshop_good_pin')->where( array('goods_id' => $goods_id ) )->find();
  627. $pin_data['is_commiss_tuan'] = $goods_pin['is_commiss_tuan'];
  628. $pin_data['is_newman_takein'] = $goods_pin['is_newman'];
  629. //}
  630. $pin_data['is_pintuan_rebate'] = $goods_pin['is_pintuan_rebate'];
  631. $pin_data['random_delivery_count'] = $goods_pin['random_delivery_count'];
  632. $pin_data['rebate_reward'] = $goods_pin['rebate_reward'];
  633. if($goods_pin['rebate_reward'] == 1){//送积分
  634. $pin_data['reward_amount'] = $goods_pin['reward_point'];
  635. }else if($goods_pin['rebate_reward'] == 2){//送余额
  636. $pin_data['reward_amount'] = $goods_pin['reward_balance'];
  637. }
  638. }else if($goods_detail['type'] == 'lottery')
  639. {
  640. $pin_data['is_lottery'] = 1;
  641. $pin_data['lottery_state'] = 0;
  642. }
  643. $pin_id = M('lionfish_comshop_pin')->add( $pin_data );
  644. //$pin_data['need_count']
  645. $total = $pin_data['need_count'];
  646. D('Seller/Redisorder')->sysnc_pintuan_total($pin_id, $total);
  647. return $pin_id;
  648. }
  649. /**
  650. 检测拼团商品是否可以0元开团
  651. **/
  652. public function check_goods_iszero_opentuan( $goods_id )
  653. {
  654. $pin_goods = M('lionfish_comshop_good_pin')->where( array('goods_id' => $goods_id) )->find();
  655. if( $pin_goods['is_commiss_tuan'] == 1 && $pin_goods['is_zero_open'] == 1 )
  656. {
  657. return 1;
  658. }else{
  659. return 0;
  660. }
  661. }
  662. /**
  663. 插入订单通知表
  664. **/
  665. function insertNotifyOrder($order_id)
  666. {
  667. $order_info = M('lionfish_comshop_order')->where( array('order_id' => $order_id ) )->field('member_id,order_num_alias')->find();
  668. $member_info = M('lionfish_comshop_member')->where( array('member_id' => $order_info['member_id'] ) )->find();
  669. $group_url = '';
  670. $data = array();
  671. $data['username'] = $member_info['username'];
  672. $data['member_id'] = $order_info['member_id'];
  673. $data['avatar'] = $member_info['avatar'];
  674. $data['order_time'] = time();
  675. $data['order_id'] = $order_id;
  676. $data['state'] = 0;
  677. $data['order_url'] = $group_url;
  678. $data['add_time'] = time();
  679. M('lionfish_comshop_notify_order')->add( $data );
  680. $order_goods_info = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_id ) )->find();
  681. //发送模板消息
  682. $is_tuanz = $this->checkOrderIsTuanzhang($order_id );
  683. $order_num_alias = $order_info['order_num_alias'];
  684. $name = $order_goods_info['name'];
  685. $price = $order_goods_info['price'];
  686. $url = D('Home/Front')->get_config_by_name('shop_domain');
  687. if( $is_tuanz )
  688. {
  689. //$jssdk = new \Lib\Weixin\Jssdk( $weixin_config['appid'], $weixin_config['appscert']);
  690. //发送开团消息
  691. $pin_info = M('lionfish_comshop_pin')->where( array('order_id' => $order_id ) )->find();
  692. //订单编号 团购名称 拼团价 邀请人数 截止时间
  693. $need_count = $pin_info['need_count'];
  694. $end_time = date('Y-m-d H:i:s', $pin_info['end_time']);
  695. $template_id = D('Home/Front')->get_config_by_name('weprogram_template_open_tuan');
  696. $pagepath = 'lionfish_comshop/moduleA/pin/share?id='.$order_id;
  697. //发送拼团成功end
  698. $member_formid_info = M('lionfish_comshop_member_formid')->where(" member_id=".$order_info['member_id']." and formid != '' ")->order('id desc')->find();
  699. $member_info = M('lionfish_comshop_member')->where( array('member_id' => $order_info['member_id'] ) )->find();
  700. $mb_subscribe = M('lionfish_comshop_subscribe')->where( array('member_id' => $order_info['member_id'], 'type' => 'open_tuan' ) )->find();
  701. //...todo
  702. if( !empty($mb_subscribe) )
  703. {
  704. $template_id = D('Home/Front')->get_config_by_name('weprogram_subtemplate_open_tuan');
  705. $name2 = mb_substr($name,0,4,'utf-8');
  706. $template_data = array();
  707. $template_data['thing1'] = array('value' => $name2 );
  708. $template_data['amount2'] = array('value' => sprintf("%.2f", $price) );
  709. $template_data['thing3'] = array('value' => $need_count );
  710. $template_data['time5'] = array('value' => $end_time );
  711. $res = D('Seller/User')->send_subscript_msg( $template_data,$url,$pagepath,$member_info['we_openid'],$template_id);
  712. M('lionfish_comshop_subscribe')->where( array('id' => $mb_subscribe['id'] ) )->delete();
  713. }
  714. $wx_template_data = array();
  715. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  716. }else{
  717. //发送参团消息
  718. $template_id = D('Home/Front')->get_config_by_name('weprogram_template_take_tuan' );
  719. $pagepath = 'lionfish_comshop/moduleA/pin/share?id='.$order_id;
  720. //发送拼团成功end
  721. \Think\Log::record("begin 1");
  722. $member_info = M('lionfish_comshop_member')->where( array('member_id' => $order_info['member_id'] ) )->find();
  723. $mb_subscribe = M('lionfish_comshop_subscribe')->where( array('member_id' => $order_info['member_id'], 'type' => 'take_tuan' ) )->find();
  724. //...todo
  725. if( !empty($mb_subscribe) )
  726. {
  727. \Think\Log::record("begin 2");
  728. $template_id = D('Home/Front')->get_config_by_name('weprogram_subtemplate_take_tuan');
  729. $name = mb_substr($name,0,10,'utf-8');
  730. $template_data = array();
  731. $template_data['thing1'] = array('value' => $name );
  732. $template_data['number2'] = array('value' => $order_num_alias );
  733. $template_data['amount3'] = array('value' => sprintf("%.2f", $price) );
  734. $res = D('Seller/User')->send_subscript_msg( $template_data,$url,$pagepath,$member_info['we_openid'],$template_id);
  735. \Think\Log::record("begin 3". json_encode($res) );
  736. M('lionfish_comshop_subscribe')->where( array('id' => $mb_subscribe['id'] ) )->delete();
  737. }
  738. }
  739. }
  740. /**
  741. * 插入拼团订单
  742. *
  743. */
  744. function insertTuanOrder($pin_id,$order_id)
  745. {
  746. $pin_order_data = array();
  747. $pin_order_data['pin_id'] = $pin_id;
  748. $pin_order_data['order_id'] = $order_id;
  749. $pin_order_data['add_time'] = time();
  750. M('lionfish_comshop_pin_order')->add($pin_order_data);
  751. }
  752. /***
  753. 生成拼团的佣金账户
  754. **/
  755. public function commission_account($member_id)
  756. {
  757. $info = M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $member_id ) )->find();
  758. if( empty($info) )
  759. {
  760. $ins_data = array();
  761. $ins_data['member_id'] = $member_id;
  762. $ins_data['money'] = 0;
  763. $ins_data['dongmoney'] = 0;
  764. $ins_data['getmoney'] = 0;
  765. M('lionfish_comshop_pintuan_commiss')->add( $ins_data );
  766. }
  767. }
  768. public function send_pinorder_commiss_money($order_id,$order_goods_id)
  769. {
  770. $pintuan_commiss_order = M('lionfish_comshop_pintuan_commiss_order')->where( array('order_id' => $order_id ,'order_goods_id' =>$order_goods_id,'state' => 0 ) )->find();
  771. if( !empty($pintuan_commiss_order) )
  772. {
  773. M('lionfish_comshop_pintuan_commiss_order')->where( array('id' => $pintuan_commiss_order['id'] ) )->save( array('state' => 1,'statement_time' => time() ) );
  774. $this->commission_account($pintuan_commiss_order['member_id']);
  775. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $pintuan_commiss_order['member_id'] ) )->setInc('money',$pintuan_commiss_order['money'] );
  776. }
  777. }
  778. /**
  779. 拼团成功后,给团长发放订单佣金到记录表
  780. 拼团成功后,可以发送佣金
  781. @param $member_id 团长的id
  782. **/
  783. public function ins_member_commiss_order($member_id,$order_id,$store_id,$order_goods_id )
  784. {
  785. //判断商品是否开启佣金团 lionfish_comshop_order_goods
  786. $order_goods_info = M('lionfish_comshop_order_goods')->where( array('order_goods_id' => $order_goods_id ) )->find();
  787. if( empty($order_goods_info) )
  788. {
  789. return false;
  790. }
  791. $goods_id = $order_goods_info['goods_id'];
  792. //找出佣金是多少
  793. $goods_pin = M('lionfish_comshop_good_pin')->where( array('goods_id' => $goods_id ) )->find();
  794. if( empty($goods_pin) )
  795. {
  796. return false;
  797. }
  798. if( $goods_pin['is_commiss_tuan'] == 1 )
  799. {
  800. $commiss_type = $goods_pin['commiss_type'];
  801. $commiss_money = $goods_pin['commiss_money'];
  802. if( $commiss_type == 0 )
  803. {
  804. $commiss_money = round( ($commiss_money * ( $order_goods_info['total'] + $order_goods_info['shipping_fare'] - $order_goods_info['voucher_credit'] ) ) / 100,2);
  805. }
  806. //注入记录中,待结算
  807. //lionfish_comshop_pintuan_commiss_order
  808. $com_order_data = array();
  809. $com_order_data['member_id'] = $member_id;
  810. $com_order_data['order_id'] = $order_id;
  811. $com_order_data['order_goods_id'] = $order_goods_id;
  812. $com_order_data['type'] = $commiss_type == 0 ? 1:2;
  813. $com_order_data['bili'] = $goods_pin['commiss_money'];
  814. $com_order_data['store_id'] = $store_id;
  815. $com_order_data['state'] = 0;
  816. $com_order_data['money'] = $commiss_money;
  817. $com_order_data['addtime'] = time();
  818. M('lionfish_comshop_pintuan_commiss_order')->add( $com_order_data );
  819. }
  820. }
  821. /***
  822. 会员拼团佣金申请,余额 审核流程
  823. **/
  824. public function send_apply_yuer( $id )
  825. {
  826. $info = M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->find();
  827. if( $info['type'] == 1 && $info['state'] == 0 )
  828. {
  829. $del_money = $info['money'] - $info['service_charge_money'];
  830. if( $del_money >0 )
  831. {
  832. D('Seller/User')->sendMemberMoneyChange($info['member_id'], $del_money, 11, '拼团佣金提现到余额,提现id:'.$id);
  833. }
  834. }
  835. M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->save( array('state' => 1,'shentime' => time() ) );
  836. $money = $info['money'];
  837. //将冻结的钱划一部分到已提现的里面
  838. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id']) )->setInc('getmoney',$money );
  839. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id']) )->setInc('dongmoney',-$money );
  840. return array('code' => 0,'msg' => '提现成功');
  841. }
  842. /**
  843. 提现到微信零钱
  844. **/
  845. public function send_apply_weixin_yuer($id)
  846. {
  847. $lib_path = dirname(dirname( dirname(__FILE__) )).'/Lib/';
  848. require_once $lib_path."/Weixin/lib/WxPay.Api.php";
  849. $open_weixin_qiye_pay = D('Home/Front')->get_config_by_name('open_weixin_qiye_pay');
  850. $info = M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->find();
  851. if( empty($open_weixin_qiye_pay) || $open_weixin_qiye_pay ==0 )
  852. {
  853. return array('code' => 1,'msg' => '未开启企业付款');
  854. }else{
  855. if( $info['type'] == 2 && $info['state'] == 0 )
  856. {
  857. $del_money = $info['money'] - $info['service_charge_money'];
  858. if( $del_money >0 )
  859. {
  860. $mb_info = M('lionfish_comshop_member')->field('we_openid')->where( array('member_id' => $info['member_id'] ) )->find();
  861. $partner_trade_no = build_order_no($info['id']);
  862. $desc = date('Y-m-d H:i:s').'申请的提现已到账';
  863. $username = $info['bankusername'];
  864. $amount = $del_money * 100;
  865. $openid = $mb_info['we_openid'];
  866. $res = \WxPayApi::payToUser($openid,$amount,$username,$desc,$partner_trade_no);
  867. if(empty($res) || $res['result_code'] =='FAIL')
  868. {
  869. //show_json(0, $res['err_code_des']);
  870. return array('code' => 1,'msg' => $res['err_code_des'] );
  871. }else{
  872. M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->save( array('state' => 1,'shentime' => time() ) );
  873. $money = $info['money'];
  874. //将冻结的钱划一部分到已提现的里面
  875. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id'] ) )->setInc('getmoney',$money);
  876. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id'] ) )->setInc('dongmoney',-$money);
  877. return array('code' => 0,'msg' => '提现成功');
  878. }
  879. }
  880. }else{
  881. return array('code' => 1,'msg' => '已提现');
  882. }
  883. }
  884. }
  885. /***
  886. 提现到支付宝,提现到银行卡
  887. **/
  888. public function send_apply_alipay_bank($id)
  889. {
  890. $info = M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->find();
  891. if( ( $info['type'] == 3 || $info['type'] == 4) && $info['state'] == 0 )
  892. {
  893. M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->save( array('state' => 1,'shentime' => time() ) );
  894. $money = $info['money'];
  895. //将冻结的钱划一部分到已提现的里面
  896. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id']) )->setInc('getmoney',$money);
  897. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id']) )->setInc('dongmoney',-$money);
  898. return array('code' => 0,'msg' => '提现成功');
  899. }else{
  900. return array('code' => 1,'msg' => '已提现');
  901. }
  902. }
  903. public function send_apply_success_msg($apply_id)
  904. {
  905. $apply_info = M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $apply_id ) )->find();
  906. $member_info = M('lionfish_comshop_member')->field('we_openid')->where( array('member_id' => $apply_info['member_id'] ) )->find();
  907. switch($apply_info['type'])
  908. {
  909. case 1:
  910. $bank_name = '余额';
  911. break;
  912. case 2:
  913. $bank_name = '微信余额';
  914. break;
  915. case 3:
  916. $bank_name = '支付宝';
  917. break;
  918. case 4:
  919. $bank_name = '银行卡';
  920. break;
  921. }
  922. $dao_zhang = floatval( $apply_info['money']-$apply_info['service_charge_money']);
  923. $template_data = array();
  924. $template_data['keyword1'] = array('value' => sprintf("%01.2f", $apply_info['money']), 'color' => '#030303');
  925. $template_data['keyword2'] = array('value' => $apply_info['service_charge_money'], 'color' => '#030303');
  926. $template_data['keyword3'] = array('value' => sprintf("%01.2f", $dao_zhang), 'color' => '#030303');
  927. $template_data['keyword4'] = array('value' => $bank_name, 'color' => '#030303');
  928. $template_data['keyword5'] = array('value' => '提现成功', 'color' => '#030303');
  929. $template_data['keyword6'] = array('value' => date('Y-m-d H:i:s' , $apply_info['addtime']), 'color' => '#030303');
  930. $template_data['keyword7'] = array('value' => date('Y-m-d H:i:s' , $apply_info['shentime']), 'color' => '#030303');
  931. $template_id = D('Home/Front')->get_config_by_name('weprogram_template_apply_tixian');
  932. $url = D('Home/Front')->get_config_by_name('shop_domain');
  933. $pagepath = 'lionfish_comshop/pages/user/me';
  934. $member_formid_info = M('lionfish_comshop_member_formid')->where(" member_id=".$head_info['member_id']." and formid != '' and state =0 ")->order('id desc')->find();
  935. if(!empty( $member_formid_info ))
  936. {
  937. $wx_template_data = array();
  938. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  939. $weixin_template_apply_tixian = D('Home/Front')->get_config_by_name('weixin_template_apply_tixian');
  940. if( !empty($weixin_appid) && !empty($weixin_template_apply_tixian) )
  941. {
  942. $wx_template_data = array(
  943. 'appid' => $weixin_appid,
  944. 'template_id' => $weixin_template_apply_tixian,
  945. 'pagepath' => $pagepath,
  946. 'data' => array(
  947. 'first' => array('value' => '尊敬的用户,您的提现已到账','color' => '#030303'),
  948. 'keyword1' => array('value' => date('Y-m-d H:i:s' , $apply_info['addtime']),'color' => '#030303'),
  949. 'keyword2' => array('value' => $community_head_commiss_info['bankname'],'color' => '#030303'),
  950. 'keyword3' => array('value' => sprintf("%01.2f", $apply_info['money']),'color' => '#030303'),
  951. 'keyword4' => array('value' => $apply_info['service_charge'],'color' => '#030303'),
  952. 'keyword5' => array('value' => sprintf('%.2f', $dao_zhang),'color' => '#030303'),
  953. 'remark' => array('value' => '请及时进行对账','color' => '#030303'),
  954. )
  955. );
  956. }
  957. D('Seller/User')->send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid'] , 0,$wx_template_data);
  958. M('lionfish_comshop_member_formid')->where( array('id' => $member_formid_info['id'] ) )->save( array('state' => 1) );
  959. }
  960. }
  961. }