Browse Source

[producer/backend] Vente au kilo : adaptation formulaire d'abonnements

dev
Guillaume Bourgeois 5 years ago
parent
commit
3bf31a1e4f
13 changed files with 154 additions and 38 deletions
  1. +6
    -0
      backend/controllers/SubscriptionController.php
  2. +1
    -0
      backend/views/subscription/_form.php
  3. +7
    -2
      backend/views/subscription/index.php
  4. +1
    -0
      backend/web/js/vuejs/subscription-form.js
  5. +19
    -0
      common/models/Subscription.php
  6. +33
    -11
      common/models/SubscriptionForm.php
  7. +27
    -0
      console/migrations/m190515_122438_ajout_champs_price_product_subscription.php
  8. +17
    -11
      producer/controllers/SubscriptionController.php
  9. +7
    -4
      producer/views/subscription/_form.php
  10. +2
    -1
      producer/views/subscription/index.php
  11. +20
    -6
      producer/web/css/screen.css
  12. +1
    -1
      producer/web/js/vuejs/subscription-form.js
  13. +13
    -2
      producer/web/sass/subscription/_form.scss

+ 6
- 0
backend/controllers/SubscriptionController.php View File



use common\models\Order ; use common\models\Order ;
use common\models\Product ; use common\models\Product ;
use common\models\SubscriptionSearch ;


class SubscriptionController extends BackendController class SubscriptionController extends BackendController
{ {
{ {
// form // form
$model = new SubscriptionForm; $model = new SubscriptionForm;
$model->isAdmin = true ;
$model->id_producer = Producer::getId(); $model->id_producer = Producer::getId();


if($idOrder) { if($idOrder) {
{ {
// form // form
$model = new SubscriptionForm; $model = new SubscriptionForm;
$model->isAdmin = true ;
$subscription = Subscription::findOne($id); $subscription = Subscription::findOne($id);
if ($subscription) { if ($subscription) {
$model->id = $id; $model->id = $id;
'unit' => $theProduct['unit'], 'unit' => $theProduct['unit'],
'wording_unit' => Product::strUnit($theProduct['unit'], 'wording_short'), 'wording_unit' => Product::strUnit($theProduct['unit'], 'wording_short'),
'step' => $theProduct['step'], 'step' => $theProduct['step'],
'price' => $theProduct['price']
] ; ] ;
if(isset($theProduct['productSubscription'][0])) { if(isset($theProduct['productSubscription'][0])) {
'unit' => $theProduct['productSubscription'][0]['unit'], 'unit' => $theProduct['productSubscription'][0]['unit'],
'wording_unit' => Product::strUnit($theProduct['productSubscription'][0]['unit'], 'wording_short'), 'wording_unit' => Product::strUnit($theProduct['productSubscription'][0]['unit'], 'wording_short'),
'step' => $theProduct['productSubscription'][0]['step'], 'step' => $theProduct['productSubscription'][0]['step'],
'price' => $theProduct['productSubscription'][0]['price'],
] ; ] ;
$theProduct['unit'] = $theProduct['productSubscription'][0]['unit'] ; $theProduct['unit'] = $theProduct['productSubscription'][0]['unit'] ;
$theProduct['step'] = $theProduct['productSubscription'][0]['step'] ; $theProduct['step'] = $theProduct['productSubscription'][0]['step'] ;
$theProduct['price'] = $theProduct['productSubscription'][0]['price'] ;
} }
$theProduct['quantity'] = $theProduct['productSubscription'][0]['quantity'] * Product::$unitsArray[$theProduct['unit']]['coefficient'] ; $theProduct['quantity'] = $theProduct['productSubscription'][0]['quantity'] * Product::$unitsArray[$theProduct['unit']]['coefficient'] ;
} }

+ 1
- 0
backend/views/subscription/_form.php View File

<td>{{ product.name }}</td> <td>{{ product.name }}</td>
<td> <td>
<input type="hidden" :value="product.step" :name="'product_step_'+product.step" /> <input type="hidden" :value="product.step" :name="'product_step_'+product.step" />
<input type="hidden" :value="product.price" :name="'product_price_'+product.price" />
<div class="input-group"> <div class="input-group">
<span class="input-group-btn"> <span class="input-group-btn">
<button class="btn btn-default" type="button" @click="changeQuantityProductSubscription(false, product)"><span class="glyphicon glyphicon-minus"></span></button> <button class="btn btn-default" type="button" @click="changeQuantityProductSubscription(false, product)"><span class="glyphicon glyphicon-minus"></span></button>

+ 7
- 2
backend/views/subscription/index.php View File

foreach($model->productSubscription as $productSubscription) foreach($model->productSubscription as $productSubscription)
{ {
if(isset($productSubscription->product)) { if(isset($productSubscription->product)) {
$html .= Html::encode($productSubscription->product->name).' ('.$productSubscription->quantity.'&nbsp;'.Product::strUnit($productSubscription->unit, 'wording_short').')<br />' ;
$html .= Html::encode($productSubscription->product->name).' ('.($productSubscription->quantity * Product::$unitsArray[$productSubscription->unit]['coefficient']).'&nbsp;'.Product::strUnit($productSubscription->unit, 'wording_short').')<br />' ;
} }
else { else {
$html .= 'Produit non défini<br />' ; $html .= 'Produit non défini<br />' ;
// aucun produit // aucun produit
if(!count($model->productSubscription)) if(!count($model->productSubscription))
{ {
$html .= '<span class="glyphicon glyphicon-warning-sign"></span> Aucun produit' ;
$html .= '<span class="glyphicon glyphicon-warning-sign"></span> Aucun produit<br />' ;
}
// des unités ne correspondent pas
if($model->hasUnitsNotMatch()) {
$html .= '<span class="glyphicon glyphicon-warning-sign" title="Des unités ne correspondent pas"></span>' ;
} }
return $html ; return $html ;

+ 1
- 0
backend/web/js/vuejs/subscription-form.js View File

for(var j = 0; j < this.products[i].units.length ; j ++) { for(var j = 0; j < this.products[i].units.length ; j ++) {
if(this.products[i].unit == this.products[i].units[j].unit) { if(this.products[i].unit == this.products[i].units[j].unit) {
this.products[i].step = this.products[i].units[j].step ; this.products[i].step = this.products[i].units[j].step ;
this.products[i].price = this.products[i].units[j].price ;
} }
} }
} }

+ 19
- 0
common/models/Subscription.php View File

} }
} }
} }
/**
* Retourne true si des unités des ProductSubscription ne correspondent pas
* aux Product.
*
* @return boolean
*/
public function hasUnitsNotMatch()
{
if(isset($this->productSubscription) && is_array($this->productSubscription)) {
foreach($this->productSubscription as $productSubscription) {
if(isset($productSubscription->product) && $productSubscription->unit != $productSubscription->product->unit) {
return true ;
}
}
}
return false ;
}
} }

