Example #1
0
 /**
  * Displays a single Card model.
  *
  * @param string $id
  *
  * @return mixed
  */
 public function actionView($id)
 {
     $searchModel = new SalesSearch();
     //        $param  = Yii::$app->request->getQueryParams();
     $param['SalesSearch']['card'] = $id;
     $dataProvider = $searchModel->search($param);
     return $this->render('view', ['model' => $this->findModel($id), 'sales' => Sales::find(['card' => $id]), 'dataProvider' => $dataProvider, 'searchModel' => $searchModel]);
 }
 /**
  * Finds the Sales model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Sales the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Sales::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #3
0
 public function search($params)
 {
     $query = Sales::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'sum' => $this->sum, 'card' => $this->card]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
Example #4
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Sales::find();
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $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');
         return $dataProvider;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'discount' => $this->discount, 'quantity' => $this->quantity]);
     $query->andFilterWhere(['like', 'title', $this->title]);
     return $dataProvider;
 }
 public function save(Request $request)
 {
     $products = $request->get('products');
     $stockId = $request->get('stock_id');
     $customerId = $request->get('customer_id');
     $salesmanId = $request->get('employee_id');
     $sale = Sales::create(['stock_id' => $stockId, 'customer_id' => $customerId, 'employee_id' => $salesmanId]);
     /**
      * Precisa incrementar a quantidade de produtos no estoque pelo "ammount"
      */
     foreach ($products as $product) {
         $ammount = $request->get($product . '_ammount');
         $sp = StockProducts::query()->where('stock_id', $stockId)->where('product_id', $product)->first();
         SalesProducts::create(['sale_id' => $sale->id, 'stock_product_id' => $sp->id, 'ammount' => $ammount]);
         $sp->ammount -= $ammount;
         $sp->save();
     }
     return view('pages.home');
 }
 private function getData($stock, $category)
 {
     $last_period = $stock;
     $current_period = Helper::periodAfterId($last_period);
     $item_purchases = [];
     $last_stock = [];
     $current_stock = [];
     $item_sales = [];
     $item_wastes = [];
     $items = [];
     $wastage = [];
     $sales_chart = [];
     $items_without_price = 0;
     $period = null;
     if ($last_period) {
         $purchases = Purchases::orderBy('date_created', 'ASC')->where(['stock_period_id' => $last_period])->get();
         foreach ($purchases as $purchase) {
             foreach ($purchase->purchases()->get() as $itemPurchase) {
                 if (array_key_exists($itemPurchase->item_id, $item_purchases)) {
                     $item_purchases[$itemPurchase->item_id]['value'] += $itemPurchase->value;
                     $item_purchases[$itemPurchase->item_id]['occurrences']++;
                     $item_purchases[$itemPurchase->item_id]['price'] += $itemPurchase->value == 0 ? 0 : $itemPurchase->price / $itemPurchase->value;
                 } else {
                     $item_purchases[$itemPurchase->item_id]['value'] = $itemPurchase->value;
                     $item_purchases[$itemPurchase->item_id]['price'] = $itemPurchase->value == 0 ? 0 : $itemPurchase->price / $itemPurchase->value;
                     $item_purchases[$itemPurchase->item_id]['occurrences'] = 1;
                 }
             }
         }
         foreach ($item_purchases as $key => $purchase) {
             $item_purchases[$key]['price'] = $purchase['price'] / $purchase['occurrences'];
         }
         $last_stock_items = StockItem::where(['stock_period_id' => $last_period])->get();
         foreach ($last_stock_items as $si) {
             if (array_key_exists($si->item_id, $last_stock)) {
                 $last_stock[$si->item_id] += $si->stock;
             } else {
                 $last_stock[$si->item_id] = $si->stock;
             }
         }
         $current_stock_items = StockItem::where(['stock_period_id' => $current_period])->get();
         foreach ($current_stock_items as $si) {
             if (array_key_exists($si->item_id, $current_stock)) {
                 $current_stock[$si->item_id] += $si->stock;
             } else {
                 $current_stock[$si->item_id] = $si->stock;
             }
         }
         $sales = Sales::where(['stock_period_id' => $last_period])->get();
         foreach ($sales as $sale) {
             foreach ($sale->sales()->orderBy('quantity', 'DESC')->get() as $sale_item) {
                 $menu = $sale_item->menu()->first();
                 if ($menu) {
                     $color = $this->rand_color();
                     $sales_chart[$menu->id] = ['value' => $sale_item->quantity, 'color' => $color, 'highlight' => $this->alter_brightness($color, 20), 'label' => $menu->title];
                     if ($menu->type == 'item') {
                         if (array_key_exists($menu->item_id, $item_sales)) {
                             $item_sales[$menu->item_id] += $menu->value * $sale_item->quantity;
                         } else {
                             $item_sales[$menu->item_id] = $menu->value * $sale_item->quantity;
                         }
                     } elseif ($menu->type == 'recipe') {
                         $recipe = $menu->recipe()->first();
                         if ($recipe) {
                             $usage = $this->countUsageFromRecipe($recipe);
                             foreach ($usage as $key => $use) {
                                 if (array_key_exists($key, $item_sales)) {
                                     $item_sales[$key] += $sale_item->quantity * $use;
                                 } else {
                                     $item_sales[$key] = $sale_item->quantity * $use;
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $wastes = Wastes::where(['stock_period_id' => $last_period])->get();
         foreach ($wastes as $waste) {
             if (array_key_exists($waste->reason()->first()->id, $wastage)) {
                 $wastage[$waste->reason()->first()->id]['value'] = $wastage[$waste->reason()->first()->id]['value'] + 1;
             } else {
                 $color = $this->rand_color();
                 $wastage[$waste->reason()->first()->id] = ['value' => 1, 'color' => $color, 'highlight' => $this->alter_brightness($color, 20), 'label' => $waste->reason()->first()->reason];
             }
             if ($waste->type == 'item') {
                 if (array_key_exists($waste->item_id, $item_wastes)) {
                     $item_wastes[$waste->item_id] += $waste->value;
                 } else {
                     $item_wastes[$waste->item_id] = $waste->value;
                 }
             } elseif ($waste->type == 'recipe') {
                 $recipe = $waste->recipe()->first();
                 if ($recipe) {
                     $usage = $this->countUsageFromRecipe($recipe);
                     foreach ($usage as $key => $use) {
                         if (array_key_exists($key, $item_wastes)) {
                             $item_wastes[$key] += $waste->recipe_count * $use;
                         } else {
                             $item_wastes[$key] = $waste->recipe_count * $use;
                         }
                     }
                 }
             } elseif ($waste->type == 'menu') {
                 $menu = $waste->menu()->first();
                 if ($menu) {
                     if ($menu->type == 'item') {
                         if (array_key_exists($menu->item_id, $item_wastes)) {
                             $item_wastes[$menu->item_id] += $waste->menu_count * $menu->value;
                         } else {
                             $item_wastes[$menu->item_id] = $waste->menu_count * $menu->value;
                         }
                     } elseif ($menu->type == 'recipe') {
                         $recipe = $menu->recipe()->first();
                         if ($recipe) {
                             $usage = $this->countUsageFromRecipe($recipe);
                             foreach ($usage as $key => $use) {
                                 if (array_key_exists($key, $item_wastes)) {
                                     $item_wastes[$key] += $waste->menu_count * $use;
                                 } else {
                                     $item_wastes[$key] = $waste->menu_count * $use;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $all_items = Items::all();
     $variance = 0;
     $count = 0;
     foreach ($all_items as $item) {
         if (!array_key_exists($item->category()->first()->id, $items)) {
             $items[$item->category()->first()->id]['category'] = $item->category()->first()->title;
             $items[$item->category()->first()->id]['variance'] = 0;
             $items[$item->category()->first()->id]['items'] = [];
         }
         $current_item = [];
         //$items[$item->id]['object'] = $item;
         $current_item['title'] = $item->title;
         $current_item['units'] = $item->units()->where(['default' => 1])->first()->unit()->first()->title;
         $current_item['wastage'] = array_key_exists($item->id, $item_wastes) ? $item_wastes[$item->id] : 0;
         $current_item['last_stock'] = array_key_exists($item->id, $last_stock) ? $last_stock[$item->id] : 0;
         $current_item['current_stock'] = array_key_exists($item->id, $current_stock) ? $current_stock[$item->id] : 0;
         if (array_key_exists($item->id, $item_purchases)) {
             $current_item['purchases'] = $item_purchases[$item->id];
         } else {
             $price = 0;
             $item_price = ItemPurchases::where(['item_id' => $item->id])->orderBy('created_at', 'DESC')->first();
             if ($item_price) {
                 $value = $item_price->value;
                 if ($value == 0) {
                     $value = 1;
                 }
                 $price = $item_price->price / $value;
             } else {
                 if ($item->price) {
                     $price = $item->price;
                 } else {
                     $items_without_price++;
                 }
             }
             $current_item['purchases'] = ['value' => 0, 'price' => $price, 'occurrences' => 0];
         }
         $current_item['sales'] = array_key_exists($item->id, $item_sales) ? $item_sales[$item->id] : 0;
         $current_item['must_stock'] = $current_item['last_stock'] + $current_item['purchases']['value'] - $current_item['sales'] - $current_item['wastage'];
         $current_item['stock_difference'] = $current_item['current_stock'] - $current_item['must_stock'];
         $current_item['variance'] = round($current_item['stock_difference'] * $current_item['purchases']['price'], 2);
         $variance += $current_item['variance'];
         $items[$item->category()->first()->id]['variance'] += $current_item['variance'];
         $items[$item->category()->first()->id]['items'][$item->id] = $current_item;
         $count++;
     }
     return $category && array_key_exists($category, $items) ? $items[$category] : $items;
 }
Example #7
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSale()
 {
     return $this->hasOne(Sales::className(), ['id' => 'sale_id']);
 }
Example #8
0
 public function save_history($input, Lead $lead = null)
 {
     $is_admin = $is_sales = $is_member = false;
     if (is_null($lead)) {
         return false;
     }
     $lead_history = new LeadHistory();
     $lead_history->lead_id = $lead->id;
     if (isset($input['is_admin'])) {
         $lead_history->admin_id = $input['is_admin'];
     }
     if (isset($input['is_sales'])) {
         $lead_history->sales_id = $input['is_sales'];
     }
     if (isset($input['is_member'])) {
         $lead_history->member_id = $input['is_member'];
     }
     # check if status changed
     if ($input['status'] != $lead->status) {
         $lead_history->old_status = $lead->status;
         $lead_history->new_status = $input['status'];
         $lead->status = $input['status'];
     }
     # check if sales assigned
     if (isset($input['sales_id']) and !empty($input['sales_id'])) {
         if ($lead->sales_id != $input['sales_id']) {
             $lead_history->old_sales_id = $lead->sales_id;
             $lead_history->new_sales_id = $input['sales_id'];
             $lead->sales_id = $input['sales_id'];
         }
     }
     # check if there any notes
     if (!empty($input['notes'])) {
         $lead_history->notes = $input['notes'];
     }
     # if no new status update and no notes, don't save and throw error
     if (is_null($lead_history->new_sales_id) && is_null($lead_history->new_status) && is_null($lead_history->notes)) {
         return redirect()->back()->withErrors('Please insert notes, assign sales or change lead status to save history')->send();
     } else {
         $lead->save();
         $lead_history->save();
         if (!is_null($lead_history->new_sales_id)) {
             $sales = Sales::find($lead_history->new_sales_id);
             app('events')->fire('lead.assign_sales', [$lead, $sales, $lead_history->notes]);
         }
         if (!is_null($lead_history->new_status)) {
             app('events')->fire('member.lead.status_update', [$lead, $lead_history->notes]);
         }
         return $lead_history;
     }
 }
Example #9
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSales()
 {
     return $this->hasMany(Sales::className(), ['id' => 'sale_id'])->viaTable(ProductsSales::tableName(), ['product_id' => 'id']);
 }
Example #10
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id, Request $request)
 {
     $sale = Sales::findOrFail($id);
     $sale->delete();
     Helper::add($id, 'deleted sale');
     Session::flash('flash_message', $this->title . ' item successfully deleted!');
     $variables = $request->exists('stock_period_id') ? ['stock_period' => $request->get('stock_period_id')] : [];
     return Redirect::action('SalesController@index', $variables);
 }
Example #11
0
 public function validate($input, $id = null)
 {
     return Sales::validate($input, $id);
 }
Example #12
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getSales()
 {
     return $this->hasMany(Sales::className(), ['fkfruit_id' => 'fruit_id']);
 }
Example #13
0
File: Card.php Project: riros/tests
 public function afterDelete()
 {
     $sales = Sales::find()->where(['card' => $this->no])->all();
     foreach ($sales as $sale) {
         $sale->delete();
     }
 }