MakeSeal.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. class MakeSeal extends Command
  10. {
  11. protected function configure()
  12. {
  13. // 指令配置
  14. $this->setName('makeseal')
  15. ->setDescription('the makeseal command');
  16. }
  17. protected function execute(Input $input, Output $output)
  18. {
  19. $list = $this->GetInfo();
  20. while ( $list->valid()){
  21. $item = $list->current();
  22. $this->makeSeal($item);
  23. $list->next();
  24. }
  25. }
  26. public function GetInfo()
  27. {
  28. $info = ProductFz::where([['status','=',1]])->order("fz_date asc")->cursor();
  29. foreach ($info as $item){
  30. yield $item->toArray();
  31. }
  32. }
  33. public function makeSeal($item){
  34. var_dump($item);
  35. $lastMonth = date('Y-m',strtotime('-1 month',strtotime($item['fz_date'])));
  36. $ist = ProductFz::where([['fz_date','=',$lastMonth],['company_code','=',$item['company_code']]])->findOrEmpty();
  37. if(!$ist->isEmpty()&& $ist['status']!=2){
  38. $item['status']=3;
  39. $item['remark']=$lastMonth.'未封账';
  40. (new ProductFz)->save($item);
  41. return;
  42. };
  43. $mamger= FinancialManager::where(function($query) use($item){
  44. $query->whereOr([[['buyer_code','=',$item['company_code']],['type','in',[1,3]]],[['seller_code','=',$item['company_code']],['type','in',[2,4]]]]);
  45. })->where(["status"=>1,"fz_date"=>$item['fz_date']])->findOrEmpty();
  46. if(!$mamger->isEmpty()){
  47. $item['status']=3;
  48. $item['remark']="出入库【{$mamger['id']}】未处理";
  49. (new ProductFz)->save($item);
  50. return;
  51. }
  52. (new ProductSeal)->saveAll($this->getProductLog($item,$ist->id));
  53. $item['status']=2;
  54. $item['remark']='';
  55. (new ProductFz)->save($item);
  56. }
  57. public function getProductLog($item,$lastId){
  58. $pastLog= ProductOnlog::withJoin(['Product'],'left')
  59. ->where([['fz_date','=',$item['fz_date']],['company_code','=',$item['company_code']]])
  60. ->field('type,product_id,skuCode,goodName,spec,unit,unit_price,num')->cursor();
  61. $list=[];
  62. $balance = ProductSeal::where([['fz_id','=',$lastId],['balance_num','>',0]])->column('balance_num','product_id');
  63. foreach ($pastLog as $item){
  64. if(!isset($list[$item['product_id']])){
  65. $list[$item['product_id']] = [
  66. 'product_id'=>$item['product_id'],
  67. 'skuCode'=>$item['skuCode'],
  68. 'goodName'=>$item['goodName'],
  69. 'spec'=>$item['spec'],
  70. 'unit'=>$item['unit'],
  71. 'unit_price'=>$item['unit_price'],
  72. 'begin_num'=>$balance[$item['product_id']]??"0",
  73. 'in_num'=>"0",
  74. 'out_num'=>"0",
  75. 'balance_num'=>$balance[$item['product_id']]??"0",
  76. 'fz_id'=>$item['fz_id'],
  77. ];
  78. }
  79. if($item['type']==3|| $item['type']==1){
  80. $list[$item['product_id']]['balance_num']=bcadd($list[$item['product_id']]['balance_num'],$item['num'],8);
  81. $list[$item['product_id']]['in_num']=bcadd($list[$item['product_id']]['in_num'],$item['num'],8);
  82. }
  83. if($item['type']==4|| $item['type']==2){
  84. $list[$item['product_id']]['balance_num']=bcsub($list[$item['product_id']]['balance_num'],$item['num'],8);
  85. $list[$item['product_id']]['out_num']=bcadd($list[$item['product_id']]['out_num'],$item['num'],8);
  86. }
  87. }
  88. return array_values($list);
  89. }
  90. }