+ 33
- 11
common/models/SubscriptionForm.php View File

*/ */
class SubscriptionForm extends Model class SubscriptionForm extends Model
{ {
public $isAdmin = false ;
public $id; public $id;
public $id_user; public $id_user;
public $username; public $username;


// produits // produits
if ($this->id) { if ($this->id) {
$productsSubscriptionsArray = ProductSubscription::findAll(['id_subscription' => $this->id]) ;
ProductSubscription::deleteAll(['id_subscription' => $this->id]); ProductSubscription::deleteAll(['id_subscription' => $this->id]);
} }


$idProduct = (int) str_replace('product_', '', $nameInput); $idProduct = (int) str_replace('product_', '', $nameInput);
$product = Product::findOne($idProduct) ; $product = Product::findOne($idProduct) ;


$subscriptionProduct = new ProductSubscription;
$subscriptionProduct->id_subscription = $subscription->id;
$subscriptionProduct->id_product = $idProduct;
$newProductSubscription = new ProductSubscription;
$newProductSubscription->id_subscription = $subscription->id;
$newProductSubscription->id_product = $idProduct;
$unit = (Yii::$app->getRequest()->post('product_unit_'.$idProduct)) ? Yii::$app->getRequest()->post('product_unit_'.$idProduct) : $product->unit ;
$subscriptionProduct->unit = $unit;

$step = (Yii::$app->getRequest()->post('product_step_'.$idProduct)) ? Yii::$app->getRequest()->post('product_step_'.$idProduct) : $product->step ;
$subscriptionProduct->step = $step;
if($this->isAdmin) {
$unit = (Yii::$app->getRequest()->post('product_unit_'.$idProduct)) ? Yii::$app->getRequest()->post('product_unit_'.$idProduct) : $product->unit ;
$newProductSubscription->unit = $unit;
$step = (Yii::$app->getRequest()->post('product_step_'.$idProduct)) ? Yii::$app->getRequest()->post('product_step_'.$idProduct) : $product->step ;
$newProductSubscription->step = $step;
$price = (Yii::$app->getRequest()->post('product_price_'.$idProduct)) ? Yii::$app->getRequest()->post('product_price_'.$idProduct) : $product->price ;
$newProductSubscription->price = $price;
}
else {
foreach($productsSubscriptionsArray as $productSubscription) {
if($productSubscription->id_product == $idProduct) {
$newProductSubscription->unit = $productSubscription->unit ;
$newProductSubscription->step = $productSubscription->step ;
$newProductSubscription->price = $productSubscription->price ;
}
}
if(!$newProductSubscription->unit) {
$newProductSubscription->unit = $product->unit ;
}
if(!$newProductSubscription->step) {
$newProductSubscription->step = $product->step;
}
if(!$newProductSubscription->price) {
$newProductSubscription->price = $product->price;
}
}
$subscriptionProduct->quantity = $quantity / Product::$unitsArray[$unit]['coefficient'];
$newProductSubscription->quantity = $quantity / Product::$unitsArray[$newProductSubscription->unit]['coefficient'];


$subscriptionProduct->save();
$newProductSubscription->save();
} }
} }

