Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. public function actionImport(){
  2. $modelImport = new \yii\base\DynamicModel([
  3. 'fileImport'=>'File Import',
  4. ]);
  5. $modelImport->addRule(['fileImport'],'required');
  6. $modelImport->addRule(['fileImport'],'file',['extensions'=>'ods,xls,xlsx']);
  7. if(Yii::$app->request->post()){
  8. $modelImport->fileImport = \yii\web\UploadedFile::getInstance($modelImport,'fileImport');
  9. if ($modelImport->fileImport && $modelImport->validate()){
  10. $inputFileType = \PHPExcel_IOFactory::identify($modelImport->fileImport->tempName);
  11. $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
  12. $objPHPExcel = $objReader->load($modelImport->fileImport->tempName);
  13. $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
  14. $baseRow = 1;
  15.  
  16. while(!empty($sheetData[$baseRow]['C'])){
  17. $x = Item::find()->where(['item_no'=>$sheetData[$baseRow]['C']])->count();
  18. if($x==0){
  19. $connection = \Yii::$app->db;
  20. $connection->createCommand()->insert('item',['item_no'=>$sheetData[$baseRow]['C']])->execute();
  21.  
  22. }
  23.  
  24. while(!empty($sheetData[$baseRow]['E'])){
  25. $x = Salesman::find()->where(['id'=>$sheetData[$baseRow]['E']])->count();
  26. if($x==0){
  27. $connection = \Yii::$app->db;
  28. $connection->createCommand()->insert('salesman',['id'=>$sheetData[$baseRow]['E']])->execute();
  29.  
  30.  
  31.  
  32. }
  33.  
  34. while(!empty($sheetData[$baseRow]['D'])){
  35. $x = Customer::find()->where(['customer_no'=>$sheetData[$baseRow]['D']])->count();
  36. if($x==0){
  37. $connection = \Yii::$app->db;
  38. $connection->createCommand()->insert('customer',['customer_no'=>$sheetData[$baseRow]['D']])->execute();
  39. }
  40.  
  41. while(!empty($sheetData[$baseRow]['F'])){
  42. $x = Invoice::find()->where(['invoice_no'=>$sheetData[$baseRow]['F']])->count();
  43. if($x==0){
  44. $connection = \Yii::$app->db;
  45. $connection->createCommand()->insert('invoice',['invoice_no'=>$sheetData[$baseRow]['F']])->execute();
  46.  
  47.  
  48. $model = new TransaksiItem();
  49. $model->qty = (string) $sheetData[$baseRow]['A'];
  50. $model->unit_price = (string) $sheetData[$baseRow]['B'];
  51. $model->item_no = (string) $sheetData[$baseRow]['C'];
  52. $model->customer_no = (string) $sheetData[$baseRow]['D'];
  53. $model->id_salesman = (string) $sheetData[$baseRow]['E'];
  54. $model->id_invoice = (string) $sheetData[$baseRow]['F'];
  55. $model->inv_date = (string) $sheetData[$baseRow]['G'];
  56. $model->rate = (string) $sheetData[$baseRow]['H'];
  57.  
  58. $model->save();
  59.  
  60. $baseRow++;
  61.  
  62. }}}}}
  63.  
  64.  
  65. Yii::$app->getSession()->setFlash('success','Success');
  66. }else{
  67. Yii::$app->getSession()->setFlash('error','Error');
  68. }
  69. //return $this->redirect(['barang/index']);
  70.  
  71. }
  72.  
  73. return $this->render('import',[
  74. 'modelImport'=>$modelImport,
  75. ]);
  76. }
  77.  
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement