我疯了,这是我的代码
public function add_remito()
{
$post = $this->input->post();
$data = array(
'id_cliente' => $post['id_cliente'],
'razonsocial_cliente' => $post['nombre_cliente'],
'fecha_remito' => $post['fecha'],
'total_remito' => $post['total_remito']
);
$this->db->insert('remitosclientes', $data);
$id = $this->db->insert_id();
if ($id > 0) {
for ($i=0;$i<count($post['nombre_producto']);$i++) {
$data1 = array(
'id_factura' => $id,
'id_product' => $post['id_producto'][$i],
'nombre_producto' => $post['nombre_producto'][$i],
'cantidad' => $post['stock'][$i],
'precio' => $post['precio'][$i],
'color' => $post['color'][$i],
'talle' => $post['talle'][$i]
);
$this->db->insert('detalleremitos', $data1);
if ($this->db->insert_id()) {
$this->db->select('*');
$this->db->where('id_producto', $post['id_producto'][$i]);
$query = $this->db->get('productos');
$row = $query->row();
$quantity_in_stock = $row->stock;
$new_quantity_in_stock = intval($quantity_in_stock) - intval($post['stock']);
$this->db->where('id_producto', $post['id_producto'][$i]);
$this->db->update('productos', array('stock' => $new_quantity_in_stock ));
}
$data2 = array(
'id_reg_producto' => $post['id_producto'][$i],
'reg_nombre_producto' => $post['nombre_producto'][$i],
'reg_cantidad_ant' => $quantity_in_stock,
'reg_cantidad' => $new_quantity_in_stock,
'reg_fecha' => $post['fecha'],
'reg_motivo' => "Venta"
);
$this->db->insert('regstocks', $data2);
}
$this->session->set_flashdata('success', 'Data Save Successfully.');
return redirect('venta/remitos');
}
}
public function update_remito()
{
$post = $this->input->post();
$idremito = $this->uri->segment('3');
$data = array(
'id_cliente' => $post['id_cliente'],
'razonsocial_cliente' => $post['nombre_cliente'],
'fecha_remito' => $post['fecha'],
'total_remito' => $post['total_remito']
);
$this->db->where('id_remito', $idremito);
$this->db->update('remitosclientes', $data);
$q = $this->db->select('id_factura')->get('detalleremitos')->result_array();
$db_id=array();
$update=array();
foreach ($q as $key => $value) {
$db_id[$key]=$value['id_factura'];
}
for ($i=0;$i<count($post['id_producto']);$i++) {
$update = array(
'id_factura' => $idremito,
'id_producto' => $post['id_producto'][$i],
'nombre_producto' => $post['nombre_producto'][$i],
'cantidad' => $post['stock'][$i],
'precio' => $post['precio'][$i],
'color' => $post['color'][$i],
'talle' => $post['talle'][$i]
);
$update_query = $this->db->where_in('id_factura', $idremito)
->get('detalleremitos')->result ();
var_dump($update_query);
if (count($update_query) > 0) {
$this->db->insert('detalleremitos', $update);//update if ids exist
} else {
$this->db->update('detalleremitos', $update, $idremito);//insert if does not exist
}
}
}
要添加发票,我没有任何问题,我无法使其工作的是更新该发票,如果添加了产品或更改了详细信息,是否有任何想法?非常感谢你&lt; 3