+ 27
- 0
console/migrations/m190515_122438_ajout_champs_price_product_subscription.php View File

<?php

use yii\db\Migration;
use yii\db\mysql\Schema;

class m190515_122438_ajout_champs_price_product_subscription extends Migration {

public function up() {
$this->addColumn('product_subscription', 'price', Schema::TYPE_FLOAT. ' DEFAULT 0') ;
$productsSubscriptionsArray = common\models\ProductSubscription::find()->all() ;
$productsArray = common\models\Product::find()->all() ;
foreach($productsSubscriptionsArray as $productSubscription) {
foreach($productsArray as $product) {
if($productSubscription->id_product == $product->id) {
$productSubscription->price = $product->price ;
$productSubscription->save() ;
}
}
}
}

public function down() {
$this->dropColumn('product_subscription', 'price') ;
}
}

+ 17
- 11
producer/controllers/SubscriptionController.php View File



use common\models\SubscriptionForm ; use common\models\SubscriptionForm ;
use common\models\SubscriptionSearch ; use common\models\SubscriptionSearch ;
use common\models\Product ;


class SubscriptionController extends ProducerBaseController class SubscriptionController extends ProducerBaseController
{ {
if($idSubscription > 0) { if($idSubscription > 0) {
// Quantités produit de l'abonnement
$productQuantitiesArray = [] ;
$arrayProductsSubscription = ProductSubscription::searchAll([ $arrayProductsSubscription = ProductSubscription::searchAll([
'id_subscription' => $idSubscription 'id_subscription' => $idSubscription
]) ; ]) ;
if(count($arrayProductsSubscription)) {
foreach ($arrayProductsSubscription as $productSubscription) {
$productQuantitiesArray[$productSubscription->id_product] = $productSubscription->quantity;
}
}
} }
// Produits // Produits
$productsArray = Product::searchAll() ; $productsArray = Product::searchAll() ;
$indexProduct = 0 ; $indexProduct = 0 ;
foreach($productsArray as &$product) { foreach($productsArray as &$product) {
$quantity = 0 ; $quantity = 0 ;
if(isset($productQuantitiesArray) && count($productQuantitiesArray) && isset($productQuantitiesArray[$product->id])) {
$quantity = $productQuantitiesArray[$product->id] ;
$coefficientUnit = Product::$unitsArray[$product->unit]['coefficient'] ;
if(isset($arrayProductsSubscription) && count($arrayProductsSubscription)) {
foreach ($arrayProductsSubscription as $productSubscription) {
if($product->id == $productSubscription->id_product) {
$coefficientUnit = Product::$unitsArray[$productSubscription->unit]['coefficient'] ;
$quantity = $productSubscription->quantity * $coefficientUnit;
$product->step = $productSubscription->step;
$product->unit = $productSubscription->unit;
$product->price = $productSubscription->price;
}
}
} }
$product = array_merge( $product = array_merge(
$product->getAttributes(), $product->getAttributes(),
[ [
'index' => $indexProduct ++, 'index' => $indexProduct ++,
'quantity_form' => $quantity
'quantity_form' => $quantity,
'coefficient_unit' => $coefficientUnit,
'wording_unit' => Product::strUnit($product->unit, 'wording_unit', true),
'wording_short' => Product::strUnit($product->unit, 'wording_short'),
] ]
) ; ) ;
} }

+ 7
- 4
producer/views/subscription/_form.php View File

<div class="recipe" v-if="product.recipe.length">{{ product.recipe }}</div> <div class="recipe" v-if="product.recipe.length">{{ product.recipe }}</div>
</td> </td>
<td class="price-unit"> <td class="price-unit">
{{ formatPrice(product.price) }}
{{ formatPrice(product.price) }}<br /><span class="unit">{{ product.wording_unit }}</span>
</td> </td>
<td class="quantity"> <td class="quantity">
<div class="input-group"> <div class="input-group">
<input type="hidden" :value="product.step" :name="'product_step_'+product.step" />
<input type="hidden" :value="product.unit" :name="'product_unit_'+product.unit" />
<span class="input-group-btn"> <span class="input-group-btn">
<button class="btn btn-default" type="button" @click="productQuantityClick(product, -1)"><span class="glyphicon glyphicon-minus"></span></button>
<button class="btn btn-default" type="button" @click="productQuantityClick(product, -product.step)"><span class="glyphicon glyphicon-minus"></span></button>
</span> </span>
<input type="text" v-model="product.quantity_form" :class="'form-control '+((product.quantity_form > 0) ? 'has-quantity' : '')"> <input type="text" v-model="product.quantity_form" :class="'form-control '+((product.quantity_form > 0) ? 'has-quantity' : '')">
<span class="input-group-addon">{{ product.wording_short }}</span>
<span class="input-group-btn"> <span class="input-group-btn">
<button class="btn btn-default" type="button" @click="productQuantityClick(product, 1)"><span class="glyphicon glyphicon-plus"></span></button>
<button class="btn btn-default" type="button" @click="productQuantityClick(product, product.step)"><span class="glyphicon glyphicon-plus"></span></button>
</span> </span>
</div> </div>
</td> </td>
<td class="price-total"> <td class="price-total">
{{ formatPrice(product.price * product.quantity_form) }}
{{ formatPrice(product.price * (product.quantity_form / product.coefficient_unit )) }}
</td> </td>
</tr> </tr>
<tr class="total"> <tr class="total">

+ 2
- 1
producer/views/subscription/index.php View File



use yii\helpers\Html; use yii\helpers\Html;
use yii\grid\GridView; use yii\grid\GridView;
use common\models\Product ;


$this->setTitle('Abonnements') ; $this->setTitle('Abonnements') ;
$this->addButton(['label' => '<span class="glyphicon glyphicon-plus"></span> Ajouter', 'url' => 'subscription/form', 'class' => 'btn btn-primary']) ; $this->addButton(['label' => '<span class="glyphicon glyphicon-plus"></span> Ajouter', 'url' => 'subscription/form', 'class' => 'btn btn-primary']) ;
foreach($model->productSubscription as $productSubscription) foreach($model->productSubscription as $productSubscription)
{ {
if(isset($productSubscription->product)) { if(isset($productSubscription->product)) {
$html .= $productSubscription->quantity . '&nbsp;x&nbsp;'.Html::encode($productSubscription->product->name).'<br />' ;
$html .= Html::encode($productSubscription->product->name).' ('.($productSubscription->quantity * Product::$unitsArray[$productSubscription->unit]['coefficient']) . '&nbsp'.Product::strUnit($productSubscription->unit, 'wording_short').')<br />' ;
} }
else { else {
$html .= 'Produit non défini<br />' ; $html .= 'Produit non défini<br />' ;

+ 20
- 6
producer/web/css/screen.css View File

/* line 73, ../sass/subscription/_form.scss */ /* line 73, ../sass/subscription/_form.scss */
.subscription-create .subscription-form .products td.quantity, .subscription-create .subscription-form .products td.quantity,
.subscription-update .subscription-form .products td.quantity { .subscription-update .subscription-form .products td.quantity {
width: 150px;
width: 180px;
} }
/* line 76, ../sass/subscription/_form.scss */ /* line 76, ../sass/subscription/_form.scss */
.subscription-create .subscription-form .products td.quantity input, .subscription-create .subscription-form .products td.quantity input,
.subscription-update .subscription-form .products td.quantity input { .subscription-update .subscription-form .products td.quantity input {
text-align: center; text-align: center;
border-right: 0px none;
} }
/* line 79, ../sass/subscription/_form.scss */ /* line 79, ../sass/subscription/_form.scss */
.subscription-create .subscription-form .products td.quantity input.has-quantity, .subscription-create .subscription-form .products td.quantity input.has-quantity,
.subscription-update .subscription-form .products td.quantity input.has-quantity { .subscription-update .subscription-form .products td.quantity input.has-quantity {
font-weight: bold; font-weight: bold;
} }
/* line 85, ../sass/subscription/_form.scss */
/* line 84, ../sass/subscription/_form.scss */
.subscription-create .subscription-form .products td.quantity .input-group-addon,
.subscription-update .subscription-form .products td.quantity .input-group-addon {
background-color: white;
padding-left: 0px;
border-left: 0px none;
}
/* line 91, ../sass/subscription/_form.scss */
.subscription-create .subscription-form .products .name, .subscription-create .subscription-form .products .name,
.subscription-update .subscription-form .products .name { .subscription-update .subscription-form .products .name {
font-family: "capsuularegular"; font-family: "capsuularegular";
font-size: 20px; font-size: 20px;
color: black; color: black;
} }
/* line 91, ../sass/subscription/_form.scss */
/* line 97, ../sass/subscription/_form.scss */
.subscription-create .subscription-form .products .description, .subscription-create .subscription-form .products .description,
.subscription-update .subscription-form .products .description { .subscription-update .subscription-form .products .description {
font-style: italic; font-style: italic;
} }
/* line 95, ../sass/subscription/_form.scss */
/* line 101, ../sass/subscription/_form.scss */
.subscription-create .subscription-form .products .recipe, .subscription-create .subscription-form .products .recipe,
.subscription-update .subscription-form .products .recipe { .subscription-update .subscription-form .products .recipe {
font-size: 12px; font-size: 12px;
} }
/* line 99, ../sass/subscription/_form.scss */
/* line 105, ../sass/subscription/_form.scss */
.subscription-create .subscription-form .products .price-unit, .subscription-create .subscription-form .products .price-total, .subscription-create .subscription-form .products .price-unit, .subscription-create .subscription-form .products .price-total,
.subscription-update .subscription-form .products .price-unit, .subscription-update .subscription-form .products .price-unit,
.subscription-update .subscription-form .products .price-total { .subscription-update .subscription-form .products .price-total {
text-align: center; text-align: center;
width: 150px; width: 150px;
} }
/* line 104, ../sass/subscription/_form.scss */
/* line 110, ../sass/subscription/_form.scss */
.subscription-create .subscription-form .products .unit,
.subscription-update .subscription-form .products .unit {
color: gray;
font-size: 13px;
}
/* line 115, ../sass/subscription/_form.scss */
.subscription-create .subscription-form .products tr.total .price-total, .subscription-create .subscription-form .products tr.total .price-total,
.subscription-update .subscription-form .products tr.total .price-total { .subscription-update .subscription-form .products tr.total .price-total {
text-align: center; text-align: center;

+ 1
- 1
producer/web/js/vuejs/subscription-form.js View File

var price = 0 ; var price = 0 ;
for(var key in this.products) { for(var key in this.products) {
if(this.products[key].quantity_form > 0) { if(this.products[key].quantity_form > 0) {
price += this.products[key].quantity_form * this.products[key].price ;
price += (this.products[key].quantity_form / this.products[key].coefficient_unit) * this.products[key].price ;
} }
} }
if(format) { if(format) {

+ 13
- 2
producer/web/sass/subscription/_form.scss View File

} }
td.quantity { td.quantity {
width: 150px ;
width: 180px ;
input { input {
text-align: center ; text-align: center ;
border-right: 0px none ;
&.has-quantity { &.has-quantity {
font-weight: bold ; font-weight: bold ;
} }
} }
.input-group-addon {
background-color: white ;
padding-left: 0px ;
border-left: 0px none ;
}
} }
.name { .name {
width: 150px ; width: 150px ;
} }
.unit {
color: gray ;
font-size: 13px ;
}
tr.total .price-total { tr.total .price-total {
text-align: center ; text-align: center ;
font-size: 20px ; font-size: 20px ;

Loading…
Cancel
Save