예제 #1
0
파일: Purchase.php 프로젝트: robby-xp/IMS
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getPurchasesDetails()
 {
     return $this->hasMany(PurchaseDetail::className(), ['purchase_id' => 'id']);
 }
예제 #2
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     if (!$request->input('items')) {
         Session()->flash('error_message', 'Purchases Update has not been Completed');
         return redirect('purchases');
     }
     try {
         DB::transaction(function () use($request, $id) {
             $time = time();
             $workspace_id = Auth::user()->workspace_id;
             $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');
             $year = CommonHelper::get_current_financial_year();
             $user_id = Auth::user()->id;
             $old_main_purchase = Purchase::findOrFail($id);
             $purchase = Purchase::findOrFail($id);
             $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->updated_at = time();
             $purchase->updated_by = $user_id;
             $purchase->update();
             //get all old items
             $arrange_old_items = [];
             $old_purchases = PurchaseDetail::where('purchase_id', $id)->get();
             foreach ($old_purchases as $old_purchase) {
                 $arrange_old_items[$old_purchase['material_id']] = $old_purchase;
             }
             foreach ($request->input('items') as $item) {
                 if (isset($arrange_old_items[$item['material_id']])) {
                     // update old data
                     $PurchaseDetail = PurchaseDetail::findOrFail($arrange_old_items[$item['material_id']]['id']);
                     $PurchaseDetail->quantity = $item['quantity'];
                     $PurchaseDetail->received_quantity = $item['received_quantity'];
                     $PurchaseDetail->unit_price = $item['unit_price'];
                     $PurchaseDetail->status = 1;
                     $PurchaseDetail->updated_at = time();
                     $PurchaseDetail->updated_by = $user_id;
                     $PurchaseDetail->update();
                     //update stock info
                     if ($arrange_old_items[$item['material_id']]['received_quantity'] < $item['received_quantity']) {
                         $add_amount = $item['received_quantity'] - $arrange_old_items[$item['material_id']]['received_quantity'];
                         RawStock::where(['material_id' => $item['material_id'], 'year' => $year, 'stock_type' => $balance_type])->increment('quantity', $add_amount, ['updated_at' => $time, 'updated_by' => $user_id]);
                     } elseif ($arrange_old_items[$item['material_id']]['received_quantity'] > $item['received_quantity']) {
                         $sub_amount = $arrange_old_items[$item['material_id']]['received_quantity'] - $item['received_quantity'];
                         RawStock::where(['material_id' => $item['material_id'], 'year' => $year, 'stock_type' => $balance_type])->decrement('quantity', $sub_amount, ['updated_at' => $time, 'updated_by' => $user_id]);
                     }
                     unset($arrange_old_items[$item['material_id']]);
                 } else {
                     //purchase details
                     $item['purchase_id'] = $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]);
                 }
             }
             //delete old data
             foreach ($arrange_old_items as $old_item) {
                 //reduce the stock info
                 RawStock::where(['material_id' => $item['material_id'], 'year' => $year, 'stock_type' => $balance_type])->decrement('quantity', $old_item['received_quantity'], ['updated_at' => $time, 'updated_by' => $user_id]);
                 //update the purchase info
                 $PurchaseDetail = PurchaseDetail::findOrFail($old_item['id']);
                 $PurchaseDetail->delete();
             }
             /*
              * Account management
              *
              */
             $input = $request->input();
             if ($old_main_purchase['transportation_cost'] != $input['transportation_cost']) {
                 if ($old_main_purchase['transportation_cost'] > $input['transportation_cost']) {
                     $deviation = $old_main_purchase['transportation_cost'] - $input['transportation_cost'];
                     // 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();
                     //Update General Journal
                     $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 24000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                     $general_journal->amount -= $deviation;
                     $general_journal->updated_by = $user_id;
                     $general_journal->updated_at = $time;
                     $general_journal->save();
                     $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                     $general_journal->amount -= $deviation;
                     $general_journal->updated_by = $user_id;
                     $general_journal->updated_at = $time;
                     $general_journal->save();
                 } elseif ($old_main_purchase['transportation_cost'] < $input['transportation_cost']) {
                     $deviation = $input['transportation_cost'] - $old_main_purchase['transportation_cost'];
                     // 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();
                     //Update General Journal
                     $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 24000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                     $general_journal->amount += $deviation;
                     $general_journal->updated_by = $user_id;
                     $general_journal->updated_at = $time;
                     $general_journal->save();
                     $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                     $general_journal->amount += $deviation;
                     $general_journal->updated_by = $user_id;
                     $general_journal->updated_at = $time;
                     $general_journal->save();
                 }
             }
             if ($input['paid'] && $old_main_purchase['paid'] < $input['paid']) {
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $new_remain_paid_amount = $input['paid'] - $old_main_purchase['paid'];
                 if ($new_remain_paid_amount > $workspace->balance) {
                     Session()->flash('warning_message', 'Low Balance!! New Purchase paid amount(' . $new_remain_paid_amount . ') is greater than Balance (' . $workspace->balance . ')');
                     throw new \Exception('error');
                 }
             }
             //update the accounting tables
             $workspace_id = Auth::user()->workspace_id;
             $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');
             $user_id = Auth::user()->id;
             $time = time();
             //update general journal for raw material purchase
             $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 25000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
             $general_journal->amount = $input['total'];
             $general_journal->updated_by = $user_id;
             $general_journal->updated_at = $time;
             $general_journal->save();
             //update Workspace for raw material purchase
             $new_total_amount = $input['total'];
             $old_total_amount = $old_main_purchase['total'];
             if ($new_total_amount > $old_total_amount) {
                 $workspace = WorkspaceLedger::where(['account_code' => 25000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_total_amount - $old_total_amount;
                 //add material purchase
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_total_amount < $old_total_amount) {
                 $workspace = WorkspaceLedger::where(['account_code' => 25000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_total_amount - $new_total_amount;
                 //sub material purchase
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             }
             //update cash amount
             $new_paid_amount = $input['paid'];
             $old_paid_amount = $old_main_purchase['paid'];
             //                throw new \Exception($new_paid_amount.'='.$old_paid_amount);
             if (!$new_paid_amount && $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_paid_amount;
                 //sub Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif (!$old_paid_amount && $new_paid_amount) {
                 //Insert data into General Journal
                 $journal = new GeneralJournal();
                 $journal->date = $time;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $id;
                 $journal->year = $year;
                 $journal->account_code = 11000;
                 //Cash
                 $journal->workspace_id = $workspace_id;
                 $journal->amount = $new_paid_amount;
                 $journal->dr_cr_indicator = Config::get('common.debit_credit_indicator.credit');
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $new_paid_amount;
                 //sub Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_paid_amount > $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->amount = $new_paid_amount;
                 $general_journal->updated_by = $user_id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $new_paid_amount - $old_paid_amount;
                 //sub Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             } elseif ($new_paid_amount < $old_paid_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 11000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->amount = $new_paid_amount;
                 $general_journal->updated_by = $user_id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 11000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $old_paid_amount - $new_paid_amount;
                 //add Cash
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
             }
             //DUE management (liabilities)
             $new_due_amount = $input['total'] - $input['paid'];
             $old_due_amount = $old_main_purchase['total'] - $old_main_purchase['paid'];
             if ($new_due_amount && !$old_due_amount) {
                 $journal = new GeneralJournal();
                 $journal->date = $time;
                 $journal->transaction_type = $transaction_type;
                 $journal->reference_id = $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 = $new_due_amount;
                 $journal->created_by = $user_id;
                 $journal->created_at = $time;
                 $journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_due_amount;
                 //add account payable credit
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $input['supplier_id'])->where('person_type', $person_type_supplier)->first();
                 $personal->balance += $new_due_amount;
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($old_due_amount && !$new_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->delete();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_due_amount;
                 //add account payable debit
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $input['supplier_id'])->where('person_type', $person_type_supplier)->first();
                 $personal->balance -= $old_due_amount;
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($new_due_amount > $old_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->amount = $new_due_amount;
                 $general_journal->updated_by = $user_id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance += $new_due_amount - $old_due_amount;
                 //add account payable debit
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $input['supplier_id'])->where('person_type', $person_type_supplier)->first();
                 $personal->balance += $new_due_amount - $old_due_amount;
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             } elseif ($new_due_amount < $old_due_amount) {
                 $general_journal = GeneralJournal::where(['transaction_type' => $transaction_type, 'reference_id' => $id, 'account_code' => 41000, 'year' => $year, 'workspace_id' => $workspace_id])->first();
                 $general_journal->amount = $new_due_amount;
                 $general_journal->updated_by = $user_id;
                 $general_journal->updated_at = $time;
                 $general_journal->save();
                 $workspace = WorkspaceLedger::where(['account_code' => 41000, 'workspace_id' => $workspace_id, 'balance_type' => $balance_type, 'year' => $year])->first();
                 $workspace->balance -= $old_due_amount - $new_due_amount;
                 //add account payable debit
                 $workspace->updated_by = $user_id;
                 $workspace->updated_at = $time;
                 $workspace->save();
                 // Update Personal Account
                 $personal = PersonalAccount::where('person_id', $input['supplier_id'])->where('person_type', $person_type_supplier)->first();
                 $personal->balance -= $old_due_amount - $new_due_amount;
                 $personal->updated_by = $user_id;
                 $personal->updated_at = $time;
                 $personal->save();
             }
         });
     } catch (\Exception $e) {
         Session()->flash('error_message', $e->getMessage());
         //            Session()->flash('error_message','Purchases Update has not been Completed');
         return Redirect::back();
     }
     Session()->flash('flash_message', 'Purchases Update has been Completed');
     return redirect('purchases');
 }
