Brak opisu

SyncMjWarehouse.php 2.6KB

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