---
# System prepended metadata

title: Adjustment Submit Step 1

---

# Adjustment Submit Step 1

## Params:
```json=
{
  "purchase_order_adjustment_item_massal_id": 1542, # ID disini wajib yg candidate=false
	"adjustment_coverages": [
		// Alamat Baru
    {
      "id": null,
      "purchase_order_item_massal_coverage_id": null,
      "shipping_address_id": 255,
      "quantity": 30,
      "unit": null,
      "will_be_deleted": false
    },
		// Alamat Baru dihapus
		{
      "id": 12,
      "purchase_order_item_massal_coverage_id": null,
      "shipping_address_id": 255,
      "quantity": 30,
      "unit": null,
      "will_be_deleted": true
    },
		// Alamat Baru tidak ada perubahan
		{
      "id": 12,
      "purchase_order_item_massal_coverage_id": null,
      "shipping_address_id": 255,
      "quantity": 30,
      "unit": null,
      "will_be_deleted": false
    },
		// Alamat Existing ganti Alamat
    {
      "id": null, // id present if already saved
      "purchase_order_item_massal_coverage_id": 1669,
      "shipping_address_id": 204,
      "quantity": 200,
      "unit": "pcs",
      "will_be_deleted": false
    },
		// Alamat Existing update Qty
    {
      "id": null, // id present if already saved
      "purchase_order_item_massal_coverage_id": 1668,
      "shipping_address_id": 229,
      "quantity": 150,
      "unit": "pcs",
      "will_be_deleted": false
    },
 		// Alamat Existing tidak ada perubahan
		{
			"id": null,
			"purchase_order_item_massal_coverage_id": 1668,
			"shipping_address_id": 128,
			"quantity": 100,
			"unit": "pcs",
			"will_be_deleted": false
		},
		// Alamat Existing quantity sebelumnya 70 tapi sesudah nya ada 20, karena sudah ada pengiriman sebelumnya 50
		{
			"id": null, // id present if already saved
			"purchase_order_item_massal_coverage_id": 1668,
			"shipping_address_id": 128,
			"quantity": 50,
			"unit": "pcs",
			"will_be_deleted": false
		}
  ]
}
```

