public function actionCreateid()
 {
     $session = new Session();
     $session->open();
     if (\yii::$app->request->isAjax) {
         $id = \yii::$app->request->post('invid');
         $session['invid'] = $id;
         $x;
         $count = 0;
         foreach ($session['invid'] as $data) {
             //
             $model = Saleorderline::find()->where(['saleid' => $data])->all();
             $model2 = new \backend\models\Saleorderinvoiceline();
             foreach ($model as $value) {
                 $model2 = new \backend\models\Saleorderinvoiceline();
                 $count += 1;
                 $model2->invid = $session['invrecid'];
                 $model2->invline = $count;
                 $model2->partno = $value->custorderno;
                 $model2->description = $value->customername;
                 $model2->quantity = $value->quantity;
                 $model2->unitprice = $value->unitprice;
                 $model2->totalamount = $value->totalamount;
                 $model2->unit = $value->unit;
                 $model2->saleid = $value->saleid;
                 $model2->invoiceqty = $value->quantity;
                 $model2->save();
             }
             $model3 = new \backend\models\Invoicerefsale();
             $model3->invid = $session['invrecid'];
             $model3->saleid = $data;
             $model3->save();
             //cu  $x = $data;
         }
         //            foreach ($_POST['sales'] as $data)
         //            {
         //                $model2 = new \backend\models\Invoicerefsale();
         //                $model2->invid =$session['invid'];
         //                $model2->saleid = $data;
         //
         //                $model2->save();
         //            }
         //   return $this->redirect(['saleorderinvoice/update','id'=>$session['invid']]);
         return $this->redirect(['saleorderinvoice/update', 'id' => $session['invrecid']]);
         //  return $count;
     } else {
         // echo "ess";
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Saleorderline::find();
     $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;
     }
     //        $query->andFilterWhere([
     //            'recid' => $this->recid,
     //            'saleid' => $this->saleid,
     //            'saleline' => $this->saleline,
     //            'quantity' => $this->quantity,
     //            'unitprice' => $this->unitprice,
     //            'totalamount' => $this->totalamount,
     //        ]);
     $query->orFilterWhere(['like', 'custorderno', $this->globalSearch])->orFilterWhere(['like', 'customername', $this->globalSearch])->orFilterWhere(['like', 'partno', $this->globalSearch]);
     return $dataProvider;
 }
 public function actionDelete($id)
 {
     if ($this->findModel($id)->delete()) {
         $deldetail = Saleorderline::deleteAll('saleid = :saleid', [':saleid' => $id]);
         if ($deldetail) {
             return $this->redirect(['index']);
         }
         return $this->redirect(['index']);
     }
 }
 public function actionUpdate($id)
 {
     $session = new Session();
     $session->open();
     $totalCount = Saleorderline::find()->where(['saleid' => $id])->count();
     $pages = new Pagination(['totalCount' => $totalCount, 'pageSize' => 20]);
     $searchModel = Saleorderline::find()->where(['saleid' => $id])->orderBy('recid')->offset($pages->offset)->limit($pages->limit)->all();
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post())) {
         $saledate = $_POST['Saleorder']['saledate'];
         $shipdate = $_POST['Saleorder']['shipdate'];
         $model->saledate = date('d/M/Y H:i:s', strtotime($saledate));
         $model->shipdate = date('d/M/Y H:i:s', strtotime($shipdate));
         //         $model->shipfrom = $_POST['Saleorder']['shipfrom'];
         $model->shipto = isset($session['shipto']) ? $session['shipto'] : '';
         $model->saleman = isset($session['salecode']) ? $session['salecode'] : NULL;
         $model->createby = $session['userid'];
         if ($model->save()) {
             $uploaded = UploadedFile::getInstance($model, 'upfile');
             $result = 0;
             if (!empty($uploaded)) {
                 //                    print_r($uploaded);
                 //                    return;
                 $deldetail = Saleorderline::deleteAll('saleid = :saleid', [':saleid' => $id]);
                 $upfiles = time() . "." . $uploaded->getExtension();
                 $uploaded->saveAs('../../uploads/' . $upfiles);
                 //                    // $uploaded->saveAs();
                 //                  //  move_uploaded_file($uploaded, '../../uploads/' . $upfiles);
                 //
                 $handle = fopen('../../uploads/' . $upfiles, 'r');
                 //                  $handle = fopen($uploaded, 'r');
                 $n = 0;
                 setlocale(LC_ALL, 'en_US.UTF-8');
                 while (($fileop = fgetcsv($handle, 1000, ",")) !== false) {
                     if ($n < 1) {
                         $n++;
                         continue;
                     }
                     $model2 = new \backend\models\Saleorderline();
                     $model2->saleid = $model->recid;
                     $model2->saleline = $fileop[0];
                     $model2->partno = $fileop[1];
                     $model2->custorderno = iconv("TIS-620", "UTF-8", $fileop[2]);
                     $model2->customername = iconv("TIS-620", "UTF-8", $fileop[3]);
                     $model2->quantity = $fileop[4];
                     $model2->unitprice = $fileop[5];
                     $model2->totalamount = $fileop[6];
                     $model2->unit = 1;
                     //                         $model2->partno=$fileop[7];
                     //  echo $name."<BR />";
                     // $age = $fileop[1];
                     //$location = $fileop[2];
                     //print_r($fileop);exit();
                     //                        $sql = "INSERT INTO details(name, age, location) VALUES ('$name', '$age', '$location')";
                     //                        $query = Yii::$app->db->createCommand($sql)->execute();
                     if ($model2->save()) {
                         $result++;
                     }
                 }
                 fclose($handle);
                 unlink('../../uploads/' . $upfiles);
                 if ($result > 0) {
                     $session = new \yii\web\Session();
                     $session->open();
                     $session->setFlash('msgsuccess', 'บันทึกรายการเรียบร้อย');
                     return $this->redirect(['update', 'id' => $model->recid]);
                 }
             }
         }
     } else {
         return $this->render('update', ['model' => $model, 'saleline' => $searchModel, 'pages' => $pages, 'rowcount' => $totalCount]);
     }
     return $this->render('update', ['model' => $model, 'saleline' => $searchModel, 'pages' => $pages, 'rowcount' => $totalCount]);
 }
 /**
  * Finds the Saleorderline model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Saleorderline the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Saleorderline::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }