No Description

CleaningData.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php namespace App\Console\Commands;
  2. use Illuminate\Console\Command;
  3. use DB;
  4. use App\Order;
  5. use App\OrderScript;
  6. use App\GoodsSkus;
  7. use App\OrderGoodsSkus;
  8. class CleaningData extends Command {
  9. protected $signature = 'CleaningData';
  10. /**
  11. * The console command description.
  12. *
  13. * @var string
  14. */
  15. protected $description = '清洗订单成本数据';
  16. public function handle()
  17. {
  18. // $this->SyncMjOrderStatus();
  19. $this->SyncMjGoodsSkus();
  20. }
  21. public function SyncMjOrderStatus(){
  22. $orders = Order::select('id', 'outerCode')->where('orderCode','>',0)->where('status', 3)->where('warehouse', 3)->where('is_del', 0)->orderBy('id', 'desc')->get();
  23. $orders = json_decode(json_encode($orders), true);
  24. foreach($orders as $k=>$order){
  25. $result = $this->getMjOrder($order['id'], $order['outerCode']);
  26. // print_r($result);die;
  27. if( !empty($result) ){
  28. $data = array();
  29. if( isset($result['expressCode']) && !empty($result['expressCode']) ){
  30. $data['is_cannot_del'] = 1;
  31. }
  32. $status = $result['status'];
  33. if($status == '已发货' || $status == '已完成'){
  34. $data['status'] = 3;
  35. $data['logistics_id'] = $result['expressCode'];
  36. $data['send_time'] = !empty($result['deliveryTime']) ? date('Y-m-d', substr($result['deliveryTime'], 0, 10) ) : date('Y-m-d');
  37. $data['cost'] = 0;
  38. #计算成本
  39. //if($sku_info->referenceCost) $cost += $sku_info->referenceCost * $gnum[$k];
  40. foreach($result['skuList'] as $key=>$sku){
  41. $skuCode = $sku['skuCode'];
  42. $code_arr = explode('|', $skuCode);
  43. $sku_id = $code_arr[1];
  44. $cost = GoodsSkus::select('referenceCost', 'is_weigh')->where('id', $sku_id)->first(); //获取规格成本/是否称重
  45. $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量
  46. if(isset($sku['cost'])) {
  47. $data['cost'] += $sku['cost']*$sku['quantity']; //对应规格总成本
  48. } else {
  49. $data['cost'] += 0;
  50. // echo $order['id'].' ';
  51. // die;
  52. // $data['cost'] += $cost->referenceCost * $quantity; //对应规格总成本
  53. }
  54. // $data['cost'] += $cost->referenceCost * $quantity; //对应规格总成本
  55. #更新order_sku
  56. OrderGoodsSkus::where('order_id', $order['id'])->where('sku_id', $sku_id)->update(['send_num'=>$quantity]);
  57. }
  58. }
  59. if(!empty($data)){
  60. Order::where('id', $order['id'])->update($data);
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * 获取订单信息,可用来判断订单是否存在,同步订单状态
  67. */
  68. public function getMjOrder($id, $outerCode=''){
  69. $order = array();
  70. $order['eshopCode'] = config('constants.ESHOP_CODE');
  71. if(!$outerCode){
  72. $outerCode = Order::where('id', $id)->pluck('outerCode');
  73. }
  74. $order['outerCode'] = $outerCode;
  75. $mjOrder = OrderScript::mjOrderGet($order);
  76. if(!isset($mjOrder['order']['code'])){
  77. return false;
  78. }
  79. return $mjOrder['order'];
  80. }
  81. public function SyncMjGoodsSkus(){
  82. $goodsSkusList = GoodsSkus::whereIn('is_del',array(0,1))->get();
  83. $goodsSkusList = json_decode(json_encode($goodsSkusList),true);
  84. foreach ($goodsSkusList as $goodsSkus){
  85. //更新erp系统sku
  86. $erpArr['skuId'] = $goodsSkus['erpSkuId'];
  87. $erpArr['price'] = $goodsSkus['price'];
  88. $erpArr['referenceCost'] = $goodsSkus['referenceCost'];
  89. $erp_up = Order::mjErpSkuUpdate($erpArr);
  90. if( $erp_up == false ){
  91. throw new Exception("erpSku更新同步到卖家云失败");
  92. }
  93. //更新店铺中sku
  94. $shopArr['eshopCode'] = config('constants.ESHOP_CODE');
  95. $shopArr['goodsCode'] = $goodsSkus['goodsCode'];
  96. $shopArr['outerCode'] = $goodsSkus['id'];
  97. $shopArr['price'] = $goodsSkus['price'];
  98. $sku_up = Order::mjSkuUpdate($shopArr);
  99. if( $sku_up == false ){
  100. throw new Exception("店铺商品Sku更新同步到卖家云失败");
  101. }
  102. }
  103. }
  104. }