Beispiel #1
0
 public static void GeneralPaymentDelete(GeneralPayment gp, AriClinicContext ctx)
 {
     foreach (Payment p in gp.Payments)
     {
         PaymentDelete(p, ctx);
     }
     ctx.Delete(gp);
     ctx.SaveChanges();
 }
Beispiel #2
0
 public static void GeneralPaymentDelete(GeneralPayment gp, AriClinicContext ctx)
 {
     foreach (Payment p in gp.Payments)
     {
         PaymentDelete(p, ctx);
     }
     ctx.Delete(gp);
     ctx.SaveChanges();
 }
    protected void Page_Init(object sender, EventArgs e)
    {
        ctx = new AriClinicContext("AriClinicContext");
        // security control, it must be a user logged
        if (Session["User"] == null)
            Response.Redirect("Default.aspx");
        else
        {
            user = CntAriCli.GetUser((Session["User"] as User).UserId, ctx);
            Process proc = (from p in ctx.Processes
                            where p.Code == "payment"
                            select p).FirstOrDefault<Process>();
            per = CntAriCli.GetPermission(user.UserGroup, proc, ctx);
            btnAccept.Visible = per.Modify;
        }
        if (Session["Clinic"] != null)
            cl = (Clinic)Session["Clinic"];
        // 

        //
        if (Request.QueryString["ServiceNoteId"] != null)
        {
            serviceNoteId = int.Parse(Request.QueryString["ServiceNoteId"]);
            snote = CntAriCli.GetServiceNote(serviceNoteId, ctx);
            // calcute total amount and total payments
            //txtServiceNoteData.Text = String.Format("ID:{0} Fecha:{1:dd/MM/yy} Total:{2} Pagado:{3}", 
            //    snote.ServiceNoteId, 
            //    snote.ServiceNoteDate,
            //    CntAriCli.GetServiceNoteAmount(snote), 
            //    CntAriCli.GetServiceNoteAmountPay(snote));
            txtServiceNoteData.Text = String.Format("{0} ({1:dd/MM/yy}) T:{2:0.00} P:{3:0.00}", snote.Customer.ComercialName, snote.ServiceNoteDate, snote.Total, snote.Paid);
            txtAmount.Value = (double)CntAriCli.GetUnpaid(snote, ctx);
            //txtAmount.Text = string.Format("{0:#.#}", CntAriCli.GetUnpaid(snote, ctx));
            SetFocus(rdcbClinic);
        }

        if (Request.QueryString["GeneralPaymentId"] != null)
        {
            paymentId = Int32.Parse(Request.QueryString["GeneralPaymentId"]);
            gpay = CntAriCli.GetGeneralPayment(paymentId, ctx);
            LoadData(gpay);
        }
        else
        {
            rddpGeneralPaymentDate.SelectedDate = DateTime.Now;
            LoadPaymentMethodCombo(null);
            LoadClinicCombo(null);
        }
    }
Beispiel #4
0
        public static GeneralPayment GeneralPaymentNew(Clinic clinic, ServiceNote sn, decimal amount, PaymentMethod payMethod, DateTime payDate, string description, AriClinicContext ctx)
        {
            var rs = from t in sn.Tickets
                     where t.Amount > t.Paid
                     select t;
            GeneralPayment gp = new GeneralPayment();

            gp.ServiceNote   = sn;
            gp.PaymentDate   = payDate;
            gp.Description   = description;
            gp.PaymentMethod = payMethod;
            gp.Amount        = amount;
            gp.Clinic        = clinic;
            ctx.Add(gp);
            foreach (Ticket t in rs.OrderByDescending(tk => tk.Amount - tk.Paid))
            {
                Payment pay = new Payment();
                pay.PaymentMethod  = payMethod;
                pay.PaymentDate    = payDate;
                pay.Ticket         = t;
                pay.GeneralPayment = gp;
                pay.Description    = description;
                pay.Clinic         = clinic;
                decimal dif = t.Amount - t.Paid;
                if (dif <= amount)
                {
                    pay.Amount = dif;
                    amount     = amount - dif;
                    t.Paid     = t.Paid + dif;
                }
                else
                {
                    pay.Amount = amount;
                    t.Paid     = t.Paid + amount;
                    amount     = 0;
                }
                ctx.Add(pay);
                if (amount == 0)
                {
                    break;
                }
            }
            ctx.SaveChanges();
            return(gp);
        }
