Jacqueline Maldonado преди 7 години
родител
ревизия
4414f23b0d

+ 1 - 1
app/controllers/sales_controller.rb

@@ -78,7 +78,7 @@ class SalesController < ApplicationController
       if @sale.save
         # agregar detalles de la venta
         @pre_sales.each do |pre_sale|
-          detail = SalesDetail.new(product_id: pre_sale.product_id, unit_price: pre_sale.unit_price, quantity: pre_sale.quantity, amount: pre_sale.amount, tax: pre_sale.tax, discount: pre_sale.discount, total: pre_sale.total, special_price_id: pre_sale.special_price_id, status: :active)
+          detail = SalesDetail.new(product_id: pre_sale.product_id, unit_price: pre_sale.unit_price, quantity: pre_sale.quantity, amount: pre_sale.amount, tax: pre_sale.tax, discount: pre_sale.discount, total: pre_sale.total, special_price_id: pre_sale.special_price_id, status: :active, haggle: pre_sale.haggle, haggle_percent: pre_sale.haggle_percent)
           @sale.sales_details << detail
           pre_sale.destroy
           # actualizar stock del producto

+ 5 - 0
app/helpers/sales_helper.rb

@@ -30,4 +30,9 @@ module SalesHelper
     end
     total
   end
+
+  def pre_sale_haggle_type(pre_sale)
+    type = !pre_sale.haggle.zero? ? number_to_currency(pre_sale.haggle, precision: 2) : "#{pre_sale.haggle_percent}%"
+    content_tag(:i, "", { class: "fa fa-question-circle tooltips font-blue", title: "Se aplicó #{type} de regateo" })
+  end
 end

+ 6 - 9
app/models/sales_detail.rb

@@ -1,13 +1,10 @@
 class SalesDetail < ActiveRecord::Base
-	belongs_to :sale
-	belongs_to :product
-	belongs_to :special_price
+  belongs_to :sale
+  belongs_to :product
+  belongs_to :special_price
+  has_many :pointsales, through: :sale
 
+  enum status: [:inactive, :active]
 
-	has_many :pointsales, :through => :sale
-
-	enum status: [ :inactive, :active ]
-
-	scope :activos, -> { where( "sales_details.status != 0") }
-
+  scope :activos, -> { where.not(sales_details: { status: 0 }) }
 end

+ 6 - 2
app/views/pre_sales/_pre_sale.html.erb

@@ -12,17 +12,21 @@
     <%= pre_sale.product.display_attributes %>
   </td>
   <td>
-    <input type="number" min="1" id="quantity_<%= pre_sale.id %>" class="form-control" id="quantity_<%= pre_sale.id %>" oninput="calculatePrice($(this))" value="<%= pre_sale.quantity %>" pattern="^[0-9]*[1-9][0-9]*$" title="La cantidad debe de ser mayor o igual a 1" required>
+    <input type="number" min="1" id="quantity_<%= pre_sale.id %>" class="form-control input-xsmall" id="quantity_<%= pre_sale.id %>" oninput="calculatePrice($(this))" value="<%= pre_sale.quantity %>" pattern="^[0-9]*[1-9][0-9]*$" title="La cantidad debe de ser mayor o igual a 1" required>
   </td>
   <td><%= pre_sale.unit_price %></td>
   <td><%= pre_sale.tax %></td>
   <td><%= pre_sale.discount %></td>
-  <td><%= pre_sale.total %></td>
+  <td>
+    <%= pre_sale.amount %>
+  </td>
   <td style="width:5%">
     <% if @pos_config.enable_haggle? && pre_sale.haggle.zero? && pre_sale.haggle_percent.zero? %>
       <%= link_to add_sale_haggle_path(pre_sale), :remote => true, :class => "btn btn-icon-only btn-primary hagglebutton", :title=>"Agregar regate" do %>
         <i class="fa fa-tag"></i>
       <% end %>
+    <% else %>
+      <%= pre_sale_haggle_type(pre_sale) %>
     <% end %>
     <button type="button" class="btn btn-icon-only btn-danger" onclick="deleteRow($(this))"><i class="fa fa-trash-o"></i></button>
   </td>

+ 20 - 19
app/views/sales/_form.html.erb

@@ -484,26 +484,26 @@
 				var preSaleId = idText.substring(idText.lastIndexOf('_') + 1, idText.length);
 				$('#save_sale').attr('disabled', true);
 				$.ajax({
-						type: "PUT",
-						url: "/pre_sales/" + preSaleId,
-						dataType: "json",
-						data: { pre_sale: { quantity: input.val() } },
-						success: function(xhr, status, error) {
+					type: "PUT",
+					url: "/pre_sales/" + preSaleId,
+					dataType: "json",
+					data: { pre_sale: { quantity: input.val() } },
+					success: function(xhr, status, error) {
 
-							input.closest('tr').find('td:eq(6)').text(Math.round(xhr.tax * 100) / 100);
+						input.closest('tr').find('td:eq(6)').text(Math.round(xhr.tax * 100) / 100);
 
-							input.closest('tr').find('td:eq(7)').text(Math.round(xhr.discount * 100) / 100);
+						input.closest('tr').find('td:eq(7)').text(Math.round(xhr.discount * 100) / 100);
 
-							input.closest('tr').find('td:eq(8)').text(Math.round(xhr.total * 100) / 100);
+						input.closest('tr').find('td:eq(8)').text(Math.round(xhr.amount * 100) / 100);
 
-							calculateTotals();
+						calculateTotals();
 
-							$('#save_sale').attr('disabled', false);
-						},
-						error: function (xhr, status, errorThrown) {
-							input.val(1);
-							toastr["error"](xhr.responseText.substr(3, (xhr.responseText.length -6)));
-						}
+						$('#save_sale').attr('disabled', false);
+					},
+					error: function (xhr, status, errorThrown) {
+						input.val(1);
+						toastr["error"](xhr.responseText.substr(3, (xhr.responseText.length -6)));
+					}
 				});
 			}, 500);
 		}
