1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainConsignmentBundle\Command; |
4
|
|
|
|
5
|
|
|
use Loevgaard\DandomainConsignmentBundle\ConsignmentService\ConsignmentServiceCollection; |
6
|
|
|
use Loevgaard\DandomainFoundation\Repository\ManufacturerRepository; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
8
|
|
|
use Symfony\Component\Console\Helper\Table; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
15
|
|
|
|
16
|
|
|
class ReportCommand extends ContainerAwareCommand |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ManufacturerRepository |
20
|
|
|
*/ |
21
|
|
|
protected $manufacturerRepository; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ConsignmentServiceCollection |
25
|
|
|
*/ |
26
|
|
|
protected $consignmentServiceCollection; |
27
|
|
|
|
28
|
|
|
public function __construct(ManufacturerRepository $manufacturerRepository, ConsignmentServiceCollection $consignmentServiceCollection) |
29
|
|
|
{ |
30
|
|
|
$this->manufacturerRepository = $manufacturerRepository; |
31
|
|
|
$this->consignmentServiceCollection = $consignmentServiceCollection; |
32
|
|
|
|
33
|
|
|
parent::__construct(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->setName('loevgaard:dandomain-consignment:report') |
40
|
|
|
->setDescription('Generates a report and optionally delivers it to the given manufacturer') |
41
|
|
|
->addArgument('manufacturer', InputArgument::REQUIRED, 'The manufacturer to generate a report for. Use the id from Dandomain') |
42
|
|
|
->addOption('deliver', null, InputOption::VALUE_REQUIRED, 'If set the command will deliver the report. Default: true', true) |
43
|
|
|
->addOption('start', null, InputOption::VALUE_REQUIRED, 'The start date in the format `YYYY-MM-DD`') |
44
|
|
|
->addOption('end', null, InputOption::VALUE_REQUIRED, 'The end date in the format `YYYY-MM-DD`') |
45
|
|
|
->addOption('updateLastStockMovement', null, InputOption::VALUE_REQUIRED, 'If set, the command will update the last stock movement property for the manufacturer', true) |
46
|
|
|
->addOption('useLastStockMovement', null, InputOption::VALUE_REQUIRED, 'If set, the command will use the last stock movement as the starting point when generating the report', true) |
47
|
|
|
; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param InputInterface $input |
52
|
|
|
* @param OutputInterface $output |
53
|
|
|
* @return int|null|void |
54
|
|
|
* @throws \Loevgaard\DandomainConsignmentBundle\Exception\NonExistentConsignmentServiceException |
55
|
|
|
*/ |
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
57
|
|
|
{ |
58
|
|
|
// fetch arguments and options |
59
|
|
|
$manufacturer = $input->getArgument('manufacturer'); |
60
|
|
|
$deliver = $input->getOption('deliver') === true || $input->getOption('deliver') === 'true'; |
61
|
|
|
$updateLastStockMovement = $input->getOption('updateLastStockMovement') === true || $input->getOption('updateLastStockMovement') === 'true'; |
62
|
|
|
$useLastStockMovement = $input->getOption('useLastStockMovement') === true || $input->getOption('useLastStockMovement') === 'true'; |
63
|
|
|
$start = $input->getOption('start'); |
64
|
|
|
$end = $input->getOption('end'); |
65
|
|
|
|
66
|
|
|
// validate dates |
67
|
|
View Code Duplication |
if($start) { |
|
|
|
|
68
|
|
|
$start = \DateTime::createFromFormat('Y-m-d', $start); |
69
|
|
|
if ($start === false) { |
70
|
|
|
throw new \InvalidArgumentException('The format for start is invalid'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
if($end) { |
|
|
|
|
75
|
|
|
$end = \DateTime::createFromFormat('Y-m-d', $end); |
76
|
|
|
if ($end === false) { |
77
|
|
|
throw new \InvalidArgumentException('The format for end is invalid'); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// find manufacturer |
82
|
|
|
$manufacturer = $this->manufacturerRepository->findOneByExternalId($manufacturer); |
83
|
|
|
|
84
|
|
|
if(!$manufacturer) { |
85
|
|
|
throw new \InvalidArgumentException('The manufacturer does not exist'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// check if the manufacturer is enabled for consignment |
89
|
|
|
if(!$manufacturer->isConsignment()) { |
90
|
|
|
throw new \InvalidArgumentException('Consignment is not enabled for the manufacturer'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if($input->isInteractive()) { |
94
|
|
|
// output config |
95
|
|
|
$table = new Table($output); |
96
|
|
|
$table |
97
|
|
|
->setHeaders(['Option', 'Value']) |
98
|
|
|
->setRows([ |
99
|
|
|
['Manufacturer', $manufacturer->getName()], |
100
|
|
|
['Delivery', $deliver ? 'Yes' : 'No'], |
101
|
|
|
['Update last stock movement', $updateLastStockMovement ? 'Yes' : 'No'], |
102
|
|
|
['Use last stock movement', $useLastStockMovement ? 'Yes' : 'No'], |
103
|
|
|
['Start date', $start ? $start->format('Y-m-d') : 'None'], |
104
|
|
|
['End date', $end ? $end->format('Y-m-d') : 'None'], |
105
|
|
|
]); |
106
|
|
|
$table->render(); |
107
|
|
|
|
108
|
|
|
// confirm config |
109
|
|
|
$helper = $this->getHelper('question'); |
110
|
|
|
$question = new ConfirmationQuestion('Continue with this config? ', false); |
111
|
|
|
|
112
|
|
|
if (!$helper->ask($input, $output, $question)) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// find the consignment service |
118
|
|
|
$consignmentService = $this->consignmentServiceCollection->findConsignmentService($manufacturer); |
119
|
|
|
$consignmentService->setLogger(new ConsoleLogger($output)); |
120
|
|
|
|
121
|
|
|
// generate the report |
122
|
|
|
$report = $consignmentService->generateReport([ |
123
|
|
|
'update_last_stock_movement' => $updateLastStockMovement, |
124
|
|
|
'use_last_stock_movement' => $useLastStockMovement, |
125
|
|
|
'start_date' => $start, |
126
|
|
|
'end_date' => $end |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
// generate report file because we want to generate the file no matter if the $deliver option is set |
130
|
|
|
$consignmentService->generateReportFile($report); |
131
|
|
|
|
132
|
|
|
// deliver report |
133
|
|
|
if($deliver) { |
134
|
|
|
$consignmentService->deliverReport($report); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.