jeudi 10 septembre 2020

Getting server error when populating data from yajra datatables

I am trying to render a table through yajra datatables in laravel but when I run the code it gives me the following error

Uncaught Error: DataTables warning: table id=contact_table - Ajax error. For more information about this error, please see http://datatables.net/tn/7

I have tried almost everything but this error is not going can some please help me out here is my code:

Controller

private function indexCustomer()
{
    if (!auth()->user()->can('customer.view')) {
        abort(403, 'Unauthorized action.');
    }

    $business_id = request()->session()->get('user.business_id');

    $query = Contact::leftjoin('transactions AS t', 'contacts.id', '=', 't.contact_id')
                ->leftjoin('customer_groups AS cg', 'contacts.customer_group_id', '=', 'cg.id')
                ->where('contacts.business_id', $business_id)
                ->onlyCustomers()
                ->select(['contacts.contact_id', 'contacts.name', 'contacts.created_at', 'total_rp', 'cg.name as customer_group', 'city', 'state', 'country', 'landmark', 'mobile', 'contacts.id', 'is_default',
                    DB::raw("SUM(IF(t.type = 'sell' AND t.status = 'final', final_total, 0)) as total_invoice"),
                    DB::raw("SUM(IF(t.type = 'sell' AND t.status = 'final', (SELECT SUM(IF(is_return = 1,-1*amount,amount)) FROM transaction_payments WHERE transaction_payments.transaction_id=t.id), 0)) as invoice_received"),
                    DB::raw("SUM(IF(t.type = 'sell_return', final_total, 0)) as total_sell_return"),
                    DB::raw("SUM(IF(t.type = 'sell_return', (SELECT SUM(amount) FROM transaction_payments WHERE transaction_payments.transaction_id=t.id), 0)) as sell_return_paid"),
                    DB::raw("SUM(IF(t.type = 'opening_balance', final_total, 0)) as opening_balance"),
                    DB::raw("SUM(IF(t.type = 'opening_balance', (SELECT SUM(IF(is_return = 1,-1*amount,amount)) FROM transaction_payments WHERE transaction_payments.transaction_id=t.id), 0)) as opening_balance_paid"),
                    'email', 'tax_number', 'contacts.pay_term_number', 'contacts.pay_term_type', 'contacts.credit_limit', 'contacts.custom_field1', 'contacts.custom_field2', 'contacts.custom_field3', 'contacts.custom_field4', 'contacts.type'
                    ])
                ->groupBy('contacts.id');

    $contacts = Datatables::of($query)
        ->addColumn('address', '')
        ->addColumn(
            'due',
            '<span class="display_currency contact_due" data-orig-value="" data-currency_symbol=true data-highlight=true></span>'
        )
        ->addColumn(
            'return_due',
            '<span class="display_currency return_due" data-orig-value="" data-currency_symbol=true data-highlight=false></span>'
        )
        ->addColumn(
            'action',
            '<div class="btn-group">
                <button type="button" class="btn btn-info dropdown-toggle btn-xs" 
                    data-toggle="dropdown" aria-expanded="false">' .
                    __("messages.actions") .
                    '<span class="caret"></span><span class="sr-only">Toggle Dropdown
                    </span>
                </button>
                <ul class="dropdown-menu dropdown-menu-left" role="menu">
            @if(($total_invoice + $opening_balance - $invoice_received - $opening_balance_paid)  > 0)
                <li><a href="?type=sell" class="pay_sale_due"><i class="fas fa-money-bill-alt" aria-hidden="true"></i>@lang("contact.pay_due_amount")</a></li>
            @endif
            @if(($total_sell_return - $sell_return_paid)  > 0)
                <li><a href="?type=sell_return" class="pay_purchase_due"><i class="fas fa-money-bill-alt" aria-hidden="true"></i>@lang("lang_v1.pay_sell_return_due")</a></li>
            @endif
            @can("customer.view")
                <li><a href=""><i class="fas fa-eye" aria-hidden="true"></i> @lang("messages.view")</a></li>
            @endcan
            @can("customer.update")
                <li><a href="" class="edit_contact_button"><i class="glyphicon glyphicon-edit"></i> @lang("messages.edit")</a></li>
            @endcan
            @if(!$is_default)
            @can("customer.delete")
                <li><a href="" class="delete_contact_button"><i class="glyphicon glyphicon-trash"></i> @lang("messages.delete")</a></li>
            @endcan
            @endif
            @can("customer.view")
                <li class="divider"></li>
                <li>
                    <a href="">
                        <i class="fas fa-user" aria-hidden="true"></i>
                        @lang("contact.contact_info", ["contact" => __("contact.contact") ])
                    </a>
                </li>
                <li>
                    <a href="">
                        <i class="fas fa-scroll" aria-hidden="true"></i>
                        @lang("lang_v1.ledger")
                    </a>
                </li>
                @if(in_array($type, ["both", "supplier"]))
                    <li>
                        <a href="">
                            <i class="fas fa-arrow-circle-down" aria-hidden="true"></i>
                            @lang("purchase.purchases")
                        </a>
                    </li>
                    <li>
                        <a href="">
                            <i class="fas fa-hourglass-half" aria-hidden="true"></i>
                            @lang("report.stock_report")
                        </a>
                    </li>
                @endif
                @if(in_array($type, ["both", "customer"]))
                    <li>
                        <a href="">
                            <i class="fas fa-arrow-circle-up" aria-hidden="true"></i>
                            @lang("sale.sells")
                        </a>
                    </li>
                @endif
                <li>
                    <a href="">
                        <i class="fas fa-paperclip" aria-hidden="true"></i>
                         @lang("lang_v1.documents_and_notes")
                    </a>
                </li>
            @endcan
            </ul></div>'
        )
        ->editColumn('opening_balance', function ($row) {
            $html = '<span class="display_currency" data-currency_symbol="true" data-orig-value="' . $row->opening_balance . '">' . $row->opening_balance . '</span>';

            return $html;
        })
        ->editColumn('credit_limit', function ($row) {
            $html = __('lang_v1.no_limit');
            if (!is_null($row->credit_limit)) {
                $html = '<span class="display_currency" data-currency_symbol="true" data-orig-value="' . $row->credit_limit . '">' . $row->credit_limit . '</span>';
            }

            return $html;
        })
        ->editColumn('pay_term', '
            @if(!empty($pay_term_type) && !empty($pay_term_number))
                
                @lang("lang_v1.".$pay_term_type)
            @endif
        ')
        ->editColumn('total_rp', '')
        ->editColumn('created_at', '')
        ->removeColumn('total_invoice')
        ->removeColumn('opening_balance_paid')
        ->removeColumn('invoice_received')
        ->removeColumn('state')
        ->removeColumn('country')
        ->removeColumn('city')
        ->removeColumn('type')
        ->removeColumn('id')
        ->removeColumn('is_default')
        ->removeColumn('total_sell_return')
        ->removeColumn('sell_return_paid')
        ->filterColumn('address', function ($query, $keyword) {
            $query->whereRaw("CONCAT(COALESCE(landmark, ''), ', ', COALESCE(city, ''), ', ', COALESCE(state, ''), ', ', COALESCE(country, '') ) like ?", ["%{$keyword}%"]);
        });
    $reward_enabled = (request()->session()->get('business.enable_rp') == 1) ? true : false;
    if (!$reward_enabled) {
        $contacts->removeColumn('total_rp');
    }
    return $contacts->rawColumns(['action', 'opening_balance', 'credit_limit', 'pay_term', 'due', 'return_due'])
                    ->make(true);
}

And here is my JS

var contact_table_type = $('#contact_type').val();
if (contact_table_type == 'supplier') {
    var columns = [
        { data: 'action', searchable: false, orderable: false },
        { data: 'contact_id', name: 'contact_id' },
        { data: 'supplier_business_name', name: 'supplier_business_name' },
        { data: 'name', name: 'name' },
        { data: 'email', name: 'email' },
        { data: 'tax_number', name: 'tax_number' },
        { data: 'pay_term', name: 'pay_term', searchable: false, orderable: false },
        { data: 'opening_balance', name: 'opening_balance', searchable: false },
        { data: 'created_at', name: 'contacts.created_at' },
        { data: 'mobile', name: 'mobile' },
        { data: 'due', searchable: false, orderable: false },
        { data: 'return_due', searchable: false, orderable: false },
        { data: 'custom_field1', name: 'custom_field1', searchable: false, orderable: false},
        { data: 'custom_field2', name: 'custom_field2', searchable: false, orderable: false},
        { data: 'custom_field3', name: 'custom_field3', searchable: false, orderable: false},
        { data: 'custom_field4', name: 'custom_field4', searchable: false, orderable: false},
    ];
} else if (contact_table_type == 'customer') {
    var columns = [
        { data: 'action', searchable: false, orderable: false },
        { data: 'contact_id', name: 'contact_id' },
        { data: 'name', name: 'name' },
        { data: 'email', name: 'email' },
        { data: 'tax_number', name: 'tax_number' },
        { data: 'credit_limit', name: 'credit_limit' },
        { data: 'pay_term', name: 'pay_term', searchable: false, orderable: false },
        { data: 'opening_balance', name: 'opening_balance', searchable: false },
        { data: 'created_at', name: 'contacts.created_at' }
    ];

    if ($('#rp_col').length) {
        columns.push({ data: 'total_rp', name: 'total_rp' });
    }
    Array.prototype.push.apply(columns, [{ data: 'customer_group', name: 'cg.name' },
        { data: 'address', name: 'address', orderable: false },
        { data: 'mobile', name: 'mobile' },
        { data: 'due', searchable: false, orderable: false },
        { data: 'return_due', searchable: false, orderable: false },
        { data: 'custom_field1', name: 'custom_field1'},
        { data: 'custom_field2', name: 'custom_field2'},
        { data: 'custom_field3', name: 'custom_field3'},
        { data: 'custom_field4', name: 'custom_field4'},
        ]);

View

<table class="table table-bordered table-striped" id="contact_table">
                <thead>
                    <tr>
                        <th>@lang('messages.action')</th>
                        <th>@lang('lang_v1.contact_id')</th>
                        @if($type == 'supplier') 
                            <th>@lang('business.business_name')</th>
                            <th>@lang('contact.name')</th>
                            <th>@lang('business.email')</th>
                            <th>@lang('contact.tax_no')</th>
                            <th>@lang('contact.pay_term')</th>
                            <th>@lang('account.opening_balance')</th>
                            <th>@lang('lang_v1.added_on')</th>
                            <th>@lang('contact.mobile')</th>
                            <th>@lang('contact.total_purchase_due')</th>
                            <th>@lang('lang_v1.total_purchase_return_due')</th>
                        @elseif( $type == 'customer')
                            <th>@lang('user.name')</th>
                            <th>@lang('business.email')</th>
                            <th>@lang('contact.tax_no')</th>
                            <th>@lang('lang_v1.credit_limit')</th>
                            <th>@lang('contact.pay_term')</th>
                            <th>@lang('account.opening_balance')</th>
                            <th>@lang('lang_v1.added_on')</th>
                            @if($reward_enabled)
                                <th id="rp_col"></th>
                            @endif
                            <th>@lang('lang_v1.customer_group')</th>
                            <th>@lang('business.address')</th>
                            <th>@lang('contact.mobile')</th>
                            <th>@lang('contact.total_sale_due')</th>
                            <th>@lang('lang_v1.total_sell_return_due')</th>
                        @endif
                        <th>
                            @lang('lang_v1.contact_custom_field1')
                        </th>
                        <th>
                            @lang('lang_v1.contact_custom_field2')
                        </th>
                        <th>
                            @lang('lang_v1.contact_custom_field3')
                        </th>
                        <th>
                            @lang('lang_v1.contact_custom_field4')
                        </th>
                    </tr>
                </thead>
                <tfoot>
                    <tr class="bg-gray font-17 text-center footer-total">
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td
                            @if($type == 'supplier')
                                colspan="5"
                            @elseif( $type == 'customer')
                                @if($reward_enabled)
                                    colspan="8"
                                @else
                                    colspan="7"
                                @endif
                            @endif>
                                <strong>
                                    @lang('sale.total'):
                                </strong>
                        </td>
                        <td><span class="display_currency" id="footer_contact_due" data-currency_symbol ="true"></span></td>
                        <td><span class="display_currency" id="footer_contact_return_due" data-currency_symbol ="true"></span></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                </tfoot>
            </table>

Can anyone please help me out identifying the issue that would be greate. Thank you in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire