Bez popisu

SyncMjWarehouse.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use App\Order;
  6. use App\OrderScript;
  7. class SyncMjWarehouse extends Command {
  8. protected $signature = 'SyncMjWarehouse';
  9. /**
  10. * The console command description.
  11. *
  12. * @var string
  13. */
  14. protected $description = '同步库存';
  15. protected $limit = 20;
  16. public function handle()
  17. {
  18. $this->SyncMjWarehouse();
  19. }
  20. public function SyncMjWarehouse(){
  21. $params = array();
  22. $params['eshopCode'] = config('constants.ESHOP_CODE');
  23. $params['warehouseCode'] = config('constants.WAREHOUSE_CODE'); //仓库编码
  24. $params['pageIndex'] = '1';
  25. $params['pageSize'] = $this->limit;
  26. $mjRes = OrderScript::mjWarehouseSkuGet($params);
  27. if($mjRes == false){
  28. exit('获取卖家云数据出错');
  29. }
  30. //插入第一页:
  31. $this->insertData( $mjRes['resultSet']['skuList'] );
  32. $count = $mjRes['resultSet']['totalNum'];
  33. $pages = ceil($count / $this->limit);
  34. //插入其他页面
  35. for($i=2; $i<=$pages; $i++){
  36. $params['pageIndex'] = $i;
  37. $mjRes = OrderScript::mjWarehouseSkuGet($params);
  38. $this->insertData( $mjRes['resultSet']['skuList'] );
  39. }
  40. }
  41. /**
  42. * 获取订单信息,可用来判断订单是否存在,同步订单状态
  43. */
  44. public function insertData($data){
  45. foreach($data as $k=>$sku){
  46. $code = $sku['skuProductCode'];
  47. $up = array();
  48. //1.更新商品库存数量(有可能为负数,有可能为0)
  49. //2.更新商品库存总成本(有可能为空)
  50. //3.计算商品真实价格
  51. //4.计算商品外部价格
  52. $cost = DB::table('goods_skus')->where('code', $code)->select('referenceCost', 'is_weigh')->first(); //获取规格成本/是否称重
  53. if($cost){
  54. $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量
  55. //实际库存=库存-冻结
  56. $real_quantity = $sku['quantity'] - $sku['fi'];
  57. $real_quantity = $cost->is_weigh == 1 ? $real_quantity/2 : $real_quantity; //对应规格数量
  58. $up['quantity'] = $quantity;
  59. $up['totalCost'] = isset($sku['totalCost']) ? $sku['totalCost'] : 0;
  60. if($up['quantity'] != 0 ){
  61. $up['realPrice'] = $up['totalCost'] / $up['quantity'];
  62. // $up['outPrice'] = ceil($up['realPrice'] * 1.15);
  63. } else {
  64. // $up['realPrice'] = 0;
  65. // $up['outPrice'] = 0;
  66. }
  67. if(!$cost->outPrice){
  68. if(isset($up['realPrice'])){
  69. $up['outPrice'] = ceil($up['realPrice']*1.15);
  70. }
  71. }
  72. $up['cost'] = isset($sku['cost']) ? $sku['cost'] : 0;
  73. //实际库存=库存-冻结
  74. $up['quantity'] = $real_quantity;
  75. $up_re = DB::table('goods_skus')->where('code', $code)->update($up);
  76. }
  77. }
  78. return true;
  79. }
  80. }