123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- declare (strict_types = 1);
- namespace app\cxinv\command;
- use app\cxinv\model\FinancialManager;use app\cxinv\model\ProductFz;use app\cxinv\model\ProductOnlog;use app\cxinv\model\ProductSeal;use think\console\Command;
- use think\console\Input;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;
- use think\facade\Cache;
- class MakeSeal extends Command
- {
- protected function configure()
- {
- // 指令配置
- $this->setName('makeseal')
- ->setDescription('the makeseal command');
- }
- protected function execute(Input $input, Output $output)
- {
- if(Cache::get('makeseal')==1){
- return ;
- }
- $list = $this->GetInfo();
- while ( $list->valid()){
- Cache::set('makeseal',1,600);
- $date= date('Y-m-d H:i:s');
- $item = $list->current();
- $output->writeln("[{$date}]: {$item['company_code']}=>{$item['fz_date']} 开始执行");
- $this->makeSeal($item);
- $list->next();
- }
- Cache::set('makeseal',0);
- }
- public function GetInfo()
- {
- $info = ProductFz::where([['status','=',0]])->order("fz_date asc")->group('company_code')->cursor();
- foreach ($info as $item){
- yield $item->toArray();
- }
- }
- public function makeSeal($item){
- $lastMonth = date('Y-m',strtotime('-1 month',strtotime($item['fz_date'])));
- $ist = ProductFz::where([['fz_date','=',$lastMonth],['company_code','=',$item['company_code']]])->findOrEmpty();
- if(!$ist->isEmpty()&& $ist['status']!=2){
- return false;
- };
- $seal = ProductSeal::where([['fz_id','=',$item['id']]])->select();
- if($seal->isEmpty()){
- $lastSeal = ProductSeal::where([['fz_id','=',$ist->id],['balance_num','>',0]])->select();
- if(!$lastSeal->isEmpty()){
- (new ProductSeal)->saveAll(array_map(function ($val)use($item){
- return [ 'product_id'=>$val['product_id'],
- 'skuCode'=>$val['skuCode'],
- 'goodName'=>$val['goodName'],
- 'spec'=>$val['spec'],
- 'unit'=>$val['unit'],
- 'unit_price'=>$val['unit_price'],
- 'begin_num'=>$val['balance_num'],
- 'in_num'=>"0",
- 'out_num'=>"0",
- 'balance_num'=>$val['balance_num'],
- 'fz_id'=>$item['id'],
- ];
- },$lastSeal->toArray()));
- }
- }
- (new ProductSeal)->saveAll($this->getProductLog($item,$ist->id));
- }
- public function getProductLog($row,$lastId){
- $pastLog= ProductOnlog::withJoin(['Product'],'left')
- ->where([['fz_date','=',$row['fz_date']],['buyer_code','=',$row['company_code']]])
- ->field('type,order_item_id,product_id,skuCode,goodName,spec,unit,product_onlog.unit_price,num')->cursor();
- $list=[];
- $balance = ProductSeal::where([['fz_id','=',$lastId],['balance_num','>',0]])->column('balance_num','product_id');
- foreach ($pastLog as $item){
- if(!isset($list[$item['product_id']])){
- $list[$item['product_id']] = [
- 'id'=>ProductSeal::where([['fz_id','=',$row['id']],['product_id','=',$item['product_id']]])->value('id',null),
- 'product_id'=>$item['product_id'],
- 'skuCode'=>$item['skuCode'],
- 'goodName'=>$item['goodName'],
- 'spec'=>$item['spec'],
- 'unit'=>$item['unit'],
- 'unit_price'=>$item['unit_price'],
- 'begin_num'=>$balance[$item['product_id']]??"0",
- 'in_num'=>"0",
- 'out_num'=>"0",
- 'balance_num'=>$balance[$item['product_id']]??"0",
- 'fz_id'=>$row['id'],
- ];
- }
- if($item['type']==3|| $item['type']==1|| $item['type']==5|| $item['type']==7){
- $list[$item['product_id']]['balance_num']=bcadd($list[$item['product_id']]['balance_num'],$item['num'],8);
- $list[$item['product_id']]['in_num']=bcadd($list[$item['product_id']]['in_num'],$item['num'],8);
- }
- if($item['type']==4|| $item['type']==2|| $item['type']==6|| $item['type']==8){
- $list[$item['product_id']]['balance_num']=bcsub($list[$item['product_id']]['balance_num'],$item['num'],8);
- $list[$item['product_id']]['out_num']=bcadd($list[$item['product_id']]['out_num'],$item['num'],8);
- }
- }
- return array_values($list);
- }
- }
|