| Current Path : /var/www/html/crm/views/debitnote/ |
| Current File : /var/www/html/crm/views/debitnote/_form.php |
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\ParamList;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use app\models\Vendor;
use app\models\Company;
use app\models\Customer;
use app\models\Product;
use app\models\DebitnoteItem;
/* @var $this yii\web\View */
/* @var $model app\models\Debitnote */
/* @var $form yii\widgets\ActiveForm */
$ParamList = new ParamList();
$disabled = false;
if (Yii::$app->controller->action->id == 'update')
$disabled = true;
?>
<div class="debitnote-form">
<div class="kt-container kt-container--fluid kt-grid__item kt-grid__item--fluid">
<div class="kt-portlet kt-portlet--mobile">
<div class="kt-portlet__body kt-portlet__body--fit">
<div class="kt-portlet__head">
<div class="kt-portlet__head-label">
<span class="kt-font-info"><i class="fas fa-info-circle"></i> Field with <span class="required">*</span> is required</span>
</div>
</div>
<div class="kt-portlet__body">
<?php $form = ActiveForm::begin(['options' => ['class' => 'kt-form kt-form--label-right']]); ?>
<div class="row">
<div class="col-4">
<div class='form-group-custom'>
<?php
echo $form->field($model, 'type', [
'template' => "{label}<span class='required'>*</span>{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"])
->dropDownList(['product' => 'Product', 'freetext' => 'Freetext'], [
'disabled' => $disabled, 'prompt' => '- Please choose -',
'class' => 'form-control kt-selectpicker', 'data-live-search' => 'true']);
?>
</div>
</div>
<div class="col-4">
<div class='form-group-custom'>
<?php
echo $form->field($model, 'company_id', [
'template' => "{label}<span class='required'>*</span>{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"])
->dropDownList(ArrayHelper::map(Company::find()->orderBy(['name' => SORT_ASC])->all(), 'id', 'name'), [
'disabled' => $disabled, 'prompt' => '- Please choose -',
'class' => 'form-control kt-selectpicker', 'data-live-search' => 'true']);
?>
</div>
</div>
<div class="col-4">
<div class='form-group-custom'>
<?=
$form->field($model, 'doc_dt', [
'template' => "{label}<span class='required'>*</span>
<div class='input-group'>
{input}
<div class='input-group-append'><span class='input-group-text'><i class='la la-calendar-check-o'></i></span></div>
</div>
<span class='kt-font-danger font-weight-bold'>{error}</span>"
])->textInput(['readonly' => true]);
?>
</div>
</div>
<div class="col-4">
<div class='form-group-custom'>
<?=
$form->field($model, 'ref', [
'template' => "{label}{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"
])->textInput(['maxlength' => true]);
?>
</div>
</div>
<div class="col-4">
<div class='form-group-custom'>
<?=
$form->field($model, 'customer_id', [
'template' => "{label}{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"])
->dropDownList(ArrayHelper::map(Customer::find()->orderBy(['name' => SORT_ASC])->all(), 'id', 'name'), [
'prompt' => '- Please choose -', 'class' => 'form-control kt-selectpicker', 'data-live-search' => 'true', 'onchange' => 'changecustomer(this.value)']);
?>
</div>
</div>
<div class="col-4">
<div class='form-group-custom'>
<?=
$form->field($model, 'customer_name', [
'template' => "{label}<span class='required'>*</span>{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"
])->textInput(['maxlength' => true]);
?>
</div>
</div>
<div class="col-6">
<div class='form-group-custom'>
<?=
$form->field($model, 'customer_address1', [
'template' => "{label}<span class='required'>*</span>{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"
])->textInput(['maxlength' => true, 'placeholder' => "Address Line 1"]);
?>
</div>
</div>
<div class="col-6">
<div class='form-group-custom'>
<?=
$form->field($model, 'customer_address2', [
'template' => "<label class='control-label'> </label>{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"
])->textInput(['maxlength' => true, 'placeholder' => "Address Line 2"])->label(false);
?>
</div>
</div>
<div class="col-4">
<div class='form-group-custom'>
<?=
$form->field($model, 'customer_postcode', [
'template' => "{label}<span class='required'>*</span>{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"
])->textInput(['maxlength' => true]);
?>
</div>
</div>
<div class="col-4">
<div class='form-group-custom'>
<?=
$form->field($model, 'customer_city', [
'template' => "{label}<span class='required'>*</span>{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"
])->textInput(['maxlength' => true]);
?>
</div>
</div>
<div class="col-4">
<div class='form-group-custom'>
<?=
$form->field($model, 'customer_state', [
'template' => "{label}<span class='required'>*</span>{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"])
->dropDownList($ParamList->getparamlist(3), ['class' => 'form-control kt-selectpicker']);
?>
</div>
</div>
<div class="col-4">
<div class='form-group-custom'>
<?=
$form->field($model, 'customer_pic', [
'template' => "{label}<span class='required'>*</span>
<div class='input-group'>
{input}
<div class='input-group-append'>
<button class='btn btn-warning' type='button' onclick='choosecontact()'>Select</button>
</div>
</div>
<span class='kt-font-danger font-weight-bold'>{error}</span>"
])->textInput(['maxlength' => true, 'placeholder' => 'Click "Select" to choose from "Contact Person"']);
?>
</div>
</div>
<div class="col-4">
<div class='form-group-custom'>
<?=
$form->field($model, 'customer_phone', [
'template' => "{label}<span class='required'>*</span>{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"
])->textInput(['maxlength' => true]);
?>
</div>
</div>
<div class="col-12">
<div class='form-group-custom'>
<?= $form->field($model, 'notes', ['template' => "{label}{input}{error}"])->textarea(['rows' => 6]); ?>
</div>
</div>
</div>
<?php if (Yii::$app->controller->action->id == 'update') { ?>
<table class="table table-bordered table-striped" id="wrapper_product_list">
<thead>
<tr class="thead-dark">
<th width="1%">Action</th>
<th>Description</th>
<th width="10%">Qty</th>
<?php if ($model->type == 'product') { ?>
<th width="10%">UoM</th>
<?php } ?>
<th width="10%">Unit Price (RM)</th>
<th width="10%">Discount (%)</th>
<th width="10%">Total (RM)</th>
</tr>
</thead>
<tbody>
<?php
$modelItem = DebitnoteItem::find()->where(['doc_id' => $model->id])->orderBy(['id' => SORT_ASC])->all();
$Product = new Product();
$colspan = 5;
if ($model->type == 'product')
$colspan = 6;
if (!empty($modelItem)) {
foreach ($modelItem as $rowItem) {
$uom_label = '';
$descr_label = '';
$stock_owner_label = '';
if ($model->type == 'product') {
$uom_label = '<td><input type="text" class="form-control" value="' . $rowItem->uom . '" name="item_uom_' . $rowItem->id . '"></td>';
$descr_label = $Product->getname($rowItem->product_id);
}
if ($model->type == 'freetext') {
$descr_label = '<input type="text" class="form-control" value="' . $rowItem->descr . '" name="item_descr_' . $rowItem->id . '">';
}
echo '<tr id="rowItem_' . $rowItem->id . '">
<td align="center" style="vertical-align:middle;"><i class="fas fa-trash-alt kt-font-danger" style="cursor:pointer;" title="Delete product" onclick="deleteproduct(' . $rowItem->id . ')"></i></td>
<td style="vertical-align:middle;">
<input type="hidden" value="' . $rowItem->id . '" name="item_id[]">
' . $descr_label . '
</td>
<td><input type="text" class="form-control" value="' . $rowItem->quantity . '" name="item_quantity_' . $rowItem->id . '" onchange="updatetotal(' . $rowItem->id . ')"></td>
' . $uom_label . $stock_owner_label . '
<td><input type="text" class="form-control" value="' . $rowItem->unit_price . '" name="item_unitprice_' . $rowItem->id . '" onchange="updatetotal(' . $rowItem->id . ')"></td>
<td><input type="text" class="form-control" value="' . $rowItem->discount . '" name="item_discount_' . $rowItem->id . '" onchange="updatetotal(' . $rowItem->id . ')"></td>
<td><input type="text" class="form-control item_total" value="' . $rowItem->total . '" name="item_total_' . $rowItem->id . '"></td>
</tr>';
}
}
?>
</tbody>
<tfoot>
<tr>
<th colspan="<?= $colspan; ?>" class="text-right" style="vertical-align: middle;">Grand Total</th>
<td class="pb-0">
<div class='form-group-custom'>
<?=
$form->field($model, 'grandtotal', ['template' => "{label}{input}<span class='kt-font-danger font-weight-bold'>{error}</span>"
])->textInput(['maxlength' => true, 'readonly' => true])->label(false);
?>
</div>
</td>
</tr>
</tfoot>
</table>
<?php } ?>
</div>
<div class="kt-portlet__foot">
<div class="kt-form__actions">
<?= Html::a('<i class="fas fa-arrow-left"></i> Back', ['index'], ['class' => 'btn btn-outline-brand']) ?>
<?= Html::a('<i class="fas fa-sync"></i> Reset', [Yii::$app->controller->action->id, 'uuid' => Yii::$app->getRequest()->getQueryParam('uuid')], ['class' => 'btn btn-outline-success']); ?>
<?= Html::submitButton('<i class="far fa-save"></i> Save', ['class' => 'btn btn-info']); ?>
<?php
if (Yii::$app->controller->action->id == 'update') {
if ($model->type == 'product')
echo Html::button('<i class="fas fa-box-open"></i> Add Item', ['class' => 'btn btn-outline-warning', 'onclick' => 'addproduct()']);
if ($model->type == 'freetext')
echo Html::button('<i class="fas fa-box-open"></i> Add Item', ['class' => 'btn btn-outline-warning', 'onclick' => 'addfreetext()']);
echo ' ' . Html::a('<i class="far fa-file-pdf"></i> View Debit Note', ['download', 'uuid' => Yii::$app->getRequest()->getQueryParam('uuid')], ['class' => 'btn btn-danger', 'target' => '_blank']);
}
?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header"><h5 class="modal-title" id="exampleModalLongTitle">Product List</h5></div>
<div class="modal-body">
<input type="hidden" class="form-control" id="doc_id" value="<?= $model->id; ?>">
<div id="productlist_wrapper"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-primary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" onclick="saveproduct()">Add product</button>
</div>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="modalContact" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Contact Person List</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body"><div id="contactwrapper"></div></div>
</div>
</div>
</div>
<?php
$this->registerJs("$('#debitnote-doc_dt').datepicker({
rtl: KTUtil.isRTL(),
todayHighlight: true,
orientation: 'bottom left',
format: 'yyyy-mm-dd',
clearBtn: true,
todayHighlight: true,
});");
?>
<script>
function addfreetext() {
var doc_id = $('#doc_id').val();
$.ajax({
url: "index.php?r=debitnote/addfreetext",
method: "POST",
data: {"doc_id": doc_id},
success: function (response) {
if (response !== '') {
$('#wrapper_product_list').append(response);
}
},
});
}
function addproduct() {
$('#productlist_wrapper').html('');
var com_id = $('#debitnote-company_id').val();
var doc_id = $('#doc_id').val();
$.ajax({
url: "index.php?r=debitnote/getproductlist",
method: "POST",
data: {"com_id": com_id, "doc_id": doc_id},
success: function (response) {
$('#productlist_wrapper').html(response);
$('#error_message').hide();
$('#myModal').modal('show');
},
});
}
function saveproduct() {
var doc_id = $('#doc_id').val();
var company_id = $('#debitnote-company_id').val();
var items = [];
$('.product_id').each(function () {
if ($(this).is(":checked")) {
var item_val = $(this).val();
items.push(item_val);
}
});
if (items.length === 0) {
$('#error_message').show();
} else {
$.ajax({
url: "index.php?r=debitnote/addproduct",
method: "POST",
data: {"doc_id": doc_id, "items": items, "company_id": company_id},
success: function (response) {
if (response !== '') {
$('#myModal').modal('hide');
$('#wrapper_product_list').append(response);
$('#wrapper_product_list .kt-selectpicker').selectpicker('refresh');
}
},
});
}
}
function changecustomer(id) {
if (id !== '') {
$.ajax({
url: "index.php?r=quotation/getcustomerdetails",
method: "POST",
data: {"id": id},
success: function (response) {
if (response !== '') {
var arr = response.split("<->");
$('#debitnote-customer_name').val(arr[0]);
$('#debitnote-customer_address1').val(arr[1]);
$('#debitnote-customer_address2').val(arr[2]);
$('#debitnote-customer_postcode').val(arr[3]);
$('#debitnote-customer_city').val(arr[4]);
$('#debitnote-customer_state').val(arr[5]);
$('#debitnote-customer_state').selectpicker('refresh');
$('#debitnote-customer_pic').val(arr[6]);
$('#debitnote-customer_phone').val(arr[7]);
}
},
});
}
}
function updatetotal(item_id) {
var quantity = parseInt($("input[name=item_quantity_" + item_id + "]").val());
var unitprice = parseFloat($("input[name=item_unitprice_" + item_id + "]").val());
var discount = parseFloat($("input[name=item_discount_" + item_id + "]").val());
var total = quantity * unitprice - (discount / 100 * (unitprice * quantity));
$("input[name=item_total_" + item_id + "]").val(total.toFixed(2));
updategrandtotal();
}
function updategrandtotal() {
var subtotal = 0;
$('.item_total').each(function () {
subtotal += parseFloat($(this).val());
});
$('#debitnote-grandtotal').val(subtotal.toFixed(2));
}
function deleteproduct(id) {
Swal.fire({
title: 'Are you sure to delete this product?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.value) {
$.ajax({
url: "index.php?r=debitnote/deleteproduct",
method: "POST",
data: {"id": id},
success: function (response) {
if (response === 'success') {
swal.fire({
position: 'center-center',
type: 'success',
title: 'Product has been deleted',
showConfirmButton: false,
timer: 1500,
}).then(function (result) {
$('#rowItem_' + id).remove();
updategrandtotal();
});
}
},
});
}
}
);
}
function choosecontact() {
var type = 'ctc_cust';
var data_id = $('#debitnote-customer_id').val();
if (data_id !== '') {
$.ajax({
url: "index.php?r=contact/getcontactlist",
method: "POST",
data: {"type": type, "data_id": data_id},
success: function (response) {
$('#contactwrapper').html(response);
$('#modalContact').modal('show');
},
});
} else {
alert("Customer cannot be empty if you want to choose Contact Person");
}
}
function changecontact(val) {
var arr = val.split("<->");
$('#debitnote-customer_pic').val(arr[0]);
$('#debitnote-customer_phone').val(arr[1]);
$('#modalContact').modal('hide');
}
</script>