public function actionBulkentry() { if (!$this->token instanceof Token) { echo CJSON::encode($this->statusError('You have to login first')); Yii::app()->end(); } ini_set('max_execution_time', 300); $model = new FOrderDataEntry(); if (isset($_FILES['FOrderDataEntry'])) { $model->attributes = $_FILES['FOrderDataEntry']; $model->setScenario('api'); if ($model->validate(array('file'))) { $model->setScenario('insert'); $customer = Customer::model()->findByPk($this->token->customer_id); $contact = $customer->getContactData(); $csvFile = CUploadedFile::getInstance($model, 'file'); $tempLoc = $csvFile->getTempName(); $rawdatas = file($tempLoc); $city_routing = IntraCityRouting::model()->findByAttributes(array('postcode' => $contact->postal)); if ($city_routing instanceof IntraCityRouting) { $routing_code = $city_routing->code; } else { $routing_code = ''; } try { $trans = Yii::app()->db->beginTransaction(); $bulk = Shipment::bulkOrder($rawdatas, $customer, $contact, $routing_code); $list_failed = implode(', ', $bulk['failed']); $list_success = implode(', ', $bulk['success']); echo CJSON::encode($this->statusSuccess(array('failed' => $list_failed, 'success' => $list_success))); Yii::app()->end(); $trans->commit(); } catch (ServiceControllerException $e) { CVarDumper::dump($e, 10, TRUE); exit; $trans->rollBack(); } } else { echo CJSON::encode($this->statusError($model->getErrors())); Yii::app()->end(); } } else { echo CJSON::encode($this->statusError('The File is not exixst')); yii::app()->end(); } }
public function actionEntryBulkOrder() { ini_set('max_execution_time', 300); $this->layout = '//layouts/column2'; $model = new FOrderDataEntry(); if (isset($_POST['FOrderDataEntry'])) { $model->attributes = $_POST['FOrderDataEntry']; if ($model->validate()) { $customer = Customer::model()->findByAttributes(array('accountnr' => $model->customer_account)); $contact = $customer->getContactData(); $csvFile = CUploadedFile::getInstance($model, 'file'); $tempLoc = $csvFile->getTempName(); $rawdatas = file_get_contents($tempLoc); $rawdatas = trim($rawdatas); $data = explode("\r", $rawdatas); $data = count($data) != 1 ? $data : explode("\n", $rawdatas); $city_routing = IntraCityRouting::model()->findByAttributes(array('postcode' => $contact->postal)); if ($city_routing instanceof IntraCityRouting) { $routing_code = $city_routing->code; } else { $routing_code = ''; } try { $trans = Yii::app()->db->beginTransaction(); $bulk = Shipment::bulkOrder($data, $customer, $contact, $routing_code); if (count($bulk['failed']) != 0) { $counter = array(); foreach ($bulk['failed'] as $key => $val) { array_push($counter, $val['counter']); } $list_failed = implode(', ', $counter); Yii::app()->user->setFlash('error', 'Failed at line : ' . $list_failed); } else { Yii::app()->user->setFlash('success', 'Success importing all datas'); } $trans->commit(); } catch (Exception $e) { CVarDumper::dump($e, 10, TRUE); exit; $trans->rollBack(); } } } $this->render("importDataEntry", array('model' => $model)); }