diff --git a/kfet/forms.py b/kfet/forms.py index 2418c840..0fc02dd3 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -326,11 +326,10 @@ class KPsulOperationForm(forms.ModelForm): widget = forms.HiddenInput()) class Meta: model = Operation - fields = ['type', 'amount', 'is_checkout', 'article', 'article_nb'] + fields = ['type', 'amount', 'article', 'article_nb'] widgets = { 'type': forms.HiddenInput(), 'amount': forms.HiddenInput(), - 'is_checkout': forms.HiddenInput(), 'article_nb': forms.HiddenInput(), } @@ -346,7 +345,6 @@ class KPsulOperationForm(forms.ModelForm): "Un achat nécessite un article et une quantité") if article_nb < 1: raise ValidationError("Impossible d'acheter moins de 1 article") - self.cleaned_data['is_checkout'] = True elif type_ope and type_ope in [Operation.DEPOSIT, Operation.WITHDRAW]: if not amount or article or article_nb: raise ValidationError("Bad request") diff --git a/kfet/migrations/0050_remove_checkout.py b/kfet/migrations/0050_remove_checkout.py new file mode 100644 index 00000000..f9c374ca --- /dev/null +++ b/kfet/migrations/0050_remove_checkout.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('kfet', '0049_merge'), + ] + + operations = [ + migrations.RemoveField( + model_name='operation', + name='is_checkout', + ), + migrations.AlterField( + model_name='operation', + name='type', + field=models.CharField(choices=[('purchase', 'Achat'), ('deposit', 'Charge'), ('withdraw', 'Retrait'), ('initial', 'Initial'), ('edit', 'Édition')], max_length=8), + ), + ] diff --git a/kfet/models.py b/kfet/models.py index 29ae57b6..c039ab06 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -537,54 +537,63 @@ class OperationGroup(models.Model): class Operation(models.Model): PURCHASE = 'purchase' - DEPOSIT = 'deposit' + DEPOSIT = 'deposit' WITHDRAW = 'withdraw' INITIAL = 'initial' + EDIT = 'edit' TYPE_ORDER_CHOICES = ( (PURCHASE, 'Achat'), (DEPOSIT, 'Charge'), (WITHDRAW, 'Retrait'), (INITIAL, 'Initial'), + (EDIT, 'Édition'), ) group = models.ForeignKey( - OperationGroup, on_delete = models.PROTECT, - related_name = "opes") + OperationGroup, on_delete=models.PROTECT, + related_name="opes") type = models.CharField( - choices = TYPE_ORDER_CHOICES, - max_length = choices_length(TYPE_ORDER_CHOICES)) + choices=TYPE_ORDER_CHOICES, + max_length=choices_length(TYPE_ORDER_CHOICES)) amount = models.DecimalField( - max_digits = 6, decimal_places = 2, - blank = True, default = 0) - is_checkout = models.BooleanField(default = True) + max_digits=6, decimal_places=2, + blank=True, default=0) # Optional article = models.ForeignKey( - Article, on_delete = models.PROTECT, - related_name = "operations", - blank = True, null = True, default = None) + Article, on_delete=models.PROTECT, + related_name="operations", + blank=True, null=True, default=None) article_nb = models.PositiveSmallIntegerField( - blank = True, null = True, default = None) + blank=True, null=True, default=None) canceled_by = models.ForeignKey( - Account, on_delete = models.PROTECT, - related_name = "+", - blank = True, null = True, default = None) + Account, on_delete=models.PROTECT, + related_name="+", + blank=True, null=True, default=None) canceled_at = models.DateTimeField( - blank = True, null = True, default = None) + blank=True, null=True, default=None) addcost_for = models.ForeignKey( - Account, on_delete = models.PROTECT, - related_name = "addcosts", - blank = True, null = True, default = None) + Account, on_delete=models.PROTECT, + related_name="addcosts", + blank=True, null=True, default=None) addcost_amount = models.DecimalField( - max_digits = 6, decimal_places = 2, - blank = True, null = True, default = None) + max_digits=6, decimal_places=2, + blank=True, null=True, default=None) + + @property + def is_checkout(self): + return (self.type == Operation.DEPOSIT or + self.type == Operation.WITHDRAW or + (self.type == Operation.PURCHASE and self.group.on_acc.is_cash) + ) def __str__(self): templates = { self.PURCHASE: "{nb} {article.name} ({amount}€)", - self.DEPOSIT: "charge ({amount})", - self.WITHDRAW: "retrait ({amount})", - self.INITIAL: "initial ({amount})", + self.DEPOSIT: "charge ({amount}€)", + self.WITHDRAW: "retrait ({amount}€)", + self.INITIAL: "initial ({amount}€)", + self.EDIT: "édition ({amount}€)", } return templates[self.type].format(nb=self.article_nb, article=self.article, diff --git a/kfet/static/kfet/js/history.js b/kfet/static/kfet/js/history.js index 291c106d..8559f050 100644 --- a/kfet/static/kfet/js/history.js +++ b/kfet/static/kfet/js/history.js @@ -31,13 +31,22 @@ function KHistory(options={}) { if (ope['type'] == 'purchase') { infos1 = ope['article_nb']; infos2 = ope['article__name']; - } else if (ope['type'] == 'initial') { - infos1 = parsed_amount.toFixed(2)+'€'; - infos2 = 'Initial'; } else { infos1 = parsed_amount.toFixed(2)+'€'; - infos2 = (ope['type'] == 'deposit') ? 'Charge' : 'Retrait'; - infos2 = ope['is_checkout'] ? infos2 : 'Édition'; + switch (ope['type']) { + case 'initial': + infos2 = 'Initial'; + break; + case 'withdraw': + infos2 = 'Retrait'; + break; + case 'deposit': + infos2 = 'Charge'; + break; + case 'edit': + infos2 = 'Édition'; + break; + } } $ope_html diff --git a/kfet/static/kfet/js/kfet.js b/kfet/static/kfet/js/kfet.js index 6bd03a1f..cc369e32 100644 --- a/kfet/static/kfet/js/kfet.js +++ b/kfet/static/kfet/js/kfet.js @@ -134,7 +134,10 @@ function getErrorsHtml(data) { content += ''; } if ('account' in data['errors']) { - content += data['errors']['account']; + content += 'Général'; + content += ''; } return content; } diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index b4a44492..f7246ec3 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -875,15 +875,27 @@ $(document).ready(function() { return (-5 <= stock - nb && stock - nb <= 5); } - function addDeposit(amount, is_checkout=1) { + function addDeposit(amount) { var deposit_basket_html = $(item_basket_default_html); var amount = parseFloat(amount).toFixed(2); - var index = addDepositToFormset(amount, is_checkout); - var text = is_checkout ? 'Charge' : 'Édition'; + var index = addDepositToFormset(amount); deposit_basket_html .attr('data-opeindex', index) .find('.number').text(amount+"€").end() - .find('.name').text(text).end() + .find('.name').text('Charge').end() + .find('.amount').text(amountToUKF(amount, account_data['is_cof'], false)); + basket_container.prepend(deposit_basket_html); + updateBasketRel(); + } + + function addEdit(amount) { + var deposit_basket_html = $(item_basket_default_html); + var amount = parseFloat(amount).toFixed(2); + var index = addEditToFormset(amount); + deposit_basket_html + .attr('data-opeindex', index) + .find('.number').text(amount+"€").end() + .find('.name').text('Édition').end() .find('.amount').text(amountToUKF(amount, account_data['is_cof'], false)); basket_container.prepend(deposit_basket_html); updateBasketRel(); @@ -1046,11 +1058,10 @@ $(document).ready(function() { // Ask deposit or withdraw // ----- - function askDeposit(is_checkout=1) { - var title = is_checkout ? 'Montant de la charge' : "Montant de l'édition"; + function askDeposit() { $.confirm({ - title: title, - content: '', + title: 'Montant de la charge', + content: '', backgroundDismiss: true, animation:'top', closeAnimation:'bottom', @@ -1059,7 +1070,34 @@ $(document).ready(function() { var amount = this.$content.find('input').val(); if (!$.isNumeric(amount) || amount <= 0) return false; - addDeposit(amount, is_checkout); + addDeposit(amount); + }, + onOpen: function() { + var that = this + this.$content.find('input').on('keydown', function(e) { + if (e.keyCode == 13) { + e.preventDefault(); + that.$confirmButton.click(); + } + }); + }, + onClose: function() { this._lastFocused = (articleSelect.val() ? articleNb : articleSelect) ; } + }); + } + + function askEdit() { + $.confirm({ + title: "Montant de l'édition", + content: '', + backgroundDismiss: true, + animation:'top', + closeAnimation:'bottom', + keyboardEnabled: true, + confirm: function() { + var amount = this.$content.find('input').val(); + if (!$.isNumeric(amount)) + return false; + addEdit(amount); }, onOpen: function() { var that = this @@ -1077,7 +1115,7 @@ $(document).ready(function() { function askWithdraw() { $.confirm({ title: 'Montant du retrait', - content: '', + content: '', backgroundDismiss: true, animation:'top', closeAnimation:'bottom', @@ -1124,7 +1162,7 @@ $(document).ready(function() { var mngmt_total_forms = 1; var prefix_regex = /__prefix__/; - function addOperationToFormset(type, amount, article='', article_nb='', is_checkout=1) { + function addOperationToFormset(type, amount, article='', article_nb='') { var operation_html = operation_empty_html.clone(); var index = mngmt_total_forms; @@ -1134,8 +1172,7 @@ $(document).ready(function() { .find('#id_form-__prefix__-type').val(type).end() .find('#id_form-__prefix__-amount').val((parseFloat(amount)).toFixed(2)).end() .find('#id_form-__prefix__-article').val(article).end() - .find('#id_form-__prefix__-article_nb').val(article_nb).end() - .find('#id_form-__prefix__-is_checkout').val(is_checkout); + .find('#id_form-__prefix__-article_nb').val(article_nb).end(); mngmt_total_forms_input.val(index+1); mngmt_total_forms++; @@ -1150,12 +1187,16 @@ $(document).ready(function() { return index; } - function addDepositToFormset(amount, is_checkout=1) { - return addOperationToFormset('deposit', amount, '', '', is_checkout); + function addDepositToFormset(amount) { + return addOperationToFormset('deposit', amount, '', ''); } - function addWithdrawToFormset(amount, is_checkout=1) { - return addOperationToFormset('withdraw', amount, '', '', is_checkout); + function addEditToFormset(amount) { + return addOperationToFormset('edit', amount, '', ''); + } + + function addWithdrawToFormset(amount) { + return addOperationToFormset('withdraw', amount, '', ''); } function addPurchaseToFormset(article_id, article_nb, amount=0) { @@ -1440,7 +1481,7 @@ $(document).ready(function() { return false; case 119: // F8 - Edition - askDeposit(0); + askEdit(); return false; case 120: // F9 - Addcost diff --git a/kfet/views.py b/kfet/views.py index 823a205d..b4d1328b 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -165,8 +165,7 @@ def account_create_special(request): ope = Operation.objects.create( group = opegroup, type = Operation.INITIAL, - amount = amount, - is_checkout = False) + amount = amount) messages.success(request, 'Compte créé : %s' % account.trigramme) return redirect('kfet.account.create') except Account.UserHasAccount as e: @@ -1010,10 +1009,7 @@ def kpsul_perform_operations(request): operation.amount -= operation.addcost_amount to_addcost_for_balance += operation.addcost_amount if operationgroup.on_acc.is_cash: - operation.is_checkout = True to_checkout_balance += -operation.amount - else: - operation.is_checkout = False if operationgroup.on_acc.is_cof: if is_addcost: operation.addcost_amount = operation.addcost_amount / cof_grant_divisor @@ -1021,13 +1017,13 @@ def kpsul_perform_operations(request): to_articles_stocks[operation.article] -= operation.article_nb else: if operationgroup.on_acc.is_cash: - data['errors']['account'] = 'Charge et retrait impossible sur LIQ' - to_checkout_balance += operation.amount + data['errors']['account'] = 'LIQ' + if operation.type != Operation.EDIT: + to_checkout_balance += operation.amount operationgroup.amount += operation.amount if operation.type == Operation.DEPOSIT: required_perms.add('kfet.perform_deposit') - if (not operation.is_checkout - and operation.type in [Operation.DEPOSIT, Operation.WITHDRAW]): + if operation.type == Operation.EDIT: required_perms.add('kfet.edit_balance_account') need_comment = True if operationgroup.on_acc.is_cof: @@ -1124,8 +1120,7 @@ def kpsul_perform_operations(request): ope_data = { 'id': operation.pk, 'type': operation.type, 'amount': operation.amount, 'addcost_amount': operation.addcost_amount, - 'addcost_for__trigramme': is_addcost and addcost_for.trigramme or None, - 'is_checkout': operation.is_checkout, + 'addcost_for__trigramme': operation.addcost_for and addcost_for.trigramme or None, 'article__name': operation.article and operation.article.name or None, 'article_nb': operation.article_nb, 'group_id': operationgroup.pk, @@ -1215,11 +1210,11 @@ def kpsul_cancel_operations(request): .order_by('at') .last()) if not last_statement or last_statement.at < ope.group.at: - if ope.type == Operation.PURCHASE: + if ope.is_checkout: if ope.group.on_acc.is_cash: to_checkouts_balances[ope.group.checkout] -= - ope.amount - else: - to_checkouts_balances[ope.group.checkout] -= ope.amount + else: + to_checkouts_balances[ope.group.checkout] -= ope.amount # Pour les stocks d'articles # Les stocks d'articles dont il y a eu un inventaire depuis la date @@ -1380,7 +1375,6 @@ def history_json(request): 'type' : ope.type, 'amount' : ope.amount, 'article_nb' : ope.article_nb, - 'is_checkout' : ope.is_checkout, 'addcost_amount': ope.addcost_amount, 'canceled_at' : ope.canceled_at, 'article__name':