Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function actionImport(){
- $modelImport = new \yii\base\DynamicModel([
- 'fileImport'=>'File Import',
- ]);
- $modelImport->addRule(['fileImport'],'required');
- $modelImport->addRule(['fileImport'],'file',['extensions'=>'ods,xls,xlsx']);
- if(Yii::$app->request->post()){
- $modelImport->fileImport = \yii\web\UploadedFile::getInstance($modelImport,'fileImport');
- if ($modelImport->fileImport && $modelImport->validate()){
- $inputFileType = \PHPExcel_IOFactory::identify($modelImport->fileImport->tempName);
- $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
- $objPHPExcel = $objReader->load($modelImport->fileImport->tempName);
- $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
- $baseRow = 1;
- while(!empty($sheetData[$baseRow]['C'])){
- $x = Item::find()->where(['item_no'=>$sheetData[$baseRow]['C']])->count();
- if($x==0){
- $connection = \Yii::$app->db;
- $connection->createCommand()->insert('item',['item_no'=>$sheetData[$baseRow]['C']])->execute();
- }
- while(!empty($sheetData[$baseRow]['E'])){
- $x = Salesman::find()->where(['id'=>$sheetData[$baseRow]['E']])->count();
- if($x==0){
- $connection = \Yii::$app->db;
- $connection->createCommand()->insert('salesman',['id'=>$sheetData[$baseRow]['E']])->execute();
- }
- while(!empty($sheetData[$baseRow]['D'])){
- $x = Customer::find()->where(['customer_no'=>$sheetData[$baseRow]['D']])->count();
- if($x==0){
- $connection = \Yii::$app->db;
- $connection->createCommand()->insert('customer',['customer_no'=>$sheetData[$baseRow]['D']])->execute();
- }
- while(!empty($sheetData[$baseRow]['F'])){
- $x = Invoice::find()->where(['invoice_no'=>$sheetData[$baseRow]['F']])->count();
- if($x==0){
- $connection = \Yii::$app->db;
- $connection->createCommand()->insert('invoice',['invoice_no'=>$sheetData[$baseRow]['F']])->execute();
- $model = new TransaksiItem();
- $model->qty = (string) $sheetData[$baseRow]['A'];
- $model->unit_price = (string) $sheetData[$baseRow]['B'];
- $model->item_no = (string) $sheetData[$baseRow]['C'];
- $model->customer_no = (string) $sheetData[$baseRow]['D'];
- $model->id_salesman = (string) $sheetData[$baseRow]['E'];
- $model->id_invoice = (string) $sheetData[$baseRow]['F'];
- $model->inv_date = (string) $sheetData[$baseRow]['G'];
- $model->rate = (string) $sheetData[$baseRow]['H'];
- $model->save();
- $baseRow++;
- }}}}}
- Yii::$app->getSession()->setFlash('success','Success');
- }else{
- Yii::$app->getSession()->setFlash('error','Error');
- }
- //return $this->redirect(['barang/index']);
- }
- return $this->render('import',[
- 'modelImport'=>$modelImport,
- ]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement