OrderService.php
21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
namespace App\Http\Services;
use App\Exceptions\InvalidRequestException;
use App\Http\Models\User\TaxInfoModel;
use App\Models\OrderAddressModel;
use App\Models\OrderItemsModel;
use App\Models\OrderModel;
use App\Models\OrderPriceModel;
use App\Models\OrderReturnItemsModel;
use App\Models\UserAddressModel;
use App\Models\UserModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Models\CartModel;
//订单服务层
class OrderService
{
#收款常量
static $bank_info = [
"paypal" => "Paypal@semour.com",
"ttl_bank_name" => "HSBC Hong Kong",
"ttl_bank_address" => "1 Queen's Road Central, Hong Kong",
"ttl_swift_code" => "HSBCHKHHHKH",
"ttl_company_name" => "SEMOUR ELECTRONICS CO., LIMITED",
"ttl_account_no" => "819-847187-838",
];
/*
* 新增订单
address_id:0
items: [{
"goods_id": 1166788996788323300, //sku_id
"buy_number": 2, //购买数量
"buy_price":1.1 //购买单价
"remark":"",//备注
}]
*/
public static function addOrder($data, $user_id)
{
$shipping_address_id = $data["shipping_address_id"]; //收货地址
$billing_address_id = $data["billing_address_id"]; //账单地址
$items = json_decode(\Arr::get($data, 'items', []),true);
//查询用户详情
$userInfo = UserModel::getUserInfo($user_id);
$order_sn = self::createOrderSn();
try{
$con = DB::connection();
$redis = Redis::connection();
$con->beginTransaction();
#订单主表
$mainData =[
"order_sn"=>$order_sn,
"user_id"=>$user_id,
"creator_uid"=>$user_id,
"company_name"=>$userInfo["company_name"],
"sale_id"=>$userInfo["sale_id"],
"sale_name"=>$userInfo["sale_name"],
"exchange_rate"=>getRate(),
"currency"=>2,
"create_time"=>time(),
"update_time"=>time(),
];
$order_id = OrderModel::insertGetId($mainData);
if (!$order_id){
throw new InvalidRequestException("error:order main");
}
#订单地址
$shippingAddress = UserAddressModel::where("address_id",$shipping_address_id)->first();
$billingAddress = UserAddressModel::where("address_id",$billing_address_id)->first();
if (!$shippingAddress || !$billingAddress){
throw new InvalidRequestException("error: Address");
}
$addressData =[[
"address_id"=>$shipping_address_id,
"user_id"=>$user_id,
"order_id"=>$order_id,
"address_type"=>\Arr::get($shippingAddress,"address_type",0),
"order_address_type"=>1, //收货信息
"consignee"=>\Arr::get($shippingAddress,"first_name","")." ".\Arr::get($shippingAddress,"last_name",""),
"company_name"=>\Arr::get($shippingAddress,"company_name",""),
"email"=>\Arr::get($shippingAddress,"email",""),
"post_code"=>\Arr::get($shippingAddress,"post_code",""),
"phone"=>\Arr::get($shippingAddress,"phone",""),
"country_name"=>\Arr::get($shippingAddress,"country_name","") ,
"country"=>\Arr::get($shippingAddress,"country",""),
"province"=>\Arr::get($shippingAddress,"province",""),
"city"=>\Arr::get($shippingAddress,"city",""),
"detail_address"=>\Arr::get($shippingAddress,"detail_address",""),
"create_time"=>time(),
"update_time"=>time(),
],[
"address_id"=>$billing_address_id,
"user_id"=>$user_id,
"order_id"=>$order_id,
"address_type"=>\Arr::get($billingAddress,"address_type",0),
"order_address_type"=>2, //账单邮寄信息
"consignee"=>\Arr::get($billingAddress,"first_name","")." ".\Arr::get($billingAddress,"last_name",""),
"company_name"=>\Arr::get($billingAddress,"company_name",""),
"email"=>\Arr::get($billingAddress,"email",""),
"post_code"=>\Arr::get($billingAddress,"post_code",""),
"phone"=>\Arr::get($billingAddress,"phone",""),
"country_name"=>\Arr::get($billingAddress,"country_name","") ,
"country"=>\Arr::get($billingAddress,"country",""),
"province"=>\Arr::get($billingAddress,"province",""),
"city"=>\Arr::get($billingAddress,"city",""),
"detail_address"=>\Arr::get($billingAddress,"detail_address",""),
"create_time"=>time(),
"update_time"=>time(),
]
];
$order_address_id = OrderAddressModel::insert($addressData);
if (!$order_address_id){
throw new InvalidRequestException("error:order address");
}
#订单明细
$goodsInfoArr = ThirdService::getGoodsInfo(array_column($items,"goods_id")); //商品详情
$orderItems = []; //订单商品明细
$orderAmount = 0;
foreach ($items as $v) {
$goods_id = $v['goods_id'];
$skuInfo = $goodsInfoArr[$goods_id]; //sku详情
$cartInfo = CartModel::where(["user_id"=>$user_id,"goods_id"=>(string)$goods_id,"status"=>1])->first();
$buy_number = $v["buy_number"];
$buy_price = $v["buy_price"];
$orderAmount += round($buy_number*$buy_price,2);
$standard_brand = data_get($skuInfo,"standard_brand");
#查询英文品牌缩写
$brandName = $skuInfo["brand_name"];
$standard_brand_id = \Arr::get($standard_brand,"standard_brand_id",0);
$standardBrandInfo = $redis->hget("standard_brand",$standard_brand_id);
if ($standardBrandInfo){
$standardBrandInfoArr = json_decode($standardBrandInfo,true);
#深贸商城的品牌字段需展示该SKU对应基石标准品牌列表的英文简称,无则取英文名称、都无则取品牌名称
if ($standardBrandInfoArr["brand_short_name_en"]){
$brandName = $standardBrandInfoArr["brand_short_name_en"];
}else if ($standardBrandInfoArr["brand_name_en"]){
$brandName = $standardBrandInfoArr["brand_name_en"];
}
}
//订单明细
$orderItems[] = [
"order_id"=>$order_id, //订单ID
"user_id"=>$user_id, //用户ID
"goods_id"=>$goods_id, //商品ID
"supplier_id"=>\Arr::get($skuInfo,"supplier_id",0), //供应商ID(=company_id)
"brand_id"=>$skuInfo["brand_id"], //品牌ID
"standard_brand_id"=>$standard_brand_id, //标准品牌ID
"goods_name"=>\Arr::get($skuInfo,"goods_name",0), //型号
"class_id2"=>\Arr::get($skuInfo,"class_id2",0), //商品二级分类id
"batch"=>\Arr::get($skuInfo,"batch_sn",0), //批次
"class_id2_name"=>"", //商品二级分类名称
"supplier_name"=>\Arr::get($skuInfo,"supplier_name",0), //供应商名(=company_name)
"brand_name"=>$brandName, //品牌名
"standard_brand_name"=>\Arr::get($standard_brand,"brand_name",""), //标准品牌
"goods_type"=>2, //商品类型 联营:1专卖 2联营 自营 :3自营 4寄售 5第三方仓库
"goods_number"=>$buy_number, //购买数量
"goods_price"=>$buy_price, //商品单价
"goods_unit"=>"pcs", //商品单位
"delivery_time"=>\Arr::get($skuInfo,"hk_delivery_time",""), //交货时间
"canal"=>\Arr::get($skuInfo,"canal",""), //渠道标签
"initial_price"=>\Arr::get($v,"buy_price",0), //原始销售价格
"purchase_uid"=>0, //采购员id(内部账户系统id)
"purchase_name"=>"", //采购员
"contract_remark"=>"", //合同备注 6个汉子
"tax_rate"=>0, //税率
"raw_goods_sn"=>\Arr::get($cartInfo,"raw_goods_sn",0), //DGK原始编码
"raw_goods_packing"=>\Arr::get($cartInfo,"raw_goods_packing",0), //DGK原始包装
"raw_brand_name"=>\Arr::get($cartInfo,"raw_brand_name",0), //DGK原始品牌名称
"discount_amount"=>0, //折扣金额
"other_amount"=>0, //其它费用金额
"remarks"=>$v["remark"], //客户备注
];
if ($cartInfo){
#扣减购物车库存
$temp["status"] = $v["buy_number"] > 0 ? CartModel::status_yes:CartModel::status_no;
$temp["create_time"] = time();
$temp["update_time"] = time();
$cartUpdate = CartModel::where("cart_id",$cartInfo["cart_id"])->update($temp);
if (!$cartUpdate){
throw new InvalidRequestException("error:update cart");
}
}
}
$itemFlag = OrderItemsModel::insert($orderItems);
if (!$itemFlag){
throw new InvalidRequestException("error:order items");
}
#插入收款信息
$priceData = [
"order_id"=>$order_id, //用户ID
"order_sn"=>$order_sn, //
"price"=>$orderAmount, //
"price_type"=>1, //金额类型(正数ID大于0,负数ID小于0)-8活动优惠 -7支付优惠 -6运费优惠 -5尾款减款 -4优惠券优惠金额 -3尾款 -2预付款 -1付款,1货款 2附加费 3运费 4退款 5支付手续费
"create_time"=>time(), //添加时间
];
$priceFlag = OrderPriceModel::insert($priceData);
if (!$priceFlag){
throw new InvalidRequestException("error:order price");
}
#更新订单总额
$orderFlag = OrderModel::where("order_id",$order_id)->update(["order_amount"=>$orderAmount,"update_time"=>time()]);
if (!$orderFlag){
throw new InvalidRequestException("error:order update");
}
$con->commit();
return $order_id;
}catch (\Exception $e){
$con->rollback();
throw new InvalidRequestException($e->getMessage());
}
}
//生成订单单号
public static function createOrderSn($pre = "SE"){
$order_sn = $pre.date("Ymd").rand(1000,9999);
$check = OrderModel::getOrderInfo(["order_sn"=>$order_sn]);
if ($check){
self::createOrderSn();
}
return $order_sn;
}
/*
* 个人所有订单列表
*/
public static function orderLists($user_id,$param){
$query = OrderModel::with('order_items')
->select([
'order_id',
'order_sn',
'order_amount',
'status',
'create_time',
])
->orderBy('order_id', 'desc');
$query->where("user_id",$user_id);
#拼接搜索
foreach ($param as $a => $b) {
$b = trim($b);
$a = trim($a);
if (empty($b)) {
continue;
}
switch ($a) {
case "order_sn": //
case "order_id": //
case "status": //
$query->where($a,$b);
break;
case "goods_name": //
$query->whereHas('order_items', function ($q) use ($b) {
$q->where('goods_name', $b);
});
break;
}
}
$res = $query->paginate(\Arr::get($param,"page_size",10), ['*'], 'page',\Arr::get($param,"page",1) )->toArray();
if (!$res){
return false;
}
$result = $res["data"];
$orderAddressArr = OrderAddressModel::whereIn("order_id", array_column($result, "order_id"))->get()->keyBy("order_address_type")->toArray(); //地址信息
$temp = [];
foreach ($result as $k=>$v){
$order_id = $v["order_id"];
$items = OrderItemsModel::where("order_id",$order_id)->get()->toArray();
$receiveAddress = \Arr::get($orderAddressArr,OrderAddressModel::order_address_type_receive,[]);
$piaoAddress = \Arr::get($orderAddressArr,OrderAddressModel::order_address_type_piao,[]);
$merchandise_total = 0; //商品总额
foreach ($items as $a=>$b){
$ext_price = round($b["goods_number"]*$b["goods_price"],2);
$merchandise_total += $ext_price;
}
$merchandise_total = round($merchandise_total,2);
$priceInfo = OrderPriceModel::selectRaw("price_type,price")
->where("order_id",$order_id)
->get()->toArray(); //金额类型(正数ID大于0,负数ID小于0)-8活动优惠 -7支付优惠 -6运费优惠 -5尾款减款 -4优惠券优惠金额 -3尾款 -2预付款 -1付款,1货款 2附加费 3运费 4退款 5支付手续费
$priceArr = array_column($priceInfo,"price","price_type");
# $sub_total = OrderPriceModel::getOrderSubTotal($order_id);
$payment_surcharge_paypal = round($merchandise_total*0.046+0.5,2);
$ts =[
"order_id"=>$order_id,
"order_sn"=>$v["order_sn"],
"order_amount"=>$v["order_amount"],
"goods_name_arr"=>array_column($items,"goods_name"),
"status"=>$v["status"],
"status_en"=>\Arr::get(OrderModel::$status,$v["status"],""),
"receiver"=>\Arr::get($receiveAddress,"consignee"),
"email"=>\Arr::get($receiveAddress,"email"),
"phone"=>\Arr::get($receiveAddress,"phone"),
"shipping_address"=>\Arr::get($receiveAddress,"detail_address"),
"shipping"=>\Arr::get($priceArr,3,0), //运费
"payment_surcharge_paypal"=>$payment_surcharge_paypal, //paypal 支付手续费
"payment_surcharge_ttl"=>35, //ttl 支付手续费
"merchandise_total"=>$merchandise_total, //商品总额
"create_time"=>date('Y-m-d H:i:s', $v['create_time']), //下单时间
"over_time"=> date('Y-m-d H:i:s', $v['create_time']+48*3600), //截止时间
];
$ts["sub_total_payal"] = round($ts["payment_surcharge_paypal"]+$merchandise_total+$ts["shipping"],2); //paypal 小计
$ts["sub_total_ttl"] = round($ts["payment_surcharge_ttl"]+$merchandise_total+$ts["shipping"],2); //ttl 小计
$temp[] = $ts;
}
#统计当前用户各个状态数量
$orderCount = OrderModel::select(DB::raw('count(*) as num, status'))
->where("user_id",$user_id)
->groupBy('status')
->get()
->keyby("status")
->toArray();
$statusCount[0] = OrderModel::where(["user_id"=>$user_id])->count();
foreach (OrderModel::$status as $k=>$v){
$statusCount[$k] = \Arr::get(\Arr::get($orderCount,$k),"num",0);
}
return [
"order_count"=>$statusCount,
"bank_info"=>self::$bank_info,
"total"=> $res["total"], //返回总条数
"page"=>$res["current_page"], //第几页
"page_size"=> \Arr::get($param,"page_size",10), //每页多少条
"lists"=>$temp //数据列表
];
}
/*
* 取消订单
*/
public static function cancelOrder($user_id,$order_id){
if (!$order_id){
return [1001,"error:order_id no empty"];
}
$where["order_id"] = $order_id;
if ($user_id >0){
$where["user_id"] = $user_id;
}
$orderInfo = OrderModel::getOrderInfo($where);
if (!$orderInfo || $orderInfo["status"] > OrderModel::status_waiting_delivery){
return [1003,"error:order status"];
}
$flag = OrderModel::where("order_id",$order_id)->update(["status"=>OrderModel::status_cancel,"update_time"=>time()]);
return $flag ? [0,"Cancel order success"]:[1002,"Cancel order failed"];
}
/*
* 订单详情
*/
public static function orderDetail($user_id,$order_id){
$where["order_id"] = $order_id;
if ($user_id >0){
// $where["user_id"] = $user_id;
}
$orderInfo = OrderModel::getOrderInfo($where);
if (!$orderInfo){
return false;
}
$items = OrderItemsModel::where("order_id",$order_id)->get()->toArray(); //商品详情
$orderAddressArr = OrderAddressModel::selectRaw("consignee,company_name,email,phone,detail_address,shipping_sn,shipping_name,order_address_type")
->where("order_id",$order_id)
->get()
->keyBy("order_address_type")->toArray(); //地址信息,1收货信息 2账单邮寄信息
$goodsInfoArr = ThirdService::getGoodsInfo(array_column($items,"goods_id")); //商品详情
$itemsTemp = [];
$merchandise_total = 0; //商品总额
foreach ($items as $k=>$v){
$goods_id = $v["goods_id"];
$oneInfo = \Arr::get($goodsInfoArr,$goods_id,[]);
$ext_price = round($v["goods_number"]*$v["goods_price"],2);
$itemsTemp[] = [
"id"=>$k+1,
"goods_id"=>$v["goods_id"],
"goods_name"=>$v["goods_name"],
"brand_name"=>$v["brand_name"],
"delivery_time"=>changeDeliverToEn($v["delivery_time"]),
"pdf"=>\Arr::get($oneInfo,"pdf"),
"goods_number"=>$v["goods_number"],
"goods_price"=>$v["goods_price"],
"ext_price"=>$ext_price,
"remarks"=>$v["remarks"],
];
$merchandise_total += $ext_price;
}
$priceInfo = OrderPriceModel::selectRaw("price_type,price")
->where("order_id",$order_id)
->get()->toArray(); //金额类型(正数ID大于0,负数ID小于0)-8活动优惠 -7支付优惠 -6运费优惠 -5尾款减款 -4优惠券优惠金额 -3尾款 -2预付款 -1付款,1货款 2附加费 3运费 4退款 5支付手续费
$priceArr = array_column($priceInfo,"price","price_type");
$temp = [
"order_id"=>$order_id,
"order_sn"=>$orderInfo["order_sn"],
"order_amount"=>$orderInfo["order_amount"],
"sub_total"=>OrderPriceModel::getOrderSubTotal($order_id), //小计
"created_time"=>timeToDate($orderInfo["create_time"]), //创建时间
"reviewed_time"=>timeToDate($orderInfo["confirm_time"]), //审核时间
"paid_time"=>timeToDate($orderInfo["pay_time"]), //支付时间
"delivered_time"=>timeToDate($orderInfo["shipping_time"]), //发货时间
"received_time"=>"", //收货时间
"cancel_time"=>timeToDate($orderInfo["cancel_time"]), //订单取消时间
"finish_time"=>"", //交易成功时间
"shipping"=>\Arr::get($priceArr,3,0), //运费
"payment_surcharge"=>\Arr::get($priceArr,5,0), //支付手续费
"merchandise_total"=>round($merchandise_total,2), //商品总额
"status"=>$orderInfo["status"],
"status_en"=>\Arr::get(OrderModel::$status,$orderInfo["status"],""),
"shipping_address"=>\Arr::get($orderAddressArr,1,[]),
"billing_address"=>\Arr::get($orderAddressArr,2,[]),
"items"=>$itemsTemp,
];
return $temp;
}
//订单售后详情
public static function orderServiceDetail($user_id,$order_id){
$returnItems = OrderReturnItemsModel::where("order_id",$order_id)->get()->toArray();
if (!$returnItems){
return false;
}
$orderItemsArr = OrderItemsModel::where(["order_id"=>$order_id])
->wherein("rec_id",array_column($returnItems,"rec_id"))
->get()
->keyby("rec_id")
->toArray();
$temp = [];
$return_amount_all = 0;
foreach ($returnItems as $k=>$v){
$orderGoodsInfo = \Arr::get($orderItemsArr,$v["rec_id"]);
$temp[] = [
"no"=>$k+1,
"part_no"=>$orderGoodsInfo["goods_name"], // 型号名称
"manufacturer"=>$orderGoodsInfo["brand_name"], // 品牌名称
"price_per_unit"=>$orderGoodsInfo["goods_price"], // 单价
"quantity"=>$orderGoodsInfo["goods_number"], // 购买数量
"subtotal"=>round($orderGoodsInfo["goods_price"]*$orderGoodsInfo["goods_number"],2), // 金额
"type"=>OrderReturnItemsModel::$types[$v["type"]], // 售后类型
"after_sale_quantity"=>$v["return_num"], // 售后数量
"after_sale_price"=>$v["return_price"], // 售后单价
"return_amount"=>$v["return_amount"], // 售后金额
];
$return_amount_all += $v["return_amount"];
}
return ["return_amount_all"=>$return_amount_all,"items"=>$temp];
}
}