1
|
|
View Code Duplication |
define( |
|
|
|
|
2
|
|
|
[ |
3
|
|
|
'core/module', |
4
|
|
|
'jquery', |
5
|
|
|
'backbone', |
6
|
|
|
'lodash', |
7
|
|
|
'client/view/client_select', |
8
|
|
|
'core/billing/model/footer_row_model', |
9
|
|
|
'core/billing/model/row_model', |
10
|
|
|
'core/billing/model/discount', |
11
|
|
|
'core/billing/model/collection', |
12
|
|
|
'core/billing/view/footer', |
13
|
|
|
'invoice/view', |
14
|
|
|
'invoice/discount', |
15
|
|
|
'routing', |
16
|
|
|
'accounting' |
17
|
|
|
], |
18
|
|
|
function(Module, |
19
|
|
|
$, |
20
|
|
|
Backbone, |
21
|
|
|
_, |
22
|
|
|
ClientSelectView, |
23
|
|
|
FooterRowModel, |
24
|
|
|
RowModel, |
25
|
|
|
DiscountModel, |
26
|
|
|
Collection, |
27
|
|
|
FooterView, |
28
|
|
|
InvoiceView, |
29
|
|
|
Discount, |
30
|
|
|
Routing, |
31
|
|
|
Accounting) { |
32
|
|
|
"use strict"; |
33
|
|
|
|
34
|
|
|
return Module.extend({ |
35
|
|
|
collection: null, |
36
|
|
|
footerRowModel: null, |
37
|
|
|
regions: { |
38
|
|
|
'clientInfo': '#client-info', |
39
|
|
|
'invoiceRows': '#invoice-items', |
40
|
|
|
'invoiceForm': '#invoice-create-form' |
41
|
|
|
}, |
42
|
|
|
_renderClientSelect: function(options) { |
43
|
|
|
var model = new Backbone.Model(options.client), |
44
|
|
|
viewOptions = {type: 'invoice', model: model, 'hideLoader': false}, |
45
|
|
|
module = this, |
46
|
|
|
clientSelectView = new ClientSelectView(_.merge(options, viewOptions)); |
47
|
|
|
|
48
|
|
|
clientSelectView.on('currency:update', function(clientOptions) { |
49
|
|
|
Accounting.settings.currency.symbol = clientOptions.currency_format; |
50
|
|
|
|
51
|
|
|
$.getJSON( |
52
|
|
|
Routing.generate('_invoices_get_fields', {'currency': clientOptions.currency}) |
53
|
|
|
).done(_.bind(function(fieldData) { |
54
|
|
|
module.collection.each(function(model) { |
55
|
|
|
model.set('fields', fieldData); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
var invoiceView = module._getInvoiceView(fieldData); |
59
|
|
|
|
60
|
|
|
this.hideLoader(); |
61
|
|
|
|
62
|
|
|
module.app.showChildView('invoiceRows', invoiceView); |
63
|
|
|
|
64
|
|
|
this.$el.find(this.regions.invoiceForm).attr('action', Routing.generate('_invoices_create', {'client': clientOptions.client})); |
65
|
|
|
|
66
|
|
|
module.app.initialize(module.app.options); |
67
|
|
|
}, this)); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
this.app.showChildView('clientInfo', clientSelectView); |
71
|
|
|
}, |
72
|
|
|
_getInvoiceView: function(fieldData) { |
73
|
|
|
return new InvoiceView( |
74
|
|
|
{ |
75
|
|
|
'collection': this.collection, |
76
|
|
|
'footerView': new FooterView({model: this.footerRowModel}), |
77
|
|
|
'selector': '#invoice-footer', |
78
|
|
|
'fieldData': fieldData, |
79
|
|
|
'hasTax': this.options.tax |
80
|
|
|
} |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
}, |
84
|
|
|
initialize: function(options) { |
85
|
|
|
var discountModel = new DiscountModel(), |
86
|
|
|
recurring = $('#invoice_recurring'), |
87
|
|
|
recurringInfo = $('.recurring-info'); |
88
|
|
|
|
89
|
|
|
this.footerRowModel = new FooterRowModel(); |
90
|
|
|
|
91
|
|
|
this.footerRowModel.set('hasTax', options.tax); |
92
|
|
|
|
93
|
|
|
recurring.on('change', function() { |
94
|
|
|
recurringInfo.toggleClass('hidden'); |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
if (recurring.is(':checked')) { |
98
|
|
|
recurringInfo.removeClass('hidden'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
this._renderClientSelect(options); |
102
|
|
|
|
103
|
|
|
var models = []; |
104
|
|
|
|
105
|
|
|
if (!_.isEmpty(options.formData)) { |
106
|
|
|
var counter = 0; |
107
|
|
|
|
108
|
|
|
_.each(options.formData, function(item) { |
109
|
|
|
models.push(new RowModel({ |
110
|
|
|
id: counter++, |
111
|
|
|
fields: item |
112
|
|
|
})); |
113
|
|
|
}); |
114
|
|
|
} else { |
115
|
|
|
models.push(new RowModel({ |
116
|
|
|
id: 0, |
117
|
|
|
fields: options.fieldData |
118
|
|
|
})); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/* COLLECTION */ |
122
|
|
|
this.collection = new Collection(models, {"discountModel": discountModel, 'footerModel': this.footerRowModel}); |
123
|
|
|
|
124
|
|
|
/* DISCOUNT */ |
125
|
|
|
new Discount({model: discountModel, collection: this.collection}); |
|
|
|
|
126
|
|
|
|
127
|
|
|
this.app.showChildView('invoiceRows', this._getInvoiceView(options.fieldData)); |
128
|
|
|
} |
129
|
|
|
}); |
130
|
|
|
} |
131
|
|
|
); |