public HttpResponseMessage AddInternalRequisition([FromBody] InternalRequisitionModel objInternalRequisitionModel) { try { if (string.IsNullOrEmpty(objInternalRequisitionModel.InternalRequisitionMaster.from_warehouse_id.ToString())) { var formatter = RequestFormat.JsonFormaterString(); return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation { output = "error", msg = "Please Select From Warehouse!!" }, formatter)); } if (string.IsNullOrEmpty(objInternalRequisitionModel.InternalRequisitionMaster.to_department.ToString())) { var formatter = RequestFormat.JsonFormaterString(); return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation { output = "error", msg = "Please Select To Department!!" }, formatter)); } else { internalRequisitionRepository.AddInternalRequisition(objInternalRequisitionModel); var formatter = RequestFormat.JsonFormaterString(); return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation { output = "success", msg = "Requisition save successfully" }, formatter)); } } catch (Exception ex) { var formatter = RequestFormat.JsonFormaterString(); return(Request.CreateResponse(HttpStatusCode.OK, new Confirmation { output = "error", msg = "Sorry Internal Requisition Failed!" }, formatter)); } }
// Create New Internal Requisition Cash public long AddInternalRequisition(InternalRequisitionModel objInternalRequisitionModel) { try { var intReqMaster = objInternalRequisitionModel.InternalRequisitionMaster; var internalRequisitionDetailsList = objInternalRequisitionModel.InternalRequisitionDetails; int save = 0; //Auto Number Creator long internalRequisitionSerial = _entities.internal_requisition_master.Max(re => (long?)re.internal_requisition_master_id) ?? 0; internalRequisitionSerial++; var internalRequisitionSerialNo = internalRequisitionSerial.ToString().PadLeft(7, '0'); string intReqNo = "INT-REQ-" + internalRequisitionSerialNo; //Master Table intReqMaster.internal_requisition_no = intReqNo; intReqMaster.from_warehouse_id = objInternalRequisitionModel.InternalRequisitionMaster.from_warehouse_id; intReqMaster.to_department = objInternalRequisitionModel.InternalRequisitionMaster.to_department; intReqMaster.customar_name = objInternalRequisitionModel.InternalRequisitionMaster.customar_name; intReqMaster.mobile_no = objInternalRequisitionModel.InternalRequisitionMaster.mobile_no; intReqMaster.requisition_date = DateTime.Now; intReqMaster.payment_type = objInternalRequisitionModel.InternalRequisitionMaster.payment_type; intReqMaster.pricing = objInternalRequisitionModel.InternalRequisitionMaster.pricing; intReqMaster.remarks = objInternalRequisitionModel.InternalRequisitionMaster.remarks; intReqMaster.requisition_status = "Created"; intReqMaster.total_incentive_amount = internalRequisitionDetailsList.Sum(tin => tin.incentive_amount); intReqMaster.total_amount = objInternalRequisitionModel.InternalRequisitionMaster.total_amount; intReqMaster.created_by = objInternalRequisitionModel.InternalRequisitionMaster.created_by; intReqMaster.created_date = DateTime.Now; _entities.internal_requisition_master.Add(intReqMaster); _entities.SaveChanges(); //Details Table long intReqMasterId = intReqMaster.internal_requisition_master_id; foreach (var item in internalRequisitionDetailsList) { var internalRequisitionDetails = new internal_requisition_details { internal_requisition_master_id = intReqMasterId, product_id = item.product_id, color_id = item.color_id, product_version_id = item.product_version_id, quantity = item.quantity, price = item.price, amount = item.amount, is_gift = item.is_gift, gift_type = item.gift_type, incentive_amount = item.incentive_amount, promotion_master_id = item.promotion_master_id, }; _entities.internal_requisition_details.Add(internalRequisitionDetails); save = _entities.SaveChanges(); } // Delivery Master Table if (save > 0) { var deliveryMaster = new delivery_master(); //Get To Warehouse Id var toWarehouseId = _entities.warehouses.SingleOrDefault( a => a.warehouse_name == "Internal (Cash) Warehouse").warehouse_id; //Get Delivery Party Id var partyId = _entities.warehouses.SingleOrDefault( a => a.warehouse_name == "Internal (Cash) Warehouse").party_id; //Get Party Address var partyAddress = _entities.parties.SingleOrDefault( a => a.party_name == "Internal (Cash)").address; long deliverySerial = _entities.delivery_master.Max(po => (long?)po.delivery_master_id) ?? 0; deliverySerial++; var deliveryStr = deliverySerial.ToString().PadLeft(7, '0'); string deliveryNo = "DN-INT-REQ" + "-" + deliveryStr; deliveryMaster.delivery_no = deliveryNo; deliveryMaster.delivery_date = DateTime.Now; deliveryMaster.party_id = partyId; deliveryMaster.requisition_master_id = intReqMasterId; deliveryMaster.courier_id = 0; deliveryMaster.courier_slip_no = ""; deliveryMaster.delivery_address = partyAddress; deliveryMaster.created_by = objInternalRequisitionModel.InternalRequisitionMaster.created_by; deliveryMaster.created_date = DateTime.Now; deliveryMaster.from_warehouse_id = objInternalRequisitionModel.InternalRequisitionMaster.from_warehouse_id; deliveryMaster.to_warehouse_id = toWarehouseId; deliveryMaster.status = "CASH-RFD"; deliveryMaster.remarks = "DN-INT-REQ"; deliveryMaster.total_amount = objInternalRequisitionModel.InternalRequisitionMaster.total_amount; deliveryMaster.lot_no = ""; deliveryMaster.vehicle_no = "DN-INT-REQ"; deliveryMaster.truck_driver_name = "DN-INT-REQ"; deliveryMaster.truck_driver_mobile = "DN-INT-REQ"; _entities.delivery_master.Add(deliveryMaster); _entities.SaveChanges(); long deliveryMasterId = deliveryMaster.delivery_master_id; // Delivery Details Table foreach (var item in internalRequisitionDetailsList) { var deliveryDetails = new delivery_details() { delivery_master_id = deliveryMasterId, product_id = item.product_id, color_id = item.color_id, product_version_id = item.product_version_id, delivered_quantity = item.quantity, is_gift = item.is_gift, requisition_quantity = item.quantity, unit_price = item.product_id, line_total = item.amount, party_id = partyId, gift_type = item.gift_type, is_live_dummy = false }; _entities.delivery_details.Add(deliveryDetails); int saved = _entities.SaveChanges(); if (saved > 0) { // update inventory InventoryRepository updateInventoty = new InventoryRepository(); var intransitWarehouseId = _entities.warehouses.SingleOrDefault(k => k.warehouse_name == "In Transit").warehouse_id; var masterDelivery = _entities.delivery_master.Find(deliveryMasterId); //'39' virtual in-transit warehouse updateInventoty.UpdateInventory("INT-REQ-DELIVERY", masterDelivery.delivery_no, masterDelivery.from_warehouse_id ?? 0, intransitWarehouseId, item.product_id ?? 0, item.color_id ?? 0, item.product_version_id ?? 0, 1, item.quantity ?? 0, objInternalRequisitionModel.InternalRequisitionMaster.created_by ?? 0); } } //For Party Journal //Get Party Account Balance var partyJournal = _entities.party_journal.Where(pj => pj.party_id == partyId) .OrderByDescending(p => p.party_journal_id) .FirstOrDefault(); decimal partyAccountBalance = 0; if (partyJournal != null) { partyAccountBalance = partyJournal.closing_balance ?? 0; } //Find Invoice Total decimal invoiceTotal = 0; invoiceTotal = objInternalRequisitionModel.InternalRequisitionMaster.total_amount ?? 0; //insert in both invoice master and party journal table //Account Balance decimal accountBalance = 0; accountBalance = invoiceTotal + partyAccountBalance; //Invoice Master Table long InvoiceSerial = _entities.invoice_master.Max(po => (long?)po.invoice_master_id) ?? 0; InvoiceSerial++; var invStr = InvoiceSerial.ToString().PadLeft(7, '0'); string invoiceNo = "INV-" + "INT-REQ" + "-" + invStr; invoice_master insert_invoice = new invoice_master { invoice_no = invoiceNo, invoice_date = System.DateTime.Now, party_id = partyId, requisition_master_id = intReqMasterId, party_type_id = 8, company_id = 0, remarks = objInternalRequisitionModel.InternalRequisitionMaster.remarks, created_by = objInternalRequisitionModel.InternalRequisitionMaster.created_by, created_date = DateTime.Now, incentive_amount = internalRequisitionDetailsList.Sum(tin => tin.incentive_amount), item_total = objInternalRequisitionModel.InternalRequisitionMaster.total_amount, rebate_total = 0, invoice_total = invoiceTotal, account_balance = accountBalance, }; _entities.invoice_master.Add(insert_invoice); _entities.SaveChanges(); long InvoiceMasterId = insert_invoice.invoice_master_id; //Invoice Details Table foreach (var item in internalRequisitionDetailsList) { var getProductCategory = _entities.products.SingleOrDefault(p => p.product_id == item.product_id).product_category_id; var getProductUnit = _entities.products.SingleOrDefault(p => p.product_id == item.product_id).unit_id; var invoiceDetails_insert = new invoice_details { invoice_master_id = InvoiceMasterId, product_category_id = getProductCategory, product_id = item.product_id, color_id = item.color_id, product_version_id = item.product_version_id, unit_id = getProductUnit, price = item.price, quantity = item.quantity, line_total = item.amount, is_gift = item.is_gift, gift_type = item.gift_type, is_live_dummy = false }; _entities.invoice_details.Add(invoiceDetails_insert); _entities.SaveChanges(); } //Party Journal partyJournalRepository.PartyJournalEntry("INVOICE", partyId ?? 0, invoiceTotal, "Invoice", objInternalRequisitionModel.InternalRequisitionMaster.created_by ?? 0, invoiceNo); //Receive Serial Table var serialNumber = objInternalRequisitionModel.ReceiveSerialNoDetails; var intransitWarehouse = _entities.warehouses.SingleOrDefault(k => k.warehouse_name == "In Transit").warehouse_id; //Get Delivery Party Id var partyIdForDelivery = _entities.warehouses.SingleOrDefault( a => a.warehouse_name == "Internal (Cash) Warehouse").party_id; foreach (var item in serialNumber) { receive_serial_no_details receiveSerialNoDetails = _entities.receive_serial_no_details.FirstOrDefault( r => r.imei_no == item.imei_no || r.imei_no2 == item.imei_no2); receiveSerialNoDetails.current_warehouse_id = intransitWarehouse; receiveSerialNoDetails.party_id = partyIdForDelivery; receiveSerialNoDetails.deliver_date = DateTime.Now; receiveSerialNoDetails.requisition_id = intReqMasterId; receiveSerialNoDetails.deliver_master_id = deliveryMasterId; receiveSerialNoDetails.is_gift = item.is_gift; receiveSerialNoDetails.gift_type = item.gift_type; receiveSerialNoDetails.is_live_dummy = false; _entities.SaveChanges(); } } return(1); } catch (Exception ex) { return(0); } }