| Current Path : /var/www/html/dyna-crm/controllers/ |
| Current File : /var/www/html/dyna-crm/controllers/InvoicetransferController.php |
<?php
namespace app\controllers;
use Yii;
use app\models\Invoice;
use app\models\InvoiceSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use yii\helpers\Url;
use app\models\Log;
use app\models\LogSearch;
use app\models\Company;
use app\models\ParamList;
use kartik\mpdf\Pdf;
use app\models\User;
use app\models\Notes;
use app\models\InvoiceItem;
use app\models\Product;
use app\models\StockLog;
use app\models\StockTransfer;
class InvoicetransferController extends Controller {
public $menu_id = 91;
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['index', 'create', 'update', 'view'],
'rules' => [
// allow authenticated users
[
'allow' => true,
'roles' => ['@'],
],
// everything else is denied
],
],
];
}
public function actionIndex() {
$searchModel = new InvoiceSearch();
$dataProvider = $searchModel->searchtransfer(Yii::$app->request->queryParams);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
}
public function actionLog($uuid) {
$searchModel = new LogSearch();
$searchModel->menu_id = $this->menu_id;
$searchModel->data_uuid = $uuid;
$dataProvider = $searchModel->log(Yii::$app->request->queryParams);
return $this->render('log', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
}
public function actionCreate() {
$Company = new Company();
$model = new Invoice();
$model->type = 'transfer';
$model->doc_dt = date("Y-m-d");
$model->created_dt = date("Y-m-d H:i:s");
$model->created_by = Yii::$app->user->identity->id;
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$model->uuid = Uuid::uuid4();
if ($model->load(Yii::$app->request->post())) {
$countdoc = (int) Invoice::find()->where(['com_id' => $model->com_id])->andwhere(['type' => 'transfer'])->count() + 1;
$model->invoice_no = $Company->getshortcode($model->com_id) . '/STO/' . date("y") . '/' . sprintf("%05d", $countdoc);
if ($model->save()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'create', $model->uuid);
$this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 1500,}).then(function(result){window.location = 'index.php?r=invoicetransfer/update&uuid=$model->uuid';});");
}
}
return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
}
public function actionUpdate($uuid) {
$model = $this->findModel($uuid);
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
if ($model->load(Yii::$app->request->post())) {
if ($model->save()) {
if (!empty($_POST['item_id'])) {
$item_arr = $_POST['item_id'];
foreach ($item_arr as $item_id) {
if (($modelItem = InvoiceItem::findOne($item_id)) !== null) {
$modelItem->quantity = $_POST['item_quantity_' . $item_id];
$modelItem->uom = $_POST['item_uom_' . $item_id];
$modelItem->unit_price = $_POST['item_unitprice_' . $item_id];
$modelItem->discount = $_POST['item_discount_' . $item_id];
$modelItem->total = $_POST['item_total_' . $item_id];
$modelItem->save();
}
}
}
$Log = new Log();
$Log->insertlog($this->menu_id, 'update', $model->uuid);
$this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 1500,}).then(function(result){window.location = 'index.php?r=invoicetransfer/update&uuid=$model->uuid';});");
}
}
return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
}
protected function findModel($uuid) {
if (($model = Invoice::findOne(['uuid' => $uuid])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionCalcpaymentdue() {
$output = '';
$invoice_term_arr = explode('_', $_POST['invoice_term']);
if ($invoice_term_arr[0] == 'end') {
$start_date = date("Y-m-t", strtotime($_POST['invoice_dt']));
$output = date('Y-m-t', strtotime($start_date . ' + ' . $invoice_term_arr[1] . ' days'));
} else {
$start_date = date("Y-m-d", strtotime($_POST['invoice_dt']));
$output = date('Y-m-d', strtotime($start_date . ' + ' . $invoice_term_arr[1] . ' days'));
}
return $output;
}
public function actionGetcustomerdetails() {
$id = $_POST['id'];
$output = '';
if (($model = Company::findOne($id)) !== null) {
$output = $model->name . '<->' .
$model->address1 . '<->' .
$model->address2 . '<->' .
$model->postcode . '<->' .
$model->city . '<->' .
$model->state . '<->' .
$model->contact_name . '<->' .
$model->contact_no;
}
return $output;
}
public function actionGetproductlist() {
$product_keyword = $_POST['product_keyword'];
$output = '';
$modelProduct = Product::find()->where(['status' => 1])->andwhere(['like', 'name', $product_keyword])->orderBy(['name' => SORT_ASC])->all();
if (!empty($modelProduct)) {
$output .= '<div class="row">';
foreach ($modelProduct as $rowProduct) {
$output .= '<div class="col-6">
<label class="kt-checkbox">
<input type="checkbox" value="' . $rowProduct->id . '" class="product_id"> ' . $rowProduct->name . '
<span></span>
</label>
</div>';
}
$output .= '</div><div class="kt-font-bold kt-font-danger" id="error_message">Please select at lease one (1) product</div>';
}
return $output;
}
public function actionAddproduct() {
$output = '';
$Product = new Product();
$ParamList = new ParamList();
$output = '';
if (!empty($_POST['items'])) {
foreach ($_POST['items'] as $item) {
$model = new InvoiceItem();
$model->doc_id = $_POST['doc_id'];
$model->descr = $item;
$model->uom = $ParamList->getlabel(9, $Product->getuom($model->descr));
$model->quantity = '0';
$model->unit_price = $Product->getprice($model->descr);
$model->discount = '0';
$model->total = '0.00';
$model->created_dt = date("Y-m-d H:i:s");
$model->created_by = Yii::$app->user->identity->id;
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$model->owner_type = 'company';
$model->owner_id = $_POST['com_id'];
if ($model->save()) {
$output .= '<tr id="rowItem_' . $model->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(' . $model->id . ')"></i></td>
<td style="vertical-align:middle;">
<input type="hidden" value="' . $model->id . '" name="item_id[]">
' . $Product->getname($model->descr) . '
</td>
<td>
<select class="form-control kt-selectpicker" name="item_ownertype_' . $model->id . '">
<option value="company" selected>Company</option>
</select>
</td>
<td><input type="text" class="form-control" value="' . $model->quantity . '" name="item_quantity_' . $model->id . '" onchange="updatetotal(' . $model->id . ')"></td>
<td><input type="text" class="form-control" value="' . $model->uom . '" name="item_uom_' . $model->id . '"></td>
<td><input type="text" class="form-control" value="' . $model->unit_price . '" name="item_unitprice_' . $model->id . '" onchange="updatetotal(' . $model->id . ')"></td>
<td><input type="text" class="form-control" value="' . $model->discount . '" name="item_discount_' . $model->id . '" onchange="updatetotal(' . $model->id . ')"></td>
<td><input type="text" class="form-control item_total" value="' . $model->total . '" name="item_total_' . $model->id . '"></td>
</tr>';
}
}
}
return $output;
}
public function actionDeleteproduct() {
$id = $_POST['id'];
$status = 'failed';
if (InvoiceItem::findOne($id)->delete()) {
$status = 'success';
}
return $status;
}
public function actionUpdatepaymentstatus() {
$id = $_POST['id'];
$status = 'failed';
if (($model = Invoice::findOne($id)) !== null) {
$model->payment_status = 'Paid';
$model->paid_dt = date("Y-m-d H:i:s");
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
if ($model->save()) {
$status = 'success';
$Log = new Log();
$Log->insertlog($this->menu_id, 'update payment status', $model->uuid);
}
}
return $status;
}
public function actionDelete() {
$id = $_POST['id'];
$status = 'failed';
$model = Invoice::findOne($id);
if (!empty($model)) {
$model->deleted_status = 1;
if ($model->save(false)) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'delete', $model->uuid);
$status = 'success';
}
}
return $status;
}
public function actionDownload($uuid) {
$ParamList = new ParamList();
$User = new User();
$Notes = new Notes();
$model = $this->findModel($uuid);
if (!empty($model)) {
$modelcom = Company::findOne($model->com_id);
if (!empty($modelcom)) {
$header = '<table id="table-header" border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td><img src="uploads/' . $modelcom->logo . '" height="50"></td>
<td align="right"><b>' . $modelcom->tagline . '</b><br/>' . $modelcom->website . '</td>
</tr>
</table>';
$content = '<br><table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="50%" valign="top">
<br/><br/><b>BILL TO</b>
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">Company Name</td>
<td valign="top"> : </td>
<td valign="top">' . $model->customer_name . '</td>
</tr>
<tr>
<td valign="top">Address</td>
<td valign="top"> : </td>
<td valign="top">' . $model->customer_address1 . ' ' . $model->customer_address2 . ' ' . $model->customer_postcode . ' ' . $model->customer_city . ' ' . $ParamList->getlabel(3, $model->customer_state) . '</td>
</tr>
<tr>
<td valign="top">Contact Person</td>
<td valign="top"> : </td>
<td valign="top">' . $model->customer_pic . '</td>
</tr>
<tr>
<td valign="top">Contact No</td>
<td valign="top"> : </td>
<td valign="top">' . $model->customer_phone . '</td>
</tr>
</table>
</td>
<td width="50%" valign="top" align="right">
<table cellpadding="0" cellspacing="0" style="padding-right:1em;">
<tr><td colspan="3" align="center"><h4><b>INVOICE</b></h4><br/><br/></td></tr>
<tr>
<td valign="top">Date</td>
<td valign="top"> : </td>
<td valign="top" align="left">' . date("dS M Y", strtotime($model->doc_dt)) . '</td>
</tr>
<tr>
<td valign="top">Invoice No</td>
<td valign="top"> : </td>
<td valign="top" align="left">' . $model->invoice_no . '</td>
</tr>';
if (!empty($model->term)) {
$ParamList = new ParamList();
$content .= '<tr>
<td valign="top">Term</td>
<td valign="top"> : </td>
<td valign="top" align="left">' . $ParamList->getlabel(26, $model->term) . '</td>
</tr>';
}
$content .= '<tr>
<td valign="top">Issued by</td>
<td valign="top"> : </td>
<td valign="top" align="left">' . $User->getusername($model->sales_pic) . '</td>
</tr>
<tr>
<td valign="top">Contact No</td>
<td valign="top"> : </td>
<td valign="top" align="left">' . $User->getuserphone($model->sales_pic) . '</td>
</tr>
</table>
</td>
</tr>
</table><br>';
$content .= '<table width="100%" cellpadding="5" cellspacing="1" border="0" style="background-color:black;" id="table-product">
<thead>
<tr>
<th width="5%">No</th>
<th>Description</th>
<th width="10%">Qty</th>
<th width="10%">UoM</th>
<th width="15%">Unit Price</th>
<th width="10%">Discount</th>
<th width="15%">Total</th>
</tr>
</thead>
<tbody>';
$Product = new Product();
$modelItem = InvoiceItem::find()->where(['doc_id' => $model->id])->orderBy(['id' => SORT_ASC])->all();
$grandtotal = 0;
$subtotal = 0;
if (!empty($modelItem)) {
$countItem = 0;
foreach ($modelItem as $rowItem) {
$countItem++;
$content .= '<tr>
<td align="center">' . $countItem . '</td>
<td>' . $Product->getname($rowItem->descr) . '</td>
<td>' . number_format($rowItem->quantity) . '</td>
<td>' . $rowItem->uom . '</td>
<td>RM ' . number_format((float) $rowItem->unit_price, 2, '.', ',') . '</td>
<td>' . $rowItem->discount . '%</td>
<td>RM ' . number_format((float) $rowItem->total, 2, '.', ',') . '</td>
</tr>';
$grandtotal += $rowItem->total;
$subtotal += $rowItem->total;
}
} else {
$content .= '<tr><td colspan="7">No record found</td></tr>';
}
$content .= '</tbody><tfoot>';
if ($model->tax != '0') {
$tax = $model->tax / 100 * $subtotal;
$grandtotal = $tax + $subtotal;
$content .= '<tr>
<td colspan="6" align="right">Sub Total</td>
<td><b>RM ' . number_format((float) $subtotal, 2, '.', ',') . '</b></td>
</tr>
<tr>
<td colspan="6" align="right">Tax (' . $model->tax . '%)</td>
<td><b>RM ' . number_format((float) $tax, 2, '.', ',') . '</b></td>
</tr>';
}
$content .= '<tr>
<td colspan="6" align="right">Grand Total</td>
<td><b>RM ' . number_format((float) $grandtotal, 2, '.', ',') . '</b></td>
</tr>';
$content .= '</tfoot></table>';
if (!empty($model->remarks))
$content .= '<br>Remarks : ' . $model->remarks . '<br>';
$content .= Yii::$app->sharedfunction->convertMoneyToText($grandtotal) . '<hr><div style="font-size:8px;line-height:1.7em;">' . $Notes->getnotes('Invoice', $modelcom->id) . '</div>';
$company_stamp = '<tr><td align="center" height="110"></td></tr>';
if (!empty($modelcom->stamp))
$company_stamp = '<tr><td align="center"><img src="uploads/' . $modelcom->stamp . '" height="100"></td></tr>';
$content .= '<table width="30%">
' . $company_stamp . '
<tr><td align="center" style="border-top:1px solid black;">Authorized Signature</td></tr>
</table>';
$footer = '<table id="table-footer" border="0" width="100%" cellpadding="0" cellspacing="0">
<tr><td align="center">' . $modelcom->address1 . ' ' .
$modelcom->address2 . ' ' .
$modelcom->postcode . ' ' .
$modelcom->city . ' ' . $ParamList->getlabel(3, $modelcom->state) . '
</td>
</tr>
</table>';
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
'mode' => Pdf::MODE_CORE,
'filename' => 'Invoice ' . $model->invoice_no . '.pdf',
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_BROWSER,
'cssFile' => '@vendor/kartik-v/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
'cssInline' => 'body,table td{font-size:11px;Arial, Helvetica, sans-serif;line-height:1.7em;}
#table-header{padding-bottom:10px;}
#table-footer{padding-top:10px;}
#table-product th{background-color:black;color:white;text-align:center;}
#table-product tr:nth-child(odd) td{background-color:white;}
#table-product tr:nth-child(even) td{background-color:whitesmoke;}',
'methods' => ['SetHeader' => [$header], 'SetFooter' => $footer],
'content' => $content,
'options' => ['setAutoTopMargin' => 'stretch'],
]);
return $pdf->render();
}
}
}
public function actionCreateauto($transfer_id) {
$Company = new Company();
$ParamList = new ParamList();
$Product = new Product();
if (($modeltransfer = StockTransfer::findOne($transfer_id)) !== null) {
//insert row : invoice
$model = new Invoice();
$model->uuid = Uuid::uuid4();
$model->doc_dt = $modeltransfer->transfer_dt;
$model->type = 'transfer';
$model->com_id = $modeltransfer->stockout_owner_id;
$model->term = 'end_30';
$model->payment_due = date('Y-m-t', strtotime($model->doc_dt . ' + 30 days'));
$model->customer_id = $modeltransfer->stockin_owner_id;
if (($modelcustomer = Company::findOne($model->customer_id)) !== null) {
$model->customer_name = $modelcustomer->name;
$model->customer_address1 = $modelcustomer->address1;
$model->customer_address2 = $modelcustomer->address2;
$model->customer_postcode = $modelcustomer->postcode;
$model->customer_city = $modelcustomer->city;
$model->customer_state = $modelcustomer->state;
$model->customer_pic = $modelcustomer->contact_name;
$model->customer_phone = $modelcustomer->contact_no;
}
$countdoc = (int) Invoice::find()->where(['com_id' => $model->com_id])->count() + 1;
$model->invoice_no = $Company->getshortcode($model->com_id) . '/INV/' . date("y") . '/' . sprintf("%05d", $countdoc);
$model->payment_status = 'Pending';
$model->tax = 0;
$model->remarks = $modeltransfer->remarks;
$model->sales_pic = Yii::$app->user->identity->id;
$model->created_dt = date("Y-m-d H:i:s");
$model->created_by = Yii::$app->user->identity->id;
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$model->deleted_status = 0;
$modelItem = new InvoiceItem();
$modelItem->descr = $modeltransfer->stockout_product_id;
$modelItem->uom = $ParamList->getlabel(9, $Product->getuom($modelItem->descr));
$modelItem->quantity = $modeltransfer->stockout_quantity;
$modelItem->unit_price = $Product->getprice($modelItem->descr);
$modelItem->discount = '0';
$modelItem->total = (int) $modelItem->quantity * (float) $modelItem->unit_price;
$modelItem->created_dt = date("Y-m-d H:i:s");
$modelItem->created_by = Yii::$app->user->identity->id;
$modelItem->updated_dt = date("Y-m-d H:i:s");
$modelItem->updated_by = Yii::$app->user->identity->id;
$modelItem->owner_type = 'company';
$modelItem->owner_id = $model->com_id;
$model->grandtotal = $modelItem->total;
if ($model->save(false)) {
//insert row : invoice_item
$modelItem->doc_id = $model->id;
$modelItem->save(false);
//update stock_transfer : invoice_id
$modeltransfer->invoice_id = $model->id;
$modeltransfer->save(false);
$Log = new Log();
$Log->insertlog($this->menu_id, 'create', $model->uuid);
}
}
return $this->redirect('index.php?r=stocktransfer%2Findex');
}
}