PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/GreenChem/FORM/SALES/frmEditDeliveryOrder.cs

https://gitlab.com/juldhais/greenchem
C# | 277 lines | 240 code | 35 blank | 2 comment | 53 complexity | 12ef1834eb34d178230f5d6a17631f8e MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Windows.Forms;
  4. using DevExpress.XtraEditors;
  5. using JulFunctions;
  6. namespace GreenChem.MenuTransaction
  7. {
  8. public partial class frmEditDeliveryOrder : DevExpress.XtraEditors.XtraForm
  9. {
  10. GreenChemDataContext db;
  11. public frmEditDeliveryOrder(int id)
  12. {
  13. InitializeComponent();
  14. cmbDate.DateTime = DateTime.Now;
  15. cmbNumber.EditValue = id;
  16. lblRefresh_Click(null, null);
  17. cmbNumber.EditValue = id;
  18. cmbNumber_EditValueChanged(null, null);
  19. if (!db.CheckUserRole(frmUtama._iduseraccount.Value, "Allow Change Location"))
  20. cmbLocation.Enabled = false;
  21. if (!db.CheckUserRole(frmUtama._iduseraccount.Value, "Allow Change Status"))
  22. cmbStatus.Enabled = false;
  23. }
  24. private void lblRefresh_Click(object sender, EventArgs e)
  25. {
  26. if (cmbNumber.EditValue.IsNotEmpty())
  27. {
  28. try
  29. {
  30. Cursor.Current = Cursors.WaitCursor;
  31. db = new GreenChemDataContext();
  32. DateTime datelimit = DateTime.Now.AddYears(-1);
  33. var query = from x in db.SalesOrders
  34. where x.DateDelivered != null && x.DateDelivered.Value >= datelimit
  35. select new
  36. {
  37. x.id,
  38. x.TransactionNumber,
  39. x.DateDelivered,
  40. Customer = x.Customer.Name,
  41. x.TotalDelivered
  42. };
  43. cmbNumber.Properties.DataSource = query;
  44. cmbCustomer.Properties.DataSource = db.Customers.GetName();
  45. cmbProduct.DataSource = db.Products.GetNameAll();
  46. cmbLocation.Properties.DataSource = db.Locations.GetName();
  47. cmbExpedition.Properties.DataSource = db.Couriers.GetName();
  48. GetTotal();
  49. }
  50. catch { }
  51. Cursor.Current = Cursors.Default;
  52. }
  53. }
  54. private void cmbNumber_EditValueChanged(object sender, EventArgs e)
  55. {
  56. try
  57. {
  58. Cursor.Current = Cursors.WaitCursor;
  59. db = new GreenChemDataContext();
  60. var salesorder = db.SalesOrders.First(x => x.id == cmbNumber.EditValue.ToInteger());
  61. cmbDate.EditValue = salesorder.DateDelivered;
  62. cmbStatus.Text = cmbStatus.Text;
  63. cmbCustomer.EditValue = salesorder.idCustomer;
  64. cmbLocation.EditValue = salesorder.idLocation;
  65. cmbExpedition.EditValue = salesorder.idCourier;
  66. txtRemarks.Text = salesorder.Remarks;
  67. txtRemarks2.Text = salesorder.Remarks2;
  68. txtSubtotal.EditValue = salesorder.SubtotalDelivered;
  69. txtDiscount.EditValue = salesorder.DiscountDelivered;
  70. txtCost.EditValue = salesorder.CostDelivered;
  71. if (salesorder.SubtotalOrder != 0) txtTaxPercent.EditValue = (salesorder.TaxDelivered / salesorder.SubtotalDelivered) * 100;
  72. txtTax.EditValue = salesorder.TaxDelivered;
  73. txtTotal.EditValue = salesorder.TotalDelivered;
  74. cmbStock.Text = salesorder.Stock;
  75. if (cmbStock.Text.IsEmpty()) cmbStock.Text = "JKT";
  76. txtFOB.Text = salesorder.FOB;
  77. txtShipVia.Text = salesorder.ShipVia;
  78. tempSalesOrderBindingSource.Clear();
  79. foreach (var item in salesorder.DetailSalesOrders)
  80. {
  81. TempSalesOrder detail = new TempSalesOrder();
  82. detail.id = item.id;
  83. detail.idProduct = item.idProduct;
  84. detail.QuantityOrder = item.QuantityOrder;
  85. detail.QuantityDelivered = item.QuantityDelivered;
  86. detail.UoM = item.Product.UoM;
  87. detail.PriceDelivered = item.PriceOrder;
  88. detail.DiscountDelivered = item.DiscountOrder;
  89. detail.SubtotalDelivered = item.SubtotalOrder;
  90. detail.Packaging = item.Packaging;
  91. detail.NoBatch = item.NoBatch;
  92. tempSalesOrderBindingSource.Add(detail);
  93. }
  94. GetTotal();
  95. }
  96. catch (Exception ex)
  97. {
  98. XtraMessageBox.Show("Gagal.\n" + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  99. }
  100. Cursor.Current = Cursors.Default;
  101. }
  102. private void GetTotal()
  103. {
  104. try
  105. {
  106. var details = tempSalesOrderBindingSource.Cast<TempSalesOrder>();
  107. txtSubtotal.EditValue = details.Sum(x => x.SubtotalDelivered);
  108. decimal subtotaldiscount = txtSubtotal.EditValue.ToDecimal() - txtDiscount.EditValue.ToDecimal();
  109. txtTax.EditValue = subtotaldiscount * txtTaxPercent.EditValue.ToDecimal() / 100;
  110. txtTotal.EditValue = subtotaldiscount + txtTax.EditValue.ToDecimal() + txtCost.EditValue.ToDecimal();
  111. }
  112. catch { }
  113. }
  114. private void gridView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
  115. {
  116. if (e.Column.FieldName == "idProduct" && gridView.GetFocusedRowCellValue("idProduct").IsNotEmpty())
  117. {
  118. int id = gridView.GetFocusedRowCellValue("idProduct").ToInteger();
  119. db = new GreenChemDataContext();
  120. var product = db.Products.First(x => x.id == id);
  121. gridView.SetFocusedRowCellValue("UoM", product.UoM);
  122. gridView.SetFocusedRowCellValue("PriceDelivered", product.SalesPrice);
  123. }
  124. else if (e.Column.FieldName == "QuantityDelivered" || e.Column.FieldName == "PriceDelivered" || e.Column.FieldName == "DiscountDelivered")
  125. {
  126. decimal quantity = gridView.GetFocusedRowCellValue("QuantityDelivered").ToDecimal();
  127. decimal price = gridView.GetFocusedRowCellValue("PriceDelivered").ToDecimal();
  128. decimal discount = gridView.GetFocusedRowCellValue("DiscountDelivered").ToDecimal();
  129. decimal subtotal = 0;
  130. if (discount < 100) subtotal = (quantity * price) * (100 - discount) / 100;
  131. else subtotal = quantity * (price - discount);
  132. gridView.SetFocusedRowCellValue("SubtotalDelivered", subtotal);
  133. GetTotal();
  134. }
  135. }
  136. private void btnSave_Click(object sender, EventArgs e)
  137. {
  138. if (cmbNumber.EditValue.IsEmpty())
  139. {
  140. XtraMessageBox.Show("Transaction number can't be empty.", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  141. cmbNumber.Focus();
  142. return;
  143. }
  144. if (cmbCustomer.EditValue.IsEmpty())
  145. {
  146. XtraMessageBox.Show("Customer can't be empty.", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  147. cmbCustomer.Focus();
  148. return;
  149. }
  150. try
  151. {
  152. Cursor.Current = Cursors.WaitCursor;
  153. db = new GreenChemDataContext();
  154. SalesOrder salesorder = db.SalesOrders.First(x => x.id == cmbNumber.EditValue.ToInteger());
  155. string stockold = salesorder.Stock;
  156. salesorder.idUserAccount = frmUtama._iduseraccount;
  157. if (cmbLocation.EditValue.IsNotEmpty()) salesorder.idLocation = cmbLocation.EditValue.ToInteger();
  158. if (cmbExpedition.EditValue.IsNotEmpty()) salesorder.idCourier = cmbExpedition.EditValue.ToInteger();
  159. salesorder.DateDelivered = cmbDate.DateTime;
  160. salesorder.Status = cmbStatus.Text;
  161. salesorder.Remarks = txtRemarks.Text;
  162. salesorder.Remarks2 = txtRemarks2.Text;
  163. salesorder.SubtotalDelivered = txtSubtotal.EditValue.ToDecimal();
  164. salesorder.DiscountDelivered = txtDiscount.EditValue.ToDecimal();
  165. salesorder.CostDelivered = txtCost.EditValue.ToDecimal();
  166. salesorder.TaxDelivered = txtTax.EditValue.ToDecimal();
  167. salesorder.TotalDelivered = txtTotal.EditValue.ToDecimal();
  168. salesorder.Stock = cmbStock.Text;
  169. salesorder.FOB = txtFOB.Text;
  170. salesorder.ShipVia = txtShipVia.Text;
  171. foreach (TempSalesOrder item in tempSalesOrderBindingSource)
  172. {
  173. if (item.idProduct == null) continue;
  174. Product product = db.Products.First(x => x.id == item.idProduct);
  175. DetailSalesOrder detail = salesorder.DetailSalesOrders.First(x => x.id == item.id);
  176. //repair old stock
  177. if (stockold == "JKT" || stockold.IsEmpty())
  178. product.Stock += detail.QuantityDelivered;
  179. else if (stockold == "BPP")
  180. product.Stock2 = product.Stock2.ToDecimal() + detail.QuantityDelivered;
  181. else if (stockold == "BTG")
  182. product.Stock3 = product.Stock3.ToDecimal() + detail.QuantityDelivered;
  183. product.TransitStock = product.TransitStock -= detail.QuantityDelivered;
  184. detail.idProduct = item.idProduct;
  185. detail.QuantityDelivered = item.QuantityDelivered;
  186. detail.PriceDelivered = item.PriceDelivered;
  187. detail.DiscountDelivered = item.DiscountDelivered;
  188. detail.SubtotalDelivered = item.SubtotalDelivered;
  189. detail.Cost = product.Cost;
  190. detail.Packaging = item.Packaging;
  191. detail.NoBatch = item.NoBatch;
  192. if (cmbStock.Text == "JKT")
  193. {
  194. product.Stock -= item.QuantityDelivered;
  195. if (product.Stock < 0)
  196. {
  197. XtraMessageBox.Show("Stock JKT Product : " + product.Name + " tidak mencukupi.", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  198. gridControl.Focus();
  199. return;
  200. }
  201. }
  202. else if (cmbStock.Text == "BPP")
  203. {
  204. product.Stock2 = product.Stock2.ToDecimal() - item.QuantityDelivered;
  205. if (product.Stock2 < 0)
  206. {
  207. XtraMessageBox.Show("Stock BPP Product : " + product.Name + " tidak mencukupi.", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  208. gridControl.Focus();
  209. return;
  210. }
  211. }
  212. else if (cmbStock.Text == "BTG")
  213. {
  214. product.Stock3 = product.Stock3.ToDecimal() - item.QuantityDelivered;
  215. if (product.Stock3 < 0)
  216. {
  217. XtraMessageBox.Show("Stock BTG Product : " + product.Name + " tidak mencukupi.", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  218. gridControl.Focus();
  219. return;
  220. }
  221. }
  222. product.TransitStock = product.TransitStock.ToDecimal() + item.QuantityDelivered;
  223. }
  224. db.SubmitChanges();
  225. XtraMessageBox.Show("Data saved successfully.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  226. //Printing.DeliveryOrder(salesorder.id);
  227. Close();
  228. }
  229. catch (Exception ex)
  230. {
  231. XtraMessageBox.Show("Gagal.\n" + ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  232. }
  233. Cursor.Current = Cursors.Default;
  234. }
  235. private void btnCancel_Click(object sender, EventArgs e)
  236. {
  237. DialogResult = XtraMessageBox.Show("Are you sure want to cancel this transaction?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  238. if (DialogResult == DialogResult.Yes)
  239. Close();
  240. }
  241. }
  242. }