<commit_before>(function() {
    'use strict';

    var module = angular.module('billett.admin');

    module.controller('AdminOrderController', function ($http, $modal, $q, $state, $stateParams, Page, AdminOrder) {
        var ctrl = this;
        Page.setTitle("Ordre");

        var loadOrder = function () {
            return AdminOrder.get({id: $stateParams['id']}, function(ret) {
                ctrl.order = ret;

                // calc order total and generate list of valid tickets
                ctrl.validtickets = [];
                ctrl.total = 0;
                ctrl.total_reserved = 0;
                ctrl.ticketcount = {
                    'reserved': 0,
                    'revoked': 0,
                    'valid': 0
                };
                ctrl.order.tickets.forEach(function (ticket) {
                    if (!ticket.is_valid) {
                        ctrl.total_reserved += ticket.ticketgroup.price + ticket.ticketgroup.fee;
                        ctrl.ticketcount.reserved++;
                    } else if (!ticket.is_revoked) {
                        ctrl.total += ticket.ticketgroup.price + ticket.ticketgroup.fee;
                        ctrl.validtickets.push(ticket.id);
                        ctrl.ticketcount.valid++;
                    } else {
                        ctrl.ticketcount.revoked++;
                    }
                });

                // total paid amount
                ctrl.total_paid = 0;
                ctrl.order.payments.forEach(function (payment) {
                    ctrl.total_paid += 1*payment.amount;
                });
            }).$promise;
        };

        var loader = Page.setLoading();
        loadOrder().then(function () {
            loader();
        }, function() {
            loader();
            Page.set404();
        });

        var editFields = ['name', 'email', 'phone', 'recruiter', 'comment'];

        // start edit mode (order details)
        ctrl.startEdit = function () {
            ctrl.edit = editFields.reduce(function (prev, cur) {
                prev[cur] = ctrl.order[cur];
                return prev;
            }, {});
        };

        // abort edit mode (order details)
        ctrl.abortEdit = function () {
            delete ctrl.edit;
        };

        // save order details when editing
        ctrl.save = function () {
            editFields.forEach(function (field) {
                ctrl.order[field] = ctrl.edit[field];
            });
            ctrl.order.$update(function (ret) {
                ctrl.order = ret;
                delete ctrl.edit;
            });
        };

        // delete reservation (of order)
        ctrl.deleteReservation = function () {
            var eventgroup_id = ctrl.order.eventgroup.id;
            ctrl.order.$delete(function () {
                $state.go('admin-orders', {eventgroup_id: eventgroup_id});
            }, function () {
                alert("Feil ved sletting av ordre");
            });
        };

        ctrl.completeOrder = function () {
            var modal = $modal.open({
                templateUrl: 'assets/views/admin/paymentgroup/paymentgroup_select.html',
                controller: 'AdminPaymentgroupSelectController as ctrl',
                resolve: {
                    order: function () {
                        return ctrl.order;
                    },
                    actionText: function () {
                        return 'Marker som betalt';
                    },
                    amount: function () {
                        return ctrl.total_reserved;
                    }
                }
            });

            modal.result.then(function (paymentgroup) {
                $http.post('api/order/'+ctrl.order.id+'/validate', {
                    paymentgroup: paymentgroup.id,
                    amount: ctrl.total_reserved,
                    sendmail: false
                }).success(function () {
                    loadOrder();
                }).error(function (err) {
                    if (err == 'amount mismatched') {
                        alert("Noe i ordren ser ut til å ha endret seg. Prøv på nytt.");
                        loadOrder();
                    } else {
                        alert(err);
                    }
                });
            });
        };

        ctrl.convertOrder = function () {
            $http.post('api/order/'+ctrl.order.id+'/validate').success(function () {
                loadOrder();
            }).error(function (err) {
                alert(err);
            });
        };

        // start new tickets form
        ctrl.addTickets = function () {
            var modal = $modal.open({
                templateUrl: 'assets/views/admin/ticketgroup/add_ticketgroup_to_order.html',
                controller: 'AdminTicketgroupAddToOrderController as ctrl',
                resolve: {
                    eventgroup_id: function () {
                        return ctrl.order.eventgroup.id;
                    },
                    getOrder: function () {
                        return ctrl.order;
                    },
                    addHandler: function () {
                        return function (ticketgroups) {
                            return $q(function (resolve, reject) {
                                // reload order with new data
                                loadOrder().then(function () {
                                    resolve();
                                }, function () {
                                    alert("Ukjent feil oppsto ved forsøk på å laste ordren på nytt");
                                    resolve(); // consider it a success anyways
                                });
                            });
                        }
                    }
                }
            });
        };

        // revoke specific ticket
        ctrl.revokeTicket = function (ticket) {
            var modal = $modal.open({
                templateUrl: 'assets/views/admin/ticket/ticket_revoke_modal.html',
                controller: 'AdminTicketRevokeController as ctrl',
                resolve: {
                    order: function () {
                        return ctrl.order;<commit_msg>Fix add tickets to existing order<commit_after>(function() {
    'use strict';

    var module = angular.module('billett.admin');

    module.controller('AdminOrderController', function ($http, $modal, $q, $state, $stateParams, Page, AdminOrder) {
        var ctrl = this;
        Page.setTitle("Ordre");

        var loadOrder = function () {
            return AdminOrder.get({id: $stateParams['id']}, function(ret) {
                ctrl.order = ret;

                // calc order total and generate list of valid tickets
                ctrl.validtickets = [];
                ctrl.total = 0;
                ctrl.total_reserved = 0;
                ctrl.ticketcount = {
                    'reserved': 0,
                    'revoked': 0,
                    'valid': 0
                };
                ctrl.order.tickets.forEach(function (ticket) {
                    if (!ticket.is_valid) {
                        ctrl.total_reserved += ticket.ticketgroup.price + ticket.ticketgroup.fee;
                        ctrl.ticketcount.reserved++;
                    } else if (!ticket.is_revoked) {
                        ctrl.total += ticket.ticketgroup.price + ticket.ticketgroup.fee;
                        ctrl.validtickets.push(ticket.id);
                        ctrl.ticketcount.valid++;
                    } else {
                        ctrl.ticketcount.revoked++;
                    }
                });

                // total paid amount
                ctrl.total_paid = 0;
                ctrl.order.payments.forEach(function (payment) {
                    ctrl.total_paid += 1*payment.amount;
                });
            }).$promise;
        };

        var loader = Page.setLoading();
        loadOrder().then(function () {
            loader();
        }, function() {
            loader();
            Page.set404();
        });

        var editFields = ['name', 'email', 'phone', 'recruiter', 'comment'];

        // start edit mode (order details)
        ctrl.startEdit = function () {
            ctrl.edit = editFields.reduce(function (prev, cur) {
                prev[cur] = ctrl.order[cur];
                return prev;
            }, {});
        };

        // abort edit mode (order details)
        ctrl.abortEdit = function () {
            delete ctrl.edit;
        };

        // save order details when editing
        ctrl.save = function () {
            editFields.forEach(function (field) {
                ctrl.order[field] = ctrl.edit[field];
            });
            ctrl.order.$update(function (ret) {
                ctrl.order = ret;
                delete ctrl.edit;
            });
        };

        // delete reservation (of order)
        ctrl.deleteReservation = function () {
            var eventgroup_id = ctrl.order.eventgroup.id;
            ctrl.order.$delete(function () {
                $state.go('admin-orders', {eventgroup_id: eventgroup_id});
            }, function () {
                alert("Feil ved sletting av ordre");
            });
        };

        ctrl.completeOrder = function () {
            var modal = $modal.open({
                templateUrl: 'assets/views/admin/paymentgroup/paymentgroup_select.html',
                controller: 'AdminPaymentgroupSelectController as ctrl',
                resolve: {
                    order: function () {
                        return ctrl.order;
                    },
                    actionText: function () {
                        return 'Marker som betalt';
                    },
                    amount: function () {
                        return ctrl.total_reserved;
                    }
                }
            });

            modal.result.then(function (paymentgroup) {
                $http.post('api/order/'+ctrl.order.id+'/validate', {
                    paymentgroup: paymentgroup.id,
                    amount: ctrl.total_reserved,
                    sendmail: false
                }).success(function () {
                    loadOrder();
                }).error(function (err) {
                    if (err == 'amount mismatched') {
                        alert("Noe i ordren ser ut til å ha endret seg. Prøv på nytt.");
                        loadOrder();
                    } else {
                        alert(err);
                    }
                });
            });
        };

        ctrl.convertOrder = function () {
            $http.post('api/order/'+ctrl.order.id+'/validate').success(function () {
                loadOrder();
            }).error(function (err) {
                alert(err);
            });
        };

        // start new tickets form
        ctrl.addTickets = function () {
            var modal = $modal.open({
                templateUrl: 'assets/views/admin/ticketgroup/add_ticketgroup_to_order.html',
                controller: 'AdminTicketgroupAddToOrderController as ctrl',
                resolve: {
                    eventgroup_id: function () {
                        return ctrl.order.eventgroup.id;
                    },
                    getOrder: function () {
                        return function () {
                            return $q(function (resolve) {
                                resolve(ctrl.order);
                            });
                        };
                    },
                    addHandler: function () {
                        return function (ticketgroups) {
                            return $q(function (resolve, reject) {
                                // reload order with new data
                                loadOrder().then(function () {
                                    resolve();
                                }, function () {
                                    alert("Ukjent feil oppsto ved forsøk på å laste ordren på nytt");
                                    resolve(); // consider it a success anyways
                                });
                            });
                        }
                    }
                }
            });
        };

        // revoke specific ticket
        ctrl.revokeTicket = function (ticket) {
            var modal = $modal.open({
                templateUrl: 'assets/views/admin/ticket/ticket_revoke_modal.html',
                controller: 'AdminTicketRevokeController as ctrl',
                resolve: {
                    order: function () {
                        return ctrl.order;
{'fraction_non_alphanumeric': 0.10683727137137923, 'fraction_numerical': 0.00156998194520763, 'mean_word_length': 1.0665044606650447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1449929', 'n_tokens_mistral': 3097, 'n_tokens_neox': 2811, 'n_words': 600}