<style>
    .success_f {
        background-color: #befabf !important;
    }

    .error_f {
        background-color: #f8b8b8 !important;
    }

    .hasRemain {
        padding: 8px;
        background-color: black;
        border-radius: 9px;
        color: white;
    }
</style>
<div class="row">
    <div class="container">
        <ol class="breadcrumb">
            <li><a href="#">ادارة الفواتير</a></li>
            <li><a href="returnsellbillController.php?do=show">فواتير مردودات المبيعات</a></li>
            <li class="active">عرض فواتير مردودات المبيعات</li>
        </ol>
        <div class="panel panel-default panel-m">
            <!-- Default panel contents -->
            <div class="panel-heading">عرض فواتير مردودات المبيعات <br />
                {if $message != ""}
                {$message}
                {/if}
                <a href="{$youtubes[38]->url}" target="_blank" class="hidden-print"><img
                        src="../views/default/assets/img/youtube.png" class="pull-left" alt="Youtube"
                        style="width: 2%;"></a>
            </div>
            <div class="panel-body">
                <input type="hidden" id="searchvalue" value="{$Programsettingdata->searchvalue}">
                <div id="outputString" class="alert alert-block alert-success fade in hidden-print"
                     style="display:none">
                    <button type="button" class="close pull-left" data-dismiss="alert"
                            aria-hidden="true">&times;</button>
                    <p id="outputStringValue">{$outputString}</p>
                    <div class="clearfix"></div>
                </div>
                <form class="form-inline hidden-print" action="returnsellbillController.php?do=show" method="post">
                    <div class="form-group">
                        <label class="sr-only">العميل</label>
                        <!--                        <select class="select2 wdt100 search_bill"  name="clientid" id="clientid" >
                                                    <option value="-1">اختر عميل</option>



                                                    {foreach $clientData as $client}



                                                    <option value="{$client->clientid}">{$client->clientname}</option>


                                                    {/foreach}



                                                </select>-->
                        <input type="hidden" name="clientid" id="clientid" class="select2Supp search_bill" />
                        <input type="hidden" name="oldsupplierid" id="oldsupplierid" value="" />
                        <input type="hidden" name="oldsuppliername" id="oldsuppliername" value="" />
                    </div>

                    <div class="form-group {if {$billsettingsData[3]->billsettingsvalue} eq 0}  {else} hidden {/if}">
                        <label class="sr-only">{if $Programsettingdata->delegate_seller eq 0} بائع {elseif
                            $Programsettingdata->delegate_seller == 1}مندوب {elseif $Programsettingdata->delegate_seller
                            == 2}طبيب {/if}</label>
                        <select class="select2 wdt100 search_bill" name="sellerid" id="sellerid">
                            <option value="-1">اختر {if $Programsettingdata->delegate_seller eq 0} بائع {elseif
                                $Programsettingdata->delegate_seller == 1}مندوب {elseif
                                $Programsettingdata->delegate_seller == 2}طبيب {/if}</option>



                            {foreach $sellerData as $mysellerData}



                            <option value="{$mysellerData->userid}">{$mysellerData->employeename}</option>


                            {/foreach}



                        </select>
                    </div>


                    <div class="form-group">
                        <label class="sr-only">اسم المستخدم</label>
                        <select class="select2 wdt100 search_bill" name="selleruserid" id="selleruserid">
                            <option value="-1">اختر المستخدم</option>



                            {foreach $sellerinsertData as $mysellerinsertData}



                            <option value="{$mysellerinsertData->userid}">{$mysellerinsertData->employeename}</option>


                            {/foreach}



                        </select>
                    </div>



                    <div class="form-group">
                        <label class="sr-only">رقم تسلسل الفاتورة</label>
                        <input type="text" class="form-control search_bill" placeholder="رقم المتسلسل"
                               name="returnsellbillid" id="returnsellbillid">
                    </div>

                    <div class="form-group" {if {$Programsettingdata->billid} eq 1} style="display:none" {/if}>
                        <label class="sr-only">رقم الفاتورة</label>
                        <select data-placeholder="اختر رقم الفاتورة" class="select2 wdt100 search_bill"
                                name="sellbillserial" id="sellbillserial">
                            <option value="-1">اختر رقم الفاتورة</option>



                            {foreach $returnsellbillSerialData as $returnsellbillSerial}



                            <option value="{$returnsellbillSerial->returnsellbillserial}">
                                {$returnsellbillSerial->returnsellbillserial}</option>

                            {/foreach}



                        </select>
                    </div>
                    <div class="form-group">
                        <label class="sr-only">Serial</label>
                        <input type="text" class="form-control" name="proSellTrackingSerial" id="proSellTrackingSerial"
                               value="">
                    </div>

                    <br />
                    <div class="form-group">
                        <label class="sr-only">المخزن</label>
                        <select class="select2 wdt100 search_bill" name="storeid" id="storeid">
                            <option value="-1">اختر المخزن</option>
                            {foreach $stores as $data}
                            <option value="{$data->storeId}">{$data->storeName}</option>
                            {/foreach}
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="sr-only">الخزنة</label>
                        <select class="select2 wdt100 search_bill" name="saveid" id="saveid">
                            <option value="-1">اختر الخزنة</option>
                            {foreach $saves as $data}
                            <option value="{$data->saveid}">{$data->savename}</option>
                            {/foreach}
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="sr-only">نوع السعر</label>
                        <select class="select2 search_bill wdt100" name="pricetype" id="pricetype">
                            <option value="-1"></option>
                            {if $Programsettingdata->showpriceall eq 1}
                            <option value="0">جملة</option>
                            {/if}
                            {if $Programsettingdata->showpriceunit eq 1}
                            <option value="1">تجزئة</option>
                            {/if}
                            {if $Programsettingdata->showpricehalf eq 1}
                            <option value="2">نصف جملة</option>
                            {/if}
                            {if $Programsettingdata->showprice4 eq 1}
                            <option value="4">سعر4</option>
                            {/if}
                            {if $Programsettingdata->showprice5 eq 1}
                            <option value="5">سعر5</option>
                            {/if}
                            {if $Programsettingdata->showprice6 eq 1}
                            <option value="6">سعر6</option>
                            {/if}
                            {if $Programsettingdata->showprice7 eq 1}
                            <option value="7">سعر7</option>
                            {/if}
                            {if $Programsettingdata->showprice8 eq 1}
                            <option value="8">سعر8</option>
                            {/if}
                            {if $Programsettingdata->showprice9 eq 1}
                            <option value="9">سعر9</option>
                            {/if}
                            {if $Programsettingdata->showprice10 eq 1}
                            <option value="10">سعر10</option>
                            {/if}
                            {if $Programsettingdata->showprice11 eq 1}
                            <option value="11">سعر11</option>
                            {/if}
                            {if $Programsettingdata->showprice12 eq 1}
                            <option value="12">سعر12</option>
                            {/if}
                            {if $Programsettingdata->showprice13 eq 1}
                            <option value="13">سعر13</option>
                            {/if}
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="sr-only">من التاريخ</label>
                        <input type="text" class="form-control awdatepicker search_bill" autocomplete="off"
                               data-date-format="yyyy-mm-dd" name="from" id="from">
                    </div>
                    <div class="form-group">
                        <label class="sr-only">إلى التاريخ</label>
                        <input type="text" class="form-control awdatepicker search_bill" autocomplete="off"
                               data-date-format="yyyy-mm-dd" name="to" id="to">
                    </div>
                    <div class="form-group">
                        <label class="sr-only">اخفاء المحذوف
                            <input type="checkbox" class="search_bill" name="hideDel" id="hideDel" value="1"></label>
                    </div>
                    <button type="submit" class="btn btn-primary">بحث</button>
                    <input type="hidden" name="getTodayBills" id="getTodayBills" value="0" />
                    <button type="submit" class="btn btn-primary" onclick="$('#getTodayBills').val(1);">فواتير
                        اليوم</button>
                </form>
            </div>
            <hr />
            <div class="col-lg-offset-8 col-lg-4 ">
                <div class="well well-sm">
                    <h4 class="text-center">إجمالي المردودات: <span id="totalBills2">{$totalBills}</span></h4>
                </div>
            </div>
            <div class="clearfix"></div>
            {literal}
            <form method="post" action="returnsellbillController.php?do=executeOperation" onsubmit="if (Spry.Widget.Form.validate(this)) {
                        return countChoosenProducts('returnsellbillController');
                    } else {
                        return false;
                    }">
                {/literal}
                <table class="table table-black dataTableClass">
                    <thead>
                        <tr>
                            <th class="hidden-print">
                                <input type="checkbox" name="checkAll" id="checkAll" />
                            </th>
                            {if $Programsettingdata->clientserial eq 1}<th>رقم تسلسل الفاتورة</th>{/if}
                            {if {$Programsettingdata->billid} neq 1} <th>الرقم الفاتورة</th>{/if}
                            {if $Programsettingdata->showparcode eq 1}<th>باركود المنتجات</th>{/if}
                            <th>اجمالى الفاتورة</th>
                            <!--th>المدفوع</th-->
                            <th>تاريخ الفاتورة</th>
                            <th>العميل</th>
                            <th {if {$billsettingsData[3]->billsettingsvalue} eq 0} style="display:block" {else}
                                style="display:none" {/if}>{if $Programsettingdata->delegate_seller eq 0} بائع {elseif
                                $Programsettingdata->delegate_seller == 1}مندوب {elseif
                                $Programsettingdata->delegate_seller == 2}طبيب {/if}</th>
                            <th>اسم المستخدم</th>
                            <th>المخزن</th>
                            <th>الخزنة</th>
                            <th class="hidden-print">تفاصيل الفاتورة</th>
                            <th class="hidden-print">طباعه الباركود </th>
                            {if $usergroup->allowEditDelInBills eq 0}<th class="hidden-print">تعديل الفاتوره</th>
                            <th class="hidden-print">حذف</th>{/if}
                            <th class="hidden-print">قيد اليومية</th>
                        </tr>
                    </thead>
                    <tbody>
                        {assign var=piecesCount value=0}
                        {assign var=payedTotal value=0}
                        {assign var=remainTotal value=0}
                        {foreach $returnsellbillData as $returnsellbill}

                        {if {$returnsellbill->conditions} == 0}
                        {assign var=piecesCount value=$piecesCount+$returnsellbill->returnsellQuantity}
                        {assign var=payedTotal value=$payedTotal+$returnsellbill->returnsellbilltotalpayed}
                        {assign var=remainTotal value=$remainTotal+$returnsellbill->returnsellbillfinalbill}
                        {/if}

                        <!--                        {if {$returnsellbill->conditions} > 0}
                                                <tr class="danger">
                                                    {elseif {$returnsellbill->deletedsellid} eq 0}

                                                <tr>
                                                    {/if}-->

                        {if isset($returnsellbill->newSellbillidAfterEdit)&& $returnsellbill->newSellbillidAfterEdit >
                        0} <!--this bill is deleted for editing-->
                        <tr class="danger">
                            {elseif {$returnsellbill->conditions} <> 0}
                        <tr class="dark">
                            {else}
                        <tr>
                            {/if}

                            <td class="hidden-print">
                                <input type="checkbox" name="choosedItem[]" id="checked{$i}" class="choosenItem"
                                       value="{$returnsellbill->returnsellbillid}" />
                            </td>
                            {if $Programsettingdata->clientserial eq 1}<td>{$returnsellbill->returnsellbillid}</td>{/if}
                            {if $Programsettingdata->billid neq 1} <td>{$returnsellbill->returnsellbillserial}</td>{/if}
                            {if $Programsettingdata->showparcode eq 1} <td>{$returnsellbill->parcodepro}</td> {/if}
                            <td>{$returnsellbill->returnsellbillaftertotalbill}</td>
                            <!--td><span class="{if $returnsellbill->returnsellbillaftertotalbill != $returnsellbill->returnsellbilltotalpayed}hasRemain{/if}">{$returnsellbill->returnsellbilltotalpayed}</span></td-->
                            <td>{$returnsellbill->returnsellbilldate}</td>
                            <td> {if {$returnsellbill->clientname} eq ""}
                                نقدى
                                {else}
                                {$returnsellbill->clientname}
                                {/if} </td>
                            <td {if {$billsettingsData[3]->billsettingsvalue} eq 0} style="display:block" {else}
                                style="display:none" {/if}>{$returnsellbill->employeename}</td>
                            <td>{$returnsellbill->useremployeename}{if $returnsellbill->delbyusername}<br /><span
                                    style="color: red">{$returnsellbill->delbyusername}</span>{/if}</td>
                            <td>{$returnsellbill->storeName}</td>
                            <td>{$returnsellbill->savename}</td>
                            <td class="hidden-print"> <a
                                    href="returnsellbillController.php?do=showDetail&returnsellbillid={$returnsellbill->returnsellbillid}"
                                    type="button" class="btn btn-default btn-lg editicon"></a> </td>
                            <td class="hidden-print"><a
                                    href="productController.php?do=showbarcoderetsellbill&returnsellbillid={$returnsellbill->returnsellbillid}"
                                    target="_new" type="button" class="btn btn-default btn-lg editicon printparcode">
                                </a></td><!--&type=2-->

                            {if $usergroup->allowEditDelInBills eq 0}<td class="hidden-print">{if
                                {$returnsellbill->conditions} eq 0}

                                {if $Programsettingdata->noBillEditTillPay == 0 || ($Programsettingdata->noBillEditTillPay == 1 && $returnsellbill->returnsellbilltotalpayed == $returnsellbill->returnsellbillaftertotalbill)}
                                <a {if $returnsellbill->billPayments > 0 && $usergroup->noBillEditIfTheirIsPayments == 0} data-href={else}href={/if}"sellbillController.php?do=editreturnsellBill&returnsellbillid={$returnsellbill->returnsellbillid}"
                                    type="button" class="btn btn-default btn-lg editicon {if $returnsellbill->billPayments > 0 && $usergroup->noBillEditIfTheirIsPayments == 0} billHasPayments {/if}"></a>

                                {else}
                                سدد الفاتوره اولا
                                {/if}


                                {else}


                                {if isset($returnsellbill->newSellbillidAfterEdit)&&
                                $returnsellbill->newSellbillidAfterEdit > 0} <!--this bill is deleted for editing-->
                                حذف للتعديل
                                {else}
                                تم حذف الفاتورة
                                {/if}


                                {/if} </td>


                            <td class="hidden-print">
                                {if {$returnsellbill->conditions} eq 0}

                                {if $Programsettingdata->noBillEditTillPay == 0 || ($Programsettingdata->noBillEditTillPay == 1 && $returnsellbill->returnsellbilltotalpayed == $returnsellbill->returnsellbillaftertotalbill)}
                                <a href="#" type="button" class="btn btn-default btn-lg removeicon {if $returnsellbill->billPayments > 0 && $usergroup->noBillEditIfTheirIsPayments == 0} billHasPayments {else} conformdel1{/if}""
                                   data-href="returnsellbillController.php?do=delete&returnsellbillid={$returnsellbill->returnsellbillid}&action={$returnsellbill->dailyentryid}"></a>
                                {else}
                                سدد الفاتوره اولا
                                {/if}

                                {else}

                                {if isset($returnsellbill->newSellbillidAfterEdit)&&
                                $returnsellbill->newSellbillidAfterEdit > 0} <!--this bill is deleted for editing-->
                                حذف للتعديل
                                {else}
                                تم حذف الفاتورة
                                {/if}


                                {/if}

                            </td>{/if}
                            <td class="hidden-print">
                                <a href="dailyentry.php?id={$returnsellbill->dailyentryid}" target="_blank"
                                   type="button" class="btn btn-default btn-lg editicon"></a>
                            </td>
                        </tr>
                        {/foreach}

                    </tbody>

                </table>
                <div class="clearfix"></div>

                <!-- Modal -->
                <div class="hidden-print">
                    <div class="modal fade " id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                         aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close pull-left" data-dismiss="modal"
                                            aria-hidden="true">&times;</button>
                                    <div class="modal-title">تأكيد حذف الفاتوره</div>
                                </div>
                                <div class="modal-body"> هل تريد حذف هذه الفاتوره؟ </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">لا</button>
                                    <button type="button" class="btn btn-primary savemodel1">نعم</button>
                                </div>
                            </div>
                            <!-- /.modal-content -->
                        </div>
                        <!-- /.modal-dialog -->
                    </div>
                </div>
                <!-- /.modal -->

                <hr />
                <div class="hidden-print">
                    <div class="col-lg-12">
                        <div class="well well-sm">
                            <div class="form-group">
                                <div class="col-lg-4">
                                    <label for="operation"></label>
                                    <span id="spryselect1">
                                        <select name="operation" id="operation" data-placeholder="اختر العملية"
                                                class="select2 wdt100">
                                            <option value="-1">اختر عملية</option>
                                            <option value="1">حذف</option>
                                        </select>
                                        <span class="selectInvalidMsg">*</span></span>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-lg-4">
                                    <input name="" class="noprint btn btn-primary" type="submit" value="تنفيذ" />
                                </div>
                            </div>
                            <div class="clearfix"></div>
                            <div id="noElementsError" class="alert alert-danger fade in" style="display:none;">
                                <button type="button" class="close pull-left" data-dismiss="alert"
                                        aria-hidden="true">×</button>
                                <p>يجب اختيار العناصر أولاً</p>
                                <div class="clearfix"></div>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                    <div class="col-lg-4 "><!-- col-lg-offset-8 -->
                        <div class="well well-sm">
                            <h4 class="text-center">عدد القطع: <span id="piecesCount">{$piecesCount}</span></h4>
                        </div>
                    </div>
                    <div class="col-lg-4 "><!-- col-lg-offset-8 -->
                        <div class="well well-sm">
                            <h4 class="text-center">إجمالي المردودات <span id="totalBills">{$totalBills}</span></h4>
                        </div>
                    </div>
                    <div class="col-lg-4 "><!-- col-lg-offset-8 -->
                        <div class="well well-sm">
                            <h4 class="text-center">اجمالى المدفوع: <span id="payedTotal">{$payedTotal}</span></h4>
                        </div>
                    </div>
                    <div class="col-lg-4 "><!-- col-lg-offset-8 -->
                        <div class="well well-sm">
                            <h4 class="text-center">اجمالى المؤجل : <span id="remainTotal">{$remainTotal}</span></h4>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </form>
        </div>
    </div>
