PinModel.class.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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')."pin as p,".C('DB_PREFIX')."pin_order as po,"
  73. .C('DB_PREFIX')."order_goods as og,
  74. ".C('DB_PREFIX')."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')."pin as p,".C('DB_PREFIX')."order_goods as og,".C('DB_PREFIX')."pin_order as po,
  86. ".C('DB_PREFIX')."order as o,".C('DB_PREFIX')."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')."pin as p,".C('DB_PREFIX')."order_goods as og,".C('DB_PREFIX')."pin_order as po,
  89. ".C('DB_PREFIX')."order as o,".C('DB_PREFIX')."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. foreach($pin_order_arr as $pin_order)
  192. {
  193. $order_model->change_order_status($pin_order['order_id'],1);
  194. $oh = array();
  195. $oh['order_id']=$pin_order['order_id'];
  196. $oh['order_status_id']=1;
  197. $oh['comment']='拼团成功';
  198. $oh['date_added']=time();
  199. $oh['notify']=1;
  200. M('lionfish_comshop_order_history')->add( $oh );
  201. //拼团成功加入缓存S中
  202. $day_time = strtotime( date('Y-m-d '.'00:00:00') );
  203. $day_key = 'new_ordernotice_'.$day_time;
  204. $day_arr = S( $day_key );
  205. if( empty($day_arr) )
  206. {
  207. $day_arr = array();
  208. $day_arr[] = $oh['order_id'];
  209. }else{
  210. $day_arr[] = $oh['order_id'];
  211. }
  212. S($day_key, $day_arr );
  213. //判断是否要插入到团长配送的订单中去,兼容团长逻辑 TODO...
  214. if( $pintuan_model_buy == 1 && $pin_order['type'] != 'ignore' && $pin_order['delivery'] != 'express' && $pin_order['head_id'] > 0 )
  215. {
  216. $shipping_money = 0;
  217. if($pin_order['delivery'] == 'tuanz_send')
  218. {
  219. $shipping_money = $pin_order['shipping_fare'];
  220. }
  221. $community_model->ins_head_commiss_order($pin_order['order_id'],$pin_order['order_goods_id'], $shipping_money);
  222. }
  223. //发送拼团成功begin
  224. $pagepath = 'lionfish_comshop/moduleA/pin/share?id='.$pin_order['order_id'];
  225. //发送拼团成功end
  226. $member_formid_info = M('lionfish_comshop_member_formid')->where(" member_id=".$pin_order['member_id']." and formid != '' ")->order('id desc')->find();
  227. $member_info = M('lionfish_comshop_member')->where( array('member_id' => $pin_order['member_id'] ) )->find();
  228. $mb_subscribe = M('lionfish_comshop_subscribe')->where( array('member_id' => $pin_order['member_id'], 'type' => 'pin_tuansuccess' ) )->find();
  229. //...todo
  230. if( !empty($mb_subscribe) )
  231. {
  232. $template_id = D('Home/Front')->get_config_by_name('weprogram_subtemplate_pin_tuansuccess' );
  233. $pin_order_name = mb_substr( $pin_order['name'],0,10,'utf-8');
  234. $template_data = array();
  235. $template_data['number3'] = array('value' => $pin_order['order_num_alias'] );
  236. $template_data['thing1'] = array('value' => $pin_order_name );
  237. $template_data['amount7'] = array('value' => $pin_order['price'] );
  238. D('Seller/User')->send_subscript_msg( $template_data,$url,$pagepath,$member_info['we_openid'],$template_id );
  239. M('lionfish_comshop_subscribe')->where( array('id' => $mb_subscribe['id'] ) )->delete();
  240. }
  241. $wx_template_data = array();
  242. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  243. if( !empty($weixin_appid) && !empty($template_id) )
  244. {
  245. $template_data = array();
  246. $template_data['keyword1'] = array('value' => $pin_order['order_num_alias'], 'color' => '#030303');
  247. $template_data['keyword2'] = array('value' => $pin_order['name'], 'color' => '#030303');
  248. $template_data['keyword3'] = array('value' => sprintf("%01.2f", $pin_order['price'] ) , 'color' => '#030303');
  249. if( $pin_order['type'] == 'ignore' )
  250. {
  251. $template_data['keyword3'] = array('value' => '0元开团', 'color' => '#030303');
  252. }
  253. $res = D('Seller/User')->send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid'], 0);
  254. $data = array();
  255. $data['username'] = $member_formid_info['formid'];
  256. $data['member_id'] = $order_info['member_id'];
  257. $data['avatar'] = json_encode($res );
  258. $data['order_time'] = time();
  259. $data['order_id'] = $order_id;
  260. $data['state'] = 0;
  261. $data['order_url'] = $member_info['we_openid'];
  262. $data['add_time'] = time();
  263. M('lionfish_comshop_notify_order')->add( $data );
  264. //会员下单成功发送公众号提醒给团长 weixin_template_order_buy
  265. }
  266. if($i == 0)
  267. {
  268. //暂时关闭发送拼团成功通知
  269. $tuanzhang_member_id = $pin_order['member_id'];
  270. } else {
  271. //插入佣金团分佣
  272. $this->ins_member_commiss_order($tuanzhang_member_id,$pin_order['order_id'],$pin_order['store_id'],$pin_order['order_goods_id']);
  273. }
  274. if($pin_order['delivery'] == 'pickup' && $pin_order['type'] != 'lottery')
  275. { //如果订单是抽奖类型,那么久暂时不修改订单的发货状态
  276. //$order_model->change_order_status($pin_order['order_id'],4);
  277. //暂时关闭自提发货信息发送
  278. //$weixin_notify->sendPickupMsg($pin_order['order_id']);
  279. }
  280. //暂时关闭商品分佣
  281. //$fenxiao_model->ins_member_commiss_order($pin_order['member_id'],$pin_order['order_id'],$pin_order['store_id'],$pin_order['order_goods_id']);
  282. $fenxiao_model->ins_member_commiss_order($pin_order['member_id'],$pin_order['order_id'],$pin_order['store_id'], $pin_order['order_goods_id'] );
  283. //暂时关闭分享列表分佣
  284. //$share_model->send_order_commiss_money( $pin_order['order_id'] );
  285. //暂时关闭有新订单来的通知
  286. //$weixin_notify->send_neworder_template_msg($pin_order['store_id'],$pin_order['order_num_alias']);
  287. $i++;
  288. //通知开关状态 0为关,1为开
  289. if(!empty($template_order_success_notice))
  290. {
  291. //判断是否关联团长
  292. $weixin_template_order =array();
  293. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  294. $weixin_template_order_buy = D('Home/Front')->get_config_by_name('weixin_template_order_buy');
  295. if( !empty($weixin_appid) && !empty($weixin_template_order_buy) )
  296. {
  297. $head_pathinfo = "lionfish_comshop/pages/groupCenter/groupDetail?groupOrderId=".$pin_order['order_id'];
  298. $weixin_template_order = array(
  299. 'appid' => $weixin_appid,
  300. 'template_id' => $weixin_template_order_buy,
  301. 'pagepath' => $head_pathinfo,
  302. 'data' => array(
  303. 'first' => array('value' => '您好团长,您收到了一个新订单,请尽快接单处理','color' => '#030303'),
  304. 'tradeDateTime' => array('value' => date('Y-m-d H:i:s'),'color' => '#030303'),
  305. 'orderType' => array('value' => '用户购买','color' => '#030303'),
  306. 'customerInfo' => array('value' => $member_info['username'],'color' => '#030303'),
  307. 'orderItemName' => array('value' => '订单编号','color' => '#030303'),
  308. 'orderItemData' => array('value' => $pin_order['order_num_alias'],'color' => '#030303'),
  309. 'remark' => array('value' => '点击查看订单详情','color' => '#030303'),
  310. )
  311. );
  312. }
  313. //$pin_order['head_id'] > 0
  314. $headid = $pin_order['head_id'];
  315. if($headid > 0)
  316. {
  317. $head_info = M('lionfish_community_head')->where( array('id' => $headid ) )->find();
  318. $head_weopenid = M('lionfish_comshop_member')->where( array('member_id' => $head_info['member_id'] ) )->find();
  319. $mnzember_formid_info = M('lionfish_comshop_member_formid')->where( "member_id=".$head_info['member_id']." and formid != ''" )->order('id desc')->find();
  320. $template_ids = D('Home/Front')->get_config_by_name('weprogram_template_pay_order');
  321. $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);
  322. }
  323. }
  324. $platform_send_info_member= D('Home/Front')->get_config_by_name('platform_send_info_member');
  325. if($platform_send_info_member){
  326. $template_ids = D('Home/Front')->get_config_by_name('weprogram_template_pay_order');
  327. $platform_send_info =array();
  328. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  329. $weixin_template_order_buy = D('Home/Front')->get_config_by_name('weixin_template_order_buy');
  330. if( !empty($weixin_appid) && !empty($weixin_template_order_buy) )
  331. {
  332. $head_pathinfo = "lionfish_comshop/pages/groupCenter/groupDetail?groupOrderId=".$pin_order['order_id'];
  333. $platform_send_info = array(
  334. 'appid' => $weixin_appid,
  335. 'template_id' => $weixin_template_order_buy,
  336. 'pagepath' => $head_pathinfo,
  337. 'data' => array(
  338. 'first' => array('value' => '您好平台,您收到了一个新订单,请尽快接单处理','color' => '#030303'),
  339. 'tradeDateTime' => array('value' => date('Y-m-d H:i:s'),'color' => '#030303'),
  340. 'orderType' => array('value' => '用户购买','color' => '#030303'),
  341. 'customerInfo' => array('value' => $member_info['username'],'color' => '#030303'),
  342. 'orderItemName' => array('value' => '订单编号','color' => '#030303'),
  343. 'orderItemData' => array('value' => $pin_order['order_num_alias'],'color' => '#030303'),
  344. 'remark' => array('value' => '点击查看订单详情','color' => '#030303'),
  345. )
  346. );
  347. }
  348. $memberid = $platform_send_info_member;
  349. $result = explode(",", $memberid);
  350. foreach($result as $re){
  351. $pingtai = M('lionfish_comshop_member')->where( array('member_id' => $re ) )->find();
  352. $mnzember_formid_info = M('lionfish_comshop_member_formid')->where( " member_id={$re} and formid != '' " )->order('id desc')->find();
  353. $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);
  354. }
  355. }
  356. }
  357. foreach($pin_order_arr as $pin_order)
  358. {
  359. //小票打印
  360. D('Seller/Printaction')->check_print_order($pin_order['order_id']);
  361. }
  362. }
  363. public function get_area_info($id)
  364. {
  365. $area_info = M('lionfish_comshop_area')->where( array('id' => $id ) )->find();
  366. return $area_info;
  367. }
  368. /**
  369. * 开新团
  370. */
  371. function openNewTuan($order_id,$goods_id,$member_id){
  372. $goods_detail = M('lionfish_comshop_goods')->where( array('id' => $goods_id ) )->find();
  373. $pin_data = array();
  374. $pin_data['user_id'] = $member_id;
  375. $pin_data['order_id'] = $order_id;
  376. $pin_data['state'] = 0;
  377. $pin_data['is_commiss_tuan'] = 0;
  378. $pin_data['is_newman_takein'] = 0;
  379. $pin_data['begin_time'] = time();
  380. $pin_data['add_time'] = time();
  381. if($goods_detail['type'] == 'pin')
  382. {
  383. $pin_data['is_lottery'] = 0;
  384. $pin_data['lottery_state'] = 0;
  385. $goods_info = M('lionfish_comshop_good_pin')->where( array('goods_id' => $goods_id ) )->find();
  386. $pin_data['end_time'] = time() + intval(60*60 * $goods_info['pin_hour']);
  387. if($pin_data['end_time'] > $goods_info['end_time'])
  388. {
  389. $pin_data['end_time'] = $goods_info['end_time'];
  390. }
  391. $pin_data['need_count'] = $goods_info['pin_count'];
  392. //order_id
  393. $order = M('lionfish_comshop_order')->field('type')->where( array('order_id' => $order_id ) )->find();
  394. //if( $order['type'] == 'ignore' )
  395. //{
  396. $goods_pin = M('lionfish_comshop_good_pin')->where( array('goods_id' => $goods_id ) )->find();
  397. $pin_data['is_commiss_tuan'] = $goods_pin['is_commiss_tuan'];
  398. $pin_data['is_newman_takein'] = $goods_pin['is_newman'];
  399. //}
  400. }else if($goods_detail['type'] == 'lottery')
  401. {
  402. $pin_data['is_lottery'] = 1;
  403. $pin_data['lottery_state'] = 0;
  404. }
  405. $pin_id = M('lionfish_comshop_pin')->add( $pin_data );
  406. return $pin_id;
  407. }
  408. /**
  409. 检测拼团商品是否可以0元开团
  410. **/
  411. public function check_goods_iszero_opentuan( $goods_id )
  412. {
  413. $pin_goods = M('lionfish_comshop_good_pin')->where( array('goods_id' => $goods_id) )->find();
  414. if( $pin_goods['is_commiss_tuan'] == 1 && $pin_goods['is_zero_open'] == 1 )
  415. {
  416. return 1;
  417. }else{
  418. return 0;
  419. }
  420. }
  421. /**
  422. 插入订单通知表
  423. **/
  424. function insertNotifyOrder($order_id)
  425. {
  426. $order_info = M('lionfish_comshop_order')->where( array('order_id' => $order_id ) )->field('member_id,order_num_alias')->find();
  427. $member_info = M('lionfish_comshop_member')->where( array('member_id' => $order_info['member_id'] ) )->find();
  428. $group_url = '';
  429. $data = array();
  430. $data['username'] = $member_info['username'];
  431. $data['member_id'] = $order_info['member_id'];
  432. $data['avatar'] = $member_info['avatar'];
  433. $data['order_time'] = time();
  434. $data['order_id'] = $order_id;
  435. $data['state'] = 0;
  436. $data['order_url'] = $group_url;
  437. $data['add_time'] = time();
  438. M('lionfish_comshop_notify_order')->add( $data );
  439. $order_goods_info = M('lionfish_comshop_order_goods')->where( array('order_id' => $order_id ) )->find();
  440. //发送模板消息
  441. $is_tuanz = $this->checkOrderIsTuanzhang($order_id );
  442. $order_num_alias = $order_info['order_num_alias'];
  443. $name = $order_goods_info['name'];
  444. $price = $order_goods_info['price'];
  445. $url = D('Home/Front')->get_config_by_name('shop_domain');
  446. if( $is_tuanz )
  447. {
  448. //$jssdk = new \Lib\Weixin\Jssdk( $weixin_config['appid'], $weixin_config['appscert']);
  449. //发送开团消息
  450. $pin_info = M('lionfish_comshop_pin')->where( array('order_id' => $order_id ) )->find();
  451. //订单编号 团购名称 拼团价 邀请人数 截止时间
  452. $need_count = $pin_info['need_count'] - 1;
  453. $end_time = date('Y-m-d H:i:s', $pin_info['end_time']);
  454. $template_id = D('Home/Front')->get_config_by_name('weprogram_template_open_tuan');
  455. $pagepath = 'lionfish_comshop/moduleA/pin/share?id='.$order_id;
  456. //发送拼团成功end
  457. $member_formid_info = M('lionfish_comshop_member_formid')->where(" member_id=".$order_info['member_id']." and formid != '' ")->order('id desc')->find();
  458. $member_info = M('lionfish_comshop_member')->where( array('member_id' => $order_info['member_id'] ) )->find();
  459. $mb_subscribe = M('lionfish_comshop_subscribe')->where( array('member_id' => $order_info['member_id'], 'type' => 'open_tuan' ) )->find();
  460. //...todo
  461. if( !empty($mb_subscribe) )
  462. {
  463. $template_id = D('Home/Front')->get_config_by_name('weprogram_subtemplate_open_tuan');
  464. $name2 = mb_substr($name,0,4,'utf-8');
  465. $template_data = array();
  466. $template_data['thing1'] = array('value' => $name2 );
  467. $template_data['amount2'] = array('value' => sprintf("%01.2f", $price) );
  468. $template_data['thing3'] = array('value' => $need_count );
  469. $template_data['time5'] = array('value' => $end_time.'结束' );
  470. D('Seller/User')->send_subscript_msg( $template_data,$url,$pagepath,$member_info['we_openid'],$template_id);
  471. M('lionfish_comshop_subscribe')->where( array('id' => $mb_subscribe['id'] ) )->delete();
  472. }
  473. $wx_template_data = array();
  474. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  475. if( !empty($weixin_appid) && !empty($template_id) )
  476. {
  477. $template_data = array();
  478. $template_data['keyword1'] = array('value' => $order_num_alias, 'color' => '#030303');
  479. $template_data['keyword2'] = array('value' => $name, 'color' => '#030303');
  480. $template_data['keyword3'] = array('value' => sprintf("%01.2f", $price), 'color' => '#030303');
  481. $template_data['keyword4'] = array('value' => $need_count, 'color' => '#030303');
  482. $template_data['keyword5'] = array('value' => $end_time, 'color' => '#030303');
  483. $res = D('Seller/User')->send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid'], 0);
  484. $data = array();
  485. $data['username'] = $member_formid_info['formid'];
  486. $data['member_id'] = $order_info['member_id'];
  487. $data['avatar'] = json_encode($res );
  488. $data['order_time'] = time();
  489. $data['order_id'] = $order_id;
  490. $data['state'] = 0;
  491. $data['order_url'] = $member_info['we_openid'];
  492. $data['add_time'] = time();
  493. M('lionfish_comshop_notify_order')->add( $data );
  494. //会员下单成功发送公众号提醒给团长 weixin_template_order_buy
  495. }
  496. }else{
  497. //发送参团消息
  498. $template_id = D('Home/Front')->get_config_by_name('weprogram_template_take_tuan' );
  499. $pagepath = 'lionfish_comshop/moduleA/pin/share?id='.$order_id;
  500. //发送拼团成功end
  501. $member_formid_info = M('lionfish_comshop_member_formid')->where(" member_id=".$order_info['member_id']." and formid != '' ")->order('id desc')->find();
  502. $member_info = M('lionfish_comshop_member')->where( array('member_id' => $order_info['member_id'] ) )->find();
  503. $mb_subscribe = M('lionfish_comshop_subscribe')->where( array('member_id' => $order_info['member_id'], 'type' => 'take_tuan' ) )->find();
  504. //...todo
  505. if( !empty($mb_subscribe) )
  506. {
  507. $template_id = D('Home/Front')->get_config_by_name('weprogram_subtemplate_take_tuan');
  508. $name = mb_substr($name,0,10,'utf-8');
  509. $template_data = array();
  510. $template_data['thing1'] = array('value' => $name );
  511. $template_data['number2'] = array('value' => $order_num_alias );
  512. $template_data['amount3'] = array('value' => $price );
  513. D('Seller/User')->send_subscript_msg( $template_data,$url,$pagepath,$member_info['we_openid'],$template_id);
  514. M('lionfish_comshop_subscribe')->where( array('id' => $mb_subscribe['id'] ) )->delete();
  515. }
  516. }
  517. }
  518. /**
  519. * 插入拼团订单
  520. *
  521. */
  522. function insertTuanOrder($pin_id,$order_id)
  523. {
  524. $pin_order_data = array();
  525. $pin_order_data['pin_id'] = $pin_id;
  526. $pin_order_data['order_id'] = $order_id;
  527. $pin_order_data['add_time'] = time();
  528. M('lionfish_comshop_pin_order')->add($pin_order_data);
  529. }
  530. /***
  531. 生成拼团的佣金账户
  532. **/
  533. public function commission_account($member_id)
  534. {
  535. $info = M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $member_id ) )->find();
  536. if( empty($info) )
  537. {
  538. $ins_data = array();
  539. $ins_data['member_id'] = $member_id;
  540. $ins_data['money'] = 0;
  541. $ins_data['dongmoney'] = 0;
  542. $ins_data['getmoney'] = 0;
  543. M('lionfish_comshop_pintuan_commiss')->add( $ins_data );
  544. }
  545. }
  546. public function send_pinorder_commiss_money($order_id,$order_goods_id)
  547. {
  548. $pintuan_commiss_order = M('lionfish_comshop_pintuan_commiss_order')->where( array('order_id' => $order_id ,'order_goods_id' =>$order_goods_id,'state' => 0 ) )->find();
  549. if( !empty($pintuan_commiss_order) )
  550. {
  551. M('lionfish_comshop_pintuan_commiss_order')->where( array('id' => $pintuan_commiss_order['id'] ) )->save( array('state' => 1,'statement_time' => time() ) );
  552. $this->commission_account($pintuan_commiss_order['member_id']);
  553. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $pintuan_commiss_order['member_id'] ) )->setInc('money',$pintuan_commiss_order['money'] );
  554. }
  555. }
  556. /**
  557. 拼团成功后,给团长发放订单佣金到记录表
  558. 拼团成功后,可以发送佣金
  559. @param $member_id 团长的id
  560. **/
  561. public function ins_member_commiss_order($member_id,$order_id,$store_id,$order_goods_id )
  562. {
  563. //判断商品是否开启佣金团 lionfish_comshop_order_goods
  564. $order_goods_info = M('lionfish_comshop_order_goods')->where( array('order_goods_id' => $order_goods_id ) )->find();
  565. if( empty($order_goods_info) )
  566. {
  567. return false;
  568. }
  569. $goods_id = $order_goods_info['goods_id'];
  570. //找出佣金是多少
  571. $goods_pin = M('lionfish_comshop_good_pin')->where( array('goods_id' => $goods_id ) )->find();
  572. if( empty($goods_pin) )
  573. {
  574. return false;
  575. }
  576. if( $goods_pin['is_commiss_tuan'] == 1 )
  577. {
  578. $commiss_type = $goods_pin['commiss_type'];
  579. $commiss_money = $goods_pin['commiss_money'];
  580. if( $commiss_type == 0 )
  581. {
  582. $commiss_money = round( ($commiss_money * ( $order_goods_info['total'] + $order_goods_info['shipping_fare'] - $order_goods_info['voucher_credit'] ) ) / 100,2);
  583. }
  584. //注入记录中,待结算
  585. //lionfish_comshop_pintuan_commiss_order
  586. $com_order_data = array();
  587. $com_order_data['member_id'] = $member_id;
  588. $com_order_data['order_id'] = $order_id;
  589. $com_order_data['order_goods_id'] = $order_goods_id;
  590. $com_order_data['type'] = $commiss_type == 0 ? 1:2;
  591. $com_order_data['bili'] = $goods_pin['commiss_money'];
  592. $com_order_data['store_id'] = $store_id;
  593. $com_order_data['state'] = 0;
  594. $com_order_data['money'] = $commiss_money;
  595. $com_order_data['addtime'] = time();
  596. M('lionfish_comshop_pintuan_commiss_order')->add( $com_order_data );
  597. }
  598. }
  599. /***
  600. 会员拼团佣金申请,余额 审核流程
  601. **/
  602. public function send_apply_yuer( $id )
  603. {
  604. $info = M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->find();
  605. if( $info['type'] == 1 && $info['state'] == 0 )
  606. {
  607. $del_money = $info['money'] - $info['service_charge_money'];
  608. if( $del_money >0 )
  609. {
  610. D('Seller/User')->sendMemberMoneyChange($info['member_id'], $del_money, 11, '拼团佣金提现到余额,提现id:'.$id);
  611. }
  612. }
  613. M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->save( array('state' => 1,'shentime' => time() ) );
  614. $money = $info['money'];
  615. //将冻结的钱划一部分到已提现的里面
  616. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id']) )->setInc('getmoney',$money );
  617. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id']) )->setInc('dongmoney',-$money );
  618. return array('code' => 0,'msg' => '提现成功');
  619. }
  620. /**
  621. 提现到微信零钱
  622. **/
  623. public function send_apply_weixin_yuer($id)
  624. {
  625. $lib_path = dirname(dirname( dirname(__FILE__) )).'/Lib/';
  626. require_once $lib_path."/Weixin/lib/WxPay.Api.php";
  627. $open_weixin_qiye_pay = D('Home/Front')->get_config_by_name('open_weixin_qiye_pay');
  628. $info = M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->find();
  629. if( empty($open_weixin_qiye_pay) || $open_weixin_qiye_pay ==0 )
  630. {
  631. return array('code' => 1,'msg' => '未开启企业付款');
  632. }else{
  633. if( $info['type'] == 2 && $info['state'] == 0 )
  634. {
  635. $del_money = $info['money'] - $info['service_charge_money'];
  636. if( $del_money >0 )
  637. {
  638. $mb_info = M('lionfish_comshop_member')->field('we_openid')->where( array('member_id' => $info['member_id'] ) )->find();
  639. $partner_trade_no = build_order_no($info['id']);
  640. $desc = date('Y-m-d H:i:s').'申请的提现已到账';
  641. $username = $info['bankusername'];
  642. $amount = $del_money * 100;
  643. $openid = $mb_info['we_openid'];
  644. $res = \WxPayApi::payToUser($openid,$amount,$username,$desc,$partner_trade_no);
  645. if(empty($res) || $res['result_code'] =='FAIL')
  646. {
  647. //show_json(0, $res['err_code_des']);
  648. return array('code' => 1,'msg' => $res['err_code_des'] );
  649. }else{
  650. M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->save( array('state' => 1,'shentime' => time() ) );
  651. $money = $info['money'];
  652. //将冻结的钱划一部分到已提现的里面
  653. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id'] ) )->setInc('getmoney',$money);
  654. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id'] ) )->setInc('dongmoney',-$money);
  655. return array('code' => 0,'msg' => '提现成功');
  656. }
  657. }
  658. }else{
  659. return array('code' => 1,'msg' => '已提现');
  660. }
  661. }
  662. }
  663. /***
  664. 提现到支付宝,提现到银行卡
  665. **/
  666. public function send_apply_alipay_bank($id)
  667. {
  668. $info = M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->find();
  669. if( ( $info['type'] == 3 || $info['type'] == 4) && $info['state'] == 0 )
  670. {
  671. M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $id ) )->save( array('state' => 1,'shentime' => time() ) );
  672. $money = $info['money'];
  673. //将冻结的钱划一部分到已提现的里面
  674. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id']) )->setInc('getmoney',$money);
  675. M('lionfish_comshop_pintuan_commiss')->where( array('member_id' => $info['member_id']) )->setInc('dongmoney',-$money);
  676. return array('code' => 0,'msg' => '提现成功');
  677. }else{
  678. return array('code' => 1,'msg' => '已提现');
  679. }
  680. }
  681. public function send_apply_success_msg($apply_id)
  682. {
  683. $apply_info = M('lionfish_comshop_pintuan_tixian_order')->where( array('id' => $apply_id ) )->find();
  684. $member_info = M('lionfish_comshop_member')->field('we_openid')->where( array('member_id' => $apply_info['member_id'] ) )->find();
  685. switch($apply_info['type'])
  686. {
  687. case 1:
  688. $bank_name = '余额';
  689. break;
  690. case 2:
  691. $bank_name = '微信余额';
  692. break;
  693. case 3:
  694. $bank_name = '支付宝';
  695. break;
  696. case 4:
  697. $bank_name = '银行卡';
  698. break;
  699. }
  700. $dao_zhang = floatval( $apply_info['money']-$apply_info['service_charge_money']);
  701. $template_data = array();
  702. $template_data['keyword1'] = array('value' => sprintf("%01.2f", $apply_info['money']), 'color' => '#030303');
  703. $template_data['keyword2'] = array('value' => $apply_info['service_charge_money'], 'color' => '#030303');
  704. $template_data['keyword3'] = array('value' => sprintf("%01.2f", $dao_zhang), 'color' => '#030303');
  705. $template_data['keyword4'] = array('value' => $bank_name, 'color' => '#030303');
  706. $template_data['keyword5'] = array('value' => '提现成功', 'color' => '#030303');
  707. $template_data['keyword6'] = array('value' => date('Y-m-d H:i:s' , $apply_info['addtime']), 'color' => '#030303');
  708. $template_data['keyword7'] = array('value' => date('Y-m-d H:i:s' , $apply_info['shentime']), 'color' => '#030303');
  709. $template_id = D('Home/Front')->get_config_by_name('weprogram_template_apply_tixian');
  710. $url = D('Home/Front')->get_config_by_name('shop_domain');
  711. $pagepath = 'lionfish_comshop/pages/user/me';
  712. $member_formid_info = M('lionfish_comshop_member_formid')->where(" member_id=".$head_info['member_id']." and formid != '' and state =0 ")->order('id desc')->find();
  713. if(!empty( $member_formid_info ))
  714. {
  715. $wx_template_data = array();
  716. $weixin_appid = D('Home/Front')->get_config_by_name('weixin_appid');
  717. $weixin_template_apply_tixian = D('Home/Front')->get_config_by_name('weixin_template_apply_tixian');
  718. if( !empty($weixin_appid) && !empty($weixin_template_apply_tixian) )
  719. {
  720. $wx_template_data = array(
  721. 'appid' => $weixin_appid,
  722. 'template_id' => $weixin_template_apply_tixian,
  723. 'pagepath' => $pagepath,
  724. 'data' => array(
  725. 'first' => array('value' => '尊敬的用户,您的提现已到账','color' => '#030303'),
  726. 'keyword1' => array('value' => date('Y-m-d H:i:s' , $apply_info['addtime']),'color' => '#030303'),
  727. 'keyword2' => array('value' => $community_head_commiss_info['bankname'],'color' => '#030303'),
  728. 'keyword3' => array('value' => sprintf("%01.2f", $apply_info['money']),'color' => '#030303'),
  729. 'keyword4' => array('value' => $apply_info['service_charge'],'color' => '#030303'),
  730. 'keyword5' => array('value' => sprintf("%01.2f", $dao_zhang),'color' => '#030303'),
  731. 'remark' => array('value' => '请及时进行对账','color' => '#030303'),
  732. )
  733. );
  734. }
  735. D('Seller/User')->send_wxtemplate_msg($template_data,$url,$pagepath,$member_info['we_openid'],$template_id,$member_formid_info['formid'] , 0,$wx_template_data);
  736. M('lionfish_comshop_member_formid')->where( array('id' => $member_formid_info['id'] ) )->save( array('state' => 1) );
  737. }
  738. }
  739. }