예제 #3
0
 /**
  * Updates an existing Purchase model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $modelDetails = $model->getPurchasesDetails()->all();
     if ($model->load(Yii::$app->request->post())) {
         $oldModelDetails = array();
         foreach ($modelDetails as $modelDetail) {
             $oldModelDetails[$modelDetail->id] = ['item_id' => $modelDetail->item_id, 'quantity' => $modelDetail->quantity];
         }
         $oldIDs = ArrayHelper::map($modelDetails, 'id', 'id');
         $modelDetails = Model::createMultiple(PurchaseDetail::classname(), $modelDetails);
         Model::loadMultiple($modelDetails, Yii::$app->request->post());
         $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelDetails, 'id', 'id')));
         // validate all models
         $valid = $model->validate() & Model::validateMultiple($modelDetails);
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     if (!empty($deletedIDs)) {
                         $flag = PurchaseDetail::deleteAll(['id' => $deletedIDs]) > 0;
                         if ($flag) {
                             foreach ($deletedIDs as $id) {
                                 $item = Item::findOne($oldModelDetails[$id]['item_id']);
                                 $item->stock -= $oldModelDetails[$id]['quantity'];
                                 if (!($flag = $item->save())) {
                                     $transaction->rollBack();
                                     break;
                                 }
                             }
                         } else {
                             $transaction->rollBack();
                         }
                     }
                     if ($flag) {
                         foreach ($modelDetails as $modelDetail) {
                             $quantity = $modelDetail->quantity;
                             if (!empty($modelDetail->id) && $modelDetail->item_id == $oldModelDetails[$modelDetail->id]['item_id']) {
                                 $quantity -= $oldModelDetails[$modelDetail->id]['quantity'];
                             }
                             $modelDetail->purchase_id = $model->id;
                             if (($flag = $modelDetail->save(false)) && $quantity !== 0) {
                                 $item = Item::findOne($modelDetail->item_id);
                                 $item->stock += $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('update', ['model' => $model, 'modelDetails' => empty($modelDetails) ? [new PurchaseDetail()] : $modelDetails]);
 }
예제 #4
0
 public function getAdjustmentAmounts(Request $request)
 {
     $workspace_id = Auth::user()->workspace_id;
     $account = $request->input('account');
     $year_str = strtotime(date('Y'));
     if ($account == 25000) {
         $purchaseDetail = PurchaseDetail::where(['status' => 1], ['created_at', '>', $year_str])->get(['unit_price', 'quantity']);
         $total_amount = 0;
         $total_quantity = 0;
         foreach ($purchaseDetail as $detail) {
             $total_amount += $detail->quantity * $detail->unit_price;
             $total_quantity += $detail->quantity;
         }
         $unit_price = $total_amount / $total_quantity;
         $stocks = RawStock::where('status', 1)->sum('quantity');
         $remaining_amount = $stocks * $unit_price;
         $return = new stdClass();
         $return->total_amount = $total_amount;
         $return->remaining_amount = $remaining_amount;
         return response()->json($return);
     } elseif ($account == 27000) {
         $supply_amount = TransactionRecorder::where(['workspace_id' => $workspace_id, 'account_code' => $account, 'status' => 1, 'year' => date('Y')])->sum('total_amount');
         return response()->json($supply_amount);
     }
 }