[text] general helper

Viewer

copydownloadembedprintName: general helper
  1. <?php
  2.  
  3. /**
  4.  * Created by PhpStorm.
  5.  * User: Tj
  6.  * Date: 6/29/2016
  7.  * Time: 3:11 PM
  8.  */
  9. namespace App\Helpers;
  10.  
  11. use App\Models\Asset;
  12. use App\Models\AssetValuation;
  13. use App\Models\AuditTrail;
  14. use App\Models\Capital;
  15. use App\Models\Expense;
  16. use App\Models\Loan;
  17. use App\Models\LoanRepayment;
  18. use App\Models\LoanSchedule;
  19. use App\Models\OtherIncome;
  20. use App\Models\Payroll;
  21. use App\Models\PayrollMeta;
  22. use App\Models\Saving;
  23. use App\Models\SavingTransaction;
  24. use App\Models\Setting;
  25. use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
  26.  
  27. class GeneralHelper
  28. {
  29.     /*
  30.      * determine interest
  31.      */
  32.     public static function determine_interest_rate($id)
  33.     {
  34.         $loan = Loan::find($id);
  35.         $interest = '';
  36.         if ($loan->override_interest == 1) {
  37.             $interest = $loan->override_interest_amount;
  38.         } else {
  39.             if ($loan->repayment_cycle == 'annually') {
  40.                 //return the interest per year
  41.                 if ($loan->interest_period == 'year') {
  42.                     $interest = $loan->interest_rate;
  43.                 }
  44.                 if ($loan->interest_period == 'month') {
  45.                     $interest = $loan->interest_rate * 12;
  46.                 }
  47.                 if ($loan->interest_period == 'week') {
  48.                     $interest = $loan->interest_rate * 52;
  49.                 }
  50.                 if ($loan->interest_period == 'day') {
  51.                     $interest = $loan->interest_rate * 365;
  52.                 }
  53.             }
  54.             if ($loan->repayment_cycle == 'semi_annually') {
  55.                 //return the interest per semi annually
  56.                 if ($loan->interest_period == 'year') {
  57.                     $interest = $loan->interest_rate / 2;
  58.                 }
  59.                 if ($loan->interest_period == 'month') {
  60.                     $interest = $loan->interest_rate * 6;
  61.                 }
  62.                 if ($loan->interest_period == 'week') {
  63.                     $interest = $loan->interest_rate * 26;
  64.                 }
  65.                 if ($loan->interest_period == 'day') {
  66.                     $interest = $loan->interest_rate * 182.5;
  67.                 }
  68.             }
  69.             if ($loan->repayment_cycle == 'quarterly') {
  70.                 //return the interest per quaterly
  71.  
  72.                 if ($loan->interest_period == 'year') {
  73.                     $interest = $loan->interest_rate / 4;
  74.                 }
  75.                 if ($loan->interest_period == 'month') {
  76.                     $interest = $loan->interest_rate * 3;
  77.                 }
  78.                 if ($loan->interest_period == 'week') {
  79.                     $interest = $loan->interest_rate * 13;
  80.                 }
  81.                 if ($loan->interest_period == 'day') {
  82.                     $interest = $loan->interest_rate * 91.25;
  83.                 }
  84.             }
  85.             if ($loan->repayment_cycle == 'bi_monthly') {
  86.                 //return the interest per bi-monthly
  87.                 if ($loan->interest_period == 'year') {
  88.                     $interest = $loan->interest_rate / 6;
  89.                 }
  90.                 if ($loan->interest_period == 'month') {
  91.                     $interest = $loan->interest_rate * 2;
  92.                 }
  93.                 if ($loan->interest_period == 'week') {
  94.                     $interest = $loan->interest_rate * 8.67;
  95.                 }
  96.                 if ($loan->interest_period == 'day') {
  97.                     $interest = $loan->interest_rate * 58.67;
  98.                 }
  99.  
  100.             }
  101.  
  102.             if ($loan->repayment_cycle == 'monthly') {
  103.                 //return the interest per monthly
  104.  
  105.                 if ($loan->interest_period == 'year') {
  106.                     $interest = $loan->interest_rate / 12;
  107.                 }
  108.                 if ($loan->interest_period == 'month') {
  109.                     $interest = $loan->interest_rate * 1;
  110.                 }
  111.                 if ($loan->interest_period == 'week') {
  112.                     $interest = $loan->interest_rate * 4.33;
  113.                 }
  114.                 if ($loan->interest_period == 'day') {
  115.                     $interest = $loan->interest_rate * 30.4;
  116.                 }
  117.             }
  118.             if ($loan->repayment_cycle == 'weekly') {
  119.                 //return the interest per weekly
  120.  
  121.                 if ($loan->interest_period == 'year') {
  122.                     $interest = $loan->interest_rate / 52;
  123.                 }
  124.                 if ($loan->interest_period == 'month') {
  125.                     $interest = $loan->interest_rate / 4;
  126.                 }
  127.                 if ($loan->interest_period == 'week') {
  128.                     $interest = $loan->interest_rate;
  129.                 }
  130.                 if ($loan->interest_period == 'day') {
  131.                     $interest = $loan->interest_rate * 7;
  132.                 }
  133.             }
  134.             if ($loan->repayment_cycle == 'daily') {
  135.                 //return the interest per day
  136.  
  137.                 if ($loan->interest_period == 'year') {
  138.                     $interest = $loan->interest_rate / 365;
  139.                 }
  140.                 if ($loan->interest_period == 'month') {
  141.                     $interest = $loan->interest_rate / 30.4;
  142.                 }
  143.                 if ($loan->interest_period == 'week') {
  144.                     $interest = $loan->interest_rate / 7.02;
  145.                 }
  146.                 if ($loan->interest_period == 'day') {
  147.                     $interest = $loan->interest_rate * 1;
  148.                 }
  149.             }
  150.         }
  151.         return $interest / 100;
  152.     }
  153.  
  154. //determine monthly payment using amortization
  155.     public static function amortized_monthly_payment($id, $balance)
  156.     {
  157.         $loan = Loan::find($id);
  158.         $period = GeneralHelper::loan_period($id);
  159.         $interest_rate = GeneralHelper::determine_interest_rate($id);
  160.         //calculate here
  161.         $amount = ($interest_rate * $balance * pow((1 + $interest_rate), $period)) / (pow((1 + $interest_rate),
  162.                     $period) - 1);
  163.         return $amount;
  164.     }
  165.  
  166.     public static function loan_period($id)
  167.     {
  168.         $loan = Loan::find($id);
  169.         $period = 0;
  170.         if ($loan->repayment_cycle == 'annually') {
  171.             if ($loan->loan_duration_type == 'year') {
  172.                 $period = ceil($loan->loan_duration);
  173.             }
  174.             if ($loan->loan_duration_type == 'month') {
  175.                 $period = ceil($loan->loan_duration * 12);
  176.             }
  177.             if ($loan->loan_duration_type == 'week') {
  178.                 $period = ceil($loan->loan_duration * 52);
  179.             }
  180.             if ($loan->loan_duration_type == 'day') {
  181.                 $period = ceil($loan->loan_duration * 365);
  182.             }
  183.         }
  184.         if ($loan->repayment_cycle == 'semi_annually') {
  185.             if ($loan->loan_duration_type == 'year') {
  186.                 $period = ceil($loan->loan_duration * 2);
  187.             }
  188.             if ($loan->loan_duration_type == 'month') {
  189.                 $period = ceil($loan->loan_duration * 6);
  190.             }
  191.             if ($loan->loan_duration_type == 'week') {
  192.                 $period = ceil($loan->loan_duration * 26);
  193.             }
  194.             if ($loan->loan_duration_type == 'day') {
  195.                 $period = ceil($loan->loan_duration * 182.5);
  196.             }
  197.         }
  198.         if ($loan->repayment_cycle == 'quarterly') {
  199.             if ($loan->loan_duration_type == 'year') {
  200.                 $period = ceil($loan->loan_duration);
  201.             }
  202.             if ($loan->loan_duration_type == 'month') {
  203.                 $period = ceil($loan->loan_duration * 12);
  204.             }
  205.             if ($loan->loan_duration_type == 'week') {
  206.                 $period = ceil($loan->loan_duration * 52);
  207.             }
  208.             if ($loan->loan_duration_type == 'day') {
  209.                 $period = ceil($loan->loan_duration * 365);
  210.             }
  211.         }
  212.         if ($loan->repayment_cycle == 'bi_monthly') {
  213.             if ($loan->loan_duration_type == 'year') {
  214.                 $period = ceil($loan->loan_duration*6);
  215.             }
  216.             if ($loan->loan_duration_type == 'month') {
  217.                 $period = ceil($loan->loan_duration * 2);
  218.             }
  219.             if ($loan->loan_duration_type == 'week') {
  220.                 $period = ceil($loan->loan_duration * 8);
  221.             }
  222.             if ($loan->loan_duration_type == 'day') {
  223.                 $period = ceil($loan->loan_duration * 60);
  224.             }
  225.         }
  226.  
  227.         if ($loan->repayment_cycle == 'monthly') {
  228.             if ($loan->loan_duration_type == 'year') {
  229.                 $period = ceil($loan->loan_duration * 12);
  230.             }
  231.             if ($loan->loan_duration_type == 'month') {
  232.                 $period = ceil($loan->loan_duration);
  233.             }
  234.             if ($loan->loan_duration_type == 'week') {
  235.                 $period = ceil($loan->loan_duration * 4.3);
  236.             }
  237.             if ($loan->loan_duration_type == 'day') {
  238.                 $period = ceil($loan->loan_duration * 30.4);
  239.             }
  240.         }
  241.         if ($loan->repayment_cycle == 'weekly') {
  242.             if ($loan->loan_duration_type == 'year') {
  243.                 $period = ceil($loan->loan_duration * 52);
  244.             }
  245.             if ($loan->loan_duration_type == 'month') {
  246.                 $period = ceil($loan->loan_duration * 4);
  247.             }
  248.             if ($loan->loan_duration_type == 'week') {
  249.                 $period = ceil($loan->loan_duration * 1);
  250.             }
  251.             if ($loan->loan_duration_type == 'day') {
  252.                 $period = ceil($loan->loan_duration * 7);
  253.             }
  254.         }
  255.         if ($loan->repayment_cycle == 'daily') {
  256.             if ($loan->loan_duration_type == 'year') {
  257.                 $period = ceil($loan->loan_duration * 365);
  258.             }
  259.             if ($loan->loan_duration_type == 'month') {
  260.                 $period = ceil($loan->loan_duration * 30.42);
  261.             }
  262.             if ($loan->loan_duration_type == 'week') {
  263.                 $period = ceil($loan->loan_duration * 7.02);
  264.             }
  265.             if ($loan->loan_duration_type == 'day') {
  266.                 $period = ceil($loan->loan_duration);
  267.             }
  268.         }
  269.         return $period;
  270.     }
  271.  
  272.     public static function time_ago($eventTime)
  273.     {
  274.         $totaldelay = time() - strtotime($eventTime);
  275.         if ($totaldelay <= 0) {
  276.             return '';
  277.         } else {
  278.             if ($days = floor($totaldelay / 86400)) {
  279.                 $totaldelay = $totaldelay % 86400;
  280.                 return $days . ' days ago';
  281.             }
  282.             if ($hours = floor($totaldelay / 3600)) {
  283.                 $totaldelay = $totaldelay % 3600;
  284.                 return $hours . ' hours ago';
  285.             }
  286.             if ($minutes = floor($totaldelay / 60)) {
  287.                 $totaldelay = $totaldelay % 60;
  288.                 return $minutes . ' minutes ago';
  289.             }
  290.             if ($seconds = floor($totaldelay / 1)) {
  291.                 $totaldelay = $totaldelay % 1;
  292.                 return $seconds . ' seconds ago';
  293.             }
  294.         }
  295.     }
  296.  
  297.     public static function determine_due_date($id, $date)
  298.     {
  299.         $schedule = LoanSchedule::where('due_date', ' >=', $date)->where('loan_id', $id)->orderBy('due_date',
  300.             'asc')->first();
  301.         if (!empty($schedule)) {
  302.             return $schedule->due_date;
  303.         } else {
  304.             $schedule = LoanSchedule::where('loan_id',
  305.                 $id)->orderBy('due_date',
  306.                 'desc')->first();
  307.             if ($date > $schedule->due_date) {
  308.                 return $schedule->due_date;
  309.             } else {
  310.                 $schedule = LoanSchedule::where('due_date', '>', $date)->where('loan_id',
  311.                     $id)->orderBy('due_date',
  312.                     'asc')->first();
  313.                 return $schedule->due_date;
  314.             }
  315.  
  316.         }
  317.     }
  318.  
  319.     public static function loan_total_interest($id, $date = '')
  320.     {
  321.         if (empty($date)) {
  322.             return LoanSchedule::where('loan_id', $id)->sum('interest');
  323.         } else {
  324.             return LoanSchedule::where('loan_id', $id)->where('due_date', '<=', $date)->sum('interest');
  325.         }
  326.     }
  327.  
  328.     public static function loan_total_principal($id, $date = '')
  329.     {
  330.         if (empty($date)) {
  331.             return LoanSchedule::where('loan_id', $id)->sum('principal');
  332.         } else {
  333.             return LoanSchedule::where('loan_id', $id)->where('due_date', '<=', $date)->sum('principal');
  334.         }
  335.     }
  336.  
  337.     public static function loan_total_fees($id, $date = '')
  338.     {
  339.         if (empty($date)) {
  340.             return LoanSchedule::where('loan_id', $id)->sum('fees');
  341.         } else {
  342.             return LoanSchedule::where('loan_id', $id)->where('due_date', '<=', $date)->sum('fees');
  343.         }
  344.     }
  345.  
  346.     public static function loan_total_penalty($id, $date = '')
  347.     {
  348.         if (empty($date)) {
  349.             return LoanSchedule::where('loan_id', $id)->sum('penalty');
  350.         } else {
  351.             return LoanSchedule::where('loan_id', $id)->where('due_date', '<=', $date)->sum('penalty');
  352.         }
  353.     }
  354.  
  355.     public static function loan_total_paid($id, $date = '')
  356.     {
  357.         if (empty($date)) {
  358.             return LoanRepayment::where('loan_id', $id)->sum('amount');
  359.         } else {
  360.             return LoanRepayment::where('loan_id', $id)->where('due_date', '<=', $date)->sum('amount');
  361.         }
  362.  
  363.     }
  364.  
  365.     public static function loan_total_balance($id, $date = '')
  366.     {
  367.         $loan = Loan::find($id);
  368.         if (empty($date)) {
  369.             if ($loan->override == 1) {
  370.                 return $loan->balance - GeneralHelper::loan_total_paid($id);
  371.             } else {
  372.                 return GeneralHelper::loan_total_due_amount($id) - GeneralHelper::loan_total_paid($id);
  373.             }
  374.  
  375.         } else {
  376.             return GeneralHelper::loan_total_due_amount($id, $date) - GeneralHelper::loan_total_paid($id, $date);
  377.         }
  378.  
  379.     }
  380.  
  381.     public static function loan_total_due_amount($id, $date = '')
  382.     {
  383.         if (empty($date)) {
  384.             return (GeneralHelper::loan_total_penalty($id) + GeneralHelper::loan_total_fees($id) + GeneralHelper::loan_total_interest($id) + GeneralHelper::loan_total_principal($id));
  385.         } else {
  386.             return (GeneralHelper::loan_total_penalty($id, $date) + GeneralHelper::loan_total_fees($id,
  387.                     $date) + GeneralHelper::loan_total_interest($id, $date) + GeneralHelper::loan_total_principal($id,
  388.                     $date));
  389.  
  390.         }
  391.  
  392.     }
  393.  
  394.     public static function loan_total_due_period($id, $date)
  395.     {
  396.         return (LoanSchedule::where('loan_id', $id)->where('due_date',
  397.                 $date)->sum('penalty') + LoanSchedule::where('loan_id', $id)->where('due_date',
  398.                 $date)->sum('fees') + LoanSchedule::where('loan_id', $id)->where('due_date',
  399.                 $date)->sum('principal') + LoanSchedule::where('loan_id', $id)->where('due_date',
  400.                 $date)->sum('interest'));
  401.  
  402.     }
  403.  
  404.     public static function loan_total_paid_period($id, $date)
  405.     {
  406.         return LoanRepayment::where('loan_id', $id)->where('due_date', $date)->sum('amount');
  407.  
  408.     }
  409.  
  410.     public static function loans_total_paid($start_date = '', $end_date = '')
  411.     {
  412.  
  413.         if (empty($start_date)) {
  414.             $paid = 0;
  415.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'disbursed')->get()->chunk(100000) as $key) {
  416.                 $paid = $paid + LoanRepayment::where('loan_id', $key->id)->sum('amount');
  417.             }
  418.             return $paid;
  419.         } else {
  420.             $paid = 0;
  421.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'disbursed')->whereBetween('release_date',
  422.                                                                                                                 [$start_date, $end_date])->get() as $key) {
  423.                 $paid = $paid + LoanRepayment::where('loan_id', $key->id)->sum('amount');
  424.             }
  425.             return $paid;
  426.  
  427.         }
  428.  
  429.     }
  430.  
  431.  
  432.     public static function addMonths($date, $months)
  433.     {
  434.         $orig_day = $date->format("d");
  435.         $date->modify("+" . $months . " months");
  436.         while ($date->format("d") < $orig_day && $date->format("d") < 5) {
  437.             $date->modify("-1 day");
  438.         }
  439.     }
  440.  
  441.     //determine paid principal
  442.     public static function loan_paid_item($id, $item = 'principal', $date = '')
  443.     {
  444.         
  445.  
  446.  
  447.         $loan = Loan::find($id);
  448.  
  449.  
  450.         $principal = 0;
  451.         $interest = 0;
  452.         $penalty = 0;
  453.         $fees = 0;
  454.         if (empty($date)) {
  455.             $schedules = $loan->schedules;
  456.         } else {
  457.             $schedules = LoanSchedule::where('loan_id', $id)->wheredueDate('due_date', '<=', $date)->get();
  458.         }
  459.  
  460.         $repayment_order = unserialize($loan->loan_product->repayment_order);
  461.         foreach ($schedules as $schedule) {
  462.             $payments = LoanRepayment::where('loan_id', $id)->where('due_date', $schedule->due_date)->sum('amount');
  463.             if ($payments > 0) {
  464.                 foreach ($repayment_order as $order) {
  465.                     if ($payments > 0) {
  466.                         if ($order == 'interest') {
  467.                             if ($payments > $schedule->interest) {
  468.                                 $interest = $interest + $schedule->interest;
  469.                                 $payments = $payments - $schedule->interest;
  470.                             } else {
  471.                                 $interest = $interest + $payments;
  472.                                 $payments = 0;
  473.                             }
  474.                         }
  475.                         if ($order == 'penalty') {
  476.                             if ($payments > $schedule->penalty) {
  477.                                 $penalty = $penalty + $schedule->penalty;
  478.                                 $payments = $payments - $schedule->penalty;
  479.                             } else {
  480.                                 $penalty = $penalty + $payments;
  481.                                 $payments = 0;
  482.                             }
  483.                         }
  484.                         if ($order == 'fees') {
  485.                             if ($payments > $schedule->fees) {
  486.                                 $fees = $fees + $schedule->fees;
  487.                                 $payments = $payments - $schedule->fees;
  488.                             } else {
  489.                                 $fees = $fees + $payments;
  490.                                 $payments = 0;
  491.                             }
  492.                         }
  493.                         if ($order == 'principal') {
  494.                             if ($payments > $schedule->principal) {
  495.                                 $principal = $principal + $schedule->principal;
  496.                                 $payments = $payments - $schedule->principal;
  497.                             } else {
  498.                                 $principal = $principal + $payments;
  499.                                 $payments = 0;
  500.                             }
  501.                         }
  502.                     }
  503.                 }
  504.             }
  505.             //apply remainder to principal
  506.             $principal = $principal + $payments;
  507.         }
  508.         if ($item == 'principal') {
  509.             return $principal;
  510.         }
  511.         if ($item == 'fees') {
  512.             return $fees;
  513.         }
  514.         if ($item == 'penalty') {
  515.             return $penalty;
  516.         }
  517.         if ($item == 'interest') {
  518.             return $interest;
  519.         }
  520.         return $principal;
  521.     }
  522.  
  523.     public static function single_payroll_total_pay($id)
  524.     {
  525.         return PayrollMeta::where('payroll_id', $id)->where('position', 'bottom_left')->sum('value');
  526.     }
  527.  
  528.     public static function single_payroll_total_deductions($id)
  529.     {
  530.         return PayrollMeta::where('payroll_id', $id)->where('position', 'bottom_right')->sum('value');
  531.     }
  532.  
  533.     public static function total_expenses($start_date = '', $end_date = '')
  534.     {
  535.         if (empty($start_date)) {
  536.             return Expense::where('branch_id', session('branch_id'))->sum('amount');
  537.         } else {
  538.             return Expense::where('branch_id', session('branch_id'))->whereBetween('date', [$start_date, $end_date])->sum('amount');
  539.  
  540.         }
  541.  
  542.     }
  543.  
  544.     public static function total_payroll($start_date = '', $end_date = '')
  545.     {
  546.         if (empty($start_date)) {
  547.             $payroll = 0;
  548.             foreach (Payroll::where('branch_id', session('branch_id'))->get() as $key) {
  549.                 $payroll = $payroll + GeneralHelper::single_payroll_total_pay($key->id);
  550.             }
  551.             return $payroll;
  552.         } else {
  553.             $payroll = 0;
  554.             foreach (Payroll::where('branch_id', session('branch_id'))->whereBetween('date', [$start_date, $end_date])->get() as $key) {
  555.                 $payroll = $payroll + GeneralHelper::single_payroll_total_pay($key->id);
  556.             }
  557.             return $payroll;
  558.  
  559.         }
  560.  
  561.     }
  562.  
  563.     public static function loans_total_principal($start_date = '', $end_date = '')
  564.     {
  565.         if (empty($start_date)) {
  566.             $principal = 0;
  567.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'disbursed')->get() as $key) {
  568.                 $principal = $principal + $key->principal;
  569.             }
  570.             return $principal;
  571.         } else {
  572.             $principal = 0;
  573.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'disbursed')->whereBetween('release_date',
  574.                 [$start_date, $end_date])->get() as $key) {
  575.                 $principal = $principal + $key->principal;
  576.             }
  577.             return $principal;
  578.  
  579.         }
  580.  
  581.     }
  582.  
  583.     public static function total_other_income($start_date = '', $end_date = '')
  584.     {
  585.         if (empty($start_date)) {
  586.             return OtherIncome::where('branch_id', session('branch_id'))->sum('amount');
  587.         } else {
  588.             return OtherIncome::where('branch_id', session('branch_id'))->whereBetween('date', [$start_date, $end_date])->sum('amount');
  589.  
  590.         }
  591.  
  592.     }
  593.  
  594.     public static function total_savings_interest($start_date = '', $end_date = '')
  595.     {
  596.         if (empty($start_date)) {
  597.             return SavingTransaction::where('branch_id', session('branch_id'))->where('type', 'interest')->sum('amount');
  598.         } else {
  599.             return SavingTransaction::where('branch_id', session('branch_id'))->where('type', 'interest')->whereBetween('date',
  600.                 [$start_date, $end_date])->sum('amount');
  601.  
  602.         }
  603.  
  604.     }
  605.  
  606.     public static function total_savings_deposits($start_date = '', $end_date = '')
  607.     {
  608.         if (empty($start_date)) {
  609.             return SavingTransaction::where('branch_id', session('branch_id'))->where('type', 'deposit')->sum('amount');
  610.         } else {
  611.             return SavingTransaction::where('branch_id', session('branch_id'))->where('type', 'deposit')->whereBetween('date',
  612.                 [$start_date, $end_date])->sum('amount');
  613.  
  614.         }
  615.  
  616.     }
  617.  
  618.     public static function total_savings_withdrawals($start_date = '', $end_date = '')
  619.     {
  620.         if (empty($start_date)) {
  621.             return SavingTransaction::where('branch_id', session('branch_id'))->where('type', 'withdrawal')->sum('amount');
  622.         } else {
  623.             return SavingTransaction::where('branch_id', session('branch_id'))->where('type', 'withdrawal')->whereBetween('date',
  624.                 [$start_date, $end_date])->sum('amount');
  625.  
  626.         }
  627.  
  628.     }
  629.  
  630.     public static function total_capital($start_date = '', $end_date = '')
  631.     {
  632.         if (empty($start_date)) {
  633.             return Capital::where('branch_id', session('branch_id'))->sum('amount');
  634.         } else {
  635.             return Capital::where('branch_id', session('branch_id'))->whereBetween('date', [$start_date, $end_date])->sum('amount');
  636.  
  637.         }
  638.  
  639.     }
  640.  
  641.     public static function loans_total_paid_item($item, $start_date = '', $end_date = '')
  642.     {
  643.         if (empty($start_date)) {
  644.             $amount = 0;
  645.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'disbursed')->get() as $key) {
  646.                 $amount = $amount + GeneralHelper::loan_paid_item($key->id, $item, $key->due_date);
  647.             }
  648.             return $amount;
  649.         } else {
  650.             $amount = 0;
  651.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'disbursed')->whereBetween('release_date',
  652.                 [$start_date, $end_date])->get() as $key) {
  653.                 $amount = $amount + GeneralHelper::loan_paid_item($key->id, $item, $key->due_date);
  654.             }
  655.             return $amount;
  656.  
  657.         }
  658.  
  659.     }
  660.  
  661.     public static function loans_total_due_item($item, $start_date = '', $end_date = '')
  662.     {
  663.         if (empty($start_date)) {
  664.             $amount = 0;
  665.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'disbursed')->get() as $key) {
  666.                 if ($item == 'principal') {
  667.                     $amount = $amount + GeneralHelper::loan_total_principal($key->id);
  668.                 }
  669.                 if ($item == 'interest') {
  670.                     $amount = $amount + GeneralHelper::loan_total_interest($key->id);
  671.                 }
  672.                 if ($item == 'fees') {
  673.                     $amount = $amount + GeneralHelper::loan_total_fees($key->id);
  674.                 }
  675.                 if ($item == 'penalty') {
  676.                     $amount = $amount + GeneralHelper::loan_total_penalty($key->id);
  677.                 }
  678.  
  679.             }
  680.             return $amount;
  681.         } else {
  682.             $amount = 0;
  683.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'disbursed')->whereBetween('release_date',
  684.                 [$start_date, $end_date])->get() as $key) {
  685.                 if ($item == 'principal') {
  686.                     $amount = $amount + GeneralHelper::loan_total_principal($key->id);
  687.                 }
  688.                 if ($item == 'interest') {
  689.                     $amount = $amount + GeneralHelper::loan_total_interest($key->id);
  690.                 }
  691.                 if ($item == 'fees') {
  692.                     $amount = $amount + GeneralHelper::loan_total_fees($key->id);
  693.                 }
  694.                 if ($item == 'penalty') {
  695.                     $amount = $amount + GeneralHelper::loan_total_penalty($key->id);
  696.                 }
  697.             }
  698.             return $amount;
  699.         }
  700.  
  701.     }
  702.  
  703.     public static function loans_total_default($start_date = '', $end_date = '')
  704.     {
  705.         if (empty($start_date)) {
  706.             $principal = 0;
  707.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'written_off')->get() as $key) {
  708.                 $principal = $principal + ($key->principal - GeneralHelper::loan_total_paid($key->id));
  709.             }
  710.             return $principal;
  711.         } else {
  712.             $principal = 0;
  713.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'written_off')->whereBetween('release_date',
  714.                 [$start_date, $end_date])->get() as $key) {
  715.                 $principal = $principal + ($key->principal - GeneralHelper::loan_total_paid($key->id));
  716.             }
  717.             return $principal;
  718.  
  719.         }
  720.  
  721.     }
  722.  
  723.     public static function loans_total_due($start_date = '', $end_date = '')
  724.     {
  725.         if (empty($start_date)) {
  726.             $due = 0;
  727.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'disbursed')->get() as $key) {
  728.                 $due = $due + GeneralHelper::loan_total_due_amount($key->id);
  729.             }
  730.             return $due;
  731.         } else {
  732.             $due = 0;
  733.             foreach (Loan::where('branch_id', session('branch_id'))->where('status', 'disbursed')->whereBetween('release_date',
  734.                 [$start_date, $end_date])->get() as $key) {
  735.                 $due = $due + GeneralHelper::loan_total_due_amount($key->id);
  736.             }
  737.             return $due;
  738.  
  739.         }
  740.     }
  741.  
  742.     public static function borrower_loans_total_due($id)
  743.     {
  744.  
  745.         $due = 0;
  746.         foreach (Loan::where('status', 'disbursed')->where('borrower_id', $id)->get() as $key) {
  747.             $due = $due + GeneralHelper::loan_total_due_amount($key->id);
  748.         }
  749.         return $due;
  750.  
  751.     }
  752.  
  753.     public static function borrower_loans_total_paid($id)
  754.     {
  755.  
  756.         $paid = 0;
  757.         foreach (Loan::where('status', 'disbursed')->where('borrower_id', $id)->get() as $key) {
  758.             $paid = $paid + LoanRepayment::where('loan_id', $key->id)->sum('amount');
  759.         }
  760.         return $paid;
  761.  
  762.     }
  763.  
  764.     public static function audit_trail($notes)
  765.     {
  766.         $audit_trail = new AuditTrail();
  767.         $audit_trail->user_id = Sentinel::getUser()->id;
  768.         $audit_trail->user = Sentinel::getUser()->first_name . ' ' . Sentinel::getUser()->last_name;
  769.         $audit_trail->notes = $notes;
  770.         $audit_trail->branch_id = session('branch_id');
  771.         $audit_trail->save();
  772.  
  773.     }
  774.  
  775.     public static function savings_account_balance($id)
  776.     {
  777.  
  778.         $balance = 0;
  779.         foreach (SavingTransaction::where('savings_id', $id)->get() as $key) {
  780.             if ($key->type == 'deposit' || $key->type == 'interest' || $key->type == 'dividend' || $key->type == 'guarantee_restored') {
  781.                 $balance = $balance + $key->amount;
  782.             } else {
  783.                 $balance = $balance - $key->amount;
  784.             }
  785.         }
  786.         return $balance;
  787.  
  788.     }
  789.  
  790.     public static function borrower_savings_account_balance($id)
  791.     {
  792.  
  793.         $balance = 0;
  794.         if (!empty(Saving::where('borrower_id', $id)->first())) {
  795.             foreach (SavingTransaction::where('savings_id', Saving::where('borrower_id', $id)->first()->id)->get() as $key) {
  796.                 if ($key->type == 'deposit' || $key->type == 'interest' || $key->type == 'dividend' || $key->type == 'guarantee_restored') {
  797.                     $balance = $balance + $key->amount;
  798.                 } else {
  799.                     $balance = $balance - $key->amount;
  800.                 }
  801.             }
  802.         }
  803.         return $balance;
  804.  
  805.     }
  806.  
  807.     public static function asset_valuation($id, $start_date = '')
  808.     {
  809.  
  810.         if (empty($start_date)) {
  811.             $value = 0;
  812.             if (!empty(AssetValuation::where('asset_id', $id)->orderBy('date', 'desc')->first())) {
  813.                 $value = AssetValuation::where('asset_id', $id)->orderBy('date', 'desc')->first()->amount;
  814.             }
  815.             return $value;
  816.         } else {
  817.             $value = 0;
  818.             if (!empty(AssetValuation::where('asset_id', $id)->where('date', '<=', $start_date)->orderBy('date',
  819.                 'desc')->first())
  820.             ) {
  821.                 $value = AssetValuation::where('asset_id', $id)->where('date', '<=', $start_date)->orderBy('date',
  822.                     'desc')->first()->amount;
  823.             }
  824.             return $value;
  825.  
  826.         }
  827.  
  828.     }
  829.  
  830.     public static function asset_type_valuation($id, $start_date = '')
  831.     {
  832.  
  833.         if (empty($start_date)) {
  834.             $value = 0;
  835.             foreach (Asset::where('branch_id', session('branch_id'))->where('asset_type_id', $id)->get() as $key) {
  836.                 if (!empty(AssetValuation::where('asset_id', $key->id)->orderBy('date', 'desc')->first())) {
  837.                     $value = AssetValuation::where('asset_id', $key->id)->orderBy('date', 'desc')->first()->amount;
  838.                 }
  839.             }
  840.             return $value;
  841.         } else {
  842.             $value = 0;
  843.             foreach (Asset::where('branch_id', session('branch_id'))->where('asset_type_id', $id)->get() as $key) {
  844.                 if (!empty(AssetValuation::where('asset_id', $key->id)->where('date', '<=',
  845.                     $start_date)->orderBy('date',
  846.                     'desc')->first())
  847.                 ) {
  848.                     $value = AssetValuation::where('asset_id', $key->id)->where('date', '<=',
  849.                         $start_date)->orderBy('date',
  850.                         'desc')->first()->amount;
  851.                 }
  852.             }
  853.             return $value;
  854.  
  855.         }
  856.  
  857.     }
  858. }

Editor

You can edit this paste and save as new:


File Description
  • general helper
  • Paste Code
  • 22 Jun-2021
  • 31.94 Kb
You can Share it: