diff --git a/backend/controllers/TransferController.php b/backend/controllers/TransferController.php index a41f25c..682f990 100644 --- a/backend/controllers/TransferController.php +++ b/backend/controllers/TransferController.php @@ -8,6 +8,8 @@ use backend\models\TransferSearch; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; +use common\models\Account; +use common\models\User; /** * TransferController implements the CRUD actions for Transfer model. @@ -34,10 +36,20 @@ class TransferController extends Controller { $searchModel = new TransferSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + $accounts = Account::readAccounts(); + $users = User::read(); + + $searchModel->totals(Yii::$app->request->queryParams); + + return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, + 'accounts' => $accounts, + 'users' => $users, +// 'totals' =>$totals ]); } diff --git a/backend/models/ProcurementSearch.php b/backend/models/ProcurementSearch.php index ef371da..eac7970 100644 --- a/backend/models/ProcurementSearch.php +++ b/backend/models/ProcurementSearch.php @@ -77,7 +77,7 @@ class ProcurementSearch extends Procurement 'id_product' => $this->id_product, ]); - $query->andFilterWhere([ '>=', 'created_at', $this->date_start ] ); + $query->andFilterWhere([ '>=', 'created_at', $this->timestampStart ] ); $query->andFilterWhere([ '<', 'created_at', $this->timestampEnd ] ); diff --git a/backend/models/TransferSearch.php b/backend/models/TransferSearch.php index 903aaeb..1604c29 100644 --- a/backend/models/TransferSearch.php +++ b/backend/models/TransferSearch.php @@ -6,20 +6,39 @@ use Yii; use yii\base\Model; use yii\data\ActiveDataProvider; use common\models\Transfer; +use yii\db\Expression; +use yii\base\Object; +use yii\db\Query; +use yii\helpers\ArrayHelper; +use common\models\Account; /** * TransferSearch represents the model behind the search form about `common\models\Transfer`. */ class TransferSearch extends Transfer { + public $searchObjectName; + public $searchTypeName; + public $searchUserName; + public $start; + public $end; + + public $timestampStart; + public $timestampEnd; + + public $accountTotals; + public $fullTotal; + /** * @inheritdoc */ public function rules() { return [ - [['id_transfer', 'id_discount', 'id_currency', 'id_object', 'status', 'type', 'item_price', 'count', 'money', 'money_currency', 'rate', 'id_user'], 'integer'], - [['comment', 'created_at', 'updated_at'], 'safe'], + [[ 'id_account','id_user', 'type'], 'integer'], + [[ 'searchObjectName' ], 'string'], + [[ 'start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ], + [[ 'end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ], ]; } @@ -47,33 +66,108 @@ class TransferSearch extends Transfer 'query' => $query, ]); + $selectObjectName = ""; + $selectObjectName .= " case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end as object_name"; + + $query->addSelect( ['*', new Expression($selectObjectName) ]); + $query->joinWith('ticket'); + $query->joinWith('ticketType'); + $query->joinWith('product'); + $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails - // $query->where('0=1'); + $query->where('0=1'); return $dataProvider; } $query->andFilterWhere([ - 'id_transfer' => $this->id_transfer, - 'id_discount' => $this->id_discount, - 'id_currency' => $this->id_currency, - 'id_object' => $this->id_object, - 'status' => $this->status, - 'type' => $this->type, - 'item_price' => $this->item_price, - 'count' => $this->count, - 'money' => $this->money, - 'money_currency' => $this->money_currency, - 'rate' => $this->rate, - 'id_user' => $this->id_user, - 'created_at' => $this->created_at, - 'updated_at' => $this->updated_at, + 'transfer.id_account' => $this->id_account, + 'transfer.type' => $this->type, + 'transfer.id_user' => $this->id_user, ]); + + $query->andFilterWhere([ '>=', 'transfer.created_at', $this->timestampStart ] ); + $query->andFilterWhere([ '<', 'transfer.created_at', $this->timestampEnd ] ); - $query->andFilterWhere(['like', 'comment', $this->comment]); - + if ( isset($this->searchObjectName)) + $query->andWhere( new Expression(" case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end like '%" . $this->searchObjectName ."%'")); + + + return $dataProvider; } + + public function totals($params){ + $query = new Query(); + + $result = [ ]; + + $accountTotals = []; + $fullTotal = [ + 'label' => Yii::t('common/transfer', 'Total'), + 'money' => 0 + ]; + + $query->addSelect( [ new Expression( 'transfer.id_account as account'), new Expression( ' COALESCE(sum( transfer.money ),0) as money' )]); + $query->from('transfer'); + $query->leftJoin('ticket', 'transfer.type = ' .self::TYPE_TICKET .' and transfer.id_object = ticket.id_ticket ' ); + $query->leftJoin('ticket_type', 'ticket.id_ticket_type = ticket_type.id_ticket_type ' ); + $query->leftJoin('product', 'product.id_product = ' .self::TYPE_TICKET .' and transfer.id_object = ticket.id_ticket ' ); + + $this->load($params); + + if (!$this->validate()) { + return ; + } + + $query->andFilterWhere([ + 'transfer.id_account' => $this->id_account, + 'transfer.type' => $this->type, + 'transfer.id_user' => $this->id_user, + ]); + + $query->andFilterWhere([ '>=', 'transfer.created_at', $this->timestampStart ] ); + $query->andFilterWhere([ '<', 'transfer.created_at', $this->timestampEnd ] ); + + + if ( isset($this->searchObjectName) && !empty($this->searchObjectName)){ + $query->andWhere( new Expression(" case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end like '%" . $this->searchObjectName ."%'")); + } + + $query->groupBy('transfer.id_account'); + + $command = $query->createCommand(); + // $command->sql returns the actual SQL + $result = $command->queryAll(); + + + $accounts = Account::find()->orderBy("name asc")->all(); + $accountMap = ArrayHelper::map( $accounts ,'id_account','name' ); + + + + foreach ($result as $item){ + $account = ""; + if ( array_key_exists($item['account'], $accountMap)){ + $account = $accountMap[$item['account']]; + } + + $accountTotal = [ + 'label' => $account, + 'money' => $item['money'] + ]; + + $accountTotals[] = $accountTotal; + + $fullTotal['money'] += $item['money']; + } + + $this->accountTotals = $accountTotals; + $this->fullTotal = $fullTotal; + +// return $result; + } + } diff --git a/backend/views/transfer/_search.php b/backend/views/transfer/_search.php index 3e67083..d55f5d4 100644 --- a/backend/views/transfer/_search.php +++ b/backend/views/transfer/_search.php @@ -2,52 +2,68 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; +use frontend\components\HtmlHelper; +use yii\helpers\ArrayHelper; +use common\models\Transfer; +use kartik\widgets\DatePicker; /* @var $this yii\web\View */ /* @var $model backend\models\TransferSearch */ /* @var $form yii\widgets\ActiveForm */ ?> +