Merge pull request #9681 from jitendrapurohit/CRM-19884
[civicrm-core.git] / CRM / Contribute / Form / CloseAccPeriod.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27 /**
28 * This class generates form components for closing an account period.
29 */
30 class CRM_Contribute_Form_CloseAccPeriod extends CRM_Core_Form {
31
32 /**
33 * Set default values.
34 *
35 * @return array
36 */
37 public function setDefaultValues() {
38 $defaults = $period = array();
39 $period = Civi::settings()->get('closing_date');
40 if (empty($period)) {
41 $prior = CRM_Contribute_BAO_Contribution::checkContributeSettings('prior_financial_period');
42 }
43 else {
44 $defaults['closing_date'] = $period;
45 return $defaults;
46 }
47 if (!empty($prior)) {
48 $period = array(
49 'M' => date('n', strtotime($prior)),
50 'd' => date('j', strtotime($prior)),
51 );
52 if ($period['M'] == 1) {
53 $period['M'] = 12;
54 }
55 else {
56 $period['M']--;
57 }
58 $defaults['closing_date'] = $period;
59 }
60 else {
61 $defaults['closing_date'] = array(
62 'M' => date('n', strtotime("-1 month")),
63 'd' => date('j'),
64 );
65 }
66 return $defaults;
67 }
68
69 /**
70 * Build the form object.
71 */
72 public function buildQuickForm() {
73 $this->add('date', 'closing_date', ts('Accounting Period to Close'), CRM_Core_SelectValues::date(NULL, 'M d'), TRUE);
74 $confirmClose = ts('Are you sure you want to close accounting period?');
75 $this->addButtons(array(
76 array(
77 'type' => 'cancel',
78 'name' => ts('Cancel'),
79 ),
80 array(
81 'type' => 'upload',
82 'name' => ts('Close Accounting Period'),
83 'js' => array('onclick' => 'return confirm(\'' . $confirmClose . '\');'),
84 ),
85 )
86 );
87 }
88
89 /**
90 * Global form rule.
91 *
92 * @param array $fields
93 * The input form values.
94 * @param array $files
95 * The uploaded files if any.
96 * @param $self
97 *
98 */
99 public static function formRule($fields, $files, $self) {
100 }
101
102 /**
103 * Process the form submission.
104 */
105 public function postProcess() {
106 // store the submitted values in an array
107 $params = $this->controller->exportValues($this->_name);
108 // Create activity
109 $activityType = CRM_Core_OptionGroup::getValue('activity_type',
110 'Close Accounting Period',
111 'name'
112 );
113 $activityParams = array(
114 'source_contact_id' => CRM_Core_Session::singleton()->get('userID'),
115 'assignee_contact_id' => CRM_Core_Session::singleton()->get('userID'),
116 'activity_type_id' => $activityType,
117 'subject' => ts('Close Accounting Period'),
118 'status_id' => CRM_Core_OptionGroup::getValue('activity_status',
119 'Completed',
120 'name'
121 ),
122 'activity_date_time' => date('YmdHis'),
123 );
124 CRM_Activity_BAO_Activity::create($activityParams);
125 // Set Prior Financial Period
126 $priorFinPeriod = $params['closing_date']['M'] . '/' . $params['closing_date']['d'] . '/' . date('Y');
127 Civi::settings()->set('prior_financial_period', date('m/d/Y', strtotime($priorFinPeriod)));
128 // Set closing date
129 Civi::settings()->set('closing_date', $params['closing_date']);
130 CRM_Core_Session::setStatus(ts("Accounting Period has been closed successfully!"), ts('Success'), 'success');
131 }
132
133 }