/** * observe Calendar event deleting * 1. delete child * 2. delete chart * 3. delete schedule * 4. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. delete child foreach ($model->calendars as $key => $value) { if (!$value->delete()) { $errors->add('Calendar', $value->getError()); } } //2. delete chart foreach ($model->follows as $key => $value) { if (!$value->delete()) { $errors->add('Calendar', $value->getError()); } } //3. delete schedule foreach ($model->schedules as $key => $value) { if (!$value->delete()) { $errors->add('Calendar', $value->getError()); } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
function saving($model) { $errors = new MessageBag(); /////////// // RULES // /////////// if (is_null($model->_id)) { $id = 0; } else { $id = $model->_id; } ////////////// // VALIDATE // ////////////// $client = Client::key($model->key)->where('_id', '<>', $id)->first(); if ($client) { $errors->add('Key', 'Key must be unique'); } $client = Client::secret($model->key)->where('_id', '<>', $id)->first(); if ($client) { $errors->add('Secret', 'Secret must be unique'); } if ($errors->count()) { $model->setErrors($errors); return false; } }
/** * Veritrans Credit Card * * 1. Check Order * 2. Save Payment * * @return Response */ public function veritranscc() { if (!Input::has('order_id')) { return new JSend('error', (array) Input::all(), 'Tidak ada data order id.'); } $errors = new MessageBag(); DB::beginTransaction(); //1. Validate Sale Parameter $order = Input::only('order_id', 'gross_amount', 'payment_type', 'masked_card', 'transaction_id'); //1a. Get original data $sale_data = \App\Models\Sale::findorfail($order['order_id']); //2. Save Payment $paid_data = new \App\Models\Payment(); $payment['transaction_id'] = $sale_data['id']; $payment['method'] = $order['payment_type']; $payment['destination'] = 'Veritrans'; $payment['account_name'] = $order['masked_card']; $payment['account_number'] = $order['transaction_id']; $payment['ondate'] = \Carbon\Carbon::parse($order['transaction_time'])->format('Y-m-d H:i:s'); $payment['amount'] = $order['gross_amount']; $paid_data = $paid_data->fill($payment); if (!$paid_data->save()) { $errors->add('Log', $paid_data->getError()); } if ($errors->count()) { DB::rollback(); return response()->json(new JSend('error', (array) Input::all(), $errors), 404); } DB::commit(); $final_sale = \App\Models\Sale::id($sale_data['id'])->with(['voucher', 'transactionlogs', 'user', 'transactiondetails', 'transactiondetails.varian', 'transactiondetails.varian.product', 'paidpointlogs', 'payment', 'shipment', 'shipment.address', 'shipment.courier', 'transactionextensions', 'transactionextensions.productextension'])->first()->toArray(); return response()->json(new JSend('success', (array) $final_sale), 200); }
/** * observe policy saving * 1. act if error or not * * @param $model * @return bool */ public function saving($model) { $errors = new MessageBag(); if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe user invitation log event deleting * 1. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); $errors->add('User', 'Tidak bisa menghapus log invitation.'); if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe Employee event deleting * 1. check contract works * 2. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. check contract works if ($model->contractworks->count()) { $errors->add('ContractElement', 'Tidak dapat menghapus element kontrak yang sudah / sedang dipakai dalam kontrak kerja karyawan.'); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe payment event deleting * 1. check relationship with transaction * 2. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. check relationship with transaction if ($model->sale()->count()) { $errors->add('Payment', 'Tidak bisa menghapus data payment yang sudah divalidasi.'); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe Template event deleting * 1. check documentdetails * 2. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. check documentdetails if ($model->documentdetails()->count()) { $errors->add('Template', 'Tidak dapat menghapus dokumen yang berkaitan dengan karyawan atau yang memiliki template.'); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe QuotaLog event created * 1. Audit * 2. act, accept or refuse * * @param $model * @return bool */ public function created($model) { $errors = new MessageBag(); //1. audit if ($model->voucher()->count()) { event(new AuditStore($model, 'quota_added', 'Penambahan quota voucher ' . $model->voucher->code)); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe address event deleting * 1. check if zipcode updated * 2. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. check if address was destination if ($model->shipments()->count()) { $errors->add('Address', 'Tidak dapat menghapus alamat yang pernah digunakan dalam pengiriman.'); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe Workleave event deleting * 1. check followed works * 2. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. check followed works if ($model->followedworks()->count()) { $errors->add('Workleave', 'Tidak dapat menghapus data cuti yang menjadi acuan cuti karyawan. Silahkan non aktif kan data cuti yang tidak berlaku lagi.'); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe Price event saved * 1. Audit * 2. act, accept or refuse * * @param $model * @return bool */ public function saved($model) { $errors = new MessageBag(); //1. audit if ($model->product()->count()) { event(new AuditStore($model, 'price_changed', 'Perubahan harga ' . $model->product->name)); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe product event deleting * 1. check varian relationship with transaction * 2. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); /* --------------------------------DELETE VARIAN RELATIONSHIP--------------------------------------*/ //1. Check varian relationship with transaction if ($model->transactions()->count()) { $errors->add('varian', 'Tidak dapat menghapus produk varian yang pernah di stok &/ order.'); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe policy event deleting * 1. validate delete * 2. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. validate deleting $validating_policy = new POO(); if (!$validating_policy->validatedeleting($model)) { $errors->add('Policy', $validating_policy->getError()); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * Validate the model instance * * @param array $rules Validation rules * @param array $customMessages Custom error messages * @param array $customAttributes Custom attributes * @return bool * @throws InvalidModelException */ public function validate(array $rules = array(), array $customMessages = array(), array $customAttributes = array()) { if ($this->fireModelEvent('validating') === false) { if ($this->throwOnValidation) { throw new InvalidModelException($this); } else { return false; } } // check for overrides, then remove any empty rules $rules = empty($rules) ? static::$rules : $rules; foreach ($rules as $field => $rls) { if ($rls == '') { unset($rules[$field]); } } if (empty($rules)) { $success = true; } else { $customMessages = empty($customMessages) ? static::$customMessages : $customMessages; $customAttributes = empty($customAttributes) ? static::$customAttributes : $customAttributes; if ($this->forceEntityHydrationFromInput || empty($this->attributes) && $this->autoHydrateEntityFromInput) { $this->fill(Input::all()); } $data = $this->getAttributes(); // the data under validation // perform validation $this->validator = static::makeValidator($data, $rules, $customMessages, $customAttributes); $success = $this->validator->passes(); if ($success) { // if the model is valid, unset old errors if ($this->validationErrors === null || $this->validationErrors->count() > 0) { $this->validationErrors = new MessageBag(); } } else { // otherwise set the new ones $this->validationErrors = $this->validator->messages(); // stash the input to the current session if (!self::$externalValidator && Input::hasSession()) { Input::flash(); } } } $this->fireModelEvent('validated', false); if (!$success && $this->throwOnValidation) { throw new InvalidModelException($this); } return $success; }
/** * observe Work event deleting * 1. delete contract * 2. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. delete contract foreach ($model->contractworks as $key => $value) { if (!$value->delete()) { $errors->add('Employee', $value->getError()); } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe PersonContact event saving * 1. check item email * 2. act, accept or refuse * * @param $model * @return bool */ public function saving($model) { $errors = new MessageBag(); // 1. check item email if ($model->item == 'email' && $model->person()->count()) { $other_person = PersonContact::item('email')->notpersonid($model->person_id)->value($model->value)->first(); if ($other_person) { $errors->add('Email', 'Email sudah terdaftar sebagai milik ' . $other_person->person->name); } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe personschedule event deleted * 1. check employee on that day logs * 2. act, accept or refuse * * @param $model * @return bool */ public function deleted($model) { $errors = new MessageBag(); //1. check employee on that day logs if ($model->person()->count()) { $log = \App\Models\Log::ondate($model->on->format('Y-m-d'))->personid($model->person_id)->first(); if ($log) { $log->created_by = $model->created_by; if (!$log->save()) { $errors->add('Log', $log->getError()); } } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe Api event saving * 1. unique workstation address * 2. act, accept or refuse * * @param $model * @return bool */ public function saving($model) { $errors = new MessageBag(); if (is_null($model->id)) { $id = 0; } else { $id = $model->id; } //1. unique workstation address $other_api = Api::address($model->workstation_address)->notid($id)->first(); if ($other_api) { $errors->add('Api', 'Workstation ID sudah terdaftar'); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * Validate a one to many relationship * * @param string $method * @param string $name * @param string $type * @return bool */ private function recurseRelationship($method, $name, $type = 'one') { $dataset = $this->getParameters('dataset', $name); // Assume by default this will pass $result = true; if ($type == 'one') { $result = $this->recurseRecord($method, $name, $dataset); } else { foreach ($dataset as $index => $data) { $subresult = $this->recurseRecord($method, $name, $data, $index); // Relationship failures are failures if (!$subresult) { $result = false; } } } // A positive result and no errors mean action was successful return $result && $this->errors->count() === 0; }
/** * Validate the model instance * * @param array $rules Validation rules * @param array $customMessages Custom error messages * @return bool */ public function validate(array $rules = array(), array $customMessages = array()) { // check for overrides, then remove any empty rules $rules = empty($rules) ? static::$rules : $rules; foreach ($rules as $field => $rls) { if ($rls == '') { unset($rules[$field]); } } if (empty($rules)) { return true; } $customMessages = empty($customMessages) ? static::$customMessages : $customMessages; if ($this->forceEntityHydrationFromInput || empty($this->attributes) && $this->autoHydrateEntityFromInput) { // pluck only the fields which are defined in the validation rule-set $attributes = array_intersect_key(Input::all(), $rules); //Set each given attribute on the model foreach ($attributes as $key => $value) { $this->setAttribute($key, $value); } } $data = $this->attributes; // the data under validation // perform validation $validator = Validator::make($data, $rules, $customMessages); $success = $validator->passes(); if ($success) { // if the model is valid, unset old errors if ($this->validationErrors->count() > 0) { $this->validationErrors = new MessageBag(); } } else { // otherwise set the new ones $this->validationErrors = $validator->messages(); // stash the input to the current session if (Input::hasSessionStore()) { Input::flash(); } } return $success; }
/** * observe product extension event deleting * 1. check transaction * 2. delete image * 3. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. check transaction $transaction = TransactionExtension::productextensionid($model->id)->first(); if ($transaction) { $errors->add('Product', 'Tidak dapat menghapus produk yang sudah pernah dibeli.'); } //2. delete product extension's image $images = Image::where('imageable_type', 'App\\Models\\ProductExtension')->where('imageable_id', $model->id)->get(); foreach ($images as $image) { if (!$image->delete()) { $errors->add('image', $image->getError()); } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe Document event deleting * 1. delete person documents * 2. delete document template * 3. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. delete person documents foreach ($model->persondocuments as $key => $value) { if (!$value->delete()) { $errors->add('Document', $delete->getError()); } } //2. delete document template foreach ($model->templates as $key => $value) { if (!$value->delete()) { $errors->add('Document', $delete->getError()); } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe purchase event deleting * 1. remove transaction details * 2. remove transaction logs * 3. execute it there was no error * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. Remove all details foreach ($model->transactiondetails as $key => $value) { if (!$value->delete()) { $errors->add('Log', $value->getError()); } } //2. Remove all logs foreach ($model->transactionlogs as $key => $value) { if (!$value->delete()) { $errors->add('Log', $value->getError()); } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe branch event deleting * 1. delete chart * 2. delete contact * 3. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. delete chart foreach ($model->charts as $key => $value) { if (!$value->delete()) { $errors->add('Branch', $value->getError()); } } //2. delete contact foreach ($model->contacts as $key => $value) { if (!$value->delete()) { $errors->add('Branch', $value->getError()); } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe Calendar event deleting * 1. delete schedule * 2. delete calendar work * 3. act, accept or refuse * * @param $model * @return bool */ public function deleting($model) { $errors = new MessageBag(); //1. delete schedule foreach ($model->schedules as $key => $value) { if (!$value->delete()) { $errors->add('Calendar', $value->getError()); } } //2. delete calendar work $works = CalendarWork::calendarid($model->id)->get(); foreach ($works as $key => $value) { if (!$value->delete()) { $errors->add('Calendar', $value->getError()); } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * observe transaction detail event saving * 1. Check transaction status * 2. Check duplicate product extension * 3. act, accept or refuse * * @param $model * @return bool */ public function saving($model) { $errors = new MessageBag(); //1. check transactions tatus if ($model->sale()->count() && $model->sale->status != 'cart' && count($model->getDirty())) { $errors->add('Detail', 'Tidak dapat menambahkan item baru. Silahkan membuat nota baru.'); } //2. Check duplicate product extension if (is_null($model->id)) { $id = 0; } else { $id = $model->id; } $check_prev_trans = \App\Models\TransactionExtension::transactionid($model->transaction_id)->productextensionid($model->product_extension_id)->notid($id)->first(); if ($check_prev_trans) { $errors->add('Detail', 'Tidak dapat menyimpan 2 record untuk product extension id yang sama.'); } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * Store a queue * * 1. Validate Price Parameter * * @return Response */ public function price() { if (!Input::has('price')) { return new JSend('error', (array) Input::all(), 'Tidak ada data price.'); } $user = \LucaDegasperi\OAuth2Server\Facades\Authorizer::getResourceOwnerId(); $user = json_decode($user, true)['data']; if ($user) { $userid = $user['id']; } else { \App::abort(404); } $errors = new MessageBag(); DB::beginTransaction(); //1. Validate Price Parameter $price = Input::get('price'); $price_rules = ['discount_amount' => 'required_without:discount_percentage|numeric', 'discount_percentage' => 'required_without:discount_amount|numeric', 'started_at' => 'required|date_format:"Y-m-d H:i:s"', 'ended_at' => 'required|date_format:"Y-m-d H:i:s"|after:started_at', 'category_ids' => 'required_if:item,category|array', 'tag_ids' => 'required_if:item,tag|array', 'is_labeled' => 'boolean']; $validator = Validator::make($price, $price_rules); if (!$validator->passes()) { $errors->add('Price', $validator->errors()); } else { $products = new \App\Models\Product(); $products = $products->sellable(true); if (isset($price['category_ids'])) { $products = $products->categoriesid($price['category_ids']); } elseif (isset($price['tag_ids'])) { $products = $products->tagsid($price['tag_ids']); } $products = $products->get(['id']); $parameter = $price; $queue = new \App\Models\Queue(); $queue->fill(['user_id' => $userid, 'process_name' => 'broadcast:discount', 'parameter' => json_encode($parameter), 'total_process' => count($products), 'task_per_process' => 1, 'process_number' => 0, 'total_task' => count($products), 'message' => 'Initial Commit']); if (!$queue->save()) { $errors->add('Product', $queue->getError()); } } //End of validate price if ($errors->count()) { DB::rollback(); return new JSend('error', (array) Input::all(), $errors); } DB::commit(); $final_queue = \App\Models\Queue::id($queue['id'])->first()->toArray(); return new JSend('success', (array) $final_queue); }
/** * function to store an item to cart or transaction detail * * 1. Parsing data * 2. Check quantity of varian * 3. Check logged user * 4. return data * @param current cart, changes product, changes qty, changes varian */ function addToCart($temp_carts, $product, $qtys, $varianids) { //1. Parsing data $errors = new MessageBag(); //2. Check quantity of varian foreach ($varianids as $key => $value) { //2a. get valid quantity of varian if (isset($qtys[$value]) && $qtys[$value] != 0 && isset($temp_carts[$product['id']]['varians'][$value])) { $validqty = $qtys[$value]; } elseif (isset($temp_carts[$product['id']]['varians'][$value]) && $qtys[$value] != 0) { $validqty = $temp_carts[$product['id']]['varians'][$value]['quantity']; } else { $validqty = $qtys[$value]; } //2b. collect varian from product foreach ($product['varians'] as $key2 => $value2) { if ($value2['id'] == $value) { $varianp = $value2; } } //2c. check varian stock if (isset($varianp)) { if ($varianp['current_stock'] < $validqty || $varianp['current_stock'] == 0) { $errors->add('Stock', $product['name'] . ' tidak tersedia dalam ukuran ' . $varianp['size'] . '.'); } } // else // { // $errors->add('Stock', $product['name']. ' tidak tersedia dalam ukuran yang dicari.'); // } //2d. parsing detail if (!$errors->count() && $validqty != 0 && isset($varianp)) { if (!isset($temp_carts[$product['id']])) { $temp_carts[$product['id']] = $product; $temp_carts[$product['id']]['discount'] = $product['promo_price'] != 0 ? $product['price'] - $product['promo_price'] : 0; unset($temp_carts[$product['id']]['varians']); } $temp_carts[$product['id']]['varians'][$varianp['id']] = ['varian_id' => $varianp['id'], 'sku' => $varianp['sku'], 'quantity' => $validqty, 'size' => $varianp['size'], 'current_stock' => $varianp['current_stock']]; } elseif (!$errors->count() && $validqty == 0 && isset($temp_carts[$product['id']]) && isset($varianp)) { unset($temp_carts[$product['id']]['varians'][$varianp['id']]); } } // Check if temp carts is 0 to flash session carts if (count($temp_carts) == 0) { Session::flash('carts', 'remove carts'); } elseif (count($temp_carts[$product['id']]['varians']) == 0) { unset($temp_carts[$product['id']]); } //3. Check logged user if (Session::has('whoami') && !$errors->count()) { //3a. Check cart $APIUser = new APIUser(); $order_in_cart = $APIUser->getMeOrderInCart(['user_id' => Session::get('whoami')['id']]); if ($order_in_cart['status'] != 'success') { $order['id'] = ''; $order['transactiondetails'] = []; } else { $order['id'] = $order_in_cart['data']['id']; $order['voucher_id'] = $order_in_cart['data']['voucher_id']; $order['transactiondetails'] = $order_in_cart['data']['transactiondetails']; } $order_detail = []; //3b. Check transactiondetail foreach ($temp_carts as $key => $value) { foreach ($value['varians'] as $key2 => $value2) { $id = ''; foreach ($order['transactiondetails'] as $key3 => $value3) { if ($value3['varian_id'] == $value2['varian_id']) { $id = $value3['id']; } else { $id = ''; } } $varian = ['id' => $id, 'varian_id' => $value2['varian_id'], 'quantity' => $value2['quantity'], 'price' => $value['price'], 'discount' => $value['discount']]; $order_detail[] = $varian; } } if (empty($temp_carts)) { foreach ($order['transactiondetails'] as $keyx => $valuex) { $valuex['quantity'] = 0; $order_detail[] = $valuex; } } $order['transactiondetails'] = $order_detail; $order['status'] = 'cart'; $order['user_id'] = Session::get('whoami')['id']; //3c. Store cart $result = $APIUser->postMeOrder($order); //3d. check result if (isset($result['message'])) { $errors->add('Cart', $result['message']); } } //4. return data if ($errors->count()) { return ['status' => false, 'data' => $temp_carts, 'message' => $errors]; } return ['status' => true, 'data' => $temp_carts]; }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id, Requests\CreateClaimRequest $request) { $request = $request->all(); $claim = Claim::findOrFail($id); $request['update_by'] = Auth::user()->id; $propertyList = array(); $errors = new Support\MessageBag(); if (isset($request["property"])) { $properties = Property::getPropertyByModel($claim); foreach ($properties as $property) { try { //if(isset($request["property"][$property->id])) //{ $attributes = ['value' => $request["property"][$property->id], 'property_id' => $property->id, 'element_id' => $claim->id]; if ($property->type == 'date') { $pr = PropertyTypes\DateProperty::where('property_id', $property->id)->where('element_id', $claim->id)->first(); //$pr->setPropertyTitle($property->title); if (empty($pr)) { $pr = new PropertyTypes\DateProperty($attributes, $property->title); } else { $pr->value = $attributes["value"]; } } elseif ($property->type == 'number') { $pr = PropertyTypes\NumberProperty::where('property_id', $property->id)->where('element_id', $claim->id)->first(); //$pr->setPropertyTitle($property->title); if (empty($pr)) { $pr = new PropertyTypes\NumberProperty($attributes, $property->title); } else { $pr->value = $attributes["value"]; } } else { $pr = PropertyTypes\TextProperty::where('property_id', $property->id)->where('element_id', $claim->id)->first(); //$pr->setPropertyTitle($property->title); if (empty($pr)) { $pr = new PropertyTypes\TextProperty($attributes, $property->title); } else { $pr->value = $attributes["value"]; } } //} $propertyList[] = $pr; } catch (ValidationException $e) { $errors->merge($e->errors()); } } } if ($errors->count() > 0) { return \Redirect::back()->withInput($request)->withErrors($errors); } $claim->update($request); foreach ($propertyList as $pr) { $pr->save(); } return redirect("claim/{$id}"); }