123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- use App\Order;
- use App\OrderScript;
- class SyncMjWarehouse extends Command {
- protected $signature = 'SyncMjWarehouse';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '同步库存';
- protected $limit = 20;
- public function handle()
- {
- $this->SyncMjWarehouse();
- }
- public function SyncMjWarehouse(){
- $params = array();
- $params['eshopCode'] = config('constants.ESHOP_CODE');
- $params['warehouseCode'] = config('constants.WAREHOUSE_CODE'); //仓库编码
- $params['pageIndex'] = '1';
- $params['pageSize'] = $this->limit;
- $mjRes = OrderScript::mjWarehouseSkuGet($params);
- if($mjRes == false){
- exit('获取卖家云数据出错');
- }
- //插入第一页:
- $this->insertData( $mjRes['resultSet']['skuList'] );
- $count = $mjRes['resultSet']['totalNum'];
- $pages = ceil($count / $this->limit);
- //插入其他页面
- for($i=2; $i<=$pages; $i++){
- $params['pageIndex'] = $i;
- $mjRes = OrderScript::mjWarehouseSkuGet($params);
- $this->insertData( $mjRes['resultSet']['skuList'] );
- }
- }
- /**
- * 获取订单信息,可用来判断订单是否存在,同步订单状态
- */
- public function insertData($data){
- foreach($data as $k=>$sku){
- $code = $sku['skuProductCode'];
- $up = array();
- //1.更新商品库存数量(有可能为负数,有可能为0)
- //2.更新商品库存总成本(有可能为空)
- //3.计算商品真实价格
- //4.计算商品外部价格
- $cost = DB::table('goods_skus')->where('code', $code)->select('referenceCost', 'is_weigh')->first(); //获取规格成本/是否称重
- if($cost){
- $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量
- //实际库存=库存-冻结
- $real_quantity = $sku['quantity'] - $sku['fi'];
- $real_quantity = $cost->is_weigh == 1 ? $real_quantity/2 : $real_quantity; //对应规格数量
- $up['quantity'] = $quantity;
- $up['totalCost'] = isset($sku['totalCost']) ? $sku['totalCost'] : 0;
- if($up['quantity'] != 0 ){
- $up['realPrice'] = $up['totalCost'] / $up['quantity'];
- $up['outPrice'] = ceil($up['realPrice'] * 115)/100;
- } else {
- $up['realPrice'] = 0;
- $up['outPrice'] = 0;
- }
- $up['cost'] = isset($sku['cost']) ? $sku['cost'] : 0;
- //实际库存=库存-冻结
- $up['quantity'] = $real_quantity;
- $up_re = DB::table('goods_skus')->where('code', $code)->update($up);
- }
- }
- return true;
- }
- }
|