Bez popisu

OrderController.php 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Apiistrator
  5. * Date: 2018-02-22
  6. * Time: 10:32
  7. */
  8. namespace App\Http\Controllers\Api;
  9. use App\Http\Controllers\Controller;
  10. use Illuminate\Http\Request;
  11. use App\Log;
  12. use App\Admin;
  13. use App\Order;
  14. use App\Oplog;
  15. use App\RedisModel;
  16. use App\Goods;
  17. use App\GoodsSkus;
  18. use App\OrderGoodsSkus;
  19. use App\CustomerInfo;
  20. use App\CustomerConsum;
  21. use Illuminate\Support\Facades\DB;
  22. class OrderController extends Controller {
  23. /**
  24. * 订单列表
  25. */
  26. public function orderList(Request $request){
  27. $admin_id = (int)$request->input('admin_id');
  28. $self_role = DB::table('admin_role')->where('user_id', $admin_id)->pluck('role_name');
  29. $team_id = null;
  30. if($self_role == '管理员'){
  31. $team_id = Admin::where('id', $admin_id)->pluck('team_id');
  32. $admin_id = null;
  33. }
  34. $page = (int)$request->input('page');
  35. $pageSize = 20;
  36. if($page<=0){
  37. $page = 1;
  38. }
  39. $offset = ($page-1) * $pageSize;
  40. $count = Order::where('is_del', 0)->where('warehouse', 3)->where(function($query) use($admin_id, $team_id){
  41. if($admin_id>0) $query->where('admin_id', $admin_id);
  42. if($team_id>0) $query->where('team_id', $team_id);
  43. })->count();
  44. if ($count > 1) {
  45. // 总页数
  46. $pages = ceil($count/$pageSize);
  47. }else{
  48. // 总页数
  49. $pages = 1;
  50. }
  51. $result = Order::select('id', 'status', 'orderCode', 'customerName', 'receiverName', 'receiverMobile', 'receiverState','receiverCity','receiverDistrict','receiverAddress','receivedAmount','buyerMemo','sellerMemo','createTime','admin_id','admin_name','cost', 'logistics_id','is_refund','warehouse','delivery_date','refund_price','refund_type','send_note','freight_cost','payment_type','refuse_reason','refund_note','verify_time','aftersale_fee','aftersale_reason','aftersale_time','refund_time','send_time','should_amount', 'buyer_phone', 'use_coupon', 'use_m_gift', 'use_b_gift')->where('is_del', 0)->where('warehouse', 3)->where(function($query) use($admin_id, $team_id){
  52. if($admin_id>0) $query->where('admin_id', $admin_id);
  53. if($team_id>0) $query->where('team_id', $team_id)->where('status','>',0);
  54. })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  55. $result = json_decode(json_encode($result),true);
  56. $status_arr = array(
  57. 0 => '已录入',
  58. 1 => '待审核',
  59. 2 => '已审核',
  60. 3 => '已发货',
  61. 4 => '已退款'
  62. );
  63. foreach($result as $k=>&$v){
  64. $goods = DB::table('order_goods_skus')->where('order_id', $v['id'])->where('is_del', 0)->orderBy('gift_id', 'asc')->get();
  65. if(!empty($goods)){
  66. foreach($goods as &$item){
  67. $item->goods_name = Goods::where('id', $item->goods_id)->pluck('name');
  68. $item->picUrl = Goods::where('id', $item->goods_id)->pluck('picUrl');
  69. $goods_sku_info = GoodsSkus::select('propsName', 'is_weigh')->where('id', $item->sku_id)->first();
  70. if(!empty($goods_sku_info)){
  71. $item->props_name = $goods_sku_info->propsName;
  72. $item->is_weigh = $goods_sku_info->is_weigh;
  73. }else{
  74. $item->props_name = '';
  75. $item->is_weigh = 0;
  76. }
  77. $item->gift_type = null;
  78. if($item->gift_id>0){
  79. $gift_flag = DB::table('customer_month_gift')->where('id', $item->gift_id)->first();
  80. if($gift_flag->type==1){
  81. $item->gift_type = 1;
  82. }else{
  83. $item->gift_type = 0;
  84. }
  85. }
  86. }
  87. }
  88. $goods = json_decode(json_encode($goods), true);
  89. $v['sku_count'] = count($goods);
  90. $goods = array_slice($goods, 0, 2);
  91. $v['skus'] = $goods;
  92. #加粉时间
  93. $fanTime = DB::table('customers')->select('fanTime')->where('phone', $v['receiverMobile'])->first();
  94. $v['fanTime'] = isset($fanTime->fanTime) ? $fanTime->fanTime : '';
  95. //$v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
  96. $v['status_name'] = $status_arr[$v['status']];
  97. // 代金券
  98. if($v['use_coupon'] == 1){
  99. $v['coupon_id'] = DB::table('customer_coupons')->where('order_id', $v['id'])->where('is_use', 1)->pluck('id');
  100. }
  101. }
  102. $pageInfo = [
  103. 'page' => $page,
  104. 'total' => $count,
  105. 'pageSize' => $pageSize,
  106. 'pages' => $pages,
  107. ];
  108. return self::returnValue(['data'=>$result, 'page_info'=>$pageInfo]);
  109. }
  110. /**
  111. * 分组管理-进行添加操作
  112. * @param Request $request
  113. * @return \Illuminate\Http\RedirectResponse
  114. */
  115. public function orderstore(Request $request)
  116. {
  117. $admin_id = (int)$request->input('admin_id');
  118. if(empty($admin_id)){
  119. return self::returnValue(['msg'=>'登录过期'], 2000);
  120. }
  121. $buyer_phone = $request->input('buyer_phone');
  122. if(!preg_match('/^1\d{10}$/', $buyer_phone)){
  123. return self::returnValue(['msg'=>'买家手机号格式有误'], 2000);
  124. }
  125. $phone = $request->input('receiverMobile');
  126. if(!preg_match('/^1\d{10}$/', $phone)){
  127. return self::returnValue(['msg'=>'收货人手机号格式有误'], 2000);
  128. }
  129. $customerName = $request->input('customerName');
  130. if(empty($customerName)){
  131. return self::returnValue(['msg'=>'买家姓名不能为空'], 2000);
  132. }
  133. $receiverName = $request->input('receiverName');
  134. if(empty($receiverName)){
  135. return self::returnValue(['msg'=>'收货人姓名不能为空'], 2000);
  136. }
  137. $receiverState = $request->input('receiverState');
  138. if(empty($receiverState)){
  139. return self::returnValue(['msg'=>'省份不能为空'], 2000);
  140. }
  141. $receiverCity = $request->input('receiverCity');
  142. if(empty($receiverCity)){
  143. return self::returnValue(['msg'=>'市不能为空'], 2000);
  144. }
  145. $receiverAddress = $request->input('receiverAddress');
  146. if(empty($receiverAddress)){
  147. return self::returnValue(['msg'=>'详细地址不能为空'], 2000);
  148. }
  149. $delivery_date = $request->input('delivery_date');
  150. if(empty($delivery_date)){
  151. return self::returnValue(['msg'=>'预发货时间不能为空'], 2000);
  152. }
  153. $receivedAmount = $request->input('receivedAmount');
  154. if(empty($receivedAmount)){
  155. return self::returnValue(['msg'=>'实付金额不能为空'], 2000);
  156. }
  157. $skus = $request->input('skus');
  158. if(empty($skus)){
  159. return self::returnValue(['msg'=>'必须添加商品'], 2000);
  160. }
  161. $payment_type = (int)$request->input('payment_type');
  162. if(empty($payment_type)){
  163. return self::returnValue(['msg'=>'必选支付方式'], 2000);
  164. }
  165. /*
  166. $redisKey = 'seafood.order_phone_add.'.$phone;
  167. $res = RedisModel::setnx($redisKey, 1);
  168. if( $res == 0 ){
  169. $msg = '操作过于频繁,请一分钟后继续操作';
  170. return self::returnValue(['msg'=>$msg], 2000);
  171. }
  172. $res = RedisModel::expire( $redisKey, 60 );
  173. */
  174. //数据库-新增数据
  175. $order = array();
  176. $order['eshopCode'] = config('constants.ESHOP_CODE'); //默认
  177. $order['outerCode'] = Order::createOuterCode();//随机生成
  178. $order['status'] = (int)$request->input('status');
  179. $order['customerName'] = trim($request->input('customerName')); //买家名称
  180. $order['shippingType'] = '卖家包邮';
  181. $order['expressCompanyCode'] = '10084';
  182. $order['expressCompanyName'] = '顺丰快递';
  183. $order['receiverName'] = trim($request->input('receiverName')); //收件人
  184. $order['receiverMobile'] = trim($request->input('receiverMobile')); //收件人手机
  185. $order['receiverPhone'] = trim($request->input('receiverMobile')); //收件人手机
  186. $order['receiverState'] = trim($request->input('receiverState')); //省份
  187. $order['receiverCity'] = trim($request->input('receiverCity')); //市
  188. $order['receiverDistrict'] = trim($request->input('receiverDistrict')); //市
  189. #省市格式处理
  190. $order['receiverState'] = str_replace(['省', '市'], '', $order['receiverState']);
  191. $order['receiverCity'] = str_replace('市', '', $order['receiverCity']);
  192. $order['receiverDistrict'] = trim($request->input('receiverDistrict')); //区
  193. $order['receiverStreet'] = trim($request->input('receiverStreet')); //街道
  194. $order['receiverAddress'] = trim($request->input('receiverAddress')); //详细地址
  195. $order['buyerPostageFee'] = 0; //买家邮费
  196. $order['discountFee'] = 0; //订单优惠
  197. $order['adjustFee'] = 0; //订单调价
  198. $order['serviceFee'] = 0; //服务费
  199. $order['paymentMethod'] = '在线付款';
  200. $order['paymentType'] = '网银';
  201. $order['paymentAccount'] = '18322225555'; // 付款账号,暂时默认写死
  202. $order['paymentTime'] = date('Y-m-d H:i:s'); // 付款时间
  203. $order['receivedAmount'] = trim($request->input('receivedAmount')); //付款金额
  204. $order['buyerMemo'] = trim($request->input('buyerMemo', '')); // 买家备注
  205. $order['sellerMemo'] = trim($request->input('sellerMemo', '')); // 卖家备注
  206. $order['createTime'] = date('Y-m-d H:i:s'); // 订单创建时间
  207. $order['modifyTime'] = date('Y-m-d H:i:s'); // 订单修改时间
  208. $order['should_amount'] = $request->input('should_amount');
  209. if(!$order['should_amount']) $order['should_amount'] = null;
  210. //商品信息
  211. $skus = $request->input('skus');
  212. $order_goods = array();
  213. if(!empty($skus)){
  214. foreach($skus as $k=>$sku){
  215. $sku_id = $sku['sku_id'];
  216. $num = $sku['num'];
  217. $sku_info = GoodsSkus::where('id', $sku_id)->first();
  218. $goods_info = Goods::where('id', $sku_info->goodsCode)->first();
  219. $params = array();
  220. $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
  221. $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
  222. $params['goodsCode'] = $sku_info->goodsCode;// strstr($goods->code,'-',true);
  223. $params['skuCode'] = $sku_id;
  224. #商品名称
  225. $params['goodsName'] = $goods_info->name;
  226. $params['propertiesName'] = $sku_info->propsName;
  227. $params['price'] = $sku_info->price;
  228. $params['quantity'] = $num;
  229. $params['discountFee'] = 0;
  230. $params['adjustFee'] = 0;
  231. $params['refundStatus'] = '正常';
  232. $order_goods[] = $params;
  233. }
  234. }
  235. //本系统订单额外参数
  236. $order['warehouse'] = 3; //仓库
  237. $order['delivery_date'] = !empty($request->input('delivery_date')) ? $request->input('delivery_date') : date('Y-m-d'); //发货日期
  238. $order['payment_type'] = (int)$request->input('payment_type'); //支付方式
  239. $order['buyer_phone'] = $request->input('buyer_phone');
  240. $coupon_id = (int)$request->input('coupon_id');
  241. if($coupon_id>0){
  242. $order['use_coupon'] = 1;
  243. }
  244. $order['use_b_gift'] = (int)$request->input('use_b_gift');
  245. $m_gift_id = (int)$request->input('m_gift_id');
  246. if($m_gift_id>0){
  247. $order['use_m_gift'] = 1;
  248. }
  249. //充值卡验证余额
  250. if($order['payment_type'] == 4){
  251. $deposit_phone = $request->input('deposit_phone');
  252. $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
  253. $balance = isset($customerInfo->balance) ? $customerInfo->balance : 0;
  254. if($balance < $order['receivedAmount']){
  255. $msg = '余额不足';
  256. return self::returnValue(['msg'=>$msg], 2003);
  257. }
  258. }
  259. $admin_id = (int)$request->input('admin_id');
  260. DB::beginTransaction();
  261. try{
  262. #更新客户表
  263. $customer = array();
  264. $customers['name'] = $order['receiverName'];
  265. $customers['receiverState'] = $order['receiverState'];
  266. $customers['receiverCity'] = $order['receiverCity'];
  267. $customers['receiverDistrict'] = $order['receiverDistrict'];
  268. $customers['receiverStreet'] = $order['receiverStreet'];
  269. $customers['receiverAddress'] = $order['receiverAddress'];
  270. $customers['buyerMobile'] = $order['buyer_phone'];
  271. $customers['buyerName'] = $order['customerName'];
  272. $if_e = DB::table('customers')->select('id', 'fanTime')->where('phone', $order['receiverMobile'])->first();
  273. if(isset($if_e->id)){
  274. $customers['is_fugou'] = 1;
  275. $order['is_fugou'] = 1;
  276. $up_c = DB::table('customers')->where('id', $if_e->id)->update($customers);
  277. }else{
  278. $customers['phone'] = $order['receiverMobile'];
  279. $customers['is_fugou'] = 0;
  280. $customers['fanTime'] = $request->input('fanTime');
  281. $customers['admin_id'] = $admin_id;
  282. $in_c = DB::table('customers')->insert($customers);
  283. }
  284. $admin_info = DB::table('admin')->select('realname', 'team_id')->where('id', $admin_id)->first();
  285. $order['admin_id'] = $admin_id;
  286. $order['admin_name'] = $admin_info->realname;
  287. $order['team_id'] = $admin_info->team_id;
  288. //新加is_able 9.4之前粉丝不参与销售复购统计
  289. $fanTime = $request->input('fanTime');
  290. if(!$fanTime && isset($if_e->id)) $fanTime = $if_e->fanTime;
  291. if($fanTime < '2019-09-04'){
  292. $order['is_able'] = 0;
  293. }
  294. $res = DB::table('order')->insertGetId($order);
  295. if($res){
  296. if(!empty($skus)){
  297. //订单商品插入
  298. $order_skus = array();
  299. foreach($order_goods as $k=>$sku){
  300. $order_skus[$k]['sku_id'] = $sku['skuCode'];
  301. $order_skus[$k]['order_id'] = $res;
  302. $order_skus[$k]['goods_id'] = $sku['goodsCode'];
  303. $order_skus[$k]['num'] = $sku['quantity'];
  304. $order_skus[$k]['price'] = $sku['price'];
  305. }
  306. $order_skus_insert = DB::table('order_goods_skus')->insert($order_skus);
  307. }
  308. #加赠品逻辑
  309. $use_b_gift = (int)$request->input('use_b_gift');
  310. if($coupon_id > 0){
  311. $coupon = DB::table('customer_coupons')->where('phone', $order['buyer_phone'])->where('id', $coupon_id)->where('is_use',0)->first();
  312. if(!empty($coupon)){
  313. $cup = array();
  314. $cup['is_use'] = 1;
  315. $cup['use_time'] = date('Y-m-d H:i:s');
  316. $cup['order_id'] = $res;
  317. DB::table('customer_coupons')->where('id', $coupon->id)->update($cup);
  318. }
  319. }
  320. if($m_gift_id>0){
  321. $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
  322. $order_m_gift = array();
  323. $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
  324. $order_m_gift['sku_id'] = $m_gift->sku_id;
  325. $order_m_gift['order_id'] = $res;
  326. $order_m_gift['goods_id'] = $m_sku->goodsCode;
  327. $order_m_gift['num'] = $m_gift->num;
  328. $order_m_gift['price'] = $m_sku->price;
  329. $order_m_gift['gift_id'] = $m_gift_id;
  330. $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
  331. #记录领取
  332. $receive_gift = array();
  333. $receive_gift['phone'] = $order['buyer_phone'];
  334. $receive_gift['dtime'] = date('Y-m-d');
  335. $receive_gift['order_id'] = $res;
  336. $receive_gift['gift_id'] = $m_gift_id;
  337. $receive_gift['gift_type'] = 0;
  338. DB::table('customer_gift_receives')->insert($receive_gift);
  339. }
  340. $mdate = date('Y-m-01');
  341. if($use_b_gift>0){
  342. $b_gifts = DB::table('customer_month_gift')->where('mtime', $mdate)->where('type', 1)->get();
  343. $b_gifts = json_decode(json_encode($b_gifts),true);
  344. foreach($b_gifts as $b_gift){
  345. $order_b_gift = array();
  346. $b_sku = GoodsSkus::where('id', $b_gift['sku_id'])->first();
  347. $order_b_gift['sku_id'] = $b_gift['sku_id'];
  348. $order_b_gift['order_id'] = $res;
  349. $order_b_gift['goods_id'] = $b_sku->goodsCode;
  350. $order_b_gift['num'] = $b_gift['num'];
  351. $order_b_gift['price'] = $b_sku->price;
  352. $order_b_gift['gift_id'] = $b_gift['id'];
  353. $order_gift_insert = DB::table('order_goods_skus')->insert($order_b_gift);
  354. }
  355. #记录领取
  356. $receive_gift = array();
  357. $receive_gift['phone'] = $order['buyer_phone'];
  358. $receive_gift['dtime'] = date('Y-m-d');
  359. $receive_gift['order_id'] = $res;
  360. $receive_gift['gift_type'] = 1;
  361. DB::table('customer_gift_receives')->insert($receive_gift);
  362. }
  363. #新加逻辑,若付款方式为充值卡/消费记录
  364. if($order['payment_type'] == 4){
  365. $consum = new CustomerConsum();
  366. $consum->phone = $deposit_phone;
  367. $consum->order_id = $res;
  368. $consum->money = $order['receivedAmount'];
  369. $consum->save();
  370. //更新余额
  371. $customerInfo->balance = $customerInfo->balance - $order['receivedAmount'];
  372. $customerInfo->save();
  373. }
  374. #记录操作日志
  375. $self_id = $admin_id;
  376. $self_name = $admin_info->realname;
  377. if($order['status'] == 0){
  378. $status_text = '仅录入';
  379. }else{
  380. $status_text = '录入并提审';
  381. }
  382. $context = "录入订单:".$status_text;
  383. $type = 0; //订单录入
  384. $tables = 'order';
  385. $data_id = $res;
  386. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  387. }
  388. DB::commit();
  389. }catch (Exception $e){
  390. DB::rollback();
  391. }
  392. return self::returnValue(['msg'=>'添加成功']);
  393. }
  394. /**
  395. * 根据手机号获取默认地址信息
  396. *
  397. */
  398. public function getAddress(Request $request){
  399. $phone = $request->input('phone');
  400. if(!preg_match('/^1\d{10}$/', $phone)){
  401. return self::returnValue([],2000);
  402. }
  403. $result = DB::table('customers')->select('receiverState', 'receiverCity', 'receiverDistrict', 'receiverAddress', 'fanTime')->where('phone', $phone)->first();
  404. $result = json_decode(json_encode($result), true);
  405. return self::returnValue(['data'=>$result]);
  406. }
  407. //获余额
  408. public function getBalance(Request $request){
  409. $phone = $request->input('phone');
  410. if(!preg_match('/^1\d{10}$/', $phone)){
  411. return self::returnValue([],2000);
  412. }
  413. $result = CustomerInfo::where('phone', $phone)->pluck('balance');
  414. if(empty($result)) $result = 0;
  415. return self::returnValue(['balance'=>$result]);
  416. }
  417. /**
  418. * 分组管理-进行编辑操作
  419. * @param Request $request
  420. * @return \Illuminate\Http\RedirectResponse
  421. */
  422. public function orderupdate(Request $request)
  423. {
  424. $admin_id = (int)$request->input('admin_id');
  425. if(empty($admin_id)){
  426. return self::returnValue(['msg'=>'登录过期'], 2000);
  427. }
  428. $id = (int)$request->input('id');
  429. if(empty($id)){
  430. return self::returnValue(['msg'=>'订单id有误'], 2000);
  431. }
  432. $phone = $request->input('receiverMobile');
  433. if(!preg_match('/^1\d{10}$/', $phone)){
  434. return self::returnValue(['msg'=>'手机号格式有误'], 2000);
  435. }
  436. $customerName = $request->input('customerName');
  437. if(empty($customerName)){
  438. return self::returnValue(['msg'=>'买家姓名不能为空'], 2000);
  439. }
  440. $receiverName = $request->input('receiverName');
  441. if(empty($receiverName)){
  442. return self::returnValue(['msg'=>'收货人姓名不能为空'], 2000);
  443. }
  444. $receiverState = $request->input('receiverState');
  445. if(empty($receiverState)){
  446. return self::returnValue(['msg'=>'省份不能为空'], 2000);
  447. }
  448. $receiverCity = $request->input('receiverCity');
  449. if(empty($receiverCity)){
  450. return self::returnValue(['msg'=>'市不能为空'], 2000);
  451. }
  452. $receiverAddress = $request->input('receiverAddress');
  453. if(empty($receiverAddress)){
  454. return self::returnValue(['msg'=>'详细地址不能为空'], 2000);
  455. }
  456. $delivery_date = $request->input('delivery_date');
  457. if(empty($delivery_date)){
  458. return self::returnValue(['msg'=>'预发货时间不能为空'], 2000);
  459. }
  460. $receivedAmount = $request->input('receivedAmount');
  461. if(empty($receivedAmount)){
  462. return self::returnValue(['msg'=>'实付金额不能为空'], 2000);
  463. }
  464. $skus = $request->input('skus');
  465. if(empty($skus)){
  466. return self::returnValue(['msg'=>'必须添加商品'], 2000);
  467. }
  468. $payment_type = (int)$request->input('payment_type');
  469. if(empty($payment_type)){
  470. return self::returnValue(['msg'=>'必选支付方式'], 2000);
  471. }
  472. //更新数据
  473. $order = array();
  474. $order['customerName'] = trim($request->input('customerName')); //买家名称
  475. $order['receiverName'] = trim($request->input('receiverName')); //收件人
  476. if(preg_match('/^1\d{10}$/', $phone)) $order['receiverMobile'] = $phone; //收件人手机
  477. $order['receiverState'] = trim($request->input('receiverState')); //省份
  478. $order['receiverCity'] = trim($request->input('receiverCity')); //市
  479. $order['receiverState'] = str_replace(['省', '市'], '', $order['receiverState']);
  480. $order['receiverCity'] = str_replace('市', '', $order['receiverCity']);
  481. $order['receiverDistrict'] = trim($request->input('receiverDistrict')); //区
  482. $order['receiverAddress'] = trim($request->input('receiverAddress')); //详细地址
  483. $order['receivedAmount'] = trim($request->input('receivedAmount')); //付款金额
  484. $order['buyerMemo'] = trim($request->input('buyerMemo', '')); // 买家备注
  485. $order['sellerMemo'] = trim($request->input('sellerMemo', '')); // 卖家备注
  486. $order['delivery_date'] = !empty($request->input('delivery_date')) ? $request->input('delivery_date') : date('Y-m-d'); //发货日期
  487. $order['payment_type'] = (int)$request->input('payment_type'); //支付方式
  488. $coupon_id = (int)$request->input('coupon_id');
  489. if($coupon_id>0){
  490. $order['use_coupon'] = 1;
  491. }
  492. $order['buyer_phone'] = $request->input('buyer_phone');
  493. $order['use_b_gift'] = (int)$request->input('use_b_gift');
  494. $m_gift_id = (int)$request->input('m_gift_id');
  495. if($m_gift_id>0){
  496. $order['use_m_gift'] = 1;
  497. }
  498. $order['status'] = (int)$request->input('status');
  499. $order['should_amount'] = $request->input('should_amount');
  500. if(!$order['should_amount']) $order['should_amount'] = null;
  501. $id = (int)$request->input('id');
  502. //获取订单原信息
  503. $old_order = DB::table('order')->where('id', $id)->first();
  504. // ++++++充值卡验证余额逻辑 +++++++ //
  505. if($order['payment_type'] == 4){
  506. $deposit_phone = $request->input('deposit_phone');
  507. if($old_order->payment_type != 4){
  508. $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
  509. $balance = isset($customerInfo->balance) ? $customerInfo->balance : 0 ;
  510. if($balance < $order['receivedAmount']){
  511. //余额不足
  512. return self::returnValue(['msg'=>'充值卡余额不足'], 2000);
  513. }
  514. }elseif($old_order->receivedAmount != $order['receivedAmount']){
  515. $customerInfo = CustomerInfo::where('phone', $deposit_phone)->first();
  516. $balance = $customerInfo->balance;
  517. if( $balance < $order['receivedAmount']-$old_order->receivedAmount ){
  518. //余额不足
  519. return self::returnValue(['msg'=>'充值卡余额不足'], 2000);
  520. }
  521. }
  522. }
  523. $today_date = date('Y-m-d');
  524. DB::beginTransaction();
  525. try{
  526. $res = DB::table('order')->where('id', $id)->update($order);
  527. //商品信息
  528. $skus = $request->input('skus');
  529. $sku_ids = array_column($skus, 'sku_id');
  530. //先删除
  531. $del_re = DB::table('order_goods_skus')->where('order_id', $id)->whereNotIn('sku_id', $sku_ids)->update(['is_del'=>1]);
  532. if(!empty($skus)){
  533. //再同步
  534. foreach($skus as $k=>$sku){
  535. $sku_id = $sku['sku_id'];
  536. $num = $sku['num'];
  537. $sku_info = GoodsSkus::select('goodsCode', 'price', 'propsName','is_weigh')->where('id', $sku_id)->first();
  538. $sync_re = OrderGoodsSkus::updateOrCreate(['order_id'=>$id, 'sku_id'=>$sku_id, 'goods_id'=>$sku_info->goodsCode], ['num'=>$num, 'is_del'=>0, 'price'=>$sku_info->price]);
  539. }
  540. }
  541. if($res){
  542. #新加逻辑,若付款方式为充值卡/消费记录
  543. if($order['payment_type'] == 4 ){
  544. if($old_order->payment_type != 4 ){
  545. $consum = new CustomerConsum();
  546. $consum->phone = $deposit_phone;
  547. $consum->order_id = $id;
  548. $consum->money = $order['receivedAmount'];
  549. $consum->save();
  550. //更新余额
  551. $customerInfo->balance = $customerInfo->balance - $order['receivedAmount'];
  552. $customerInfo->save();
  553. }elseif($old_order->receivedAmount != $order['receivedAmount']){
  554. $consum = CustomerConsum::where('order_id', $id)->first();
  555. $consum->money = $order['receivedAmount'];
  556. $consum->save();
  557. //更新余额
  558. $add_amount = $order['receivedAmount'] - $old_order->receivedAmount; //新增消费额,有可能为负数
  559. $customerInfo->balance = $customerInfo->balance - $add_amount;
  560. $customerInfo->save();
  561. }
  562. }
  563. #记录操作日志
  564. $self_id = (int)$request->input('admin_id');
  565. $self_name = Admin::where('id', $self_id)->pluck('realname');
  566. $context = "修改订单";
  567. $type = 0;
  568. $tables = 'order';
  569. $data_id = $id;
  570. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  571. }
  572. #新加逻辑,使用优惠券,赠礼
  573. $use_b_gift = (int)$request->input('use_b_gift');
  574. if($coupon_id > 0 && $old_order->use_coupon != 1){
  575. $coupon = DB::table('customer_coupons')->where('phone', $order['buyer_phone'])->where('id', $coupon_id)->where('is_use',0)->orderBy('id', 'desc')->first();
  576. if(!empty($coupon)){
  577. $cup = array();
  578. $cup['is_use'] = 1;
  579. $cup['use_time'] = date('Y-m-d H:i:s');
  580. $cup['order_id'] = $id;
  581. DB::table('customer_coupons')->where('id', $coupon->id)->update($cup);
  582. }
  583. }elseif($coupon_id == 0 && $old_order->use_coupon == 1){
  584. DB::table('customer_coupons')->where('order_id', $id)->update(['is_use'=>0]);
  585. }elseif($coupon_id > 0 && $old_order->use_coupon == 1){
  586. $coupon = DB::table('customer_coupons')->where('phone', $order['buyer_phone'])->where('id', $coupon_id)->first();
  587. if(!empty($coupon) && $coupon->is_use == 0){
  588. DB::table('customer_coupons')->where('order_id', $id)->update(['is_use'=>0]);
  589. $cup = array();
  590. $cup['is_use'] = 1;
  591. $cup['use_time'] = date('Y-m-d H:i:s');
  592. $cup['order_id'] = $id;
  593. DB::table('customer_coupons')->where('id', $coupon->id)->update($cup);
  594. }
  595. }
  596. #查询本单使用赠礼
  597. $order_gifts = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->where('gift_id','>',0)->lists('gift_id');
  598. $order_m_gift_id = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 0)->pluck('id');
  599. $order_b_gift_ids = DB::table('customer_month_gift')->whereIn('id', $order_gifts)->where('type', 1)->lists('id');
  600. if($m_gift_id>0 && empty($order_m_gift_id) ){
  601. $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
  602. $order_m_gift = array();
  603. $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
  604. $order_m_gift['sku_id'] = $m_gift->sku_id;
  605. $order_m_gift['order_id'] = $id;
  606. $order_m_gift['goods_id'] = $m_sku->goodsCode;
  607. $order_m_gift['num'] = $m_gift->num;
  608. $order_m_gift['price'] = $m_sku->price;
  609. $order_m_gift['gift_id'] = $m_gift_id;
  610. $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
  611. #记录领取
  612. $receive_gift = array();
  613. $receive_gift['phone'] = $order['buyer_phone'];
  614. $receive_gift['dtime'] = date('Y-m-d');
  615. $receive_gift['order_id'] = $id;
  616. $receive_gift['gift_id'] = $m_gift_id;
  617. $receive_gift['gift_type'] = 0;
  618. DB::table('customer_gift_receives')->insert($receive_gift);
  619. }elseif($m_gift_id>0 && $m_gift_id != $order_m_gift_id){
  620. DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
  621. DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
  622. $m_gift = DB::table('customer_month_gift')->where('id', $m_gift_id)->where('type', 0)->first();
  623. $order_m_gift = array();
  624. $m_sku = GoodsSkus::where('id', $m_gift->sku_id)->first();
  625. $order_m_gift['sku_id'] = $m_gift->sku_id;
  626. $order_m_gift['order_id'] = $id;
  627. $order_m_gift['goods_id'] = $m_sku->goodsCode;
  628. $order_m_gift['num'] = $m_gift->num;
  629. $order_m_gift['price'] = $m_sku->price;
  630. $order_m_gift['gift_id'] = $m_gift_id;
  631. $order_gift_insert = DB::table('order_goods_skus')->insert($order_m_gift);
  632. #记录领取
  633. $receive_gift = array();
  634. $receive_gift['phone'] = $order['buyer_phone'];
  635. $receive_gift['dtime'] = date('Y-m-d');
  636. $receive_gift['order_id'] = $id;
  637. $receive_gift['gift_id'] = $m_gift_id;
  638. $receive_gift['gift_type'] = 0;
  639. DB::table('customer_gift_receives')->insert($receive_gift);
  640. }elseif($m_gift_id==0 && $order_m_gift_id>0){
  641. DB::table('order_goods_skus')->where('gift_id', $order_m_gift_id)->where('order_id', $id)->update(['is_del'=>1]);
  642. DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_id', $order_m_gift_id)->update(['is_del'=>1]);
  643. }
  644. $mdate = date('Y-m-01', strtotime($old_order->create_time));
  645. if($use_b_gift>0 && empty($order_b_gift_ids) ){
  646. $b_gifts = DB::table('customer_month_gift')->where('mtime', $mdate)->where('type', 1)->get();
  647. $b_gifts = json_decode(json_encode($b_gifts),true);
  648. foreach($b_gifts as $b_gift){
  649. $order_b_gift = array();
  650. $b_sku = GoodsSkus::where('id', $b_gift['sku_id'])->first();
  651. $order_b_gift['sku_id'] = $b_gift['sku_id'];
  652. $order_b_gift['order_id'] = $id;
  653. $order_b_gift['goods_id'] = $b_sku->goodsCode;
  654. $order_b_gift['num'] = $b_gift['num'];
  655. $order_b_gift['price'] = $b_sku->price;
  656. $order_b_gift['gift_id'] = $b_gift['id'];
  657. $order_gift_insert = DB::table('order_goods_skus')->insert($order_b_gift);
  658. }
  659. #记录领取
  660. $receive_gift = array();
  661. $receive_gift['phone'] = $order['buyer_phone'];
  662. $receive_gift['dtime'] = date('Y-m-d');
  663. $receive_gift['order_id'] = $id;
  664. $receive_gift['gift_type'] = 1;
  665. DB::table('customer_gift_receives')->insert($receive_gift);
  666. }elseif($use_b_gift==0 && !empty($order_b_gift_ids)){
  667. DB::table('order_goods_skus')->whereIn('gift_id', $order_b_gift_ids)->where('order_id', $id)->update(['is_del'=>1]);
  668. DB::table('customer_gift_receives')->where('order_id', $id)->where('gift_type', 1)->update(['is_del'=>1]);
  669. }
  670. DB::commit();
  671. }catch (Exception $e){
  672. DB::rollback();
  673. Log::errorLog($request,['error'=>$e->getMessage()],"order/update",0,'');
  674. return self::returnValue(['msg'=>$e->getMessage()], 2000);
  675. }
  676. return self::returnValue(['msg' => '更新成功']);
  677. }
  678. /**
  679. * 订单审核
  680. */
  681. public function setverify(Request $request){
  682. $id = (int)$request->input('id');
  683. $order = Order::find($id);
  684. $old_status = $order->status;
  685. $order->status = (int)$request->input('status');
  686. if($order->status == 0){
  687. $refuse_reason = trim($request->input('refuse_reason'));
  688. if(!empty($refuse_reason)){
  689. $order->refuse_reason = $refuse_reason;
  690. }
  691. }
  692. if($order->status == 2 || $order->status == 0){
  693. $order->verify_time = date('Y-m-d H:i:s'); //审核时间
  694. }
  695. //获取可变更状态
  696. $enableStatus = $this->getEnableStatus($old_status);
  697. if( !empty($enableStatus) ){
  698. $statusArr = array_column($enableStatus, 'status');
  699. if( !in_array($order->status, $statusArr) ){
  700. return self::returnValue(['msg'=>'异常操作'], 2000);
  701. }
  702. }else{
  703. return self::returnValue(['msg'=>'异常操作'], 2000);
  704. }
  705. DB::beginTransaction();
  706. try{
  707. $res = $order ->save();
  708. if($res){
  709. #记录操作日志
  710. $status_arr = array(
  711. 0 => '已录入',
  712. 1 => '待审核',
  713. 2 => '已审核',
  714. 3 => '已发货',
  715. 4 => '已退款'
  716. );
  717. $self_id = (int)$request->input('admin_id');
  718. $self_name = Admin::where('id', $self_id)->pluck('realname');
  719. $context = "订单状态变更:【".$status_arr[$old_status]."】变更为【". $status_arr[$order->status]."】({$old_status}->{$order->status})";
  720. $type = 1; //状态变更
  721. $tables = 'order';
  722. $data_id = $id;
  723. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  724. $today_date = date('Y-m-d');
  725. #如果审核通过,c仓, 并且到预发货时间,同步卖家云
  726. if($order->status == 2 && $order->warehouse == 3 && $order->delivery_date <= $today_date){
  727. $syncMj = $this->syncOrderToMj($id);
  728. if( $syncMj == false ){
  729. throw new Exception("订单同步到卖家云失败");
  730. }
  731. }
  732. }
  733. DB::commit();
  734. }catch (Exception $e){
  735. Log::errorLog($request,['error'=>$e->getMessage()],"order/setverify",0,['id'=>$id]);
  736. DB::rollback();
  737. return self::returnValue(['msg'=>$e->getMessage()], 2000);
  738. }
  739. return self::returnValue(['msg'=>'操作成功']);
  740. }
  741. /**
  742. * 订单状态机制,获取可变更状态
  743. * @param status:0已录入 1提交审核 2已审核 3已发货 4已退款
  744. */
  745. public function getEnableStatus($status){
  746. $status_arr = [
  747. 0 => ['status'=>0, 'name'=>'已录入'],
  748. 1 => ['status'=>1, 'name'=>'提交审核'],
  749. 2 => ['status'=>2, 'name'=>'审核通过'],
  750. 3 => ['status'=>3, 'name'=>'设为发货'],
  751. 4 => ['status'=>4, 'name'=>'设为退款']
  752. ];
  753. $status = (int)$status;
  754. $result = array();
  755. switch ($status) {
  756. case '0':
  757. $result[] = $status_arr[1];
  758. break;
  759. case '1':
  760. $result[] = $status_arr[2];
  761. $result[] = $status_arr[0];
  762. break;
  763. case '2':
  764. $result[] = $status_arr[1];
  765. $result[] = $status_arr[3];
  766. break;
  767. default:
  768. break;
  769. }
  770. return $result;
  771. }
  772. /**
  773. * 订单同步到卖家云
  774. */
  775. public function syncOrderToMj($id, $order = []){
  776. if( empty($order) ){
  777. $order = Order::select('eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
  778. $order = json_decode(json_encode($order), true);
  779. $skus = DB::table('order_goods_skus')->where('is_del', 0)->where('order_id', $id)->get();
  780. $skus = json_decode(json_encode($skus), true);
  781. foreach($skus as $k=>$v){
  782. $sku_info = GoodsSkus::where('id', $v['sku_id'])->first();
  783. $goods_info = Goods::where('id', $v['goods_id'])->first();
  784. $params = array();
  785. $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
  786. $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
  787. $params['goodsCode'] = $sku_info->goodsCode;//
  788. $params['skuCode'] = $v['sku_id'];
  789. #商品名称
  790. $params['goodsName'] = $goods_info->name;
  791. $params['propertiesName'] = $sku_info->is_weigh==1 ? $sku_info->propsName.'【按半斤称重】' : $sku_info->propsName;
  792. //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
  793. $params['price'] = $sku_info->is_weigh==1 ? round($v['price']/2,2) : $v['price'];
  794. $params['quantity'] = (int)($sku_info->is_weigh==1 ? $v['num']*2 : $v['num']);
  795. $params['discountFee'] = 0;
  796. $params['adjustFee'] = 0;
  797. $params['refundStatus'] = '正常';
  798. $order['skuList'][] = $params;
  799. }
  800. }
  801. //卖家云添加订单 ->改为审核通过再同步卖家云
  802. $mj_add = Order::mjOrderAdd($order);
  803. //同步卖家云订单数据到本系统
  804. if( isset($mj_add['order']['code']) ){
  805. $mjorder = array();
  806. $mjorder['orderCode'] = $mj_add['order']['code'];
  807. Order::where('id', $id)->update($mjorder);
  808. }
  809. return $mj_add;
  810. }
  811. /**
  812. * 订单详情
  813. */
  814. public function orderDetail(Request $request){
  815. $admin_id = (int)$request->input('admin_id');
  816. $id = (int)$request->input('id');
  817. $self_role = DB::table('admin_role')->where('user_id', $admin_id)->pluck('role_name');
  818. $team_id = null;
  819. if($self_role == '管理员'){
  820. $team_id = Admin::where('id', $admin_id)->pluck('team_id');
  821. $admin_id = null;
  822. }
  823. $order = Order::select('id', 'status', 'orderCode', 'customerName', 'receiverName', 'receiverMobile', 'receiverState','receiverCity','receiverDistrict','receiverAddress','receivedAmount','buyerMemo','sellerMemo','createTime','admin_id','admin_name','cost', 'logistics_id','is_refund','warehouse','delivery_date','refund_price','refund_type','send_note','freight_cost','payment_type','refuse_reason','refund_note','verify_time','aftersale_fee','aftersale_reason','aftersale_time','refund_time','send_time','should_amount', 'buyer_phone', 'use_coupon', 'use_m_gift', 'use_b_gift')->where('is_del', 0)->where('warehouse', 3)->where(function($query) use($admin_id, $team_id){
  824. if($admin_id>0) $query->where('admin_id', $admin_id);
  825. if($team_id>0) $query->where('team_id', $team_id);
  826. })->where('id', $id)->first();
  827. $status_arr = array(
  828. 0 => '已录入',
  829. 1 => '待审核',
  830. 2 => '已审核',
  831. 3 => '已发货',
  832. 4 => '已退款'
  833. );
  834. $goods = DB::table('order_goods_skus')->where('order_id', $id)->where('is_del', 0)->get();
  835. $skus = array();
  836. $m_gifts = array();
  837. $b_gifts = array();
  838. if(!empty($goods)){
  839. foreach($goods as &$item){
  840. $item->goods_name = Goods::where('id', $item->goods_id)->pluck('name');
  841. $item->picUrl = Goods::where('id', $item->goods_id)->pluck('picUrl');
  842. $goods_sku_info = GoodsSkus::select('propsName', 'is_weigh')->where('id', $item->sku_id)->first();
  843. if(!empty($goods_sku_info)){
  844. $item->props_name = $goods_sku_info->propsName;
  845. $item->is_weigh = $goods_sku_info->is_weigh;
  846. }else{
  847. $item->props_name = '';
  848. $item->is_weigh = 0;
  849. }
  850. $item->gift_type = null;
  851. if($item->gift_id>0){
  852. $gift_flag = DB::table('customer_month_gift')->where('id', $item->gift_id)->first();
  853. if($gift_flag->type==1){
  854. $item->gift_type = 1;
  855. $b_gifts[] = $item;
  856. }else{
  857. $item->gift_type = 0;
  858. $m_gifts[] = $item;
  859. }
  860. }else{
  861. $skus[] = $item;
  862. }
  863. }
  864. }
  865. $order->skus = $skus;
  866. $order->m_gifts = $m_gifts;
  867. $order->b_gifts = $b_gifts;
  868. #加粉时间
  869. $fanTime = DB::table('customers')->select('fanTime')->where('phone', $order->receiverMobile)->first();
  870. $order->fanTime = isset($fanTime->fanTime) ? $fanTime->fanTime : '';
  871. //$v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
  872. $order->status_name = $status_arr[$order->status];
  873. // 代金券
  874. if($order->use_coupon == 1){
  875. $order->coupon_id = DB::table('customer_coupons')->where('order_id', $id)->where('is_use', 1)->pluck('id');
  876. }
  877. return self::returnValue(['data'=>$order]);
  878. }
  879. }