Skip to content

Commit

Permalink
Merge pull request #9918 from hmislk/Issue#9905_Disposal_cancellation
Browse files Browse the repository at this point in the history
Issue#9905 disposal cancellation  Closes #9905
  • Loading branch information
Irani96 authored Jan 1, 2025
2 parents 47f6989 + 5f9fc01 commit 5edc793
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 11 deletions.
158 changes: 158 additions & 0 deletions src/main/java/com/divudi/bean/pharmacy/PharmacyBillSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.divudi.bean.common.util.JsfUtil;
import com.divudi.data.BillTypeAtomic;
import com.divudi.entity.PreBill;
import com.divudi.entity.StockBill;
import com.divudi.java.CommonFunctions;
import java.io.Serializable;
import java.text.ParseException;
Expand Down Expand Up @@ -292,12 +293,169 @@ public void unitCancell() {
if (prebill != null) {
getBill().setCancelled(true);
getBill().setCancelledBill(prebill);
getBill().setReferenceBill(prebill);
getBillFacade().edit(getBill());
JsfUtil.addSuccessMessage("Canceled");
printPreview = true;
}
}

public void cancelBill() {
if (getBill() == null) {
JsfUtil.addErrorMessage("Please Select a Bill");
return;
}

if (checkIssueReturn(getBill())) {
JsfUtil.addErrorMessage("Issue Bill had been Returned. You can't cancel the bill.");
return;
}

if (getBill().getComments() == null || getBill().getComments().trim().isEmpty()) {
JsfUtil.addErrorMessage("Please Enter Comments");
return;
}

if (checkDepartment(getBill())) {
return;
}

// Bill prebill = getPharmacyBean().reAddToStock(getBill(), getSessionController().getLoggedUser(),
// getSessionController().getDepartment(), BillNumberSuffix.ISSCAN);
if (getBill().isCancelled()) {
JsfUtil.addErrorMessage("Already Canceled");
return;
}

try {
createCancellationBill();
JsfUtil.addSuccessMessage("Canceled");
printPreview = true;
} catch (Exception e) {
JsfUtil.addErrorMessage("Failed to cancel the bill: " + e.getMessage());
// Log the exception
}
}

private void createCancellationBill() {
CancelledBill c = createCancelledBillInstance(getBill());

List<BillItem> billItems = savePreBillItems(c, sessionController.getLoggedUser(),
sessionController.getLoggedUser().getDepartment());
c.setBillItems(billItems);

if (c.getStockBill() != null) {
c.getStockBill().invertStockBillValues(getBill());
} else {
// Ensure getBill().getStockBill() is not null
StockBill stockBill = getBill().getStockBill();
if (stockBill != null) {
c.setStockBill(stockBill);
c.getStockBill().invertStockBillValues(getBill());
} else {
// Handle the case where there is no StockBill (either log or throw an exception)
System.out.println("No StockBill available in getBill()");
}
}

getBillFacade().edit(c);

bill.setForwardReferenceBill(c);
bill.setCancelled(true);
bill.setCancelledBill(c);
bill.setReferenceBill(c);
getBillFacade().edit(bill);
}

private CancelledBill createCancelledBillInstance(Bill b) {
CancelledBill c = new CancelledBill();
c.copy(b);
c.copyValue(b);
c.invertQty();
c.setBilledBill(getBill());
c.setDeptId(getBillNumberBean().institutionBillNumberGenerator(
sessionController.getLoggedUser().getDepartment(), getBill().getBillType(), BillClassType.PreBill, BillNumberSuffix.ISSCAN));
c.setInsId(getBillNumberBean().institutionBillNumberGenerator(
sessionController.getLoggedUser().getInstitution(), getBill().getBillType(), BillClassType.PreBill, BillNumberSuffix.ISSCAN));
c.setCreatedAt(new Date());
c.setCreater(sessionController.getLoggedUser());
c.setComments("Re Add To Stock");
c.setBackwardReferenceBill(b);
c.setBillClassType(BillClassType.CancelledBill);
c.setBillType(BillType.PharmacyIssue);
c.setBillTypeAtomic(BillTypeAtomic.PHARMACY_ISSUE_CANCELLED);
if (c.getId() == null) {
getBillFacade().create(c);
}
return c;
}

