Exemplo n.º 1
0
 public function createStore()
 {
     $send = Input::get('sendArray');
     $outputArray = [];
     $error = false;
     if (empty($send['number']) || empty($send['purpose_of_payment']) || empty($send['sum']) || $send['clientId'] == 0) {
         $callback = 'danger';
         $messageShot = 'Ошибка!';
         $message = 'Введите все значения';
         $error = true;
     } else {
         if (is_numeric($send['sum'])) {
             if ($send['radioChoice'] == 1 && $send['debtorId'] == 0) {
                 $callback = 'danger';
                 $messageShot = 'Ошибка!';
                 $message = 'Выберите дебитора';
                 $error = true;
             } else {
                 $repayment = new Repayment();
                 $repayment->number = $send['number'];
                 $repayment->date = new Carbon($send['date']);
                 $repayment->sum = $send['sum'];
                 $repayment->balance = $send['sum'];
                 $repayment->purpose_of_payment = $send['purpose_of_payment'];
                 $client = Client::find($send['clientId']);
                 $debtor = Debtor::find($send['debtorId']);
                 if ($send['radioChoice'] == 0) {
                     $repayment->client_id = $send['clientId'];
                     $repayment->inn = $client->inn;
                     $repayment->type = 1;
                     $repayment->info = $client->name;
                     $repayment->save();
                     $callback = 'success';
                     $messageShot = 'Успешно!';
                     $message = 'П/п № ' . $repayment->number . ' успешно добавлено!';
                 } else {
                     $repayment->client_id = $send['clientId'];
                     $repayment->debtor_id = $send['debtorId'];
                     $relation = Relation::where('client_id', $send['clientId'])->where('debtor_id', $send['debtorId'])->first();
                     if ($relation == null) {
                         $callback = 'danger';
                         $messageShot = 'Ошибка!';
                         $message = 'В системе по п/п отсутствует связь!';
                         $error = true;
                     } else {
                         if ($send['typeOfPayment'] == 1) {
                             //1-клиент
                             $repayment->inn = $client->inn;
                             $repayment->info = $client->name;
                             $repayment->type = -1;
                         } else {
                             //0-дебитор
                             $repayment->inn = $debtor->inn;
                             $repayment->info = $debtor->name;
                             $repayment->type = 0;
                         }
                         $repayment->save();
                         $callback = 'success';
                         $messageShot = 'Успешно!';
                         $message = 'П/п № ' . $repayment->number . ' успешно добавлено!';
                     }
                 }
             }
         } else {
             $callback = 'danger';
             $messageShot = 'Ошибка!';
             $message = 'Некорректное значение суммы';
             $error = true;
         }
     }
     $outputArray = ['error' => $error, 'data' => ['callback' => $callback, 'message' => $message, 'message_shot' => $messageShot]];
     return $outputArray;
 }