Subversion Repositories SmartDukaan

Rev

Rev 36668 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<section class="wrapper">
        <div class="row">
                <div class="col-lg-12">
                        <h3 class="page-header"><i class="icon_document_alt"></i> BEAT PLAN - BULK UPLOAD</h3>
                        <ol class="breadcrumb">
                                <li><i class="fa fa-home"></i><a href="${rc.contextPath}/dashboard">Home</a></li>
                                <li><i class="icon_document_alt"></i>Beat Plan Bulk Upload</li>
                        </ol>
                </div>
        </div>

        <div class="row">
                <div class="col-lg-8">
                        <div class="panel panel-default">
                                <div class="panel-heading">
                                        <strong>Upload Beat Plans (XLSX)</strong>
                                        <a href="${rc.contextPath}/beatPlan/downloadTemplate" class="btn btn-xs btn-info pull-right">Download
                                                Template</a>
                                </div>
                                <div class="panel-body">
                                        <p style="margin-bottom:15px;color:#666;">
                                                Upload an Excel file (.xlsx) to create beat plans and schedule them on the calendar in one
                                                go.<br>
                                                <strong>One partner per row.</strong> Beats are grouped by <strong>beat_name +
                                                auth_user_id</strong>.
                                        </p>

                                        <div class="well" style="background:#f9f9f9;">
                                                <strong>XLSX Format (one row = one partner):</strong>
                                                <table class="table table-bordered table-condensed" style="margin-top:10px;font-size:12px;">
                                                        <thead>
                                                        <tr style="background:#eee;">
                                                                <th>beat_name</th>
                                                                <th>auth_user_id</th>
                                                                <th>start_date</th>
                                                                <th>day_number</th>
                                                                <th>sequence_order</th>
                                                                <th>partner_code</th>
                                                        </tr>
                                                        </thead>
                                                        <tbody>
                                                        <tr>
                                                                <td>Jaipur East Route</td>
                                                                <td>280</td>
                                                                <td>2026-06-02</td>
                                                                <td>1</td>
                                                                <td>1</td>
                                                                <td>RJKAI1478</td>
                                                        </tr>
                                                        <tr>
                                                                <td></td>
                                                                <td></td>
                                                                <td></td>
                                                                <td>1</td>
                                                                <td>2</td>
                                                                <td>RJBUN1449</td>
                                                        </tr>
                                                        <tr>
                                                                <td></td>
                                                                <td></td>
                                                                <td></td>
                                                                <td>1</td>
                                                                <td>3</td>
                                                                <td>RJDEG1443</td>
                                                        </tr>
                                                        <tr>
                                                                <td></td>
                                                                <td></td>
                                                                <td></td>
                                                                <td>2</td>
                                                                <td>1</td>
                                                                <td>RJALR1362</td>
                                                        </tr>
                                                        <tr>
                                                                <td></td>
                                                                <td></td>
                                                                <td></td>
                                                                <td>2</td>
                                                                <td>2</td>
                                                                <td>RJBTR1388</td>
                                                        </tr>
                                                        <tr>
                                                                <td></td>
                                                                <td></td>
                                                                <td></td>
                                                                <td>3</td>
                                                                <td>1</td>
                                                                <td>RJRSD1518</td>
                                                        </tr>
                                                        <tr>
                                                                <td></td>
                                                                <td></td>
                                                                <td></td>
                                                                <td>3</td>
                                                                <td>2</td>
                                                                <td>RJSML356</td>
                                                        </tr>
                                                        <tr style="border-top:2px solid #ccc;">
                                                                <td>Agra Circuit</td>
                                                                <td>145</td>
                                                                <td>2026-06-05</td>
                                                                <td>1</td>
                                                                <td>1</td>
                                                                <td>UPAGR101</td>
                                                        </tr>
                                                        <tr>
                                                                <td></td>
                                                                <td></td>
                                                                <td></td>
                                                                <td>1</td>
                                                                <td>2</td>
                                                                <td>UPAGR102</td>
                                                        </tr>
                                                        </tbody>
                                                </table>
                                                <p style="font-size:11px;color:#888;">
                                                        <strong>beat_name</strong>: Name for the beat - <strong>only on the first row of the
                                                        beat</strong>.
                                                        Leave blank for subsequent rows of the same beat. Extra spaces are normalized automatically.<br>
                                                        <strong>auth_user_id</strong>: Sales user ID - <strong>only on the first row of the
                                                        beat</strong>.<br>
                                                        <strong>start_date</strong>: Start date (YYYY-MM-DD) - <strong>only on the first row of the
                                                        beat</strong>.
                                                        System auto-calculates remaining days, skipping holidays. Leave empty for unscheduled
                                                        beat.<br>
                                                        <strong>day_number</strong>: Day within the beat (1, 2, 3...) - required on every row.<br>
                                                        <strong>sequence_order</strong>: Visit order within the day (1, 2, 3...). Optional - row
                                                        order is used if blank.<br>
                                                        <strong>partner_code</strong>: Single partner code for this row.<br><br>
                                                        Multiple beats in one file - just start a new beat row (non-blank beat_name) wherever you
                                                        want to switch.
                                                </p>
                                        </div>

                                        <form id="beat-bulk-form" enctype="multipart/form-data" style="margin-top:15px;">
                                                <div class="form-group">
                                                        <label>Select XLSX File</label>
                                                        <input type="file" id="beat-bulk-file" name="file" accept=".xlsx" class="form-control">
                                                </div>
                                                <div class="checkbox" style="margin-bottom:10px;">
                                                        <label><input type="checkbox" id="beat-bulk-include-sundays"> Include Sundays (don't skip
                                                                Sundays when scheduling)</label>
                                                </div>
                                                <button type="button" class="btn btn-primary beat-bulk-submit">Upload & Process</button>
                                        </form>

                                        <div id="beat-bulk-result" style="display:none;margin-top:15px;"></div>
                                </div>
                        </div>
                </div>
        </div>
