/** * Receive Items */ public function receive_items_post() { $delivery_data = $this->input->post('deliveryData'); // should be a json object not a string if ($delivery_data['transaction'] == Transaction_Type::Deliver_Items) { $deliveries_repo = new Deliveries_Repository($this->base_model->get_db_instance()); $delivery_id = $deliveries_repo->new_delivery($delivery_data); if ($delivery_id <= 0) { if ($delivery_id == -2) { echo -2; } else { if ($delivery_id == -1) { echo -1; } else { if ($delivery_id == 0) { echo 0; } } } // delivery exists } else { if ($delivery_id >= 1) { $items_repo = new Items_Repository($this->base_model->get_db_instance()); $items = $delivery_data['items']; foreach ($items as $item) { if ($items_repo->item_exists($item['item_id'])) { $item_info = $items_repo->get_item($item['item_id']); $items_repo->update_item(new Item($item_info->id, $item['description'], $item_info->quantity + $item['quantity'], $item['price'], null, null)); } else { $items_repo->new_item(new Item($item['item_id'], $item['description'], $item['quantity'], $item['price'], null, null)); } $deliveries_repo->new_item(new Delivered_Item(null, $delivery_id, $item['item_id'], $item['quantity'])); } echo 2; // valid delivery } } } else { echo 1; // invalid transaction } }
public function new_delivery_post() { $branch_id = $this->input->post('branchId'); $items = $this->input->post('items'); // [ {itemId: 401, quantity: 20}, {itemId: 402, quantity: 10} ] $uncleared_items_repo = new Uncleared_Items_Repository($this->base_model->get_db_instance()); $deliveries_repo = new Deliveries_Repository($this->base_model->get_db_instance()); $items_repo = new Items_Repository($this->base_model->get_db_instance()); $delivery_id = $deliveries_repo->new_delivery($branch_id, $items); foreach ($items as $item) { $item_info = $items_repo->get_item($item['itemId']); $items_repo->update_item(new Item($item['itemId'], $item_info->description, $item_info->quantity - $item['quantity'], $item_info->price, null, null)); if ($uncleared_items_repo->item_exists_via_item_id_and_branch_id($item['itemId'], $branch_id)) { $uncleared_item = $uncleared_items_repo->get_item_via_item_id_and_branch_id($item['itemId'], $branch_id); $uncleared_items_repo->update_item(new Uncleared_Item($uncleared_item->id, $item['itemId'], $branch_id, $uncleared_item->quantity + $item['quantity'], null, null)); } else { $uncleared_items_repo->new_item(new Uncleared_Item(null, $item['itemId'], $branch_id, $item['quantity'], null, null)); } } echo $deliveries_repo->to_delivery_json($delivery_id); }