</div>
{literal}
<script type="text/javascript">
    var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1", {isRequired: false, invalidValue: "-1"});
    $('.printparcode').mousedown(function (e) {
        e.preventDefault();
        var href = $(this).attr('href') + '&parcodeTypeToPrint=' + localStorage.getItem("parcodeTypeToPrint");
        window.open(href)
    });

    $('.conformdel1').live('click', function (e) {

        e.preventDefault();

        var elemposition = $(this).data('href');

        $("#myModal1").find(".savemodel1").attr('data-href', elemposition);
        $('#myModal1').modal('show');
    });
    $('.savemodel1').on('click', function () {

        $('#myModal1').modal('hide');
        var pos = $(this).attr('data-href');

        window.location.href = pos;
    });


    $(function () {
        $(".select2Supp").select2({
            placeholder: "العميل",
            minimumInputLength: document.getElementById("searchvalue").value,
            inimumInputLength: 0,
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "clientControllerAjax.php?do=select2ClientsAjax",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        term: term, //search term
                        page_limit: 7, // page size
                        withAddOption: 0,
                        withNakdi: 0,
                        //jsonRowRet: 1,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: function (data) {
                if (Number(data.id) == -2) {
                    return '<span class="newoption">' + data.text + '</span >';
                } else {
                    return '<span>' + data.text + '</span >';
                }
            },
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                //  var data = {id: item[0], text: item[1]};
                var data = {id: item[0], text: item[1], productquantity: item[2], price: item[3]};
                callback(data);
            },
            //dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
        })
        //this is needed for ajax =>to obtain id from it  and use in ajax
        $('#clientid').select2('data', {id: $('#oldsupplierid').val(), text: $('#oldsuppliername').val()});

        $(".billHasPayments").live("click", function (e) {
            var link = $(this);
            e.preventDefault();
            if (confirm("يوجد مرتجعات دفعات مرتبطة بالفاتوره\n\
    هل تود المتابعه و حذف مرتجعات الدفعات ايضا؟") == true) {
                if (!link.attr('href') || link.attr('href') == "#") {
                    window.location = link.data('href');
                } else {
                    window.location = link.attr('href');
                }
            }
        });
    });
</script>
{/literal}