/** * * @return true * @author **/ public function handlingnotif() { // Set our server key Veritrans_Config::$serverKey = env('VERITRANS_KEY', 'VT_KEY'); // Uncomment for production environment Veritrans_Config::$isProduction = env('VERITRANS_PRODUCTION', false); $waiting_transaction = Sale::status('payment_process')->get(); foreach ($waiting_transaction as $key => $value) { $notif = new Veritrans_Notification(['transaction_id' => $value['ref_number']]); $transaction = $notif->transaction_status; if ($transaction == 'settlement') { $paid_data = new \App\Models\Payment(); $payment['transaction_id'] = $value['id']; $payment['method'] = $notif->payment_type; $payment['destination'] = 'Veritrans'; $payment['account_name'] = $notif->masked_card; $payment['account_number'] = $notif->approval_code; $payment['ondate'] = \Carbon\Carbon::parse($notif->transaction_time)->format('Y-m-d H:i:s'); $payment['amount'] = $notif->gross_amount; $paid_data = $paid_data->fill($payment); if (!$paid_data->save()) { \Log::error(json_encode($paid_data)); return false; } } } return true; }
/** * 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); }
function countByAgentAndDateGroupByDate($agentId, $fromSql = null, $toSql = null) { $r = Sale::where('user_id', $agentId)->where('sale_status_id', Config::get('constants.sale_status.sale')); if ($fromSql && $toSql) { $r->where('date_sold', '>=', $fromSql)->where('date_sold', '<=', $toSql); } return $r->groupBy('date_sold')->select(['date_sold', DB::raw("count(*) as salesCount")])->orderBy('date_sold')->lists('salesCount', 'date_sold')->toArray(); }
/** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $sales = Sale::wherePaymentId($id)->with(['product' => function ($query) { $query->with('info'); }])->get(); $payment = Payment::whereId($id)->with('place')->first(); return view('client.paymentinfo', compact(['sales', 'payment'])); }
public function show($uid, $pid) { return \App\Models\Sale::query()->with(['product' => function ($q) { $q->select('id', 'title'); }, 'seller' => function ($q) { $q->select('id', 'username'); }])->where('buyer_id', '=', $uid)->find($pid); }
public function getDetail() { //$id = \Input::get('id'); $input = \Input::all(); if ($input['type'] == 'sale') { $detail = Sale::find($input['id']); } else { $detail = Purchase::find($input['id']); } return \Response::json($detail); }
public function editSale() { $input = \Input::all(); $sale = Sale::find($input['id']); $sale->product_id = $input['productId']; $sale->date = $input['date']; $sale->quantity = $input['quantity']; $sale->total_price = $input['totalPrice']; $sale->unit_price = $input['unit_price']; $sale->save(); return \Response::json($sale); }
/** * Run the database seeds. * * @return void */ public function run() { $faker = Faker\Factory::create(); Model::unguard(); //truncate tables: \DB::statement('SET FOREIGN_KEY_CHECKS=0;'); $tablesToReset = ['users', 'password_resets', 'products', 'sales', 'favourites', 'categories', 'product_categories']; foreach ($tablesToReset as $table) { DB::table($table)->truncate(); } \DB::statement('SET FOREIGN_KEY_CHECKS=1;'); // categories: sort($this->categories); foreach ($this->categories as $category) { DB::table('categories')->insert(['name' => $category]); } // users: $users = factory(App\Models\User::class, 8)->create(); // products: $products = collect(); for ($i = 1; $i < 40; $i++) { $seller = $users->random(); $product = factory(App\Models\Product::class)->create(['seller_id' => $seller->id]); $products->push($product); // product categories: $total = count($this->categories); $middleTop = ceil($total / 2); $product->categories()->sync([rand(1, $middleTop - 1), rand($middleTop, $total)]); } // sales: $sales = collect(); for ($i = 1; $i < count($products) * 2; $i++) { $product = $products->random(); $buyer = collect($users)->forget($product->seller_id)->random(); $sale = \App\Models\Sale::create(['buyer_id' => $buyer->id, 'seller_id' => $product->seller_id, 'product_id' => $product->id, 'price' => $price = $product->price, 'quantity' => $quantity = 1, 'total' => $price * $quantity, 'created_at' => $created_at = $faker->dateTimeBetween($product->created_at, 'now'), 'updated_at' => $created_at]); $sales->push($sale); } // favourites: $favourites = collect(); foreach ($users as $user) { $favourites = []; $productsSelected = $products->random(rand(3, 5)); foreach ($productsSelected as $product) { $favourites[$product->id] = ['created_at' => $faker->dateTimeBetween($product->created_at, 'now')]; } $user->favourites()->sync($favourites); } Model::reguard(); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Sale::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'product_id' => $this->product_id, 'day' => $this->day]); $query->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Sale::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10], 'sort' => ['defaultOrder' => ['updated_at' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'date' => $this->date, 'total' => $this->total, 'created_at' => $this->created_at, 'created_by' => $this->created_by, 'updated_at' => $this->updated_at, 'updated_by' => $this->updated_by]); $query->andFilterWhere(['like', 'code', $this->code])->andFilterWhere(['like', 'customer', $this->customer]); return $dataProvider; }
function process($saleId, $statusId) { $response = new ResponseEntity(); try { DB::transaction(function () use(&$response, $saleId, $statusId) { $sale = Sale::find($saleId); if ($sale) { $sp = new SaleProcessed(); $sp->qc_user_id = 1; $sp->sale_status_id = $statusId; $sp->user_id = $sale->user_id; $sp->product_id = $sale->product_id; $sp->customer_id = $sale->customer_id; $sp->date_sold = $sale->date_sold; $sp->order_number = $sale->order_number; $sp->ninety_days = $sale->ninety_days; $sp->remarks = $sale->remarks; $sp->entered_datetime = $sale->created_at; $ok = $sp->save(); if ($ok) { $deleted = Sale::destroy($saleId); if ($deleted) { $response->setSuccess(true); $response->setMessages(['Sale successfully processed!']); } else { $response->setMessages(['Failed to process sale!']); } } else { $response->setMessages(['Failed to process sale!']); } } else { $response->setMessages(['Sale not available']); } }); } catch (\Exception $ex) { $response->setMessages(['Exception: ' . $ex->getMessage()]); } return $response; }
public function index() { $now = new \DateTime('now'); $month = $now->format('m'); $c = Purchase::get(); $c1 = Physicalsale::get(); $var = array($month); $spname = "salesreport"; $c2 = Info::callinfo($var, $spname); $var1 = array($month - 1); $spname1 = "salesreportp"; $c3 = Info::callinfo($var1, $spname1); $spname2 = "todaysales"; $c4 = Combo::callcombo($spname2); $spname3 = "todaycash"; $c5 = Combo::callcombo($spname3); $spname4 = "todaybankcollection"; $c6 = Combo::callcombo($spname4); $spname5 = "todaycashcollection"; $c7 = Combo::callcombo($spname5); $spname6 = "todaycontracollection"; $c8 = Combo::callcombo($spname6); $spbkash = "todaybkashcollection"; $bkash = Combo::callcombo($spbkash); $spsap = "todaysapcollection"; $sap = Combo::callcombo($spsap); $spkcs = "todaykcscollection"; $kcs = Combo::callcombo($spkcs); $spmbank = "todaymbankcollection"; $mbank = Combo::callcombo($spmbank); // print_r($c4); $sales_info = Sale::orderBy('created_at', 'desc')->take(5)->get(); $purchase_info = Purchase::orderBy('created_at', 'desc')->take(5)->get(); $bankaccount_info = Bankaccount::orderBy('created_at', 'desc')->take(5)->get(); return view('home', compact('c', 'c1', 'c2', 'c3', 'sales_info', 'purchase_info', 'bankaccount_info', 'c4', 'c5', 'c6', 'c7', 'c8', 'bkash', 'sap', 'kcs', 'mbank')); }
/** * observe payment event saving * 1. check haven't been paid * 2. recalculate shipping_cost * 3. act, accept or refuse * * @param $model * @return bool */ public function saving($model) { $errors = new MessageBag(); if ($model->package == '') { $model->package = 'regular'; } //1. check haven't been paid if ($model->sale()->count() && !in_array($model->sale->status, ['na', 'cart', 'wait']) && isset($model->getDirty()['address_id'])) { $errors->add('Shipment', 'Tidak dapat mengubah destinasi pengiriman.'); } //2. recalculate shipping_cost if ($model->address()->count()) { $shippingcost = ShippingCost::courierid($model->courier_id)->postalcode($model->address->zipcode)->first(); if ($shippingcost && $model->sale()->count() && $model->sale->transactiondetails()->count()) { $shipping_cost = $model->CountShippingCost($model->sale->transactiondetails, $shippingcost['cost']); $sale = Sale::findorfail($model->transaction_id); $sale->fill(['shipping_cost' => $shipping_cost]); if (!$sale->save()) { $errors->add('Shipment', $sale->getError()); } } else { $errors->add('Shipment', 'Tidak ada kurir ke tempat anda (Silahkan periksa kembali kode pos anda).'); } } elseif ($model->sale()->count()) { $sale = Sale::findorfail($model->transaction_id); $sale->fill(['shipping_cost' => 0]); if (!$sale->save()) { $errors->add('Shipment', $sale->getError()); } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
public function setVerified($saleId) { $response = new ResponseEntity(); try { $sale = Sale::find($saleId); if ($sale) { $sale->verified = 1; $sale->save(); $response->setSuccess(true); $response->setMessage('Sale successfully verified!'); } else { $response->setMessage('Sale not available'); } } catch (\Exception $ex) { $response->setMessages(['Exception: ' . $ex->getMessage()]); } return $response; }
/** * @return \yii\db\ActiveQuery */ public function getSale() { return $this->hasOne(Sale::className(), ['id' => 'sale_id']); }
/** * observe transaction log event saved * 1. Check if transaction is sale * 2. act, accept or refuse * * @param $model * @return bool */ public function saved($model) { $errors = new MessageBag(); //A. Check if transaction is sale if ($model->sale()->count()) { /** * Switch scheme * 1. current status = cart, save audit abandoned * 2. current status = wait, credit point, if full-paid-points, change status to paid * 3. current status = paid, add quota and point for upline, then save audit payment * 4. current status = shipping, save audit shipping * 5. current status = delivered, save audit delivered * 6. current status = canceled, revert point paid, save audit canceled */ switch ($model->status) { case 'cart': $prev_sale = \App\Models\Sale::notid($model->sale_id)->userid($model->sale->user_id)->status('cart')->first(); if ($prev_sale) { $result = $model->ChangeStatus($prev_sale, 'abandoned'); } break; case 'wait': $result = $model->CreditPoint($model->sale); if (!$result && count($model['errors'])) { return false; } if ($result == 0) { $result = $model->ChangeStatus($model->sale, 'paid', 'Full Point', ' + 1 second '); } break; case 'paid': $result = $model->AddQuotaForUpline($model->sale); if (!$result) { return false; } $result = $model->AddPointForUpline($model->sale); break; case 'shipping': break; case 'delivered': break; case 'canceled': $result = $model->RevertPoint($model->sale); break; } if (isset($result) && !$result) { return false; } } if ($errors->count()) { $model['errors'] = $errors; return false; } return true; }
/** * @return \yii\db\ActiveQuery */ public function getSales() { return $this->hasMany(Sale::className(), ['product_id' => 'id']); }
/** * 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); }
/** * Store a sale * * 1. Save Sale * 2. Save Payment or shipment * 3. Save Transaction Log * * @return Response */ public function status() { if (!Input::has('sale')) { return new JSend('error', (array) Input::all(), 'Tidak ada data sale.'); } $errors = new MessageBag(); DB::beginTransaction(); //1. Validate Sale Parameter $sale = Input::get('sale'); if (is_null($sale['id'])) { return new JSend('error', (array) Input::all(), 'Tidak ada data sale.'); } else { $is_new = false; } //1a. Get original data $sale_data = \App\Models\Sale::findorfail($sale['id']); //1b. Check if there were statuses differences if ($sale_data['status'] == $sale['status']) { $errors->add('Sale', 'Tidak ada perubahan status.'); } //2. Check if status = paid if (!$errors->count() && in_array($sale['status'], ['paid'])) { if (!isset($sale['payment']) || is_null($sale['payment'])) { $errors->add('Sale', 'Tidak ada data pembayaran.'); } else { $paid_data = \App\Models\Payment::findornew($sale['payment']['id']); $payment_rule = ['transaction_id' => 'exists:transactions,id|' . (!$paid_data ? '' : 'in:' . $sale_data['id']), 'method' => 'required|max:255', 'destination' => 'required|max:255', 'account_name' => 'required|max:255', 'account_number' => 'required|max:255', 'ondate' => 'required|date_format:"Y-m-d H:i:s"', 'amount' => 'required|numeric|in:' . $sale_data['bills']]; $validator = Validator::make($sale['payment'], $payment_rule); //if there was log and validator false if (!$validator->passes()) { $errors->add('Log', $validator->errors()); } else { $sale['payment']['transaction_id'] = $sale['id']; $paid_data = $paid_data->fill($sale['payment']); if (!$paid_data->save()) { $errors->add('Log', $paid_data->getError()); } } } } //2. Check if status = shipping if (!$errors->count() && in_array($sale['status'], ['shipping'])) { if (is_null($sale['shipment']['receipt_number'])) { $errors->add('Sale', 'Tidak ada nomor resi.'); } else { $shipping_data = \App\Models\Shipment::id($sale['shipment']['id'])->first(); if ($shipping_data) { $shipment_rule = ['receipt_number' => 'required|max:255']; $validator = Validator::make($sale['shipment'], $shipment_rule); //if there was log and validator false if (!$validator->passes()) { $errors->add('Log', $validator->errors()); } else { $sale['shipment']['transaction_id'] = $sale['id']; $shipping_data = $shipping_data->fill($sale['shipment']); if (!$shipping_data->save()) { $errors->add('Log', $shipping_data->getError()); } } } else { $errors->add('Log', 'Shipment tidak valid.'); } } } //3. Check if status = others if (!$errors->count() && !in_array($sale['status'], ['paid', 'shipping'])) { $log_rules = ['transaction_id' => 'exists:transactions,id|' . ($is_new ? '' : 'in:' . $sale_data['id']), 'status' => 'required|max:255|in:cart,wait,payment_process,paid,packed,shipping,delivered,canceled,abandoned']; $validator = Validator::make($sale, $log_rules); //if there was log and validator false if (!$validator->passes()) { $errors->add('Log', $validator->errors()); } else { $log_data = new \App\Models\TransactionLog(); $log_data = $log_data->fill(['status' => $sale['status'], 'transaction_id' => $sale_data['id'], 'notes' => isset($sale['notes']) ? $sale['notes'] : '']); 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_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 new JSend('success', (array) $final_sale); }
/** * Finds the Sale model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Sale the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Sale::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Display a listing of the resource. * * @return Response */ public function index() { $item['sales'] = Sale::all(); return view('sales.index', $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']; } }
public function download_csv_sales(Request $request, $id) { if ($request->ajax()) { $search_value = $request->input('search_value'); if ($search_value == NULL) { $download_csv_data = Factoryitem::sales($id); $item = Item::find($id); return response()->json(array('download_csv_data' => $download_csv_data, 'item' => $item)); } else { $download_csv_data = Factoryitem::where('itemsid', $id)->where(function ($query) use($search_value) { $query->where('slno', 'like', '%' . $search_value . '%')->orWhere('created_at', 'like', '%' . $search_value . '%'); })->where('sale_product', 1)->get(); if ($download_csv_data->count() > 0) { foreach ($download_csv_data as $csv_data) { $sale = Sale::find($csv_data->salesid); $csv_data->invoice_no = $sale->name; } } $item = Item::find($id); return response()->json(array('download_csv_data' => $download_csv_data, 'item' => $item)); } } }