2020-09-05 06:30:41 +00:00
var LOCK = false
2020-08-09 11:19:27 +00:00
2020-09-05 06:30:41 +00:00
sources = [ ]
sources _notes _display = [ ]
dests = [ ]
dests _notes _display = [ ]
2020-03-17 17:47:16 +00:00
2020-09-05 06:30:41 +00:00
function refreshHistory ( ) {
$ ( '#history' ) . load ( '/note/transfer/ #history' )
2020-03-17 17:47:16 +00:00
}
2020-09-05 06:30:41 +00:00
function reset ( refresh = true ) {
sources _notes _display . length = 0
sources . length = 0
dests _notes _display . length = 0
dests . length = 0
$ ( '#source_note_list' ) . html ( '' )
$ ( '#dest_note_list' ) . html ( '' )
const source _field = $ ( '#source_note' )
source _field . val ( '' )
const event = jQuery . Event ( 'keyup' )
event . originalEvent = { charCode : 97 }
source _field . trigger ( event )
source _field . removeClass ( 'is-invalid' )
source _field . attr ( 'data-original-title' , '' ) . tooltip ( 'hide' )
const dest _field = $ ( '#dest_note' )
dest _field . val ( '' )
dest _field . trigger ( event )
dest _field . removeClass ( 'is-invalid' )
dest _field . attr ( 'data-original-title' , '' ) . tooltip ( 'hide' )
const amount _field = $ ( '#amount' )
amount _field . val ( '' )
amount _field . removeClass ( 'is-invalid' )
$ ( '#amount-required' ) . html ( '' )
const reason _field = $ ( '#reason' )
reason _field . val ( '' )
reason _field . removeClass ( 'is-invalid' )
$ ( '#reason-required' ) . html ( '' )
$ ( '#last_name' ) . val ( '' )
$ ( '#first_name' ) . val ( '' )
$ ( '#bank' ) . val ( '' )
$ ( '#user_note' ) . val ( '' )
$ ( '#profile_pic' ) . attr ( 'src' , '/static/member/img/default_picture.png' )
$ ( '#profile_pic_link' ) . attr ( 'href' , '#' )
if ( refresh ) {
refreshBalance ( )
refreshHistory ( )
}
LOCK = false
2020-03-17 17:47:16 +00:00
}
2020-09-05 06:30:41 +00:00
$ ( document ) . ready ( function ( ) {
/ * *
2020-04-10 20:43:55 +00:00
* If we are in credit / debit mode , check that only one note is entered .
* More over , get first name and last name to autocomplete fields .
* /
2020-09-05 06:30:41 +00:00
function checkUniqueNote ( ) {
if ( $ ( '#type_credit' ) . is ( ':checked' ) || $ ( '#type_debit' ) . is ( ':checked' ) ) {
const arr = $ ( '#type_credit' ) . is ( ':checked' ) ? dests _notes _display : sources _notes _display
if ( arr . length === 0 ) { return }
const last = arr [ arr . length - 1 ]
arr . length = 0
arr . push ( last )
last . quantity = 1
if ( ! last . note . user ) {
$ . getJSON ( '/api/note/note/' + last . note . id + '/?format=json' , function ( note ) {
last . note . user = note . user
$ . getJSON ( '/api/user/' + last . note . user + '/' , function ( user ) {
$ ( '#last_name' ) . val ( user . last _name )
$ ( '#first_name' ) . val ( user . first _name )
} )
} )
} else {
$ . getJSON ( '/api/user/' + last . note . user + '/' , function ( user ) {
$ ( '#last_name' ) . val ( user . last _name )
$ ( '#first_name' ) . val ( user . first _name )
} )
}
2020-07-31 19:24:23 +00:00
}
2020-09-05 06:30:41 +00:00
return true
}
autoCompleteNote ( 'source_note' , 'source_note_list' , sources , sources _notes _display ,
'source_alias' , 'source_note' , 'user_note' , 'profile_pic' , checkUniqueNote )
autoCompleteNote ( 'dest_note' , 'dest_note_list' , dests , dests _notes _display ,
'dest_alias' , 'dest_note' , 'user_note' , 'profile_pic' , checkUniqueNote )
const source = $ ( '#source_note' )
const dest = $ ( '#dest_note' )
$ ( '#type_transfer' ) . change ( function ( ) {
if ( LOCK ) { return }
$ ( '#source_me_div' ) . removeClass ( 'd-none' )
$ ( '#source_note' ) . removeClass ( 'is-invalid' )
$ ( '#dest_note' ) . removeClass ( 'is-invalid' )
$ ( '#special_transaction_div' ) . addClass ( 'd-none' )
source . removeClass ( 'd-none' )
$ ( '#source_note_list' ) . removeClass ( 'd-none' )
$ ( '#credit_type' ) . addClass ( 'd-none' )
dest . removeClass ( 'd-none' )
$ ( '#dest_note_list' ) . removeClass ( 'd-none' )
$ ( '#debit_type' ) . addClass ( 'd-none' )
$ ( '#source_note_label' ) . text ( select _emitters _label )
$ ( '#dest_note_label' ) . text ( select _receveirs _label )
location . hash = 'transfer'
} )
$ ( '#type_credit' ) . change ( function ( ) {
if ( LOCK ) { return }
$ ( '#source_me_div' ) . addClass ( 'd-none' )
$ ( '#source_note' ) . removeClass ( 'is-invalid' )
$ ( '#dest_note' ) . removeClass ( 'is-invalid' )
$ ( '#special_transaction_div' ) . removeClass ( 'd-none' )
$ ( '#source_note_list' ) . addClass ( 'd-none' )
$ ( '#dest_note_list' ) . removeClass ( 'd-none' )
source . addClass ( 'd-none' )
source . tooltip ( 'hide' )
$ ( '#credit_type' ) . removeClass ( 'd-none' )
dest . removeClass ( 'd-none' )
dest . val ( '' )
dest . tooltip ( 'hide' )
$ ( '#debit_type' ) . addClass ( 'd-none' )
$ ( '#source_note_label' ) . text ( transfer _type _label )
$ ( '#dest_note_label' ) . text ( select _receveir _label )
if ( dests _notes _display . length > 1 ) {
$ ( '#dest_note_list' ) . html ( '' )
dests _notes _display . length = 0
2020-08-03 08:03:51 +00:00
}
2020-09-05 06:30:41 +00:00
location . hash = 'credit'
} )
$ ( '#type_debit' ) . change ( function ( ) {
if ( LOCK ) { return }
$ ( '#source_me_div' ) . addClass ( 'd-none' )
$ ( '#source_note' ) . removeClass ( 'is-invalid' )
$ ( '#dest_note' ) . removeClass ( 'is-invalid' )
$ ( '#special_transaction_div' ) . removeClass ( 'd-none' )
$ ( '#source_note_list' ) . removeClass ( 'd-none' )
$ ( '#dest_note_list' ) . addClass ( 'd-none' )
source . removeClass ( 'd-none' )
source . val ( '' )
source . tooltip ( 'hide' )
$ ( '#credit_type' ) . addClass ( 'd-none' )
dest . addClass ( 'd-none' )
dest . tooltip ( 'hide' )
$ ( '#debit_type' ) . removeClass ( 'd-none' )
$ ( '#source_note_label' ) . text ( select _emitter _label )
$ ( '#dest_note_label' ) . text ( transfer _type _label )
if ( sources _notes _display . length > 1 ) {
$ ( '#source_note_list' ) . html ( '' )
sources _notes _display . length = 0
2020-08-03 08:03:51 +00:00
}
2020-09-05 06:30:41 +00:00
location . hash = 'debit'
} )
$ ( '#credit_type' ) . change ( function ( ) {
const type = $ ( '#credit_type option:selected' ) . text ( )
if ( $ ( '#type_credit' ) . is ( ':checked' ) ) { source . val ( type ) } else { dest . val ( type ) }
} )
// Ensure we begin in transfer mode. Removing these lines may cause problems when reloading.
const type _transfer = $ ( '#type_transfer' ) // Default mode
type _transfer . removeAttr ( 'checked' )
$ ( '#type_credit' ) . removeAttr ( 'checked' )
$ ( '#type_debit' ) . removeAttr ( 'checked' )
if ( location . hash ) { $ ( '#type_' + location . hash . substr ( 1 ) ) . click ( ) } else { type _transfer . click ( ) }
2020-09-06 19:41:33 +00:00
$ ( '#source_me' ) . click ( function ( ) {
2020-09-05 06:30:41 +00:00
if ( LOCK ) { return }
// Shortcut to set the current user as the only emitter
sources _notes _display . length = 0
sources . length = 0
$ ( '#source_note_list' ) . html ( '' )
const source _note = $ ( '#source_note' )
source _note . focus ( )
source _note . val ( '' )
let event = jQuery . Event ( 'keyup' )
event . originalEvent = { charCode : 97 }
source _note . trigger ( event )
source _note . val ( username )
event = jQuery . Event ( 'keyup' )
event . originalEvent = { charCode : 97 }
source _note . trigger ( event )
const fill _note = function ( ) {
if ( sources . length === 0 ) {
setTimeout ( fill _note , 100 )
return
}
event = jQuery . Event ( 'keypress' )
event . originalEvent = { charCode : 13 }
source _note . trigger ( event )
source _note . tooltip ( 'hide' )
source _note . val ( '' )
$ ( '#dest_note' ) . focus ( )
2020-08-09 11:19:27 +00:00
}
2020-09-05 06:30:41 +00:00
fill _note ( )
} )
} )
$ ( '#btn_transfer' ) . click ( function ( ) {
if ( LOCK ) { return }
LOCK = true
let error = false
const amount _field = $ ( '#amount' )
amount _field . removeClass ( 'is-invalid' )
$ ( '#amount-required' ) . html ( '' )
const reason _field = $ ( '#reason' )
reason _field . removeClass ( 'is-invalid' )
$ ( '#reason-required' ) . html ( '' )
if ( ! amount _field . val ( ) || isNaN ( amount _field . val ( ) ) || amount _field . val ( ) <= 0 ) {
amount _field . addClass ( 'is-invalid' )
$ ( '#amount-required' ) . html ( '<strong>Ce champ est requis et doit comporter un nombre décimal strictement positif.</strong>' )
error = true
}
const amount = Math . floor ( 100 * amount _field . val ( ) )
if ( amount > 2147483647 ) {
amount _field . addClass ( 'is-invalid' )
$ ( '#amount-required' ) . html ( '<strong>Le montant ne doit pas excéder 21474836.47 €.</strong>' )
error = true
}
if ( ! reason _field . val ( ) ) {
reason _field . addClass ( 'is-invalid' )
$ ( '#reason-required' ) . html ( '<strong>Ce champ est requis.</strong>' )
error = true
}
if ( ! sources _notes _display . length && ! $ ( '#type_credit' ) . is ( ':checked' ) ) {
$ ( '#source_note' ) . addClass ( 'is-invalid' )
error = true
}
if ( ! dests _notes _display . length && ! $ ( '#type_debit' ) . is ( ':checked' ) ) {
$ ( '#dest_note' ) . addClass ( 'is-invalid' )
error = true
}
if ( error ) {
LOCK = false
return
}
let reason = reason _field . val ( )
if ( $ ( '#type_transfer' ) . is ( ':checked' ) ) {
// We copy the arrays to ensure that transactions are well-processed even if the form is reset
[ ... sources _notes _display ] . forEach ( function ( source ) {
[ ... dests _notes _display ] . forEach ( function ( dest ) {
if ( source . note . id === dest . note . id ) {
addMsg ( 'Attention : la transaction de ' + pretty _money ( amount ) + ' de la note ' + source . name +
' vers la note ' + dest . name + " n'a pas été faite car il s'agit de la même note au départ" +
" et à l'arrivée." , 'warning' , 10000 )
LOCK = false
return
2020-03-17 17:47:16 +00:00
}
2020-09-05 06:30:41 +00:00
$ . post ( '/api/note/transaction/transaction/' ,
{
csrfmiddlewaretoken : CSRF _TOKEN ,
quantity : source . quantity * dest . quantity ,
amount : amount ,
reason : reason ,
valid : true ,
polymorphic _ctype : TRANSFER _POLYMORPHIC _CTYPE ,
resourcetype : 'Transaction' ,
source : source . note . id ,
source _alias : source . name ,
destination : dest . note . id ,
destination _alias : dest . name
} ) . done ( function ( ) {
if ( source . note . membership && source . note . membership . date _end < new Date ( ) . toISOString ( ) ) {
addMsg ( 'Attention : la note émettrice ' + source . name + " n'est plus adhérente." ,
'danger' , 30000 )
}
if ( dest . note . membership && dest . note . membership . date _end < new Date ( ) . toISOString ( ) ) {
addMsg ( 'Attention : la note destination ' + dest . name + " n'est plus adhérente." ,
'danger' , 30000 )
}
if ( ! isNaN ( source . note . balance ) ) {
const newBalance = source . note . balance - source . quantity * dest . quantity * amount
if ( newBalance <= - 5000 ) {
addMsg ( 'Le transfert de ' +
pretty _money ( source . quantity * dest . quantity * amount ) + ' de la note ' +
source . name + ' vers la note ' + dest . name + ' a été fait avec succès, ' +
'mais la note émettrice est en négatif sévère.' , 'danger' , 10000 )
reset ( )
return
} else if ( newBalance < 0 ) {
addMsg ( 'Le transfert de ' +
pretty _money ( source . quantity * dest . quantity * amount ) + ' de la note ' +
source . name + ' vers la note ' + dest . name + ' a été fait avec succès, ' +
'mais la note émettrice est en négatif.' , 'warning' , 10000 )
reset ( )
return
}
}
addMsg ( 'Le transfert de ' +
pretty _money ( source . quantity * dest . quantity * amount ) + ' de la note ' + source . name +
' vers la note ' + dest . name + ' a été fait avec succès !' , 'success' , 10000 )
reset ( )
} ) . fail ( function ( err ) { // do it again but valid = false
const errObj = JSON . parse ( err . responseText )
if ( errObj . non _field _errors ) {
addMsg ( 'Le transfert de ' +
pretty _money ( source . quantity * dest . quantity * amount ) + ' de la note ' + source . name +
' vers la note ' + dest . name + ' a échoué : ' + errObj . non _field _errors , 'danger' )
LOCK = false
return
}
$ . post ( '/api/note/transaction/transaction/' ,
2020-03-17 17:47:16 +00:00
{
2020-09-05 06:30:41 +00:00
csrfmiddlewaretoken : CSRF _TOKEN ,
quantity : source . quantity * dest . quantity ,
amount : amount ,
reason : reason ,
valid : false ,
invalidity _reason : 'Solde insuffisant' ,
polymorphic _ctype : TRANSFER _POLYMORPHIC _CTYPE ,
resourcetype : 'Transaction' ,
source : source . note . id ,
source _alias : source . name ,
destination : dest . note . id ,
destination _alias : dest . name
2020-03-25 14:27:38 +00:00
} ) . done ( function ( ) {
2020-09-05 06:30:41 +00:00
addMsg ( 'Le transfert de ' +
pretty _money ( source . quantity * dest . quantity * amount ) + ' de la note ' + source . name +
' vers la note ' + dest . name + ' a échoué : Solde insuffisant' , 'danger' , 10000 )
reset ( )
} ) . fail ( function ( err ) {
const errObj = JSON . parse ( err . responseText )
let error = errObj . detail ? errObj . detail : errObj . non _field _errors
if ( ! error ) { error = err . responseText }
addMsg ( 'Le transfert de ' +
pretty _money ( source . quantity * dest . quantity * amount ) + ' de la note ' + source . name +
' vers la note ' + dest . name + ' a échoué : ' + error , 'danger' )
LOCK = false
} )
} )
} )
} )
} else if ( $ ( '#type_credit' ) . is ( ':checked' ) || $ ( '#type_debit' ) . is ( ':checked' ) ) {
let special _note
let user _note
let alias
const given _reason = reason
let source _id , dest _id
if ( $ ( '#type_credit' ) . is ( ':checked' ) ) {
special _note = $ ( '#credit_type' ) . val ( )
user _note = dests _notes _display [ 0 ] . note
alias = dests _notes _display [ 0 ] . name
source _id = special _note
dest _id = user _note . id
reason = 'Crédit ' + $ ( '#credit_type option:selected' ) . text ( ) . toLowerCase ( )
if ( given _reason . length > 0 ) { reason += ' (' + given _reason + ')' }
} else {
special _note = $ ( '#debit_type' ) . val ( )
user _note = sources _notes _display [ 0 ] . note
alias = sources _notes _display [ 0 ] . name
source _id = user _note . id
dest _id = special _note
reason = 'Retrait ' + $ ( '#credit_type option:selected' ) . text ( ) . toLowerCase ( )
if ( given _reason . length > 0 ) { reason += ' (' + given _reason + ')' }
2020-03-17 17:47:16 +00:00
}
2020-09-05 06:30:41 +00:00
$ . post ( '/api/note/transaction/transaction/' ,
{
csrfmiddlewaretoken : CSRF _TOKEN ,
quantity : 1 ,
amount : amount ,
reason : reason ,
valid : true ,
polymorphic _ctype : SPECIAL _TRANSFER _POLYMORPHIC _CTYPE ,
resourcetype : 'SpecialTransaction' ,
source : source _id ,
source _alias : sources _notes _display . length ? alias : null ,
destination : dest _id ,
destination _alias : dests _notes _display . length ? alias : null ,
last _name : $ ( '#last_name' ) . val ( ) ,
first _name : $ ( '#first_name' ) . val ( ) ,
bank : $ ( '#bank' ) . val ( )
} ) . done ( function ( ) {
addMsg ( 'Le crédit/retrait a bien été effectué !' , 'success' , 10000 )
if ( user _note . membership && user _note . membership . date _end < new Date ( ) . toISOString ( ) ) { addMsg ( 'Attention : la note ' + alias + " n'est plus adhérente." , 'danger' , 10000 ) }
reset ( )
} ) . fail ( function ( err ) {
const errObj = JSON . parse ( err . responseText )
let error = errObj . detail ? errObj . detail : errObj . non _field _errors
if ( ! error ) { error = err . responseText }
addMsg ( 'Le crédit/retrait a échoué : ' + error , 'danger' , 10000 )
LOCK = false
} )
}
} )