[
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action) {
return User::getCurrentStatus() == USER::STATUS_ADMIN;
}
]
],
],
];
}
/**
* Liste les producteurs.
*
* @return mixed
*/
public function actionIndex()
{
$dataProviderProducer = new ActiveDataProvider([
'query' => Producer::find()
->with('userProducer', 'user')
->orderBy('active DESC, free_price DESC'),
'pagination' => [
'pageSize' => 1000,
],
]);
$producersArray = Producer::find()->where('active = 1')->all();
$sumPrices = 0;
foreach($producersArray as $producer) {
$sumPrices += $producer->getAmountBilledLastMonth();
}
return $this->render('index', [
'dataProviderProducer' => $dataProviderProducer,
'sumPrices' => $sumPrices
]);
}
/**
* Crée un producteur.
*
* @return mixed
*/
public function actionCreate()
{
$model = new Producer();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->getSession()->setFlash('success', 'Producteur créé.');
return $this->redirect(['index']);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
public function actionUserTransfer($fromProducerId, $toProducerId, $withOrders = 1)
{
$fromProducerId = (int) $fromProducerId;
$toProducerId = (int) $toProducerId;
$count = 0;
$usersArray = User::findBy(['id_producer' => $fromProducerId])->all();
foreach($usersArray as $user) {
$idUser = $user['user_id'];
$countOrders = 0;
if($withOrders) {
$countOrders = Order::searchCount([
'id_user' => $idUser,
'distribution.id_producer' => $fromProducerId
], ['conditions' => 'date_delete IS NULL']);
}
if(($withOrders && $countOrders) || !$withOrders) {
Producer::addUser($idUser, $toProducerId);
$count ++;
}
}
if($count) {
Yii::$app->getSession()->setFlash('success', $count.' clients importés du producteur #'.$fromProducerId.' vers le producteur #'.$toProducerId.'.');
}
else {
Yii::$app->getSession()->setFlash('error', 'Aucun client à importer.');
}
return $this->redirect(['producer-admin/index']);
}
/**
* Génère la facture mensuelle d'un producteur.
*
* @param integer $idProducer
*/
public function actionBill($idProducer)
{
$producer = Producer::findOne($idProducer);
if ($producer) {
$period = date('Y-m', strtotime('-1 month'));
$last_invoice = Invoice::getLastInvoice() ;
if (!$last_invoice) {
$reference = 'BAP000001';
} else {
$reference = str_replace('BAP', '', $last_invoice->reference);
$reference ++;
$reference = 'BAP' . $reference;
}
$invoice = new Invoice;
$invoice->id_producer = $idProducer;
$invoice->date = date('Y-m-d H:i:s');
$invoice->reference = $reference;
$invoice->turnover = $producer->getTurnover($period);
$invoice->amount_ht = $producer->getFreePrice() ;
$invoice->wording = 'Facture ' . date('m/Y', strtotime('-1 month'));
$invoice->text = 'Utilisation de la plateforme distrib pour le mois : ' . date('m/Y', strtotime('-1 month')) . '
'
. 'Chiffre d\'affaire réalisé sur la plateforme : ' . number_format($facture->ca, 2) . ' € commissionné à 1%.';
$invoice->paid = 0;
$invoice->period = $period;
$invoice->save();
}
$this->redirect(['producer-admin/index']);
}
/**
* Liste les factures des producteurs.
*
* @return mxied
*/
public function actionBilling()
{
$dataProviderInvoice = new ActiveDataProvider([
'query' => Invoice::find()
->with('producer')
->orderBy('reference DESC'),
'pagination' => [
'pageSize' => 1000,
],
]);
return $this->render('billing', [
'dataProviderInvoice' => $dataProviderInvoice,
]);
}
public function actionProducerInstallTaxUpdatePrices($idProducer)
{
// product
$productsArray = Product::searchAll([
'id_producer' => $idProducer
]) ;
$connection = Yii::$app->getDb();
foreach($productsArray as $product) {
$product->price = round($product->price / (1 + $product->taxRate->value), 2) ;
$product->save() ;
$command = $connection->createCommand("
UPDATE `product_order`
SET price = ROUND(price / (1 + :tax_value), 2),
id_tax_rate = :id_tax_rate
WHERE id_product = :id_product",
[
':id_product' => $product->id,
':tax_value' => $product->taxRate->value,
':id_tax_rate' => $product->taxRate->id,
]);
$result = $command->query();
}
echo 'ok' ;
}
/**
* Recherche un établissement.
*
* @param integer $id
* @return Etablissement
* @throws NotFoundHttpException
*/
protected function findModel($id) {
if (($model = Producer::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}