Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
448259d4 EM |
12 | use Civi\Api4\Batch; |
13 | ||
6a488035 | 14 | /** |
37b8953e | 15 | * This class generates form components for batch entry. |
6a488035 TO |
16 | */ |
17 | class CRM_Batch_Form_Batch extends CRM_Admin_Form { | |
18 | ||
448259d4 EM |
19 | protected $submittableMoneyFields = ['total']; |
20 | ||
37b8953e EM |
21 | /** |
22 | * PreProcess function. | |
23 | */ | |
6a488035 TO |
24 | public function preProcess() { |
25 | parent::preProcess(); | |
37b8953e | 26 | // Set the user context. |
6a488035 TO |
27 | $session = CRM_Core_Session::singleton(); |
28 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/batch', "reset=1")); | |
29 | } | |
30 | ||
31 | /** | |
eceb18cc | 32 | * Build the form object. |
448259d4 EM |
33 | * |
34 | * @throws \CRM_Core_Exception | |
6a488035 TO |
35 | */ |
36 | public function buildQuickForm() { | |
37 | parent::buildQuickForm(); | |
38 | ||
39 | if ($this->_action & CRM_Core_Action::DELETE) { | |
40 | return; | |
41 | } | |
42 | ||
43 | $this->applyFilter('__ALL__', 'trim'); | |
fc4aa398 | 44 | $attributes = CRM_Core_DAO::getAttribute('CRM_Batch_DAO_Batch'); |
6a488035 TO |
45 | $this->add('text', 'title', ts('Batch Name'), $attributes['name'], TRUE); |
46 | ||
fc4aa398 | 47 | $batchTypes = CRM_Batch_BAO_Batch::buildOptions('type_id'); |
8ef12e64 | 48 | |
6a488035 TO |
49 | $type = $this->add('select', 'type_id', ts('Type'), $batchTypes); |
50 | ||
51 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
52 | $type->freeze(); | |
53 | } | |
54 | ||
55 | $this->add('textarea', 'description', ts('Description'), $attributes['description']); | |
73708df6 | 56 | $this->add('text', 'item_count', ts('Number of Items'), $attributes['item_count'], TRUE); |
6a488035 TO |
57 | $this->add('text', 'total', ts('Total Amount'), $attributes['total'], TRUE); |
58 | } | |
59 | ||
60 | /** | |
c490a46a | 61 | * Set default values for the form. |
6a488035 | 62 | */ |
00be9182 | 63 | public function setDefaultValues() { |
be2fb01f | 64 | $defaults = []; |
6a488035 TO |
65 | |
66 | if ($this->_action & CRM_Core_Action::ADD) { | |
37b8953e | 67 | // Set batch name default. |
6a488035 TO |
68 | $defaults['title'] = CRM_Batch_BAO_Batch::generateBatchName(); |
69 | } | |
70 | else { | |
71 | $defaults = $this->_values; | |
72 | } | |
6a488035 TO |
73 | return $defaults; |
74 | } | |
75 | ||
76 | /** | |
eceb18cc | 77 | * Process the form submission. |
448259d4 EM |
78 | * |
79 | * @throws \API_Exception | |
6a488035 | 80 | */ |
448259d4 | 81 | public function postProcess(): void { |
6a488035 | 82 | if ($this->_action & CRM_Core_Action::DELETE) { |
448259d4 | 83 | CRM_Core_Session::setStatus('', ts('Batch Deleted'), 'success'); |
6a488035 TO |
84 | CRM_Batch_BAO_Batch::deleteBatch($this->_id); |
85 | return; | |
86 | } | |
87 | ||
448259d4 EM |
88 | $batchID = Batch::save(FALSE)->setRecords([ |
89 | [ | |
90 | // Always create with data entry status. | |
91 | 'status_id:name' => 'Data Entry', | |
92 | 'id' => $this->_id, | |
93 | 'title' => $this->getSubmittedValue('title'), | |
94 | 'description' => $this->getSubmittedValue('description'), | |
95 | 'type_id' => $this->getSubmittedValue('type_id'), | |
96 | 'total' => $this->getSubmittedValue('total'), | |
97 | 'item_count' => $this->getSubmittedValue('item_count'), | |
98 | ], | |
99 | ])->execute()->first()['id']; | |
100 | ||
101 | // Redirect to batch entry page. | |
481a74f4 | 102 | if ($this->_action & CRM_Core_Action::ADD) { |
448259d4 | 103 | CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/batch/entry', "id={$batchID}&reset=1&action=add")); |
6a488035 TO |
104 | } |
105 | else { | |
448259d4 | 106 | CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/batch/entry', "id={$batchID}&reset=1")); |
6a488035 TO |
107 | } |
108 | } | |
96025800 | 109 | |
6a488035 | 110 | } |