Editor preSubmit Generate Auto Number

Editor preSubmit Generate Auto Number

vincmeistervincmeister Posts: 136Questions: 36Answers: 4

Hi Allan,

I Want to generate auto number with a custom format on field: ast_receiving_detail.barcode_number
My plan is populate the auto number (var barcode_number) on preSubmit event.
Unfortunately, the generated variable won't submitted to MySQL.

Please advise, here's my code. The last console.log is working as expected, so i think the problem is on data submit

astReceivingDetailEditor.on( 'preSubmit', function (e, mode, action) {
                item_group = astReceivingDetailEditor.field( 'master_item.item_group' ).val();
                user = astReceivingDetailEditor.field( 'user' ).val();
                location_code = astReceivingDetailEditor.field( 'ast_receiving_detail.location_code' ).val();
                $.get( "function/load-last-ast-barcode.php?location_code="+location_code, function( d ) {
                    var number_seq =  d;
                    if (d > 0 ){
                            number_seq = d;
                            len = d.toString();
                    }
                    else {
                        number_seq = 1;
                        len = 1;
                    }
                    if (len.length = 1) {
                        number_seq = "000" + number_seq;
                    } else if (len.length = 2) {
                        number_seq = "00" + number_seq;
                    } else if (len.length = 3) {
                        number_seq = "0" + number_seq;
                    } else if (len.length = 4) {
                        number_seq = number_seq;
                    }
                    barcode_number = item_group + "-" + user + "-" + location_code + "-SBY-" + number_seq;
                    console.log(barcode_number);
                    astReceivingDetailEditor.val( 'ast_receiving_detail.barcode_number',barcode_number );
console.log(astReceivingDetailEditor.field( 'ast_receiving_detail.barcode_number').val());
                });
            } );
    // DataTables PHP library
include( "../php/DataTables.php" );
// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;

    Editor::inst( $db, 'ast_receiving_detail' )
        ->fields(
            Field::inst( 'ast_receiving_detail.id' ),
            Field::inst( 'ast_receiving_head.ast_receiving_id' ),
            Field::inst( 'ast_receiving_detail.ast_receiving_id' ),
            Field::inst( 'ast_receiving_detail.item_code' )
                ->validator( 'Validate::notEmpty' )
                ->options( 'view_master_item', 'item_code', 'full_name', function ($q) {
                    $q->where( 'view_master_item.not_used', 0, '=' );
                }),
            Field::inst( 'view_master_item.full_name' ),
            Field::inst( 'view_master_item.item_name' ),
            Field::inst( 'master_item.item_group' ),
            Field::inst( 'ast_receiving_detail.quantity' )
                ->validator( 'Validate::notEmpty' ),
            Field::inst( 'ast_receiving_detail.sku_code' )
                ->options( 'master_sku', 'sku_code', 'sku_name'),
            Field::inst( 'master_sku.sku_name' ),
            Field::inst( 'ast_receiving_detail.location_code' )
                ->validator( 'Validate::notEmpty' )
                ->options( 'master_location', 'location_code', 'location_name', function ($q) {
                    $q->where( 'master_location.location_purpose', 'AST', '=' );
                }),
            Field::inst( 'master_location.location_name' ),
            Field::inst( 'ast_receiving_detail.barcode_number' ),
            Field::inst( 'ast_receiving_detail.kondisi' )->validator( 'Validate::notEmpty' ),
            Field::inst( 'ast_receiving_detail.is_active' )
        )
        ->leftJoin( 'ast_receiving_head', 'ast_receiving_head.ast_receiving_id', '=', 'ast_receiving_detail.ast_receiving_id' )
        ->leftJoin( 'master_item', 'master_item.item_code', '=', 'ast_receiving_detail.item_code' )
        ->leftJoin('view_master_item','view_master_item.item_code','=','ast_receiving_detail.item_code')
        ->leftJoin('master_sku','master_sku.sku_code','=','ast_receiving_detail.sku_code')
        ->leftJoin('master_location','master_location.location_code','=','ast_receiving_detail.location_code')
        ->where('ast_receiving_detail.barcode_number','','=')
        ->where('ast_receiving_detail.kondisi','','=')
        ->process($_POST)
        ->json();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    You can't use the val() and other field setting methods in preSubmit as the data that is to be submitted to the server has already been gathered! preSubmit is your chance to modify that data.

    What you could do is add the value directly to the object that is being submitted.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4
    edited October 2016

    Hi Allan,

    Thank you for the explanation, i'll try to find another way

    BR,
    Danny

This discussion has been closed.