SalesroomModel.class.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. <?php
  2. namespace Home\Model;
  3. use Think\Model;
  4. /**
  5. * 门店模块
  6. * @author fish
  7. *
  8. */
  9. class SalesroomModel{
  10. public $table = 'pin';
  11. /**
  12. * 获取商品门店列表
  13. * @param $goods_id 商品id
  14. * @param $supply_id 供应商id
  15. * @return mixed
  16. */
  17. public function get_goods_salesroom($goods_id,$supply_id,$field="*")
  18. {
  19. $list = array();
  20. $salesroom_base = M('lionfish_comshop_goods_salesroombase')->where(array('goods_id'=>$goods_id))->find();
  21. if($salesroom_base['hx_assign_salesroom'] == 0){
  22. $list = M('lionfish_comshop_salesroom')->where(array('supply_id'=>$supply_id,'state'=>1))->field($field)->select();
  23. }else{
  24. $goods_salesroom_list = M('lionfish_comshop_goods_relative_salesroom')->where(array('goods_id'=>$goods_id))->field('salesroom_id')->select();
  25. if(!empty($goods_salesroom_list)){
  26. $salesroom_ids = "";
  27. foreach($goods_salesroom_list as $k=>$v){
  28. if(empty($salesroom_ids)){
  29. $salesroom_ids = $v['salesroom_id'];
  30. }else{
  31. $salesroom_ids = $salesroom_ids .','. $v['salesroom_id'];
  32. }
  33. }
  34. $list = M('lionfish_comshop_salesroom')->where(array('supply_id'=>$supply_id,'state'=>1))->where("id in (".$salesroom_ids.")")->field($field)->select();
  35. }
  36. }
  37. return $list;
  38. }
  39. /**
  40. * 获取订单中所有门店信息
  41. * @param $order_id
  42. * @param string $field
  43. * @return array|mixed
  44. */
  45. public function get_order_salesroom($order_id,$page=0){
  46. $list = array();
  47. $all_salesroom_ids = "";
  48. $sql = "select gs.*,og.supply_id from ".C('DB_PREFIX')."lionfish_comshop_order_goods_saleshexiao as gs "
  49. . " left join " .C('DB_PREFIX')."lionfish_comshop_order_goods as og on og.goods_id = gs.goods_id "
  50. . " where og.order_id=".$order_id
  51. . " order by gs.id asc";
  52. $saleshexiao_list = M()->query($sql);
  53. if(!empty($saleshexiao_list)){
  54. foreach($saleshexiao_list as $sk=>$sv){
  55. $goods_id = $sv['goods_id'];
  56. $salesroom_base = $sv;
  57. $supply_id = $sv['supply_id'];
  58. if($salesroom_base['hx_assign_salesroom'] == 0){
  59. $salesroom_list = M('lionfish_comshop_salesroom')->where(array('supply_id'=>$supply_id,'state'=>1))->field('id')->select();
  60. if(!empty($salesroom_list)) {
  61. foreach ($salesroom_list as $v) {
  62. if (empty($all_salesroom_ids)) {
  63. $all_salesroom_ids = $v['id'];
  64. } else {
  65. $all_salesroom_ids = $all_salesroom_ids . ',' . $v['id'];
  66. }
  67. }
  68. }
  69. }else{
  70. $goods_salesroom_list = M('lionfish_comshop_order_goods_relative_salesroom')->where(array('goods_id'=>$goods_id,'order_id'=>$order_id))->field('salesroom_id')->select();
  71. if(!empty($goods_salesroom_list)){
  72. foreach($goods_salesroom_list as $k=>$v){
  73. if(empty($salesroom_ids)){
  74. $all_salesroom_ids = $v['salesroom_id'];
  75. }else{
  76. $all_salesroom_ids = $all_salesroom_ids .','. $v['salesroom_id'];
  77. }
  78. }
  79. }
  80. }
  81. }
  82. if(!empty($all_salesroom_ids)){
  83. if(!empty($page)){
  84. $size = 10;
  85. $offset = ($page - 1) * $size;
  86. $list = M('lionfish_comshop_salesroom')->where("id in (".$all_salesroom_ids.")")->limit($offset,$size)->select();
  87. }else{
  88. $list = M('lionfish_comshop_salesroom')->where("id in (".$all_salesroom_ids.")")->select();
  89. }
  90. }
  91. }
  92. if(!empty($list)){
  93. foreach($list as $k=>$v){
  94. $list[$k]['room_logo'] = tomedia($v['room_logo']);
  95. }
  96. }
  97. return $list;
  98. }
  99. /**
  100. * 获取商品订单中所有门店信息
  101. * @param $order_id
  102. * @param string $field
  103. * @return array|mixed
  104. */
  105. public function get_order_goods_salesroom($order_goods_id,$member_lon,$member_lat,$page){
  106. $list = array();
  107. $all_salesroom_ids = "";
  108. $sql = "select gs.*,og.supply_id from ".C('DB_PREFIX')."lionfish_comshop_order_goods_saleshexiao as gs "
  109. . " left join " .C('DB_PREFIX')."lionfish_comshop_order_goods as og on og.goods_id = gs.goods_id "
  110. . " where og.order_goods_id=".$order_goods_id
  111. . " order by gs.id asc";
  112. $salesroom_base_list = M()->query($sql);
  113. if(!empty($salesroom_base_list)) {
  114. foreach ($salesroom_base_list as $sk => $sv) {
  115. $goods_id = $sv['goods_id'];
  116. $salesroom_base = $sv;
  117. $supply_id = $sv['supply_id'];
  118. if ($salesroom_base['hx_assign_salesroom'] == 0) {
  119. $salesroom_list = M('lionfish_comshop_salesroom')->where(array('supply_id' => $supply_id, 'state' => 1))->field('id')->select();
  120. if (!empty($salesroom_list)) {
  121. foreach ($salesroom_list as $v) {
  122. if (empty($all_salesroom_ids)) {
  123. $all_salesroom_ids = $v['id'];
  124. } else {
  125. $all_salesroom_ids = $all_salesroom_ids . ',' . $v['id'];
  126. }
  127. }
  128. }
  129. } else {
  130. $goods_salesroom_list = M('lionfish_comshop_order_goods_relative_salesroom')->where(array('goods_id' => $goods_id,'order_goods_id'=>$order_goods_id))->field('salesroom_id')->select();
  131. if (!empty($goods_salesroom_list)) {
  132. foreach ($goods_salesroom_list as $k => $v) {
  133. if (empty($all_salesroom_ids)) {
  134. $all_salesroom_ids = $v['salesroom_id'];
  135. } else {
  136. $all_salesroom_ids = $all_salesroom_ids . ',' . $v['salesroom_id'];
  137. }
  138. }
  139. }
  140. }
  141. }
  142. if(!empty($all_salesroom_ids)){
  143. if (!empty($page)) {
  144. $size = 10;
  145. $offset = ($page - 1) * $size;
  146. $list = M('lionfish_comshop_salesroom')->where("id in (" . $all_salesroom_ids . ")")->limit($offset, $size)->select();
  147. } else {
  148. $list = M('lionfish_comshop_salesroom')->where("id in (" . $all_salesroom_ids . ")")->select();
  149. }
  150. foreach ($list as $slk => $slv) {
  151. $list[$slk]['distance'] = $this->cal_salesroom_distance($slv, $member_lon, $member_lat);
  152. }
  153. $distances = array_column($list, 'distance');
  154. array_multisort($distances, SORT_ASC, $list);
  155. }
  156. }
  157. return $list;
  158. }
  159. /**
  160. * 获取客户与门店距离
  161. * @param $salesroom_info 门店信息
  162. * @param $member_lon 买家经度
  163. * @param $member_lat 买家纬度
  164. */
  165. public function cal_salesroom_distance($salesroom_info,$member_lon,$member_lat){
  166. //门店经度
  167. $room_lon = $salesroom_info['lon'];
  168. //门店纬度
  169. $room_lat = $salesroom_info['lat'];
  170. $distance = D('Seller/Communityhead')->GetDistance($room_lon, $room_lat, $member_lon, $member_lat);
  171. $distance = ceil($distance / 1000);//KM距离
  172. return $distance;
  173. }
  174. /**
  175. * 获取订单最近一次核销记录
  176. * @param $order_id
  177. * @param string $order_goods_id
  178. */
  179. public function get_last_ordergoods_hexiaorecord($order_id,$order_goods_id=""){
  180. $condition = array();
  181. $condition['order_id'] = $order_id;
  182. if(!empty($order_goods_id)){
  183. $condition['order_goods_id'] = $order_goods_id;
  184. }
  185. $hx_recode = M('lionfish_comshop_order_goods_saleshexiao_record')->where($condition)->order('addtime desc')->find();
  186. return $hx_recode;
  187. }
  188. /**
  189. * 生成订单核销码
  190. * @param $order_id
  191. * @param $order_goods_id
  192. * @param $hexiao_qr_code
  193. * @return string
  194. */
  195. public function _get_ordergoods_hxqrcode($order_id,$order_goods_id,$hexiao_qr_code)
  196. {
  197. $type = 1;
  198. if(!empty($order_goods_id)){
  199. $type = 2;
  200. }
  201. $hx_qrcode = $type.'_'.$hexiao_qr_code;
  202. //核销地址
  203. $hexiao_qrcode = D('Home/Pingoods')->_get_commmon_hxqrcode($hx_qrcode);
  204. if( empty($hexiao_qrcode) )
  205. {
  206. return '';
  207. }else{
  208. if($type == 1){
  209. M('lionfish_comshop_order')->where( array('order_id' => $order_id ) )->save( array('hexiao_qr_code' => $hexiao_qrcode) );
  210. }else if($type == 2){
  211. M('lionfish_comshop_order_goods_saleshexiao')->where( array('order_id' => $order_id,'order_goods_id' => $order_goods_id ) )->save( array('hexiao_qr_code' => $hexiao_qrcode) );
  212. }
  213. return tomedia($hexiao_qrcode);
  214. }
  215. }
  216. /**
  217. * 通过member_id获取核销员id
  218. * @param $member_id
  219. * @return int
  220. */
  221. public function get_salesmember_id_by_member_id($member_id){
  222. $salesmember_info = M('lionfish_comshop_salesroom_member')->where(array('member_id'=>$member_id))->find();
  223. if(!empty($salesmember_info)){
  224. return $salesmember_info['id'];
  225. }else{
  226. return 0;
  227. }
  228. }
  229. /**
  230. * 通过核销员id获取核销门店列表
  231. * @param $smember_id
  232. * @return array
  233. */
  234. public function get_salesrooms_by_smember_id($smember_id){
  235. $sql = "select sr.* from ".C('DB_PREFIX')."lionfish_comshop_salesroom as sr "
  236. . " left join " .C('DB_PREFIX')."lionfish_comshop_salesroom_relative_member as srm on sr.id = srm.salesroom_id "
  237. . " where srm.smember_id=".$smember_id." and sr.state=1"
  238. . " order by sr.id asc";
  239. $salesrooms_list = M()->query($sql);
  240. foreach($salesrooms_list as $k=>$v){
  241. $v['room_logo'] = $v['room_logo']?tomedia($v['room_logo']):$v['room_logo'];
  242. $salesrooms_list[$k] = $v;
  243. }
  244. return $salesrooms_list;
  245. }
  246. /**
  247. * 通过核销员id获取今日核销记录
  248. * @param $smember_id
  249. */
  250. public function get_today_hexiao_record_by_smember_id($smember_id){
  251. $now_time = strtotime( date('Y-m-d').' 00:00:00' );
  252. $end_time = $now_time + 86400;
  253. $saleshexiao_record_list = M('lionfish_comshop_order_goods_saleshexiao_record')->where(array('salesmember_id'=>$smember_id))->where(array('addtime' => array('between', array($now_time,$end_time))))->select();
  254. if(!empty($saleshexiao_record_list)){
  255. foreach($saleshexiao_record_list as $k=>$v){
  256. //核销时间
  257. $saleshexiao_record_list[$k]['hx_time'] = date('Y-m-d H:i:s',$v['addtime']);
  258. $order_id = $v['order_id'];
  259. $order_goods_id = $v['order_goods_id'];
  260. $order_info = M('lionfish_comshop_order')->where(array('order_id'=>$order_id))->field('order_num_alias,shipping_tel')->find();
  261. //订单编号
  262. $saleshexiao_record_list[$k]['order_num_alias'] = $order_info['order_num_alias'];
  263. //用户手机号
  264. $saleshexiao_record_list[$k]['shipping_tel'] = substr_replace($order_info['shipping_tel'],'****',3,4);
  265. //商品信息
  266. $order_goods_info = M('lionfish_comshop_order_goods')->where(array('order_goods_id'=>$order_goods_id))->field('goods_id,name as goods_name,goods_images,total,quantity')->find();
  267. $saleshexiao_record_list[$k]['goods_id'] = $order_goods_info['goods_id'];
  268. $saleshexiao_record_list[$k]['goods_name'] =htmlspecialchars_decode(stripslashes($order_goods_info['goods_name']));
  269. $saleshexiao_record_list[$k]['goods_images'] = tomedia($order_goods_info['goods_images']);
  270. $saleshexiao_record_list[$k]['hexiao_count2'] = $v['hexiao_count'];
  271. $saleshexiao_record_list[$k]['quantity'] = $order_goods_info['quantity'];
  272. $saleshexiao_record_list[$k]['total'] = sprintf("%.2f", $order_goods_info['total']);
  273. $saleshexiao_record_list[$k]['option_sku'] = D('Seller/Order')->get_order_option_sku($order_id, $order_goods_id);
  274. $order_goods_saleshexiao_info = M('lionfish_comshop_order_goods_saleshexiao')->where(array('order_goods_id'=>$order_goods_id))->find();
  275. $saleshexiao_record_list[$k]['hexiao_type'] =$order_goods_saleshexiao_info['hexiao_type'];
  276. }
  277. }
  278. return $saleshexiao_record_list;
  279. }
  280. /**
  281. * 通过核销码获取订单信息
  282. *
  283. * return $order_result
  284. * is_exist 1、存在,0、不存在,2无核销权限
  285. */
  286. public function get_hexiao_order_by_code($hexiao_volume_code,$salesmember_id){
  287. $order_result = array();
  288. $field = "order_id,order_num_alias,member_id,ziti_name,ziti_mobile,shipping_name,shipping_tel,payment_code";
  289. //shipping_tel 收货号码
  290. $order_info = M('lionfish_comshop_order')->where(array('hexiao_volume_code'=>$hexiao_volume_code))->field($field)->find();
  291. $order_goods_saleshexiao_list = array();
  292. $order_result['is_exist'] = 1;
  293. if(empty($order_info)){
  294. $order_goods_saleshexiao_info = M('lionfish_comshop_order_goods_saleshexiao')->where(array('hexiao_volume_code'=>$hexiao_volume_code))->find();
  295. if(!empty($order_goods_saleshexiao_info)){
  296. $state = 1;
  297. //扫码
  298. $order_info = M('lionfish_comshop_order')->where(array('order_id'=>$order_goods_saleshexiao_info['order_id']))->field($field)->find();
  299. $order_goods_saleshexiao_list[0] = $order_goods_saleshexiao_info;
  300. }else{
  301. $state = 3;
  302. //手机号 1 4 5 6 7 11 12 14
  303. $order_list = M('lionfish_comshop_order')->where(array('shipping_tel'=>$hexiao_volume_code , 'delivery' =>'hexiao'))->where(' order_status_id = 4 || order_status_id = 11 ')->field('order_id')->select();
  304. if(empty($order_list)){
  305. if(strlen($hexiao_volume_code) == 11){
  306. $order_result['is_exist'] = 4;
  307. return $order_result;
  308. }else{
  309. $order_result['is_exist'] = 0;
  310. return $order_result;
  311. }
  312. }
  313. $order_list2 = array_column($order_list, 'order_id');
  314. $order_id = implode(",",$order_list2);
  315. $order_goods_saleshexiao_list = M('lionfish_comshop_order_goods_saleshexiao')->where(' order_id in('.$order_id.') ')->order('is_hexiao_over asc,addtime asc')->select();
  316. if(empty($order_goods_saleshexiao_list)){
  317. if(strlen($hexiao_volume_code) == 11){
  318. $order_result['is_exist'] = 4;
  319. return $order_result;
  320. }else{
  321. $order_result['is_exist'] = 0;
  322. return $order_result;
  323. }
  324. }
  325. $order_info = M('lionfish_comshop_order')->where(array('order_id'=>$order_list[0]['order_id']))->field($field)->find();
  326. }
  327. }else{
  328. //核销码
  329. $state = 2;
  330. $order_goods_saleshexiao_list = M('lionfish_comshop_order_goods_saleshexiao')->where(array('order_id'=>$order_info['order_id']))->select();
  331. }
  332. $hexiao_goods_list = array();
  333. $hx_i = 0;
  334. if(!empty($order_info)){
  335. $member_info = M('lionfish_comshop_member')->where(array('member_id'=>$order_info['member_id']))->find();
  336. //用户昵称
  337. $order_info['username'] = $member_info['username'];
  338. foreach($order_goods_saleshexiao_list as $k=>$v){
  339. if($this->check_goods_relative_smember($v['order_id'],$v['order_goods_id'],$v['goods_id'],$salesmember_id) == 1){
  340. $order_num_alias = M('lionfish_comshop_order')->where(array('order_id'=>$v['order_id']))->field('order_num_alias,order_status_id')->find();
  341. $order_goods_info = M('lionfish_comshop_order_goods')->where(array('order_goods_id'=>$v['order_goods_id']))->field('goods_id,name as goods_name,goods_images,quantity')->find();
  342. $order_goods_saleshexiao_list[$k]['goods_id'] = $order_goods_info['goods_id'];
  343. $order_goods_saleshexiao_list[$k]['goods_name'] = htmlspecialchars_decode(stripslashes($order_goods_info['goods_name']));
  344. $order_goods_saleshexiao_list[$k]['goods_images'] = tomedia($order_goods_info['goods_images']);
  345. $order_goods_saleshexiao_list[$k]['quantity'] = $order_goods_info['quantity'];
  346. $order_goods_saleshexiao_list[$k]['order_num_alias'] = $order_num_alias['order_num_alias'];
  347. $order_goods_saleshexiao_list[$k]['option_sku'] = D('Seller/Order')->get_order_option_sku($v['order_id'], $v['order_goods_id']);
  348. if($v['is_hexiao_over'] == 0 ){
  349. if($order_num_alias['order_status_id'] == 11){
  350. $order_goods_saleshexiao_list[$k]['is_hexiao_over'] = 1;
  351. }else{
  352. $order_goods_saleshexiao_list[$k]['is_hexiao_over'] = 0;
  353. }
  354. }else{
  355. $order_goods_saleshexiao_list[$k]['is_hexiao_over'] = 1;
  356. }
  357. //核销日期
  358. $order_goods_saleshexiao_list[$k]['effect_begin_time'] = date('Y-m-d',$v['effect_begin_time']);
  359. $order_goods_saleshexiao_list[$k]['effect_end_time'] = date('Y-m-d',$v['effect_end_time']);
  360. //已核销次数
  361. $order_goods_saleshexiao_list[$k]['has_hexiao_count'] = $v['hexiao_count']-$v['remain_hexiao_count'];
  362. if($v['hexiao_type'] == 1){
  363. if($v['hexiao_count'] == 0){
  364. $order_goods_saleshexiao_list[$k]['hexiao_count'] = '无限';
  365. $order_goods_saleshexiao_list[$k]['remain_hexiao_count'] = '无限';
  366. $has_hexiao_count = M('lionfish_comshop_order_goods_saleshexiao_record')->where(array('order_id'=>$v['order_id'],'order_goods_id'=>$v['order_goods_id']))->sum('hexiao_count');
  367. if(empty($has_hexiao_count)){
  368. $has_hexiao_count = 0;
  369. }
  370. $order_goods_saleshexiao_list[$k]['has_hexiao_count'] = $has_hexiao_count;
  371. }
  372. }
  373. $order_goods_saleshexiao_list[$k]['is_refund'] = 0;//未退款
  374. if( $order_goods_saleshexiao_list[$k]['refund_quantity'] > 0 ){
  375. if($order_goods_saleshexiao_list[$k]['refund_quantity'] == $order_goods_saleshexiao_list[$k]['quantity']){
  376. $order_goods_saleshexiao_list[$k]['is_refund'] = 1;//全部退款
  377. }else if($order_goods_saleshexiao_list[$k]['refund_quantity'] < $order_goods_saleshexiao_list[$k]['quantity']){
  378. $order_goods_saleshexiao_list[$k]['is_refund'] = 2;//部分退款
  379. }
  380. }
  381. $hexiao_goods_list[$hx_i] = $order_goods_saleshexiao_list[$k];
  382. $hx_i++;
  383. }
  384. }
  385. }
  386. //提取列数组;
  387. foreach($hexiao_goods_list as $val){
  388. $key_arrays[]=$val['is_hexiao_over'];
  389. }
  390. array_multisort($key_arrays,SORT_ASC,SORT_NUMERIC,$hexiao_goods_list);
  391. //货到付款收款码
  392. if($order_info['payment_code'] == 'cashon_delivery'){
  393. $order_info['cashondelivery_code_img'] = D('Home/Front')->getCashonDeliveryCode();
  394. }
  395. //核销订单用户信息
  396. $order_result['orders'] = $order_info;
  397. //核销订单商品列表
  398. $order_result['order_goods_saleshexiao_list'] = $hexiao_goods_list;
  399. //订单商品数量
  400. $order_result['order_goods_count'] = count($hexiao_goods_list);
  401. if(count($hexiao_goods_list) == 0){
  402. if($state == 3){
  403. $order_result['is_exist'] = 4;//无权限操作核销商品
  404. }else{
  405. $order_result['is_exist'] = 2;//无权限操作核销商品
  406. }
  407. }
  408. return $order_result;
  409. }
  410. /**
  411. * 判断核销员是否能核销商品
  412. * @param $order_id 订单id
  413. * @param $order_goods_id 商品订单id
  414. * @param $goods_id 商品id
  415. * @param $smember_id 核销员id
  416. * @return int 1、能核销,0、不能核销
  417. */
  418. public function check_goods_relative_smember($order_id,$order_goods_id,$goods_id,$smember_id){
  419. $flag = 0;
  420. $goods_salesroombase = M('lionfish_comshop_order_goods_saleshexiao')->where(array('order_id'=>$order_id,'order_goods_id'=>$order_goods_id,'goods_id'=>$goods_id))->find();
  421. $order_goods_info = M('lionfish_comshop_order_goods')->where(array('order_id'=>$order_id,'order_goods_id'=>$order_goods_id))->find();
  422. if($goods_salesroombase['hx_assign_salesroom'] == 1){
  423. $salesroom_list = M('lionfish_comshop_order_goods_relative_salesroom')->where(array('order_id'=>$order_id,'order_goods_id'=>$order_goods_id,'goods_id'=>$goods_id))->select();
  424. foreach($salesroom_list as $k=>$v){
  425. $salesroom_id = $v['salesroom_id'];
  426. $is_hx_member = $v['is_hx_member'];//是否指定核销员,0、不指定,1、指定
  427. if($is_hx_member == 0){//0、不指定
  428. $salesroom_relative_member = M('lionfish_comshop_salesroom_relative_member')->where(array('salesroom_id'=>$salesroom_id,'smember_id'=>$smember_id))->find();
  429. if(!empty($salesroom_relative_member)){
  430. $flag = 1;
  431. break;
  432. }
  433. }else{//1、指定
  434. $goods_smember_id = $v['smember_id'];
  435. $smemberid_array = explode(',',$goods_smember_id);
  436. foreach($smemberid_array as $sk=>$sv){
  437. if($sv == $smember_id){
  438. $flag = 1;
  439. break;
  440. }
  441. }
  442. }
  443. }
  444. }else{
  445. $sql = " SELECT s.supply_id FROM ".C('DB_PREFIX')."lionfish_comshop_salesroom_relative_member srm "
  446. . " LEFT JOIN ".C('DB_PREFIX')."lionfish_comshop_salesroom s ON srm.salesroom_id = s.id "
  447. . " WHERE smember_id = ".$smember_id." AND s.supply_id = ".$order_goods_info['supply_id'];
  448. $list = M()->query($sql);
  449. if(!empty($list) && count($list) > 0){
  450. $flag = 1;
  451. }
  452. }
  453. return $flag;
  454. }
  455. /**
  456. * 核销订单
  457. * @param $order_id 订单号
  458. * @param $salesmember_id 核销员
  459. * @param $salesroom_id 门店
  460. * @return array
  461. */
  462. public function hexiao_all_orders($order_id,$salesmember_id,$salesroom_id){
  463. $result = array();
  464. $hexiao_goods_list = array();
  465. $hx_goods_count = 0;
  466. $hx_i = 0;
  467. //订单信息
  468. $field = "order_id,order_num_alias,member_id,ziti_name,ziti_mobile,shipping_name,shipping_tel";
  469. $order_info = M('lionfish_comshop_order')->where(array('order_id'=>$order_id))->field($field)->find();
  470. //订单要核销商品(未核销完,剩余核销数量大于0)
  471. $order_goods_saleshexiao_list = M('lionfish_comshop_order_goods_saleshexiao')->where(array('order_id'=>$order_info['order_id'],'is_hexiao_over'=>0))->select();
  472. foreach($order_goods_saleshexiao_list as $k=>$v){
  473. if($this->check_goods_relative_smember($v['order_id'],$v['order_goods_id'],$v['goods_id'],$salesmember_id) == 1){
  474. $hexiao_goods_list[$hx_i] = $v;
  475. $hx_i++;
  476. }
  477. }
  478. if(count($hexiao_goods_list) > 0){
  479. //门店信息
  480. $salesroom_info = M('lionfish_comshop_salesroom')->where(array('id'=>$salesroom_id))->find();
  481. //核销员信息
  482. $salesmember_info = M('lionfish_comshop_salesroom_member')->where(array('id'=>$salesmember_id))->find();
  483. $result['hx_goods_count'] = count($hexiao_goods_list);
  484. foreach($hexiao_goods_list as $k=>$v){
  485. //核销商品
  486. $hx_result = $this->hexiao_order_goods($v,$salesmember_info,$salesroom_info,0);
  487. if($hx_result == 1){
  488. $hx_goods_count++;
  489. }
  490. }
  491. $this->hexiao_finished($order_id);
  492. }
  493. $result['hx_goods_count'] = $hx_goods_count;
  494. return $result;
  495. }
  496. /**
  497. * 全部核销商品
  498. * @param $order_goods_saleshexiao 订单商品核销信息表
  499. * @param $salesmember_info 核销员信息
  500. * @param $salesroom_info 门店信息
  501. * @param $hx_time 0、按订单核销,大于0、按次数核销
  502. * @return 1、核销商品成功,0、核销商品失败,-1 核销次数大于剩余次数
  503. */
  504. public function hexiao_order_goods($order_goods_saleshexiao,$salesmember_info,$salesroom_info,$hx_time){
  505. //订单核销信息表id
  506. $hx_id = $order_goods_saleshexiao['id'];
  507. //剩余核销数量
  508. $remain_hexiao_count = $order_goods_saleshexiao['remain_hexiao_count'];
  509. if($order_goods_saleshexiao['hexiao_count'] > 0) {
  510. $hexiao_count = 0;
  511. if ($hx_time > 0) {
  512. if ($hx_time > $remain_hexiao_count) {
  513. return -1;
  514. } else {
  515. $hexiao_count = $hx_time;
  516. }
  517. } else {
  518. $hexiao_count = $remain_hexiao_count;
  519. }
  520. $hexiao_data = array();
  521. if ($hexiao_count == $remain_hexiao_count) {
  522. $hexiao_data['remain_hexiao_count'] = 0;
  523. $hexiao_data['is_hexiao_over'] = 1;
  524. } else {
  525. $hexiao_data['remain_hexiao_count'] = $remain_hexiao_count - $hexiao_count;
  526. }
  527. $hx_result = M('lionfish_comshop_order_goods_saleshexiao')->where(array('id' => $hx_id))->save($hexiao_data);
  528. }else{
  529. if($hx_time == 0){
  530. $hexiao_data = array();
  531. $hexiao_data['is_hexiao_over'] = 1;
  532. $hexiao_count = 1;
  533. $hx_result = M('lionfish_comshop_order_goods_saleshexiao')->where(array('id'=>$hx_id))->save($hexiao_data);
  534. }else{
  535. $hx_result = 1;
  536. $hexiao_count = $hx_time;
  537. }
  538. }
  539. if($hx_result !== false){
  540. //添加订单核销记录表
  541. $hexiao_record = array();
  542. $hexiao_record['order_id'] = $order_goods_saleshexiao['order_id'];
  543. $hexiao_record['order_goods_id'] = $order_goods_saleshexiao['order_goods_id'];
  544. $hexiao_record['goods_id'] = $order_goods_saleshexiao['goods_id'];
  545. $hexiao_record['hexiao_count'] = $hexiao_count;
  546. $hexiao_record['salesroom_id'] = $salesroom_info['id'];
  547. $hexiao_record['salesroom_name'] = $salesroom_info['room_name'];
  548. $hexiao_record['salesmember_id'] = $salesmember_info['id'];
  549. $hexiao_record['smember_name'] = $salesmember_info['username'];
  550. $hexiao_record['member_id'] = $salesmember_info['member_id'];
  551. $hexiao_record['addtime'] = time();
  552. M('lionfish_comshop_order_goods_saleshexiao_record')->add($hexiao_record);
  553. return 1;
  554. }else{
  555. return 0;
  556. }
  557. }
  558. /**
  559. * 核销商品(按订单核销的商品)
  560. * @param $saleshexiao_info
  561. * @param $salesmember_id
  562. * @param $salesroom_id
  563. * @param $hx_time 0、按订单核销;大于0、按次核销次数
  564. * @return status -1、无限期核销,0、核销失败,1、核销成功
  565. */
  566. public function saleshexiao_order_goods($saleshexiao_info,$salesmember_id,$salesroom_id,$hx_time){
  567. $status = 0;
  568. if($this->check_goods_relative_smember($saleshexiao_info['order_id'],$saleshexiao_info['order_goods_id'],$saleshexiao_info['goods_id'],$salesmember_id) == 1){
  569. //门店信息
  570. $salesroom_info = M('lionfish_comshop_salesroom')->where(array('id'=>$salesroom_id))->find();
  571. //核销员信息
  572. $salesmember_info = M('lionfish_comshop_salesroom_member')->where(array('id'=>$salesmember_id))->find();
  573. //核销商品
  574. $hx_result = $this->hexiao_order_goods($saleshexiao_info,$salesmember_info,$salesroom_info,$hx_time);
  575. if($hx_result == 1){
  576. $this->hexiao_finished($saleshexiao_info['order_id']);
  577. $status = 1;
  578. }
  579. }else{
  580. $status = -1;
  581. }
  582. return $status;
  583. }
  584. /**
  585. * 获取按次核销商品信息
  586. * @param $saleshexiao_info
  587. * @param $salesmember_id
  588. * @return status -1、无限期核销,1、有核销数据
  589. */
  590. public function get_hxgoods_bytimes($saleshexiao_info,$salesmember_id){
  591. $result = array();
  592. $status = 1;
  593. if($this->check_goods_relative_smember($saleshexiao_info['order_id'],$saleshexiao_info['order_goods_id'],$saleshexiao_info['goods_id'],$salesmember_id) == 1){
  594. //商品信息
  595. $order_goods_info = M('lionfish_comshop_order_goods')->where(array('order_goods_id'=>$saleshexiao_info['order_goods_id']))->field('goods_id,name as goods_name,goods_images,quantity')->find();
  596. $order_goods_info['goods_images'] = tomedia($order_goods_info['goods_images']);
  597. $result['order_goods_info'] = $order_goods_info;
  598. $hexiao_record_list = M('lionfish_comshop_order_goods_saleshexiao_record')->where(array('order_id'=>$saleshexiao_info['order_id'],'order_goods_id'=>$saleshexiao_info['order_goods_id']))->select();
  599. if(!empty($hexiao_record_list)){
  600. foreach($hexiao_record_list as $k=>$v){
  601. $hexiao_record_list[$k]['goods_name'] = $order_goods_info['goods_name'];
  602. $hexiao_record_list[$k]['hx_time'] = date('Y-m-d H:i',$v['addtime']);
  603. }
  604. }
  605. $result['hexiao_record_list'] = $hexiao_record_list;
  606. //核销日期
  607. $saleshexiao_info['effect_begin_time'] = date('Y-m-d H:i',$saleshexiao_info['effect_begin_time']);
  608. $saleshexiao_info['effect_end_time'] = date('Y-m-d H:i',$saleshexiao_info['effect_end_time']);
  609. //已核销次数
  610. $saleshexiao_info['has_hexiao_count'] = $saleshexiao_info['hexiao_count']-$saleshexiao_info['remain_hexiao_count'];
  611. if($saleshexiao_info['hexiao_type'] == 1){
  612. if($saleshexiao_info['hexiao_count'] == 0){
  613. $saleshexiao_info['hexiao_count'] = '无限';
  614. $saleshexiao_info['remain_hexiao_count'] = '无限';
  615. }
  616. }
  617. $result['saleshexiao_info'] = $saleshexiao_info;
  618. }else{
  619. $status = -1;
  620. }
  621. $result['status'] = $status;
  622. return $result;
  623. }
  624. /**
  625. * 核销完成
  626. * @param $order_id
  627. */
  628. public function hexiao_finished($order_id){
  629. $is_finished = true;
  630. $hexiao_list = M('lionfish_comshop_order_goods_saleshexiao')->where(array('order_id'=>$order_id))->select();
  631. foreach($hexiao_list as $k=>$v){
  632. if($v['hexiao_type'] == 0 && $v['is_hexiao_over'] == 0){//按订单核销
  633. $is_finished = false;
  634. }
  635. if($v['hexiao_type'] == 1 && $v['is_hexiao_over'] == 0){//按次核销
  636. if($v['hexiao_count'] > 0 && $v['remain_hexiao_count'] > 0){
  637. $is_finished = false;
  638. }
  639. if($v['hexiao_count'] == 0){
  640. $is_finished = false;
  641. }
  642. }
  643. }
  644. if($is_finished){
  645. $order_history = array();
  646. $order_history['order_id'] = $order_id;
  647. $order_history['order_status_id'] = 11;
  648. $order_history['notify'] = 0;
  649. $order_history['comment'] = '核销员核销,订单完成。';
  650. $order_history['date_added']=time();
  651. M('lionfish_comshop_order_history')->add($order_history);
  652. //ims_
  653. M('lionfish_comshop_order')->where( array('order_id' => $order_id) )->save( array('order_status_id' => 11,'finishtime' => time()) );
  654. $time = time();
  655. $open_aftersale = D('Home/Front')->get_config_by_name('open_aftersale');
  656. $open_aftersale_time = D('Home/Front')->get_config_by_name('open_aftersale_time');
  657. $statements_end_time = $time;
  658. if( !empty($open_aftersale) && !empty($open_aftersale_time) && $open_aftersale_time > 0 )
  659. {
  660. $statements_end_time = $statements_end_time + 86400 * $open_aftersale_time;
  661. }
  662. $up_order_data = array();
  663. $up_order_data['statements_end_time'] = $statements_end_time;
  664. M('lionfish_comshop_order_goods')->where( array('order_id' => $order_id) )->save( $up_order_data );
  665. }
  666. }
  667. /**
  668. * 获取核销商品可退款数量
  669. * @param $order_id 订单号
  670. * @param $order_goods_id 商品订单号
  671. * @return 可退款数量
  672. */
  673. public function get_hexiao_order_goods_can_refund_quantity($order_id,$order_goods_id){
  674. $sales_hexiao_info = M('lionfish_comshop_order_goods_saleshexiao')->where(array('order_id'=>$order_id,'order_goods_id'=>$order_goods_id))->find();
  675. if($sales_hexiao_info['hexiao_type'] == 1){
  676. $can_refund_quantity = floor($sales_hexiao_info['remain_hexiao_count']/$sales_hexiao_info['one_hexiao_count']);
  677. }else{
  678. $can_refund_quantity = $sales_hexiao_info['goods_quantity']-$sales_hexiao_info['refund_quantity'];
  679. }
  680. return $can_refund_quantity;
  681. }
  682. /**
  683. * 获取核销商品已使用数量
  684. * @param $order_id 订单号
  685. * @param $order_goods_id 商品订单号
  686. * @return 已使用数量
  687. */
  688. public function get_hexiao_order_goods_used_quantity($order_id,$order_goods_id){
  689. $used_quantity = 0;
  690. $sales_hexiao_info = M('lionfish_comshop_order_goods_saleshexiao')->where(array('order_id'=>$order_id,'order_goods_id'=>$order_goods_id))->find();
  691. if($sales_hexiao_info['hexiao_type'] == 1){
  692. $used_quantity = ($sales_hexiao_info['goods_quantity'] - $sales_hexiao_info['refund_quantity']) - floor($sales_hexiao_info['hexiao_count']/$sales_hexiao_info['one_hexiao_count']);
  693. }else{
  694. if($sales_hexiao_info['hexiao_count'] == 0){
  695. $used_quantity = $sales_hexiao_info['goods_quantity'] - $sales_hexiao_info['refund_quantity'];
  696. }else{
  697. $used_quantity = 0;
  698. }
  699. }
  700. return $used_quantity;
  701. }
  702. /**
  703. * 获取核销商品已使用金额
  704. * @param $order_id 订单号
  705. * @param $order_goods_id 商品订单号
  706. * @param $refund_quantity 退款数量
  707. * @return 已使用商品金额
  708. */
  709. public function get_hexiao_order_goods_used_total($order_id,$order_goods_id){
  710. $order_goods_info = M('lionfish_comshop_order_goods')->where( array('order_goods_id' => $order_goods_id ) )->find();
  711. $order_goods_total = $order_goods_info['total']-$order_goods_info['voucher_credit']-$order_goods_info['fullreduction_money'] - $order_goods_info['score_for_money'];
  712. //已使用数量
  713. $used_quantity = $this->get_hexiao_order_goods_used_quantity($order_id,$order_goods_id);
  714. $used_total = round($order_goods_total/$order_goods_info['quantity']*$used_quantity,2);
  715. return $used_total;
  716. }
  717. /**
  718. * 获取核销订单已使用数量
  719. * @param unknown $order_id
  720. */
  721. public function get_hexiao_order_used_quantity($order_id){
  722. $used_quantity = 0;
  723. $order_goods_list = M('lionfish_comshop_order_goods')->where(array('order_id'=>$order_id))->select();
  724. foreach ($order_goods_list as $k=>$v){
  725. $used_quantity = $used_quantity + $this->get_hexiao_order_goods_used_quantity($order_id,$v['order_goods_id']);
  726. }
  727. return $used_quantity;
  728. }
  729. /**
  730. * 获取核销订单已使用金额
  731. * @param unknown $order_id
  732. */
  733. public function get_hexiao_order_used_total($order_id){
  734. $used_total = 0;
  735. $order_goods_list = M('lionfish_comshop_order_goods')->where(array('order_id'=>$order_id))->select();
  736. foreach ($order_goods_list as $k=>$v){
  737. $used_total = round($used_total + $this->get_hexiao_order_goods_used_total($order_id,$v['order_goods_id']),2);
  738. }
  739. return $used_total;
  740. }
  741. /**
  742. * 核销商品订单退款
  743. * @param unknown $order_id
  744. * @param unknown $order_goods_id
  745. * @param unknown $refund_quantity
  746. */
  747. public function hexiao_refund_action($order_id,$order_goods_id,$refund_quantity){
  748. $sales_hexiao_info = M('lionfish_comshop_order_goods_saleshexiao')->where(array('order_id'=>$order_id,'order_goods_id'=>$order_goods_id))->find();
  749. $data = array();
  750. //退款数量
  751. $data['refund_quantity'] = $sales_hexiao_info['refund_quantity']+$refund_quantity;
  752. if($sales_hexiao_info['hexiao_type'] == 0){
  753. if($sales_hexiao_info['goods_quantity'] == $data['refund_quantity']){
  754. $data['remain_hexiao_count'] = 0;
  755. $data['is_hexiao_over'] = 1;
  756. }
  757. }else{
  758. //剩余核销数量
  759. $data['remain_hexiao_count'] = $sales_hexiao_info['remain_hexiao_count']-$refund_quantity*$sales_hexiao_info['one_hexiao_count'];
  760. if($sales_hexiao_info['goods_quantity'] == $data['refund_quantity']){
  761. $data['is_hexiao_over'] = 1;
  762. }
  763. }
  764. M('lionfish_comshop_order_goods_saleshexiao')->where(array('id'=>$sales_hexiao_info['id']))->save($data);
  765. }
  766. /**
  767. * 核销订单全部退款
  768. * @param unknown $order_id
  769. * @param unknown $order_goods_id
  770. * @param unknown $refund_quantity
  771. */
  772. public function hexiao_order_refund_action($order_id){
  773. $order_goods_list = M('lionfish_comshop_order_goods')->where(array('order_id'=>$order_id))->select();
  774. foreach ($order_goods_list as $k=>$v){
  775. $sales_hexiao_info = M('lionfish_comshop_order_goods_saleshexiao')->where(array('order_id'=>$order_id,'order_goods_id'=>$v['order_goods_id']))->find();
  776. $used_quantity = $this->get_hexiao_order_goods_used_quantity($order_id,$v['order_goods_id']);
  777. $refund_quantity = $sales_hexiao_info['goods_quantity'] - $sales_hexiao_info['refund_quantity'] - $used_quantity;
  778. if($refund_quantity > 0){
  779. $this->hexiao_refund_action($order_id,$v['order_goods_id'],$refund_quantity);
  780. }
  781. }
  782. }
  783. /**
  784. * 定时执行
  785. * 核销订单过期处理
  786. */
  787. public function hexiao_expire(){
  788. $now_time = time();
  789. $condition = " is_hexiao_over = 0 and effect_end_time < ".$now_time;
  790. $order_goods_hx_list = M('lionfish_comshop_order_goods_saleshexiao')->where($condition)->select();
  791. foreach($order_goods_hx_list as $k=>$v){
  792. $order_info = M('lionfish_comshop_order')->where(array('order_id'=>$v['order_id']))->field('order_status_id')->find();
  793. if($order_info['order_status_id'] == 5){
  794. $data = array();
  795. $data['is_hexiao_over'] = 3;//已取消
  796. $data['expire_act_time'] = time();//取消时间
  797. M('lionfish_comshop_order_goods_saleshexiao')->where(array('id'=>$v['id']))->save($data);
  798. }else{
  799. $data = array();
  800. $data['is_hexiao_over'] = 2;//已过期
  801. $data['expire_act_time'] = time();//过期时间
  802. M('lionfish_comshop_order_goods_saleshexiao')->where(array('id'=>$v['id']))->save($data);
  803. //添加订单核销记录表
  804. $hexiao_record = array();
  805. $hexiao_record['order_id'] = $v['order_id'];
  806. $hexiao_record['order_goods_id'] = $v['order_goods_id'];
  807. $hexiao_record['goods_id'] = $v['goods_id'];
  808. $hexiao_record['hexiao_count'] = $v['remain_hexiao_count'];
  809. $hexiao_record['smember_name'] = "商品过期自动使用";
  810. $hexiao_record['is_admin'] = 1;
  811. $hexiao_record['addtime'] = time();
  812. M('lionfish_comshop_order_goods_saleshexiao_record')->add($hexiao_record);
  813. //过期核销订单完成
  814. $this->hexiao_auto_expire_finished($v['order_id']);
  815. }
  816. }
  817. }
  818. /**
  819. * 过期核销订单完成
  820. * @param $order_id
  821. */
  822. public function hexiao_auto_expire_finished($order_id){
  823. $is_finished = true;
  824. $hexiao_list = M('lionfish_comshop_order_goods_saleshexiao')->where(array('order_id'=>$order_id))->select();
  825. foreach($hexiao_list as $k=>$v){
  826. if($v['is_hexiao_over'] == 0){
  827. $is_finished = false;
  828. }
  829. }
  830. if($is_finished){
  831. $order_history = array();
  832. $order_history['order_id'] = $order_id;
  833. $order_history['order_status_id'] = 11;
  834. $order_history['notify'] = 0;
  835. $order_history['comment'] = '商品过期自动使用,订单完成。';
  836. $order_history['date_added']=time();
  837. M('lionfish_comshop_order_history')->add($order_history);
  838. //ims_
  839. M('lionfish_comshop_order')->where( array('order_id' => $order_id) )->save( array('order_status_id' => 11,'finishtime' => time()) );
  840. }
  841. }
  842. /**
  843. * 核销商品距离过期时间一个小时自动下架
  844. */
  845. public function hexiao_goods_expire(){
  846. $now_time = time();
  847. $sql = "SELECT g.id, g.grounding, g.goodsname, gs.hx_expire_end_time, gs.hx_auto_off_time, gs.hx_auto_off, gs.hx_expire_type "
  848. . " FROM ".C('DB_PREFIX')."lionfish_comshop_goods_salesroombase gs,".C('DB_PREFIX')."lionfish_comshop_goods g "
  849. . " WHERE g.grounding = 1 AND gs.hx_expire_type = 1 and gs.goods_id = g.id";
  850. $order_goods_hx_list = M()->query($sql);
  851. foreach($order_goods_hx_list as $k=>$v){
  852. $goods_id = $v['id'];
  853. $is_grounding = false;
  854. //商品过期时间
  855. $hx_expire_end_time = $v['hx_expire_end_time'];
  856. $hx_auto_off_time = $v['hx_auto_off_time'];
  857. if($v['hx_auto_off'] == 1){
  858. $hx_auto_time = $hx_auto_off_time*3600 + $now_time;
  859. if($hx_expire_end_time < $hx_auto_time){
  860. $is_grounding = true;
  861. }
  862. }else{
  863. if($hx_expire_end_time < $now_time){
  864. $is_grounding = true;
  865. }
  866. }
  867. if($is_grounding){
  868. $data = array();
  869. $data['grounding'] = 0;//下架
  870. M('lionfish_comshop_goods')->where(array('id'=>$goods_id))->save($data);
  871. }
  872. }
  873. }
  874. }