-
Notifications
You must be signed in to change notification settings - Fork 30
/
UninvoicedDomainController.php
157 lines (130 loc) · 4.55 KB
/
UninvoicedDomainController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace OpenProvider\WhmcsDomainAddon\Controllers\Admin;
use Carbon\Carbon;
use OpenProvider\WhmcsDomainAddon\Models\Domain;
use OpenProvider\WhmcsDomainAddon\Models\Invoice;
/**
* Client controller dispatcher.
*/
class UninvoicedDomainController{
/**
* Show an index with all the domains.
*
* @return string
*/
public function index()
{
$domain = new Domain();
$domains = $domain->possiblyUninvoicedDomains('paginate');
$notice = [];
if(isset($_REQUEST['id']) && $_GET['ignoreStatus'] == 'success')
{
try
{
$domain = Domain::findOrFail($_REQUEST['id']);
$notice =[
'notice' => 'Ignored the domain "'.$domain->domain.'" with ID "'.$domain->id.'"',
'noticeType' => 'success'
];
}
catch (\Exception $e)
{
// Do nothing.
}
}
return wView('UninvoicedDomain/index', array_merge([
'domains' => $domains['items'],
'pagination' => $domains['pagination'],
'route' => 'uninvoicedDomainsIndex'
], $notice));
}
/**
* Create invoice for a domain.
*
* @return string
*/
public function createInvoiceForm()
{
if(!isset($_REQUEST['id']))
throw new \Exception('NOT FOUND');
$domain = Domain::findOrFail($_REQUEST['id']);
return wView('UninvoicedDomain/createInvoiceForm',
['domain' => $domain]);
}
/**
* Create invoice for a domain.
*
* @return string
*/
public function createInvoice()
{
if(!isset($_REQUEST['id']) || !isset($_POST['description']) || !isset($_POST['amount']))
throw new \Exception('NOT FOUND');
$domain = Domain::findOrFail($_REQUEST['id']);
$difference_invoices = $domain->getExpectedInvoices() - $domain->getFoundInvoices();
$correctionInvoices = ceil($difference_invoices) - 1;
$_POST['amount'] = (float) $_POST['amount'];
if(!is_float($_POST['amount']))
{
$status = 'error';
$error = 'Pricing format is not acceptable.';
return wView('UninvoicedDomain/createInvoiceForm',
['domain' => $domain,
'error' => $error]);
}
// Get the offset date
$command = 'GetConfigurationValue';
$postData = array(
'setting' => 'CreateInvoiceDaysBefore',
);
$offset = localAPI($command, $postData)['value'];
$carbon = new Carbon();
$carbon->now();
$currentDate = $carbon->toDateString();
$dueDate = $carbon->addDays($offset)->toDateString();
$command = 'CreateInvoice';
$postData = array(
'userid' => $domain->userid,
'status' => 'Unpaid',
'sendinvoice' => '1',
'date' => $currentDate,
'duedate' => $dueDate,
'itemdescription1' => $_POST['description'],
'itemamount1' => $_POST['amount'] ,
'itemtaxed1' => '1',
);
$results = localAPI($command, $postData);
$invoice = new Invoice();
$invoice = $invoice->find($results['invoiceid']);
$result = $invoice->setRelidAndType($domain->id, 'DomainCorrection');
$result = $invoice->setCorrectionInvoices($correctionInvoices);
// @todo Validation
// @todo
return wView('UninvoicedDomain/createInvoiceForm',
['domain' => $domain,
'invoice' => $invoice]);
}
/**
* Ignore a domain.
*
* @return string
*/
public function ignoreDomain()
{
if(!isset($_REQUEST['id']))
throw new \Exception('NOT FOUND');
$domain = Domain::findOrFail($_REQUEST['id']);
$difference_invoices = $domain->getExpectedInvoices() - $domain->getFoundInvoices() + $domain->op_correctioninvoices;
$difference_invoices = $this->round_up($difference_invoices, 4);
$domain->op_correctioninvoices = $difference_invoices;
$domain->save();
header("Location: addonmodules.php?module=openprovider&action=uninvoicedDomainsIndex&id=".$domain->id."&ignoreStatus=success");
exit;
}
function round_up($number, $precision = 2)
{
$precision++;
$fig = (int) str_pad('1', $precision, '0');
return (ceil($number * $fig) / $fig);
}
}