source: main/waeup.ikoba/trunk/src/waeup/ikoba/customers/viewlets.py @ 12992

Last change on this file since 12992 was 12772, checked in by Henrik Bettermann, 10 years ago

Prepare contract payment receipt.

  • Property svn:keywords set to Id
File size: 13.8 KB
Line 
1## $Id: viewlets.py 12772 2015-03-16 10:02:14Z henrik $
2##
3## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18
19import grok
20from zope.i18n import translate
21from zope.interface import Interface
22from zope.component import getUtility
23from waeup.ikoba.interfaces import IIkobaObject
24from waeup.ikoba.interfaces import MessageFactory as _
25from waeup.ikoba.browser.viewlets import (
26    PrimaryNavTab, ManageActionButton, AddActionButton)
27from waeup.ikoba.browser.layout import default_primary_nav_template
28from waeup.ikoba.customers.interfaces import (
29    ICustomer, ICustomersContainer,
30    ICustomerDocumentsContainer, ICustomerDocument,
31    IContractsContainer, IContract, ICustomersUtils,
32    )
33from waeup.ikoba.customers.browser import (
34    CustomersContainerPage, CustomersContainerManagePage,
35    CustomerBaseDisplayFormPage,
36    DocumentsManageFormPage, DocumentDisplayFormPage, DocumentManageFormPage,
37    ContractsFormPage, ContractDisplayFormPage,
38    ContractManageFormPage,
39    ContractOfficialUsePage)
40from waeup.ikoba.payments.payment import get_payments_from_payable_id
41from waeup.ikoba.payments.interfaces import STATE_PAID
42
43grok.context(IIkobaObject)  # Make IIkobaObject the default context
44grok.templatedir('browser_templates')
45
46
47class CustomersTab(PrimaryNavTab):
48    """Customers tab in primary navigation.
49    """
50
51    grok.context(IIkobaObject)
52    grok.order(4)
53    grok.require('waeup.viewCustomersTab')
54    grok.name('customerstab')
55
56    pnav = 4
57    tab_title = _(u'Customers')
58
59    @property
60    def link_target(self):
61        return self.view.application_url('customers')
62
63
64class PrimaryCustomerNavManager(grok.ViewletManager):
65    """Viewlet manager for the primary navigation tab.
66    """
67    grok.name('primary_nav_customer')
68
69
70class PrimaryCustomerNavTab(grok.Viewlet):
71    """Base for primary customer nav tabs.
72    """
73    grok.baseclass()
74    grok.viewletmanager(PrimaryCustomerNavManager)
75    template = default_primary_nav_template
76    grok.order(1)
77    grok.require('waeup.Authenticated')
78    pnav = 0
79    tab_title = u'Some Text'
80
81    @property
82    def link_target(self):
83        return self.view.application_url()
84
85    @property
86    def active(self):
87        view_pnav = getattr(self.view, 'pnav', 0)
88        if view_pnav == self.pnav:
89            return 'active'
90        return ''
91
92
93class MyCustomerDataTab(PrimaryCustomerNavTab):
94    """MyData dropdown tab in primary navigation.
95    """
96    grok.order(3)
97    grok.require('waeup.viewMyCustomerDataTab')
98    grok.template('mydatadropdowntabs')
99    grok.name('mycustomerdatatab')
100    pnav = 4
101    tab_title = _(u'My Data')
102
103    @property
104    def active(self):
105        view_pnav = getattr(self.view, 'pnav', 0)
106        if view_pnav == self.pnav:
107            return 'active dropdown'
108        return 'dropdown'
109
110    @property
111    def targets(self):
112        customer = grok.getSite()['customers'][self.request.principal.id]
113        customer_url = self.view.url(customer)
114        targets = []
115        targets += [
116            {'url':customer_url, 'title':'Base Data'},
117            {'url':customer_url + '/contracts', 'title':_('My Contracts')},
118            {'url':customer_url + '/documents', 'title':_('My Documents')},
119            {'url':customer_url + '/history', 'title':_('History')},
120            ]
121        return targets
122
123
124class CustomerManageSidebar(grok.ViewletManager):
125    grok.name('left_customermanage')
126
127
128class CustomerManageLink(grok.Viewlet):
129    """A link displayed in the customer box which shows up for CustomerNavigation
130    objects.
131
132    """
133    grok.baseclass()
134    grok.viewletmanager(CustomerManageSidebar)
135    grok.view(Interface)
136    grok.order(5)
137    grok.require('waeup.viewCustomer')
138
139    link = 'index'
140    text = _(u'Base Data')
141
142    def render(self):
143        url = self.view.url(self.context.customer, self.link)
144        # Here we know that the cookie has been set
145        lang = self.request.cookies.get('ikoba.language')
146        text = translate(self.text, 'waeup.ikoba',
147            target_language=lang)
148        if not self.link:
149            return ''
150        return u'<li><a href="%s">%s</a></li>' % (
151                url, text)
152
153
154class CustomerManageBaseLink(CustomerManageLink):
155    grok.order(2)
156    link = 'index'
157    text = _(u'Base Data')
158
159
160class CustomerManageContractsLink(CustomerManageLink):
161    grok.order(3)
162    link = 'contracts'
163    text = _(u'Contracts')
164
165
166class CustomerManageDocumentsLink(CustomerManageLink):
167    grok.order(4)
168    link = 'documents'
169    text = _(u'Documents')
170
171
172class CustomerManagePaymentsLink(CustomerManageLink):
173    grok.order(5)
174    link = 'payments'
175    text = _(u'Payments')
176
177
178class CustomerManageHistoryLink(CustomerManageLink):
179    grok.order(8)
180    link = 'history'
181    text = _(u'History')
182
183
184class CustomersContainerManageActionButton(ManageActionButton):
185    grok.order(1)
186    grok.context(ICustomersContainer)
187    grok.view(CustomersContainerPage)
188    grok.require('waeup.manageCustomer')
189    text = _('Manage customer section')
190
191
192class CustomersContainerAddActionButton(AddActionButton):
193    grok.order(1)
194    grok.context(ICustomersContainer)
195    grok.view(CustomersContainerManagePage)
196    grok.require('waeup.manageCustomer')
197    text = _('Add customer')
198    target = 'addcustomer'
199
200
201class ContactActionButton(ManageActionButton):
202    grok.order(5)
203    grok.context(ICustomer)
204    grok.view(CustomerBaseDisplayFormPage)
205    grok.require('waeup.manageCustomer')
206    icon = 'actionicon_mail.png'
207    text = _('Send email')
208    target = 'contactcustomer'
209
210
211class CustomerBaseManageActionButton(ManageActionButton):
212    grok.order(1)
213    grok.context(ICustomer)
214    grok.view(CustomerBaseDisplayFormPage)
215    grok.require('waeup.manageCustomer')
216    text = _('Manage')
217    target = 'manage_base'
218
219
220class CustomerTrigTransActionButton(ManageActionButton):
221    grok.order(2)
222    grok.context(ICustomer)
223    grok.view(CustomerBaseDisplayFormPage)
224    grok.require('waeup.triggerTransition')
225    icon = 'actionicon_trigtrans.png'
226    text = _(u'Transition')
227    target = 'trigtrans'
228
229
230class CustomerLoginAsActionButton(ManageActionButton):
231    grok.order(3)
232    grok.context(ICustomer)
233    grok.view(CustomerBaseDisplayFormPage)
234    grok.require('waeup.loginAsCustomer')
235    icon = 'actionicon_mask.png'
236    text = _(u'Login as customer')
237    target = 'loginasstep1'
238
239
240class CustomerDeactivateActionButton(ManageActionButton):
241    grok.order(7)
242    grok.context(ICustomer)
243    grok.view(CustomerBaseDisplayFormPage)
244    grok.require('waeup.manageCustomer')
245    text = _('Deactivate account')
246    target = 'deactivate'
247    icon = 'actionicon_traffic_lights_red.png'
248
249    @property
250    def target_url(self):
251        if self.context.suspended:
252            return ''
253        return self.view.url(self.view.context, self.target)
254
255    @property
256    def onclick(self):
257        return "return window.confirm(%s);" % _(
258            "'A history message will be added. Are you sure?'")
259
260
261class CustomerActivateActionButton(ManageActionButton):
262    grok.order(7)
263    grok.context(ICustomer)
264    grok.view(CustomerBaseDisplayFormPage)
265    grok.require('waeup.manageCustomer')
266    text = _('Activate account')
267    target = 'activate'
268    icon = 'actionicon_traffic_lights_green.png'
269
270    @property
271    def target_url(self):
272        if not self.context.suspended:
273            return ''
274        return self.view.url(self.view.context, self.target)
275
276    @property
277    def onclick(self):
278        return "return window.confirm(%s);" % _(
279            "'A history message will be added. Are you sure?'")
280
281
282class CustomerBaseActionButton(ManageActionButton):
283    grok.order(1)
284    grok.context(ICustomer)
285    grok.view(CustomerBaseDisplayFormPage)
286    grok.require('waeup.handleCustomer')
287    target = 'edit_base'
288
289    @property
290    def text(self):
291        if self.view.is_requestable:
292            return _('Edit and request registration')
293        return _('Edit')
294
295
296class CustomerPasswordActionButton(ManageActionButton):
297    grok.order(2)
298    grok.context(ICustomer)
299    grok.view(CustomerBaseDisplayFormPage)
300    grok.require('waeup.handleCustomer')
301    icon = 'actionicon_key.png'
302    text = _('Change password')
303    target = 'changepassword'
304
305
306class CustomerPassportActionButton(ManageActionButton):
307    grok.order(3)
308    grok.context(ICustomer)
309    grok.view(CustomerBaseDisplayFormPage)
310    grok.require('waeup.handleCustomer')
311    icon = 'actionicon_up.png'
312    text = _('Upload files')
313    target = 'upload_files'
314
315    @property
316    def target_url(self):
317        CUSTMANAGE_STATES = getUtility(
318            ICustomersUtils).CUSTMANAGE_CUSTOMER_STATES
319        if self.context.state not in CUSTMANAGE_STATES:
320            return ''
321        return self.view.url(self.view.context, self.target)
322
323
324# Viewlets for customer documents
325
326class PDFDocumentOverviewActionButton(ManageActionButton):
327    grok.order(2)
328    grok.context(ICustomerDocumentsContainer)
329    grok.view(DocumentsManageFormPage)
330    grok.require('waeup.viewCustomer')
331    icon = 'actionicon_pdf.png'
332    text = _('Download documents overview')
333    target = 'documents_overview_slip.pdf'
334
335
336class DocumentManageActionButton(ManageActionButton):
337    grok.order(1)
338    grok.context(ICustomerDocument)
339    grok.view(DocumentDisplayFormPage)
340    grok.require('waeup.manageCustomer')
341    text = _('Manage')
342    target = 'manage'
343
344    @property
345    def target_url(self):
346        if not self.context.is_editable_by_manager:
347            return ''
348        return self.view.url(self.view.context, self.target)
349
350
351class DocumentEditActionButton(ManageActionButton):
352    grok.order(1)
353    grok.context(ICustomerDocument)
354    grok.view(DocumentDisplayFormPage)
355    grok.require('waeup.handleCustomer')
356    text = _('Edit')
357    target = 'edit'
358
359    @property
360    def target_url(self):
361        if not self.context.is_editable_by_customer:
362            return ''
363        return self.view.url(self.view.context, self.target)
364
365
366class DocumentTrigTransActionButton(ManageActionButton):
367    grok.order(2)
368    grok.context(ICustomerDocument)
369    grok.view(DocumentDisplayFormPage)
370    grok.require('waeup.triggerTransition')
371    icon = 'actionicon_trigtrans.png'
372    text = _(u'Transition')
373    target = 'trigtrans'
374
375
376class DocumentViewActionButton(ManageActionButton):
377    grok.order(1)
378    grok.context(ICustomerDocument)
379    grok.view(DocumentManageFormPage)
380    grok.require('waeup.handleCustomer')
381    text = _('View')
382    target = 'index'
383    icon = 'actionicon_view.png'
384
385
386class PDFDocumentSlipActionButton(ManageActionButton):
387    grok.order(3)
388    grok.context(ICustomerDocument)
389    grok.view(DocumentDisplayFormPage)
390    grok.require('waeup.viewCustomer')
391    icon = 'actionicon_pdf.png'
392    text = _('Download document slip')
393    target = 'document_slip.pdf'
394
395
396# Viewlets for customer contracts
397
398class PDFContractOverviewActionButton(ManageActionButton):
399    grok.order(2)
400    grok.context(IContractsContainer)
401    grok.view(ContractsFormPage)
402    grok.require('waeup.viewCustomer')
403    icon = 'actionicon_pdf.png'
404    text = _('Download contracts overview')
405    target = 'contracts_overview_slip.pdf'
406
407
408class ContractManageActionButton(ManageActionButton):
409    grok.order(1)
410    grok.context(IContract)
411    grok.view(ContractDisplayFormPage)
412    grok.require('waeup.manageCustomer')
413    text = _('Manage')
414    target = 'manage'
415
416
417class ContractOUActionButton(ManageActionButton):
418    grok.order(2)
419    grok.context(IContract)
420    grok.view(ContractDisplayFormPage)
421    grok.require('waeup.manageCustomer')
422    text = _('Manage official data')
423    target = 'official_use'
424
425
426class ContractEditActionButton(ManageActionButton):
427    grok.order(1)
428    grok.context(IContract)
429    grok.view(ContractDisplayFormPage)
430    grok.require('waeup.handleCustomer')
431    text = _('Edit')
432    target = 'edit'
433
434    @property
435    def target_url(self):
436        if not self.context.is_editable:
437            return ''
438        return self.view.url(self.view.context, self.target)
439
440
441class ContractTrigTransActionButton(ManageActionButton):
442    grok.order(3)
443    grok.context(IContract)
444    grok.view(ContractDisplayFormPage)
445    grok.require('waeup.triggerTransition')
446    icon = 'actionicon_trigtrans.png'
447    text = _(u'Transition')
448    target = 'trigtrans'
449
450
451class ContractViewActionButton(ManageActionButton):
452    grok.order(1)
453    grok.context(IContract)
454    grok.view(ContractManageFormPage)
455    grok.require('waeup.handleCustomer')
456    text = _('View')
457    target = 'index'
458    icon = 'actionicon_view.png'
459
460
461class ContractViewActionButton2(ContractViewActionButton):
462    grok.view(ContractOfficialUsePage)
463
464
465class PDFContractSlipActionButton(ManageActionButton):
466    grok.order(4)
467    grok.context(IContract)
468    grok.view(ContractDisplayFormPage)
469    grok.require('waeup.viewCustomer')
470    icon = 'actionicon_pdf.png'
471    text = _('Download contract slip')
472    target = 'contract_slip.pdf'
473
474class PDFContractReceiptActionButton(ManageActionButton):
475    grok.order(5)
476    grok.context(IContract)
477    grok.view(ContractDisplayFormPage)
478    grok.require('waeup.viewCustomer')
479    icon = 'actionicon_pdf.png'
480    text = _('Download payment receipt')
481    target = 'contract_payment_receipt.pdf'
482
483    @property
484    def target_url(self):
485        payments = get_payments_from_payable_id(self.context.contract_id)
486        if payments is not None:
487            for payment in payments:
488                if payment.state == STATE_PAID:
489                     return self.view.url(self.view.context, self.target)
490        return ''
491       
Note: See TracBrowser for help on using the repository browser.