package com.awspaas.user.apps.donenow_ctt.controller;

import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.donenow_ctt.service.InvoiceDealDto;
import com.awspaas.user.apps.donenow_ctt.service.InvoiceService;

import java.util.List;

/**
 * 账单管理
 */
@Controller
public class invoiceController {
    /**
     * 预览发票
     * @param uc
     * @param accDeduIds
     * @return
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.preview_invoice")
    public ResponseObject previewInvoice(UserContext uc, String accDeduIds) {
        InvoiceDealDto invoiceDealDto = new InvoiceDealDto();
        invoiceDealDto.setIds(accDeduIds);
        List<String> invoiceIds = InvoiceService.getInstance().processInvoice(uc, accDeduIds, invoiceDealDto, false);

        return ResponseObject.newOkResponse();
    }


    /**
     * 处理发票
     * @param uc
     * @param accDeduIds
     * @return
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.deal_invoice")
    public ResponseObject dealInvoice(UserContext uc, String accDeduIds, String startDate, String endDate) {
        InvoiceDealDto invoiceDealDto = new InvoiceDealDto();
        invoiceDealDto.setIds(accDeduIds);
        invoiceDealDto.setDateRangeFromStr(startDate);
        invoiceDealDto.setDateRangeToStr(endDate);

        List<String> invoiceIds = InvoiceService.getInstance().processInvoice(uc, accDeduIds, invoiceDealDto, true);

        return ResponseObject.newOkResponse();
    }


    /**
     * 发票更新
     * @param uc
     * @param invoiceIds
     * @return
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.invoiceUpdate")
    public ResponseObject invoiceUpdate(UserContext uc, String invoiceIds, String dealStatus) {

        InvoiceService.getInstance().invoiceUpdate(uc, invoiceIds, dealStatus);

        return ResponseObject.newOkResponse();
    }


    /**
     * 删除发票计划
     * @param uc
     * @param bindids
     * @return
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.delInvoicePlan")
    public ResponseObject delInvoicePlan(UserContext uc, String bindids) {

        for (String bindid : bindids.split(",")) {
            SDK.getProcessAPI().deleteById(bindid, uc.getUID());
            SDK.getBOAPI().removeByBindId("BO_EU_DNCTT_INVOICE_PLAN_DETAIL", bindid);
            SDK.getBOAPI().removeByBindId("BO_EU_DNCTT_INVOICE_PLAN", bindid);
        }

        return ResponseObject.newOkResponse();
    }


    /**
     * 获取税率
     * @param uc
     * @param taxCategoryId
     * @return
     */
    @Mapping("com.awspaas.user.apps.donenow_ctt.getTaxRate")
    public ResponseObject getTaxRate(UserContext uc, String taxCategoryId) {
        String taxRate = DBSql.getString("select EXT1 from bo_eu_dnd_general where is_active=1 and closed=0 and general_table_id=44 AND OID=? AND ORGID=?", new Object[]{taxCategoryId, uc.getCompanyModel().getId()});
        return ResponseObject.newOkResponse().put("taxRate", taxRate);
    }

}
