| Current Path : /var/www/html/crm/controllers/ |
| Current File : /var/www/html/crm/controllers/InvoiceController.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 app\models\Company;
use app\models\Customer;
use app\models\Log;
use app\models\DeliveryOrderItem;
use app\models\DeliveryOrder;
use app\models\ParamList;
use app\models\User;
use app\models\Notes;
use kartik\mpdf\Pdf;
use app\models\Product;
use app\models\LogSearch;
/**
* InvoiceController implements the CRUD actions for Invoice model.
*/
class InvoiceController extends Controller {
public $menu_id = 52;
/**
* {@inheritdoc}
*/
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 actionOverview() {
$searchModel = new InvoiceSearch();
$dataProvider = $searchModel->searchoverview(Yii::$app->request->queryParams);
return $this->render('overview', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => 60]);
}
/**
* Lists all Invoice models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new InvoiceSearch();
$dataProvider = $searchModel->search(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]);
}
/**
* Displays a single Invoice model.
* @param integer $id
* @param string $uuid
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id, $uuid) {
return $this->render('view', ['model' => $this->findModel($id, $uuid),]);
}
/**
* Creates a new Invoice model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$Company = new Company();
$Customer = new Customer();
$output = 'failed';
if (($modeldo = DeliveryOrder::findOne($_POST['do_no'])) !== null) {
$model = new Invoice();
$model->uuid = Uuid::uuid4();
$model->com_id = $_POST['com_id'];
$model->do_no = $_POST['do_no'];
$model->type = 'do';
$model->grandtotal = number_format((float) DeliveryOrderItem::find()->where(['doc_id' => $model->do_no])->sum('total'), 2, '.', '');
$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;
$countdoc = (int) Invoice::find()->where(['com_id' => $model->com_id])->andwhere(['!=', 'type', 'transfer'])->count() + 1;
$model->invoice_no = $Company->getshortcode($model->com_id) . '/INV/' . date("y") . '/' . sprintf("%05d", $countdoc);
$model->sales_pic = $modeldo->sales_pic;
$model->doc_dt = $modeldo->doc_dt;
$model->term = 'end_60';
$model->payment_due = date('Y-m-t', strtotime($model->doc_dt . ' + 60 days'));
$model->po_ref = $modeldo->po_ref;
if ($Customer->getcustomertype($modeldo->customer_id) == 'parent') {
$model->customer_id = $modeldo->customer_id;
$model->customer_name = $modeldo->customer_name;
$model->customer_address1 = $modeldo->customer_address1;
$model->customer_address2 = $modeldo->customer_address2;
$model->customer_postcode = $modeldo->customer_postcode;
$model->customer_city = $modeldo->customer_city;
$model->customer_state = $modeldo->customer_state;
$model->customer_pic = $modeldo->customer_pic;
$model->customer_phone = $modeldo->customer_phone;
} else {
if (($modelcustomer = Customer::findOne($Customer->getparentid($modeldo->customer_id))) !== null) {
$model->customer_id = $modelcustomer->id;
$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;
}
}
if ($model->save()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'create', $model->uuid);
$output = 'success';
}
}
return $output;
}
public function actionCreate2() {
$Company = new Company();
$DeliveryOrder = new DeliveryOrder();
$Customer = new Customer();
$output = 'failed';
if (($modeldo = DeliveryOrder::findOne($_POST['do_no'])) !== null) {
$model = new Invoice();
$model->uuid = Uuid::uuid4();
$model->com_id = $DeliveryOrder->getcompanyid($_POST['do_no']);
$model->do_no = $_POST['do_no'];
$model->type = 'do';
$model->grandtotal = number_format((float) DeliveryOrderItem::find()->where(['doc_id' => $model->do_no])->sum('total'), 2, '.', '');
$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;
$countdoc = (int) Invoice::find()->where(['com_id' => $model->com_id])->andwhere(['!=', 'type', 'transfer'])->count() + 1;
$model->invoice_no = $Company->getshortcode($model->com_id) . '/INV/' . date("y") . '/' . sprintf("%05d", $countdoc);
$model->sales_pic = $modeldo->sales_pic;
$model->doc_dt = $modeldo->doc_dt;
$model->term = 'end_60';
$model->payment_due = date('Y-m-t', strtotime($model->doc_dt . ' + 60 days'));
$model->po_ref = $modeldo->po_ref;
if ($Customer->getcustomertype($modeldo->customer_id) == 'parent') {
$model->customer_id = $modeldo->customer_id;
$model->customer_name = $modeldo->customer_name;
$model->customer_address1 = $modeldo->customer_address1;
$model->customer_address2 = $modeldo->customer_address2;
$model->customer_postcode = $modeldo->customer_postcode;
$model->customer_city = $modeldo->customer_city;
$model->customer_state = $modeldo->customer_state;
$model->customer_pic = $modeldo->customer_pic;
$model->customer_phone = $modeldo->customer_phone;
} else {
if (($modelcustomer = Customer::findOne($Customer->getparentid($modeldo->customer_id))) !== null) {
$model->customer_id = $modelcustomer->id;
$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;
}
}
if ($model->save()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'create', $model->uuid);
$output = 'success';
}
}
return $output;
}
/**
* Updates an existing Invoice model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @param string $uuid
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($uuid) {
$model = $this->findModel($uuid);
$model->scenario = 'update';
$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())) {
//$model->payment_due = $model->calcpaymentdue($model->created_dt, $model->term);
if ($model->save()) {
if (!empty($_POST['item_id'])) {
$item_arr = $_POST['item_id'];
foreach ($item_arr as $item_id) {
if (($modelItem = DeliveryOrderItem::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=" . Yii::$app->getRequest()->getQueryParam('returnUrl') . "/index';});");
}
}
return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
}
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';
$menu_id = '';
if ($model->type == 'do')
$menu_id = '52';
if ($model->type == 'manual')
$menu_id = '53';
if ($model->type == 'cash')
$menu_id = '59';
$Log = new Log();
$Log->insertlog($menu_id, 'update payment status', $model->uuid);
}
}
return $status;
}
/**
* Deletes an existing Invoice model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @param string $uuid
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
/* public function actionDelete($id, $uuid) {
$this->findModel($id, $uuid)->delete();
return $this->redirect(['index']);
} */
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;
}
/**
* Finds the Invoice model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @param string $uuid
* @return Invoice the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($uuid) {
if (($model = Invoice::findOne(['uuid' => $uuid])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionGetcustomerdetails() {
$id = $_POST['id'];
$output = '';
if (($model = Customer::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 actionDownload($uuid) {
$ParamList = new ParamList();
$User = new User();
$Notes = new Notes();
$model = $this->findModel($uuid);
$content = '';
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>' . strtoupper($modelcom->name) . '</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->po_ref)) {
$content .= '<tr>
<td valign="top">Customer PO ref</td>
<td valign="top"> : </td>
<td valign="top" align="left">' . $model->po_ref . '</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>';
}*/
/* if (!empty($model->payment_due)) {
$content.='<tr>
<td valign="top">Payment Due</td>
<td valign="top"> : </td>
<td valign="top" align="left">' . date("dS M Y", strtotime($model->payment_due)) . '</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="10%">Unit Price</th>
<th width="10%">Discount</th>
<th width="15%">Total</th>
</tr>
</thead>
<tbody>';
$modelItem = DeliveryOrderItem::find()->where(['doc_id' => $model->do_no])->orderBy(['id' => SORT_ASC])->all();
$Product = new Product();
$subtotal = 0;
$grandtotal = 0;
if (!empty($modelItem)) {
$countItem = 0;
foreach ($modelItem as $rowItem) {
$countItem++;
$content .= '<tr>
<td align="center">' . $countItem . '</td>
<td>' . $Product->getname($rowItem->product_id) . '</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>';
$subtotal += $rowItem->total;
$grandtotal += $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>
<!--table width="100%" style="padding-top:10em;">
<tr>
<td align="center" style="border-top:1px solid black;" width="30%">Authorized Signature</td>
<td></td>
<td align="center" style="border-top:1px solid black;" width="30%">Customer Chop and sign</td>
</tr>
</table-->';
$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 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;
}
}