$orderLine = $this->getOrderLine($data['txn_id'], $data['item_id']);
$createdHours = (int)$this->config->get('openbaypro_created_hours');
if($createdHours == 0 || $createdHours == '') { $createdHours = 24; } //This is a fallback value.
$from = date("Y-m-d H:i:00", mktime(date("H")-$createdHours, date("i"), date("s"), date("m"), date("d"), date("y")));
if($orderLine === false) {
if($created >= $from) {
$this->openbay->ebay->log('addOrderLine() - New line');
$product_id = $this->openbay->ebay->getProductId($data['item_id']);
/* add to the transaction table */
$this->db->query("
INSERT INTO `" . DB_PREFIX . "ebay_transaction`
SET
`order_id` = '".(int)$order_id."',
`txn_id` = '".$this->db->escape($data['txn_id'])."',
`item_id` = '".$this->db->escape($data['item_id'])."',
`product_id` = '".(int)$product_id."',
`containing_order_id` = '".$data['containing_order_id']."',
`order_line_id` = '".$this->db->escape($data['order_line_id'])."',
`qty` = '".(int)$data['qty']."',
`smp_id` = '".(int)$data['smp_id']."',
`sku` = '".$this->db->escape($data['sku'])."',
`created` = now(),
`modified` = now()
");
if(!empty($product_id)) {
$this->openbay->ebay->log('Link found');
$this->modifyStock($product_id, $data['qty'], '-', $data['sku']);
}
}else{
$this->openbay->ebay->log('addOrderLine() - Transaction is older than '.$this->config->get('openbaypro_created_hours').' hours');
}
}else{
$this->openbay->ebay->log('addOrderLine() - Line existed');
if($order_id != $orderLine['order_id']) {
$this->openbay->ebay->log('addOrderLine() - Order ID has changed from "'.$orderLine['order_id'].'" to "'.$order_id.'"');
$this->db->query("UPDATE `" . DB_PREFIX . "ebay_transaction` SET `order_id` = '".(int)$order_id."', `modified` = now() WHERE `txn_id` = '".$this->db->escape((string)$data['txn_id'])."' AND `item_id` = '".$this->db->escape((string)$data['item_id'])."' LIMIT 1");
//if the order id has changed then remove the old order details
$this->delete($orderLine['order_id']);
}
if($orderLine['smp_id'] != $data['smp_id']) {
$this->openbay->ebay->log('addOrderLine() - SMP ID for orderLine has changed from "'.$orderLine['smp_id'].'" to "'.$data['smp_id'].'"');
$this->db->query("UPDATE `" . DB_PREFIX . "ebay_transaction` SET `smp_id` = '".$this->db->escape($data['smp_id'])."', `modified` = now() WHERE `txn_id` = '".$this->db->escape((string)$data['txn_id'])."' AND `item_id` = '".$this->db->escape((string)$data['item_id'])."' LIMIT 1");
}
if($orderLine['containing_order_id'] != $data['containing_order_id']) {
$this->openbay->ebay->log('addOrderLine() - Containing order ID for orderLine has changed from "'.$orderLine['containing_order_id'].'" to "'.$data['containing_order_id'].'"');
$this->db->query("UPDATE `" . DB_PREFIX . "ebay_transaction` SET `containing_order_id` = '".$this->db->escape($data['containing_order_id'])."', `modified` = now() WHERE `txn_id` = '".$this->db->escape((string)$data['txn_id'])."' AND `item_id` = '".$this->db->escape((string)$data['item_id'])."' LIMIT 1");
}
}
$this->openbay->ebay->log('addOrderLine() - Done');