@@ -517,9 +517,10 @@
 
 		if($('#products_table').dataTable().fnGetData().length > 0) {
 			$('#products_table tbody tr').each(function() {
-				quantity = parseFloat(($(this).find('td:nth-child(5) input').val()));
-				price = parseFloat($(this).find('td:nth-child(6)').text());
-				amount += (quantity * price);
+				// quantity = parseFloat(($(this).find('td:nth-child(5) input').val()));
+				// price = parseFloat($(this).find('td:nth-child(6)').text());
+				// amount += (quantity * price);
+				amount += parseFloat($(this).find('td:nth-child(9)').text()); // importe
 			});
 		}
 
@@ -530,7 +531,7 @@
 			discount += parseFloat($(this).text());
 		});
 
-		total = (amount - discount) + tax;
+		total = amount + tax;
 		$('#tax').val(Math.round(tax * 100) / 100);
 		$('#discount').val(Math.round(discount * 100) / 100);
 		$('#amount').val(Math.round(amount * 100) / 100);

+ 4 - 4
app/views/sales/create_haggle.js.erb

@@ -2,10 +2,10 @@ $('#products_table tbody tr').each(function() {
  	trTxt = $(this).attr('id');
  	id = parseInt(trTxt.substring(trTxt.lastIndexOf('_') + 1, trTxt.length));
  	if (id == <%= @pre_sale.id %>) {
- 		$(this).find('td:eq(7)').text('<%= @pre_sale.discount %>');  
- 		$(this).find('td:eq(8)').text('<%= @pre_sale.total %>'); 
- 		$(this).find(".hagglebutton").addClass("disabled").click(function(e){ e.preventDefault();}); 
+ 		$(this).find('td:eq(7)').text('<%= @pre_sale.discount %>');
+ 		$(this).find('td:eq(8)').text('<%= @pre_sale.total %>');
+ 		$(this).find(".hagglebutton").addClass("disabled").click(function(e){ e.preventDefault();});
  	}
 });
-$('#dialog').modal('toggle');
 calculateTotals();
+$('#dialog').modal('toggle');

+ 11 - 4
app/views/sales/receipt.pdf.erb

@@ -49,9 +49,16 @@
             <td style="text-align:center">
               <%= number_to_currency(detail.discount, precision: 2) %>
             </td>
-            <td style="text-align:right">
-              <%= number_to_currency(((detail.unit_price * detail.quantity) - detail.discount), precision: 2) %>
-            </td>
+            <% if !detail.haggle.zero? || !detail.haggle_percent.zero? %>
+              <td style="text-align: right">
+                <%= number_to_currency(detail.amount, precision: 2) %><br>
+                <small><%= detail.haggle > 0 ? number_to_currency(detail.haggle, precision: 2) : "#{detail.haggle_percent}%" %> dscto.</small>
+              </td>
+            <% else %>
+              <td style="text-align:right">
+                <%= number_to_currency(((detail.unit_price * detail.quantity) - detail.discount), precision: 2) %>
+              </td>
+            <% end %>
           </tr>
         <% end %>
       </tbody>
@@ -71,7 +78,7 @@
         <tr>
           <td width="30%">Subtotal</td>
           <td width="15%" class="text-right">$</td>
-          <td width="40%"><%= number_to_currency(sale.amount - sale.discount, precision: 2, :unit => "") %></td>
+          <td width="40%"><%= number_to_currency(sale.amount, precision: 2, :unit => "") %></td>
         </tr>
         <% if sale.tax > 0 %>
           <% margin = margin += 20 %>

+ 6 - 0
db/migrate/20180905183025_add_haggle_to_sales_detail.rb

@@ -0,0 +1,6 @@
+class AddHaggleToSalesDetail < ActiveRecord::Migration
+  def change
+    add_column :sales_details, :haggle, :decimal, precision: 10, scale: 2, :default => 0 unless column_exists? :sales_details, :haggle
+    add_column :sales_details, :haggle_percent, :decimal, precision: 10, scale: 2, :default => 0 unless column_exists? :sales_details, :haggle_percent
+  end
+end

+ 3 - 1
db/schema.rb

@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20180817233420) do
+ActiveRecord::Schema.define(version: 20180905183025) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -592,6 +592,8 @@ ActiveRecord::Schema.define(version: 20180817233420) do
     t.integer  "special_price_id"
     t.decimal  "unit_price",            precision: 10, scale: 2
     t.decimal  "unit_price_w_discount", precision: 10, scale: 2
+    t.decimal  "haggle",                precision: 10, scale: 2, default: 0.0
+    t.decimal  "haggle_percent",        precision: 10, scale: 2, default: 0.0
   end
 
   add_index "sales_details", ["product_id"], name: "index_sales_details_on_product_id", using: :btree