/** * Check shipping cost. * * @param array of model transactiondetails, cost * @return shipping cost */ public function CountShippingCost($transactiondetails, $cost) { $qty = 0; foreach ($transactiondetails as $key => $value) { $qty = $qty + $value['quantity']; } $default = StoreSetting::type('item_for_one_package')->ondate('now')->orderby('created_at', 'desc')->first(); if (!$default) { $max_item = 1; } else { $max_item = $default['value']; } return $cost * ceil($qty / $max_item); }
/** * generate unique number * * @param model of sale * @return unique_number */ public function generateUniqueNumber($sale) { if (!is_null($sale['unique_number'])) { $i = 0; $amount = true; while ($amount) { $prev_number = Sale::orderBy('id', 'DESC')->status('wait')->first(); $limit = StoreSetting::type('limit_unique_number')->ondate('now')->first(); if ($prev_number['unique_number'] < $limit['value']) { $unique_number = $i + $prev_number['unique_number'] + 1; } else { $unique_number = $i + 1; } $amount = Sale::amount($sale->amount - $unique_number)->status('wait')->notid($sale->id)->first(); $i = $i + 1; } return $unique_number; } else { return $sale['unique_number']; } }
/** * Store a setting * * @return Response */ public function store() { if (!Input::has('setting')) { return new JSend('error', (array) Input::all(), 'Tidak ada data setting.'); } $errors = new MessageBag(); DB::beginTransaction(); //1. Validate StoreSetting Parameter $setting = Input::get('setting'); if (is_null($setting['id'])) { $is_new = true; } else { $is_new = false; } //2. Validate setting parameter //2a. Slider if (!$errors->count() && $setting['type'] == 'slider') { $setting_data = \App\Models\Slider::findornew($setting['id']); $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"', 'ended_at' => 'date_format:"Y-m-d H:i:s"|after:started_at']; $validator = Validator::make($setting, $setting_rules); } elseif (!$errors->count() && in_array($setting['type'], ['about_us', 'why_join', 'term_and_condition'])) { $setting_data = \App\Models\StorePage::findornew($setting['id']); $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"']; $validator = Validator::make($setting, $setting_rules); } elseif (!$errors->count() && in_array($setting['type'], ['url', 'logo', 'facebook_url', 'twitter_url', 'instagram_url', 'email', 'phone', 'address', 'bank_information'])) { $setting_data = \App\Models\Store::findornew($setting['id']); $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"']; $validator = Validator::make($setting, $setting_rules); } else { $setting_data = \App\Models\Policy::findornew($setting['id']); $setting_rules = ['started_at' => 'date_format:"Y-m-d H:i:s"']; $validator = Validator::make($setting, $setting_rules); } if (!$validator->passes()) { $errors->add('StoreSetting', $validator->errors()); } else { //if validator passed, save setting $setting_data = $setting_data->fill($setting); if (!$setting_data->save()) { $errors->add('StoreSetting', $setting_data->getError()); } } //3. save image for slider if (!$errors->count() && isset($setting['images']) && is_array($setting['images']) && $setting_data['type'] == 'slider') { $image_current_ids = []; foreach ($setting['images'] as $key => $value) { if (!$errors->count()) { $image_data = \App\Models\Image::findornew($value['id']); $image_rules = ['thumbnail' => 'required|max:255', 'image_xs' => 'required|max:255', 'image_sm' => 'required|max:255', 'image_md' => 'required|max:255', 'image_lg' => 'required|max:255', 'is_default' => 'boolean']; $validator = Validator::make($value, $image_rules); //if there was image and validator false if (!$validator->passes()) { $errors->add('Image', $validator->errors()); } else { $value['imageable_id'] = $setting_data['id']; $value['imageable_type'] = get_class($setting_data); $image_data = $image_data->fill($value); if (!$image_data->save()) { $errors->add('Image', $image_data->getError()); } else { $image_current_ids[] = $image_data['id']; } } } //if there was no error, check if there were things need to be delete if (!$errors->count()) { $images = \App\Models\Image::imageableid($setting['id'])->get(['id'])->toArray(); $image_should_be_ids = []; foreach ($images as $key => $value) { $image_should_be_ids[] = $value['id']; } $difference_image_ids = array_diff($image_should_be_ids, $image_current_ids); if ($difference_image_ids) { foreach ($difference_image_ids as $key => $value) { $image_data = \App\Models\Image::find($value); if (!$image_data->delete()) { $errors->add('Image', $image_data->getError()); } } } } } } if ($errors->count()) { DB::rollback(); return new JSend('error', (array) Input::all(), $errors); } DB::commit(); if ($setting_data['type'] == 'slider') { $final_setting = \App\Models\Slider::id($setting_data['id'])->with(['images'])->first()->toArray(); } else { $final_setting = \App\Models\StoreSetting::id($setting_data['id'])->first()->toArray(); } return new JSend('success', (array) $final_setting); }
/** * observe point log event saved * 1. Check if reference were from user * 2. act, accept or refuse * * @param $model * @return bool */ public function saved($model) { $errors = new MessageBag(); //1. Check if reference were from user if ($model->reference_type == 'App\\Models\\User') { $gift = StoreSetting::type('referral_royalty')->Ondate('now')->first(); //check if no royalti, refuse if (!$gift) { $errors->add('PointLog', 'Tidak ada campaign untuk point reference.'); } else { //give royalti to referral $voucher = Referral::userid($model->reference_id)->first(); if ($voucher && $voucher['value'] == 0) { $referee = new PointLog(); $referee->fill(['user_id' => $model->reference_id, 'amount' => $gift->value, 'expired_at' => $model->expired_at, 'notes' => 'Mereferensikan ' . $model->user->name]); $referee->reference()->associate($model); if (!$referee->save()) { $errors->add('PointLog', $referee->getError()); } } } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * boot * observing model * */ public static function boot() { parent::boot(); Slider::observe(new StoreSettingObserver()); }
/** * save welcome gift * * @param model of user * @return boolean */ public function giveWelcomeGift($user) { $gift = StoreSetting::type('welcome_gift')->Ondate('now')->first(); $store = StoreSetting::type('voucher_point_expired')->Ondate('now')->first(); if ($gift) { if ($store) { $expired_at = new Carbon($store->value); } else { $expired_at = new Carbon('+ 3 months'); } $point = new PointLog(); $point->fill(['user_id' => $user->id, 'amount' => $gift->value, 'expired_at' => $expired_at->format('Y-m-d H:i:s'), 'notes' => 'Welcome Gift dari BALIN']); if (!$point->save()) { $this->errors = $point->getError(); return false; } } return true; }
/** * Display store customer order * * @return Response */ public function store() { if (!Input::has('order')) { return new JSend('error', (array) Input::all(), 'Tidak ada data order.'); } $errors = new MessageBag(); $order = Input::get('order'); DB::beginTransaction(); $user_id = Request::route()[2]['user_id']; $order = Input::get('order'); if (is_null($order['id'])) { $is_new = true; } else { $is_new = false; } if (isset($order['voucher_code'])) { //a. check if voucher code is voucher $voucher = \App\Models\Voucher::code($order['voucher_code'])->type(['free_shipping_cost', 'debit_point'])->first(); if (!$voucher) { //b. check if voucher is referral $voucher_data = \App\Models\Referral::code($order['voucher_code'])->first(); if (!$voucher_data) { $voucher_data = \App\Models\Voucher::code($order['voucher_code'])->type('promo_referral')->ondate('now')->first(); } if (!$voucher_data) { return new JSend('error', (array) Input::all(), ['Voucher tidak valid.']); } elseif ($voucher_data->quota <= 0) { $errors->add('Redeem', 'Quota referral sudah habis.'); } else { $store = \App\Models\StoreSetting::type('voucher_point_expired')->Ondate('now')->first(); if ($store) { $expired_at = new Carbon($store->value); } else { $expired_at = new Carbon('+ 3 months'); } //if validator passed, save voucher if ($voucher_data['type'] == 'referral') { $reference_id = $voucher_data['user_id']; $reference_type = 'App\\Models\\User'; } else { $reference_id = $voucher_data['id']; $reference_type = 'App\\Models\\Voucher'; } $point = ['user_id' => $user_id, 'reference_id' => $reference_id, 'reference_type' => $reference_type, 'expired_at' => $expired_at->format('Y-m-d H:i:s')]; $point_data = new \App\Models\PointLog(); $point_data->fill($point); if (!$point_data->save()) { $errors->add('Redeem', $point_data->getError()); } } } else { $order['voucher_id'] = $voucher['id']; } } if (isset($order['voucher_id']) && $order['voucher_id'] == 0) { $order['voucher_id'] = ''; } $order_rules = ['voucher_id' => 'exists:tmp_vouchers,id']; //1a. Get original data $order_data = \App\Models\Sale::findornew($order['id']); //1b. Validate Basic Order Parameter $validator = Validator::make($order, $order_rules); if (!$validator->passes() && !$errors->count()) { $errors->add('Sale', $validator->errors()); } elseif (!$errors->count()) { //if validator passed, save order $order_data = $order_data->fill(['user_id' => $user_id, 'voucher_id' => isset($order['voucher_id']) ? $order['voucher_id'] : '0']); if (!$order_data->save()) { $errors->add('Sale', $order_data->getError()); } } //2. Validate Order Detail Parameter if (!$errors->count() && isset($order['transactiondetails']) && is_array($order['transactiondetails'])) { $detail_current_ids = []; foreach ($order['transactiondetails'] as $key => $value) { if (!$errors->count() && isset($value['quantity']) && $value['quantity'] > 0) { $detail_data = \App\Models\TransactionDetail::findornew($value['id']); $detail_rules = ['transaction_id' => 'exists:transactions,id|' . ($is_new ? '' : 'in:' . $order_data['id']), 'varian_id' => 'required|exists:varians,id', 'quantity' => 'required|numeric', 'price' => 'required|numeric', 'discount' => 'numeric']; $validator = Validator::make($value, $detail_rules); //if there was detail and validator false if (!$validator->passes()) { $errors->add('Detail', $validator->errors()); } else { $check_prev_trans = \App\Models\TransactionDetail::transactionid($order_data['id'])->varianid($value['varian_id'])->first(); if ($check_prev_trans) { $detail_data = $check_prev_trans; } $value['transaction_id'] = $order_data['id']; $detail_data = $detail_data->fill($value); if (!$detail_data->save()) { $errors->add('Detail', $detail_data->getError()); } else { $detail_current_ids[] = $detail_data['id']; } } } } //if there was no error, check if there were things need to be delete if (!$errors->count()) { $details = \App\Models\TransactionDetail::transactionid($order['id'])->get(['id'])->toArray(); $detail_should_be_ids = []; foreach ($details as $key => $value) { $detail_should_be_ids[] = $value['id']; } $difference_detail_ids = array_diff($detail_should_be_ids, $detail_current_ids); if ($difference_detail_ids) { foreach ($difference_detail_ids as $key => $value) { $detail_data = \App\Models\TransactionDetail::find($value); if (!$detail_data->delete()) { $errors->add('Detail', $detail_data->getError()); } } } } } //3. Validate Order Detail Parameter if (!$errors->count() && isset($order['transactionextensions']) && is_array($order['transactionextensions'])) { $extend_current_ids = []; foreach ($order['transactionextensions'] as $key => $value) { if (!$errors->count()) { $extend_data = \App\Models\TransactionExtension::findornew($value['id']); $extend_rules = ['transaction_id' => 'exists:transactions,id|' . ($is_new ? '' : 'in:' . $order_data['id']), 'product_extension_id' => 'required|exists:product_extensions,id', 'price' => 'required|numeric']; $validator = Validator::make($value, $extend_rules); //if there was extend and validator false if (!$validator->passes()) { $errors->add('Extend', $validator->errors()); } else { $check_prev_trans = \App\Models\TransactionExtension::transactionid($order_data['id'])->productextensionid($value['product_extension_id'])->first(); if ($check_prev_trans) { $extend_data = $check_prev_trans; } $value['transaction_id'] = $order_data['id']; $extend_data = $extend_data->fill($value); if (!$extend_data->save()) { $errors->add('Extend', $extend_data->getError()); } else { $extend_current_ids[] = $extend_data['id']; } } } } //if there was no error, check if there were things need to be delete if (!$errors->count()) { $extends = \App\Models\TransactionExtension::transactionid($order['id'])->get(['id'])->toArray(); $extend_should_be_ids = []; foreach ($extends as $key => $value) { $extend_should_be_ids[] = $value['id']; } $difference_extend_ids = array_diff($extend_should_be_ids, $extend_current_ids); if ($difference_extend_ids) { foreach ($difference_extend_ids as $key => $value) { $extend_data = \App\Models\TransactionExtension::find($value); if (!$extend_data->delete()) { $errors->add('Extend', $extend_data->getError()); } } } } } //4. Check if need to save address if (!$errors->count() && isset($order['shipment']['address'])) { $address_data = \App\Models\Address::findornew($order['shipment']['address']['id']); $address_rules = ['owner_id' => 'exists:users,id|' . ($is_new ? '' : 'in:' . $user_id), 'owner_type' => $is_new ? '' : 'in:App\\Models\\Customer', 'phone' => 'required|max:255', 'address' => 'required', 'zipcode' => 'required|max:255']; $validator = Validator::make($order['shipment']['address'], $address_rules); //4a. save address //if there was address and validator false if (!$validator->passes()) { $errors->add('Sale', $validator->errors()); } else { //if validator passed, save address $order['shipment']['address']['owner_id'] = $user_id; $order['shipment']['address']['owner_type'] = 'App\\Models\\Customer'; $address_data = $address_data->fill($order['shipment']['address']); if (!$address_data->save()) { $errors->add('Sale', $address_data->getError()); } } } //4b. save shipment if (!$errors->count() && isset($order['shipment'])) { if ($order_data->shipment()->count()) { $shipment_data = \App\Models\Shipment::findorfail($order_data->shipment->id); } else { $shipment_data = \App\Models\Shipment::findornew($order['shipment']['id']); } $shipment_rules = ['courier_id' => 'required|exists:couriers,id', 'receiver_name' => 'required|max:255']; $validator = Validator::make($order['shipment'], $shipment_rules); //4c. save shipment //if there was shipment and validator false if (!$validator->passes()) { $errors->add('Sale', $validator->errors()); } else { //if validator passed, save shipment $order['shipment']['transaction_id'] = $order['id']; if (isset($address_data['id'])) { $order['shipment']['address_id'] = $address_data['id']; } $shipment_data = $shipment_data->fill($order['shipment']); if (!$shipment_data->save()) { $errors->add('Sale', $shipment_data->getError()); } } } //5. update status if (!$errors->count() && isset($order['status']) && $order_data['status'] != $order['status']) { //3a. check cart price and product current price if ($order['status'] == 'wait') { foreach ($order_data['transactiondetails'] as $key => $value) { $discount = $value['varian']['product']['promo_price'] == 0 ? 0 : $value['varian']['product']['price'] - $value['varian']['product']['promo_price']; if ($value['price'] != $value['varian']['product']['price'] || $value['discount'] != $discount) { $errors->add('Price', 'Harga item ' . $value['varian']['product']['name'] . ' telah berubah sejak ' . $value['varian']['product']['price_start'] . '. Silahkan update keranjang Anda.'); } } foreach ($order_data['transactionextensions'] as $key => $value) { if ($value['price'] != $value['productextension']['price']) { $errors->add('Price', 'Biaya ' . $value['productextension']['name'] . ' telah berubah sejak ' . $value['productextension']['updated_at'] . '. Silahkan update keranjang Anda.'); } if (!$value['productextension']['is_active']) { $errors->add('Active', $value['productextension']['name'] . ' telah di non aktif kan sejak ' . $value['productextension']['updated_at'] . '. Silahkan update keranjang Anda.'); } } } if (!$errors->count()) { $log_data = new \App\Models\TransactionLog(); $log_data = $log_data->fill(['status' => $order['status'], 'transaction_id' => $order_data['id']]); if (!$log_data->save()) { $errors->add('Log', $log_data->getError()); } } } if ($errors->count()) { DB::rollback(); return new JSend('error', (array) Input::all(), $errors); } DB::commit(); $final_order = \App\Models\Sale::userid($user_id)->id($order_data['id'])->status(['cart', 'wait', 'payment_process', 'canceled', 'paid', 'shipping', 'packed', 'delivered'])->with(['payment', 'orderlogs', 'transactiondetails', 'transactiondetails.varian', 'transactiondetails.varian.product', 'shipment', 'shipment.courier', 'shipment.address', 'voucher', 'user', 'transactionextensions', 'transactionextensions.productextension'])->first()->toArray(); return new JSend('success', (array) $final_order); }
/** * Redeem code * * @return Response */ public function redeem($user_id = null) { if (!Input::has('code')) { return new JSend('error', (array) Input::all(), 'Tidak ada data code.'); } $code = Input::get('code'); $errors = new MessageBag(); DB::beginTransaction(); //1. Check Link $voucher_data = \App\Models\Referral::code($code)->first(); if (!$voucher_data) { $voucher_data = \App\Models\Voucher::code($code)->type('promo_referral')->ondate('now')->first(); } if (!$voucher_data) { $errors->add('Redeem', 'Code tidak valid.'); } elseif ($voucher_data->quota <= 0) { $errors->add('Redeem', 'Quota referral sudah habis.'); } else { $store = StoreSetting::type('voucher_point_expired')->Ondate('now')->first(); if ($store) { $expired_at = new Carbon($store->value); } else { $expired_at = new Carbon('+ 3 months'); } //if validator passed, save voucher if ($voucher_data['type'] == 'referral') { $reference_id = $voucher_data['user_id']; $reference_type = 'App\\Models\\User'; } else { $reference_id = $voucher_data['id']; $reference_type = 'App\\Models\\Voucher'; } $point = ['user_id' => $user_id, 'reference_id' => $reference_id, 'reference_type' => $reference_type, 'expired_at' => $expired_at->format('Y-m-d H:i:s')]; $point_data = new \App\Models\PointLog(); $point_data->fill($point); if (!$point_data->save()) { $errors->add('Redeem', $point_data->getError()); } } if ($errors->count()) { DB::rollback(); return new JSend('error', (array) Input::all(), $errors); } DB::commit(); $final_costumer = \App\Models\Customer::id($user_id)->with(['myreferrals', 'myreferrals.user'])->first()->toArray(); return new JSend('success', (array) $final_costumer); }
/** * boot * observing model * */ public static function boot() { parent::boot(); Policy::observe(new PolicyObserver()); }
/** * Add point for upline. * * @param model transaction * @return boolean */ public function AddPointForUpline($transaction) { $upline = PointLog::userid($transaction->user_id)->referencetype('App\\Models\\User')->first(); $point = StoreSetting::type('downline_purchase_bonus')->Ondate('now')->first(); $expired = StoreSetting::type('downline_purchase_bonus_expired')->Ondate('now')->first(); $whoisupline = 0; if ($upline && $upline->reference()->count() && $upline->reference->referral()->count()) { $whoisupline = $upline->reference->referral->value; } if ($upline && $point && $expired && $whoisupline == 0 && $upline->reference()->count() && $upline->reference->referral()->count()) { $pointlog = new PointLog(); $pointlog->fill(['user_id' => $upline->reference_id, 'reference_id' => $transaction->id, 'reference_type' => get_class($transaction), 'amount' => $point->value, 'expired_at' => date('Y-m-d H:i:s', strtotime($transaction->transact_at . ' ' . $expired->value)), 'notes' => 'Bonus belanja ' . $transaction->user->name]); if (!$pointlog->save()) { $this->errors = $pointlog->getError(); return false; } } return true; }
/** * Register customer * * @return Response */ public function signup() { if (!Input::has('customer')) { return new JSend('error', (array) Input::all(), 'Tidak ada data customer.'); } $customer = Input::get('customer'); $errors = new MessageBag(); DB::beginTransaction(); //1. Validate User Parameter if (is_null($customer['id'])) { $is_new = true; } else { $is_new = false; } $customer_rules = ['name' => 'required|max:255', 'email' => 'required|max:255|unique:users,email,' . (!is_null($customer['id']) ? $customer['id'] : ''), 'password' => 'max:255', 'sso_id' => '', 'sso_media' => 'in:facebook', 'sso_data' => '', 'gender' => 'in:male,female', 'role' => 'required|in:customer', 'date_of_birth' => 'date_format:"Y-m-d H:i:s"']; //1a. Get original data $customer_data = \App\Models\Customer::findornew($customer['id']); //1b. Validate Basic Customer Parameter $validator = Validator::make($customer, $customer_rules); if (!$validator->passes()) { $errors->add('Customer', $validator->errors()); } else { //if validator passed, save customer $customer_data = $customer_data->fill($customer); if (!$customer_data->save()) { $errors->add('Customer', $customer_data->getError()); } } //2. check invitation if (!$errors->count() && isset($customer['reference_code'])) { $referral_data = \App\Models\Referral::code($customer['reference_code'])->first(); if (!$referral_data) { $errors->add('Redeem', 'Link tidak valid. Silahkan mendaftar dengan menu biasa.'); } elseif ($referral_data->quota <= 0) { $errors->add('Redeem', 'Quota referral sudah habis.'); } else { $store = \App\Models\StoreSetting::type('voucher_point_expired')->Ondate('now')->first(); if ($store) { $expired_at = new Carbon($store->value); } else { $expired_at = new Carbon('+ 3 months'); } //if validator passed, save referral $point = ['user_id' => $customer_data['id'], 'reference_id' => $referral_data['user_id'], 'reference_type' => 'App\\Models\\User', 'expired_at' => $expired_at->format('Y-m-d H:i:s')]; $point_data = new \App\Models\PointLog(); $point_data->fill($point); if (!$point_data->save()) { $errors->add('Redeem', $point_data->getError()); } } } if (!$errors->count() && isset($referral_data)) { $invitation = \App\Models\UserInvitationLog::email($customer_data['email'])->userid($referral_data['user_id'])->first(); if ($invitation) { $invitation->is_used = true; if (!$invitation->save()) { $errors->add('Invitation', $invitation->getError()); } } } if ($errors->count()) { DB::rollback(); return new JSend('error', (array) Input::all(), $errors); } DB::commit(); $final_customer = \App\Models\Customer::id($customer_data['id'])->first()->toArray(); return new JSend('success', (array) $final_customer); }
/** * Debit Point from voucher * * @param model transaction * @return voucher discount */ public function DebitPoint($transaction, $debit) { if (!is_null($transaction->id)) { $expired = StoreSetting::type('voucher_point_expired')->Ondate('now')->first(); $previous = PointLog::referenceid($transaction->id)->referencetype('App\\Models\\Transaction')->first(); if ($expired && !$previous) { $point = new PointLog(); $point->fill(['user_id' => $transaction->user_id, 'amount' => $debit, 'expired_at' => date('Y-m-d H:i:s', strtotime($transaction->transact_at . ' ' . $expired->value)), 'notes' => 'Bonus Belanja dengan Voucher ']); $point->reference()->associate($transaction); if (!$point->save()) { $this->errors = $point->getError(); return false; } } } return $result; }