magic money distribution
authorphasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 21 Jul 2005 03:38:41 +0000 (03:38 +0000)
committerphasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 21 Jul 2005 03:38:41 +0000 (03:38 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@1332 dcc99617-32d9-48b4-a31d-7c20da2025e4

Evergreen/staff_client/chrome/content/evergreen/main/list_box.js
Evergreen/staff_client/chrome/content/evergreen/patron/patron_bills.js
Evergreen/staff_client/chrome/content/evergreen/patron/patron_display_overlay.xul
Evergreen/staff_client/chrome/content/evergreen/patron/patron_utils.js
Evergreen/staff_client/chrome/content/evergreen/util/error.js
Evergreen/staff_client/chrome/content/evergreen/util/util.js

index 776ac88..4492f9a 100644 (file)
@@ -58,6 +58,8 @@ function list_box_init( p ) {
                                sdump('D_ERROR', cols[i] + '\n' + E + '\n');
                        }
                }
+               
+               return listitem;
        }
 
        p.clear_rows = function () {
index f19ff5f..97932f3 100644 (file)
@@ -8,6 +8,108 @@ function patron_bills_init(p) {
 
        patron_bills_control_box_init( p );
 
+       p.current_payments = [];
+
+       p.update_payment_applied = function () {
+               sdump('D_PATRON_BILLS','p.update_payment_applied()\n');
+               var total_applied = 0;
+               for (var i = 0; i < p.current_payments.length; i++) {
+                       total_applied += dollars_float_to_cents_integer( p.current_payments[ i ].textbox.value );
+               }
+               var total_payment = 0;
+               if (p.control_box.bill_payment_amount.value) {
+                       try {
+                               total_payment = dollars_float_to_cents_integer( p.control_box.bill_payment_amount.value );
+                       } catch(E) {
+                               sdump('D_ERROR',E + '\n');
+                       }
+               }
+               if ( total_applied > total_payment ) {
+                       total_payment = total_applied;
+                       p.control_box.bill_payment_amount.value = cents_as_dollars( total_applied );
+               }
+               p.control_box.bill_payment_applied.value = cents_as_dollars( total_applied );
+               p.control_box.bill_credit_amount.value = '';
+               if (total_payment > total_applied ) {
+                       p.control_box.bill_change_amount.value = cents_as_dollars( total_payment - total_applied);
+                       p.control_box.bill_credit_amount.value = '0.00';
+               } else {
+                       p.control_box.bill_change_amount.value = '0.00';
+                       p.control_box.bill_credit_amount.value = '0.00';
+               }
+               var total_owed = dollars_float_to_cents_integer( p.control_box.bill_total_owed.value );
+               p.control_box.bill_new_balance.value = cents_as_dollars( total_owed - total_applied );
+       }
+
+       p.list_box.apply_to_each_listitem = function (idx, listitem) {
+               sdump('D_PATRON_BILLS','p.list_box.apply_to_each_listitem()\n');
+               p.current_payments[ idx ] = {};
+               p.current_payments[ idx ].listitem = listitem;
+               p.current_payments[ idx ].checkbox = listitem.getElementsByTagName('checkbox')[0];
+               p.current_payments[ idx ].textbox = listitem.getElementsByTagName('textbox')[0];
+               p.current_payments[ idx ].mbts_id = listitem.getAttribute('record_id');
+               p.current_payments[ idx ].balance_owed = listitem.getAttribute('balance_owed');
+
+               p.current_payments[ idx ].textbox.addEventListener(
+                       'change',
+                       function () {
+                               sdump('D_PATRON_BILLS','listitem textbox onchange handler()\n');
+                               var tb = p.current_payments[ idx ].textbox;
+                               var bo = p.current_payments[ idx ].balance_owed;
+                               tb.value = cents_as_dollars( dollars_float_to_cents_integer( tb.value ) ); // show user what we think the number is
+                               sdump('D_PATRON_BILLS','bo = ' + bo + '\ntb.value = ' + tb.value + '\n');
+                               if ( dollars_float_to_cents_integer( tb.value ) > dollars_float_to_cents_integer( bo ) ) {
+                                       sdump('D_PATRON_BILLS','Tried to overpay bill\n');
+                                       tb.value = bo;
+                               }
+                               p.update_payment_applied();
+                       },
+                       false
+               );
+       }
+
+       p.control_box.bill_payment_amount.addEventListener(
+               'change',
+               function () {
+                       var tb = p.control_box.bill_payment_amount;
+                       tb.value = cents_as_dollars( dollars_float_to_cents_integer( tb.value ) );
+                       var total = dollars_float_to_cents_integer( tb.value );
+                       for (var i = 0; i < p.current_payments.length; i++) {
+                               var bill = p.current_payments[i];
+                               var bo = dollars_float_to_cents_integer( bill.balance_owed );
+                               if ( bo > total ) {
+                                       bill.textbox.value = cents_as_dollars( total );
+                                       total = 0;
+                               } else {
+                                       bill.textbox.value = cents_as_dollars( bo );
+                                       total = total - bo;
+                               }
+                       }
+                       p.update_payment_applied();
+               },
+               false
+       );
+
+       p.control_box.bill_change_amount.addEventListener(
+               'change',
+               function() {
+                       var tb = p.control_box.bill_change_amount;
+                       var proposed_change = dollars_float_to_cents_integer( tb.value );
+                       var proposed_credit = 0;
+                       p.update_payment_applied();
+                       var real_change = dollars_float_to_cents_integer( tb.value );
+                       if ( proposed_change > real_change ) {
+                               sdump('D_ERROR','Someone wanted more money than they deserved\n');
+                               proposed_change = real_change;
+                       } else if ( real_change > proposed_change ) {
+                               proposed_credit = real_change - proposed_change;
+                       }
+                       tb.value = cents_as_dollars( proposed_change );
+                       p.control_box.bill_credit_amount.value = cents_as_dollars( proposed_credit );
+               },
+               false
+       );
+
        p.control_box.bill_apply_payment.addEventListener(
                'command',
                function() { alert('Fatal error.. gasp.. arggg.. choke... cough.. sputter'); },
@@ -24,9 +126,11 @@ function patron_bills_control_box_init( p ) {
        p.control_box.bill_total_owed = p.control_box.node.getElementsByAttribute('id','bill_total_owed')[0];
        p.control_box.payment_type = p.control_box.node.getElementsByAttribute('id','payment_type_menulist')[0];
        p.control_box.bill_payment_amount = p.control_box.node.getElementsByAttribute('id','bill_payment_amount_textbox')[0];
+       p.control_box.bill_payment_applied = p.control_box.node.getElementsByAttribute('id','bill_payment_applied_textbox')[0];
        p.control_box.bill_change_amount = p.control_box.node.getElementsByAttribute('id','bill_change_amount_textbox')[0];
        p.control_box.bill_credit_amount = p.control_box.node.getElementsByAttribute('id','bill_credit_amount_textbox')[0];
        p.control_box.bill_apply_payment = p.control_box.node.getElementsByAttribute('id','bill_apply_payment')[0];
+       p.control_box.bill_new_balance = p.control_box.node.getElementsByAttribute('id','bill_new_balance_textbox')[0];
 }
 
 function patron_bills_list_box_init( p ) {
@@ -57,7 +161,7 @@ function patron_bills_list_box_init( p ) {
        ];
 
        p.list_box = list_box_init( { 'w' : p.w, 'node' : p.node, 'cols' : p.patron_bills_cols, 'debug' : p.app } );
-       p.clear_patron_bills = p.list_box.clear_rows;
+       p.clear_patron_bills = function () { p.current_payments = []; p.list_box.clear_rows(); };
        p.add_patron_bills = function (bills) {
                sdump('D_PATRON_BILLS','p.add_patron_bills(' + bills + ')\n');
                return patron_bills_add_patron_bills(p,bills);
@@ -67,7 +171,8 @@ function patron_bills_list_box_init( p ) {
 function patron_bills_add_patron_bills(p, bills) {
        sdump('D_PATRON_BILLS',arg_dump(arguments,{1:true}));
 
-       p.control_box.bill_total_owed.setAttribute('value','');
+       p.control_box.bill_total_owed.setAttribute('value',get_bills_total( bills ));
+       p.control_box.bill_new_balance.setAttribute('value',get_bills_total( bills ));
 
        function xact_dates_box( mbts ) {
                var grid = p.w.document.createElement('grid');
@@ -148,51 +253,60 @@ function patron_bills_add_patron_bills(p, bills) {
 
        setTimeout(
                function() {
-       //p.list_box.clear_rows();
+                       //p.list_box.clear_rows();
 
-       for (var i = 0; i < bills.length; i++) {
+                       for (var i = 0; i < bills.length; i++) {
 
-               var mbts = bills[i];
+                               var mbts = bills[i];
 
-               var cols = [];
+                               var cols = [];
 
-               for (var j = 0; j < p.patron_bills_cols.length; j++) {
-                       var hash = p.patron_bills_cols[j];
-                       sdump('D_PATRON_BILLS','Considering ' + js2JSON(hash) + '\n');
-                       var listcell = p.w.document.createElement('listcell');
-                       listcell.setAttribute('pack','start');
-                       listcell.setAttribute('align','start');
-                       listcell.setAttribute('style','border-left: black solid thin');
-                       var col = '';
-                       if (hash.fm_field_render) {
+                               for (var j = 0; j < p.patron_bills_cols.length; j++) {
+                                       var hash = p.patron_bills_cols[j];
+                                       sdump('D_PATRON_BILLS','Considering ' + js2JSON(hash) + '\n');
+                                       var listcell = p.w.document.createElement('listcell');
+                                       listcell.setAttribute('pack','start');
+                                       listcell.setAttribute('align','start');
+                                       listcell.setAttribute('style','border-left: black solid thin');
+                                       var col = '';
+                                       if (hash.fm_field_render) {
 
-                               var obj = 'mbts';
-                               switch( hash.fm_class ) {
-                                       case 'mvr' : obj_string = 'mvr'; break;
-                               }
-                               var cmd = parse_render_string( obj_string, hash.fm_field_render );
-                               sdump('D_PATRON_BILLS','cmd = ' + cmd + '\n');
-                               try {
-                                       col = eval( cmd );
-                                       sdump('D_PATRON_BILLS','eval = ' + col + '\n');
-                               } catch(E) {
-                                       sdump('D_ERROR',E + '\n');
+                                               var obj = 'mbts';
+                                               switch( hash.fm_class ) {
+                                                       case 'mvr' : obj_string = 'mvr'; break;
+                                               }
+                                               var cmd = parse_render_string( obj_string, hash.fm_field_render );
+                                               sdump('D_PATRON_BILLS','cmd = ' + cmd + '\n');
+                                               try {
+                                                       col = eval( cmd );
+                                                       sdump('D_PATRON_BILLS','eval = ' + col + '\n');
+                                               } catch(E) {
+                                                       sdump('D_ERROR',E + '\n');
+                                               }
+                                               if (typeof(col) == 'string') {
+                                                       listcell.setAttribute('label',col);
+                                               } else {
+                                                       listcell.appendChild( col );
+                                               }
+                                       }
+                                       if (hash.render_xul) {
+                                               var xul = p.w.document.createElement( hash.render_xul );
+                                               listcell.appendChild( xul );
+                                               if (hash.render_xul == 'checkbox') xul.setAttribute('checked', 'true');
+                                       }
+                                       cols.push( listcell );
                                }
-                               if (typeof(col) == 'string') {
-                                       listcell.setAttribute('label',col);
-                               } else {
-                                       listcell.appendChild( col );
+
+                               var listitem = p.list_box.add_row( 
+                                       cols, { 
+                                               'record_id' : mbts.id(),
+                                               'balance_owed' : mbts.balance_owed()
+                                       } 
+                               ); 
+                               if (p.list_box.apply_to_each_listitem) {
+                                       p.list_box.apply_to_each_listitem( i, listitem );
                                }
                        }
-                       if (hash.render_xul) {
-                               var xul = p.w.document.createElement( hash.render_xul );
-                               listcell.appendChild( xul );
-                               if (hash.render_xul == 'checkbox') xul.setAttribute('checked', 'true');
-                       }
-                       cols.push( listcell );
-               }
-               p.list_box.add_row( cols, { 'record_id' : mbts.id() } ); 
-       }
                }, 0
        );
 }
index e069e4f..99a57a7 100755 (executable)
        <hbox>
                <spacer flex="2"/>
                <grid style="border: solid thin black">
-                       <columns><column /><column /></columns>                                 
+                       <columns><column /><column /><column /></columns>                                 
                        <rows>
                                <row>
                                        <label value="Total Owed"
                                                style="font-family: bold" />
 
                                        <textbox id="bill_total_owed" value="" 
-                                               style="font-family: bold" disabled="true" />
+                                               style="font-family: bold" readonly="true" />
                                </row>
                                <row>
                                        <label id="pbi1_l2" value="Payment Type" 
                                        </menulist>
                                </row>
                                <row>
-                                       <label value="Payment amount
+                                       <label value="Payment received
                                                accesskey="p" control="pbi1_tb1"
                                                style="font-family: bold" />
                
                                        <textbox id="bill_payment_amount_textbox" />
                                </row>
                                <row>
-                                       <label value="Change" 
+                                       <label value="- Payment applied" 
+                                               style="font-family: bold" />
+               
+                                       <textbox id="bill_payment_applied_textbox" readonly="true"/>
+                               </row>
+                               <row>
+                                       <label value="= Change" 
                                                control="bill_change_amount_textbox" 
                                                style="font-family: bold" />
-       
+
                                        <textbox id="bill_change_amount_textbox" />
+
+                                       <button id="convert_change" label="Credit" accesskey="t"/>
+                               </row>
+                               <row><label value=" "/></row>
+                               <row>
+
+                                       <label value="New Balance" 
+                                               style="font-family: bold" />
+
+                                       <textbox id="bill_new_balance_textbox" readonly="true"/>
+
                                </row>
+                               <row><label value=" "/></row>
                                <row>
 
-                                       <button label="Forward Credit" 
+                                       <label value="Forward Credit" 
                                                style="font-family: bold" />
 
-                                       <textbox id="bill_credit_amount_textbox" />
+                                       <textbox id="bill_credit_amount_textbox" readonly="true"/>
 
                                </row>
                                <row>
index 4b6a6e0..36daa38 100755 (executable)
@@ -79,11 +79,11 @@ function patron_get_bills_total( au ) {
        if (au.bills == null || au.bills == undefined)
                return '???';
        else {
-               return patron_get_bills_total_from_bills( au.bills );
+               return get_bills_total( au.bills );
        }
 }
 
-function patron_get_bills_total_from_bills( bills ) {
+function get_bills_total( bills ) {
        sdump('D_PATRON_UTILS',arg_dump(arguments));
        var total = 0;
        for (var i = 0; i < bills.length; i++) {
index 184b19f..79690f1 100644 (file)
@@ -40,7 +40,7 @@ var sdump_levels = {
        'D_PATRON_ITEMS' : false,
        'D_PATRON_CHECKOUT_ITEMS' : false,
        'D_PATRON_HOLDS' : false,
-       'D_PATRON_BILLS' : false,
+       'D_PATRON_BILLS' : true,
 
        'D_CHECKIN' : true,
        'D_CHECKIN_ITEMS' : false,
index 57e6f38..434a545 100644 (file)
@@ -100,15 +100,36 @@ function dollars_float_to_cents_integer( money ) {
        // careful to avoid fractions of pennies
        var money_s = money.toString();
        // FIXME: strip miscellaneous characters
-       var dollars = money_s.split(".")[0];
-       var cents = money_s.split(".")[1];
-       if (cents.length > 2) {
-               sdump('D_ERROR',"We don't round money\n");
-               cents = cents.substr(0,2);
+       var marray = money_s.split(".");
+       var dollars = marray[0];
+       var cents = marray[1];
+       try {
+               if (cents.length < 2) {
+                       cents = cents + '0';
+               }
+       } catch(E) {
+               // I'm not sure why these are getting thrown, especially with the code still working
+               sdump('D_ERROR',"cents.length? " + E + "\n");
+       }
+       try {
+               if (cents.length > 2) {
+                       sdump('D_ERROR',"We don't round money\n");
+                       cents = cents.substr(0,2);
+               }
+       } catch(E) {
+               sdump('D_ERROR',"cents.length? " + E + "\n");
        }
        var total = 0;
-       if (parseInt(cents)) total += parseInt(cents);
-       if (parseInt(dollars)) total += (parseInt(dollars) * 100);
+       try {
+               if (parseInt(cents)) total += parseInt(cents);
+       } catch(E) {
+               sdump('D_ERROR',"parseInt(cents)? " + E + "\n");
+       }
+       try {
+               if (parseInt(dollars)) total += (parseInt(dollars) * 100);
+       } catch(E) {
+               sdump('D_ERROR',"parseInt(dollars)? " + E + "\n");
+       }
        return total;   
 }