123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- use App\Order;
- use App\OrderScript;
- use App\GoodsSkus;
- use App\OrderGoodsSkus;
- class CleaningData extends Command {
- protected $signature = 'CleaningData';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '清洗订单成本数据';
- public function handle()
- {
- // $this->SyncMjOrderStatus();
- $this->SyncMjGoodsSkus();
- }
- public function SyncMjOrderStatus(){
- $orders = Order::select('id', 'outerCode')->where('orderCode','>',0)->where('status', 3)->where('warehouse', 3)->where('is_del', 0)->orderBy('id', 'desc')->get();
- $orders = json_decode(json_encode($orders), true);
- foreach($orders as $k=>$order){
- $result = $this->getMjOrder($order['id'], $order['outerCode']);
- // print_r($result);die;
- if( !empty($result) ){
- $data = array();
- if( isset($result['expressCode']) && !empty($result['expressCode']) ){
- $data['is_cannot_del'] = 1;
- }
- $status = $result['status'];
- if($status == '已发货' || $status == '已完成'){
- $data['status'] = 3;
- $data['logistics_id'] = $result['expressCode'];
- $data['send_time'] = !empty($result['deliveryTime']) ? date('Y-m-d', substr($result['deliveryTime'], 0, 10) ) : date('Y-m-d');
- $data['cost'] = 0;
- #计算成本
- //if($sku_info->referenceCost) $cost += $sku_info->referenceCost * $gnum[$k];
- foreach($result['skuList'] as $key=>$sku){
- $skuCode = $sku['skuCode'];
- $code_arr = explode('|', $skuCode);
- $sku_id = $code_arr[1];
- $cost = GoodsSkus::select('referenceCost', 'is_weigh')->where('id', $sku_id)->first(); //获取规格成本/是否称重
- $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量
- if(isset($sku['cost'])) {
- $data['cost'] += $sku['cost']*$sku['quantity']; //对应规格总成本
- } else {
- $data['cost'] += 0;
- // echo $order['id'].' ';
- // die;
- // $data['cost'] += $cost->referenceCost * $quantity; //对应规格总成本
- }
- // $data['cost'] += $cost->referenceCost * $quantity; //对应规格总成本
- #更新order_sku
- OrderGoodsSkus::where('order_id', $order['id'])->where('sku_id', $sku_id)->update(['send_num'=>$quantity]);
- }
- }
- if(!empty($data)){
- Order::where('id', $order['id'])->update($data);
- }
- }
- }
- }
- /**
- * 获取订单信息,可用来判断订单是否存在,同步订单状态
- */
- public function getMjOrder($id, $outerCode=''){
- $order = array();
- $order['eshopCode'] = config('constants.ESHOP_CODE');
- if(!$outerCode){
- $outerCode = Order::where('id', $id)->pluck('outerCode');
- }
- $order['outerCode'] = $outerCode;
- $mjOrder = OrderScript::mjOrderGet($order);
- if(!isset($mjOrder['order']['code'])){
- return false;
- }
- return $mjOrder['order'];
- }
- public function SyncMjGoodsSkus(){
- $goodsSkusList = GoodsSkus::whereIn('is_del',array(0,1))->get();
- $goodsSkusList = json_decode(json_encode($goodsSkusList),true);
- foreach ($goodsSkusList as $goodsSkus){
- //更新erp系统sku
- $erpArr['skuId'] = $goodsSkus['erpSkuId'];
- $erpArr['price'] = $goodsSkus['price'];
- $erpArr['referenceCost'] = $goodsSkus['referenceCost'];
- $erp_up = Order::mjErpSkuUpdate($erpArr);
- if( $erp_up == false ){
- throw new Exception("erpSku更新同步到卖家云失败");
- }
- //更新店铺中sku
- $shopArr['eshopCode'] = config('constants.ESHOP_CODE');
- $shopArr['goodsCode'] = $goodsSkus['goodsCode'];
- $shopArr['outerCode'] = $goodsSkus['id'];
- $shopArr['price'] = $goodsSkus['price'];
- $sku_up = Order::mjSkuUpdate($shopArr);
- if( $sku_up == false ){
- throw new Exception("店铺商品Sku更新同步到卖家云失败");
- }
- }
- }
- }
|