Exemplo n.º 1
0
 /**
  * Creates a new Saleorderline model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Saleorderline();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->recid]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 2
0
 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]);
 }