```ruby=

adj_massal = Phoenix::PurchaseOrderAdjustmentItemMassal.find params[:purchase_order_adjustment_item_massal_id]

adj_item = adj_massal.purchase_order_adjustment_item

po_item = adj_item.purchase_order_item

po_coverages = po_item.purchase_order_item_massal_coverages

def candidate_adj_massal
	@candidate_adj_massal ||= begin
		candidate_adj_massal = Phoenix::PurchaseOrderAdjustmentItemMassal.not_default
																											.find_or_initialize_by(
																												candidate: true,
																												purchase_order_adjustment_item_id: adj_item.id
																											)

		if candidate_adj_massal.blank?
			candidate_adj_massal = adj_massal.dup
			candidate_adj_massal.candidate = true
			candidate_adj_massal.save!
		end

		candidate_adj_massal
	end
end

# Alamat Existing quantity sebelumnya 70 tapi sesudah nya ada 20, kenapa 20 karena sudah ada pengiriman sebelumnya 50
{
	"id": null, // id present if already saved
	"purchase_order_item_massal_coverage_id": 1668,
	"shipping_address_id": 128,
	"quantity": 20,
	"unit": "pcs",
	"will_be_deleted": false
}

# Alamat Existing quantity sebelumnya 70 tapi sesudah nya ada 20, kenapa 20 karena sudah ada pengiriman sebelumnya 50
{
	"id": null, // id present if already saved
	"purchase_order_item_massal_coverage_id": 1668,
	"shipping_address_id": 128,
	"quantity": 20 + 30,
	"unit": "pcs",
	"will_be_deleted": false
}

def no_changes?(param_adj_coverage)
	massal_cov_id = param_adj_coverage[:purchase_order_item_massal_coverage_id]
	return false if massal_cov_id.blank?

	massal_cov = Phoenix::PurchaseOrderItemMassalCoverage.find_by(id: massal_cov_id)
	return false if massal_cov.blank?

	quantity = param_adj_coverage[:quantity].to_i
	quantity == massal_cov.quantity_available && param_adj_coverage[:shipping_address_id].to_i == massal_cov.shipping_address_id
end

def save_bulk
	params[:adjustment_coverages].each do |param_cov|
		next if no_changes?(param_cov)

		adj_cov = create_coverage(param_cov)

		# insert_existing_addrress!(adj_cov) if shipping_address_changed?(adj_cov, param_cov)

		saved_adj_massal_coverages << adj_cov
	end
end

def shipping_address_changed?(adj_cov_saved, param_adj_coverage)
	po_item_cov = adj_cov_saved.purchase_order_item_massal_coverage
	cov_shipping_address_id = po_item_cov&.shipping_address_id&.to_i

	exist_adj_cov = adj_massal.purchase_order_adjustment_item_massal_coverages.not_default
														.find_by(shipping_address_id: cov_shipping_address_id)
	po_item_cov.present? &&
		cov_shipping_address_id != param_adj_coverage[:shipping_address_id].to_i &&
		exist_adj_cov.blank? &&
		po_item_cov.quantity_used.to_i.positive?
end

def insert_existing_addrress!(adj_cov_saved)
	p_adj_coverage = {
		id: nil,
		purchase_order_item_massal_coverage_id: adj_cov_saved.purchase_order_item_massal_coverage_id,
		quantity: 0,
		shipping_address_id: adj_cov_saved.purchase_order_item_massal_coverage&.shipping_address_id
	}
	adj_cov_saved = create_coverage(p_adj_coverage, true)
	saved_adj_massal_coverages << adj_cov_saved
end

def po_item
	@po_item ||= adj_massal.purchase_order_adjustment_item.purchase_order_item
end

def create_coverage(param_cov)
	massal_cov_id = param_cov[:purchase_order_item_massal_coverage_id] # ada kemungkinan kosong karena alamat baru
	cov = po_coverages.find_by(id: massal_cov_id)

	
	po_shipping = if candidate_adj_massal.self_courier?
									po_item.purchase_order_item_massal_shippings.first
								else
									cov&.purchase_order_item_massal_shipping 
								end

	shipping_address = Phoenix::ShippingAddress.find(param_cov[:shipping_address_id]) || cov&.shipping_address

	if adj_massal.self_courier?
		adj_shipping = candidate_adj_massal.purchase_order_adjustment_item_massal_shippings.first
	else
		adj_shipping = candidate_adj_massal.purchase_order_adjustment_item_massal_shippings
														 					 .find_by(purchase_order_item_massal_shipping_id: po_shipping&.id)
	end

	adj_shipping = candidate_adj_massal.purchase_order_adjustment_item_massal_shippings.new if adj_shipping.blank?

	adj_shipping.purchase_order_item_massal_shipping_id = po_shipping&.id
	adj_shipping.price                                  = po_shipping&.price
	adj_shipping.courier                                = po_shipping&.courier
	adj_shipping.courier_name                           = po_shipping&.courier_name
	adj_shipping.service                                = po_shipping&.service
	adj_shipping.service_name                           = po_shipping&.service_name
	adj_shipping.shipping_method                        = candidate_adj_massal.shipping_method
	adj_shipping.speed                                  = po_shipping&.speed
	adj_shipping.speed_unit                             = po_shipping&.speed_unit
	adj_shipping.speed_label                            = po_shipping&.speed_label
	adj_shipping.save!

	adj_cov = candidate_adj_massal.purchase_order_adjustment_item_massal_coverages
																.not_default_will_be_deleted
																.find_by(id: param_cov[:id])

	adj_cov = Phoenix::PurchaseOrderAdjustmentItemMassalCoverage.new if adj_cov.blank?

	adj_cov.purchase_order_item_massal_coverage_id       = cov&.id
	adj_cov.purchase_order_adjustment_item_massal_shipping_id = adj_shipping.id
	adj_cov.quantity                                     = normalize_quantity(param_cov, cov, address_existing_changed)
	adj_cov.quantity_available                           = param_cov[:quantity]
	adj_cov.shipping_address_id                          = param_cov[:shipping_address_id]
	adj_cov.shipping                                     = shipping_address.address_json
	adj_cov.previous_purchase_order_item_massal_coverage = cov
	adj_cov.will_be_deleted                              = param_cov[:will_be_deleted]
	adj_cov.save!
end


```

UpsertMassalCoverageOfficer


## Save price

```ruby=
def save_price
	update_candidate!

	production_cost = params[:unit_price].to_f * calculate_quantity
	shipping_cost   = params[:shipping_cost].to_f

	if adj_item_massal.self_courier?
		adj_item_massal.shipping_cost  = shipping_cost
		adj_item_massal_shipping.price = shipping_cost
	end

	adj_item_massal.unit_price      = params[:unit_price].to_f
	adj_item_massal.production_cost = production_cost
	adj_item_massal.commission      = price_add_ppn(production_cost * adj_item_massal.commission_rate)
	adj_item_massal.quantity        = calculate_quantity 

	if adj_item_massal.no_changes?
		@errors << ResponseMessage::MESSAGES[:REQUEST_NOT_VALID][I18n.locale.upcase]
		return false
	end

	adj_item_massal_shipping.save!
	adj_item_massal.save!
end

def update_candidate!
	adj_item_massal.not_default.where(candidate: false).destroy!
	
	candidate_adj_massal.update(candidate: false)
end

def adj_item
	@adj_item ||= adj_massal.purchase_order_adjustment_item
end

def po_item
	@po_item ||= adj_item.purchase_order_item
end

def candidate_adj_massal
	@candidate_adj_massal ||= begin
		candidate_adj_massal = Phoenix::PurchaseOrderAdjustmentItemMassal.not_default
																											.find_or_initialize_by(
																												candidate: true,
																												purchase_order_adjustment_item_id: adj_item.id
																											)
	end
end

```
