<?php $__env->startSection('title', 'Edit Purchase Invoice'); ?>

<?php $__env->startPush('css'); ?>
    <!--select2-->
    <link href="<?php echo e(asset('admin/assets/plugins/select2/css/select2.css')); ?>" rel="stylesheet" type="text/css" />
    <link href="<?php echo e(asset('admin/assets/plugins/select2/css/select2-bootstrap.min.css')); ?>" rel="stylesheet" type="text/css" />
    <!-- date time -->
    <link href="<?php echo e(asset('admin/assets/plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css')); ?> " rel="stylesheet" media="screen">
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>

    <div class="page-bar">
        <div class="page-title-breadcrumb">

        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card card-box">
                <div class="card-head text-white " style="background-color:#3FCC7E;">
                    <header>Edit Purchase Invoice</header>
                </div>
                <div class="card-body" id="bar-parent">
                    <div class="row">
                        <div class="col-12">
                            <h3 class="text-center">
                                <?php echo e(env('COMPANY_NAME','QBYTESOFT')); ?> <span class="label label-rouded label-danger"><?php echo e(auth()->user()->branch->name ?? ''); ?></span>
                            </h3>
                        </div>
                    </div>
                    <?php echo Form::open(['route' => ['admin.purchases.update', $purchase->id], 'method' => 'PUT']); ?>

                    <div class="row">
                        <div class="col-12 col-md-3 col-sm-4">
                            <div class="form-group">
                                <label for="invoice_id">Invoice No.</label>
                                <input type="text" class="form-control" value="<?php echo e($purchase->purchase_no); ?>" readonly>
                            </div>
                        </div>
                        <div class="col-12 col-md-3 col-sm-4">
                            <div class="form-group">
                                <label for="user_id">Employee Name</label>
                                <input type="text" id="user_id" class="form-control" value="<?php echo e(Auth::user()->name); ?>" readonly>
                                <input type="hidden" name="user_d" value="<?php echo e(Auth::user()->id); ?>">
                            </div>
                        </div>

                        <div class="col-12 col-md-3 col-sm-4">
                            <div class="form-group">
                                <label for="branch_id">Branch Name</label>
                                <?php if(count($branches) > 1): ?>
                                    <select name="branch_id" id="branch_id" class="form-control select2">
                                        <?php $__currentLoopData = $branches; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $branch): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($branch->id); ?>" <?php echo e($purchase->branch_id == $branch->id ? 'selected' : ''); ?>><?php echo e($branch->name); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                <?php else: ?>
                                    <?php
                                        $firstBranch = $branches->first();
                                    ?>
                                    <input type="text" id="branch_id" class="form-control" value="<?php echo e(optional($firstBranch)->name ?? ''); ?>" readonly>
                                    <input type="hidden" name="branch_id" value="<?php echo e(optional($firstBranch)->id ?? ''); ?>">
                                <?php endif; ?>
                            </div>
                        </div>

                        <div class="col-12 col-sm-6 col-md-3">
                            <div class="form-group">

                                <label for="company_id">Company Name</label>
                                <select id="company_id" name="company_id" class="form-control  select2 " required>
                                    <option value="0">Select Company</option>
                                    <?php $__currentLoopData = $companies; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $companyName => $companyId): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($companyId); ?>" <?php echo e($purchase->company_id == $companyId ? 'selected' : ''); ?>><?php echo e($companyName); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-12 col-sm-6 col-md-3">
                            <div class="form-group">
                                <label class="">Bill Date</label>
                                <div class="input-group date form_date" data-date="<?php echo e($purchase->purchase_date); ?>" data-date-format="dd MM yyyy" data-link-field="dtp_input1">
                                    <input class="form-control" size="16" type="text" name="purchase_date" value="<?php echo e(\Carbon\Carbon::parse($purchase->purchase_date)->format('d M Y')); ?>">
                                    <span class="input-group-addon ml-2">
                                            <span class="fa fa-calendar"></span>
                                    </span>
                                </div>
                                <input type="hidden" id="dtp_input1" value="" />
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-12 col-sm-6 col-md-3">
                            <div class="form-group">
                                <label for="payment_type">Payment Type</label>
                                <select name="payment_type" id="payment_type" class="form-control select2">
                                    <?php $__currentLoopData = \App\Models\PaymentGateway::get(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $gateway): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?> 
                                        <option value="<?php echo e($gateway->id); ?>" <?php echo e($purchase->payment_type == $gateway->id ? 'selected' : ''); ?>><?php echo e($gateway->gateway_name); ?>(<?php echo e($gateway->gateway_account_number); ?>)</option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-12 col-sm-6 col-md-3">
                            <div class="form-group">
                                <label for="expense_head">Expense Head <small>(Required if you take some payment now)</small></label>
                                <select name="expense_head" id="expense_head" class="form-control select2">
                                    <?php
                                        $currentPurpose = optional($purchase->purchasepayment)->purpose;
                                    ?>
                                    <?php $__currentLoopData = \App\Models\ExpenseHead::get(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $expenseHead): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?> 
                                        <option value="<?php echo e($expenseHead->id); ?>" <?php echo e($currentPurpose == $expenseHead->id ? 'selected' : ''); ?>><?php echo e($expenseHead->name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                        </div>
                    </div>
                    </div>
                    <div class="row p-t-20" ng-controller="ProductSearchController">
                        <!-- Product Search  -->
                        <div class="col-xs-12 col-3">
                            <div class="form-group">
                                <label>Product Search</label>
                                <input ng-model="search" class="form-control underlined">
                            </div>

                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover">
                                    <tr ng-repeat="product in products  | filter: search | limitTo:10">
                                        <td width="80%">{{product.product_name}} </td>
                                        <td ><button class="btn btn-primary btn-xs" type="button" ng-click="addReceivingTemp(product, newreceivingtemp)"><span class="glyphicon glyphicon-share-alt" aria-hidden="true">&#x2705;</span></button></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                        <div class="col-9">
                            <div class="table-responsive">
                                <table class="table table-bordered">
                                    <tr>
                                        <th>Product name</th>
                                        <th>Size</th>
                                        <th>Cost Price</th>
                                        <th>MRP</th>
                                        <th>Quantity</th>
                                        <th>Discount</th>
                                        <th>Total</th>
                                        <th>&nbsp;</th>
                                    </tr>
                                    <tr ng-repeat="newreceivingtemp in receivingtemp">
                                        <td>{{newreceivingtemp.product.product_name}}</td>
                                        <td>{{ newreceivingtemp.product.size }}</td>
                                        <td><input type="text" style="text-align:center" autocomplete="off" ng-change="updateReceivingTemp(newreceivingtemp)" ng-model="newreceivingtemp.cost_price" size="8"></td>
                                        <td><input type="text" style="text-align:center" autocomplete="off" ng-change="updateReceivingTemp(newreceivingtemp)" ng-model="newreceivingtemp.selling_price" size="8"></td>
                                        <td width="15%"><input type="number" style="text-align:center;width: 100%" autocomplete="off" ng-change="updateReceivingTemp(newreceivingtemp)" ng-model="newreceivingtemp.quantity" step="any"></td>
                                        <td><input type="text" style="text-align:center" autocomplete="off" ng-change="updateReceivingTemp(newreceivingtemp)" ng-model="newreceivingtemp.discount" size="2"></td>
                                        <td>{{  (newreceivingtemp.cost_price * newreceivingtemp.quantity) - newreceivingtemp.discount | currency:'Tk'}}</td>
                                        <td><button class="btn btn-danger btn-xs" type="button" ng-click="removeReceivingTemp(newreceivingtemp.id)"><span class="fa fa-remove" aria-hidden="true"></span></button></td>
                                    </tr>
                                    <tfoot>
                                    <tr>
                                        <td colspan="4">
                                        </td>
                                        <td class="text-center">
                                            <b>
                                                TQ: {{ sumQuantity(receivingtemp) }}
                                            </b>

                                        </td>
                                        <td>
                                            <b>
                                                TD: {{ sumDiscount(receivingtemp) }}
                                            </b>

                                        </td>
                                        <td>
                                            <b>
                                                TA: {{ sum(receivingtemp) - sumDiscount(receivingtemp) | number:2 }}
                                            </b>
                                            <input type="hidden" name="sub_total" value="{{ sum(receivingtemp) - sumDiscount(receivingtemp) }}">
                                        </td>
                                        <td></td>
                                    </tr>
                                    </tfoot>
                                </table>
                            </div>
                            <div class="row">
                                <div class="col-6">
                                    <div class="row">
                                        <div class="col-6">
                                            <div class="form-group">
                                                <label for="invoiceDiscount">Discount</label>
                                                <input type="text" ng-model="invoiceDiscount" ng-init="invoiceDiscount=<?php echo e($purchase->discount); ?>" autocomplete="off" class="form-control" id="invoiceDiscount" name="invoiceDiscount">
                                            </div>
                                        </div>
                                        <div class="col-6">
                                            <div class="form-group">
                                                <label for="payment">Paid Amount</label>
                                                <input type="text" ng-model="payment" id="payment" ng-init="payment=<?php echo e(optional($purchase->purchasepayment)->payment ?? 0); ?>" class="form-control" name="payment">
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-6">
                                    <div class="row">
                                        <div class="col-12">
                                            <div class="form-group">
                                                <label for="totalInvoiceAmount">Total Invoice Amount</label>
                                                <input type="text" value="{{ sum(receivingtemp) - (invoiceDiscount + sumDiscount(receivingtemp)) | number:2 }}" class="form-control" id="totalInvoiceAmount" readonly style="font-weight: bold;text-align: center;font-size: 32px">
                                                <input type="hidden" name="grand_total" value="{{ sum(receivingtemp) - (invoiceDiscount + sumDiscount(receivingtemp)) }}">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-6">
                                    <div class="row">
                                        <div class="col-6">
                                            <div class="form-group">
                                                <label for="remarks">Remarks</label>
                                                <input type="text" name="remarks" class="form-control" id="remarks" value="<?php echo e($purchase->remarks); ?>">
                                            </div>

                                        </div>
                                        <div class="col-6">
                                            <div class="form-group">
                                                <label for="amountDue">Amount Due</label>
                                                <input type="text" id="amountDue" class="form-control" value="{{ (sum(receivingtemp) - (sumDiscount(receivingtemp) + invoiceDiscount)) - payment }}" readonly style="font-size: 18px;font-weight: bold; text-align: center">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="row">
                                        <div class="col-12">
                                            <div class="form-group">
                                                <?php echo Form::submit('Update Purchase',['class' => 'mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect m-b-10 btn-success btn-block form-control','style' => 'margin-left:10px;margin-top:30px;']); ?>

                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php echo Form::close(); ?>

                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('js'); ?>
    <!--select2-->
    <script src="<?php echo e(asset('admin/assets/plugins/select2/js/select2.js')); ?>" ></script>
    <script src="<?php echo e(asset('admin/assets/js/pages/select2/select2-init.js')); ?>" ></script>

    <!-- data time -->
    <script src="<?php echo e(asset('admin/assets/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js')); ?>"  charset="UTF-8"></script>
    <script src="<?php echo e(asset('admin/assets/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker-init.js')); ?>"  charset="UTF-8"></script>

    <!-- Form Control Script -->
    <script src="<?php echo e(asset('js/purchase.js')); ?>"></script>
    <script src="<?php echo e(asset('js/sale-form.js')); ?>"></script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('template.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\xampp\htdocs\forazi\resources\views/admin/purchaseinvocie/edit.blade.php ENDPATH**/ ?>