MakeSeal.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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")->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. (new ProductSeal)->saveAll($this->getProductLog($item,$ist->id));
  48. }
  49. public function getProductLog($row,$lastId){
  50. $pastLog= ProductOnlog::withJoin(['Product'],'left')
  51. ->where([['fz_date','=',$row['fz_date']],['buyer_code','=',$row['company_code']]])
  52. ->field('type,order_item_id,product_id,skuCode,goodName,spec,unit,product_onlog.unit_price,num')->cursor();
  53. $list=[];
  54. $balance = ProductSeal::where([['fz_id','=',$lastId],['balance_num','>',0]])->column('balance_num','product_id');
  55. foreach ($pastLog as $item){
  56. if(!isset($list[$item['product_id']])){
  57. $list[$item['product_id']] = [
  58. 'id'=>ProductSeal::where([['fz_id','=',$row['id']],['product_id','=',$item['product_id']]])->value('id',null),
  59. 'product_id'=>$item['product_id'],
  60. 'skuCode'=>$item['skuCode'],
  61. 'goodName'=>$item['goodName'],
  62. 'spec'=>$item['spec'],
  63. 'unit'=>$item['unit'],
  64. 'unit_price'=>$item['unit_price'],
  65. 'begin_num'=>$balance[$item['product_id']]??"0",
  66. 'in_num'=>"0",
  67. 'out_num'=>"0",
  68. 'balance_num'=>$balance[$item['product_id']]??"0",
  69. 'fz_id'=>$row['id'],
  70. ];
  71. }
  72. if($item['type']==2){
  73. $manager= FinancialManager::where([['id','=',$item['order_item_id']]])->findOrEmpty();
  74. $date = date('Y-m-d H:i:s');
  75. if($manager->isEmpty()|| $manager['status']==1){
  76. echo "[{$date}]:【{$item['order_item_id']}】 采购单不存在或未处理".PHP_EOL;
  77. continue;
  78. }
  79. }
  80. if($item['type']==3|| $item['type']==1){
  81. $list[$item['product_id']]['balance_num']=bcadd($list[$item['product_id']]['balance_num'],$item['num'],8);
  82. $list[$item['product_id']]['in_num']=bcadd($list[$item['product_id']]['in_num'],$item['num'],8);
  83. }
  84. if($item['type']==4|| $item['type']==2){
  85. $list[$item['product_id']]['balance_num']=bcsub($list[$item['product_id']]['balance_num'],$item['num'],8);
  86. $list[$item['product_id']]['out_num']=bcadd($list[$item['product_id']]['out_num'],$item['num'],8);
  87. }
  88. }
  89. return array_values($list);
  90. }
  91. }