forked from treeio/treeio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
975 lines (799 loc) · 42.4 KB
/
forms.py
File metadata and controls
975 lines (799 loc) · 42.4 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# encoding: utf-8
# Copyright 2011 Tree.io Limited
# This file is part of Treeio.
# License www.tree.io/license
# -*- coding: utf-8 -*-
"""
Sales module forms
"""
from django.shortcuts import get_object_or_404
from django import forms
from django.db.models import Q
from treeio.sales.models import Product, SaleOrder, SaleSource, Lead, Opportunity, \
SaleStatus, OrderedProduct, Subscription, Currency
from treeio.finance.models import Transaction
from treeio.identities.models import Contact
from treeio.core.models import Object, ModuleSetting, User, UpdateRecord
from django.core.urlresolvers import reverse
from treeio.core.decorators import preprocess_form
from django.utils.translation import ugettext as _
preprocess_form()
standard_currencies = (
("AED","AED United Arab Emirates, Dirhams"),
("AFN","AFN Afghanistan, Afghanis"),
("ALL","ALL Albania, Leke"),
("AMD","AMD Armenia, Drams"),
("ANG","ANG Netherlands Antilles, Guilders (also called Florins)"),
("AOA","AOA Angola, Kwanza"),
("ARS","ARS Argentina, Pesos"),
("AUD","AUD Australia, Dollars"),
("AWG","AWG Aruba, Guilders (also called Florins)"),
("AZN","AZN Azerbaijan, New Manats"),
("BAM","BAM Bosnia and Herzegovina, Convertible Marka"),
("BBD","BBD Barbados, Dollars"),
("BDT","BDT Bangladesh, Taka"),
("BGN","BGN Bulgaria, Leva"),
("BHD","BHD Bahrain, Dinars"),
("BIF","BIF Burundi, Francs"),
("BMD","BMD Bermuda, Dollars"),
("BND","BND Brunei Darussalam, Dollars"),
("BOB","BOB Bolivia, Bolivianos"),
("BRL","BRL Brazil, Brazil Real"),
("BSD","BSD Bahamas, Dollars"),
("BTN","BTN Bhutan, Ngultrum"),
("BWP","BWP Botswana, Pulas"),
("BYR","BYR Belarus, Rubles"),
("BZD","BZD Belize, Dollars"),
("CAD","CAD Canada, Dollars"),
("CDF","CDF Congo/Kinshasa, Congolese Francs"),
("CHF","CHF Switzerland, Francs"),
("CLP","CLP Chile, Pesos"),
("CNY","CNY China, Yuan Renminbi"),
("COP","COP Colombia, Pesos"),
("CRC","CRC Costa Rica, Colones"),
("CUP","CUP Cuba, Pesos"),
("CVE","CVE Cape Verde, Escudos"),
("CZK","CZK Czech Republic, Koruny"),
("DJF","DJF Djibouti, Francs"),
("DKK","DKK Denmark, Kroner"),
("DOP","DOP Dominican Republic, Pesos"),
("DZD","DZD Algeria, Algeria Dinars"),
("EGP","EGP Egypt, Pounds"),
("ERN","ERN Eritrea, Nakfa"),
("ETB","ETB Ethiopia, Birr"),
("EUR","EUR Euro Member Countries, Euro"),
("FJD","FJD Fiji, Dollars"),
("FKP","FKP Falkland Islands (Malvinas), Pounds"),
("GBP","GBP United Kingdom, Pounds"),
("GEL","GEL Georgia, Lari"),
("GGP","GGP Guernsey, Pounds"),
("GHS","GHS Ghana, Cedis"),
("GIP","GIP Gibraltar, Pounds"),
("GMD","GMD Gambia, Dalasi"),
("GNF","GNF Guinea, Francs"),
("GTQ","GTQ Guatemala, Quetzales"),
("GYD","GYD Guyana, Dollars"),
("HKD","HKD Hong Kong, Dollars"),
("HNL","HNL Honduras, Lempiras"),
("HRK","HRK Croatia, Kuna"),
("HTG","HTG Haiti, Gourdes"),
("HUF","HUF Hungary, Forint"),
("IDR","IDR Indonesia, Rupiahs"),
("ILS","ILS Israel, New Shekels"),
("IMP","IMP Isle of Man, Pounds"),
("INR","INR India, Rupees"),
("IQD","IQD Iraq, Dinars"),
("IRR","IRR Iran, Rials"),
("ISK","ISK Iceland, Kronur"),
("JEP","JEP Jersey, Pounds"),
("JMD","JMD Jamaica, Dollars"),
("JOD","JOD Jordan, Dinars"),
("JPY","JPY Japan, Yen"),
("KES","KES Kenya, Shillings"),
("KGS","KGS Kyrgyzstan, Soms"),
("KHR","KHR Cambodia, Riels"),
("KMF","KMF Comoros, Francs"),
("KPW","KPW Korea (North), Won"),
("KRW","KRW Korea (South), Won"),
("KWD","KWD Kuwait, Dinars"),
("KYD","KYD Cayman Islands, Dollars"),
("KZT","KZT Kazakhstan, Tenge"),
("LAK","LAK Laos, Kips"),
("LBP","LBP Lebanon, Pounds"),
("LKR","LKR Sri Lanka, Rupees"),
("LRD","LRD Liberia, Dollars"),
("LSL","LSL Lesotho, Maloti"),
("LTL","LTL Lithuania, Litai"),
("LVL","LVL Latvia, Lati"),
("LYD","LYD Libya, Dinars"),
("MAD","MAD Morocco, Dirhams"),
("MDL","MDL Moldova, Lei"),
("MGA","MGA Madagascar, Ariary"),
("MKD","MKD Macedonia, Denars"),
("MMK","MMK Myanmar (Burma), Kyats"),
("MNT","MNT Mongolia, Tugriks"),
("MOP","MOP Macau, Patacas"),
("MRO","MRO Mauritania, Ouguiyas"),
("MUR","MUR Mauritius, Rupees"),
("MVR","MVR Maldives (Maldive Islands), Rufiyaa"),
("MWK","MWK Malawi, Kwachas"),
("MXN","MXN Mexico, Pesos"),
("MYR","MYR Malaysia, Ringgits"),
("MZN","MZN Mozambique, Meticais"),
("NAD","NAD Namibia, Dollars"),
("NGN","NGN Nigeria, Nairas"),
("NIO","NIO Nicaragua, Cordobas"),
("NOK","NOK Norway, Krone"),
("NPR","NPR Nepal, Nepal Rupees"),
("NZD","NZD New Zealand, Dollars"),
("OMR","OMR Oman, Rials"),
("PAB","PAB Panama, Balboa"),
("PEN","PEN Peru, Nuevos Soles"),
("PGK","PGK Papua New Guinea, Kina"),
("PHP","PHP Philippines, Pesos"),
("PKR","PKR Pakistan, Rupees"),
("PLN","PLN Poland, Zlotych"),
("PYG","PYG Paraguay, Guarani"),
("QAR","QAR Qatar, Rials"),
("RON","RON Romania, New Lei"),
("RSD","RSD Serbia, Dinars"),
("RUB","RUB Russia, Rubles"),
("RWF","RWF Rwanda, Rwanda Francs"),
("SAR","SAR Saudi Arabia, Riyals"),
("SBD","SBD Solomon Islands, Dollars"),
("SCR","SCR Seychelles, Rupees"),
("SDG","SDG Sudan, Pounds"),
("SEK","SEK Sweden, Kronor"),
("SGD","SGD Singapore, Dollars"),
("SHP","SHP Saint Helena, Pounds"),
("SLL","SLL Sierra Leone, Leones"),
("SOS","SOS Somalia, Shillings"),
("SPL","SPL Seborga, Luigini"),
("SRD","SRD Suriname, Dollars"),
("STD","STD Sao Tome and Principe, Dobras"),
("SVC","SVC El Salvador, Colones"),
("SYP","SYP Syria, Pounds"),
("SZL","SZL Swaziland, Emalangeni"),
("THB","THB Thailand, Baht"),
("TJS","TJS Tajikistan, Somoni"),
("TMM","TMM Turkmenistan, Manats"),
("TND","TND Tunisia, Dinars"),
("TOP","TOP Tonga, Pa'anga"),
("TRY","TRY Turkey, New Lira"),
("TTD","TTD Trinidad and Tobago, Dollars"),
("TVD","TVD Tuvalu, Tuvalu Dollars"),
("TWD","TWD Taiwan, New Dollars"),
("TZS","TZS Tanzania, Shillings"),
("UAH","UAH Ukraine, Hryvnia"),
("UGX","UGX Uganda, Shillings"),
("USD","USD United States of America, Dollars"),
("UYU","UYU Uruguay, Pesos"),
("UZS","UZS Uzbekistan, Sums"),
("VEF","VEF Venezuela, Bolivares Fuertes"),
("VND","VND Viet Nam, Dong"),
("VUV","VUV Vanuatu, Vatu"),
("WST","WST Samoa, Tala"),
("XAF","XAF Communaute Financiere Africaine BEAC, Francs"),
("XAG","XAG Silver, Ounces"),
("XAU","XAU Gold, Ounces"),
("XCD","XCD East Caribbean Dollars"),
("XDR","XDR International Monetary Fund (IMF) Special Drawing Rights"),
("XOF","XOF Communaute Financiere Africaine BCEAO, Francs"),
("XPD","XPD Palladium Ounces"),
("XPF","XPF Comptoirs Francais du Pacifique Francs"),
("XPT","XPT Platinum, Ounces"),
("YER","YER Yemen, Rials"),
("ZAR","ZAR South Africa, Rand"),
("ZMK","ZMK Zambia, Kwacha"),
("ZWD","ZWD Zimbabwe, Zimbabwe Dollars")
)
dict_currencies = dict(standard_currencies)
class SettingsForm(forms.Form):
""" Administration settings form """
default_currency = forms.ModelChoiceField(label=_('Base Currency'), queryset=Currency.objects)
default_lead_status = forms.ModelChoiceField(label=_('Default Lead Status'), queryset=[])
default_opportunity_status = forms.ModelChoiceField(label=_('Default Opportunity Status'), queryset=[])
default_order_status = forms.ModelChoiceField(label=_('Default Order Status'), queryset=[])
default_order_source = forms.ModelChoiceField(label=_('Default Order Source'), queryset=[])
default_order_product = forms.ModelChoiceField(label=_('Default Order Product'), queryset=[], required=False)
order_fulfil_status = forms.ModelChoiceField(label=_('Order Fulfilment Status'), queryset=[])
def __init__(self, user, *args, **kwargs):
"Sets choices and initial value"
super(SettingsForm, self).__init__(*args, **kwargs)
self.fields['default_lead_status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_leads=True))
self.fields['default_opportunity_status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_opportunities=True))
self.fields['default_order_status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_sales=True))
self.fields['default_order_source'].queryset = Object.filter_permitted(user,
SaleSource.objects.all())
self.fields['order_fulfil_status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_sales=True))
self.fields['default_order_product'].queryset = Object.filter_permitted(user,
Product.objects.filter(active=True))
# Translation
self.fields['default_currency'].label = _('Base Currency')
self.fields['default_lead_status'].label = _('Default Lead Status')
self.fields['default_opportunity_status'].label = _('Default Opportunity Status')
self.fields['default_order_status'].label = _('Default Order Status')
self.fields['default_order_source'].label = _('Default Order Source')
self.fields['default_order_product'].label = _('Default Order Product')
self.fields['order_fulfil_status'].label = _('Order Fulfilment Status')
try:
self.fields['default_currency'].queryset = Currency.objects
self.fields['default_currency'].initial = Currency.objects.get(is_default__exact=True)
self.fields['default_currency'].widget.attrs.update({'popuplink': reverse('sales_currency_add')})
except:
pass
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_opportunity_status')[0]
default_opportunity_status = SaleStatus.objects.get(pk=long(conf.value))
self.fields['default_opportunity_status'].initial = default_opportunity_status.id
except:
pass
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_lead_status')[0]
default_lead_status = SaleStatus.objects.get(pk=long(conf.value))
self.fields['default_lead_status'].initial = default_lead_status.id
except:
pass
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_order_status')[0]
default_order_status = SaleStatus.objects.get(pk=long(conf.value))
self.fields['default_order_status'].initial = default_order_status.id
except:
pass
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_order_source')[0]
default_order_source = SaleSource.objects.get(pk=long(conf.value))
self.fields['default_order_source'].initial = default_order_source.id
except:
pass
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_order_product')[0]
default_order_product = Product.objects.get(pk=long(conf.value))
self.fields['default_order_product'].initial = default_order_product.id
except:
pass
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'order_fulfil_status')[0]
order_fulfil_status = SaleStatus.objects.get(pk=long(conf.value))
self.fields['order_fulfil_status'].initial = order_fulfil_status.id
except:
pass
def save(self):
"Form processor"
fields = self.fields
try:
for field in fields:
if self.cleaned_data[field]:
if field == 'default_currency':
ModuleSetting.set_for_module('default_currency',
self.cleaned_data['default_currency'],
'treeio.sales')
currency = Currency.objects.get(pk=self.cleaned_data['default_currency'])
currency.is_default = True
currency.save()
else:
ModuleSetting.set_for_module(field, self.cleaned_data[field].id,
'treeio.sales')
return True
except:
return False
class MassActionForm(forms.Form):
""" Mass action form for Orders """
status = forms.ModelChoiceField(queryset=[], required=False)
assignedto = forms.ModelChoiceField(queryset=[], required=False )
delete = forms.ChoiceField(label=_("Delete"), choices=(('', '-----'), ('delete', _('Delete Completely')),
('trash', _('Move to Trash'))), required=False)
instance = None
def __init__(self, user, *args, **kwargs):
if 'instance' in kwargs:
self.instance = kwargs['instance']
del kwargs['instance']
super(MassActionForm, self).__init__(*args, **kwargs)
self.fields['status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_sales=True),
mode='x')
self.fields['status'].label = _("Status:")
self.fields['delete'] = forms.ChoiceField(label=_("Delete"), choices=(('', '-----'),
('delete', _('Delete Completely')),
('trash', _('Move to Trash'))), required=False)
self.fields['assignedto'].queryset = User.objects
self.fields['assignedto'].label = _("Assign To:")
#self.fields['assignedto'].widget.attrs.update({'class': 'autocomplete',
# 'callback': reverse('identities_ajax_user_lookup')})
def save(self, *args, **kwargs):
"Process form"
if self.instance:
if self.is_valid():
if self.cleaned_data['status']:
self.instance.status = self.cleaned_data['status']
if self.cleaned_data['assignedto']:
self.instance.assigned.add(self.cleaned_data['assignedto'])
self.instance.save()
if self.cleaned_data['delete']:
if self.cleaned_data['delete'] == 'delete':
self.instance.delete()
if self.cleaned_data['delete'] == 'trash':
self.instance.trash = True
self.instance.save()
class LeadMassActionForm(forms.Form):
""" Mass action form for Orders """
status = forms.ModelChoiceField(queryset=[], required=False)
assignedto = forms.ModelChoiceField(queryset=[], required=False )
instance = None
def __init__(self, user, *args, **kwargs):
if 'instance' in kwargs:
self.instance = kwargs['instance']
del kwargs['instance']
super(LeadMassActionForm, self).__init__(*args, **kwargs)
self.fields['status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_leads=True),
mode='x')
self.fields['status'].label = _("Status:")
self.fields['assignedto'].queryset = User.objects
self.fields['assignedto'].label = _("Assign To:")
# self.fields['assignedto'].widget.attrs.update({'class': 'autocomplete',
# 'callback': reverse('identities_ajax_user_lookup')})
def save(self, *args, **kwargs):
"Process form"
if self.instance:
if self.is_valid():
if self.cleaned_data['status']:
self.instance.status = self.cleaned_data['status']
if self.cleaned_data['assignedto']:
self.instance.assigned.add(self.cleaned_data['assignedto'])
self.instance.save()
class OpportunityMassActionForm(forms.Form):
""" Mass action form for Orders """
status = forms.ModelChoiceField(queryset=[], required=False)
assignedto = forms.ModelChoiceField(queryset=[], required=False )
instance = None
def __init__(self, user, *args, **kwargs):
if 'instance' in kwargs:
self.instance = kwargs['instance']
del kwargs['instance']
super(OpportunityMassActionForm, self).__init__(*args, **kwargs)
self.fields['status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_opportunities=True),
mode='x')
self.fields['status'].label = _("Status:")
self.fields['assignedto'].queryset = User.objects
self.fields['assignedto'].label = _("Assign To:")
# self.fields['assignedto'].widget.attrs.update({'class': 'autocomplete',
# 'callback': reverse('identities_ajax_user_lookup')})
def save(self, *args, **kwargs):
"Process form"
if self.instance:
if self.is_valid():
if self.cleaned_data['status']:
self.instance.status = self.cleaned_data['status']
if self.cleaned_data['assignedto']:
self.instance.assigned.add(self.cleaned_data['assignedto'])
self.instance.save()
class ProductMassActionForm(forms.Form):
""" Mass action form for Products """
active = forms.ChoiceField(label=_("Action"), choices=(('', '-------'),('active','Mark as Active'),
('inactive','Mark as Inactive')), required=False)
instance = None
def __init__(self, user, *args, **kwargs):
if 'instance' in kwargs:
self.instance = kwargs['instance']
del kwargs['instance']
super(ProductMassActionForm, self).__init__(*args, **kwargs)
# Translation
self.fields['active'].label = _("Action")
def save(self, *args, **kwargs):
"Process form"
if self.instance:
if self.is_valid():
if self.cleaned_data['active'] == 'active':
self.instance.active = True
if self.cleaned_data['active'] == 'inactive':
self.instance.active = False
self.instance.save()
class SaleStatusForm(forms.ModelForm):
""" Status form """
name = forms.CharField(widget=forms.TextInput(attrs={'size':'40'}))
def __init__(self, user, *args, **kwargs):
super(SaleStatusForm, self).__init__(*args, **kwargs)
self.fields['name'].label = _("Name")
self.fields['use_leads'].label = _("Enabled for Leads")
self.fields['use_opportunities'].label = _("Enabled for Opportunities")
self.fields['use_sales'].label = _("Enabled for Sales")
self.fields['active'].label = _("Active")
self.fields['hidden'].label = _("Hidden")
self.fields['details'].label = _("Details")
self.fields['active'].initial = True
class Meta:
"Sales Status Form"
model = SaleStatus
fields = ('name', 'use_leads', 'use_opportunities', 'use_sales', 'active', 'hidden', 'details')
class SaleSourceForm(forms.ModelForm):
""" Status form """
name = forms.CharField(widget=forms.TextInput(attrs={'size':'40'}))
def __init__(self, user, *args, **kwargs):
super(SaleSourceForm, self).__init__(*args, **kwargs)
self.fields['active'].initial = True
self.fields['name'].label = _("Name")
self.fields['active'].label = _("Active")
self.fields['details'].label = _("Details")
class Meta:
"Sale Source Form"
model = SaleSource
fields = ('name', 'active', 'details')
class ProductForm(forms.ModelForm):
""" Product form """
name = forms.CharField(widget=forms.TextInput(attrs={'size':'40'}))
def __init__(self, user, parent=None, *args, **kwargs):
super(ProductForm, self).__init__(*args, **kwargs)
self.fields['supplier'].queryset = Object.filter_permitted(user, Contact.objects)
self.fields['supplier'].widget.attrs.update({'class': 'autocomplete',
'callback': reverse('identities_ajax_contact_lookup')})
self.fields['supplier'].widget.attrs.update({'popuplink': reverse('identities_contact_add')})
self.fields['supplier'].label = _("Supplier")
self.fields['active'].initial = True
self.fields['active'].label = _("Active")
manager = Product.objects.filter(active=True)
if 'instance' in kwargs:
instance = kwargs['instance']
manager = manager.exclude(Q(parent=instance) & Q(pk=instance.id))
self.fields['parent'].queryset = Object.filter_permitted(user, manager, mode='x')
if parent:
self.fields['parent'].initial = get_object_or_404(Product, pk=parent)
self.fields['parent'].label = _("Parent")
self.fields['product_type'].label = _("Product type")
self.fields['code'].label = _("Code")
self.fields['supplier_code'].label = _("Supplier code")
self.fields['buy_price'].label = _("Buy price")
self.fields['sell_price'].label = _("Sell price")
self.fields['stock_quantity'].label = _("Stock quantity")
self.fields['runout_action'].label = _("Runout action")
self.fields['details'].label = _("Details")
class Meta:
"ProductForm"
model = Product
fields = ('name', 'parent', 'product_type', 'code', 'supplier', 'supplier_code', 'buy_price',
'sell_price', 'stock_quantity', 'active', 'runout_action', 'details')
class ProductFilterForm(forms.ModelForm):
""" Ticket Filters definition """
def __init__(self, user, skip=[], *args, **kwargs):
super(ProductFilterForm, self).__init__(*args, **kwargs)
self.fields['product_type'].queryset = Object.filter_permitted(user,
Product.objects.filter(active=True))
self.fields['product_type'].required = False
self.fields['product_type'].label = _("Product type")
self.fields['supplier'].queryset = Object.filter_permitted(user, Contact.objects)
self.fields['supplier'].required = False
self.fields['supplier'].widget.attrs.update({'class': 'autocomplete',
'callback': reverse('identities_ajax_contact_lookup')})
self.fields['supplier'].label = _("Supplier")
self.fields['active'].required = False
self.fields['active'].initial = True
self.fields['active'].label = _("Active")
class Meta:
"Product Filter Form"
model = Product
fields = ('product_type', 'supplier', 'active')
class UpdateRecordForm(forms.ModelForm):
"UpdateRecord form"
def __init__(self, *args, **kwargs):
super(UpdateRecordForm, self).__init__(*args, **kwargs)
self.fields['body'].label = _("Details")
self.fields['body'].required = True
class Meta:
"UpdateRecordForm"
model = UpdateRecord
fields = ['body']
class OrderedProductForm(forms.ModelForm):
""" Add New Ordered Product """
def __init__(self, user, order, *args, **kwargs):
super(OrderedProductForm, self).__init__(*args, **kwargs)
self.fields['subscription'].queryset = Object.filter_permitted(user, Subscription.objects)
self.fields['subscription'].widget.attrs.update({'class': 'autocomplete',
'callback': reverse('sales_ajax_subscription_lookup')})
self.fields['subscription'].widget.attrs.update({'popuplink': reverse('sales_subscription_add')})
self.fields['subscription'].label = _("Subscription")
self.fields['product'].queryset = Object.filter_permitted(user, Product.objects.filter(active=True))
if user.is_admin('treeio.sales'):
self.fields['product'].widget.attrs.update({'popuplink': reverse('sales_product_add')})
self.fields['product'].label = _("Product")
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_order_product')[0]
# AJAX to set the initial rate as the currency converted value of product sell price
self.fields['product'].initial = long(conf.value)
except:
pass
# Tax
self.fields['tax'].widget.attrs.update({'popuplink': reverse('finance_tax_add')})
# TODO: rate
# self.fields['rate_display'].label = _("Rate")
# self.fields['rate_display'].help_text = order.currency.code
self.fields['quantity'].label = _("Quantity")
self.fields['quantity'].initial = 1
self.fields['discount'].label = _("Discount")
self.fields['discount'].help_text = "%"
def save(self, *args, **kwargs):
"Set Rate"
instance = super(OrderedProductForm, self).save(commit=False)
if 'product' in self.cleaned_data and self.cleaned_data['product']:
instance.rate = self.cleaned_data['product'].sell_price
instance.rate_display = instance.rate
return instance
class Meta:
"OrderedProductForm"
model = OrderedProduct
fields = ('product', 'quantity', 'subscription', 'tax', 'discount', 'description')
class SubscriptionForm(forms.ModelForm):
""" Add New Subscription """
def __init__(self, user, *args, **kwargs):
super(SubscriptionForm, self).__init__(*args, **kwargs)
del self.fields['cycle_end']
self.fields['product'].queryset = Object.filter_permitted(user, Product.objects)
self.fields['product'].label = _("Product")
self.fields['client'].queryset = Object.filter_permitted(user, Contact.objects)
self.fields['client'].widget.attrs.update({'class': 'autocomplete',
'callback': reverse('identities_ajax_contact_lookup')})
self.fields['client'].widget.attrs.update({'popuplink': reverse('identities_contact_add')})
self.fields['client'].label = _("Client")
self.fields['start'].widget.attrs.update({'class': 'datepicker'})
self.fields['start'].label = _("Start")
self.fields['expiry'].widget.attrs.update({'class': 'datepicker'})
self.fields['expiry'].label = _("Expiry")
if 'instance' in kwargs:
self.instance = kwargs['instance']
self.fields['start'].widget.attrs['readonly'] = True
del kwargs['instance']
self.fields['active'].initial = True
self.fields['active'].label = _("Active")
self.fields['cycle_period'].label = _("Cycle period")
self.fields['details'].label = _("Details")
class Meta:
"Subscription Form"
model = Subscription
fields = ('client', 'product', 'start', 'expiry', 'cycle_period', 'cycle_end', 'active', 'details')
class OrderForm(forms.ModelForm):
""" Order form """
def __init__(self, user, lead = None, opportunity = None, *args, **kwargs):
super(OrderForm, self).__init__(*args, **kwargs)
self.fields['reference'].required = False
self.fields['reference'].label = _("Reference")
if hasattr(self, 'instance') and not self.instance.reference:
next_ref = self.instance.get_next_reference()
if next_ref:
self.fields['reference'].initial = next_ref
self.fields['client'].queryset = Object.filter_permitted(user, Contact.objects)
self.fields['client'].widget.attrs.update({'class': 'autocomplete',
'callback': reverse('identities_ajax_contact_lookup')})
self.fields['client'].widget.attrs.update({'popuplink': reverse('identities_contact_add')})
self.fields['client'].label = _("Client")
self.fields['source'].queryset = Object.filter_permitted(user, SaleSource.objects.filter(active=True))
self.fields['source'].label = _("Source")
# Currency
self.fields['currency'].label = _('Currency')
instance = getattr(self, 'instance', None)
if instance and instance.id:
del self.fields['currency']
else:
self.fields['currency'].widget.attrs.update({'popuplink': reverse('finance_currency_add')})
self.fields['currency'].initial = Currency.objects.get(is_default=True)
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_order_source')[0]
self.fields['source'].initial = long(conf.value)
except:
pass
self.fields['status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_sales=True))
self.fields['status'].label = _("Status")
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_order_status')[0]
self.fields['status'].initial = long(conf.value)
except:
pass
if opportunity:
self.fields['opportunity'].queryset = Object.filter_permitted(user, Opportunity.objects)
self.fields['opportunity'].label = _("Opportunity")
self.fields['opportunity'].initial = opportunity.id
self.fields['client'].initial = opportunity.contact_id
self.fields['source'].initial = opportunity.source_id
self.fields['assigned'].initial = [i.id for i in opportunity.assigned.only('id')]
else:
del self.fields['opportunity']
if lead:
self.fields['client'].initial = lead.contact_id
self.fields['source'].initial = lead.source_id
self.fields['assigned'].initial = [i.id for i in lead.assigned.only('id')]
self.fields['assigned'].help_text = ""
self.fields['assigned'].label = _("Assigned to")
self.fields['assigned'].widget.attrs.update({'class': 'multicomplete',
'callback': reverse('identities_ajax_user_lookup')})
self.fields['datetime'].label = _("Date")
self.fields['datetime'].widget.attrs.update({'class': 'datetimepicker'})
self.fields['details'].label = _("Details")
class Meta:
"Sale Order Form"
model = SaleOrder
fields = ('reference', 'client', 'opportunity', 'currency', 'source',
'assigned', 'status', 'datetime', 'details')
class OrderFilterForm(forms.ModelForm):
""" Order Filters definition """
paid = forms.ChoiceField(choices=((None,'-----'), ('paid', _("Paid in full")), ('unpaid', _("Pending Payments"))), required=False)
def __init__(self, user, skip=[], *args, **kwargs):
super(OrderFilterForm, self).__init__(*args, **kwargs)
if 'status' in skip:
del self.fields['status']
else:
self.fields['status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_sales=True))
self.fields['status'].required = False
self.fields['status'].label = _("Status")
self.fields['paid'].label = _("Payment Status")
self.fields['client'].queryset = Object.filter_permitted(user, Contact.objects)
self.fields['client'].widget.attrs.update({'class': 'autocomplete',
'callback': reverse('identities_ajax_contact_lookup')})
self.fields['client'].required = False
self.fields['client'].label = _("Client")
self.fields['source'].queryset = Object.filter_permitted(user, SaleSource.objects.filter(active=True))
self.fields['source'].required = False
self.fields['source'].label = _("Source")
self.fields['assigned'].label = _("Assigned")
self.fields['assigned'].widget.attrs.update({'class': 'multicomplete',
'callback': reverse('identities_ajax_user_lookup')})
if 'assigned' in skip:
del self.fields['assigned']
else:
self.fields['assigned'].help_text = ""
class Meta:
"Order Filter Form"
model = SaleOrder
fields = ('client', 'source', 'assigned', 'status')
class LeadForm(forms.ModelForm):
""" Lead form """
def __init__(self, user, *args, **kwargs):
super(LeadForm, self).__init__(*args, **kwargs)
self.fields['contact'].queryset = Object.filter_permitted(user, Contact.objects)
self.fields['contact'].widget.attrs.update({'class': 'autocomplete',
'callback': reverse('identities_ajax_contact_lookup')})
self.fields['contact'].widget.attrs.update({'popuplink': reverse('identities_contact_add')})
self.fields['contact'].label = _("Contact")
self.fields['source'].queryset = Object.filter_permitted(user, SaleSource.objects.filter(active=True))
self.fields['source'].label = _("Source")
self.fields['products_interested'].queryset = Object.filter_permitted(user, Product.objects)
self.fields['products_interested'].help_text = ""
self.fields['products_interested'].widget.attrs.update({'popuplink': reverse('sales_product_add')})
self.fields['products_interested'].label = _("Products interested")
self.fields['assigned'].help_text = ""
self.fields['assigned'].label = _("Assigned to")
self.fields['assigned'].widget.attrs.update({'class': 'multicomplete',
'callback': reverse('identities_ajax_user_lookup')})
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_order_product')[0]
self.fields['products_interested'].initial = [long(conf.value)]
except:
pass
self.fields['status'].queryset = Object.filter_permitted(user, SaleStatus.objects.filter(use_leads=True))
self.fields['status'].label = _("Status")
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_lead_status')[0]
self.fields['status'].initial = long(conf.value)
except:
pass
self.fields['contact_method'].label = _("Contact method")
self.fields['details'].label = _("Details")
class Meta:
"Lead Form"
model = Lead
fields = ('contact', 'source', 'products_interested', 'contact_method',
'assigned', 'status', 'details')
class LeadFilterForm(forms.ModelForm):
""" Ticket Filters definition """
def __init__(self, user, skip=[], *args, **kwargs):
super(LeadFilterForm, self).__init__(*args, **kwargs)
self.fields['contact'].queryset = Object.filter_permitted(user, Contact.objects)
self.fields['contact'].widget.attrs.update({'class': 'autocomplete',
'callback': reverse('identities_ajax_contact_lookup')})
self.fields['contact'].required = False
self.fields['contact'].label = _("Contact")
self.fields['products_interested'].queryset = Object.filter_permitted(user, Product.objects)
self.fields['products_interested'].required = False
self.fields['products_interested'].help_text = ""
self.fields['products_interested'].label = _("Products interested")
self.fields['source'].queryset = Object.filter_permitted(user,
SaleSource.objects.filter(active=True))
self.fields['source'].required = False
self.fields['source'].label = _("Source")
self.fields['status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_leads=True))
self.fields['status'].required = False
self.fields['status'].label = _("Status")
self.fields['contact_method'].required = False
self.fields['contact_method'].label = _("Contact method")
class Meta:
"Lead Filter Form"
model = Lead
fields = ('contact', 'source', 'products_interested', 'contact_method', 'status')
class OpportunityForm(forms.ModelForm):
""" Opportunity form """
def __init__(self, user, lead, *args, **kwargs):
super(OpportunityForm, self).__init__(*args, **kwargs)
self.fields['lead'].queryset = Object.filter_permitted(user, Lead.objects)
self.fields['contact'].queryset = Object.filter_permitted(user, Contact.objects)
self.fields['contact'].widget.attrs.update({'popuplink': reverse('identities_contact_add')})
self.fields['contact'].widget.attrs.update({'class': 'autocomplete',
'callback': reverse('identities_ajax_contact_lookup')})
self.fields['products_interested'].queryset = Object.filter_permitted(user, Product.objects)
self.fields['products_interested'].widget.attrs.update({'popuplink': reverse('sales_product_add')})
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_order_product')[0]
self.fields['products_interested'].initial = [long(conf.value)]
except:
pass
self.fields['source'].queryset = Object.filter_permitted(user,
SaleSource.objects.filter(active=True))
self.fields['status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_opportunities=True))
self.fields['assigned'].widget.attrs.update({'class': 'multicomplete',
'callback': reverse('identities_ajax_user_lookup')})
try:
conf = ModuleSetting.get_for_module('treeio.sales', 'default_opportunity_status')[0]
self.fields['status'].initial = long(conf.value)
except:
pass
if lead:
self.fields['lead'].initial = lead.id
self.fields['contact'].initial = lead.contact_id
self.fields['products_interested'].initial = [i.id for i in lead.products_interested.only('id')]
self.fields['source'].initial = lead.source_id
self.fields['assigned'].initial = [i.id for i in lead.assigned.only('id')]
else:
del self.fields['lead']
self.fields['products_interested'].help_text = ""
self.fields['assigned'].help_text = ""
self.fields['expected_date'].widget.attrs.update({'class': 'datepicker'})
self.fields['closed_date'].widget.attrs.update({'class': 'datepicker'})
self.fields['contact'].label = _("Contact")
self.fields['products_interested'].label = _("Products interested")
self.fields['source'].label = _("Source")
self.fields['expected_date'].label = _("Expected date")
self.fields['closed_date'].label = _("Closed date")
self.fields['assigned'].label = _("Assigned to")
self.fields['amount_display'].label = _("Amount")
self.fields['amount_currency'].label = _("Currency")
self.fields['amount_currency'].widget.attrs.update({'popuplink': reverse('finance_currency_add')})
self.fields['amount_currency'].initial = Currency.objects.get(is_default=True)
self.fields['probability'].label = _("Probability")
self.fields['status'].label = _("Status")
self.fields['details'].label = _("Details")
class Meta:
"Opportunity Form"
model = Opportunity
fields = ('lead', 'contact', 'products_interested', 'source',
'expected_date', 'closed_date', 'assigned', 'amount_currency', 'amount_display', 'probability', 'status', 'details')
class OpportunityFilterForm(forms.ModelForm):
""" Opportunity Filters """
def __init__(self, user, skip=[], *args, **kwargs):
super(OpportunityFilterForm, self).__init__(*args, **kwargs)
self.fields['contact'].queryset = Object.filter_permitted(user, Contact.objects)
self.fields['contact'].widget.attrs.update({'class': 'autocomplete',
'callback': reverse('identities_ajax_contact_lookup')})
self.fields['contact'].required = False
self.fields['contact'].label = _("Contact")
self.fields['source'].queryset = Object.filter_permitted(user,
SaleSource.objects.filter(active=True))
self.fields['source'].required = False
self.fields['source'].label = _("Source")
self.fields['products_interested'].queryset = Object.filter_permitted(user,
Product.objects.filter(active=True))
self.fields['products_interested'].required = False
self.fields['products_interested'].help_text = ""
self.fields['products_interested'].label = _("Products interested")
self.fields['status'].queryset = Object.filter_permitted(user,
SaleStatus.objects.filter(use_opportunities=True))
self.fields['status'].required = False
self.fields['status'].label = _("Status")
class Meta:
"Opportunity Filter Form"
model = Opportunity
fields = ('contact', 'products_interested', 'source', 'status')