Ejemplo n.º 1
0
 function save(array $params)
 {
     $response = new ResponseEntity();
     DB::transaction(function () use(&$response, $params) {
         $user = User::find(Auth::user()->id);
         $product = Product::find($params['product_id']);
         $cust = new Customer();
         $cust->first_name = $params['customer']['first_name'];
         $cust->last_name = $params['customer']['last_name'];
         $cust->phone_number = $params['customer']['phone_number'];
         $custCreated = $cust->save();
         if ($custCreated && $product && $user) {
             $sale = new Sale();
             $sale->user_id = $user->id;
             $sale->sale_status_id = Config::get('constants.sale_status.sale');
             $sale->product_id = $product->id;
             $sale->customer_id = $cust->id;
             $sale->date_sold = Carbon::createFromFormat('m/d/Y', $params['date_sold'])->toDateString();
             $sale->remarks = isset($params['remarks']) ? $params['remarks'] : '';
             $sale->order_number = $params['order_number'];
             $sale->ninety_days = isset($params['ninety_days']) ?: 0;
             $ok = $sale->save();
             if ($ok) {
                 $ok = $this->setIncentive($sale->user_id, $sale->date_sold);
                 $response->setMessages($ok ? ['Sale successfully created!'] : ['Failed to create sale!']);
                 $response->setSuccess($ok);
             } else {
                 $response->setMessages(['Failed to create sale!']);
             }
         } else {
             $response->setMessages(['Entities not found']);
         }
     });
     return $response;
 }
Ejemplo n.º 2
0
 /**
  * Creates a new Sale model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Sale();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 3
0
 function save(array $params)
 {
     $response = new ResponseEntity();
     try {
         DB::transaction(function () use(&$response, $params) {
             $user = User::find(1);
             $product = Product::find($params['product_id']);
             $cust = new Customer();
             $cust->first_name = $params['customer']['first_name'];
             $cust->last_name = $params['customer']['last_name'];
             $cust->phone_number = $params['customer']['phone_number'];
             $custCreated = $cust->save();
             if ($custCreated && $product && $user) {
                 $sale = new Sale();
                 $sale->user_id = $user->id;
                 $sale->product_id = $product->id;
                 $sale->customer_id = $cust->id;
                 $sale->date_sold = Carbon::createFromFormat('m/d/Y', $params['date_sold']);
                 $sale->remarks = isset($params['remarks']) ? $params['remarks'] : '';
                 $sale->order_number = $params['order_number'];
                 $sale->ninety_days = isset($params['ninety_days']) ?: 0;
                 $ok = $sale->save();
                 if ($ok) {
                     $response->setSuccess(true);
                     $response->setMessages(['Sale successfully created!']);
                 } else {
                     $response->setMessages(['Failed to create sale!']);
                 }
             } else {
                 $response->setMessages(['Entities not found']);
             }
         });
     } catch (\Exception $ex) {
         $response->setMessages(['Exception: ' . $ex->getMessage()]);
     }
     return $response;
 }
Ejemplo n.º 4
0
 /**
  * Creates a new Sale model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Sale();
     $modelDetails = null;
     if ($model->load(Yii::$app->request->post())) {
         $modelDetails = Model::createMultiple(SaleDetail::classname());
         Model::loadMultiple($modelDetails, Yii::$app->request->post());
         // validate all models
         $valid = $model->validate() & Model::validateMultiple($modelDetails);
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     foreach ($modelDetails as $modelDetail) {
                         $modelDetail->sale_id = $model->id;
                         if ($flag = $modelDetail->save(false)) {
                             $item = Item::findOne($modelDetail->item_id);
                             $item->stock -= $modelDetail->quantity;
                             $flag = $item->save();
                         }
                         if (!$flag) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['index']);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     return $this->render('create', ['model' => $model, 'modelDetails' => empty($modelDetails) ? [new SaleDetail()] : $modelDetails]);
 }