public function delete($charge, $id)
 {
     if (ChargePayment::destroy($id)) {
         return Redirect::route('charge_modify', $charge)->with('mSucces', 'Le paiement a bien été supprimé');
     } else {
         return Redirect::route('charge_modify', $charge)->with('mError', 'Impossible de supprimer ce paiement');
     }
 }
Esempio n. 2
0
 /**
  * Modify charge (form)
  */
 public function modify_check($id)
 {
     $charge = $this->dataExist($id);
     $validator = Validator::make(Input::all(), Charge::$rules);
     if (!$validator->fails()) {
         $date_explode = explode('/', Input::get('date_charge'));
         $date_payment_explode = explode('/', Input::get('date_payment'));
         $deadline_explode = explode('/', Input::get('deadline'));
         $charge->date_charge = $date_explode[2] . '-' . $date_explode[1] . '-' . $date_explode[0];
         if (Input::get('date_payment')) {
             $charge->date_payment = $date_payment_explode[2] . '-' . $date_payment_explode[1] . '-' . $date_payment_explode[0];
         }
         if (Input::get('deadline')) {
             $charge->deadline = $deadline_explode[2] . '-' . $deadline_explode[1] . '-' . $deadline_explode[0];
         }
         if (Input::get('organisation_id')) {
             $charge->organisation_id = Input::get('organisation_id');
         }
         if (Input::file('document')) {
             $document = time(true) . '.' . Input::file('document')->guessClientExtension();
             if (Input::file('document')->move('uploads/charges', $document)) {
                 if ($charge->document) {
                     unlink(public_path() . '/uploads/charges/' . $charge->document);
                 }
                 $charge->document = $document;
             }
         }
         if ($charge->save()) {
             if (Input::get('tags')) {
                 $tags = Input::get('tags');
                 $tags_keys = array();
                 foreach ($tags as $tag) {
                     if (!array_key_exists($tag, $tags_keys)) {
                         $checkTag = Tag::where('name', '=', $tag)->first();
                         if ($checkTag) {
                             if (!ChargeTag::where('charge_id', '=', $charge->id)->where('tag_id', '=', $checkTag->id)->first()) {
                                 $chargeTag = new ChargeTag();
                                 $chargeTag->charge_id = $charge->id;
                                 $chargeTag->tag_id = $checkTag->id;
                                 if ($chargeTag->save()) {
                                     $tags_keys[$tag] = $tag;
                                 }
                             } else {
                                 $tags_keys[$tag] = $tag;
                             }
                         } else {
                             if (trim($tag) != '') {
                                 $new_tag = new Tag();
                                 $new_tag->name = $tag;
                                 if ($new_tag->save()) {
                                     $chargeTag = new ChargeTag();
                                     $chargeTag->charge_id = $charge->id;
                                     $chargeTag->tag_id = $new_tag->id;
                                     if ($chargeTag->save()) {
                                         $tags_keys[$tag] = $tag;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             foreach ($charge->items as $item) {
                 ChargeItem::where('id', $item->id)->update(array('description' => Input::get('description.' . $item->id), 'amount' => Input::get('amount.' . $item->id), 'vat_types_id' => Input::get('vat_types_id.' . $item->id)));
             }
             if (Input::get('description.0')) {
                 $item = new ChargeItem();
                 $item->insert(array('charge_id' => $id, 'description' => Input::get('description.0'), 'amount' => Input::get('amount.0'), 'vat_types_id' => Input::get('vat_types_id.0')));
             }
             if (Input::get('payment_description.0')) {
                 $date_payment_explode = explode('/', Input::get('payment_date.0'));
                 $payment = new ChargePayment();
                 $payment->insert(array('charge_id' => $id, 'description' => Input::get('payment_description.0'), 'amount' => Input::get('payment_amount.0'), 'mode' => Input::get('payment_mode.0'), 'date_payment' => $date_payment_explode[2] . '-' . $date_payment_explode[1] . '-' . $date_payment_explode[0]));
             }
             return Redirect::route('charge_modify', $charge->id)->with('mSuccess', 'Cette charge a bien été modifiée');
         } else {
             return Redirect::route('charge_modify', $charge->id)->with('mError', 'Impossible de modifier cette charge')->withInput();
         }
     } else {
         return Redirect::route('charge_modify', $charge->id)->with('mError', 'Il y a des erreurs')->withErrors($validator->messages())->withInput();
     }
 }