HomeAccounting

Форк
0
242 строки · 7.4 Кб
1

2
import template from './template.html';
3
import Currency from '../../classes/DB/models/Currency';
4
import BillsCurrencyName from './subComponents/BillsCurrencyName';
5
import BillsPriceOutput from "./subComponents/BillsPriceOutput";
6
import { find } from 'lodash';
7

8

9
export default {
10
    name: "bills-control",
11
    template: template,
12
    props: {
13
        showBillsTable: {
14
            type: Boolean,
15
            default: true,
16
        },
17
    },
18
    data: () => ({
19

20
        billFormType: 'create',
21
        billsFormShow: false,
22
        billFormTitle: 'Управление счетом',
23
        DeadLinePicker: false,
24
        billFormData: {
25
            id: '',
26
            name: '',
27
            sum: '',
28
            deadline: '',
29
            currency: '',
30
            comment: '',
31
            isArchive: false,
32
            sumRules: [
33
                v => !!v || 'Сумма обязательна к заполнению',
34
                v => /^\d+[\,]?\d*$/.test(v) || 'Сумма должна быть числом вида (100 или 100,25)'
35
            ]
36
        },
37

38
        currenciesList: [
39
            {
40
                name: "Российский рубль",
41
                num_code: 643,
42
            }
43
        ],
44

45
        search: '',
46
        loadingDataTable: false,
47
        pagination: {'sortBy': 'sum', 'descending': true, 'rowsPerPage': -1},
48
        headers: [
49
            {
50
                text: 'ID',
51
                align: 'left',
52
                sortable: false,
53
                value: 'id'
54
            },
55
            { text: 'Название', value: 'name', align: 'right' },
56
            { text: 'Сумма', value: 'sum', align: 'right' },
57
            { text: 'Валюта', value: 'currency', align: 'right'},
58
            { text: 'Окончание программы', value: 'deadline', align: 'right' },
59
            { text: 'Комментарий', value: 'comment', align: 'right' },
60
            { text: 'Управление', value: '', align: 'right'},
61
        ],
62
        dataTables: [
63

64
        ],
65
        calcTotalSum: 0,
66
    }),
67
    methods: {
68
        getBills() {
69

70
            this.$store.commit('setPreloader', true);
71

72
            axios.get('/pa/bills-list')
73
                .then(response=> {
74
                    this.dataTables = response.data;
75

76
                    this.$store.commit('setPreloader', false);
77

78
                })
79
                .catch(error => {
80
                    this.$store.commit('AlertError', error.message);
81
                });
82

83
        },
84
        getCurrency() {
85
            let currency = new Currency();
86

87
            currency.getCurrencies().then( (result) => {
88
                this.currenciesList =[];
89

90
                for(let key in result) {
91
                    this.currenciesList[key] = result[key];
92
                }
93
            });
94
        },
95
        createBillsForm() {
96
            this.billsFormShow = true;
97

98
            //clear form
99
            this.billFormData.name = this.billFormData.sum =
100
                this.billFormData.deadline = this.billFormData.comment =
101
                    this.billFormData.id = '';
102
            this.billFormData.isArchive = false;
103

104
            this.billFormTitle = 'Создание нового счета';
105
            this.billFormType = 'create';
106
        },
107
        billSave() {
108

109
            let url = '/pa/bills';
110
            let method = '';
111

112
            if(this.billFormType == 'create') {
113
                method = 'post';
114
            }
115
            else if(this.billFormType == 'update') {
116
                method = 'put';
117
            }
118
            else if(this.billFormType == 'delete') {
119
                method = 'delete';
120
            }
121

122
            axios({
123
                method: method,
124
                url: url,
125
                data:
126
                    {
127
                        bill_id: this.billFormData.id,
128
                        name: this.billFormData.name,
129
                        sum: this.billFormData.sum,
130
                        deadline: this.billFormData.deadline,
131
                        currency: this.billFormData.currency,
132
                        comment: this.billFormData.comment,
133
                        is_archive: this.billFormData.isArchive
134
                    }
135
            })
136
                .then(response=> {
137
                    if(response.data.status == 200) {
138
                        this.billsFormShow = false;
139
                        this.getBills();
140
                    }
141
                    else {
142
                        this.$store.commit('AlertError', 'произошла ошибка');
143
                    }
144

145
                })
146
                .catch(error => {
147
                    this.$store.commit('AlertError', error.message);
148
                });
149
        },
150
        billEditForm(object) {
151
            //construct form
152
            this.billFormData.id = object.id;
153
            this.billFormData.name = object.name;
154
            this.billFormData.sum = object.sum.toString().replace(/\./gi, ',');
155
            this.billFormData.deadline = object.deadline;
156
            this.billFormData.currency = object.currency;
157
            this.billFormData.comment = object.comment;
158
            this.billFormData.isArchive = object.is_archive;
159

160
            this.billFormType = 'update';
161

162
            this.billsFormShow = true;
163

164
        },
165
        billDelete(object) {
166

167
            let confirm = window.confirm('Вы действительно хотите удалить элемент?');
168

169
            if(confirm === true) {
170
                this.billFormType = 'delete';
171
                this.billFormData.id = object.id;
172
                this.billSave();
173
                this.getBills();
174
            }
175

176
        },
177
        sumFormat(sum) {
178
            let number =  sum.toString().replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ');
179
            return number.replace(/\./, ',');
180
        },
181
        isMobile() {
182
            let mobile_getter = this.$store.getters.mobile;
183

184
            let mobile = false;
185

186
            if(mobile_getter !== null) {
187
                mobile = true;
188
            }
189
            return mobile;
190
        },
191
        calculateTotalSum() {
192
            this.calcTotalSum = 0;
193

194
            let otherCurrency = [];
195

196
            const defaultCurrency = this.$store.getters.getDefaultCurrency;
197

198
            for(let key in this.dataTables) {
199
                if(this.dataTables[key]['currency'] === defaultCurrency) {
200
                    this.calcTotalSum += this.dataTables[key]['sum'];
201
                }
202
                else {
203
                    otherCurrency.push(this.dataTables[key]);
204
                }
205
            }
206

207
            if(otherCurrency.length > 0) {
208
                let currency = new Currency();
209

210
                for (let key in otherCurrency) {
211
                    let currencyInfo = currency.getCurrency(otherCurrency[key]['currency']);
212
                    currencyInfo.then((result) => {
213
                        let sum = result.value / result.nominal * otherCurrency[key]['sum'];
214
                        let rounded = Math.ceil((sum)*100)/100;
215
                        this.calcTotalSum += rounded;
216
                    });
217
                }
218
            }
219
        },
220
    },
221
    computed: {
222
        totalValue() {
223
            return this.sumFormat(this.calcTotalSum);
224
        },
225
    },
226
    watch: {
227
        showBillsTable: function (val) {
228
            this.getBills();
229
        },
230
        dataTables: function(val) {
231
            this.calculateTotalSum();
232
        }
233
    },
234
    created() {
235
        this.getBills();
236
        this.getCurrency();
237
    },
238
    components: {
239
        BillsCurrencyName,
240
        BillsPriceOutput
241
    },
242

243
}
244

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.