using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Aram_CRM_Project.UIL
{
    public partial class frmPrintBill : Form
    {
        public frmPrintBill()
        {
            InitializeComponent();
        }
        private string _Mode = string.Empty;
        private string _Id = string.Empty;
        private string _Date = string.Empty;
        private string _PayerName = string.Empty;

        public string PayerName
        {
            set { _PayerName = value; }
            get { return _PayerName; }
        }

        public string Date
        {
            set { _Date = value; }
            get { return _Date; }
        }

        public string Id
        {
            set { _Id = value; }
            get { return _Id; }
        }
        public string Mode
        {
            set { _Mode = value; }
            get { return _Mode; }
        }

        private void PrintBill()
        {
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            switch (Mode)
            {
                case "ReserveBill":
                    {
                        try
                        {
                            this.Cursor = Cursors.WaitCursor;
                            

                            Aram_CRM_Project.BLL.ConvertDigit DigitConvert = new Aram_CRM_Project.BLL.ConvertDigit();
                            Aram_CRM_Project.BLL.ReserveCRUD ReserveCrud = new Aram_CRM_Project.BLL.ReserveCRUD();
                            Aram_CRM_Project.CrystalReport.rptReserveBill RptReserveBill = new Aram_CRM_Project.CrystalReport.rptReserveBill();
                            DataTable dtReserve = new DataTable();
                            DataTable dtReserveStuff = new DataTable();
                            DataTable dtReserveCheck = new DataTable();
                            long TotalPrice = 0;
                            long ReservePrice = 0;
                            long CashPrice = 0;
                            long CheckPrice = 0;
                            long RemainPrice = 0;
                            long Discount = 0;

                            dtReserve = ReserveCrud.ReportReserveBill(Id);
                            dtReserveStuff = ReserveCrud.ReportReserveStuffBill(Id);

                            RptReserveBill.SetDataSource(dtReserveStuff);
                            
                            if (dtReserveStuff.Rows[0][8].ToString() == "")
                            {
                                RptReserveBill.Section1.ReportObjects["PicBill1"].ObjectFormat.EnableSuppress = true;
                                RptReserveBill.Section1.ReportObjects["PicBill2"].ObjectFormat.EnableSuppress = true;
                            }
                            else
                            {
                                if (((byte[])dtReserveStuff.Rows[0][8]).Length == 0)
                                {
                                    RptReserveBill.Section1.ReportObjects["PicBill1"].ObjectFormat.EnableSuppress = true;
                                    RptReserveBill.Section1.ReportObjects["PicBill2"].ObjectFormat.EnableSuppress = true;
                                }
                            
                            }
                            

                            RptReserveBill.SetParameterValue("ReserveId", dtReserve.Rows[0]["Id"].ToString());
                            RptReserveBill.SetParameterValue("ReserveId2", dtReserve.Rows[0]["Id"].ToString());
                            RptReserveBill.SetParameterValue("Name", dtReserve.Rows[0]["Name"].ToString());
                            RptReserveBill.SetParameterValue("ReserveDate", dtReserve.Rows[0]["Date"].ToString());
                            RptReserveBill.SetParameterValue("ReserveDate2", dtReserve.Rows[0]["Date"].ToString());
                            RptReserveBill.SetParameterValue("CustomerGroup", dtReserve.Rows[0]["CustomerGroupName"].ToString());
                            RptReserveBill.SetParameterValue("CityState", dtReserve.Rows[0]["CityState"].ToString());
                            RptReserveBill.SetParameterValue("Tel", dtReserve.Rows[0]["Tel"].ToString());
                            RptReserveBill.SetParameterValue("Address", dtReserve.Rows[0]["Address"].ToString());
                            RptReserveBill.SetParameterValue("PayerName1", dtReserve.Rows[0]["PayerName"].ToString());
                            RptReserveBill.SetParameterValue("PayerName2", dtReserve.Rows[0]["PayerName"].ToString());
                            RptReserveBill.SetParameterValue("ReceptionName1", dtReserve.Rows[0]["ReceptionName"].ToString());
                            RptReserveBill.SetParameterValue("ReceptionName2", dtReserve.Rows[0]["ReceptionName"].ToString());
                            RptReserveBill.SetParameterValue("PayTypeName", dtReserve.Rows[0]["PayTypeName"].ToString());
                            
                            for (int i = 0; i < dtReserveStuff.Rows.Count; i++)
                            {
                                TotalPrice += long.Parse(dtReserveStuff.Rows[i][6].ToString().Replace(",", ""));
                                Discount += long.Parse(dtReserveStuff.Rows[i][5].ToString().Replace(",", ""));
                            }
                            RptReserveBill.SetParameterValue("TotalPrice1", utility.ThousandSeparator(TotalPrice.ToString()));
                            RptReserveBill.SetParameterValue("TotalPrice2", DigitConvert.ConvertRaghamToHorof(TotalPrice) + " " + "ریال");

                            CashPrice = long.Parse(dtReserve.Rows[0]["Price"].ToString());
                            RptReserveBill.SetParameterValue("CashPrice", utility.ThousandSeparator(CashPrice.ToString()) + " " + "ریال");
                            
                            ReservePrice = long.Parse(dtReserve.Rows[0]["Price"].ToString());
                            if (dtReserve.Rows[0]["PayTypeId"].ToString() == "2" || dtReserve.Rows[0]["PayTypeId"].ToString() == "3")
                            {
                                dtReserveCheck = ReserveCrud.ReadReserveCheck(Id);
                                if (dtReserveCheck != null)
                                {
                                    for (int j = 0; j < dtReserveCheck.Rows.Count; j++)
                                    {
                                        ReservePrice += long.Parse(dtReserveCheck.Rows[j][6].ToString().Replace(",", ""));
                                        CheckPrice += long.Parse(dtReserveCheck.Rows[j][6].ToString().Replace(",", ""));
                                    }
                                }
                            }

                            RptReserveBill.SetParameterValue("CheckPrice", utility.ThousandSeparator(CheckPrice.ToString()) + " " + "ریال");

                            string Price = utility.ThousandSeparator(ReservePrice.ToString());
                            RptReserveBill.SetParameterValue("PrePrice1", Price + " ریال");
                            RptReserveBill.SetParameterValue("PrePrice2", Price + " ریال");
                            RptReserveBill.SetParameterValue("PrePrice3", Price + " ریال");
                            
                            RemainPrice = TotalPrice - ReservePrice;
                            if (RemainPrice < 0)
                            {
                                RemainPrice = -1 * RemainPrice;
                                RptReserveBill.SetParameterValue("RemainPrice", utility.ThousandSeparator(RemainPrice.ToString()) + " -  ریال می باشد .");
                            }
                            else
                            {
                                RptReserveBill.SetParameterValue("RemainPrice", utility.ThousandSeparator(RemainPrice.ToString()) + " ریال");
                            }
                            
                            RptReserveBill.SetParameterValue("PrePriceChar1", DigitConvert.ConvertRaghamToHorof(ReservePrice) + " " + "ریال");
                            RptReserveBill.SetParameterValue("PrePriceChar2", DigitConvert.ConvertRaghamToHorof(ReservePrice) + " " + "ریال");

                            crvPrint.ReportSource = RptReserveBill;

                            this.Text = "  پیش نمایش";
                            this.Cursor = Cursors.Default;
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show(exp.Message);
                        }

                        break;
                    }
                case "OrderBill":
                    {
                        try
                        {
                            this.Cursor = Cursors.WaitCursor;
                            

                            Aram_CRM_Project.BLL.ConvertDigit DigitConvert = new Aram_CRM_Project.BLL.ConvertDigit();
                            Aram_CRM_Project.BLL.OrderCRUD OrderCrud = new Aram_CRM_Project.BLL.OrderCRUD();
                            Aram_CRM_Project.CrystalReport.rptOrderBill RptOrderBill = new Aram_CRM_Project.CrystalReport.rptOrderBill();
                            DataTable dtOrder = new DataTable();
                            DataTable dtOrderStuff = new DataTable();
                            DataTable dtOrderCheck = new DataTable();
                            long TotalPrice = 0;
                            long ReservePrice = 0;
                            long CashPrice = 0;
                            long CheckPrice = 0;
                            long RemainPrice = 0;
                            long Discount = 0;

                            dtOrder = OrderCrud.ReportOrderBill(Id);
                            dtOrderStuff = OrderCrud.ReportOrderStuffBill(Id);

                            RptOrderBill.SetDataSource(dtOrderStuff);

                            if (dtOrderStuff.Rows[0][8].ToString() == "")
                            {
                                RptOrderBill.Section1.ReportObjects["PicBill1"].ObjectFormat.EnableSuppress = true;
                            }
                            else
                            {
                                if (((byte[])dtOrderStuff.Rows[0][8]).Length == 0)
                                    RptOrderBill.Section1.ReportObjects["PicBill1"].ObjectFormat.EnableSuppress = true;
                            }
                            

                            //RptOrderBill.SetParameterValue("", dtReserve.Rows[0][""].ToString());
                            RptOrderBill.SetParameterValue("OrderId", dtOrder.Rows[0]["Id"].ToString());
                            RptOrderBill.SetParameterValue("Date", dtOrder.Rows[0]["Date"].ToString());
                            RptOrderBill.SetParameterValue("Name", dtOrder.Rows[0]["Name"].ToString());
                            RptOrderBill.SetParameterValue("CustomerGroup", dtOrder.Rows[0]["CustomerGroup"].ToString());
                            RptOrderBill.SetParameterValue("Tel", dtOrder.Rows[0]["Tel"].ToString());
                            RptOrderBill.SetParameterValue("Cell", dtOrder.Rows[0]["Cell"].ToString());
                            RptOrderBill.SetParameterValue("Address", dtOrder.Rows[0]["Address"].ToString());
                            RptOrderBill.SetParameterValue("CityState", dtOrder.Rows[0]["CityState"].ToString());

                            for (int i = 0; i < dtOrderStuff.Rows.Count; i++)
                            {
                                TotalPrice += long.Parse(dtOrderStuff.Rows[i][6].ToString().Replace(",", ""));
                                Discount += long.Parse(dtOrderStuff.Rows[i][5].ToString().Replace(",", ""));
                            }

                            RptOrderBill.SetParameterValue("TotalPrice1", utility.ThousandSeparator(TotalPrice.ToString()));
                            RptOrderBill.SetParameterValue("TotalPrice2", DigitConvert.ConvertRaghamToHorof(TotalPrice) + " " + "ریال");
                            RptOrderBill.SetParameterValue("Discount", utility.ThousandSeparator(Discount.ToString()) + " " + "ریال");

                            CashPrice = long.Parse(dtOrder.Rows[0]["Price"].ToString());
                            RptOrderBill.SetParameterValue("CashPrice", utility.ThousandSeparator(CashPrice.ToString()) + " " + "ریال");

                            //ReservePrice = long.Parse(dtOrder.Rows[0]["Price"].ToString())
                            if (dtOrder.Rows[0]["ReserveId"].ToString() != "")
                                ReservePrice = long.Parse(dtOrder.Rows[0]["ReservePrice"].ToString());

                            if (dtOrder.Rows[0]["PayTypeId"].ToString() == "2" || dtOrder.Rows[0]["PayTypeId"].ToString() == "3")
                            {
                                dtOrderCheck = OrderCrud.ReadOrderCheck(Id);
                                if (dtOrderCheck != null)
                                {
                                    for (int j = 0; j < dtOrderCheck.Rows.Count; j++)
                                    {
                                        //ReservePrice += long.Parse(dtOrderCheck.Rows[j][6].ToString().Replace(",", ""));
                                        CheckPrice += long.Parse(dtOrderCheck.Rows[j][6].ToString().Replace(",", ""));
                                    }
                                }
                            }

                            RptOrderBill.SetParameterValue("CheckPrice", utility.ThousandSeparator(CheckPrice.ToString()) + " " + "ریال");
                            string st = ReservePrice.ToString();
                            RptOrderBill.SetParameterValue("PrePrice", utility.ThousandSeparator(ReservePrice.ToString()) + " " + "ریال");

                            RemainPrice = TotalPrice - ReservePrice;
                            if (RemainPrice < 0)
                            {
                                RemainPrice = -1 * RemainPrice;
                                RptOrderBill.SetParameterValue("RemainPrice", utility.ThousandSeparator(RemainPrice.ToString()) + " -  ریال می باشد .");
                            }
                            else
                            {
                                RptOrderBill.SetParameterValue("RemainPrice", utility.ThousandSeparator(RemainPrice.ToString()) + " ریال می باشد .");
                            }

                            crvPrint.ReportSource = RptOrderBill;

                            this.Text = "  پیش نمایش";
                            this.Cursor = Cursors.Default;
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show(exp.Message);
                        }

                        break;
                    }
                case "ReserveCheckBill":
                    {
                        try
                        {
                            

                            Aram_CRM_Project.BLL.ReserveCRUD ReserveCrud = new Aram_CRM_Project.BLL.ReserveCRUD();
                            Aram_CRM_Project.CrystalReport.rptReserveCheckBill RptReserveCheckBill = new Aram_CRM_Project.CrystalReport.rptReserveCheckBill();
                            DataTable dtReserveCheck = new DataTable();

                            dtReserveCheck = ReserveCrud.ReportReserveCheckBill(Id);

                            RptReserveCheckBill.SetDataSource(dtReserveCheck);

                            //RptReserveCheckBill.SetParameterValue("", );
                            RptReserveCheckBill.SetParameterValue("Count", dtReserveCheck.Rows.Count);
                            RptReserveCheckBill.SetParameterValue("ReserveId", Id);
                            RptReserveCheckBill.SetParameterValue("Date", Date);
                            RptReserveCheckBill.SetParameterValue("Name", PayerName);

                            crvPrint.ReportSource = RptReserveCheckBill;

                            this.Text = "  پیش نمایش";
                            this.Cursor = Cursors.Default;
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show(exp.Message);
                        }

                        break;
                    }
                case "OrderCheckBill":
                    {
                        try
                        {

                            Aram_CRM_Project.BLL.OrderCRUD OrderCrud = new Aram_CRM_Project.BLL.OrderCRUD();
                            Aram_CRM_Project.CrystalReport.rptOrderCheckBill RptOrderCheckBill = new Aram_CRM_Project.CrystalReport.rptOrderCheckBill();
                            DataTable dtOrderCheck = new DataTable();

                            dtOrderCheck = OrderCrud.ReportOrderCheckBill(Id);

                            RptOrderCheckBill.SetDataSource(dtOrderCheck);

                            //RptReserveCheckBill.SetParameterValue("", );
                            RptOrderCheckBill.SetParameterValue("Count", dtOrderCheck.Rows.Count);
                            RptOrderCheckBill.SetParameterValue("OrderId", Id);
                            RptOrderCheckBill.SetParameterValue("Date", Date);
                            RptOrderCheckBill.SetParameterValue("Name", PayerName);

                            crvPrint.ReportSource = RptOrderCheckBill;

                            this.Text = "  پیش نمایش";
                            this.Cursor = Cursors.Default;
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show(exp.Message);
                        }

                        break;
                    }
            }
        }

        private void frmPrintBill_Load(object sender, EventArgs e)
        {

            PrintBill();
        }



    }
}