Beispiel #5
0
 public static GeneralPayment GeneralPaymentNew(Clinic clinic, ServiceNote sn, decimal amount, PaymentMethod payMethod, DateTime payDate, string description, AriClinicContext ctx)
 {
     var rs = from t in sn.Tickets
              where t.Amount > t.Paid
              select t;
     GeneralPayment gp = new GeneralPayment();
     gp.ServiceNote = sn;
     gp.PaymentDate = payDate;
     gp.Description = description;
     gp.PaymentMethod = payMethod;
     gp.Amount = amount;
     gp.Clinic = clinic;
     ctx.Add(gp);
     foreach (Ticket t in rs.OrderByDescending(tk => tk.Amount - tk.Paid))
     {
         Payment pay = new Payment();
         pay.PaymentMethod = payMethod;
         pay.PaymentDate = payDate;
         pay.Ticket = t;
         pay.GeneralPayment = gp;
         pay.Description = description;
         pay.Clinic = clinic;
         decimal dif = t.Amount - t.Paid;
         if (dif <= amount)
         {
             pay.Amount = dif;
             amount = amount - dif;
             t.Paid = t.Paid + dif;
         }
         else
         {
             pay.Amount = amount;
             t.Paid = t.Paid + amount;
             amount = 0;
         }
         ctx.Add(pay);
         if (amount == 0) break;
     }
     ctx.SaveChanges();
     return gp;
 }
 protected void Page_Init(object sender, EventArgs e)
 {
     ctx = new AriClinicContext("AriClinicContext");
     if (Session["User"] == null) Response.Redirect("Default.aspx");
     else
     {
         user = CntAriCli.GetUser((Session["User"] as User).UserId, ctx);
         Process proc = (from p in ctx.Processes
                         where p.Code == "payment"
                         select p).FirstOrDefault<Process>();
         permission = CntAriCli.GetPermission(user.UserGroup, proc, ctx);
         btnAccept.Visible = permission.Modify;
     }
     if (Session["Clinic"] != null)
     {
         clinic = (Clinic)Session["Clinic"];
     }
     if (Request.QueryString["ServiceNoteId"] != null)
     {
         serviceNoteId = int.Parse(Request.QueryString["ServiceNoteId"]);
         serviceNote = CntAriCli.GetServiceNote(serviceNoteId, ctx);
         txtServiceNoteData.Text = String.Format("{0} ({1:dd/MM/yy}) T:{2:0.00} P:{3:0.00}", serviceNote.Customer.ComercialName, serviceNote.ServiceNoteDate, serviceNote.Total, serviceNote.Paid);
         txtAmount.Value = (double)CntAriCli.GetUnpaid(serviceNote, ctx);
         if (serviceNote.Clinic != null) clinic = serviceNote.Clinic;
         SetFocus(rdcbClinic);
     }
     if (Request.QueryString["GeneralPaymentId"] != null)
     {
         generalPaymentId = Int32.Parse(Request.QueryString["GeneralPaymentId"]);
         generalPayment = CntAriCli.GetGeneralPayment(generalPaymentId, ctx);
         serviceNote = generalPayment.ServiceNote;
         LoadData(generalPayment);
     }
     else
     {
         rddpGeneralPaymentDate.SelectedDate = DateTime.Now;
         LoadPaymentMethodCombo(null);
         LoadClinicCombo(clinic);
     }
 }
