예제 #1
0
function ComputeWagesAndDelays($PartObj, $PayAmount, $StartDate, $PayDate)
{
    $MaxWage = max($PartObj->CustomerWage * 1, $PartObj->FundWage);
    if ($PartObj->PayInterval > 0) {
        $YearMonths = $PartObj->IntervalType == "DAY" ? floor(365 / $PartObj->PayInterval) : 12 / $PartObj->PayInterval;
    } else {
        $YearMonths = 12;
    }
    $TotalWage = round(ComputeWage($PayAmount, $MaxWage / 100, $PartObj->InstallmentCount, $PartObj->IntervalType, $PartObj->PayInterval));
    $CustomerFactor = $MaxWage == 0 ? 0 : $PartObj->CustomerWage / $MaxWage;
    $FundFactor = $MaxWage == 0 ? 0 : $PartObj->FundWage / $MaxWage;
    $AgentFactor = $MaxWage == 0 ? 0 : ($PartObj->CustomerWage - $PartObj->FundWage) / $MaxWage;
    ///...........................................................
    if ($PartObj->MaxFundWage * 1 > 0) {
        if ($PartObj->WageReturn == "INSTALLMENT") {
            $FundYears = YearWageCompute($PartObj, $PartObj->MaxFundWage * 1, $YearMonths);
        } else {
            $FundYears = array();
        }
    } else {
        $years = YearWageCompute($PartObj, $TotalWage * 1, $YearMonths);
        $FundYears = array();
        foreach ($years as $year => $amount) {
            $FundYears[$year] = round($FundFactor * $amount);
        }
    }
    $AgentYears = array();
    foreach ($years as $year => $amount) {
        $AgentYears[$year] = round($amount - $FundYears[$year]);
    }
    //.............................................................
    $endDelayDate = DateModules::AddToGDate($PayDate, $PartObj->DelayDays * 1, $PartObj->DelayMonths * 1);
    $DelayDuration = DateModules::GDateMinusGDate($endDelayDate, $PayDate) + 1;
    if ($StartDate == $PayDate) {
        if ($PartObj->DelayDays * 1 > 0) {
            $CustomerDelay = round($PayAmount * $PartObj->DelayPercent * $DelayDuration / 36500);
            $FundDelay = round($PayAmount * $PartObj->FundWage * $DelayDuration / 36500);
            $AgentDelay = round($PayAmount * ($PartObj->DelayPercent - $PartObj->FundWage) * $DelayDuration / 36500);
        } else {
            $CustomerDelay = round($PayAmount * $PartObj->DelayPercent * $PartObj->DelayMonths / 1200);
            $FundDelay = round($PayAmount * $PartObj->FundWage * $PartObj->DelayMonths / 1200);
            $AgentDelay = round($PayAmount * ($PartObj->DelayPercent - $PartObj->FundWage) * $PartObj->DelayMonths / 1200);
        }
    } else {
        $endDelayDate = DateModules::AddToGDate($StartDate, $PartObj->DelayDays * 1, $PartObj->DelayMonths * 1);
        $DelayDuration = DateModules::GDateMinusGDate($endDelayDate, $PayDate) + 1;
        $CustomerDelay = round($PayAmount * $PartObj->DelayPercent * $DelayDuration / 36500);
        $FundDelay = round($PayAmount * $PartObj->FundWage * $DelayDuration / 36500);
        $AgentDelay = round($PayAmount * ($PartObj->DelayPercent - $PartObj->FundWage) * $DelayDuration / 36500);
    }
    $CustomerYearDelays = SplitYears($PayDate, $endDelayDate, $CustomerDelay);
    //.............................................................
    return array("TotalFundWage" => round($TotalWage * $FundFactor), "TotalAgentWage" => round($TotalWage * $AgentFactor), "TotalCustomerWage" => round($TotalWage * $CustomerFactor), "FundWageYears" => $FundYears, "AgentWageYears" => $AgentYears, "TotalCustomerDelay" => $CustomerDelay, "TotalFundDelay" => $FundDelay, "TotalAgentDelay" => $AgentDelay, "CustomerYearDelays" => $CustomerYearDelays);
}
예제 #2
0
function RegisterWarrantyDoc($ReqObj, $WageCost, $BankTafsili, $AccountTafsili, $Block_CostID, $DocID, $pdo)
{
    /*@var $ReqObj WAR_requests */
    //------------- get CostCodes --------------------
    $CostCode_warrenty = FindCostID("300");
    $CostCode_warrenty_commitment = FindCostID("700");
    $CostCode_wage = FindCostID("750-07");
    $CostCode_FutureWage = FindCostID("760-07");
    $CostCode_fund = FindCostID("100");
    $CostCode_seporde = FindCostID("690");
    $CostCode_pasandaz = FindCostID("209-10");
    $CostCode_guaranteeAmount_zemanati = FindCostID("904-02");
    $CostCode_guaranteeAmount_daryafti = FindCostID("904-04");
    $CostCode_guaranteeAmount2_zemanati = FindCostID("905-02");
    $CostCode_guaranteeAmount2_daryafti = FindCostID("905-04");
    //------------------------------------------------
    $CycleID = substr(DateModules::miladi_to_shamsi($ReqObj->StartDate), 0, 4);
    //------------------ find tafsilis ---------------
    $PersonTafsili = FindTafsiliID($ReqObj->PersonID, TAFTYPE_PERSONS);
    if (!$PersonTafsili) {
        ExceptionHandler::PushException("تفصیلی مربوطه یافت نشد.[" . $ReqObj->PersonID . "]");
        return false;
    }
    //------------------- compute wage ------------------
    $days = DateModules::GDateMinusGDate($ReqObj->EndDate, $ReqObj->StartDate);
    //if(DateModules::YearIsLeap($CycleID));
    $days -= 1;
    $TotalWage = round($days * $ReqObj->amount * 0.9 * $ReqObj->wage / 36500) + $ReqObj->RegisterAmount * 1;
    $years = SplitYears(DateModules::miladi_to_shamsi($ReqObj->StartDate), DateModules::miladi_to_shamsi($ReqObj->EndDate), $TotalWage);
    //--------------- check pasandaz remaindar -----------------
    $dt = PdoDataAccess::runquery("select sum(CreditorAmount-DebtorAmount) remain\r\n\t\tfrom ACC_DocItems join ACC_docs using(DocID) where CycleID=? AND CostID=?\r\n\t\t\tAND TafsiliType=? AND TafsiliID=? AND BranchID=?", array($CycleID, $CostCode_pasandaz, TAFTYPE_PERSONS, $PersonTafsili, $ReqObj->BranchID));
    if ($WageCost == "209-10" && $dt[0][0] * 1 < $ReqObj->amount * 0.1) {
        ExceptionHandler::PushException("مانده حساب پس انداز مشتری کمتر از 10% مبلغ ضمانت نامه می باشد");
        return false;
    }
    if ($WageCost == "209-10" && $dt[0][0] * 1 < $ReqObj->amount * 0.1 + $TotalWage) {
        ExceptionHandler::PushException("مانده حساب پس انداز مشتری کمتر از مبلغ کارمزد می باشد");
        return false;
    }
    if ($ReqObj->IsBlock == "YES") {
        if ($Block_CostID != "" && $Block_CostID != $CostCode_pasandaz) {
            $dt = PdoDataAccess::runquery("select sum(CreditorAmount-DebtorAmount) remain\r\n\t\t\tfrom ACC_DocItems join ACC_docs using(DocID) where CycleID=? AND CostID=?\r\n\t\t\t\tAND TafsiliType=? AND TafsiliID=? AND BranchID=?", array($CycleID, $Block_CostID, TAFTYPE_PERSONS, $PersonTafsili, $ReqObj->BranchID));
        }
        $amount = $ReqObj->amount * 1;
        if ($WageCost == "209-10") {
            $amount += $ReqObj->amount * 0.1 + $TotalWage;
        }
        if ($dt[0][0] * 1 < $amount) {
            ExceptionHandler::PushException("مانده حساب انتخابی جهت بلوکه کمتر از مبلغ ضمانت نامه می باشد");
            return false;
        }
    }
    //---------------- add doc header --------------------
    if ($DocID == null) {
        $DocObj = new ACC_docs();
        $DocObj->RegDate = PDONOW;
        $DocObj->regPersonID = $_SESSION['USER']["PersonID"];
        $DocObj->DocDate = PDONOW;
        $DocObj->CycleID = $CycleID;
        $DocObj->BranchID = $ReqObj->BranchID;
        $DocObj->DocType = DOCTYPE_WARRENTY;
        $DocObj->description = "ضمانت نامه " . $ReqObj->_TypeDesc . " به شماره " . $ReqObj->RequestID . " به نام " . $ReqObj->_fullname;
        if (!$DocObj->Add($pdo)) {
            ExceptionHandler::PushException("خطا در ایجاد سند");
            return false;
        }
    } else {
        $DocObj = new ACC_docs($DocID);
    }
    //----------------- add Doc items ------------------------
    $itemObj = new ACC_DocItems();
    $itemObj->DocID = $DocObj->DocID;
    $itemObj->TafsiliType = TAFTYPE_PERSONS;
    $itemObj->TafsiliID = $PersonTafsili;
    $itemObj->SourceType = DOCTYPE_WARRENTY;
    $itemObj->SourceID = $ReqObj->RequestID;
    $itemObj->SourceID2 = $ReqObj->ReqVersion;
    $itemObj->locked = "YES";
    $itemObj->CostID = $CostCode_warrenty;
    $itemObj->DebtorAmount = $ReqObj->amount;
    $itemObj->CreditorAmount = 0;
    if (!$itemObj->Add($pdo)) {
        ExceptionHandler::PushException("خطا در ثبت ردیف ضمانت نامه");
        return false;
    }
    unset($itemObj->ItemID);
    $itemObj->CostID = $CostCode_warrenty_commitment;
    $itemObj->DebtorAmount = 0;
    $itemObj->CreditorAmount = $ReqObj->amount;
    if (!$itemObj->Add($pdo)) {
        ExceptionHandler::PushException("خطا در ثبت ردیف تعهد ضمانت نامه");
        return false;
    }
    //------------------- compute wage -----------------------
    $curYear = substr(DateModules::miladi_to_shamsi($ReqObj->StartDate), 0, 4) * 1;
    foreach ($years as $Year => $amount) {
        if ($amount == 0) {
            continue;
        }
        $YearTafsili = FindTafsiliID($Year, TAFTYPE_YEARS);
        if (!$YearTafsili) {
            ExceptionHandler::PushException("تفصیلی مربوطه یافت نشد.[" . $Year . "]");
            return false;
        }
        unset($itemObj->ItemID);
        $itemObj->details = "کارمزد ضمانت نامه شماره " . $ReqObj->RequestID;
        $itemObj->CostID = $Year == $curYear ? $CostCode_wage : $CostCode_FutureWage;
        $itemObj->DebtorAmount = 0;
        $itemObj->CreditorAmount = $amount;
        $itemObj->TafsiliType = TAFTYPE_YEARS;
        $itemObj->TafsiliID = $YearTafsili;
        $itemObj->Add($pdo);
    }
    //---------------------------- block Cost ----------------------------
    if ($ReqObj->IsBlock == "YES") {
        $blockObj = new ACC_CostBlocks();
        $blockObj->CostID = !empty($Block_CostID) ? $Block_CostID : $CostCode_pasandaz;
        $blockObj->TafsiliType = TAFTYPE_PERSONS;
        $blockObj->TafsiliID = $PersonTafsili;
        $blockObj->BlockAmount = $ReqObj->amount;
        $blockObj->IsLock = "YES";
        $blockObj->SourceType = DOCTYPE_WARRENTY;
        $blockObj->SourceID = $ReqObj->RequestID;
        $blockObj->details = "بابت ضمانت نامه شماره " . $ReqObj->RequestID;
        if (!$blockObj->Add()) {
            print_r(ExceptionHandler::PopAllExceptions());
            ExceptionHandler::PushException("خطا در بلوکه کردن حساب پس انداز");
            return false;
        }
    }
    // ---------------------- Warrenty costs -----------------------------
    $totalCostAmount = 0;
    $dt = PdoDataAccess::runquery("select * from WAR_costs where RequestID=?", array($ReqObj->RequestID));
    foreach ($dt as $row) {
        $totalCostAmount += ($row["CostType"] == "DEBTOR" ? 1 : -1) * $row["CostAmount"] * 1;
        unset($itemObj->ItemID);
        $itemObj->SourceID2 = $row["CostID"];
        $itemObj->details = $row["CostDesc"];
        $itemObj->CostID = $row["CostCodeID"];
        $itemObj->DebtorAmount = $row["CostType"] == "DEBTOR" ? $row["CostAmount"] : 0;
        $itemObj->CreditorAmount = $row["CostType"] == "CREDITOR" ? $row["CostAmount"] : 0;
        $itemObj->Add($pdo);
    }
    // ----------------------------- bank --------------------------------
    unset($itemObj->ItemID);
    unset($itemObj->TafsiliType);
    unset($itemObj->TafsiliID);
    $itemObj->details = "بابت 10% سپرده ضمانت نامه شماره " . $ReqObj->RequestID;
    $itemObj->CostID = $CostCode_seporde;
    $itemObj->DebtorAmount = 0;
    $itemObj->CreditorAmount = $ReqObj->amount * 0.1;
    $itemObj->Add($pdo);
    unset($itemObj->ItemID);
    $itemObj->details = "بابت کارمزد ضمانت نامه شماره " . $ReqObj->RequestID;
    $itemObj->CostID = FindCostID($WageCost);
    $itemObj->DebtorAmount = $TotalWage + $ReqObj->amount * 0.1 - $totalCostAmount;
    $itemObj->CreditorAmount = 0;
    if ($WageCost == "101") {
        $itemObj->TafsiliType = TAFTYPE_BANKS;
        if ($BankTafsili != "") {
            $itemObj->TafsiliID = $BankTafsili;
        }
        $itemObj->TafsiliType2 = TAFTYPE_ACCOUNTS;
        if ($AccountTafsili != "") {
            $itemObj->TafsiliID2 = $AccountTafsili;
        }
    }
    $itemObj->Add($pdo);
    //---------- ردیف های تضمین  ----------
    $SumAmount = 0;
    $countAmount = 0;
    $dt = PdoDataAccess::runquery("\r\n\t\tSELECT DocumentID, ParamValue, InfoDesc as DocTypeDesc\r\n\t\t\tFROM DMS_DocParamValues\r\n\t\t\tjoin DMS_DocParams using(ParamID)\r\n\t\t\tjoin DMS_documents d using(DocumentID)\r\n\t\t\tjoin BaseInfo b on(InfoID=d.DocType AND TypeID=8)\r\n\t\t\tleft join ACC_DocItems on(SourceType=" . DOCTYPE_DOCUMENT . " AND SourceID=DocumentID)\r\n\t\twhere ItemID is null AND b.param1=1 AND \r\n\t\t\tparamType='currencyfield' AND ObjectType='warrenty' AND ObjectID=?", array($ReqObj->RequestID), $pdo);
    foreach ($dt as $row) {
        unset($itemObj->ItemID);
        $itemObj->CostID = $CostCode_guaranteeAmount_zemanati;
        $itemObj->DebtorAmount = $row["ParamValue"];
        $itemObj->CreditorAmount = 0;
        $itemObj->TafsiliType = TAFTYPE_PERSONS;
        $itemObj->TafsiliID = $PersonTafsili;
        $itemObj->SourceType = DOCTYPE_DOCUMENT;
        $itemObj->SourceID = $row["DocumentID"];
        $itemObj->details = $row["DocTypeDesc"];
        $itemObj->Add($pdo);
        $SumAmount += $row["ParamValue"] * 1;
        $countAmount++;
    }
    if ($SumAmount > 0) {
        unset($itemObj->ItemID);
        unset($itemObj->TafsiliType);
        unset($itemObj->TafsiliID);
        unset($itemObj->details);
        $itemObj->CostID = $CostCode_guaranteeAmount2_zemanati;
        $itemObj->DebtorAmount = 0;
        $itemObj->CreditorAmount = $SumAmount;
        $itemObj->Add($pdo);
    }
    if (ExceptionHandler::GetExceptionCount() > 0) {
        return false;
    }
    return $DocObj->DocID;
}