<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="sellbillController.php?do=show">فواتير البيع</a></li>
            <li class="active">عرض فواتير البيع</li>
        </ol>
        <div class="panel panel-default panel-m">
            <!-- Default panel contents -->
            <div class="panel-heading" style="padding:5px 15px 15px">
                <h3 class="panel-title" style="display: inline-block;">عرض فواتير البيع <br />
                    {if $message != ""}
                    {$message}
                    {/if}</h3>
                <a href="{$youtubes[23]->url}" target="_blank" class="hidden-print"><img
                        src="../views/default/assets/img/youtube.png" class="pull-left" alt="Youtube"
                        style="width: 2%;"></a>
                <a href="sellbillController.php" class="hidden-print"><input type="button" style="margin-left: 12px"
                                                                             id="submit" class="btn btn-primary pull-left " value="إضافة جديد" /></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>
                <input type="hidden" name="withNakdi" id="withNakdi"
                       value="{$Usergroupdata->showClientAndSupplierOne}" />
                <form class="form-inline hidden-print" action="sellbillController.php?do=show" method="post">
                    <div class="form-group col-lg-4">
                        <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 {if $Programsettingdata->showBillOperationSearch != 1} hidden {/if}">
                        <label class="sr-only">عملية الفاتورة</label>
                        <select class="select2 wdt100 search_bill" name="billoperationid" id="billoperationid">
                            <option value="-1">اختر عملية</option>
                            {foreach $billOperations as $data}
                            <option value="{$data->id}">{$data->name}</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" {if {$Programsettingdata->billid} eq 1} style="display:none" {/if}>
                        <label class="sr-only">رقم الفاتورة</label>
                        <input type="text" class="form-control search_bill" placeholder="رقم الفاتورة"
                               name="sellbillserial" id="sellbillserial">
                    </div>
                    <div class="form-group">
                        <label class="sr-only">الرقم تسلسل الفاتورة</label>
                        <input type="text" class="form-control search_bill" placeholder="رقم تسلسل الفاتورة"
                               name="sellbillid" id="sellbillid">
                    </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>




                    {if $searchinonesave == 1}
                    <input type="hidden" name="saveid" id="saveid" value="{$saveid}" />
                    {else}
                    <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>
                    {/if}



                    <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}
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="sr-only">الرحلة</label>
                        <input type="hidden" name="tripid" id="tripid"/>
                    </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="shippingDateFrom" id="shippingDateFrom">
                    </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="shippingDateTo" id="shippingDateTo">
                    </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="deliverDateFrom" id="deliverDateFrom">
                    </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="deliverDateTo" id="deliverDateTo">
                    </div>

                    <div class="form-group">
                        <label class="sr-only">Serial</label>
                        <input type="text" class="form-control search_bill" name="proSellTrackingSerial"
                               id="proSellTrackingSerial" value="">
                    </div>
                    {if $Programsettingdata->showBillCollectionSearch eq 1}
                    <br />
                    <div class="form-group">
                        <label class="sr-only">نوع التحصيل</label>
                        <select class="select2 search_bill wdt100" name="collectionType" id="collectionType">

                            <option value="-1"></option>
                            <option value="0">بدون</option>
                            <option value="1">شيك</option>
                            <option value="2">مفتوح</option>
                        </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="collectionDateFrom" id="collectionDateFrom">
                    </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="collectionDateTo" id="collectionDateTo">
                    </div>

                    {/if}

                    <div id="dateTimeDiv">
                        <div class="form-group">
                            <label class="sr-only">من التاريخ</label>
                            <div class="input-group date form_datetime col-md-5" data-date=""
                                 data-date-format="yyyy-mm-dd  h:i:s" data-link-field="dtp_input1">
                                <input class="form-control search_bill" size="16" type="text" value="" name="from"
                                       id="from" readonly="">
                                <span class="input-group-addon " style="width: 0%;"><span
                                        class=" glyphicon glyphicon-remove col-lg-4"></span></span>
                                <span class="input-group-addon" style="width: 0%;"><span
                                        class="glyphicon glyphicon-th col-lg-4"></span></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="sr-only">إلى التاريخ</label>
                            <div class="input-group date form_datetime col-md-5" data-date=""
                                 data-date-format="yyyy-mm-dd  h:i:s" data-link-field="dtp_input1">
                                <input class="form-control search_bill" size="16" type="text" value="" name="to" id="to"
                                       readonly="">
                                <span class="input-group-addon " style="width: 0%;"><span
                                        class=" glyphicon glyphicon-remove col-lg-4"></span></span>
                                <span class="input-group-addon" style="width: 0%;"><span
                                        class="glyphicon glyphicon-th col-lg-4"></span></span>
                            </div>
                        </div>
                    </div>

                    <div id="dateOnlyDiv">
                        <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">التعليق</label>
                            <input type="text" class="form-control search_bill" placeholder="التعليق"
                                   name="sellbillcomment" id="sellbillcomment">
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="sr-only">بحث بـ</label>
                        <div class="input-group">
                            <label> <input value="dateOnly" name="search" id="searchdateOnly" type="radio" checked
                                           onclick='localStorage.setItem("search", "dateOnly");
                                                   window.location.reload();'>
                                تاريخ</label>
                            <label>
                                <input value="dateTime" name="search" id="searchdateTime" type="radio" onclick='localStorage.setItem("search", "dateTime");
                                        window.location.reload();'>
                                تاريخ ووقت</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="sr-only">نوع الفاتورة</label>
                        <div class="input-group">
                            <label> <input value="1" name="obgyBillType" id="obgyBillType" type="radio">
                                مباشر</label>
                            <label>
                                <input value="2" name="obgyBillType" id="obgyBillType" type="radio">
                                مستهلك</label>
                            <label>
                                <input value="3" name="obgyBillType" id="obgyBillType" type="radio">
                                استقبال</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="sr-only">اخفاء المحذوف
                            <input type="checkbox" class="search_bill col-lg-12" name="hideDel" id="hideDel"
                                   value="1"></label>
                    </div>
                    <div class="clearfix"></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 id="tableWrap">
                <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>
                <div class="message-table"> {$message}</div>
                {literal}


                <form method="post" action="sellbillController.php?do=executeOperation" id="excuteOperationForm"
                      onsubmit="if (Spry.Widget.Form.validate(this)) {
                                  return countChoosenProducts('sellbillController');
                              } else {
                                  return false;
                              }">
                    {/literal}

                    <table class="table table-black dataTableClass" id="excelTable">
                        <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>المدفوع</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>
                                {if $Programsettingdata->showbilldeliverydate == 1}
                                <th {if {$billsettingsData[112]->billsettingsvalue} eq 1}
                                    class="hidden-print"{/if}>تاريخ
                                    التسليم</th>
                                {/if}{if $Programsettingdata->showbillstatus == 1 &&
                                $billsettingsData[110]->billsettingsvalue eq 0}
                                <th class="hidden-print">الحالة</th>
                                {/if}
                                <th>الصوره</th>
                                {if $Programsettingdata->showBillCollectionSearch eq 1}<th class="hidden-print">تاريخ
                                    الاستحقاق</th>{/if}
                                <th class="hidden-print">تفاصيل الفاتورة</th>
                                <th class="hidden-print">اكسل</th>
                                <th class="hidden-print">PDF</th>
                                {if $Programsettingdata->ebill eq 1}<th>ارسال الفاتوره</th>
                                <th class="hidden-print">pdf</th>{/if}
                                {if $usergroup->allowEditDelInBills eq 0}<th class="hidden-print">تعديل الفاتورة</th>
                                <th class="hidden-print">حذف</th>{/if}
                                <th class="hidden-print">قيد اليومية</th>
                                {if $usergroup->netprofit eq 0}<th class="hidden-print">ربحية الفاتورة</th>{/if}
                            </tr>
                        </thead>
                        <tbody>
                            {assign var=piecesCount value=0}
                            {assign var=payedTotal value=0}
                            {assign var=remainTotal value=0}
                            {assign var=totalDiscount value=0}
                            {assign var=totalBillsNoDiscount value=0}
                            {foreach $sellbillData as $sellbill}


                            {if isset($sellbill->newSellbillidAfterEdit)&& $sellbill->newSellbillidAfterEdit > 0}
                            <!--this bill is deleted for editing-->
                            <tr class="danger">
                                {elseif {$sellbill->conditions} <> 0}
                            <tr class="dark">
                                {else}
                                {assign var=piecesCount value=$piecesCount+$sellbill->sellQuantity}
                                {assign var=payedTotal value=$payedTotal+$sellbill->sellbilltotalpayed}
                                {assign var=totalDiscount value=$totalDiscount + $sellbill->sellbilldiscount}
                                {assign var=totalBillsNoDiscount value=$totalBillsNoDiscount +
                                ($sellbill->sellbillaftertotalbill + $sellbill->sellbilldiscount)}
                            <tr>
                                {/if}
                                <td class="hidden-print">
                                    <input type="checkbox" name="choosedItem[]" id="checked{$i}" class="choosenItem"
                                           value="{$sellbill->sellbillid}" />
                                </td>
                                {if $Programsettingdata->clientserial eq 1}<td>{$sellbill->sellbillid}</td>{/if}
                                {if {$Programsettingdata->billid} eq 0} <td>{$sellbill->sellbillserial}</td>{/if}
                                {if $Programsettingdata->showparcode eq 1} <td>{$sellbill->parcodepro}</td> {/if}
                                <td>{if $sellbill->pricetype == 0}جملة{else if $sellbill->pricetype== 1}تجزئة{else if
                                    $sellbill->pricetype== 2}نصف جملة{else if $sellbill->pricetype== 4}سعر4{else if
                                    $sellbill->pricetype== 5}سعر5{else if $sellbill->pricetype== 6}سعر6{else if
                                    $sellbill->pricetype== 7}سعر7{else if $sellbill->pricetype== 8}سعر8{/if}</td>
                                <td>{$sellbill->sellbillaftertotalbill +
                                    $sellbill->sellbilldiscount}<!--{*$sellbill->sellbilltotalbill*}--></td>
                                <td>{$sellbill->sellbilldiscount}</td>
                                <td>{$sellbill->sellbillaftertotalbill}</td>
                                <td><span
                                        class="{if $sellbill->sellbillfinalbill > 0}hasRemain{/if}">{$sellbill->sellbilltotalpayed}</span>
                                </td>
                                <td>{$sellbill->sellbilldate}</td>
                                <td> {if {$sellbill->clientname} eq ""}
                                    نقدى
                                    {else}
                                    {$sellbill->clientname}
                                    {/if} </td>
                                <td {if {$billsettingsData[3]->billsettingsvalue} eq 0} style="display:block" {else}
                                    style="display:none" {/if}>{$sellbill->employeename}</td>
                                <td>{$sellbill->useremployeename}{if $sellbill->delbyusername}<br /><span
                                        style="color: red">{$sellbill->delbyusername}</span>{/if}</td>
                                <td>{$sellbill->storeName}</td>
                                <td>{$sellbill->savename}</td>
                                <td><input type="text" readonly
                                           class="form-control awdatepicker shippingDate hidden-print"
                                           data-id="{$sellbill->sellbillid}" autocomplete="off"
                                           data-date-format="yyyy-mm-dd" name="shippingDate" id="shippingDate"
                                           value="{if $sellbill->shippingDate !='0000-00-00'}{$sellbill->shippingDate}{/if}">
                                </td>
                                {if $Programsettingdata->showbilldeliverydate == 1}
                                <td {if {$billsettingsData[112]->billsettingsvalue} eq 1}
                                    class="hidden-print"{/if}>{$sellbill->datestarting}</td>
                                {/if}
                                {if $Programsettingdata->showbillstatus == 1 &&
                                    $billsettingsData[110]->billsettingsvalue eq 0}
                                <td class="hidden-print">
                                    <a href="javascript:;" type="button" class="btn btn-lm btn-warning showStModal"
                                       data-id="{$sellbill->sellbillid}" data-status="{$sellbill->billStatus}">
                                        {if $sellbill->billStatus == "new"}
                                        {$smarty.const.SELL_BILL_STATUS_NEW}
                                        {elseif $sellbill->billStatus == "prepare"}
                                        {if $billsettingsData[111]->billsettingsvalue eq 0}
                                        {$smarty.const.SELL_BILL_STATUS_PREPARE}
                                        {else}
                                        {$smarty.const.SELL_BILL_STATUS_FACTORY_SEND}
                                        {/if}
                                        {elseif $sellbill->billStatus == "shipped"}
                                        {if $billsettingsData[111]->billsettingsvalue eq 0}
                                        {$smarty.const.SELL_BILL_STATUS_SHIPPED}
                                        {else}
                                        {$smarty.const.SELL_BILL_STATUS_FACTORY_DELIVERED}
                                        {/if}
                                        {elseif $sellbill->billStatus == "deliver"}
                                        {$smarty.const.SELL_BILL_STATUS_DELIVERED}
                                        {elseif $sellbill->billStatus == "refused"}
                                        {$smarty.const.SELL_BILL_STATUS_REFUSED}
                                        {/if}
                                    </a>
                                </td>
                                {/if}
                                <td style="width: 30px;">
                                    <div id="imagesuploadfile{$sellbill->sellbillid}">
                                        <img src="../views/default/images/sellbill/{$sellbill->images}"
                                             data-sellbill="{$sellbill->sellbillid}" class="clickuploadfile"
                                             style="width: 40px;height: 40px;border: 1px solid;border-radius: 6px;">
                                    </div>
                                    <input type="file" name="file" id="uploadfile{$sellbill->sellbillid}"
                                           data-sellbill="{$sellbill->sellbillid}"
                                           data-imageid="{$sellbill->sellbillimageid}" style="display:none" />

                                </td>
                                {if $Programsettingdata->showBillCollectionSearch eq 1}<td>{if $sellbill->collectionDate
                                    !='0000-00-00'}{$sellbill->collectionDate}{/if}</td>{/if}
                                <td class="hidden-print"> <a
                                        href="sellbillController.php?do=showDetail&sellbillid={$sellbill->sellbillid}"
                                        type="button" class="btn btn-default btn-lg editicon"></a> </td>
                                <td class="hidden-print"> <a
                                        href="sellbillController.php?do=billToExcel&sellbillid={$sellbill->sellbillid}"
                                        type="button" class="btn btn-default btn-lg excelicon"></a> </td>
                                <td class="hidden-print"> <a target="_blank"
                                                             href="sellbillController.php?do=billToPDF&id={$sellbill->sellbillid}"
                                                             type="button" class="btn btn-default btn-lg pdficon"></a> </td>
                                {if $Programsettingdata->ebill eq 1}
                                {if $sellbill->conditions != 1}
                                <td class="hidden-print" id="ebill_{$sellbill->sellbillid}">
                                    {if empty($sellbill->estatus)}
                                    <a href="javascript:;" data-sellbillid="{$sellbill->sellbillid}" type="button"
                                       class="btn btn-default  sendbill "> ارسال</a>
                                    {else if $sellbill->estatus == '-1' || $sellbill->estatus == '-2' }
                                    <a href="javascript:;" data-sellbillid="{$sellbill->sellbillid}" type="button"
                                       class="btn btn-default  sendbill"> حدث خطأ </a>
                                    <a href="sellbillController.php?do=ebillerrors&sellbillid={$sellbill->sellbillid}"
                                       type="button" class="btn btn-default"> المشكله</a>
                                    {else}
                                    <a href="electronicinvoice.php?do=detail&uuid={$sellbill->estatus}" type="button"
                                       class="btn btn-default"> تفاصيل</a>
                                    {/if}
                                </td>
                                <td id="epdf{$sellbill->sellbillid}">
                                    {if !empty($sellbill->estatus) && $sellbill->estatus != '-1' && $sellbill->estatus
                                    != '-2'}
                                    {if !$sellbill->epdf}
                                    <a href="javascript:;" data-uuid="{$sellbill->estatus}"
                                       data-sellbillid="{$sellbill->sellbillid}" id="uuid{$sellbill->sellbillid}"
                                       type="button" class="btn btn-default  downloadepdf"> pdf </a>
                                    {else}
                                    <a href="{$sellbill->epdf}" type="button" class="btn btn-default"> pdf</a>
                                    {/if}
                                    {/if}
                                </td>
                                {else}
                                <td>
                                    لا يمكن الارسال
                                </td>
                                <td>
                                    لا يمكن الارسال
                                </td>
                                {/if}

                                {/if}
                                {if $usergroup->allowEditDelInBills eq 0}<td class="hidden-print">
                                    {if {$sellbill->conditions} eq 0}
                                    <a href="sellbillController.php?do=editsellBill&sellbillid={$sellbill->sellbillid}"
                                       type="button" class="btn btn-default btn-lg editicon"></a>
                                    {else}


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



                                    {/if}
                                </td>
                                <td class="hidden-print"> {if {$sellbill->conditions} eq 0} <a href="#" type="button"
                                                                                               class="btn btn-default btn-lg removeicon conformdel1"
                                                                                               data-position="sellbillController.php?do=delete&sellbillid={$sellbill->sellbillid}&action={$sellbill->dailyentryid}"></a>
                                    {else}
                                    {if isset($sellbill->newSellbillidAfterEdit)&& $sellbill->newSellbillidAfterEdit >
                                    0} <!--this bill is deleted for editing-->
                                    حذف للتعديل
                                    {else}
                                    تم حذف الفاتورة
                                    {/if}

                                    {/if} </td>{/if}

                                <td class="hidden-print">
                                    <a href="dailyentry.php?id={$sellbill->dailyentryid}" target="_blank" type="button"
                                       class="btn btn-default btn-lg editicon"></a>
                                </td>
                                {if $usergroup->netprofit eq 0}<td class="hidden-print">
                                    <a type="button" class="btn btn-default btn-lg infoicon calBillProfit"
                                       data-id="{$sellbill->sellbillid}"></a>
                                </td>{/if}
                            </tr>

                            {/foreach}

                        </tbody>

                    </table>
            </div>


            <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 -->
            <!-- Modal -->
            <div class="hidden-print">
                <div class="modal fade " id="profitModal" 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" id="profitModalBody">

                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">اغلاق</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>
                                        <option value="2">طباعة مجمعة</option>
                                    </select>
                                    <span class="selectInvalidMsg">*</span></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-lg-4">
                                <input name="" id="excuteOperationBtn" class="noprint btn btn-primary" type="button"
                                       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">عدد الفواتير:  {$billsCount}</h4>
                                    </div>
                                </div>-->
                <div class="col-lg-4 "><!-- col-lg-offset-8 -->
                    <div class="well well-sm">
                        <h4 class="text-center">إجمالي المبيعات: <span id="totalBills">{$totalBillsNoDiscount}</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">{$totalDiscount}</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 class="hidden-print">
                <span><button class="btn btn-success pull-right excelFile" data-tblId="excelTable" data-path="">ملف
                        اكسل</button></span>
                <span id="ExcelStatus"></span>
            </div>
        </div>
    </div>
