MakeSeal.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\cxinv\command;
  4. use app\cxinv\model\FinancialManager;use app\cxinv\model\ProductFz;use app\cxinv\model\ProductOnlog;use app\cxinv\model\ProductSeal;use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\facade\Cache;
  10. class MakeSeal extends Command
  11. {
  12. protected function configure()
  13. {
  14. // 指令配置
  15. $this->setName('makeseal')
  16. ->setDescription('the makeseal command');
  17. }
  18. protected function execute(Input $input, Output $output)
  19. {
  20. if(Cache::get('makeseal')==1){
  21. return ;
  22. }
  23. $list = $this->GetInfo();
  24. while ( $list->valid()){
  25. Cache::set('makeseal',1,600);
  26. $date= date('Y-m-d H:i:s');
  27. $item = $list->current();
  28. $output->writeln("[{$date}]: {$item['company_code']}=>{$item['fz_date']} 开始执行");
  29. $this->makeSeal($item);
  30. $list->next();
  31. }
  32. Cache::set('makeseal',0);
  33. }
  34. public function GetInfo()
  35. {
  36. $info = ProductFz::where([['status','=',0]])->order("fz_date asc")->group('company_code')->cursor();
  37. foreach ($info as $item){
  38. yield $item->toArray();
  39. }
  40. }
  41. public function makeSeal($item){
  42. $lastMonth = date('Y-m',strtotime('-1 month',strtotime($item['fz_date'])));
  43. $ist = ProductFz::where([['fz_date','=',$lastMonth],['company_code','=',$item['company_code']]])->findOrEmpty();
  44. if(!$ist->isEmpty()&& $ist['status']!=2){
  45. return false;
  46. };
  47. $seal = ProductSeal::where([['fz_id','=',$item['id']]])->select();
  48. if($seal->isEmpty()){
  49. $lastSeal = ProductSeal::where([['fz_id','=',$ist->id],['balance_num','>',0]])->select();
  50. if(!$lastSeal->isEmpty()){
  51. (new ProductSeal)->saveAll(array_map(function ($val)use($item){
  52. return [ 'product_id'=>$val['product_id'],
  53. 'skuCode'=>$val['skuCode'],
  54. 'goodName'=>$val['goodName'],
  55. 'spec'=>$val['spec'],
  56. 'unit'=>$val['unit'],
  57. 'unit_price'=>$val['unit_price'],
  58. 'begin_num'=>$val['balance_num'],
  59. 'in_num'=>"0",
  60. 'out_num'=>"0",
  61. 'balance_num'=>$val['balance_num'],
  62. 'fz_id'=>$item['id'],
  63. ];
  64. },$lastSeal->toArray()));
  65. }
  66. }
  67. (new ProductSeal)->saveAll($this->getProductLog($item,$ist->id));
  68. }
  69. public function getProductLog($row,$lastId){
  70. $pastLog= ProductOnlog::withJoin(['Product'],'left')
  71. ->where([['fz_date','=',$row['fz_date']],['buyer_code','=',$row['company_code']]])
  72. ->field('type,order_item_id,product_id,skuCode,goodName,spec,unit,product_onlog.unit_price,num')->cursor();
  73. $list=[];
  74. $balance = ProductSeal::where([['fz_id','=',$lastId],['balance_num','>',0]])->column('balance_num','product_id');
  75. foreach ($pastLog as $item){
  76. if(!isset($list[$item['product_id']])){
  77. $list[$item['product_id']] = [
  78. 'id'=>ProductSeal::where([['fz_id','=',$row['id']],['product_id','=',$item['product_id']]])->value('id',null),
  79. 'product_id'=>$item['product_id'],
  80. 'skuCode'=>$item['skuCode'],
  81. 'goodName'=>$item['goodName'],
  82. 'spec'=>$item['spec'],
  83. 'unit'=>$item['unit'],
  84. 'unit_price'=>$item['unit_price'],
  85. 'begin_num'=>$balance[$item['product_id']]??"0",
  86. 'in_num'=>"0",
  87. 'out_num'=>"0",
  88. 'balance_num'=>$balance[$item['product_id']]??"0",
  89. 'fz_id'=>$row['id'],
  90. ];
  91. }
  92. if($item['type']==3|| $item['type']==1|| $item['type']==5|| $item['type']==7){
  93. $list[$item['product_id']]['balance_num']=bcadd($list[$item['product_id']]['balance_num'],$item['num'],8);
  94. $list[$item['product_id']]['in_num']=bcadd($list[$item['product_id']]['in_num'],$item['num'],8);
  95. }
  96. if($item['type']==4|| $item['type']==2|| $item['type']==6|| $item['type']==8){
  97. $list[$item['product_id']]['balance_num']=bcsub($list[$item['product_id']]['balance_num'],$item['num'],8);
  98. $list[$item['product_id']]['out_num']=bcadd($list[$item['product_id']]['out_num'],$item['num'],8);
  99. }
  100. }
  101. return array_values($list);
  102. }
  103. }