InventoryDailyLoggingOwner.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\CommodityBarcode;
  4. use App\Services\CommodityService;
  5. use App\Services\LogService;
  6. use Illuminate\Console\Command;
  7. use App\InventoryDailyLoggingOwner as LoggingOwner;
  8. use Illuminate\Support\Facades\DB;
  9. class InventoryDailyLoggingOwner extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'InventoryDailyLoggingOwner';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '监听指定货主记录';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * 增量添加,每天都会重复去录入之前数据,没有限制去重条件. 因业务需要去查看每天同批货的变动
  36. *
  37. * @param CommodityService $commodityService
  38. * @return void
  39. */
  40. public function handle(CommodityService $commodityService)
  41. {
  42. //获取需要查询的货主,键值对:code => id
  43. $owners = $this->getLoggingOwners();
  44. //计算数量,为0直接return
  45. $let = count($owners);
  46. if ($let == 0)return;
  47. //拼接SQL,SELECT仅取指定字段
  48. $sql = "SELECT INV_LOT.customerid,INV_LOT.sku,INV_LOT.qty,BAS_SKU.skulength,
  49. BAS_SKU.skuwidth,BAS_SKU.skuhigh,BAS_SKU.cube,BAS_SKU.descr_c,BAS_SKU.alternate_sku1,BAS_SKU.grossweight,
  50. INV_LOT_ATT.lotatt05
  51. FROM INV_LOT
  52. LEFT JOIN BAS_SKU ON INV_LOT.sku = BAS_SKU.sku AND INV_LOT.customerid = BAS_SKU.customerid
  53. LEFT JOIN INV_LOT_ATT ON INV_LOT.lotnum = INV_LOT_ATT.lotnum
  54. WHERE INV_LOT.customerid IN (";
  55. $index = 1;
  56. foreach ($owners as $code => $id){
  57. $sql .= "'".$code."'";
  58. if ($index != $let)$sql .= ",";
  59. $index++;
  60. }
  61. $sql .= ")";
  62. //执行获取结果:stdClass类型
  63. $invLots = DB::connection('oracle')->select(DB::raw($sql));
  64. //声明一个数组,作为第一次去重的容器
  65. $inventoryDailyLogs = [];
  66. foreach ($invLots as $invLot){
  67. //以MAP形式记录进数组,货主code与商品sku作为联合主键唯一标识,如重复叠加其数量
  68. if ($inventoryDailyLogs[$owners[$invLot->customerid].'-'.$invLot->sku.'-'.$invLot->lotatt05] ?? false){
  69. $inventoryDailyLogs[$owners[$invLot->customerid].'-'.$invLot->sku.'-'.$invLot->lotatt05]['amount'] += $invLot->qty;
  70. }else{
  71. //符合的结果取此对象的关键信息存进第一个数组
  72. $inventoryDailyLogs[$owners[$invLot->customerid].'-'.$invLot->sku.'-'.$invLot->lotatt05] = [
  73. 'commodity' => [
  74. 'owner_id'=>$owners[$invLot->customerid],
  75. 'sku'=>$invLot->sku,
  76. 'name'=>$invLot->descr_c,
  77. 'length'=>$invLot->skulength,
  78. 'width'=>$invLot->skuwidth,
  79. 'height'=>$invLot->skuhigh,
  80. 'volumn'=>$invLot->cube,
  81. 'code'=>$invLot->alternate_sku1,
  82. 'weight' => $invLot->grossweight,
  83. ],
  84. 'amount' => $invLot->qty,
  85. 'volumn_occupied' => 0,
  86. 'gross_weight' => 0,
  87. 'depository_code' => $invLot->lotatt05,
  88. ];
  89. }
  90. }
  91. //第二个数组作为批量插入使用
  92. $data = [];
  93. //遍历第一个数组,此时已经去重完成,直接取对应参数push进data中
  94. foreach ($inventoryDailyLogs as $inventoryDailyLog){
  95. //寻找己方库中是否存在对应商品,存在更新其长宽高体积,不存在录入
  96. $commodity = $inventoryDailyLog['commodity'];
  97. $param = ['owner_id'=>$commodity['owner_id'],'sku'=>$commodity["sku"]];
  98. //体积存在为0的情况,需再次计算一次,此处体积为m³
  99. if (!$commodity['volumn'] || $commodity['volumn'] == "0"){
  100. $commodity['volumn'] = $commodity['length']*$commodity['width']*$commodity['height'];
  101. }
  102. $column = [
  103. 'owner_id'=>$commodity['owner_id'],
  104. 'sku'=>$commodity['sku'],
  105. 'name'=>$commodity['name'],
  106. 'length'=>$commodity['length'],
  107. 'width'=>$commodity['width'],
  108. 'height'=>$commodity['height'],
  109. 'volumn'=>$commodity['volumn'],
  110. ];
  111. $result = $commodityService->updateOrCreate($param,$column);
  112. app('LogService')->log(__METHOD__,"同步库存每日记录时修改或生成商品",json_encode($column));
  113. $commodity_id = $result->id;
  114. //寻找对应barcode是否存在,不存在录入
  115. if ($commodity['code']) CommodityBarcode::query()->firstOrCreate(['commodity_id'=>$commodity_id,'code'=>$commodity['code']]);
  116. //计算总体积,商品体积×该单数量
  117. $volumn_occupied = $commodity['volumn']*$inventoryDailyLog["amount"];
  118. $gross_weight = $commodity['weight']*$inventoryDailyLog["amount"];
  119. $depository = null;
  120. if ($inventoryDailyLog['depository_code']){
  121. $depository = app('DepositoryService')->firstOrCreate(["code"=>$inventoryDailyLog['depository_code']],[
  122. "name"=>$inventoryDailyLog['depository_code'],
  123. "code"=>$inventoryDailyLog['depository_code'],
  124. ]);
  125. }
  126. $data[] = [
  127. "owner_id"=>$commodity['owner_id'],
  128. "created_at"=>date('Y-m-d H:i:s'),
  129. "commodity_id"=>$commodity_id,
  130. "amount"=>$inventoryDailyLog['amount'],
  131. "volumn_occupied"=>$volumn_occupied,
  132. "gross_weight"=>$gross_weight,
  133. "depository_id"=>$depository ? $depository->id : null,
  134. ];
  135. }
  136. DB::table('inventory_daily_logs')->insert($data);
  137. app('LogService')->log(__METHOD__,"同步库存每日记录",json_encode($data));
  138. }
  139. public function getLoggingOwners(){
  140. $loggingOwners = LoggingOwner::with('owner')->select('id','owner_id')->where('status','启用')->get();
  141. $owners = [];
  142. foreach ($loggingOwners as $loggingOwner){
  143. if ($loggingOwner->owner){
  144. $owners[$loggingOwner->owner->code] = $loggingOwner->owner_id;
  145. }
  146. }
  147. return $owners;
  148. }
  149. }