| Current Path : /var/www/html/crm/models/ |
| Current File : /var/www/html/crm/models/DeliveryOrder.php |
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "delivery_order".
*
* @property int $id
* @property string $uuid
* @property string $doc_no
* @property string $doc_dt
* @property string $po_ref
* @property int $company_id
* @property int $customer_id
* @property string $customer_name
* @property string $customer_address1
* @property string $customer_address2
* @property string $customer_postcode
* @property string $customer_city
* @property string $customer_state
* @property string $customer_pic
* @property string $customer_phone
* @property string $created_dt
* @property int $created_by
* @property string $updated_dt
* @property int $updated_by
*/
class DeliveryOrder extends \yii\db\ActiveRecord {
public $file;
/**
* {@inheritdoc}
*/
public static function tableName() {
return 'delivery_order';
}
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['uuid', 'doc_dt', 'company_id', 'customer_name', 'customer_address1', 'customer_postcode', 'customer_city', 'customer_state', 'customer_pic', 'customer_phone', 'sales_pic'], 'required'],
[['doc_dt', 'created_dt', 'updated_dt', 'status', 'uploaded_file'], 'safe'],
[['company_id', 'customer_id', 'created_by', 'updated_by'], 'integer'],
[['doc_no', 'po_ref', 'customer_name', 'customer_address1', 'customer_address2', 'customer_city', 'customer_pic'], 'string', 'max' => 255],
[['customer_postcode'], 'string', 'max' => 10],
[['customer_state', 'customer_phone'], 'string', 'max' => 20],
[['file'], 'file'],
[['file'], 'required', 'on' => 'upload'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels() {
return [
'id' => 'ID',
'uuid' => 'Uuid',
'doc_no' => 'DO Number',
'doc_dt' => 'Date',
'po_ref' => 'Customer PO ref ',
'company_id' => 'Company',
'customer_id' => 'Customer',
'customer_name' => 'Customer Name',
'customer_address1' => 'Address',
'customer_address2' => 'Customer Address2',
'customer_postcode' => 'Postcode',
'customer_city' => 'City',
'customer_state' => 'State',
'customer_pic' => 'Contact Person',
'customer_phone' => 'Contact No.',
'created_dt' => 'Created Dt',
'created_by' => 'Created By',
'updated_dt' => 'Updated Dt',
'updated_by' => 'Updated By',
'status' => 'Status',
'file' => 'Upload Signed DO',
'sales_pic' => 'Sales Person'
];
}
public function getdocno($id) {
$output = '';
if (($model = static::findOne($id)) !== null) {
$output = $model->doc_no;
}
return $output;
}
public function getcustomername($id) {
$output = '';
if (($model = static::findOne($id)) !== null) {
$output = $model->customer_name;
}
return $output;
}
public function getdocdate($id) {
$output = '';
if (($model = static::findOne($id)) !== null) {
$output = $model->doc_dt;
}
return $output;
}
public function getcompanyid($id) {
$output = '';
if (($model = static::findOne($id)) !== null) {
$output = $model->company_id;
}
return $output;
}
public function consignedqty($product_id, $company_id) {
$sql_ext = "";
if ($company_id != 0)
$sql_ext = " AND t1.company_id='$company_id'";
$sql = "SELECT t1.id AS do_id, t2.quantity, t2.product_id FROM delivery_order t1 INNER JOIN delivery_order_item t2 ON t1.id=t2.doc_id
WHERE t1.status='Consigned' AND t2.product_id='$product_id'" . $sql_ext;
$model = Yii::$app->db->createCommand($sql)->queryAll();
$total = 0;
if (!empty($model)) {
foreach ($model as $row) {
$qty_send = $row['quantity'];
//check if stock send has return
$model_return = Yii::$app->db->createCommand("SELECT SUM(quantity) FROM grn WHERE do_id=" . $row['do_id'] . " AND product_id=" . $row['product_id'])->queryScalar();
$qty_return = 0;
if (!empty($model_return))
$qty_return = $model_return;
$balance = $qty_send - $qty_return;
$total+=$balance;
}
}
return $total;
}
public function intransitqty($product_id, $company_id) {
$sql_ext = "";
if ($company_id != 0)
$sql_ext = " AND t1.company_id='$company_id'";
$sql = "SELECT SUM(t2.quantity) FROM delivery_order t1 INNER JOIN delivery_order_item t2 ON t1.id=t2.doc_id
WHERE t1.status='In-Transit' AND t2.product_id='$product_id'" . $sql_ext;
$total = (int) Yii::$app->db->createCommand($sql)->queryScalar();
return $total;
}
public function totalInTransitByCompany($company_id) {
$sql = "SELECT SUM(t2.quantity) FROM delivery_order t1 INNER JOIN delivery_order_item t2 ON t1.id=t2.doc_id
WHERE t1.status='In-Transit' AND t1.company_id='$company_id'";
$total = (int) Yii::$app->db->createCommand($sql)->queryScalar();
return $total;
}
/* public function totalConsignedByCompany($company_id) {
$sql = "SELECT t1.id AS do_id, t2.quantity, t2.product_id FROM delivery_order t1 INNER JOIN delivery_order_item t2 ON t1.id=t2.doc_id
WHERE t1.status='Consigned' AND t1.company_id='$company_id'";
$model = Yii::$app->db->createCommand($sql)->queryAll();
$total = 0;
if (!empty($model)) {
foreach ($model as $row) {
$qty_send = $row['quantity'];
//check if stock send has return
$model_return = Yii::$app->db->createCommand("SELECT SUM(quantity) FROM grn WHERE do_id=" . $row['do_id'] . " AND product_id=" . $row['product_id'])->queryScalar();
$qty_return = 0;
if (!empty($model_return))
$qty_return = $model_return;
$balance = $qty_send - $qty_return;
$total+=$balance;
}
}
return $total;
} */
public function totalConsignedByCompany($company_id = '', $start_dt = '', $end_dt = '') {
$sqlExt = "";
if (!empty($company_id))
$sqlExt .= " AND company_id IN (" . $company_id . ")";
if (!empty($start_dt) && !empty($end_dt))
$sqlExt .= " AND DATE(doc_dt) BETWEEN '" . $start_dt . "' AND '" . $end_dt . "'";
$sql = "SELECT *,
IFNULL((SELECT SUM(quantity) FROM delivery_order_item WHERE delivery_order.id=delivery_order_item.doc_id),0) AS qty_send,
IFNULL((SELECT SUM(quantity) FROM grn WHERE grn.do_id=delivery_order.id),0) AS qty_return,
(
IFNULL((SELECT SUM(quantity) FROM delivery_order_item WHERE delivery_order.id=delivery_order_item.doc_id),0)-
IFNULL((SELECT SUM(quantity) FROM grn WHERE grn.do_id=delivery_order.id),0)
) AS qty_consigned
FROM delivery_order WHERE status='Consigned'" . $sqlExt . " ORDER BY doc_dt DESC";
return $sql;
}
public function totalConsignedByProduct(array $brand_arr = [], array $product_arr = [], array $company_arr = [], $start_dt = '', $end_dt = '') {
$sqlExt = "";
if (!empty($brand_arr))
$sqlExt .= " AND t3.brand_id IN (" . implode(',', $brand_arr) . ")";
if (!empty($product_arr))
$sqlExt .= " AND t3.id IN (" . implode(',', $product_arr) . ")";
if (!empty($company_arr))
$sqlExt .= " AND t2.company_id IN (" . implode(',', $company_arr) . ")";
if (!empty($start_dt) && !empty($end_dt))
$sqlExt .= " AND DATE(t2.doc_dt) BETWEEN '" . $start_dt . "' AND '" . $end_dt . "'";
$sql = "SELECT t3.name AS product_name, t1.product_id, t2.doc_dt, t2.doc_no, t2.company_id, t2.customer_name, t2.uuid, t1.unit_price,
t1.quantity AS qty_send,
(SELECT SUM(grn.quantity) FROM grn WHERE grn.do_id=t2.id AND t1.product_id=grn.product_id) AS qty_return
FROM delivery_order_item t1
INNER JOIN delivery_order t2 ON t1.doc_id=t2.id
INNER JOIN product t3 ON t1.product_id=t3.id
WHERE t2.status='Consigned' " . $sqlExt . "
ORDER BY t2.doc_dt DESC";
return $sql;
}
}