Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Activities.Hosting;
  7. using System.Activities;
  8. using System.Activities.Statements;
  9. using PapaCarloDBApp;
  10.  
  11.  
  12. namespace PapaCarlo
  13. {
  14.     public sealed class Shipment : CodeActivity
  15.     {
  16.         [RequiredArgument]
  17.         public InArgument<ContractShipment> contr { get; set; }
  18.  
  19.         [RequiredArgument]
  20.         public InArgument<QueryContractShipment> query { get; set; }
  21.  
  22.         [RequiredArgument]
  23.         public InArgument<int> Id { get; set; }
  24.  
  25.         [RequiredArgument]
  26.         public InArgument<int> oldAmount { get; set; }
  27.  
  28.         [RequiredArgument]
  29.         public InArgument<int> storeId { get; set; }
  30.  
  31.         public OutArgument<bool> Result { get; set; }
  32.  
  33.         public List<ReportBalanceTable> querySelectReportBalanceByShip(ContractShipment co, int storeId)
  34.         {
  35.            
  36.                 using (DataBaseContext db = new DataBaseContext())
  37.                 {
  38.                     var query = from c in db.ReportBalances
  39.                                 join sc in db.StoreCells on c.StoreCellId equals sc.Id
  40.                                 join sh in db.Storehouses on sc.StorehouseId equals sh.Id
  41.                                 join p in db.Products on c.ProductId equals p.Id
  42.                                 select new { c, p, sc, sh };
  43.  
  44.                     List<ReportBalanceTable> table = new List<ReportBalanceTable>();
  45.  
  46.                     foreach (var item in query)
  47.                     {
  48.                         if (co.ProductId == item.p.Id && co.StoreCellFromId == item.sc.Id
  49.                             && item.sh.Id == storeId)
  50.                         table.Add(new ReportBalanceTable(item.c, item.p, item.sc, item.sh));
  51.                     }
  52.                     return table;
  53.                 }
  54.         }
  55.  
  56.         public bool queryUpdateReportBalance(ReportBalance c)
  57.         {
  58.             using (DataBaseContext db = new DataBaseContext())
  59.             {
  60.                 try
  61.                 {
  62.                     ReportBalance p = db.ReportBalances.Find(c.Id);
  63.  
  64.                     p.Amount = c.Amount;
  65.                     p.ProductId = c.ProductId;
  66.                     p.Date = c.Date;
  67.                     p.StoreCellId = c.StoreCellId;
  68.  
  69.                     db.SaveChanges();
  70.                 }
  71.                 catch (Exception e)
  72.                 {
  73.                     return false;
  74.                 }
  75.                 return true;
  76.             }
  77.         }
  78.  
  79.         protected override void Execute(CodeActivityContext context)
  80.         {
  81.             ContractShipment co = contr.Get(context);
  82.             QueryContractShipment q = query.Get(context);
  83.             int id = Id.Get(context);
  84.  
  85.             if (co.Amount < 0)
  86.             {
  87.                 //Result.Set(context, false);
  88.                 //return;
  89.             }
  90.            
  91.             List<ReportBalanceTable> res = querySelectReportBalanceByShip(co, storeId.Get(context));
  92.             if (res == null || res.Count == 0)
  93.             {
  94.                 if (oldAmount.Get(context) == 0)
  95.                 {
  96.                     Result.Set(context, false);
  97.                     return;
  98.                 }
  99.                 else
  100.                 {
  101.                     ReportBalance rb = new ReportBalance();
  102.                     rb.Amount = oldAmount.Get(context) - co.Amount;
  103.                     rb.Date = co.Date;
  104.                     rb.Id = new Random().Next();
  105.                     rb.ProductId = co.ProductId;
  106.                     rb.StoreCellId = co.StoreCellFromId;
  107.                     new QueryReports().queryAddReportBalance(rb);
  108.                     if (id == -1)
  109.                     {
  110.                         q.queryAddContractShipment(co);
  111.                     }
  112.                     else
  113.                     {
  114.                         co.Id = id;
  115.                         q.queryUpdateContractShipment(co);
  116.                     }
  117.                     Result.Set(context, true);
  118.                     return;
  119.                 }
  120.                
  121.             }
  122.  
  123.  
  124.             res[0].reportBalance.Amount -= (co.Amount - oldAmount.Get(context));
  125.  
  126.             if (res[0].reportBalance.Amount < 0)
  127.             {
  128.                 Result.Set(context, false);
  129.                 return;
  130.             }
  131.    
  132.             if (id == -1)
  133.             {
  134.                 q.queryAddContractShipment(co);
  135.             }
  136.             else
  137.             {
  138.                 co.Id = id;
  139.                 q.queryUpdateContractShipment(co);
  140.             }
  141.            
  142.             if (res[0].reportBalance.Amount > 0)
  143.             {
  144.                 queryUpdateReportBalance(res[0].reportBalance);
  145.             }
  146.             else
  147.             {
  148.                 new QueryReports().queryDeleteReportBalance(res[0].reportBalance.Id);
  149.             }
  150.  
  151.  
  152.             Result.Set(context, true);
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement