/
msSimplePayInvoice.php
127 lines (90 loc) · 5.46 KB
/
msSimplePayInvoice.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
<?php
switch ($modx->event->name) {
case 'msOnCreateOrder':
//error_reporting(E_ALL);
$year = date('Y');
$month = date('m');
$day = date('d');
$time = time();
$payment_ids = $modx->getOption('mssimplepayinvoice_payment', null, 1);
$tplWrapper = $modx->getOption('mssimplepayinvoice_tplWrapper', null, 'tpl.mssimplepayinvoiceWrapper');
$tplRow = $modx->getOption('mssimplepayinvoice_tplRow', null, 'tpl.mssimplepayinvoiceRow');
$pdfPath = $modx->getOption('mssimplepayinvoice_pdfPath', null, MODX_ASSETS_PATH.'pdf/'.$year.'/'.$month.'/');
$emailFrom = $modx->getOption('mssimplepayinvoice_emailFrom', null, $modx->getOption('emailsender'));
$nameFrom = $modx->getOption('mssimplepayinvoice_nameFrom', null, $modx->getOption('site_name'));
$mailSubject = $modx->getOption('mssimplepayinvoice_mailSubject', null, 'Pay Invoice');
$mailBody = $modx->getOption('mssimplepayinvoice_mailBody', null, 'Pay Invoice');
$products = array();
$wrapper = array();
$rows = '';
$pdoTools = $modx->getService('pdoTools'); // for using fenom style
$payment_ids_arr = explode(',', $payment_ids);
$mspPayment = $msOrder->get('payment');
if (!in_array($mspPayment, $payment_ids_arr)) return ;
// get user profile
$user_id = $msOrder->get('user_id');
$profile = $modx->getObject('modUserProfile', array('internalKey' => $user_id));
if ($profile) {
$wrapper['mspEmail'] = $profile->get('email');
}else{
return;
}
$mspId = $msOrder->get('id');
$wrapper['mspCreatedon'] = $msOrder->get('createdon');
$wrapper['mspCost'] = $msOrder->get('cost');
$wrapper['mspNum'] = $msOrder->get('num');
$wrapper['mspCart_cost'] = $msOrder->get('cart_cost');
$wrapper['mspDelivery_cost']= $msOrder->get('delivery_cost');
// user address fields
$msAddress = $msOrder->getOne('Address');
$wrapper['mspName'] = $msAddress->get('receiver');
$wrapper['mspPhone'] = $msAddress->get('phone');
$wrapper['mspIndex'] = $msAddress->get('index');
$wrapper['mspCountry'] = $msAddress->get('country');
$wrapper['mspRegion'] = $msAddress->get('region');
$wrapper['mspCity'] = $msAddress->get('city');
$wrapper['mspMetro'] = $msAddress->get('metro');
$wrapper['mspStreet'] = $msAddress->get('street');
$wrapper['mspBuilding'] = $msAddress->get('building');
$wrapper['mspRoom'] = $msAddress->get('room');
$wrapper['mspComment'] = $msAddress->get('comment');
// get order products
foreach ($msOrder->getMany('Products') as $orderProduct) {
$products['product_id'] = $orderProduct->get('product_id');
$products['name'] = $orderProduct->get('name');
$products['price'] = $orderProduct->get('price');
$products['cost'] = $orderProduct->get('cost');
$products['article'] = $orderProduct->get('article');
$products['weight'] = $orderProduct->get('weight');
$products['thumb'] = $orderProduct->get('thumb');
$products['count'] = $orderProduct->get('count');
$rows .= $pdoTools->getChunk($tplRow,$products);
}
$wrapper['rows'] = $rows;
$output .= $pdoTools->getChunk($tplWrapper,$wrapper);
// make path and pdf file
if(!@mkdir($pdfPath, 0755, true)){
$modx->log(modX::LOG_LEVEL_ERROR,'Cant create folder, check permision and full path: '.$pdfPath);
}
$pdfTitle = $day.'.'.$month.'.'.$year.'-'.str_replace('/','_',$wrapper['mspNum']);
$pdfFile = $pdfPath.$pdfTitle.'.pdf';
require_once MODX_ASSETS_PATH.'components/mssimplepayinvoice/mpdf/vendor/autoload.php';
$mpdf = new mPDF('utf-8','A4','','','10','10','10','5');
$mpdf->SetHTMLHeader('');
$mpdf->SetHTMLFooter('');
$mpdf->WriteHTML($output);
$mpdf->Output($pdfFile, 'F');
// send mail
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->mailer->AddAttachment($pdfFile);
$modx->mail->set(modMail::MAIL_BODY, $mailBody);
$modx->mail->set(modMail::MAIL_FROM, $emailFrom);
$modx->mail->set(modMail::MAIL_FROM_NAME, $nameFrom);
$modx->mail->set(modMail::MAIL_SUBJECT, $mailSubject);
$modx->mail->address('to', $wrapper['mspEmail']);
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email (plugin msSimplePayInvoice): '.$modx->mail->mailer->ErrorInfo);
}
break;
}