説明なし

SyncMjOrderStatus.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use App\Order;
  6. use App\OrderScript;
  7. use App\FxOrder;
  8. use App\GoodsSkus;
  9. use App\OrderGoodsSkus;
  10. use App\FxOrderGoodsSkus;
  11. class SyncMjOrderStatus extends Command {
  12. protected $signature = 'SyncMjOrderStatus';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '同步订单状态';
  19. public function handle()
  20. {
  21. $this->SyncMjOrderStatus();
  22. $this->SyncFxOrderStatus();
  23. }
  24. public function SyncMjOrderStatus(){
  25. $orders = Order::select('id', 'outerCode', 'admin_id')->where('orderCode','>',0)->where('status', '<', 3)->where('warehouse', 3)->where('is_del', 0)->orderBy('id', 'desc')->get();
  26. $orders = json_decode(json_encode($orders), true);
  27. foreach($orders as $k=>$order){
  28. $result = $this->getMjOrder($order['id'], $order['outerCode']);
  29. if( !empty($result) ){
  30. $data = array();
  31. if( isset($result['expressCode']) && !empty($result['expressCode']) ){
  32. $data['is_cannot_del'] = 1;
  33. }
  34. $status = $result['status'];
  35. if($status == '已发货' || $status == '已完成'){
  36. $data['status'] = 3;
  37. $data['logistics_id'] = $result['expressCode'];
  38. $data['send_time'] = !empty($result['deliveryTime']) ? date('Y-m-d', substr($result['deliveryTime'], 0, 10) ) : date('Y-m-d');
  39. $data['cost'] = 0;
  40. #计算成本
  41. //if($sku_info->referenceCost) $cost += $sku_info->referenceCost * $gnum[$k];
  42. foreach($result['skuList'] as $key=>$sku){
  43. $skuCode = $sku['skuCode'];
  44. $code_arr = explode('|', $skuCode);
  45. $sku_id = $code_arr[1];
  46. $cost = GoodsSkus::select('referenceCost', 'is_weigh')->where('id', $sku_id)->first(); //获取规格成本/是否称重
  47. $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量
  48. // if(isset($sku['cost']) && $sku['cost']>0) {
  49. // $order_sku_cost = $cost->is_weigh == 1 ? $sku['cost'] * 2 : $sku['cost'];
  50. // $data['cost'] += $order_sku_cost * $quantity; //对应规格总成本
  51. // } else {
  52. $order_sku_cost = $cost->referenceCost;
  53. $data['cost'] += $order_sku_cost * $quantity; //对应规格总成本
  54. // }
  55. // $data['cost'] += $cost->referenceCost * $quantity; //对应规格总成本
  56. #更新order_sku
  57. OrderGoodsSkus::where('order_id', $order['id'])->where('sku_id', $sku_id)->update(['send_num'=>$quantity, 'cost'=>$order_sku_cost]);
  58. }
  59. $data['cost'] = round($data['cost'], 2);
  60. }
  61. if(!empty($data)){
  62. Order::where('id', $order['id'])->update($data);
  63. }
  64. }
  65. }
  66. }
  67. public function SyncFxOrderStatus(){
  68. $orders = FxOrder::select('id', 'outerCode')->where('orderCode','>',0)->where('status', '<', 3)->where('warehouse', 3)->where('is_del', 0)->orderBy('id', 'desc')->get();
  69. $orders = json_decode(json_encode($orders), true);
  70. foreach($orders as $k=>$order){
  71. $result = $this->getFxMjOrder($order['id'], $order['outerCode']);
  72. if( !empty($result) ){
  73. $data = array();
  74. if( isset($result['expressCode']) && !empty($result['expressCode']) ){
  75. $data['is_cannot_del'] = 1;
  76. }
  77. $status = $result['status'];
  78. if($status == '已发货' || $status == '已完成'){
  79. $data['status'] = 3;
  80. $data['logistics_id'] = $result['expressCode'];
  81. $data['send_time'] = !empty($result['deliveryTime']) ? date('Y-m-d', substr($result['deliveryTime'], 0, 10) ) : date('Y-m-d');
  82. $data['cost'] = 0;
  83. #计算成本
  84. foreach($result['skuList'] as $key=>$sku){
  85. $skuCode = $sku['skuCode'];
  86. $code_arr = explode('|', $skuCode);
  87. $sku_id = $code_arr[1];
  88. $cost = GoodsSkus::select('referenceCost', 'is_weigh')->where('id', $sku_id)->first(); //获取规格成本/是否称重
  89. $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量
  90. if(isset($sku['cost']) && $sku['cost']>0) {
  91. $order_sku_cost = $cost->is_weigh == 1 ? ceil($sku['cost'] * 2 * 115)/100 : ceil($sku['cost'] * 115)/100 ;
  92. $data['cost'] += $order_sku_cost * $quantity; //对应规格总成本
  93. } else {
  94. $order_sku_cost = ceil($cost->referenceCost * 115)/100;
  95. $data['cost'] += $order_sku_cost * $quantity; //对应规格总成本
  96. }
  97. #更新order_sku
  98. FxOrderGoodsSkus::where('order_id', $order['id'])->where('sku_id', $sku_id)->update(['send_num'=>$quantity, 'cost'=>$order_sku_cost]);
  99. }
  100. $data['cost'] = round($data['cost'], 2);
  101. }
  102. if(!empty($data)){
  103. FxOrder::where('id', $order['id'])->update($data);
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * 获取订单信息,可用来判断订单是否存在,同步订单状态
  110. */
  111. public function getMjOrder($id, $outerCode=''){
  112. $order = array();
  113. $order['eshopCode'] = config('constants.ESHOP_CODE');
  114. if(!$outerCode){
  115. $outerCode = Order::where('id', $id)->pluck('outerCode');
  116. }
  117. $order['outerCode'] = $outerCode;
  118. $mjOrder = OrderScript::mjOrderGet($order);
  119. if(!isset($mjOrder['order']['code'])){
  120. return false;
  121. }
  122. return $mjOrder['order'];
  123. }
  124. /**
  125. * 获取订单信息,可用来判断订单是否存在,同步订单状态
  126. */
  127. public function getFxMjOrder($id, $outerCode=''){
  128. $order = array();
  129. $order['eshopCode'] = config('constants.ESHOP_CODE');
  130. if(!$outerCode){
  131. $outerCode = FxOrder::where('id', $id)->pluck('outerCode');
  132. }
  133. $order['outerCode'] = $outerCode;
  134. $mjOrder = OrderScript::mjOrderGet($order);
  135. if(!isset($mjOrder['order']['code'])){
  136. return false;
  137. }
  138. return $mjOrder['order'];
  139. }
  140. }