Beispiel #7
0
 public static bool PayNote(PaymentMethod pm, Decimal amount, DateTime dt, string des, ServiceNote note, Clinic cl, GeneralPayment gp, AriClinicContext ctx)
 {
     Payment p = null; // payment to be created
     Ticket t = null; // related ticket
     //(0) Control if amount > pending in note
     decimal total_paid = 0;
     foreach (Ticket tk in note.Tickets)
         total_paid += tk.Paid;
     if ((note.Total - total_paid) < amount)
         return false; // amount bigger than debt.
          
     //(1) Look for a ticket (inside note) with the same amount
     t = (from tk in note.Tickets
          where (tk.Amount - tk.Paid) == amount
          select tk).FirstOrDefault<Ticket>();
     if (t != null)
     {
         // (1.1) It exists.
         CreatePayment(t, pm, amount, dt, des, note, cl, gp, ctx);
     }
     else
     {
         // (1.2) It doesn't exist
         var rs = from tk in note.Tickets
                  orderby (tk.Amount - tk.Paid)
                  select tk;
         foreach (Ticket tk in rs)
         {
             if (tk.Amount - tk.Paid >= amount)
             {
                 CreatePayment(tk, pm, amount, dt, des, note, cl, gp, ctx);
                 break; // out
             }
             else
             {
                 decimal paid = tk.Amount - tk.Paid;
                 amount = amount - paid;
                 CreatePayment(tk, pm, paid, dt, des, note, cl, gp, ctx);
             }
         }
     }
     ctx.SaveChanges();
     return true;
 }
 protected void LoadClinicCombo(GeneralPayment gp)
 {
     Clinic clinic = null;
     if (gp != null) clinic = gp.Clinic;
     rdcbClinic.Items.Clear();
     foreach(Clinic c in CntAriCli.GetClinics(ctx))
     {
         rdcbClinic.Items.Add(new RadComboBoxItem(c.Name, c.ClinicId.ToString()));
     }
     if (clinic != null)
     {
         rdcbClinic.SelectedValue = clinic.ClinicId.ToString();
     }
     else
     {
         rdcbClinic.Items.Add(new RadComboBoxItem(" ", ""));
         rdcbClinic.SelectedValue = "";
     }
 }
 protected void LoadPaymentMethodCombo(GeneralPayment gp)
 {
     PaymentMethod pm = null;
     if (gp != null)
         pm = gp.PaymentMethod;
     // clear previous items 
     rdcbPaymentMethod.Items.Clear();
     foreach (PaymentMethod pm2 in ctx.PaymentMethods)
     {
         rdcbPaymentMethod.Items.Add(new RadComboBoxItem(pm2.Name, pm2.PaymentMethodId.ToString()));
     }
     if (pm != null)
     {
         rdcbPaymentMethod.SelectedValue = pm.PaymentMethodId.ToString();
     }
     else
     {
         rdcbPaymentMethod.Items.Add(new RadComboBoxItem(" ", ""));
         rdcbPaymentMethod.SelectedValue = "";
     }
 }
 protected void UnloadData(GeneralPayment pay)
 {
     if (pay != null)
     {
         snote = pay.ServiceNote;
         CntAriCli.GeneralPaymentDelete(pay, ctx);
     }
     Clinic clinic = CntAriCli.GetClinic(int.Parse(rdcbClinic.SelectedValue), ctx);
     PaymentMethod payMethod = CntAriCli.GetPaymentMethod(int.Parse(rdcbPaymentMethod.SelectedValue), ctx);
     DateTime payDate = (DateTime)rddpGeneralPaymentDate.SelectedDate;
     Decimal amount = (decimal)txtAmount.Value;
     string description = txtComments.Text;
     pay = CntAriCli.GeneralPaymentNew(clinic, snote, amount, payMethod, payDate, description, ctx);
 }
 protected void LoadData(GeneralPayment pay)
 {
     LoadClinicCombo(pay);
     LoadPaymentMethodCombo(pay);
     if (pay != null)
     {
         txtGeneralPaymentId.Text = pay.GeneralPaymentId.ToString();
         txtAmount.Value = (double)pay.Amount;
         //txtAmount.Text = String.Format("{0:#.#}", pay.Amount);
         txtServiceNoteData.Text = String.Format("{0} ({1:dd/MM/yy})", pay.ServiceNote.Customer.ComercialName, pay.ServiceNote.ServiceNoteDate);
     }
     else
     {
         txtServiceNoteData.Text = String.Format("{0} ({1:dd/MM/yy})", snote.Customer.ComercialName, snote.ServiceNoteDate);
         txtAmount.Value = (double)CntAriCli.GetUnpaid(snote, ctx);
         //txtAmount.Text = string.Format("{0:#.#}", CntAriCli.GetUnpaid(snote, ctx));
     }
 }
    protected bool CreateChange()
    {
        if (!DataOk())
            return false;
        if (gpay == null)
        {
            UnloadData(gpay);
        }
        else
        {
            gpay = CntAriCli.GetGeneralPayment(paymentId, ctx);
            UnloadData(gpay);
        }
        // update payments in related ticket

        return true;
    }
 protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
 {
     // weonly process commands with a datasource (our image buttons)
     if (e.CommandSource == null)
         return;
     string typeOfControl = e.CommandSource.GetType().ToString();
     if (typeOfControl.Equals("System.Web.UI.WebControls.ImageButton"))
     {
         int id = 0;
         ImageButton imgb = (ImageButton)e.CommandSource;
         if (imgb.ID != "New" && imgb.ID != "Exit")
             id = (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][e.Item.OwnerTableView.DataKeyNames[0]];
         switch (imgb.ID)
         {
             case "Select":
                 break;
             case "Edit":
                 break;
             case "Delete":
                 pay = (from p in ctx.GeneralPayments
                        where p.GeneralPaymentId == id
                        select p).FirstOrDefault<GeneralPayment>();
                 CntAriCli.GeneralPaymentDelete(pay, ctx);
                 RefreshGrid(true);
                 break;
         }
     }
 }
