123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- use App\Order;
- use App\OrderScript;
- use App\FxOrder;
- use App\GoodsSkus;
- use App\OrderGoodsSkus;
- use App\FxOrderGoodsSkus;
- class SyncMjOrderStatus extends Command {
- protected $signature = 'SyncMjOrderStatus';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '同步订单状态';
- public function handle()
- {
- $this->SyncMjOrderStatus();
- $this->SyncFxOrderStatus();
- }
- public function SyncMjOrderStatus(){
- $orders = Order::select('id', 'outerCode', 'admin_id')->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']);
- 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']) && $sku['cost']>0) {
- // $order_sku_cost = $cost->is_weigh == 1 ? $sku['cost'] * 2 : $sku['cost'];
- // $data['cost'] += $order_sku_cost * $quantity; //对应规格总成本
- // } else {
- $order_sku_cost = $cost->referenceCost;
- $data['cost'] += $order_sku_cost * $quantity; //对应规格总成本
- // }
- // $data['cost'] += $cost->referenceCost * $quantity; //对应规格总成本
- #更新order_sku
- OrderGoodsSkus::where('order_id', $order['id'])->where('sku_id', $sku_id)->update(['send_num'=>$quantity, 'cost'=>$order_sku_cost]);
- }
- $data['cost'] = round($data['cost'], 2);
- }
- if(!empty($data)){
- Order::where('id', $order['id'])->update($data);
- }
- }
- }
- }
- public function SyncFxOrderStatus(){
- $orders = FxOrder::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->getFxMjOrder($order['id'], $order['outerCode']);
- 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;
- #计算成本
- 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']) && $sku['cost']>0) {
- $order_sku_cost = $cost->is_weigh == 1 ? ceil($sku['cost'] * 2 * 115)/100 : ceil($sku['cost'] * 115)/100 ;
- $data['cost'] += $order_sku_cost * $quantity; //对应规格总成本
- } else {
- $order_sku_cost = ceil($cost->referenceCost * 115)/100;
- $data['cost'] += $order_sku_cost * $quantity; //对应规格总成本
- }
- #更新order_sku
- FxOrderGoodsSkus::where('order_id', $order['id'])->where('sku_id', $sku_id)->update(['send_num'=>$quantity, 'cost'=>$order_sku_cost]);
- }
- $data['cost'] = round($data['cost'], 2);
- }
- if(!empty($data)){
- FxOrder::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 getFxMjOrder($id, $outerCode=''){
- $order = array();
- $order['eshopCode'] = config('constants.ESHOP_CODE');
- if(!$outerCode){
- $outerCode = FxOrder::where('id', $id)->pluck('outerCode');
- }
- $order['outerCode'] = $outerCode;
- $mjOrder = OrderScript::mjOrderGet($order);
- if(!isset($mjOrder['order']['code'])){
- return false;
- }
- return $mjOrder['order'];
- }
- }
|