</div>


<!-- Modal -->
<div class="hidden-print">
    <div class="modal fade " id="statusModal" 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">
                    <form method="post" action="sellbillController.php?do=changeStatus">
                        <input type="hidden" name="sellbillid" id="statusSellbillId" value="" />
                        <select name="status" id="sellbillStatus" data-placeholder="اختر الحالة" class="select wdt90">
                            <option value="new">{$smarty.const.SELL_BILL_STATUS_NEW}</option>
                            <option value="prepare">
                                {if $billsettingsData[111]->billsettingsvalue eq 0}
                                {$smarty.const.SELL_BILL_STATUS_PREPARE}
                                {else}
                                {$smarty.const.SELL_BILL_STATUS_FACTORY_SEND}
                                {/if}
                            </option>
                            <option value="shipped">
                                {if $billsettingsData[111]->billsettingsvalue eq 0}
                                {$smarty.const.SELL_BILL_STATUS_SHIPPED}
                                {else}
                                {$smarty.const.SELL_BILL_STATUS_FACTORY_DELIVERED}
                                {/if}
                            </option>
                            <option value="deliver">
                                {$smarty.const.SELL_BILL_STATUS_DELIVERED}
                            </option>
                            <option value="refused">
                                {$smarty.const.SELL_BILL_STATUS_REFUSED}
                            </option>
                        </select>
                </div>
                <div class="modal-footer">
                    <input type="submit" class="btn btn-success" value="حفظ" />
                    </form>
                    <button type="button" class="btn btn-default" data-dismiss="modal">اغلاق</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
