/MobileWeb/MobileWebBLL/BLL/HoaDonBLL.cs

https://bitbucket.org/nguyenngoc/project_ptudweb · C# · 356 lines · 298 code · 22 blank · 36 comment · 64 complexity · 798fbeb8cace1aa6cd05e54dd720ffd3 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MobileWebBLL;
  6. using MobileWebBLL.Models;
  7. namespace MobileWebBLL.BLL
  8. {
  9. public class HoaDonBLL
  10. {
  11. MobileWebEntities webmob = new MobileWebEntities();
  12. ChiTietHoaDonBLL adcthd = new ChiTietHoaDonBLL();
  13. public List<HoaDon> ShowListBills()
  14. {
  15. var Bills = webmob.HoaDons.Where(b => b.TrangThai == 1 && b.TinhTrang != 0).OrderByDescending(b => b.TongTien);
  16. return Bills.ToList();
  17. }
  18. public List<HoaDon> ShowListCancelBills()
  19. {
  20. var Bills = webmob.HoaDons.Where(b => b.TrangThai == 1 && b.TinhTrang == 0).OrderByDescending(b => b.TongTien);
  21. return Bills.ToList();
  22. }
  23. public bool UpdateTinhTrangDAO12(long MaHD)
  24. {
  25. HoaDon hd = webmob.HoaDons.Single(b => b.MaHD == MaHD);
  26. hd.TinhTrang = 3;
  27. webmob.SaveChanges();
  28. return true;
  29. }
  30. public bool UpdateTinhTrangDAO3(long MaHD)
  31. {
  32. HoaDon hd = webmob.HoaDons.Single(b => b.MaHD == MaHD);
  33. hd.TinhTrang = 4;
  34. webmob.SaveChanges();
  35. return true;
  36. }
  37. public bool UpdateTinhTrangDAO4(long MaHD)
  38. {
  39. HoaDon hd = webmob.HoaDons.Single(b => b.MaHD == MaHD);
  40. hd.TinhTrang = 5;
  41. webmob.SaveChanges();
  42. return true;
  43. }
  44. public HoaDon GetOneBillToEdit(long MaHD)
  45. {
  46. HoaDon hd = webmob.HoaDons.Single(b => b.MaHD == MaHD);
  47. return hd;
  48. }
  49. public bool UpdateBillDAO(HoaDon HoaDon)
  50. {
  51. HoaDon hd = webmob.HoaDons.Single(b => b.MaHD == HoaDon.MaHD);
  52. hd.DienThoai = HoaDon.DienThoai;
  53. hd.DiaChiGiaoHang = HoaDon.DiaChiGiaoHang;
  54. webmob.SaveChanges();
  55. return true;
  56. }
  57. public bool DeleteBillDAO(long MaHD)
  58. {
  59. HoaDon hd = webmob.HoaDons.Single(b => b.MaHD == MaHD);
  60. hd.TrangThai = 2;
  61. List<ChiTietHoaDon> cthd = webmob.ChiTietHoaDons.Where(ct => ct.MaHD == MaHD).ToList();
  62. for (int i = 0; i < cthd.Count; i++)
  63. {
  64. long temp = cthd[i].MaCTHD;
  65. adcthd.DeleteDetailBill(temp);
  66. }
  67. webmob.SaveChanges();
  68. return true;
  69. }
  70. public bool CancelBillDAO(long MaHD)
  71. {
  72. HoaDon hd = webmob.HoaDons.Single(b => b.MaHD == MaHD);
  73. hd.TinhTrang = 0;
  74. List<ChiTietHoaDon> cthd = webmob.ChiTietHoaDons.Where(ct => ct.MaHD == MaHD).ToList();
  75. for (int i = 0; i < cthd.Count; i++)
  76. {
  77. long temp = cthd[i].MaCTHD;
  78. adcthd.DeleteDetailBill(temp);
  79. }
  80. webmob.SaveChanges();
  81. return true;
  82. }
  83. public int Soluong(long MaSP)
  84. {
  85. int soluong = webmob.SanPhams.First(p => p.MaSP == MaSP).ChiTietHoaDons.Sum(p => p.SoLuong);
  86. return soluong;
  87. }
  88. public List<HoaDon> GetBillOverdue()
  89. {
  90. DateTime deadline = DateTime.Now.AddHours(-24);
  91. List<HoaDon> listhd = webmob.HoaDons.Where(p => p.TinhTrang == 1 && p.ThoiGianDatHang <= deadline).ToList();
  92. return listhd;
  93. }
  94. public bool CancelBillOverdue()
  95. {
  96. //DateTime deadline = DateTime.Now.AddHours(-24);
  97. List<HoaDon> listhd = GetBillOverdue();
  98. for (int i = 0; i < listhd.Count; i++)
  99. {
  100. listhd[i].TinhTrang = 0;
  101. long MaHD = listhd[i].MaHD;
  102. List<ChiTietHoaDon> cthd = webmob.ChiTietHoaDons.Where(ct => ct.MaHD == MaHD).ToList();
  103. for (int j = 0; j < cthd.Count; j++)
  104. {
  105. long temp = cthd[j].MaCTHD;
  106. adcthd.DeleteDetailBill(temp);
  107. }
  108. }
  109. webmob.SaveChanges();
  110. return true;
  111. }
  112. /*
  113. public List<HoaDon> RevenueDate()
  114. {
  115. var hd = from b in webmob.HoaDons
  116. group b by b.ThoiGianDatHang.ToString("MM/dd/yyyy") into g
  117. select new { NgayDat = g.Key, DoanhThu = g.Sum(p => p.TongTien) };
  118. return hd;
  119. }
  120. public List<HoaDon> RevenueMonth()
  121. {
  122. var hd = from b in webmob.HoaDons
  123. group b by b.ThoiGianDatHang.Year into yg
  124. select new
  125. {
  126. Nam = yg.Key,
  127. Thang = from o in yg
  128. group o by o.ThoiGianDatHang.Month into mg
  129. select new { Thang = mg.Key, DoanhThu = mg.Sum(p => p.TongTien)}
  130. };
  131. return hd;
  132. }
  133. public List<HoaDon> RevenueYear()
  134. {
  135. var hd = from b in webmob.HoaDons
  136. group b by b.ThoiGianDatHang.Year into yg
  137. select new { Nam = yg.Key, DoanhThu = yg.Sum(p => p.TongTien) };
  138. return hd;
  139. }
  140. */
  141. //No admin
  142. public long AddHoaDon(List<SanPham> sp, string TenKHDat, string DienThoai, string DiaChiGiaoHang, short HinhThucGiaoHang, short HinhThucThanhToan, int TongTienVND, int MaKH)
  143. {
  144. var tt = webmob.TrangThais.Where(p => p.TrangThai1 == "active").ToList();
  145. HoaDon hd = new HoaDon
  146. {
  147. MaKH = MaKH,
  148. TenKHDat = TenKHDat,
  149. DienThoai = DienThoai,
  150. DiaChiGiaoHang = DiaChiGiaoHang,
  151. HinhThucGiaoHang = HinhThucGiaoHang,
  152. HinhThucThanhToan = HinhThucThanhToan,
  153. ThoiGianDatHang = DateTime.Now,
  154. NgayCapNhat = DateTime.Now,
  155. NgayTao = DateTime.Now,
  156. TinhTrang = 1,
  157. TrangThai = tt.First().MaTrangThai,
  158. TongTien = TongTienVND
  159. };
  160. try
  161. {
  162. webmob.AddToHoaDons(hd);
  163. webmob.SaveChanges();
  164. var hoadon = webmob.HoaDons.Where(p => p.MaKH == MaKH && p.TongTien == TongTienVND).ToList();
  165. if (hoadon.Count <= 0)
  166. return -1;
  167. long MaHD = hoadon.Last().MaHD;
  168. foreach (var item in sp)
  169. {
  170. ProductBLL pro = new ProductBLL();
  171. int TT = pro.GetGiaFromMaSP(item.MaSP);
  172. ChiTietHoaDon ct = new ChiTietHoaDon
  173. {
  174. MaHD = MaHD,
  175. MaSP = item.MaSP,
  176. SoLuong = item.SoLuong,
  177. ThanhTien = item.SoLuong * TT,
  178. NgayCapNhat = DateTime.Now,
  179. NgayTao = DateTime.Now,
  180. TrangThai = tt.First().MaTrangThai
  181. };
  182. webmob.AddToChiTietHoaDons(ct);
  183. }
  184. webmob.SaveChanges();
  185. return MaHD;
  186. }
  187. catch
  188. {
  189. return -1;
  190. }
  191. }
  192. public bool CheckHoaDon(long MaHD, int MaKH)
  193. {
  194. TrangThaiBLL tt = new TrangThaiBLL();
  195. short status = tt.GetTrangThai("active");
  196. var hd = webmob.HoaDons.Where(h => h.MaHD == MaHD && h.MaKH == MaKH && h.TrangThai == status).ToList();
  197. if (hd.Count > 0)
  198. return true;
  199. return false;
  200. }
  201. public bool UpdateStatusHoaDon(long MaHD, short TinhTrang, short HinhThucThanhToan)
  202. {
  203. var hd = webmob.HoaDons.Single(h => h.MaHD == MaHD);
  204. hd.TinhTrang = TinhTrang;
  205. // thay đổi hình thức thanh toán
  206. if (HinhThucThanhToan != -1)
  207. hd.HinhThucThanhToan = HinhThucThanhToan;
  208. try
  209. {
  210. webmob.SaveChanges();
  211. return true;
  212. }
  213. catch
  214. {
  215. return false;
  216. }
  217. }
  218. public List<HoaDon> GetHDFromMaKH(int MaKH, int page, int rowsperpage)
  219. {
  220. var hoadons = new List<HoaDon>();
  221. if (rowsperpage != 0)
  222. hoadons = webmob.HoaDons.Where(c => c.MaKH == MaKH && c.TrangThai1.TrangThai1 == "active").OrderByDescending(c => c.NgayTao).Skip((page - 1) * rowsperpage).Take(rowsperpage).ToList();
  223. else hoadons = webmob.HoaDons.Where(c => c.MaKH == MaKH).ToList();
  224. return hoadons;
  225. }
  226. public HoaDon GetHDFromMaHD(long MaHD)
  227. {
  228. var hoadons = webmob.HoaDons.Where(p=>p.MaHD == MaHD && p.TrangThai1.TrangThai1 == "active").ToList();
  229. if (hoadons.Count > 0)
  230. return hoadons.First();
  231. return new HoaDon();
  232. }
  233. public double CountPagesHoaDon(List<HoaDon> objects, int rowsperpage)
  234. {
  235. double pages = Math.Ceiling((float)objects.Count() / rowsperpage);
  236. return pages;
  237. }
  238. public int CountHoaDons(List<HoaDon> objects)
  239. {
  240. return objects.Count;
  241. }
  242. public List<StatisticParts> Statistic(string fromdate, string todate, int format)
  243. {
  244. List<StatisticParts> liststa = new List<StatisticParts>();
  245. MobileWebEntities web = new MobileWebEntities();
  246. string[] strfrom = fromdate.Split('/');
  247. string[] strto = todate.Split('/');
  248. DateTime muctren = new DateTime(int.Parse(strfrom[2]), int.Parse(strfrom[0]), int.Parse(strfrom[1]));
  249. DateTime mucduoi = new DateTime(int.Parse(strto[2]), int.Parse(strto[0]), int.Parse(strto[1]));
  250. var hds = web.HoaDons.Where(h => h.ThoiGianDatHang >= muctren && h.ThoiGianDatHang <= mucduoi
  251. && h.TrangThai1.TrangThai1 == "active" && (h.TinhTrang == 3 || h.TinhTrang == 4 || h.TinhTrang == 5));
  252. var cthds = (from hd in hds
  253. join ct in web.ChiTietHoaDons
  254. on hd.MaHD equals ct.MaHD
  255. where ct.TrangThai1.TrangThai1 == "active"
  256. select new
  257. {
  258. Day = hd.ThoiGianDatHang.Value.Day,
  259. Month = hd.ThoiGianDatHang.Value.Month,
  260. Year = hd.ThoiGianDatHang.Value.Year,
  261. Revenue = ct.ThanhTien
  262. }
  263. );
  264. if (format == 1)
  265. {
  266. var tks1 = (from cthd in cthds
  267. group cthd by new { cthd.Day, cthd.Month, cthd.Year } into g
  268. select new
  269. {
  270. Day = g.Key.Day,
  271. Month = g.Key.Month,
  272. Year = g.Key.Year,
  273. Revenue = g.Sum(p => p.Revenue)
  274. }
  275. );
  276. foreach (var item in tks1.ToList())
  277. {
  278. StatisticParts sta = new StatisticParts();
  279. sta.Day = item.Day;
  280. sta.Month = item.Month;
  281. sta.Year = item.Year;
  282. sta.Revenue = item.Revenue;
  283. liststa.Add(sta);
  284. }
  285. }
  286. if (format == 2)
  287. {
  288. var tks2 = (from cthd in cthds
  289. group cthd by new { cthd.Month, cthd.Year } into g
  290. select new
  291. {
  292. Month = g.Key.Month,
  293. Year = g.Key.Year,
  294. Revenue = g.Sum(p => p.Revenue)
  295. }
  296. );
  297. foreach (var item in tks2.ToList())
  298. {
  299. StatisticParts sta = new StatisticParts();
  300. sta.Day = 0;
  301. sta.Month = item.Month;
  302. sta.Year = item.Year;
  303. sta.Revenue = item.Revenue;
  304. liststa.Add(sta);
  305. }
  306. }
  307. if (format == 3)
  308. {
  309. var tks3 = (from cthd in cthds
  310. group cthd by new { cthd.Year } into g
  311. select new
  312. {
  313. Year = g.Key.Year,
  314. Revenue = g.Sum(p => p.Revenue)
  315. }
  316. );
  317. foreach (var item in tks3.ToList())
  318. {
  319. StatisticParts sta = new StatisticParts();
  320. sta.Day = 0;
  321. sta.Month = 0;
  322. sta.Year = item.Year;
  323. sta.Revenue = item.Revenue;
  324. liststa.Add(sta);
  325. }
  326. }
  327. return liststa;
  328. }
  329. }
  330. }