Geen omschrijving

FxSyncMjOrderStatus.php 6.5KB

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