Esempio n. 1
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     $nowDate = new Carbon(date('Y-m-d'));
     $chargeCommission = ChargeCommission::where('waybill_status', false)->whereDate('charge_date', '<', $nowDate)->get();
     //$dateOfFundingDiff = $chargeDate->diffInDays($nowDate,false);
     if (count($chargeCommission) > 0) {
         return Redirect::to('recalculation');
         //return $next($request);
     } else {
         return $next($request);
     }
 }
 public function index(Request $request)
 {
     if ($request->ajax()) {
         $client_id = Input::get('filter-client');
         $debtor_id = Input::get('filter-debtor');
         $registry = Input::get('filter-registry');
         $choice = Input::get('filter-choice');
         $commissions = DailyChargeCommission::query();
         $commissions->where('handler', true);
         if ($client_id != 0) {
             $commissions->whereHas('delivery', function ($q) use($client_id) {
                 $q->where('client_id', '=', $client_id);
             });
         }
         if ($debtor_id != 0) {
             $commissions->whereHas('delivery', function ($q) use($debtor_id) {
                 $q->where('debtor_id', '=', $debtor_id);
             });
         }
         if ($registry != 0) {
             $commissions->whereHas('delivery', function ($q) use($registry) {
                 $q->where('registry', '=', $registry);
             });
         }
         if ($choice != 0) {
             $before = Input::get('filter-before');
             $after = Input::get('filter-after');
             $arratBetween = [$before, $after];
             if ($choice == 1) {
                 $commissions->whereHas('delivery', function ($q) use($arratBetween) {
                     $q->whereBetween('date_of_registry', $arratBetween);
                 });
             } elseif ($choice == 2) {
                 $commissions->whereBetween('created_at', $arratBetween);
             }
         }
         $commissions = $commissions->get();
         return view('reportRepayment.tableRow', ['commissions' => $commissions]);
     } else {
         $clients = Client::Where('active', true)->get();
         $debtors = Debtor::all();
         $debtors = Debtor::all();
         $registries = ChargeCommission::Distinct('registry')->lists('registry');
         return view('reportRepayment.index', ['clients' => $clients, 'debtors' => $debtors, 'registries' => $registries]);
     }
 }
 public function store()
 {
     $financeId = Input::get('finance');
     $nds = 18;
     $dateNow = new Carbon(date('Y-m-d'));
     $finance = Finance::find($financeId);
     $deliveyToFinances = $finance->deliveryToFinance;
     if (count($deliveyToFinances) != 0) {
         foreach ($deliveyToFinances as $deliveyToFinance) {
             $allDailyArray = [];
             $delivery = $deliveyToFinance->delivery;
             $relation = $delivery->relation;
             //связь
             $tariff = $relation->tariff;
             //тарифы
             $commission = new ChargeCommission();
             $commission->client = $delivery->client_id;
             $commission->debtor = $delivery->debtor_id;
             $commission->registry = $delivery->registry;
             $commission->waybill = $delivery->waybill;
             $commission->date_of_waybill = $delivery->date_of_waybill;
             $commission->waybill_status = $delivery->state;
             $commission->date_of_funding = $delivery->date_of_funding;
             $commission->delivery_id = $delivery->id;
             $commission->percent = 0;
             $commission->percent_nds = 0;
             $commission->udz = 0;
             $commission->udz_nds = 0;
             $commission->deferment_penalty = 0;
             $commission->deferment_penalty_nds = 0;
             $commission->fixed_charge = 0;
             $commission->fixed_charge_nds = 0;
             ///---------------------------------------------
             //       //Дней со дня финансирования
             $dateOfFunding = new Carbon($delivery->date_of_funding);
             $dateOfFundingDiff = $dateOfFunding->diffInDays($dateNow, false);
             //        //Фиксированный сбор
             $fixed_charge_w_nds = 0;
             $fixed_charge_nds = 0;
             if ($dateOfFundingDiff > 0) {
                 $fixed_charge_var = $tariff->commissions()->where('type', 'document')->first();
                 if ($fixed_charge_var) {
                     $fixed_charge_w_nds = $fixed_charge_var->commission_value;
                     $commission->fixed_charge = $fixed_charge_w_nds;
                     if ($fixed_charge_var->nds == true) {
                         $fixed_charge_nds = $fixed_charge_w_nds / 100.0 * $nds;
                         $commission->fixed_charge_nds = $fixed_charge_nds;
                     }
                 }
             } else {
                 $commission->fixed_charge = 0;
                 $commission->fixed_charge_nds = 0;
             }
             $percent_w_nds = 0;
             $percent_nds = 0;
             $udz_w_nds = 0;
             $udz_nds = 0;
             $penalty_w_nds = 0;
             $penalty_nds = 0;
             if ($dateOfFundingDiff > 0) {
                 //если сегодняшнее число меньше даты финансирования
                 $dateOfFundingClone = clone $dateOfFunding;
                 $percent_commission = $tariff->commissions()->where('type', 'finance')->first();
                 $udz_commission = $tariff->commissions()->where('type', 'udz')->first();
                 $penalty_commission = $tariff->commissions()->where('type', 'peni')->first();
                 $dateOfRecourse = new Carbon($delivery->date_of_recourse);
                 $dateOfRecourseClone = clone $dateOfRecourse;
                 $dateRecourceFunding = $dateOfRecourse->diffInDays($dateOfFunding, false);
                 if ($dateRecourceFunding > 0) {
                     for ($i = 0; $i < $dateRecourceFunding; $i++) {
                         $dateNowVarFunding = $dateOfRecourseClone->addDays(1);
                         $daysInYear = date("L", mktime(0, 0, 0, 7, 7, $dateNowVarFunding->year)) ? 366 : 365;
                         $actualDeferment = $dateOfRecourse->diffInDays($dateNowVarFunding, false);
                         //Фактическая просрочка
                         if ($penalty_commission) {
                             $dayOrYear = $penalty_commission->rate_stitching;
                             //Нахождение разницы
                             if ($actualDeferment > 0) {
                                 $rage = $penalty_commission->commissionsRages()->where('min', '<=', $actualDeferment)->where(function ($query) use($actualDeferment) {
                                     $query->where('max', '>=', $actualDeferment)->orWhere('max', '=', 0);
                                 })->first();
                                 if ($rage) {
                                     $handle = $penalty_commission->additional_sum;
                                     //проверка накладной и финансирования
                                     if ($handle == true) {
                                         $waybillOrFirstPayment = $delivery->balance_owed;
                                     } else {
                                         $waybillOrFirstPayment = $delivery->remainder_of_the_debt_first_payment;
                                     }
                                     if ($dayOrYear == true) {
                                         $penalty = $rage->value / $daysInYear;
                                     } else {
                                         $penalty = $rage->value;
                                     }
                                     $penalty_w_nds = $commission->deferment_penalty + $waybillOrFirstPayment / 100.0 * $penalty;
                                     //без ндс
                                     $commission->deferment_penalty = $penalty_w_nds;
                                     //Ндс
                                     if ($penalty_commission->nds == true) {
                                         $penalty_nds = $penalty_w_nds / 100.0 * $nds;
                                         $commission->deferment_penalty_nds = $penalty_nds;
                                     }
                                 }
                                 //диапазон
                             }
                             //просрочка меньше нуля
                         }
                     }
                 }
                 for ($i = 0; $i < $dateOfFundingDiff; $i++) {
                     $dailyArray = [];
                     $dateNowVar = $dateOfFundingClone->addDays(1);
                     $dateOfFundingDiffTest = $dateOfFunding->diffInDays($dateNowVar, false);
                     $daysInYear = date("L", mktime(0, 0, 0, 7, 7, $dateNowVar->year)) ? 366 : 365;
                     $actualDeferment = $dateOfRecourse->diffInDays($dateNowVar, false);
                     //Фактическая просрочка
                     if ($i == 0) {
                         $dailyFixed = $fixed_charge_w_nds;
                         $dailyFixedNds = $fixed_charge_nds;
                     } else {
                         $dailyFixed = 0;
                         $dailyFixedNds = 0;
                     }
                     $dailyPercent = 0;
                     $dailyPercentNds = 0;
                     //       	//Процент
                     if ($percent_commission) {
                         //Годовые/дни
                         $handle = $percent_commission->additional_sum;
                         if ($percent_commission->rate_stitching == true) {
                             $percent = $percent_commission->commission_value / $daysInYear;
                         } else {
                             $percent = $percent_commission->commission_value;
                         }
                         //От финансирование либо накладной
                         if ($handle == true) {
                             $dailyPercent = $delivery->balance_owed / 100.0 * $percent;
                             $percent_w_nds = $commission->percent + $dailyPercent;
                             $commission->percent = $percent_w_nds;
                         } else {
                             $dailyPercent = $delivery->remainder_of_the_debt_first_payment / 100.0 * $percent;
                             $percent_w_nds = $commission->percent + $dailyPercent;
                             $commission->percent = $percent_w_nds;
                         }
                         //var_dump($delivery->remainder_of_the_debt_first_payment);
                         if ($percent_commission->nds == true) {
                             $percent_nds = $percent_w_nds / 100.0 * $nds;
                             $commission->percent_nds = $percent_nds;
                             $dailyPercentNds = $dailyPercent / 100.0 * $nds;
                         }
                     }
                     $dailyUdz = 0;
                     $dailyUdzNds = 0;
                     if ($udz_commission) {
                         $dayOrYear = $udz_commission->rate_stitching;
                         //Нахождение разницы
                         $handle = $udz_commission->additional_sum;
                         //проверка накладной и финансирования
                         if ($handle == true) {
                             $waybillOrFirstPayment = $delivery->balance_owed;
                         } else {
                             $waybillOrFirstPayment = $delivery->remainder_of_the_debt_first_payment;
                         }
                         $udz_commission_id = $udz_commission->id;
                         $rage = $udz_commission->commissionsRages()->where('min', '<=', $dateOfFundingDiffTest)->where(function ($query) use($dateOfFundingDiffTest) {
                             $query->where('max', '>=', $dateOfFundingDiffTest)->orWhere('max', '=', 0);
                         })->first();
                         if ($rage) {
                             if ($dayOrYear == true) {
                                 $udz = $rage->value / $daysInYear;
                             } else {
                                 $udz = $rage->value;
                             }
                             $dailyUdz = $waybillOrFirstPayment / 100.0 * $udz;
                             $udz_w_nds = $commission->udz + $dailyUdz;
                             //без ндс
                             $commission->udz = $udz_w_nds;
                             //Ндс
                             if ($udz_commission->nds == true) {
                                 $udz_nds = $udz_w_nds / 100.0 * $nds;
                                 $commission->udz_nds = $udz_nds;
                                 $dailyUdzNds = $dailyUdz / 100.0 * $nds;
                             }
                         }
                     } else {
                         // var_dump('Коммиссии не найдено');
                     }
                     //         //Пеня за просрочку
                     $dailyDeferment = 0;
                     $dailyDefermentNds = 0;
                     if ($penalty_commission) {
                         $dayOrYear = $penalty_commission->rate_stitching;
                         //Нахождение разницы
                         $penalty_commission_id = $penalty_commission->id;
                         if ($actualDeferment > 0) {
                             $rage = $penalty_commission->commissionsRages()->where('min', '<=', $actualDeferment)->where(function ($query) use($actualDeferment) {
                                 $query->where('max', '>=', $actualDeferment)->orWhere('max', '=', 0);
                             })->first();
                             if ($rage) {
                                 $handle = $penalty_commission->additional_sum;
                                 //проверка накладной и финансирования
                                 if ($handle == true) {
                                     $waybillOrFirstPayment = $delivery->balance_owed;
                                 } else {
                                     $waybillOrFirstPayment = $delivery->remainder_of_the_debt_first_payment;
                                 }
                                 if ($dayOrYear == true) {
                                     $penalty = $rage->value / $daysInYear;
                                 } else {
                                     $penalty = $rage->value;
                                 }
                                 $dailyDeferment = $waybillOrFirstPayment / 100.0 * $penalty;
                                 $penalty_w_nds = $commission->deferment_penalty + $dailyDeferment;
                                 if ($i == 0) {
                                     $dailyDeferment = $penalty_w_nds;
                                 }
                                 //без ндс
                                 $commission->deferment_penalty = $penalty_w_nds;
                                 //Ндс
                                 if ($penalty_commission->nds == true) {
                                     $penalty_nds = $penalty_w_nds / 100.0 * $nds;
                                     $commission->deferment_penalty_nds = $penalty_nds;
                                     $dailyDefermentNds = $dailyDeferment / 100.0 * $nds;
                                 }
                             }
                             //диапазон
                         }
                         //просрочка меньше нуля
                     }
                     $daily_without_nds = $dailyFixed + $dailyPercent + $dailyUdz + $dailyDeferment;
                     $daily_nds = $dailyFixedNds + $dailyPercentNds + $dailyUdzNds + $dailyDefermentNds;
                     $daily_with_nds = $daily_without_nds + $daily_nds;
                     $dailyArray = ['dailyFixed' => $dailyFixed, 'dailyFixedNds' => $dailyFixedNds, 'dailyPercent' => $dailyPercent, 'dailyPercentNds' => $dailyPercentNds, 'dailyUdz' => $dailyUdz, 'dailyUdzNds' => $dailyUdzNds, 'dailyDeferment' => $dailyDeferment, 'dailyDefermentNds' => $dailyDefermentNds, 'dailyWithoutNds' => $daily_without_nds, 'dailyNds' => $daily_nds, 'dailyWithNds' => $daily_with_nds, 'dateNow' => $dateNowVar->format('Y-m-d')];
                     array_push($allDailyArray, $dailyArray);
                 }
                 //цикл
             } else {
                 $commission->percent = 0;
                 $commission->percent_nds = 0;
                 $commission->udz = 0;
                 $commission->udz_nds = 0;
                 $commission->deferment_penalty = 0;
                 $commission->deferment_penalty_nds = 0;
             }
             //       //без НДС
             $without_nds = $fixed_charge_w_nds + $percent_w_nds + $udz_w_nds + $penalty_w_nds;
             $commission->without_nds = $without_nds;
             //НДС
             $nds_amount = $fixed_charge_nds + $percent_nds + $udz_nds + $penalty_nds;
             $commission->nds = $nds_amount;
             //с НДС
             $commission_sum = $without_nds + $nds_amount;
             $commission->with_nds = $commission_sum;
             //Долг по коммиссии
             $commission->debt = $commission_sum;
             $commission->charge_date = $dateNow;
             if ($commission->save()) {
                 foreach ($allDailyArray as $array) {
                     $this->createDailyCharge($array, $commission->id, $delivery->id);
                 }
             }
             $delivery->stop_commission = false;
             $delivery->save();
         }
     }
 }