Beispiel #14
0
        public static void ImportPayments(OleDbConnection con, AriClinicContext ctx)
        {
            //(1) Borrar antiguos pagos
            ctx.Delete(ctx.Payments);
            foreach (Ticket tt in ctx.Tickets)
            {
                tt.Paid = 0;
            }
            ctx.SaveChanges();
            //(2) Obtener la clínica por defecto
            Clinic cl = ctx.Clinics.FirstOrDefault<Clinic>();

            //(3) Leer todos los pagos y darlos de alta.
            string sql = "SELECT * FROM LinNotaPago";
            cmd = new OleDbCommand(sql, con);
            da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "ConPagos");
            foreach (DataRow dr in ds.Tables["ConPagos"].Rows)
            {
                int id = (int)dr["IdFormaPago"];
                PaymentMethod pm = (from p in ctx.PaymentMethods
                                    where p.OftId == id
                                    select p).FirstOrDefault<PaymentMethod>();
                int idAno = (int)dr["Ano"];
                int idNumNota = (int)dr["NumNota"];
                ServiceNote note = (from n in ctx.ServiceNotes
                                    where n.Oft_Ano == idAno && n.Oft_NumNota == idNumNota
                                    select n).FirstOrDefault<ServiceNote>();
                GeneralPayment gp = new GeneralPayment();
                gp.PaymentMethod = pm;
                gp.Amount = (decimal)dr["Importe"];
                gp.PaymentDate = (DateTime)dr["Fecha"];
                gp.ServiceNote = note;
                gp.Clinic = cl;
                gp.Description = (string)dr["Descripcion"];
                ctx.Add(gp);
                bool res = CntAriCli.PayNote(pm, (decimal)dr["Importe"], (DateTime)dr["Fecha"], (string)dr["Descripcion"],note, cl, gp, ctx);
                if (!res)
                {
                }
            }
        }
Beispiel #15
0
 public static void CreatePayment(Ticket t, PaymentMethod pm, Decimal amount, DateTime dt, string des, ServiceNote note, Clinic cl, GeneralPayment gp, AriClinicContext ctx)
 {
     // Now we need verify if there's a payment yet with the same values
     Payment p = new Payment();
     p.Amount = amount;
     p.Clinic = cl;
     p.PaymentDate = dt;
     p.PaymentMethod = pm;
     p.GeneralPayment = gp;
     p.Description = des;
     p.Ticket = t;
     t.Paid = t.Paid + amount;
     ctx.Add(p);
 }
