/**
  * @POST("/purchase", as="purchase.store")
  * @param Purchase $purchase
  * @param Request $request
  * @return \Illuminate\Http\RedirectResponse
  */
 public function store(Purchase $purchase, Request $request)
 {
     $data = $request->all();
     if ((int) $data['amount'] <= 0) {
         return redirect()->route('profile.index')->with('message', 'Некорректная сумма вклада!');
     }
     $type1 = TypePurchase::where('id', 1)->first();
     $type2 = TypePurchase::where('id', 2)->first();
     $type3 = TypePurchase::where('id', 3)->first();
     $user = User::where('id', $request->input('user_id'))->first();
     $course = Course::where('currency_id', $request->input('currency_id'))->first();
     $data['course'] = $course->course_purchase;
     if ($request->input('status_trust') === 'true') {
         $data['status_trust'] = 1;
         $purchase->fill($data);
         $purchase->type()->associate($type2);
         $purchase->user()->associate($user);
         $purchase->course()->associate($course);
         $purchase->save();
     } elseif (!$request->input('total')) {
         $data['total'] = $request->input('amount');
         $purchase->fill($data);
         $purchase->type()->associate($type3);
         $purchase->user()->associate($user);
         $purchase->course()->associate($course);
         $purchase->save();
     } else {
         $purchase->fill($data);
         $purchase->type()->associate($type1);
         $purchase->user()->associate($user);
         $purchase->course()->associate($course);
         $purchase->save();
     }
     return redirect()->route('profile.index')->with('message', 'Заявка сделана! Свои оперции Вы можете
     посмотреть в разделе "Мои операции"');
 }
Beispiel #2
0
 /**
  * Creates a new Purchase model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Purchase();
     $modelDetails = null;
     if ($model->load(Yii::$app->request->post())) {
         $modelDetails = Model::createMultiple(PurchaseDetail::classname());
         Model::loadMultiple($modelDetails, Yii::$app->request->post());
         // validate all models
         $valid = $model->validate() & Model::validateMultiple($modelDetails);
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     foreach ($modelDetails as $modelDetail) {
                         $modelDetail->purchase_id = $model->id;
                         if ($flag = $modelDetail->save(false)) {
                             $item = Item::findOne($modelDetail->item_id);
                             $item->stock += $modelDetail->quantity;
                             $flag = $item->save();
                         }
                         if (!$flag) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['index']);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     }
     return $this->render('create', ['model' => $model, 'modelDetails' => empty($modelDetails) ? [new PurchaseDetail()] : $modelDetails]);
 }
 public function register(Request $request)
 {
     // echo $request->input('sub_total');
     // echo $request->input('discount');
     // echo $request->input('others_exp');
     // echo $request->input('gross_total'); die();
     $qnt = $request->input('qnt');
     $rate = $request->input('rate');
     $count_row = 0;
     foreach ($qnt as $key => $value) {
         if ($value != '' && $rate[$key] != '') {
             $count_row++;
         }
     }
     if ($count_row > 0) {
         $u = new Purchase();
         $u->name = $request->input('name');
         if (!empty($request->input('purchase_date'))) {
             $u->purchasedate = CommonController::date_format($request->input('purchase_date'));
         }
         $u->suppliersid = $request->input('supplierid');
         $u->suppliersbillno = $request->input('supplier_bill_no');
         $u->challanno = $request->input('supplier_challan_no');
         if (!empty($request->input('supplier_bill_date'))) {
             $u->suppliersbilldate = CommonController::date_format($request->input('supplier_bill_date'));
         }
         $u->sub_total = $request->input('sub_total');
         $u->discount = $request->input('discount');
         $u->others_exp = $request->input('others_exp');
         $u->gross_total = $request->input('gross_total');
         $u->old_sub_total = $request->input('sub_total');
         $u->old_discount = $request->input('discount');
         $u->old_others_exp = $request->input('others_exp');
         $u->old_gross_total = $request->input('gross_total');
         $u->userid = $request->input('userid');
         $u->save();
         $LastInsertId = $u->id;
         if ($LastInsertId != NULL) {
             //echo $LastInsertId.'<br>';
             $itemid = $request->input('itemid');
             $qnt = $request->input('qnt');
             $measurementid = $request->input('measurementid');
             $rate = $request->input('rate');
             $amount = $request->input('amount');
             $sum = 0;
             foreach ($itemid as $item => $value) {
                 if ($qnt[$item] != '' && $rate[$item] != '') {
                     $u = new Purchasedetails();
                     $u->purchaseid = $LastInsertId;
                     $u->itemid = $value;
                     $u->quantity = $qnt[$item];
                     $u->old_quantity = $qnt[$item];
                     $u->mesid = $measurementid[$item];
                     $u->rate = $rate[$item];
                     $u->old_rate = $rate[$item];
                     $u->amount = $amount[$item];
                     $u->old_amount = $amount[$item];
                     $u->userid = $request->input('userid');
                     $u->save();
                     $sum = $sum + $amount[$item];
                 }
                 // echo 'itemid-'.$value.'quantity-'.$qnt[$item].'measureid-'.$measurementid[$item].'rate-'.$rate[$item].'amount-'.$amount[$item].'<br>';
             }
         }
         //$dis=$sum-$discount;
         $c = new Suppliersledger();
         $c->puv = $LastInsertId;
         $c->sid = $request->input('supplierid');
         $c->amount = $request->input('gross_total');
         $c->save();
         // if (Session::has('invoice_info') && Session::has('total_amount')) {
         Session::forget('invoice_info');
         Session::forget('sub_total');
         Session::forget('discount');
         Session::forget('others_exp');
         Session::forget('gross_total');
         Session::forget('purchase_date');
         Session::forget('supplier');
         Session::forget('supplier_bill_no');
         Session::forget('supplier_bill_date');
         Session::forget('supplier_challan_no');
         // }
     } else {
         return Redirect('purchase/addnew');
     }
     return Redirect('purchase');
 }
Beispiel #4
0
 /**
  * переход по реф ссылке
  * @param type $affiliate_id
  * @param type $url
  */
 public function purchase($affiliate_id, $url)
 {
     if (static::findOne($affiliate_id) === null) {
         $affiliate_id = null;
     }
     $purchase = new Purchase();
     if ($this->id != $affiliate_id) {
         $purchase->affiliate_id = $affiliate_id;
     }
     $purchase->user_id = $this->id;
     $purchase->url_id = $url->id;
     if ($url->shop_id) {
         $purchase->shop_id = $url->shop_id;
     } elseif (isset($url->user->profile)) {
         //$purchase->shop_id = $url->user->profile->user_id;
     }
     $purchase->status = Paylog::STATUS_PENDING;
     $purchase->url = $url->link;
     $purchase->save(false);
     Yii::$app->mailer->compose('user/purchase', ['user' => $this, 'url' => $url])->setFrom(Yii::$app->params['emailFrom'])->setTo($url->user->email)->setSubject('Переход по ссылке')->send();
 }
Beispiel #5
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function store(PurchaseRequest $request)
 {
     if (!$request->input('items')) {
         Session()->flash('error_message', 'Purchases has not been Completed');
         return redirect('purchases');
     }
     try {
         DB::transaction(function () use($request) {
             $user_id = Auth::user()->id;
             $workspace_id = Auth::user()->workspace_id;
             $year = CommonHelper::get_current_financial_year();
             $balance_type = Config::get('common.balance_type_intermediate');
             $transaction_type = Config::get('common.transaction_type.purchase');
             $person_type_supplier = Config::get('common.person_type_supplier');
             $time = time();
             $purchase = new Purchase();
             $purchase->supplier_id = $request->input('supplier_id');
             $purchase->purchase_date = $request->input('purchase_date');
             $purchase->transportation_cost = $request->input('transportation_cost');
             $purchase->paid = $request->input('paid');
             $purchase->total = $request->input('total');
             $purchase->created_at = time();
             $purchase->created_by = $user_id;
             $purchase->save();
             $purchase_id = $purchase->id;
             foreach ($request->input('items') as $item) {
                 //purchase details
                 $item['purchase_id'] = $purchase_id;
                 $item['status'] = 1;
                 $item['created_at'] = time();
                 $item['created_by'] = $user_id;
                 PurchaseDetail::create($item);
                 //update stock info
                 RawStock::where(['material_id' => $item['material_id'], 'year' => $year, 'stock_type' => $balance_type])->increment('quantity', $item['received_quantity'], ['updated_at' => $time, 'updated_by' => $user_id]);
             }
             // Account management
             $input = $request->input();
             if ($input['transportation_cost'] > 0) {
                 // Update Workspace Ledger
                 $workspace = WorkspaceLedger::where(['account_code' => 24000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $input['transportation_cost'];
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $input['transportation_cost'];
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = strtotime($request->input('purchase_return_date'));
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $purchase_id;
                 $journal->year = $year;
                 $journal->account_code = 11000;
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $input['transportation_cost'];
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $journal = new GeneralJournal();
                 $journal->date = strtotime($request->input('purchase_return_date'));
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $purchase_id;
                 $journal->year = $year;
                 $journal->account_code = 24000;
                 //purchase transportation cost
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $input['transportation_cost'];
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
             }
             if ($input['paid']) {
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 if ($input['paid'] > $workspace->balance) {
                     Session()->flash('warning_message', 'Low Balance!! Purchase paid amount(' . $input['paid'] . ') is greater than Balance (' . $workspace->balance . ')');
                     throw new \Exception('error');
                 }
             }
             if ($input['paid'] == $input['total']) {
                 // Update Workspace Ledger
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $input['total'];
                 //add Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 25000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $input['total'];
                 //Add Raw Purchase
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = strtotime($request->input('purchase_date'));
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $purchase_id;
                 $journal->year = $year;
                 $journal->account_code = 11000;
                 //Cash
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $input['total'];
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $journal = new GeneralJournal();
                 $journal->date = strtotime($request->input('purchase_date'));
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $purchase_id;
                 $journal->year = $year;
                 $journal->account_code = 25000;
                 //purchase Return
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $input['total'];
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
             } elseif (!$input['paid']) {
                 // Update Workspace Ledger
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $input['total'];
                 //add Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 25000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $input['total'];
                 //Add Raw Purchase
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = strtotime($request->input('purchase_date'));
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $purchase_id;
                 $journal->year = $year;
                 $journal->account_code = 41000;
                 //Cash
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $input['total'];
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $journal = new GeneralJournal();
                 $journal->date = strtotime($request->input('purchase_date'));
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $purchase_id;
                 $journal->year = $year;
                 $journal->account_code = 25000;
                 //purchase Return
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $input['total'];
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $input['supplier_id'])->where('person_type', $person_type_supplier)->first();
                 $personal->balance += $input['total'];
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($input['paid'] && $input['total']) {
                 $due_amount = $input['total'] - $input['paid'];
                 // Update Workspace Ledger
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $input['paid'];
                 //sub Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $due_amount;
                 //add account payable credit
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 25000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $input['total'];
                 //Add Raw Purchase
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = strtotime($request->input('purchase_date'));
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $purchase_id;
                 $journal->year = $year;
                 $journal->account_code = 11000;
                 //Cash
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $input['paid'];
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $journal = new GeneralJournal();
                 $journal->date = strtotime($request->input('purchase_date'));
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $purchase_id;
                 $journal->year = $year;
                 $journal->account_code = 25000;
                 //Purchase
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $input['total'];
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.debit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $journal = new GeneralJournal();
                 $journal->date = strtotime($request->input('purchase_date'));
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $purchase_id;
                 $journal->year = $year;
                 $journal->account_code = 41000;
                 //Account Payable
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $due_amount;
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $input['supplier_id'])->where('person_type', $person_type_supplier)->first();
                 $personal->balance += $due_amount;
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             }
         });
     } catch (\Exception $e) {
         //            dd($e);
         Session()->flash('error_message', 'Purchases has not been Completed');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Purchases has been Completed');
     return redirect('purchases');
 }