private List<BillItem> savePreBillItems(CancelledBill cancelBill, WebUser user, Department department) {
List<BillItem> billItems = new ArrayList<>();

if (getBill() == null) {
return billItems;
}

if (getBill().getBillItems() == null) {
return billItems;
}

for (BillItem bItem : getBill().getBillItems()) {
try {
if (bItem == null) {
System.err.println("BillItem is null, skipping.");
continue;
}

BillItem newBillItem = new BillItem();
newBillItem.copy(bItem);
newBillItem.invertValue(bItem);
newBillItem.setBill(cancelBill);
newBillItem.setReferanceBillItem(bItem);
newBillItem.setCreatedAt(new Date());
newBillItem.setCreater(user);

if (newBillItem.getId() == null) {
System.out.println("Creating BillItem: " + newBillItem);
getBillItemFacade().create(newBillItem);
}

PharmaceuticalBillItem ph = new PharmaceuticalBillItem();
if (bItem.getPharmaceuticalBillItem() != null) {
ph.copy(bItem.getPharmaceuticalBillItem());
ph.invertValue(bItem.getPharmaceuticalBillItem());
ph.setBillItem(newBillItem);

if (ph.getId() == null) {
System.out.println("Creating PharmaceuticalBillItem: " + ph);
getPharmaceuticalBillItemFacade().create(ph);
}

newBillItem.setPharmaceuticalBillItem(ph);
getBillItemFacade().edit(newBillItem);

double qty = (bItem.getQty() != null) ? Math.abs(bItem.getQty()) : 0.0;
if (ph.getStock() != null) {
getPharmacyBean().addToStock(ph.getStock(), qty, ph, department);
} else {
System.err.println("Stock is null for PharmaceuticalBillItem: " + ph);
}
} else {
System.err.println("PharmaceuticalBillItem is null for BillItem: " + bItem);
}

billItems.add(newBillItem);
} catch (Exception e) {
System.err.println("Error processing BillItem: " + bItem);
e.printStackTrace();
throw e; // Re-throw to trigger transaction rollback
}
}

return billItems;
}

public void unitIssueCancel() {

if (bill == null) {
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/com/divudi/entity/StockBill.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ public class StockBill implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;



private double stockValueAtPurchaseRates;
private double stockValueAsSaleRate;


@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER, optional = true,orphanRemoval = true)

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = true, orphanRemoval = true)
private Bill bill;


public Long getId() {
return id;
Expand All @@ -37,8 +34,20 @@ public Long getId() {
public void setId(Long id) {
this.id = id;
}



public void copyStockBill(StockBill bill) {
if (bill != null) {
stockValueAsSaleRate = bill.getStockValueAsSaleRate();
stockValueAtPurchaseRates = bill.getStockValueAtPurchaseRates();
}
}


public void invertStockBillValues(Bill bill) {
stockValueAsSaleRate = 0 - bill.getStockBill().getStockValueAsSaleRate();
stockValueAtPurchaseRates = 0 - bill.getStockBill().getStockValueAtPurchaseRates();

}

@Override
public int hashCode() {
Expand Down Expand Up @@ -88,5 +97,5 @@ public Bill getBill() {
public void setBill(Bill bill) {
this.bill = bill;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class="ui-button-danger"
icon="fa fa-cancel"
value="Cancel"
action="#{pharmacyBillSearch.unitCancell()}"
action="#{pharmacyBillSearch.cancelBill()}"
style="float: right;"
ajax="false"
>
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/reports/inventoryReports/consumption.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
<p:column headerText="Request No" width="6em" >
<h:outputText value="#{b.invoiceNumber}" />
</p:column>

<p:column headerText="Cancel No" width="10em">
<h:outputText value="#{b.cancelledBill.deptId}" />
</p:column>
Expand Down

0 comments on commit 5edc793

Please sign in to comment.