ROrder.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /**
  3. * User: charlie
  4. * O2O 订单表
  5. */
  6. class ROrder extends MongoAr
  7. {
  8. public $_id;
  9. // public $pay_id;//由ping++生成的支付单号
  10. //public $pay_type;//用户的支付方式
  11. public $charge_id;//ping++的chargeId,charge_id即为支付单号
  12. public $pay_channel;//支付渠道
  13. public $channel;//订单来源渠道
  14. public $precedence = 0;//加急 默认0
  15. public $booking_time; //用户预约的时间
  16. public $order_time; //订单生成时间
  17. public $deal_time; //订单处理时间
  18. public $take_time; //接单时间
  19. public $set_out_time; //出发时间
  20. public $arrive_time; //到达时间
  21. public $finish_time; //完成时间
  22. public $cancel_time; //订单取消时间
  23. public $apply_refund_time; //订单申请退款时间
  24. public $refund_time; //订单退款时间
  25. public $append_orders = array();
  26. public $products=array(); //订单包含的商品数组 数据库设计 支持多个产品在一个订单
  27. public $station = '';//服务网点,station的mongoid
  28. public $price; //订单金额
  29. public $final_price; //订单使用代金券之后的金额 如果没有使用代金券 $final_price=$price
  30. public $pay_price;//订单最终支付的金额 price-coupons-用户要支付的余额
  31. public $coupons=array();//订单使用的代金券 数据库设计可以支持多张
  32. public $coupon_type;
  33. public $user; //对应的RUser 的mongoid
  34. public $status=0;//订单状态 0=>待支付 1=>已支付 2=>已处理 3=>已接单 4=>已出发 5=>已上门 6=>已完成 -1=>已取消 -2=>已退款
  35. public $memo = '';//用户备注
  36. public $remark = '';//后台备注
  37. public $type = 0;//订单类型
  38. public $counts=1;//预订的数量
  39. public $have_comment = 0; //是否已评价
  40. public $technicians = array(); // 接单保洁师
  41. //public $technician = ''; // 接单保洁师,user的id
  42. //public $technician_name = ''; // 接单保洁师姓名
  43. public $address = array(); //地址信息 包含 province city area position detail name mobile position
  44. public static $order_filter = array(
  45. 0 => array('name' => '未选择'),
  46. 1 => array('name' => '来源'),
  47. 2 => array('name' => '服务'),
  48. 3 => array('name' => '状态')
  49. );
  50. public static $status_option = array(
  51. 0 => array('name' => '待支付'),
  52. 1 => array('name' => '已支付'),
  53. 2 => array('name' => '已处理'),
  54. 3 => array('name' => '已接单'),
  55. 4 => array('name' => '已出发'),
  56. 5 => array('name' => '已上门'),
  57. 6 => array('name' => '已完成'),
  58. 7 => array('name' => '退款中'),
  59. -1 => array('name' => '已取消'),
  60. -2 => array('name' => '已退款'),
  61. -3 => array('name' => '申请退款'),
  62. );
  63. public static $channel_option = array(
  64. "wx_pub" => array('name' => '微信公众号'),
  65. "dongfang" => array('name' => '东方CJ'),
  66. "youzan" => array('name' => '有赞'),
  67. "shangmenshoukuan" => array('name' => '保洁师上门收款'),
  68. "other" => array('name' => '其他'),
  69. );
  70. public function __construct($scenario='insert'){
  71. $this->setMongoDBComponent(Yii::app()->getComponent('mongodb_o2o'));
  72. parent::__construct($scenario);
  73. }
  74. public static function model($className=__CLASS__)
  75. {
  76. return parent::model($className);
  77. }
  78. public static function get($_id) {
  79. if(CommonFn::isMongoId($_id)){
  80. $criteria = new EMongoCriteria();
  81. $criteria->_id('==', $_id);
  82. $model = self::model()->find($criteria);
  83. return $model;
  84. }else{
  85. return false;
  86. }
  87. }
  88. public function getCollectionName()
  89. {
  90. return 'orders';
  91. }
  92. public function parseRow($row,$output=array()){
  93. $newRow = array();
  94. $newRow['id'] = (string)$row['_id'];
  95. // $newRow['pay_id'] = CommonFn::get_val_if_isset($row,'pay_id','');
  96. $newRow['pay_channel'] = CommonFn::get_val_if_isset($row,'pay_channel','');
  97. $newRow['charge_id'] = CommonFn::get_val_if_isset($row,'charge_id','');
  98. $newRow['channel'] = CommonFn::get_val_if_isset($row,'channel','wx_pub');
  99. $newRow['type'] = CommonFn::get_val_if_isset($row,'type','');
  100. $newRow['counts'] = CommonFn::get_val_if_isset($row,'counts',1);
  101. $newRow['booking_time'] = CommonFn::get_val_if_isset($row,'booking_time',0);
  102. $newRow['order_time'] = CommonFn::get_val_if_isset($row,'order_time',0);
  103. $newRow['have_comment'] = CommonFn::get_val_if_isset($row,'have_comment',0);
  104. $newRow['order_num'] = $newRow['order_time'].hexdec(substr((string)$row['user'],-3));
  105. $newRow['deal_time'] = CommonFn::get_val_if_isset($row,'deal_time',0);
  106. $newRow['take_time'] = CommonFn::get_val_if_isset($row,'take_time',0);
  107. $newRow['set_out_time'] = CommonFn::get_val_if_isset($row,'set_out_time',0);
  108. $newRow['arrive_time'] = CommonFn::get_val_if_isset($row,'arrive_time',0);
  109. $newRow['finish_time'] = CommonFn::get_val_if_isset($row,'finish_time',0);
  110. $newRow['cancel_time'] = CommonFn::get_val_if_isset($row,'cancel_time',0);
  111. $newRow['apply_refund_time'] = CommonFn::get_val_if_isset($row,'apply_refund_time',0);
  112. $newRow['apply_refund_time_str'] = $newRow['apply_refund_time']?date('Y年n月d日 H:i',$newRow['apply_refund_time']):'';
  113. $newRow['refund_time'] = CommonFn::get_val_if_isset($row,'refund_time',0);
  114. $newRow['booking_time_str'] = date('Y年n月d日 H:i',$newRow['booking_time']);
  115. $newRow['order_time_str'] = date("n月d日 H:i", $newRow['order_time']);
  116. $newRow['deal_time_str'] = $newRow['deal_time']?CommonFn::sgmdate("Y年n月d日:H:i:s", $newRow['deal_time'],1):'';
  117. $newRow['take_time_str'] = $newRow['take_time']?CommonFn::sgmdate("Y年n月d日:H:i:s", $newRow['take_time'],1):'';
  118. $newRow['set_out_time_str'] = $newRow['set_out_time']?CommonFn::sgmdate("Y年n月d日:H:i:s", $newRow['set_out_time'],1):'';
  119. $newRow['arrive_time_str'] = $newRow['arrive_time']?CommonFn::sgmdate("Y年n月d日:H:i:s", $newRow['arrive_time'],1):'';
  120. $newRow['finish_time_str'] = $newRow['finish_time']?CommonFn::sgmdate("Y年n月d日:H:i:s", $newRow['finish_time'],1):'';
  121. $newRow['cancel_time_str'] = $newRow['cancel_time']?CommonFn::sgmdate("Y年n月d日:H:i:s", $newRow['cancel_time'],1):'';
  122. $newRow['refund_time_str'] = $newRow['refund_time']?CommonFn::sgmdate("Y年n月d日:H:i:s", $newRow['refund_time'],1):'';
  123. $newRow['sum_price'] = 0;
  124. $newRow['price'] = CommonFn::get_val_if_isset($row,'price',0);
  125. $newRow['af_sum_price'] = $newRow['price'];
  126. $newRow['final_price'] = intval(CommonFn::get_val_if_isset($row,'final_price',0));
  127. $newRow['pay_price'] = CommonFn::get_val_if_isset($row,'pay_price',$newRow['final_price']);
  128. $newRow['sum_price'] = $newRow['final_price'];
  129. $newRow['memo'] = CommonFn::get_val_if_isset($row,'memo','');
  130. $newRow['remark'] = CommonFn::get_val_if_isset($row,'remark','');
  131. $newRow['technicians'] = CommonFn::get_val_if_isset($row,'technicians','');
  132. $user = array();
  133. $t_user = new ZUser();
  134. if(isset($row['user']) && $row['user']){
  135. $_user = $t_user->get($row['user']);
  136. $user = RUser::model()->parseRow($_user->attributes,array('user_name','id','avatar'));
  137. $user['otherPlatform'] = 0;
  138. }else{
  139. $user_name = self::$channel_option[$newRow['channel']]['name'].'用户';
  140. $user = array('user_name'=>$user_name,'id'=>'','avatar'=>Yii::app()->params['defaultUserAvatar'], 'otherPlatform' => 1);
  141. }
  142. $newRow['user'] = $user;
  143. $products = array();
  144. // $t_component = new ZComponent();
  145. $newRow['products_str'] = '';
  146. if(isset($row['products'])&&is_array($row['products'])){
  147. // $_products = $t_component->getList(Product::model(),$row['products']);
  148. foreach ($row['products'] as $key => $product) {
  149. $product_obj = Product::get($product['product']);
  150. $temp_info = $product_obj->parseRow($product_obj);
  151. //echo(json_encode($temp_info));exit;
  152. $temp_info['count'] = $product['count'];
  153. //$temp_info['extra'] = $product['extra'];
  154. $products[] = array('product'=>$temp_info,'extra'=>$product['extra']);
  155. //$temp_info['extra'] = $product['extra'];
  156. if($key == 0){
  157. $newRow['products_str'] .= $temp_info['name'];
  158. }else{
  159. $newRow['products_str'] .= '+'.$temp_info['name'];
  160. }
  161. }
  162. }
  163. $newRow['products'] = $products;
  164. if(!isset($newRow['products'])||empty($newRow['products'])){
  165. $newRow['products']=CommonFn::$empty;
  166. }
  167. $newRow['station'] = (object)array();
  168. if(isset($row['station'])){
  169. $staion = Station::get($row['station']);
  170. $staion = Station::model()->parseRow($staion,array('id','name'));
  171. $newRow['station'] = $staion;
  172. $newRow['station_id'] = $staion['id'];
  173. $newRow['station_name'] = $staion['name'];
  174. }
  175. foreach( $newRow['technicians'] as $technicians) {
  176. $newRow['technician'] = $technicians;
  177. if ($newRow['technician']) {
  178. $tech_info = TechInfo::get($newRow['technician']);
  179. if ($tech_info) {
  180. $newRow['tech_info'][] = TechInfo::model()->parseRow($tech_info, array('id', 'name', 'mobile', 'weixin_userid'));
  181. } else {
  182. $newRow['tech_info'][] = [];
  183. }
  184. } else {
  185. $newRow['tech_info'][] = [];
  186. }
  187. }
  188. //$newRow['technician_name'] = isset($newRow['tech_info']['name']) ? $newRow['tech_info']['name'] : '';
  189. // $newRow['hasSendTec'] = $newRow['technicians'] == 0 ? 0:1;
  190. foreach($newRow['technicians'] as $technician) {
  191. if ($technician == 0) {
  192. $newRow['hasSendTec'] = 0;
  193. } else {
  194. $newRow['hasSendTec'] = 1;
  195. }
  196. }
  197. $coupons = array();
  198. if(isset($row['coupons'])&&is_array($row['coupons'])){
  199. foreach ($row['coupons'] as $coupon) {
  200. $user_coupon_obj = UserCoupon::get($coupon);
  201. if(!$user_coupon_obj){
  202. continue;
  203. }
  204. $coupons[] = $user_coupon_obj->parseRow($user_coupon_obj);
  205. }
  206. }
  207. $newRow['coupons'] = $coupons;
  208. if(!isset($newRow['coupons'])||empty($newRow['coupons'])){
  209. $newRow['coupons']=CommonFn::$empty;
  210. }
  211. if(!isset($newRow['coupons'])||empty($newRow['coupons'])){
  212. $newRow['coupons']=CommonFn::$empty;
  213. }
  214. $newRow['address'] = CommonFn::get_val_if_isset($row,'address',array("province"=>"","city"=>"","area"=>"","address_info"=>"","name"=>"","mobile"=>"","position"=>array(121,31)));
  215. if(!isset($newRow['address']['province'])){
  216. $newRow['address']['province'] = '';
  217. }
  218. if(!isset($newRow['address']['city'])){
  219. $newRow['address']['city'] = '';
  220. }
  221. if(!isset($newRow['address']['area'])){
  222. $newRow['address']['area'] = '';
  223. }
  224. if(!isset($newRow['address']['detail'])){
  225. $newRow['address']['detail'] = '';
  226. }
  227. if(!isset($newRow['address']['name'])){
  228. $newRow['address']['name'] = '';
  229. }
  230. if(!isset($newRow['address']['mobile'])){
  231. $newRow['address']['mobile'] = '';
  232. }
  233. if(!isset($newRow['address']['position'])){
  234. $newRow['address']['position'] = array(121,31);
  235. }
  236. if(!isset($newRow['address']['poi']) || !isset($newRow['address']['poi']['name'])){
  237. $newRow['address']['poi'] = array('name'=>'','uid'=>'');
  238. }
  239. //echo(ROrder::$status_option[$newRow['status']]);exit;
  240. $newRow['status'] = CommonFn::get_val_if_isset($row,'status',1);
  241. $newRow['status_str'] = ROrder::$status_option[$newRow['status']]['name'];
  242. $newRow['book_status'] = '';
  243. if($newRow['status'] == 6){
  244. $newRow['book_status'] = 3;
  245. $newRow['book_status_str'] = '已完成';
  246. }elseif ($newRow['status'] == -1) {
  247. $newRow['book_status'] = 2;
  248. $newRow['book_status_str'] = '已取消';
  249. }else{
  250. $newRow['book_status'] = 1;
  251. $newRow['book_status_str'] = '预约中';
  252. }
  253. $newRow['action_user'] = CommonFn::get_val_if_isset($row,'action_user',"");
  254. $newRow['action_time'] = CommonFn::get_val_if_isset($row,'action_time',"");
  255. $newRow['action_log'] = CommonFn::get_val_if_isset($row,'action_log',"");
  256. // 订单评分处理
  257. if (intval($row['have_comment']) == 1) {
  258. $criteria = new EMongoCriteria();
  259. $criteria->order = $row['_id'];
  260. $comment = Comment::model()->find($criteria);
  261. $newRow['score'] = $comment->score;
  262. $newRow['commentId'] = (string)$comment->_id;
  263. } else {
  264. $newRow['score'] = 100;
  265. $newRow['commentId'] = '';
  266. }
  267. if(APPLICATION=='admin'){
  268. if($newRow['address']['mobile']){
  269. $mongo = new MongoClient(DB_CONNETC);
  270. $where = array();
  271. $where['address.mobile'] = $newRow['address']['mobile'];
  272. $where['status'] = array('$gt'=>0);
  273. $newRow['order_count'] = $mongo->fuwu->orders->count($where);
  274. }else{
  275. $newRow['order_count'] = 0;
  276. }
  277. }
  278. if(APPLICATION=='api'){
  279. unset($newRow['charge_id']);
  280. unset($newRow['action_user']);
  281. unset($newRow['action_time']);
  282. unset($newRow['action_log']);
  283. }
  284. return $this->output($newRow,$output);
  285. }
  286. /**
  287. * ROrder保存后的回调函数
  288. */
  289. protected function afterSave() {
  290. parent::afterSave();
  291. // 生成保洁师提成并保存
  292. if ($this->status == 6 && !empty($this->technicians)) {
  293. // 订单时间检查
  294. // 预定时间是否在12月之后
  295. $time = time();
  296. if ($this->booking_time >= strtotime('2015-11-01')) {
  297. // 若完成时间与预定时间相差大于7天,则完成时间为预定时间+3天
  298. if ($this->booking_time + 604800 < $time) {
  299. $time = $this->booking_time + 259200;
  300. }
  301. } else {
  302. return true;
  303. }
  304. $order = $this->_id;
  305. $commisionObj = Commision::getByOrder($order);
  306. // 检查Cmmmision信息是否已录入
  307. foreach ($this->technicians as $key => $value) {
  308. $technician_id = $value['technician_id'];
  309. $technician = $value['technician_name'];
  310. $tech = TechInfo::get($technician_id);
  311. if ($commisionObj == false) {
  312. // 普通订单生成提成并保存
  313. $commisionObj = new Commision();
  314. $commisionObj->time = empty($this->finish_time) ? $time : $this->finish_time;
  315. $commisionObj->booking_time = $this->booking_time;
  316. $commisionObj->user = $technician;
  317. $commisionObj->order = $this->_id;
  318. $commisionObj->commision = Commision::getCommision($this, Commision::MAIN, $tech->scheme);
  319. $commisionObj->type = Commision::MAIN;
  320. $commisionObj->insert();
  321. // 订单内附加订单生成提成并保存
  322. $appends = $this->append_orders;
  323. if (!empty($appends)) {
  324. $criteria = new EMongoCriteria();
  325. $criteria->_id('in', $appends);
  326. $appendOrders = AppendOrder::model()->findAll($criteria);
  327. foreach ($appendOrders as $key => $row) {
  328. if ($row->status != 1) {
  329. continue;
  330. }
  331. $commisionObj = new Commision();
  332. $commisionObj->time = empty($this->finish_time) ? $time : $this->finish_time;
  333. $commisionObj->booking_time = $this->booking_time;
  334. $commisionObj->user = $technician;
  335. $commisionObj->order = $row->_id;
  336. $commisionObj->commision = Commision::getCommision($row, Commision::APPEND, $tech->scheme);
  337. $commisionObj->type = Commision::APPEND;
  338. $commisionObj->insert();
  339. }
  340. }
  341. }
  342. // 提示保洁师订单已完成
  343. if ($tech && $tech->weixin_userid) {
  344. // 检查订单评价是否存在,若存在则不发送
  345. $comment = Comment::getByOrder($this->_id);
  346. if (!$comment) {
  347. $wechat = O2oApp::getWechatActive();
  348. $url_prefix = ENVIRONMENT == 'product' ? 'http:// api.yiguanjia.me' : 'http:// apitest.yiguanjia.me';
  349. $wechat_data = [
  350. 'touser' => $tech->weixin_userid,
  351. 'msgtype' => 'news',
  352. 'agentid' => '24',
  353. 'news' => [
  354. 'articles' => [
  355. [
  356. 'title' => '壹管家提示-订单已完成',
  357. 'description' => $tech->name . '你好!预定时间在' . date('m月d日H:i', $this->booking_time) . '的订单已完成,请点击查看订单情况。',
  358. 'url' => $url_prefix . '/index.php?r=o2o/myCommision/info&order=' . (string)$order . '&user=' . $technician . '&type=0',
  359. ],
  360. ],
  361. ],
  362. ];
  363. $wechat->sendMessage($wechat_data);
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }