InventoryDailyLoggingOwner.php 8.2 KB

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