Beispiel #16
0
        public static void ImportPayments(OleDbConnection con, AriClinicContext ctx)
        {
            //(1) Borrar antiguos pagos
            ctx.Delete(ctx.GeneralPayments);
            ctx.Delete(ctx.Payments);
            ctx.SaveChanges();

            foreach (Ticket tt in ctx.Tickets)
            {
                tt.Paid = 0;
            }
            ctx.SaveChanges();
            //(2) Obtener la clínica por defecto
            Clinic cl = ctx.Clinics.FirstOrDefault<Clinic>();

            //(3) Leer todos los pagos y darlos de alta.
            string sql = "SELECT * FROM LinNotaPago";
            cmd = new OleDbCommand(sql, con);
            da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "ConPagos");
            int nreg = ds.Tables["ConPagos"].Rows.Count;
            int reg = 0;
            foreach (DataRow dr in ds.Tables["ConPagos"].Rows)
            {
                reg++;
                Console.WriteLine("Formas de pago {0:#####0} de {1:#####0} {2}", reg, nreg, "PAGOS");
                int id = (int)dr["IdFormaPago"];
                PaymentMethod pm = (from p in ctx.PaymentMethods
                                    where p.OftId == id
                                    select p).FirstOrDefault<PaymentMethod>();
                int idAno = (int)dr["Ano"];
                int idNumNota = (int)dr["NumNota"];
                ServiceNote note = (from n in ctx.ServiceNotes
                                    where n.Oft_Ano == idAno && n.Oft_NumNota == idNumNota
                                    select n).FirstOrDefault<ServiceNote>();
                // we create a general payment too
                GeneralPayment gp = (from gpp in ctx.GeneralPayments
                                     where gpp.ServiceNote.ServiceNoteId == note.ServiceNoteId
                                     && gpp.PaymentDate == (DateTime)dr["Fecha"]
                                     && gpp.PaymentMethod.PaymentMethodId == pm.PaymentMethodId
                                     && gpp.Amount == (decimal)dr["Importe"]
                                     select gpp).FirstOrDefault<GeneralPayment>();
                if (gp == null)
                {
                    gp = new GeneralPayment();
                    gp.Clinic = cl;
                    ctx.Add(gp);
                }
                gp.Amount = (decimal)dr["Importe"];
                gp.ServiceNote = note;
                gp.PaymentDate = (DateTime)dr["Fecha"];
                gp.PaymentMethod = pm;
                gp.Description = (string)dr["Descripcion"];
                note.Paid = note.Paid + gp.Amount;
                ctx.Delete(gp.Payments);
                bool res = CntAriCli.PayNote(pm, (decimal)dr["Importe"], (DateTime)dr["Fecha"], (string)dr["Descripcion"], note, gp.Clinic, gp, ctx);
                if (!res)
                {
                }
            }
        }
 protected bool DataOk(GeneralPayment gpy)
 {
     decimal actValue = 0;
     decimal oldValue = 0;
     actValue = (decimal)txtAmount.Value;
     if (gpy != null) oldValue = gpy.Amount;
     if (actValue > CntAriCli.GetUnpaid(serviceNote, ctx) + oldValue)
     {
         return false;
     }
     return true;
 }
 protected void LoadData(GeneralPayment pay)
 {
     LoadClinicCombo(pay.Clinic);
     LoadPaymentMethodCombo(pay.PaymentMethod);
     rddpGeneralPaymentDate.SelectedDate = pay.PaymentDate;
     if (pay != null)
     {
         txtGeneralPaymentId.Text = pay.GeneralPaymentId.ToString();
         txtAmount.Value = (double)pay.Amount;
         txtServiceNoteData.Text = String.Format("{0} ({1:dd/MM/yy})", pay.ServiceNote.Customer.ComercialName, pay.ServiceNote.ServiceNoteDate);
     }
     else
     {
         txtServiceNoteData.Text = String.Format("{0} ({1:dd/MM/yy})", serviceNote.Customer.ComercialName, serviceNote.ServiceNoteDate);
         txtAmount.Value = (double)CntAriCli.GetUnpaid(serviceNote, ctx);
     }
     txtComments.Text = pay.Description;
 }