</section>

<script>
        $(document).on('click', '.beat-bulk-submit', function () {
                var fileInput = document.getElementById('beat-bulk-file');
                if (!fileInput.files || !fileInput.files[0]) {
                        alert('Please select an XLSX file');
                        return;
                }

                var formData = new FormData();
                formData.append('file', fileInput.files[0]);
                formData.append('includeSundays', $('#beat-bulk-include-sundays').is(':checked'));

                var $btn = $(this);
                        $btn.prop('disabled', true).text('Processing...');

                $.ajax({
                        url: context + '/beatPlan/bulkUploadProcess',
                        type: 'POST',
                        data: formData,
                        processData: false,
                        contentType: false,
                        success: function (response) {
                                        $btn.prop('disabled', false).text('Upload & Process');
                                var data = response.response || response;
                var hasErrors = (data.errors || 0) > 0 || data.status === false;
                var html;
                if (!hasErrors) {
                    // Clean import — zero errors, every row persisted.
                    html = ''
                            + '<div class="alert alert-success" style="font-size:13px;">'
                            + '<i class="fa fa-check-circle" style="font-size:18px;margin-right:6px;"></i>'
                            + '<strong>' + (data.beatsCreated || 0) + ' beat(s) created successfully.</strong>'
                            + '</div>';
                    alert('✓ ' + (data.beatsCreated || 0) + ' beat(s) created successfully.');
                } else {
                    // All-or-nothing failure — nothing persisted, list every issue.
                    var errs = data.errorMessages || [];
                    html = ''
                            + '<div class="alert alert-danger" style="font-size:13px;">'
                            + '<div style="font-size:14px;font-weight:600;margin-bottom:6px;">'
                            + '<i class="fa fa-times-circle" style="margin-right:6px;"></i>'
                            + 'No beats were created.'
                            + '</div>'
                            + '<div style="margin-bottom:8px;color:#b91c1c;">'
                            + (data.message || 'Fix the issues below and re-upload the file.')
                            + '</div>'
                            + '<div style="font-weight:600;color:#7f1d1d;margin-bottom:4px;">'
                            + errs.length + ' issue(s) found:'
                            + '</div>'
                            + '<ol style="margin:0;padding-left:22px;line-height:1.6;color:#7f1d1d;">';
                    errs.forEach(function (msg) {
                        html += '<li>' + msg + '</li>';
                    });
                    html += '</ol></div>';

                    // Modal alert with the first few issues so the user can't miss it
                    // even if the result panel is below the fold.
                    var preview = errs.slice(0, 5).map(function (m, i) {
                        return (i + 1) + '. ' + m;
                    }).join('\n');
                    var more = errs.length > 5 ? '\n\n…and ' + (errs.length - 5) + ' more — see the panel below.' : '';
                    alert('✗ No beats created.\n\n' + errs.length + ' issue(s) found:\n\n' + preview + more);
                                }
                                $('#beat-bulk-result').html(html).show();
                        },
                        error: function (xhr) {
                                        $btn.prop('disabled', false).text('Upload & Process');
                                $('#beat-bulk-result').html('<div class="alert alert-danger">Error: ' + (xhr.responseText || xhr.statusText) + '</div>').show();
                        }
                });
        });
</script>