</div>
<!-- /.modal -->

{literal}
<script type="text/javascript">
    var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1", {isRequired: false, invalidValue: "-1"});
    //

    var search = localStorage.getItem("search");
    if (search == "dateTime") {
        $('#searchdateTime').attr('checked', 'checked');
        $('#searchdateOnly').removeAttr('checked');
        //
        $('#dateTimeDiv').show();
        $('#dateOnlyDiv').remove();
    } else {
        $('#searchdateOnly').attr('checked', 'checked');
        $('#searchdateTime').removeAttr('checked');
        //
        $('#dateOnlyDiv').show();
        $('#dateTimeDiv').remove();
    }

    $('.sendbill').live('click', function (e) {
        var sellbillid = Number($(this).attr('data-sellbillid'));
        $(this).hide();
        $("#ebill_" + sellbillid).html("<span><img style=width:30px;height:30px' src='../views/default/assets/img/icons8-spinner.gif'/></span>");
        var thisIs = $(this);
        if (sellbillid > 0) {

            $.ajax({
                type: "POST",
                url: 'eBillRequests.php?do=etsellbill',
                data: {sellbillid: sellbillid},
                success: function (data) {
                    $("#ebill_" + sellbillid).html("");
                    $(thisIs).show();
                    if (data == 1) {
                        $('#ebill_' + sellbillid).html('تم الارسال ');
                    } else {
                        $('#ebill_' + sellbillid).html(' حدث خطأ');
                        window.open('sellbillController.php?do=ebillerrors&sellbillid=' + sellbillid);
                    }
                }
            });
        }
    });

    $('.downloadepdf').live('click', function (e) {
        var sellbillid = Number($(this).attr('data-sellbillid'));
        var uuid = $(this).attr('data-uuid');
        $(this).hide();
        $("#epdf" + sellbillid).html("<span><img style=width:30px;height:30px' src='../views/default/assets/img/icons8-spinner.gif'/></span>");
        var thisIs = $(this);
        if (uuid) {
            $.ajax({
                type: "POST",
                url: 'eBillRequests.php?do=epdf',
                dataType: 'json',
                data: {uuid: uuid, sellbillid: sellbillid},
                success: function (data) {

                    if (data != 0) {
                        $("#epdf" + sellbillid).html('<a href="' + data + '" type="button" class="btn btn-default"> pdf</a> ');
                        window.open("" + data + "", "_blank");
                    }

                }
            });
        }
    });
    //
    $('#excuteOperationBtn').live('click', function (e) {
        e.preventDefault();
        e.stopPropagation();
        //    $('#excuteOperationForm').
        var ids = '';
        $(".choosenItem:checked").each(function () {
            ids += $(this).val() + ',';
        });

        if (ids != "") {
            if ($('#operation').val() == "2") {
                window.open('sellbillprintController.php?do=showDetail2Bulk&id=' + ids);
            } else if ($('#operation').val() == 1) {
                var arr = ids.split(",");

                for (let i = 0; i < arr.length; i++) {
                    if (arr[i] != "") {
                        const sellbillid = arr[i];
                        $.post('sellbillController.php?do=delete&sellbillid=' + sellbillid, {curlpost: 1}, function (data) {
                            location.reload();
                        }).fail(function (xhr, status, error) {
                            console.log(error);
                        });
                    }
                }
            } else {
                return true;
            }
        } else {
            $("#noElementsError").css("display", "block");
            setTimeout(function () {
                $("#noElementsError").css("display", "none");
            }, 4000);
        }
    });

    $('.shippingDate').live('change', function (e) {
        var id = Number($(this).data('id'));
        var val = $(this).val();
        var thisIs = $(this);
        if (id > 0 && val != "") {
            $.ajax({
                type: "POST",
                url: 'sellbillAjaxController.php?do=updateshippingDate',
                data: {shippingDate: val, id},
                success: function (data) {
                    if (data == 1) {
                        thisIs.addClass('success_f');
                        setTimeout(function () {
                            thisIs.removeClass('success_f');
                        }, 2000);
                    } else {
                        thisIs.addClass('error_f');
                        setTimeout(function () {
                            thisIs.removeClass('error_f');
                        }, 2000);
                    }
                },
                error: function () {
                    thisIs.addClass('error_f');
                    setTimeout(function () {
                        thisIs.removeClass('error_f');
                    }, 2000);
                }
            });
        }
    });

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

        e.preventDefault();

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

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

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

        window.location.href = pos;
    });


    $('.clickuploadfile').live('click', function (e) {
        var sellbill = $(this).attr('data-sellbill');
        uploadfile(sellbill);
        $("#uploadfile" + sellbill).click();

    });
    function uploadfile(sellbill) {
        $("#uploadfile" + sellbill).on('change', (function (e) {
            e.preventDefault();
            var formData = new FormData();
            formData.append('imageid', $(this).attr('data-imageid'));
            formData.append('sellbillid', sellbill);
            formData.append('imageu', $(this)[0].files[0]);
            $.ajax({
                url: 'allajax.php?do=uploadfile',
                type: "POST",
                data: formData,
                contentType: false,
                cache: false,
                processData: false,
                success: function (data) {
                    $('#imagesuploadfile' + sellbill).html(data);
                },
            });
        }));
    }
    $('.calBillProfit').live('click', function () {
        var id = $(this).data('id');
        $.ajax({
            type: "POST",
            url: 'sellbillAjaxController.php?do=billprofit',
            data: {id: id},
            //async: false,
            success: function (data) {
                data = JSON.parse(data);

                $html = '<form class="form-horizontal">';
                if (data.u > 0) {
                    Object.keys(data.cost).forEach(function (index, value) {
                        $cost = Number(Number(data.cost[index]).toFixed(2));
                        $profit = Number(Number(data.totalbill - $cost).toFixed(2));
                        //                        $priceName = getPriceName(index);

                        $html += '<div class="form-group">' +
                                '    <label class="col-lg-3 control-label">صافى الفاتورة</label>' +
                                '    <div class="col-lg-6">' +
                                '        <input class="form-control" type="text" value="' + Number(data.totalbill) + '" readonly/>' +
                                '    </div>' +
                                '</div>';
                        $html += '<div class="form-group">' +
                                '    <label class="col-lg-3 control-label">التكلفة</label>' +
                                '    <div class="col-lg-6">' +
                                '        <input class="form-control" type="text" value="' + $cost + '" readonly/>' +
                                '    </div>' +
                                '</div>';
                        $html += '<div class="form-group">' +
                                '    <label class="col-lg-3 control-label">الربح</label>' +
                                '    <div class="col-lg-6">' +
                                '        <input class="form-control" type="text" value="' + $profit + '"  readonly/>' +
                                '    </div>' +
                                '</div>';
                    });
                } else {
                    $html += '<table class="table">' +
                            '    <thead>' +
                            '        <tr>' +
                            '            <th>صافى الفاتورة</th>' +
                            '            <th>نوع السعر</th>' +
                            '            <th>التكلفة</th>' +
                            '            <th>الربح</th>' +
                            '        </tr>' +
                            '    </thead>' +
                            '    <tbody>';
                    Object.keys(data.cost).forEach(function (index, value) {
                        $cost = Number(Number(data.cost[index]).toFixed(2));
                        $profit = Number(Number(data.totalbill - $cost).toFixed(2));
                        $priceName = getPriceName(index);

                        $html += '        <tr>' +
                                '           <td>' + Number(data.totalbill) + '</td>' +
                                '            <td>' + $priceName + '</td>' +
                                '           <td>' + $cost + '</td>' +
                                '            <td>' + $profit + '</td>' +
                                '        </tr>';

                    });
                    $html += '    </tbody>' +
                            '</table>';
                }
                $html += '</form>';
                $('#profitModalBody').html($html);
                $('#profitModal').modal('show');
            }
        });
        function getPriceName(key) {
            let name = '';
            switch (key) {
                case 'buyprice':
                    name = 'سعر الشراء ';
                    break;
                case 'lastbuyprice':
                    name = 'اخر سعر شراء ';
                    break;
                case 'meanbuyprice':
                    name = 'متوسط سعر الشراء ';
                    break;
                case 'lastbuyprice_withDiscount':
                    name = 'اخر سعر شراء بعد الخصم ';
                    break;
                case 'meanbuyprice_withDiscount':
                    name = 'متوسط سعر الشراء بعد الخصم ';
                    break;
            }
            return name;
        }

    });
    $(function () {
        $(".select2Supp").select2({
            placeholder: "العميل",
            minimumInputLength: document.getElementById("searchvalue").value,
            inimumInputLength: 0,
            multiple: false,
            quietMillis: 100,
            //width: '80%',
            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: function () {
                            return (Number($('#withNakdi').val()) == 1) ? 0 : 1;
                        },
                        //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()});

        $("#tripid").select2({
            placeholder: "اختر رحلة",
            width: '100%',
            minimumInputLength: 0, //document.getElementById("searchvalue").value,
            multiple: false,
            quietMillis: 100,
            allowClear: true,
            ajax: {
                url: "expensesControllerajex.php?do=trips",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        term: term, //search term
                        page_limit: 10,

                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });
    });
    $('.showStModal').click(function (e) {
        e.preventDefault();
        var id = $(this).data('id');
        var status = $(this).data('status');
        $('#statusModal').modal('show');
        $('#statusSellbillId').val(id);
        $('#sellbillStatus option[value="' + status + '"]').attr("selected", true);
    });
</script>
{/literal}