WorkerOrderForm.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. <?php
  2. namespace app\modules\client\models\v1;
  3. use app\constants\OptionSetting;
  4. use app\models\District;
  5. use app\models\Favorite;
  6. use app\models\Goods;
  7. use app\models\GoodsPic;
  8. use app\models\Option;
  9. use app\models\Order;
  10. use app\models\OrderComment;
  11. use app\models\OrderDetail;
  12. use app\models\Worker;
  13. use app\models\WorkerCat;
  14. use app\models\WorkerGoods;
  15. use app\models\WorkerGoodsCat;
  16. use app\models\WorkerGoodsExt;
  17. use app\models\WorkerLevel;
  18. use app\models\WorkerOrderExt;
  19. use app\models\WorkerPic;
  20. use app\models\WorkerSetting;
  21. use yii\base\Model;
  22. use yii\helpers\Json;
  23. use app\utils\Notice\NoticeSend;
  24. class WorkerOrderForm extends Model
  25. {
  26. public $latitude;
  27. public $longitude;
  28. public $store_id;
  29. public $status;
  30. public $_from;
  31. public $goods_id;
  32. public $goods_ids;
  33. public $id;
  34. public function rules()
  35. {
  36. return [
  37. [['latitude', 'longitude', '_from', 'goods_ids'], 'string'],
  38. [['store_id', 'id', 'status', 'goods_id'], 'integer']
  39. ];
  40. }
  41. public function workerOrderList() {
  42. try {
  43. $store_id = $this->store_id;
  44. $user_id = get_user_id();
  45. $status = $this->status;
  46. $query = Order::find()->alias('o')->where(['o.store_id' => $store_id, 'o.order_type' => 6, 'o.user_id' => $user_id])
  47. ->leftJoin(['woe' => WorkerOrderExt::tableName()], 'o.id = woe.order_id')
  48. ->leftJoin(['oc' => OrderComment::tableName()], 'o.id = oc.order_id');
  49. if (!is_null($status) && $status > -1) {
  50. switch ($status) {
  51. case 0: //未开始
  52. $query->andWhere(['woe.status_ext' =>
  53. [
  54. WorkerOrderExt::STATUS_EXT_WAIT_BIND,
  55. WorkerOrderExt::STATUS_EXT_WAIT_BIND_OK
  56. ]
  57. ]);
  58. break;
  59. case 1: //进行中
  60. $query->andWhere(['woe.status_ext' => [
  61. WorkerOrderExt::STATUS_EXT_START,
  62. WorkerOrderExt::STATUS_EXT_HAS_BIND,
  63. WorkerOrderExt::STATUS_EXT_ON_ROAD,
  64. WorkerOrderExt::STATUS_EXT_ARRIVE
  65. ]]);
  66. break;
  67. case 2: //待评价
  68. $query->andWhere(['AND', ['woe.status_ext' => WorkerOrderExt::STATUS_EXT_FINISH], ['IS', 'oc.id', null]]);
  69. break;
  70. case 4: //待支付
  71. $query->andWhere(['woe.status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_PAY]);
  72. break;
  73. default: //已取消
  74. $query->andWhere(['OR', ['woe.status_ext' => WorkerOrderExt::STATUS_EXT_CANCEL], ['AND', ['woe.status_ext' => WorkerOrderExt::STATUS_EXT_WAIT_BIND], ['<', 'o.created_at', (time() - 900)]]]);
  75. break;
  76. }
  77. }
  78. $query->select('o.id, woe.worker_id, woe.status_ext, o.book_info, o.order_type_ext, o.pay_price, o.created_at, o.is_pay, o.apply_delete');
  79. $query->orderBy('o.created_at desc');//woe.status_ext ASC,
  80. $list = pagination_make($query);
  81. foreach ($list['list'] as &$item) {
  82. $order = Order::findOne($item['id']);
  83. if (time() - $order->created_at > 15 * 60 && intval($order->trade_status) != Order::ORDER_FLOW_CANCEL && intval($order->is_pay) === 0) {
  84. $order->trade_status = Order::ORDER_FLOW_CANCEL;
  85. $order->updated_at = time();
  86. $order->save();
  87. }
  88. $item['goods_list'] = OrderDetail::find()->alias('od')->where(['od.order_id' => $item['id']])
  89. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
  90. ->leftJoin(['wge' => WorkerGoodsExt::tableName()], 'wge.goods_id = g.id')
  91. ->select('od.goods_id, od.goods_name, od.num, od.pic goods_pic, od.total_price, od.pre_price, wge.payment_type')->asArray()->all();
  92. $num = array_sum(array_column($item['goods_list'], 'num'));
  93. $price = $item['pay_price'] / $num;
  94. foreach ($item['goods_list'] as &$goods) {
  95. $goods['is_comment'] = intval(OrderComment::findOne(['order_id' => $item['id'], 'goods_id' => $goods['goods_id']]));
  96. $goods['payment_type'] = intval($goods['payment_type']);
  97. $goods['book_info'] = json_decode($item['book_info'], true);
  98. $goods['price'] = sprintf('%.2f', $price * $goods['num']);
  99. //$goods['price'] = $goods['payment_type'] && intval($item['order_type_ext']) === 601 ? $goods['pre_price'] : $goods['total_price'];
  100. }
  101. $worker = Worker::findOne($item['worker_id']);
  102. $item['worker'] = $worker ? [
  103. 'id' => $worker->id,
  104. 'name' => $worker->name,
  105. 'level_name' => WorkerLevel::findOne(['store_id' => $store_id, 'level' => $worker->level])->name ?? '服务人员',
  106. 'logo' => $worker->logo
  107. ] : null;
  108. $item['book_info'] = json_decode($item['book_info'], true);
  109. $item['status'] = -1;
  110. $item['is_pay'] = (int)$item['is_pay'];
  111. $item['status_ext'] = (int)$item['status_ext'];
  112. if (in_array($item['status_ext'], [10, 15, 20, 30, 40])) {
  113. $item['status'] = 0;
  114. }
  115. if ($item['status_ext'] === 50) {
  116. $item['status'] = 1;
  117. }
  118. if ($item['status_ext'] === 100) {
  119. $item['status'] = 3;
  120. $comment_ = OrderComment::find()->where(['order_id' => $item['id']])->select('id')->column();
  121. if (count($comment_) !== count($item['goods_list'])) {
  122. $item['status'] = 2;
  123. }
  124. }
  125. if ($item['status_ext'] === 70 || $item['status_ext'] === 5 && ($item['created_at'] + 900) < time()) {
  126. $item['status'] = 4;
  127. }
  128. $item['pay_type_list'] = $this->getPayType();
  129. $item['apply_delete'] = intval($item['apply_delete']);
  130. }
  131. return [
  132. 'code' => 0,
  133. 'msg' => 'success',
  134. 'data' => [
  135. 'list' => $list['list'],
  136. 'pageNo' => $list['pageNo'],
  137. 'totalCount' => $list['totalCount']
  138. ],
  139. ];
  140. } catch (\Exception $e) {
  141. return [
  142. 'code' => 1,
  143. 'msg' => $e->getMessage()
  144. ];
  145. }
  146. }
  147. public function workerOrderCancelReason() {
  148. return [
  149. 'code' => 0,
  150. 'msg' => 'success',
  151. 'data' => [
  152. [
  153. 'status' => 0,
  154. 'msg' => '不想要了'
  155. ],
  156. [
  157. 'status' => 1,
  158. 'msg' => '商品错选/多选'
  159. ],
  160. [
  161. 'status' => 2,
  162. 'msg' => '商品无货'
  163. ],
  164. [
  165. 'status' => 3,
  166. 'msg' => '地址信息填写错误'
  167. ],
  168. [
  169. 'status' => 4,
  170. 'msg' => '商品降价'
  171. ],
  172. [
  173. 'status' => 5,
  174. 'msg' => '价格高于其他平台'
  175. ],
  176. [
  177. 'status' => 6,
  178. 'msg' => '没用/少用/错用优惠'
  179. ],
  180. ]
  181. ];
  182. }
  183. public function workerOrderInfo() {
  184. try {
  185. $id = $this->id;
  186. $store_id = $this->store_id;
  187. $user_id = get_user_id();
  188. if (!$id) {
  189. throw new \Exception('参数错误');
  190. }
  191. $info = Order::find()->alias('o')->where(['o.id' => $id, 'o.user_id' => $user_id])
  192. ->leftJoin(['woe' => WorkerOrderExt::tableName()], 'o.id = woe.order_id')
  193. ->select('o.id, o.order_no, o.pay_type, o.book_info, o.express_price, o.total_price, o.pay_price,
  194. o.created_at, woe.status_ext, woe.worker_id, o.name, o.mobile, o.address, woe.time_start_service, woe.user_revoke_reason')->asArray()->one();
  195. $info['status'] = -1;
  196. $info['status_ext'] = (int)$info['status_ext'];
  197. if (in_array($info['status_ext'], [10, 15, 20, 30, 40])) {
  198. $info['status'] = 0;
  199. }
  200. if ($info['status_ext'] === 50) {
  201. $info['status'] = 1;
  202. }
  203. if ($info['status_ext'] === 70 || $info['status'] === -1 && ($info['created_at'] + 900) < time()) {
  204. $info['status'] = 4;
  205. }
  206. $info['pay_type'] = intval($info['pay_type']);
  207. $info['created_at_str'] = date("Y-m-d H:i:s", $info['created_at']);
  208. $info['time_start_service'] = $info['time_start_service'] ? date("Y-m-d H:i:s", $info['time_start_service']) : '';
  209. $info['time_end_service'] = $info['time_end_service'] ? date("Y-m-d H:i:s", $info['time_end_service']) : '';
  210. $info['goods_list'] = OrderDetail::find()->alias('od')->where(['od.order_id' => $info['id']])
  211. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
  212. ->leftJoin(['wge' => WorkerGoodsExt::tableName()], 'wge.goods_id = g.id')
  213. ->select('g.id goods_id, od.pic goods_pic, od.goods_name, od.total_price, od.num, od.pre_price, wge.payment_type')->asArray()->all();
  214. if ($info['status_ext'] === 100) {
  215. $info['status'] = 3;//OrderComment::findOne(['order_id' => $info['id']]) ? 3 : 2;
  216. $comment_ = OrderComment::find()->where(['order_id' => $info['id']])->select('id')->column();
  217. if (count($comment_) !== count($info['goods_list'])) {
  218. $info['status'] = 2;
  219. }
  220. }
  221. $info['goods_price'] = 0;
  222. $info['progress'] = $this->workerOrderProgress()['data']['progress'];
  223. if ($info['goods_list']) {
  224. foreach ($info['goods_list'] as &$item) {
  225. if (intval($item['payment_type']) === 1) {
  226. $item['price'] = $item['pre_price'];
  227. } else {
  228. $item['price'] = $item['total_price'];
  229. }
  230. $info['goods_price'] += $item['price'];
  231. $item['is_comment'] = intval($info['status'] === 2 && !OrderComment::findOne(['order_id' => $info['id'], 'goods_id' => $item['goods_id']])) ? 0 : 1;
  232. $item['book_info'] = json_decode($info['book_info'], true);
  233. }
  234. }
  235. $info['pay_type_list'] = $this->getPayType();
  236. $info['goods_price'] = sprintf('%.2f', $info['goods_price']);
  237. $worker = Worker::findOne($info['worker_id']);
  238. $info['worker'] = $worker ? [
  239. 'id' => $worker->id,
  240. 'name' => $worker->name,
  241. 'level_name' => WorkerLevel::findOne(['store_id' => $store_id, 'level' => $worker->level])->name ?? '服务人员',
  242. 'logo' => $worker->logo
  243. ] : null;
  244. return [
  245. 'code' => 0,
  246. 'msg' => '获取成功',
  247. 'data' => $info
  248. ];
  249. } catch (\Exception $e) {
  250. return [
  251. 'code' => 1,
  252. 'msg' => $e->getMessage()
  253. ];
  254. }
  255. }
  256. //服务人员进度
  257. public function workerOrderProgress() {
  258. try {
  259. $id = $this->id;
  260. $store_id = $this->store_id;
  261. $order_ext = Order::find()->alias('o')->leftJoin(['woe' => WorkerOrderExt::tableName()], 'o.id = woe.order_id')
  262. ->where(['order_id' => $id])->select('woe.status_ext, woe.time_outgoing, woe.time_has_bind,
  263. woe.time_reach, woe.time_start_service, woe.time_end_service, woe.time_user_revoke, o.created_at, woe.worker_id, woe.user_revoke, o.name, o.mobile, o.address')->asArray()->one();
  264. $progress = [];
  265. $t_date = date("Ymd");
  266. $order_ext['status_ext'] = (int)$order_ext['status_ext'];
  267. if ($order_ext['status_ext'] > 5) {
  268. $date = date('Ymd', $order_ext['created_at']);
  269. $text = date('m.d', $order_ext['created_at']);
  270. if (($t_date-$date) == 0) {
  271. $text = "今天 ";
  272. }
  273. if (($t_date-$date) == 1) {
  274. $text = "昨天 ";
  275. }
  276. $progress = [
  277. [
  278. 'status' => 0,
  279. 'time' => $text . date('H:i', $order_ext['created_at']),
  280. 'msg' => '商品已经下单'
  281. ]
  282. ];
  283. }
  284. //已接单
  285. if ($order_ext['status_ext'] >= 20) {
  286. if (($order_ext['status_ext'] === 70 && ($order_ext['time_has_bind'] < $order_ext['time_user_revoke'])) || ($order_ext['status_ext'] !== 70)) {
  287. $date = date('Ymd', $order_ext['time_has_bind']);
  288. $text = date('m.d', $order_ext['time_has_bind']);
  289. if (($t_date-$date) == 0) {
  290. $text = "今天 ";
  291. }
  292. if (($t_date-$date) == 1) {
  293. $text = "昨天 ";
  294. }
  295. $worker = Worker::findOne($order_ext['worker_id']);
  296. $name = $worker->name ?? '-';
  297. $tel = $worker->tel ?? '-';
  298. $progress = array_merge([
  299. [
  300. 'status' => 1,
  301. 'time' => $order_ext['time_has_bind'] > 0 ? $text . date('H:i', $order_ext['time_has_bind']) : '',
  302. 'msg' => '您的订单已由【' . $name . '】接单。等待出发 联系人姓名:' . $name. ',电话' . $tel
  303. ]
  304. ], $progress);
  305. }
  306. }
  307. //已出发
  308. if ($order_ext['status_ext'] >= 30) {
  309. if (($order_ext['status_ext'] === 70 && ($order_ext['time_outgoing'] < $order_ext['time_user_revoke'])) || ($order_ext['status_ext'] !== 70)) {
  310. $date = date('Ymd', $order_ext['time_outgoing']);
  311. $text = date('m.d', $order_ext['time_outgoing']);
  312. if (($t_date-$date) == 0) {
  313. $text = "今天 ";
  314. }
  315. if (($t_date-$date) == 1) {
  316. $text = "昨天 ";
  317. }
  318. $progress = array_merge([
  319. [
  320. 'status' => 2,
  321. 'time' => $order_ext['time_outgoing'] > 0 ? $text . date('H:i', $order_ext['time_outgoing']) : '',
  322. 'msg' => '服务人员已出发'
  323. ]
  324. ], $progress);
  325. }
  326. }
  327. // 已到达
  328. if ($order_ext['status_ext'] >= 40) {
  329. if (($order_ext['status_ext'] === 70 && ($order_ext['time_reach'] < $order_ext['time_user_revoke'])) || ($order_ext['status_ext'] !== 70)) {
  330. $date = date('Ymd', $order_ext['time_reach']);
  331. $text = date('m.d', $order_ext['time_reach']);
  332. if (($t_date-$date) == 0) {
  333. $text = "今天 ";
  334. }
  335. if (($t_date-$date) == 1) {
  336. $text = "昨天 ";
  337. }
  338. $progress = array_merge([
  339. [
  340. 'status' => 3,
  341. 'time' => $order_ext['time_reach'] > 0 ? $text . date('H:i', $order_ext['time_reach']) : '',
  342. 'msg' => '服务人员已到达'
  343. ]
  344. ], $progress);
  345. }
  346. }
  347. // 服务中
  348. if ($order_ext['status_ext'] >= 50) { //
  349. //已取消
  350. if (($order_ext['status_ext'] === 70 && ($order_ext['time_start_service'] < $order_ext['time_user_revoke'])) || ($order_ext['status_ext'] !== 70)) {
  351. $date = date('Ymd', $order_ext['time_start_service']);
  352. $text = date('m.d', $order_ext['time_start_service']);
  353. if (($t_date-$date) == 0) {
  354. $text = "今天 ";
  355. }
  356. if (($t_date-$date) == 1) {
  357. $text = "昨天 ";
  358. }
  359. $progress = array_merge([
  360. [
  361. 'status' => 4,
  362. 'time' => $order_ext['time_start_service'] > 0 ? $text . date('H:i', $order_ext['time_start_service']) : '',
  363. 'msg' => '服务人员已开始服务'
  364. ]
  365. ], $progress);
  366. }
  367. }
  368. // 已完成
  369. if ($order_ext['status_ext'] == 100) {
  370. $date = date('Ymd', $order_ext['time_end_service']);
  371. $text = date('m.d', $order_ext['time_end_service']);
  372. if (($t_date-$date) == 0) {
  373. $text = "今天 ";
  374. }
  375. if (($t_date-$date) == 1) {
  376. $text = "昨天 ";
  377. }
  378. $progress = array_merge([
  379. [
  380. 'status' => 5,
  381. 'time' => $order_ext['time_end_service'] > 0 ? $text . date('H:i', $order_ext['time_end_service']) : '',
  382. 'msg' => '服务人员已完成服务'
  383. ]
  384. ], $progress);
  385. }
  386. // 已取消
  387. if ($order_ext['status_ext'] == 70 || $order_ext['user_revoke']) {
  388. $date = date('Ymd', $order_ext['time_user_revoke']);
  389. $text = date('m.d', $order_ext['time_user_revoke']);
  390. if (($t_date-$date) == 0) {
  391. $text = "今天 ";
  392. }
  393. if (($t_date-$date) == 1) {
  394. $text = "昨天 ";
  395. }
  396. $progress = array_merge([
  397. [
  398. 'status' => 6,
  399. 'time' => $order_ext['time_end_service'] > 0 ? $text . date('H:i', $order_ext['time_end_service']) : '',
  400. 'msg' => '订单已取消'
  401. ]
  402. ], $progress);
  403. }
  404. $worker = Worker::findOne($order_ext['worker_id']);
  405. return [
  406. 'code' => 0,
  407. 'msg' => 'success',
  408. 'data' => [
  409. 'progress' => $progress,
  410. 'worker' => [
  411. 'id' => $worker->id ?? '',
  412. 'name' => $worker->name ?? '',
  413. 'level_name' => WorkerLevel::findOne(['store_id' => $store_id, 'level' => $worker->level])->name ?? '服务人员',
  414. 'logo' => $worker->logo ?? '',
  415. 'tel' => $worker->tel ?? ''
  416. ],
  417. 'address' => [
  418. 'name' => $order_ext['name'],
  419. 'mobile' => $order_ext['mobile'],
  420. 'address' => $order_ext['address'],
  421. ]
  422. ]
  423. ];
  424. } catch (\Exception $e) {
  425. return [
  426. 'code' => 1,
  427. 'msg' => $e->getMessage()
  428. ];
  429. }
  430. }
  431. //未支付订单取消 / 20250326增加已支付订单退款功能 已支付未接单状态
  432. public function workerOrderCancel() {
  433. try {
  434. $id = $this->id;
  435. $status = $this->status;
  436. $order = Order::find()->alias('o')->leftJoin(['woe' => WorkerOrderExt::tableName()], 'woe.order_id = o.id')
  437. ->where(['o.id' => $id])->select('o.id, o.created_at, woe.status_ext, o.trade_status, o.apply_delete')->asArray()->one();
  438. $order['status_ext'] = (int)$order['status_ext'];
  439. if (intval($order['trade_status']) === Order::ORDER_FLOW_CANCEL) {
  440. throw new \Exception('订单已经取消');
  441. }
  442. if (intval($order['apply_delete']) === Order::ORDER_APPLY_DELETE) {
  443. throw new \Exception('订单取消申请中');
  444. }
  445. if (($order['status_ext'] > 10 && $order['status_ext'] !== 70)) {
  446. throw new \Exception('当前订单状态不可取消');
  447. }
  448. $result = $this->workerOrderCancelReason();
  449. $data = $result['data'];
  450. $status_arr = array_column($data, 'status');
  451. if (!in_array($status, $status_arr)) {
  452. throw new \Exception('状态错误');
  453. }
  454. $order_ext = WorkerOrderExt::findOne(['order_id' => $id]);
  455. $order_ext->user_revoke_reason = $status;
  456. if (!$order_ext->save()) {
  457. throw new \Exception('保存失败');
  458. }
  459. $order_ = Order::findOne($id);
  460. if ($order['status_ext'] === 5) {
  461. $order_->trade_status = Order::ORDER_FLOW_CANCEL;
  462. $msg = '取消成功';
  463. } else {
  464. $order_->apply_delete = Order::ORDER_APPLY_DELETE;
  465. $msg = '申请成功';
  466. }
  467. if (!$order_->save()) {
  468. throw new \Exception('保存失败');
  469. }
  470. return [
  471. 'code' => 0,
  472. 'msg' => $msg
  473. ];
  474. } catch (\Exception $e) {
  475. return [
  476. 'code' => 1,
  477. 'msg' => $e->getMessage()
  478. ];
  479. }
  480. }
  481. //评价页面
  482. public function getOrderGoodsInfo() {
  483. try {
  484. $id = $this->id;
  485. $goods_id = $this->goods_ids;
  486. $store_id = $this->store_id;
  487. if (!$goods_id) {
  488. throw new \Exception('缺失必要参数');
  489. }
  490. $goods_id = explode(',', $goods_id);
  491. $goods_id = array_merge($goods_id, [0]);
  492. $comment_order_detail_id = OrderComment::find()->where(['order_id' => $id])->select('order_detail_id')->column();
  493. $list = OrderDetail::find()->alias('od')->where(['od.order_id' => $id, 'od.goods_id' => $goods_id])
  494. ->andWhere(['NOT IN', 'od.id', $comment_order_detail_id])
  495. ->leftJoin(['o' => Order::tableName()], 'od.order_id = o.id')
  496. ->leftJoin(['woe' => WorkerOrderExt::tableName()], 'woe.order_id = od.order_id')
  497. ->leftJoin(['wge' => WorkerGoodsExt::tableName()], 'od.goods_id = wge.goods_id')
  498. ->select('od.id, od.goods_id, woe.worker_id, od.goods_name, od.pic goods_pic, od.num, od.total_price, o.book_info, woe.status_ext, wge.payment_type, od.pre_price')
  499. ->asArray()->all();
  500. $setting = WorkerSetting::getByStoreId($store_id);
  501. $tag = [];
  502. if($setting){
  503. $tag = $setting->tag ? explode(',', $setting->tag) : [];
  504. }
  505. foreach ($list as &$info) {
  506. if (empty($info)) {
  507. throw new \Exception('订单信息查询失败');
  508. }
  509. $info['tag'] = $tag;
  510. $info['status'] = -1;
  511. $info['status_ext'] = (int)$info['status_ext'];
  512. if (in_array($info['status_ext'], [10, 15, 20, 30, 40])) {
  513. $info['status'] = 0;
  514. }
  515. if ($info['status_ext'] === 50) {
  516. $info['status'] = 1;
  517. }
  518. $info['book_info'] = json_decode($info['book_info'], true);
  519. if ($info['status_ext'] === 100) {
  520. $info['status'] = OrderComment::findOne(['order_id' => $info['id']]) ? 3 : 2;
  521. }
  522. if ($info['status_ext'] === 70) {
  523. $info['status'] = 4;
  524. }
  525. if (intval($info['payment_type']) === 1) {
  526. $info['price'] = $info['pre_price'];
  527. } else {
  528. $info['price'] = $info['total_price'];
  529. }
  530. $info['goods_price'] += $info['price'];
  531. $worker = Worker::findOne($info['worker_id']);
  532. $info['worker'] = $worker ? [
  533. 'id' => $worker->id,
  534. 'name' => $worker->name,
  535. 'level_name' => WorkerLevel::findOne(['store_id' => $store_id, 'level' => $worker->level])->name ?? '服务人员',
  536. 'logo' => $worker->logo
  537. ] : null;
  538. }
  539. return [
  540. 'code' => 0,
  541. 'msg' => '获取成功',
  542. 'data' => $list
  543. ];
  544. } catch (\Exception $e) {
  545. return [
  546. 'code' => 1,
  547. 'msg' => $e->getMessage()
  548. ];
  549. }
  550. }
  551. public function getPayType() {
  552. $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', '{"wechat":{"value":1}}')['value']);
  553. $pay_type_list = Json::decode($pay_type_list_json['value']);
  554. $new_list = [];
  555. $ok = true;
  556. $is_virtual = false;
  557. $is_integral = false; //积分兑换商品
  558. if (is_wechat_platform() || $this->_from == 'app') {
  559. $new_list[] = [
  560. 'name' => '微信支付',
  561. 'payment' => 1,
  562. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
  563. ];
  564. }
  565. if (is_alipay_platform() || $this->_from == 'app') {
  566. $new_list[] = [
  567. 'name' => '支付宝支付',
  568. 'payment' => 4,
  569. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png'
  570. ];
  571. }
  572. if (is_toutiao_platform()) {
  573. $new_list[] = [
  574. 'name' => '线上支付',
  575. 'payment' => 5,
  576. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
  577. ];
  578. }
  579. //积分兑换商品取消这两种支付方式
  580. if(!$is_integral){
  581. foreach ($pay_type_list as $index => $value) {
  582. if ($index == 'huodao' && $value['value'] == 1 && $ok && !$is_virtual) {
  583. $new_list[] = [
  584. 'name' => '货到付款',
  585. 'payment' => 2,
  586. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-huodao.png'
  587. ];
  588. }
  589. if ($index == 'friend' && $value['value'] == 1) {
  590. $new_list[] = [
  591. 'name' => '朋友代付',
  592. 'payment' => 7,
  593. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-huodao.png'
  594. ];
  595. }
  596. }
  597. }
  598. foreach ($pay_type_list as $index => $value) {
  599. if ((is_app_platform() || is_wechat_platform()) && $index == Order::PAY_TYPE_KEY_ADAPAY_WX && $value['value'] == 1) {
  600. $new_list[] = [
  601. 'name' => Order::PAY_TYPE_NAME_ADAPAY_WX,
  602. 'payment' => Order::PAY_TYPE_ADAPAY_WX,
  603. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
  604. ];
  605. }
  606. if ((is_app_platform() || is_alipay_platform()) && $index == Order::PAY_TYPE_KEY_ADAPAY_ALIPAY && $value['value'] == 1) {
  607. $new_list[] = [
  608. 'name' => Order::PAY_TYPE_NAME_ADAPAY_ALIPAY,
  609. 'payment' => Order::PAY_TYPE_ADAPAY_ALIPAY,
  610. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png'
  611. ];
  612. }
  613. if ($index == Order::PAY_TYPE_KEY_ADAPAY_QUICKPAY_FRONTPAY && $value['value'] == 1) {
  614. $new_list[] = [
  615. 'name' => Order::PAY_TYPE_NAME_ADAPAY_QUICKPAY_FRONTPAY,
  616. 'payment' => Order::PAY_TYPE_ADAPAY_QUICKPAY_FRONTPAY,
  617. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png'
  618. ];
  619. }
  620. if ((is_wechat_platform()) && $index == Order::PAY_TYPE_KEY_HUIFU_V2_JSPAY_WX && $value['value'] == 1) {
  621. $new_list[] = [
  622. 'name' => Order::PAY_TYPE_NAME_HUIFU_V2_JSPAY_WX,
  623. 'payment' => Order::PAY_TYPE_HUIFU_V2_JSPAY_WX,
  624. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png'
  625. ];
  626. }
  627. }
  628. return $new_list;
  629. }
  630. public function distance($distance)
  631. {
  632. if ($distance == -1) {
  633. return -1;
  634. }
  635. if ($distance > 1000) {
  636. $distance = round($distance / 1000, 2) . 'km';
  637. } else {
  638. $distance = round($distance, 2);
  639. $distance .= 'm';
  640. }
  641. return $distance;
  642. }
  643. public static function orderFinishPay($worker_id = 0, $order_id = 0, $fee = 0) {
  644. $t = \Yii::$app->db->beginTransaction();
  645. try{
  646. $typeExt = Order::ORDER_TYPE_EXT_WORKER_LAST_PAY;
  647. $has = Order::findOne(['base_order_id' => $order_id, 'order_type_ext' => $typeExt, 'is_delete' => 0]);
  648. if($has){
  649. throw new \Exception('尾款订单已存在');
  650. }
  651. $order = Order::findOne($order_id);
  652. $orderExt = WorkerOrderExt::findOne(['order_id' => $order_id]);
  653. if($orderExt['worker_id'] != $worker_id){
  654. throw new \Exception('服务人员错误');
  655. }
  656. $attrCopy = [
  657. 'store_id', 'user_id', 'saas_id', 'name', 'mobile', 'address', 'address_data', 'version', 'order_type', 'book_info',
  658. 'province_id', 'city_id', 'district_id',
  659. ];
  660. $orderNew = new Order();
  661. foreach($attrCopy as $attr){
  662. $orderNew->setAttribute($attr, $order[$attr]);
  663. }
  664. $orderNew->base_order_id = $order_id;
  665. $orderNew->order_no = $order['order_no'] . 'E' . $typeExt;
  666. $orderNew->first_price = 0;
  667. $orderNew->second_price = 0;
  668. $orderNew->third_price = 0;
  669. $orderNew->pay_price = $fee;
  670. $orderNew->total_price = $fee;
  671. $orderNew->order_type = Order::ORDER_TYPE_WORKER;
  672. $orderNew->order_type_ext = $typeExt;
  673. $orderNew->created_at = time();
  674. $save = $orderNew->save();
  675. if(!$save){
  676. throw new \Exception('保存订单失败' . array_shift($orderNew->getFirstErrors()));
  677. }
  678. $attrOdCopy = [
  679. 'goods_id', 'goods_name', 'num', 'total_price', 'attr', 'pic', 'goods_info',
  680. ];
  681. $goods_name = '';
  682. $od = $order->detail;
  683. foreach ($od as $d) {
  684. $goods_name = $d['goods_name'];
  685. $odNew = new OrderDetail();
  686. foreach($attrOdCopy as $attr){
  687. $odNew->setAttribute($attr, $d[$attr]);
  688. }
  689. $odNew->order_id = $orderNew->id;
  690. $save = $odNew->save();
  691. if(!$save){
  692. throw new \Exception('保存订单商品失败' . array_shift($odNew->getFirstErrors()));
  693. }
  694. }
  695. $attrExtCopy = [
  696. 'worker_id', 'lat', 'lng'
  697. ];
  698. $orderExtNew = WorkerOrderExt::findOne(['order_id' => $orderNew->id]);
  699. foreach($attrExtCopy as $attr){
  700. $orderExtNew->setAttribute($attr, $orderExt[$attr]);
  701. }
  702. $save = $orderExtNew->save();
  703. if(!$save){
  704. throw new \Exception('保存订单扩展信息失败' . array_shift($orderExtNew->getFirstErrors()));
  705. }
  706. $t->commit();
  707. // 订单提交完成发送消息
  708. NoticeSend::OrderSubmit($orderNew->user_id, $orderNew->mobile, $orderNew->order_no, $orderNew->pay_price, $goods_name, $orderNew->order_type, $orderNew->store_id);
  709. return [
  710. 'code' => 0,
  711. 'msg' => 'ok',
  712. ];
  713. } catch (\Exception $ex) {
  714. $t->rollback();
  715. return [
  716. 'code' => 1,
  717. 'msg' => '创建尾款订单失败,' . $ex->getMessage(),
  718. ];
  719. }
  720. }
  721. public static function orderAddTime($worker_id = 0, $order_id = 0, $fee = 0, $goodsCount = 0) {
  722. $t = \Yii::$app->db->beginTransaction();
  723. try{
  724. $typeExt = Order::ORDER_TYPE_EXT_WORKER_ADD_TIME;
  725. $has = Order::findOne(['AND', ['base_order_id' => $order_id, 'order_type_ext' => $typeExt, 'is_delete' => 0], ['<>', 'trade_status', 1]]);
  726. if($has->id){
  727. throw new \Exception('加钟订单已存在');
  728. }
  729. $order = Order::findOne($order_id);
  730. $orderExt = WorkerOrderExt::findOne(['order_id' => $order_id]);
  731. if($orderExt['worker_id'] != $worker_id){
  732. throw new \Exception('服务人员错误');
  733. }
  734. if($goodsCount){
  735. $od = $order->detail;
  736. foreach ($od as $d) {
  737. $fee += $d['total_price'] / $d['num'] * $goodsCount;
  738. }
  739. }
  740. $attrCopy = [
  741. 'store_id', 'user_id', 'saas_id', 'name', 'mobile', 'address', 'address_data', 'version', 'order_type', 'book_info',
  742. 'province_id', 'city_id', 'district_id',
  743. ];
  744. $orderNew = new Order();
  745. foreach($attrCopy as $attr){
  746. $orderNew->setAttribute($attr, $order[$attr]);
  747. }
  748. $orderNew->base_order_id = $order_id;
  749. $orderNew->order_no = $order['order_no'] . 'E' . $typeExt;
  750. $orderNew->first_price = 0;
  751. $orderNew->second_price = 0;
  752. $orderNew->third_price = 0;
  753. $orderNew->pay_price = $fee;
  754. $orderNew->total_price = $fee;
  755. $orderNew->order_type = Order::ORDER_TYPE_WORKER;
  756. $orderNew->order_type_ext = $typeExt;
  757. $orderNew->created_at = time();
  758. $save = $orderNew->save();
  759. if(!$save){
  760. throw new \Exception('保存订单失败' . array_shift($orderNew->getFirstErrors()));
  761. }
  762. $attrOdCopy = [
  763. 'goods_id', 'goods_name', 'num', 'total_price', 'attr', 'pic', 'goods_info',
  764. ];
  765. $goods_name = '';
  766. $od = $order->detail;
  767. foreach ($od as $d) {
  768. $goods_name = $d['goods_name'];
  769. $odNew = new OrderDetail();
  770. foreach($attrOdCopy as $attr){
  771. $odNew->setAttribute($attr, $d[$attr]);
  772. }
  773. $odNew->order_id = $orderNew->id;
  774. $save = $odNew->save();
  775. if(!$save){
  776. throw new \Exception('保存订单商品失败' . array_shift($odNew->getFirstErrors()));
  777. }
  778. }
  779. $attrExtCopy = [
  780. 'worker_id', 'lat', 'lng'
  781. ];
  782. $orderExtNew = WorkerOrderExt::findOne(['order_id' => $orderNew->id]);
  783. foreach($attrExtCopy as $attr){
  784. $orderExtNew->setAttribute($attr, $orderExt[$attr]);
  785. }
  786. $save = $orderExtNew->save();
  787. if(!$save){
  788. throw new \Exception('保存订单扩展信息失败' . array_shift($orderExtNew->getFirstErrors()));
  789. }
  790. $t->commit();
  791. // 订单提交完成发送消息
  792. NoticeSend::OrderSubmit($orderNew->user_id, $orderNew->mobile, $orderNew->order_no, $orderNew->pay_price, $goods_name, $orderNew->order_type, $orderNew->store_id);
  793. return [
  794. 'code' => 0,
  795. 'msg' => 'ok',
  796. ];
  797. } catch (\Exception $ex) {
  798. $t->rollback();
  799. return [
  800. 'code' => 1,
  801. 'msg' => '创建加钟订单失败,' . $ex->getMessage(),
  802. ];
  803. }
  804. }
  805. }