Esempio n. 1
0
 public function store()
 {
     $rules = array('year' => 'required', 'month' => 'required');
     $validator = Validator::make(Input::all(), $rules);
     // process the login
     if ($validator->fails()) {
         return Redirect::to('invoicing')->withErrors($validator);
     } else {
         $clients = Client::All();
         $bill_date = Input::get('year') . '-' . Input::get('month') . '-' . cal_days_in_month(CAL_GREGORIAN, Input::get('month'), Input::get('year'));
         $bill_date_first_day = Carbon::createFromDate(Input::get('year'), Input::get('month'), 1);
         $bill_date_last_day = clone $bill_date_first_day;
         $bill_date_last_day = $bill_date_last_day->addMonth();
         foreach ($clients as $client) {
             foreach ($client->agreements as $agreement) {
                 $nds = 0;
                 $commission_sum = 0;
                 $without_nds = 0;
                 $with_nds = 0;
                 $debt = 0;
                 $pred_with_nds = 0;
                 $with_nds_delivery = 0;
                 $repayments = 0;
                 foreach ($agreement->relations as $relation) {
                     if ($agreement->account == FALSE) {
                         foreach ($relation->deliveries as $delivery) {
                             if ($delivery->status == 'Профинансирована') {
                                 $pred_with_nds = $delivery->dailyChargeCommission()->where('handler', false)->whereDate('created_at', '<', $bill_date_first_day)->sum('with_nds');
                                 $repayments = $delivery->dailyChargeCommission()->where('handler', true)->whereDate('created_at', '<', $bill_date_last_day)->sum('with_nds');
                                 $with_nds_delivery = $delivery->dailyChargeCommission()->where('handler', false)->whereYear('created_at', '=', Input::get('year'))->whereMonth('created_at', '=', Input::get('month'))->sum('with_nds');
                                 if ($repayments > $pred_with_nds) {
                                     if ($repayments >= $with_nds_delivery + $pred_with_nds) {
                                         $debt += 0;
                                     } else {
                                         $debt += $with_nds_delivery - ($repayments - $pred_with_nds);
                                     }
                                 } else {
                                     $debt += $with_nds_delivery;
                                 }
                                 $with_nds += $with_nds_delivery;
                                 $nds += $delivery->dailyChargeCommission()->where('handler', false)->whereYear('created_at', '=', Input::get('year'))->whereMonth('created_at', '=', Input::get('month'))->sum('nds');
                                 $without_nds += $delivery->dailyChargeCommission()->where('handler', false)->whereYear('created_at', '=', Input::get('year'))->whereMonth('created_at', '=', Input::get('month'))->sum('without_nds');
                                 $bill_date = Input::get('year') . '-' . Input::get('month') . '-' . cal_days_in_month(CAL_GREGORIAN, Input::get('month'), Input::get('year'));
                             }
                         }
                     } else {
                         foreach ($relation->deliveries as $delivery) {
                             if ($delivery->date_of_payment != NULL and $delivery->date_of_payment->year == Input::get('year') and $delivery->date_of_payment->month == Input::get('month') and $delivery->status == 'Профинансирована') {
                                 foreach ($delivery->dailyChargeCommission->where('handler', false) as $commission) {
                                     $nds += $delivery->dailyChargeCommission()->where('handler', false)->sum('nds');
                                     $without_nds += $delivery->dailyChargeCommission()->where('handler', false)->sum('without_nds');
                                     $with_nds += $delivery->dailyChargeCommission()->where('handler', false)->sum('with_nds');
                                     $bill_date = $delivery->date_of_payment;
                                     $debt = 0;
                                 }
                             }
                         }
                     }
                 }
                 if ($with_nds != 0) {
                     $bill = new Bill();
                     $bill->bill_date = $bill_date;
                     $bill->agreement_id = $agreement->id;
                     $bill->nds = $nds;
                     $bill->with_nds = $with_nds;
                     $bill->without_nds = $without_nds;
                     $bill->client_id = $client->id;
                     $bill->debt = $debt;
                     $bill->save();
                 }
             }
         }
     }
 }