<?php
$year           = intval($_POST['salary_year']);
$month          = intval($_POST['salary_month']);

$department_id  = intval($_POST['department_id']);
$employeesData  = $_POST['employeesData'];

$firstDayTimestamp = strtotime("$year-$month-01");
$department=$db->get_rowData('employee_department','id',$department_id);

if(!empty($department)){
    $db->transactionStart();

    if(!empty($employeesData)){
        foreach($employeesData as $e){
            $pay_date       = strtotime($e['pay_date']);
            $total=0;
            $already=$db->getRowData('employee_salary','where employee_id='.$e['id'].' and salary_date='.$firstDayTimestamp);

            if(empty($already)){
                $data=[
                    'employee_id'   => $e['id'],
                    'pay_date'      => $pay_date,
                    'salary_date'   => $firstDayTimestamp,
                    'allowance'     => 0,
                    'total'         => 0,
                ];

                $db->arrayUserInfoAdd($data);
                $salary_id=$db->insert('employee_salary',$data,true); 
            }
            else{
                $salary_id=$already['id'];
            }

            if($salary_id!=false){
                $where=['salary_id'=>$salary_id];
                $delete=$db->delete('employee_salary_allowance',$where);
                $allowances_total=0;
                if(isset($e['allowances'])&&!empty($e['allowances'])){
                    foreach($e['allowances'] as $a){
                        $amount=floatval($a['amount']);
                        $allowances_total=$allowances_total+$amount;
                        if($amount>0){
                            $data=[
                                'salary_id'             => $salary_id,
                                'salary_allowance_id'   => $a['id'],
                                'salary_period'         => $firstDayTimestamp,
                                'amount'                => $amount,
                            ];
                            $insert=$db->insert('employee_salary_allowance',$data);
                            if($insert==false){$error=fl();setMessage(66);}
                        }

                    } 
                }
                $total=floatval($e['salary'])+$allowances_total;
                if(!isset($error)){
                    $data=[
                        'allowance' => $allowances_total,
                        'pay_date'  => $pay_date,
                        'total'     => $total,
                        'salary'   => floatval($e['salary']),
                    ];
                    $where=['id'=>$salary_id];
                    $update=$db->update('employee_salary',$data,$where);
                    if($update==false){$error=fl();setMessage(66);}
                }

                if(!isset($error)){
                    $employee=$smt->employeeInfoByID($e['id']);
                    $cashHead= $acc->getSystemHead(AH_CASH,$jArray);
                    if($cashHead==false){$error=fl();setMessage(66);}
                    $empHead= $acc->getEmployeeHead($employee,$jArray);
                    if($empHead==false){$error=fl();setMessage(66);}
                    $create_new=1;
                    if(!empty($already)){
                        $old_voucher=$acc->voucherDetails(V_T_EMPLOYEE_SALARY,$salary_id);
                        $o=current($old_voucher);
                        $jArray[fl()]=$o;
                        $jArray[fl()]=$empHead;
                        if(!empty($old_voucher)){
                            $dHead=$empHead;
                            $cHead=$cashHead; 
                            $update=$acc->voucherEdit($o['id'],$total,$o['note'],$dHead,$cHead,$pay_date,[],$jArray);
                            if($update==false){
                                $error=fl();setMessage(66);
                            }
                            else{
                                $create_new=0;
                            }
                        }
                    }
                    
                    if($create_new==1){
                        $voucher=$acc->voucher_create(V_T_EMPLOYEE_SALARY,$total,$empHead,$cashHead,$pay_date,'Pay to '.$employee['name'],$salary_id);
                        if($voucher==false){
                            $error=fl();setMessage(66);
                        }
                    }
                }
            }
            else{
                $error=fl();setMessage(66);
            }
        }
    }
    else{
        $error=fl();setMessage(63,'Request');
    }

    if(!isset($error)){
        $ac=true;
        $jArray['status']=1;
        setMessage(30,'Employee Salary');
    }
    else{$ac=false;}
    $db->transactionStop($ac);
}




?>
