<?php

if (cfr('SALARY')) {
    $altcfg = $ubillingConfig->getAlter();
    if ($altcfg['SALARY_ENABLED']) {
        $salary = new Salary();

            show_window('', $salary->renderControls());

            // jobtype pricing creation
            if (wf_CheckPost(array('newjobtypepriceid', 'newjobtypepriceunit'))) {
                $salary->jobPriceCreate($_POST['newjobtypepriceid'], $_POST['newjobtypeprice'], $_POST['newjobtypepriceunit'], $_POST['newjobtypepricetime']);
                rcms_redirect('?module=salary&jobprices=true');
            }

            //jobtype price deletion
            if (wf_CheckGet(array('deletejobprice'))) {
                $salary->jobPriceDelete($_GET['deletejobprice']);
                rcms_redirect('?module=salary&jobprices=true');
            }
            //saving jobprices into database
            if (wf_CheckPost(array('editjobtypepriceid'))) {
                $salary->jobPriceEdit($_POST['editjobtypepriceid']);
                rcms_redirect('?module=salary&jobprices=true');
            }

            //listing avalable job pricings            
            if (wf_CheckGet(array('jobprices'))) {
                $jpCf = $salary->jobPricesCreateForm();
                if ($jpCf) {
                    show_window(__('Job types pricing'), $jpCf);
                } else {
                    show_warning(__('No available job types for pricing'));
                }
                show_window(__('Available job types pricing'), $salary->jobPricesRender());
                show_window('', wf_BackLink($salary::URL_ME));
            }

            /**
              Come sing along with the pirate song
              Hail to the wind, hooray to the glory
              We're gonna fight 'til the battle's won
              On the raging sea
             */
            //creation new employee wage
            if (wf_CheckPost(array('newemployeewageemployeeid', 'newemployeewage'))) {
                $salary->employeeWageCreate($_POST['newemployeewageemployeeid'], $_POST['newemployeewage'], $_POST['newemployeewagebounty'], $_POST['newemployeewageworktime']);
                rcms_redirect($salary::URL_ME . '&' . $salary::URL_WAGES);
            }

            //editing existing employee
            if (wf_CheckPost(array('editemployeewageemployeeid', 'editemployeewage'))) {
                $salary->employeeWageEdit($_POST['editemployeewageemployeeid'], $_POST['editemployeewage'], $_POST['editemployeewagebounty'], $_POST['editemployeewageworktime']);
                rcms_redirect($salary::URL_ME . '&' . $salary::URL_WAGES);
            }


            //deleting employee wage
            if (wf_CheckGet(array('deletewage'))) {
                $salary->employeeWageDelete($_GET['deletewage']);
                rcms_redirect($salary::URL_ME . '&' . $salary::URL_WAGES);
            }

            //listing available employee wages
            if (wf_CheckGet(array('employeewages'))) {
                $ewCf = $salary->employeeWageCreateForm();
                if ($ewCf) {
                    show_window(__('Employee wages'), $ewCf);
                } else {
                    show_warning(__('No available workers for wage creation'));
                }
                show_window(__('Available employee wages'), $salary->employeeWagesRender());
                show_window('', wf_BackLink($salary::URL_ME));
            }
            //rendering payroll report
            if (ubRouting::checkGet('payroll')) {
                //printable per-employee report
                if (ubRouting::checkGet(array('print', 'e', 'df', 'dt'))) {
                    $reportPrintTitle = __('Payroll') . ': ' . $salary->getEmployeeName(ubRouting::get('e')) . ' ' . __('from') . ' ' . ubRouting::get('df') . ' ' . __('to') . ' ' . ubRouting::get('dt');
                    $salary->reportPrintable($reportPrintTitle, $salary->payrollRenderSearch(ubRouting::get('df'), ubRouting::get('dt'), ubRouting::get('e'), '', false));
                } else {
                        //printable all employee report
                        if (ubRouting::checkGet(array('print', 'df', 'dt'))) {
                            $reportPrintTitle = __('Payroll') . ': ' . __('All') . ' ' . __('Employee')  . ' ' . __('from') . ' ' . ubRouting::get('df') . ' ' . __('to') . ' ' . ubRouting::get('dt');
                            $salary->reportPrintable($reportPrintTitle, $salary->payrollRenderSearchDate(ubRouting::get('df'), ubRouting::get('dt'), ''));
                        }
                }

                


                show_window(__('Search'), $salary->payrollRenderSearchForm());
                //job state processing confirmation
                if (wf_CheckPost(array('prstateprocessing'))) {
                    show_window(__('Confirmation'), $salary->payrollStateProcessingForm());
                }

                if (wf_CheckPost(array('prstateprocessingconfirmed'))) {
                    $salary->payrollStateProcessing();
                }


                if (ubRouting::checkPost(array('prdatefrom', 'prdateto'))) {
                    if (ubRouting::checkPost('premployeeid')) {
                        //single employee report
                        $reportTitle = __('Payroll') . ': ' . $salary->getEmployeeName(ubRouting::post('premployeeid')) . ' ' . __('from') . ' ' . ubRouting::post('prdatefrom') . ' ' . __('to') . ' ' . ubRouting::post('prdateto') . ' ';
                        $extraParams = (ubRouting::checkPost('prnocharts')) ? '&nc=1' : '';
                        $printLink = wf_tag('a', false, '', 'href="' . $salary::URL_ME . '&' . salary::URL_PAYROLL . '&print=true&e=' . ubRouting::post('premployeeid') . '&df=' . ubRouting::post('prdatefrom') . '&dt=' . ubRouting::post('prdateto') . $extraParams . '" TARGET="_BLANK"');
                        $printLink .= web_icon_print();
                        $printLink .= wf_tag('a', true);
                        $reportTitle .= $printLink;
                        show_window($reportTitle, $salary->payrollRenderSearch(ubRouting::post('prdatefrom'), ubRouting::post('prdateto'), ubRouting::post('premployeeid'), ubRouting::post('prjobtypeid')));
                    } else {
                        //multiple employee report
                        $reportTitle = __('Payroll') . ': ' . __('All') . ' ' . __('Employee') . ' ' . __('from') . ' ' . ubRouting::post('prdatefrom') . ' ' . __('to') . ' ' . ubRouting::post('prdateto') . ' ';
                        $extraParams = (ubRouting::checkPost('prnocharts')) ? '&nc=1' : '';
                        $printLink = wf_tag('a', false, '', 'href="' . $salary::URL_ME . '&' . salary::URL_PAYROLL . '&print=true&df=' . ubRouting::post('prdatefrom') . '&dt=' . ubRouting::post('prdateto') . $extraParams . '" TARGET="_BLANK"');
                        $printLink .= web_icon_print();
                        $printLink .= wf_tag('a', true);
                        $reportTitle .= $printLink;
                        show_window($reportTitle, $salary->payrollRenderSearchDate(ubRouting::post('prdatefrom'), ubRouting::post('prdateto'), ubRouting::post('prjobtypeid')));
                    }
                }
                show_window('', wf_BackLink($salary::URL_ME));
            }

            //rendering factor control report 
            if (wf_CheckGet(array('factorcontrol'))) {
                show_window(__('Search'), $salary->facontrolRenderSearchForm());
                if (wf_CheckPost(array('facontroljobtypeid', 'facontrolmaxfactor'))) {
                    show_window(__('Factor control'), $salary->facontrolRenderSearch($_POST['facontroljobtypeid'], $_POST['facontrolmaxfactor']));
                }
                show_window('', wf_BackLink($salary::URL_ME));
            }


            // tasks without assinged jobs report
            if (wf_CheckGet(array('twjreport'))) {
                show_window(__('Search'), $salary->twjReportSearchForm());
                if (wf_CheckPost(array('twfdatefrom', 'twfdateto'))) {
                    show_window(__('Tasks without jobs'), $salary->twjReportSearch($_POST['twfdatefrom'], $_POST['twfdateto']));
                }
                show_window('', wf_BackLink($salary::URL_ME));
            }
            //timesheets reports
            if (wf_CheckGet(array('timesheets'))) {
                //creating of new timesheet
                if (wf_CheckPost(array('newtimesheet', 'newtimesheetdate', '_employeehours'))) {
                    $tsSheetCreateResult = $salary->timesheetCreate();
                    if ($tsSheetCreateResult == 0) {
                        //succeful creation
                        rcms_redirect($salary::URL_ME . '&' . $salary::URL_TSHEETS);
                    } else {
                        if ($tsSheetCreateResult == 1) {
                            //date duplicate
                            show_error(__('Timesheets with that date already exist'));
                        }
                    }
                }

                $tsCf = $salary->timesheetCreateForm();
                if ($tsCf) {
                    $timesheetsControls = wf_modal(web_add_icon() . ' ' . __('Create'), __('Create') . ' ' . __('Timesheet'), $tsCf, 'ubButton', '800', '600');
                    $timesheetsControls .= wf_Link($salary::URL_ME . '&' . $salary::URL_TSHEETS . '&print=true', web_icon_print() . ' ' . __('Print'), false, 'ubButton');
                    show_window('', $timesheetsControls);
                    if (!wf_CheckGet(array('showdate'))) {
                        if (wf_CheckGet(array('print'))) {
                            //printing soubrutine
                            show_window('', $salary->timesheetRenderPrintableForm());
                            if (wf_CheckPost(array('tsheetprintyear', 'tsheetprintmonth'))) {
                                die($salary->timesheetRenderPrintable($_POST['tsheetprintyear'], $_POST['tsheetprintmonth']));
                            }

                            show_window('', wf_BackLink($salary::URL_ME . '&' . $salary::URL_TSHEETS));
                        } else {
                            //render available timesheets list by date
                            show_window(__('Timesheets'), $salary->timesheetsListRender());
                            show_window('', wf_BackLink($salary::URL_ME));
                        }
                    } else {
                        //saving changes for single timesheet row
                        if (wf_CheckPost(array('edittimesheetid'))) {
                            $salary->timesheetSaveChanges();
                            rcms_redirect($salary::URL_ME . '&' . $salary::URL_TSHEETS . '&showdate=' . $_GET['showdate']);
                        }
                        //render timesheet by date (edit form)
                        show_window(__('Timesheet') . ' ' . $_GET['showdate'], $salary->timesheetEditForm($_GET['showdate']));
                        show_window('', wf_BackLink($salary::URL_ME . '&' . $salary::URL_TSHEETS));
                    }
                } else {
                    show_warning(__('No available workers for timesheets'));
                }
            }

            //jobs/time report
            if (wf_CheckGet(array('ltreport'))) {
                show_window(__('Search'), $salary->ltReportRenderForm());
                show_window(__('Labor time'), $salary->ltReportRenderResults());
                show_window('', wf_BackLink($salary::URL_ME));
            }

            if (wf_CheckGet(array('yearreport'))) {
                show_window(__('Year salary reports'), $salary->renderYearReport());
                show_window('', wf_BackLink($salary::URL_ME));
            }

            //module summary stats
            $salary->summaryReport();
    } else {
        show_error(__('This module is disabled'));
    }
} else {
    show_error(__('Permission denied'));
}
