using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace HH.YiDaSyncNC.Models
{
    class BZJ_SyncServiceModel
    {
        public class DataModel
        {
            public string 集团 { get; set; } = "0";
            public string 组织 { get; set; }

            public string 销售订单单据类型 { get; set; } = "30-Cxx-02";
            public string 销售订单单据大类 { get; set; } = "30";

            public string 销售发票单据类型 { get; set; } = "32-01";
            public string 销售发票单据大类 { get; set; } = "32";

            public string 应收单单据类型 { get; set; } = "F0";
            public string 应收类型 { get; set; } = "D0";
            public string 应收单单据大类 { get; set; } = "ys";

            public string 业务流程 { get; set; } = "Cxx-xs-03";
            public string 单据号 { get; set; } // 不传
            public string 单据日期 { get; set; }

            public string 客户 { get; set; }
            public string 部门 { get; set; }
            public string 业务员 { get; set; }
            public string 币种 { get; set; } = "CNY";
            public string 收款协议 { get; set; }
            public string 审批人 { get; set; }
            public string 审批时间 { get; set; }
            public string 制单人 { get; set; }
            public string 制单时间 { get; set; }
            public string 创建人 { get; set; }
            public string 创建时间 { get; set; }

            public string 国家 { get; set; } = "CN";
            public string 项目 { get; set; }

            public string 数量 { get; set; }
            public string 价税合计 { get; set; }

            // ----------------------------------------------------------------------
            public string 发票号 { get; set; }
            public string 发票类型 { get; set; } = "32-01";
            public string 客户打印名称 { get; set; }

            public string 开票类型 { get; set; }
            public string OA单据号 { get; set; }
            public string 合同业务类型 { get; set; }

            public string 扣税类别 { get; set; } = "1";
            public string 汇率 { get; set; } = "1";
            public string 折扣 { get; set; } = "100";

            public string 销售订单单据号 { get; set; } // ---------
            public string 销售订单主表id { get; set; } // ---------

            public string 销售发票主表id { get; set; } // ---------

            public string 年份 { get; set; }
            public string 月份 { get; set; }


            public List<OrderB> 明细 { get; set; }

            public class OrderB
            {
                public string 物料 { get; set; }
                public string 计量单位 { get; set; }

                public string 换算率 { get; set; } = "1.00/1.00";
                public string 数量 { get; set; }
                public string 税码 { get; set; }
                public string 税率 { get; set; }

                public string 含税单价 { get; set; }
                public string 无税单价 { get; set; }
                public string 含税净价 { get; set; }
                public string 无税净价 { get; set; }
                public string 税额 { get; set; }
                public string 无税金额 { get; set; }
                public string 价税合计 { get; set; }

                // ----------------------------------------------
                public string 开票内容 { get; set; }
                public string 行id { get; set; }

                public string 销售订单子表id { get; set; } // ---------

                public string 销售发票子表id { get; set; } // ---------
            }
        }

        public class ImportXml_应收单
        {
            /// <summary>
            /// 模板头部份
            /// </summary>
            /// <returns></returns>
            public static XElement TemplateHeade()
            {

                return new XElement(new XElement("ufinterface", new object[]
                {
                new XAttribute("sender","dingding"),
                new XAttribute("roottag",""),
                new XAttribute("replace","Y"),
                new XAttribute("isexchange","Y"),
                new XAttribute("groupcode","0"),
                new XAttribute("filename",""),
                new XAttribute("billtype","F0"),
                new XAttribute("account","develop")
                }));
            }

            /// <summary>
            /// 组建表头xml
            /// </summary>
            /// <param name="item">表头参数</param>
            /// <returns></returns>
            public static XElement Header(DataModel item, string id)
            {
                var bodys = new List<XElement>();

                if (item.明细 != null && item.明细.Count > 0)
                {
                    var crowno = 10; // 行号

                    foreach (var itemData in item.明细)
                    {
                        bodys.Add(new XElement("item", new object[] {
                            new XElement("pk_group",item.集团),
                            new XElement("pk_org",item.组织),
                            new XElement("pk_pcorg",""),
                            new XElement("pk_fiorg",item.组织),
                            new XElement("sett_org",item.组织),
                            new XElement("so_org",item.组织),

                            new XElement("material",itemData.物料),
                            new XElement("customer",item.客户),
                            new XElement("postunit",itemData.计量单位),

                            new XElement("postpricenotax",itemData.无税单价),
                            new XElement("postquantity",itemData.数量),
                            new XElement("postprice",itemData.含税单价),

                            new XElement("pausetransact","N"),

                            new XElement("billdate",item.单据日期),
                            new XElement("pk_billtype",item.应收单单据类型),
                            new XElement("billclass",item.应收单单据大类),
                            new XElement("pk_tradetype",item.应收类型),
                            new XElement("busidate",item.单据日期),

                            new XElement("objtype","0"),
                            new XElement("rowno",crowno.ToString()),
                            new XElement("rowtype","0"),
                            new XElement("direction","1"),

                            new XElement("pk_currtype",item.币种),
                            new XElement("rate",item.汇率),

                            new XElement("money_de",itemData.价税合计),
                            new XElement("local_money_de",itemData.价税合计),
                            new XElement("quantity_de",itemData.数量),
                            new XElement("money_bal",itemData.价税合计),
                            new XElement("local_money_bal",itemData.价税合计),
                            new XElement("quantity_bal",itemData.数量),

                            new XElement("local_tax_de",itemData.税额),

                            new XElement("notax_de",itemData.无税金额),
                            new XElement("local_notax_de",itemData.无税金额),

                            new XElement("price",itemData.无税单价),
                            new XElement("taxprice",itemData.含税单价),
                            new XElement("taxrate",itemData.税率),

                            new XElement("top_billid",item.销售发票主表id),
                            // new XElement("top_itemid",itemData.销售发票子表id),
                            new XElement("top_billtype",item.销售发票单据大类),
                            new XElement("top_tradetype",item.销售发票单据类型),

                            new XElement("src_billid",item.销售订单主表id),
                            new XElement("src_itemid",itemData.销售订单子表id),
                            new XElement("src_billtype",item.销售订单单据大类),
                            new XElement("src_tradetype",item.销售订单单据类型),

                            new XElement("taxtype","1"),
                            new XElement("pk_payterm",item.收款协议),
                            new XElement("ordercubasdoc",item.客户),

                            new XElement("purchaseorder",item.销售订单单据号),
                            new XElement("invoiceno",item.发票号),

                            new XElement("def1",itemData.开票内容),
                            new XElement("def10",itemData.行id),

                            new XElement("grouprate","0"),
                            new XElement("globalrate","0"),
                            new XElement("groupdebit","0"),
                            new XElement("globaldebit","0"),
                            new XElement("groupbalance","0"),
                            new XElement("globalbalance","0"),
                            new XElement("groupnotax_de","0"),
                            new XElement("globalnotax_de","0"),
                            new XElement("occupationmny","0"),

                            new XElement("project",item.项目),

                            new XElement("rececountryid",item.国家),
                            new XElement("buysellflag","1"),
                            new XElement("taxcodeid",itemData.税码),
                            new XElement("caltaxmny",itemData.无税金额),
                        }));

                        crowno = crowno + 10;
                    }
                }

                return new XElement("bill", new object[]
                {
                    "",
                    new XAttribute("id",id),
                    new XElement("billhead", new object[]
                    {
                        new XElement("pk_group",item.集团),
                        new XElement("pk_org",item.组织),
                        new XElement("pk_fiorg",item.组织),
                        new XElement("pk_pcorg",""),
                        new XElement("sett_org",item.组织),

                        new XElement("isreded","N"),
                        new XElement("outbusitype",""),
                        new XElement("officialprintuser",""),
                        new XElement("officialprintdate",""),

                        new XElement("approver",""),
                        new XElement("taudittime",""),
                        new XElement("billmaker",item.制单人),
                        new XElement("dmakedate",item.制单时间),
                        new XElement("creator",item.创建人),
                        new XElement("creationtime",item.创建时间),
                        new XElement("modifier",""),
                        new XElement("modifiedtime",""),

                        new XElement("pk_billtype",item.应收单单据类型),
                        new XElement("custdelegate",""),
                        new XElement("pk_corp",""),
                        new XElement("pk_tradetype",item.应收类型),
                        new XElement("pk_tradetype",item.应收类型),

                        new XElement("ctrantypeid",item.销售发票单据类型),
                        new XElement("vtrantypecode",item.销售发票单据类型),
                        new XElement("pk_busitype",item.业务流程),
                        new XElement("billclass",item.应收单单据大类),

                        new XElement("accessorynum","0"),
                        new XElement("subjcode",""),
                        new XElement("isflowbill","N"),
                        new XElement("confirmuser",""),
                        new XElement("isinit","N"),

                        new XElement("billno",item.OA单据号),
                        new XElement("billdate",item.单据日期),

                        new XElement("syscode","0"),
                        new XElement("src_syscode","3"),
                        new XElement("billstatus","-1"),
                        new XElement("lastadjustuser",""),

                        new XElement("money",item.价税合计),
                        new XElement("local_money",item.价税合计),
                        new XElement("rate",""),
                        new XElement("billyear",item.年份),
                        new XElement("billperiod",item.月份),

                        new XElement("scomment",""),
                        new XElement("effectstatus","10"),
                        new XElement("effectuser",""),
                        new XElement("effectdate",""),
                        new XElement("lastapproveid",""),

                        new XElement("so_org",item.组织),
                        new XElement("customer",item.客户),
                        new XElement("pk_currtype",item.币种),

                        new XElement("sendcountryid",item.国家),
                        new XElement("taxcountryid",item.国家),

                        new XElement("coordflag",""),
                        new XElement("inner_effect_date",""),
                        new XElement("busidate",""),

                        new XElement("def2",item.发票号),
                        new XElement("def4",item.开票类型),
                        new XElement("def5",item.合同业务类型),

                        new XElement("bodys",bodys),
                    }),
                });
            }
        }
    }
}
