Hi
*i have create an invoice with angular and pass data throw API Controller -
i store data in list to let me save it on database when i need, but when i
open new invoice from my PC and add item's on it before i save it
to database i open another invoice from other PC and when it's open i got
the item's from invoice in my pc on it too - i don't know how to set it to
open with empty array in each PC to be works as it should be ?*
*(function () { var app = angular.module('jqanim', []);
app.controller('InvoiceController', function ($scope, $http) {
$scope.tax = 0.00; $scope.PercentageDiscount = 0.00;
$scope.MoneyDiscount = 0.00; $scope.Total = 0.00;
$scope.save = function () {
$http.post("/api/customerService/Post", $scope.item).success(function
(data) { $scope.orders.push(data); // Clear
AutoComplete Text
$("#Products").data("kendoAutoComplete").value(""); if
(window.event.keyCode == 13) { $("#ProductID").focus();
} $('#ProductID').prop('readonly', false);
}); }; $scope.saveInvoice = function () {
$http.post("/api/customerService/SaveInvoice", $scope.invoice)
.success(function (data) { // Clear AutoComplete Text
$("#Customers").data("kendoAutoComplete").value("");
$scope.invoice = []; $scope.orders =
[]; $scope.tax = 0.00;
$scope.PercentageDiscount = 0.00; $scope.MoneyDiscount
= 0.00; alertify.set('notifier', 'position',
'top-left'); alertify.success('تم حفظ الفاتورة', 3);
}) .error(function (data) {
alertify.set('notifier', 'position', 'top-right');
alertify.error('يوجد خطأ', 2); return;
}); }; $scope.remove = function (id, index) {
$http.delete("/api/customerService?id=" + id).success(function (data) {
$scope.orders.splice(index, 1); }); };
$http.get("/api/CustomerService/getDetails").success(function (data) {
$scope.orders = data; }); $scope.keypress = function
(id) { if (window.event.keyCode == 13) {
$http.get("/api/CustomerService/GetProduct?id=" + id).success(function
(data) { angular.forEach(data, function (item) {
$("#Products").val(item.ProductName).trigger('change');
$("#SellPrice").val(item.sell_price).trigger('change');
$("#Stock").val(item.stock).trigger('change');
$("#Quantity").focus(); });
}); } }; });})();**my view code:*
*@{ ViewBag.Title = "About";}<style> .k-list { padding-right:
15px; }</style><br /><div ng-app="jqanim"
ng-controller="InvoiceController"> @* إختيار العملاء *@ <form
method="post" ng-submit="saveInvoice(); invoice={}"> <table
class="table table-striped table-bordered" dir="rtl"> <tr>
<th class="text-center">العميل</th> <th
class="text-center">تاريخ الفاتورة</th> <th
class="text-center">الإجمالى</th> <th></th>
</tr> <tr> <td align="center"
style="vertical-align:middle;"> <div>
@(Html.Kendo() .AutoComplete()
.Name("Customers")
.DataTextField("CompanyName") .Events(events
=> events.Select("CustomerSelect"))
.MinLength(1) .HtmlAttributes(new { style =
"width:450px; font-weight:bold" })
.Placeholder("إكتب جزء من إسم العميل")
.Filter("contains") .Delay(1)
.DataSource(source => {
source.Custom()
.ServerFiltering(true)
.ServerPaging(true)
.Type("aspnetmvc-ajax") //Set this type if you want to use
DataSourceRequest and ToDataSourceResult instances
.Transport(transport =>
{
transport.Read("Virtualization_Customer_Read", "Home");
})
.Schema(schema => {
schema.Data("Data") //define the
[data](http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-schema.data)
option .Total("Total");
//define the
[total](http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-schema.total)
option });
}) .Virtual(v =>
v.ItemHeight(26).ValueMapper("cusvalueMapper")) )
</div> </td> <td
style="display:none;"> @Html.TextBox("CustomerID", null,
new { ng_model = "invoice.CustomerID" }) </td>
<td align="center" style="vertical-align:middle;">
@(Html.Kendo() .DatePicker()
.Format("yyyy-MM-dd") .Name("OrderDate")
.HtmlAttributes(new { style = "width:150px;
font-weight:bold; text-align:center", ng_model = "invoice.OrderDate" })
) </td> <td align="center"
style="vertical-align:middle; width:175px; font-weight:bold">
{{Total | currency}} @Html.TextBox("Total", null,
new { ng_model = "invoice.Total", @style = "display:none;" })
</td> <td align="center" style="vertical-align:middle;">
<input ng-disabled="invoice.CustomerID == null ||
invoice.OrderDate == null || Total == 0.00" type="submit" name="submitbtn"
value="حفظ الفاتورة" class="btn btn-success" style="font-weight:bold;" />
</td> </tr> <tr> <td
colspan="6" class="text-center" style="color:red; font-weight:bold;">
<div ng-show="invoice.CompanyName == ''">يجب إختيار أحد
العملاء</div> <div ng-show="invoice.OrderDate ==
''">يجب إدخال تاريخ الفاتورة</div> </td> </tr>
</table> </form> @* اختيار الاصناف *@ <form method="post"
ng-submit="save(); item={}"> <table class="table table-striped
table-bordered" dir="rtl"> <tr> <th
class="text-center">كود الصنف</th> <th
class="text-center">إسم الصنف</th> <th
class="text-center">الكمية</th> <th
class="text-center">السعر</th> <th
class="text-center">الرصيد</th> <th
class="text-center">الإجمالى</th> <th></th>
</tr> <tr> <td style="vertical-align:middle;">
<input id="ProductID" type="text" ng-model="item.ProductID"
ng-keypress="keypress(item.ProductID,$index)" ng:required
ng:validate="integer" placeholder="حدد الكود" class="text-center
form-control" style="width:100px; font-weight:bold" />
</td> <td align="center" style="vertical-align:middle;">
<div> @(Html.Kendo()
.AutoComplete()
.Name("Products")
.DataTextField("ProductName") .Events(events
=> events.Select("ProductSelect"))
.MinLength(1) .HtmlAttributes(new { style =
"width:450px; font-weight:bold", ng_model = "item.ProductName" })
.Placeholder("إكتب جزء من إسم الصنف")
.Filter("contains") .Delay(1)
.DataSource(source =>
{ source.Custom()
.ServerFiltering(true)
.ServerPaging(true)
.Type("aspnetmvc-ajax") //Set this type if you want to use
DataSourceRequest and ToDataSourceResult instances
.Transport(transport =>
{
transport.Read("Virtualization_Read", "Home");
}) .Schema(schema =>
{
schema.Data("Data") //define the
[data](http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-schema.data)
option .Total("Total");
//define the
[total](http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-schema.total)
option });
}) .Virtual(v =>
v.ItemHeight(26).ValueMapper("valueMapper")) )
</div> </td> <td align="center"
style="vertical-align:middle;"> <input
ng-readonly="item.ProductID == '' || item.ProductID == null" id="Quantity"
name="Quantity" class="text-center form-control" ng-model="item.Quantity"
value="1" size="4" ng:required ng:validate="integer" placeholder="حدد
الكمية" style="width:100px; font-weight:bold"> </td>
<td align="center" style="vertical-align:middle;">
<input ng-readonly="true" id="SellPrice" class="text-center form-control"
ng-model="item.SellPrice" value="0.00" ng:required ng:validate="number"
size="6" style="width:100px; font-weight:bold"> </td>
<td align="center" style="vertical-align:middle;">
<input ng-readonly="true" id="Stock" class="text-center form-control"
ng-model="item.Stock" value="0.00" ng:required ng:validate="number"
size="6" style="width:100px; font-weight:bold"> </td>
<td class="text-center" style="vertical-align:middle; width:100px;
font-weight:bold;"> {{item.SellPrice * item.Quantity |
currency}} @Html.TextBox("Total_Item", null, new {
ng_model = "item.Total_Item", @style = "display:none;" })
</td> <td align="center" style="vertical-align:middle;">
<input ng-disabled="item.Quantity > item.Stock ||
item.Quantity == 0 || item.Quantity == null || item.SellPrice == null"
type="submit" name="submitbtn" value="إضافة" class="btn btn-primary"
style="font-weight:bold;" /> </td> </tr>
<tr> <td colspan="7" class="text-center" style="color:red;
font-weight:bold;"> <div ng-show="item.Quantity >
item.Stock">رصيد الصنف لا يكفى</div> <div
ng-show="item.Quantity == 0">غير مسموح بكمية أقل من 1</div>
</td> </tr> </table> </form> <p><input type="text"
ng-model="ok"></p> <br /> @* Details *@ <table class="table
table-striped table-bordered" align="center" dir="rtl"
ng-show="(orders).length"> <tr> <th class="text-center"
style="width:100px;">كود الصنف</th> <th class="text-center">إسم
الصنف</th> <th class="text-center"
style="width:150px;">الكمية</th> <th class="text-center"
style="width:150px;">السعر</th> <th class="text-center"
style="width:150px;">الإجمالى</th> <th
style="width:150px;"></th> </tr> <tr ng-repeat="ord in orders
| filter:ok"> <td align="center" style="vertical-align:middle;
font-weight:bold;"> {{ord.ProductID}} </td>
<td align="center" style="vertical-align:middle; font-weight:bold;">
{{ord.ProductName}} </td> <td
align="center" style="vertical-align:middle; font-weight:bold;">
{{ ord.Quantity}} </td> <td align="center"
style="vertical-align:middle; font-weight:bold;">
{{ord.SellPrice}} </td> <td class="text-center"
style="vertical-align:middle; font-weight:bold;">
{{ord.SellPrice * ord.Quantity | currency}} </td> <td
align="center" style="vertical-align:middle;"> <input
type="submit" name="submitbtn" value="x" class="btn btn-danger"
ng-click="remove(ord.ProductID,$index)" style="font-weight:bold;" />
</td> </tr> </table> @* Totals *@ <table class="table
table-striped" dir="ltr"> <tr> <td align="left"
style="width:150px;"> Sub Total </td>
<td align="left" style="font-weight: bold;">
{{invoice_sub_total() | currency}} </td> </tr>
<tr> <td align="left" style="width:150px;">
Tax(%): <input ng-model="tax" ng:validate="number"
style="background-color: #F9F9F9; border: 1px solid #F9F9F9; width:43px;"
class="text-center"> </td> <td align="left"
style="font-weight: bold;"> {{calculate_tax() | currency}}
</td> </tr> <tr> <td align="left"
style="width:150px;"> Discount(%): <input
ng-model="PercentageDiscount" ng:validate="number" style="background-color:
#F9F9F9; border: 1px solid #F9F9F9; width:43px;" class="text-center">
</td> <td align="left" style="color:red;
font-weight:bold;"> {{calculate_PercentageDiscount() |
currency}} </td> </tr> <tr> <td
align="left" style="width:150px;"> Discount:
<input ng-model="MoneyDiscount" ng:validate="number"
style="background-color: #F9F9F9; border: 1px solid #F9F9F9; width:43px;"
class="text-center"> </td> <td align="left"
style="color:red; font-weight:bold;">
{{calculate_MoneyDiscount() | currency}} </td> </tr>
<tr> <td align="left" style="width:150px;">
Grand Total: </td> <td align="left"
style="color:blue; font-weight:bold;">
{{calculate_grand_total() | currency}} </td> </tr>
</table>@section scripts{ <script
src="~/Scripts/angular.min.js"></script> <script
src="~/Scripts/ang2.js"></script> <script> function
valueMapper(productoptions) { $.ajax({ url:
"@Url.Action("ProductsValueMapper", "Home")", data:
convertValues(productoptions.value), success: function
(data) { productoptions.success(data); }
}); } function convertValues(productvalue) {
var data = {}; productvalue = $.isArray(productvalue) ?
productvalue : [productvalue]; for (var idx = 0; idx <
productvalue.length; idx++) { data["productvalues[" + idx +
"]"] = productvalue[idx]; } return data; }
function cusvalueMapper(customeroptions) { $.ajax({
url: "@Url.Action("CustomerValueMapper", "Home")",
data: cusconvertValues(customeroptions.value), success:
function (data) { customeroptions.success(data);
} }); } function
cusconvertValues(customervalue) { var data = {};
customervalue = $.isArray(customervalue) ? customervalue :
[customervalue]; for (var idx = 0; idx < customervalue.length;
idx++) { data["customervalues[" + idx + "]"] =
customervalue[idx]; } return data; }
//Get SellPrice & Stock & Product ID function ProductSelect(e) {
$('#ProductID').prop('readonly', true); var DataItem =
this.dataItem(e.item.index());
$("#SellPrice").val(DataItem.sell_price).trigger('change');
$("#Stock").val(DataItem.stock).trigger('change');
$("#ProductID").val(DataItem.ProductID).trigger('change'); //
Focus ON Quantity Input setTimeout(function () {
$("#Quantity").focus(); }); }; // Get Customer ID
function CustomerSelect(e) { var DataItem =
this.dataItem(e.item.index());
$("#CustomerID").val(DataItem.CustomerID).trigger('change'); };
// Total Items $('#Quantity').blur(function () { var
Quantity = $("#Quantity").val(); var SellPrice =
$("#SellPrice").val(); $("#Total_Item").val(Quantity *
SellPrice).trigger('change'); }); </script>}API Code:*
* readonly Model1 db = new Model1(); private static
List<Order_Details_VM> listOfOrders; private static List<Product>
listOfProducts; int statusCode = 0; public
CustomerServiceController() { listOfProducts = null;
if (listOfProducts == null) {
listOfProducts = new List<Product>(); foreach (var pro in
db.Products) { Product p = new
Product(); p.ProductID = pro.ProductID;
p.ProductName = pro.ProductName; p.sell_price =
pro.sell_price; p.stock = pro.stock;
listOfProducts.Add(p); } }
if (listOfOrders == null) { listOfOrders =
new List<Order_Details_VM>(); } } public
IHttpActionResult GetDetails() { return
Ok(listOfOrders); } public IHttpActionResult GetProduct(int
id) { var selectedProduct = listOfProducts.Where(i =>
i.ProductID == id).ToList(); return Ok(selectedProduct);
} public IHttpActionResult Post(Order_Details_VM newOrderDetails)
{ if (newOrderDetails != null) {
listOfOrders.Add(newOrderDetails); return
Ok(newOrderDetails); } return null; }
public IHttpActionResult SaveInvoice(Order_VM newOrderMaster) {
// save on header table to get invoice id if
(newOrderMaster != null) { var order = new
Order(); order.CustomerID = newOrderMaster.CustomerID;
order.OrderDate = newOrderMaster.OrderDate;
order.Freight = (decimal)newOrderMaster.Total; //order.type
= "بيع"; db.Orders.Add(order);
db.SaveChanges(); newOrderMaster.OrderID = order.OrderID;
} // loop throw items list to check Stock & add to
database with invoice no foreach (var item in listOfOrders)
{ item.OrderID = newOrderMaster.OrderID;
// ckeck Stock in online database not in the list var
model = db.Products.Find(item.ProductID); if (model.stock >=
item.Quantity) { model.stock =
model.stock - item.Quantity; db.Entry(model).State =
EntityState.Modified; statusCode = 200;
} else { statusCode =
404; } if (statusCode == 200)
{ // save to DB var orderdetails =
new Order_Detail(); orderdetails.OrderID =
item.OrderID; orderdetails.ProductID = item.ProductID;
orderdetails.UnitPrice = (decimal)item.SellPrice;
orderdetails.Quantity = item.Quantity;
orderdetails.Quantity_buy = item.Quantity;
orderdetails.Total = item.Total_Item;
db.Order_Details.Add(orderdetails); db.SaveChanges();
} else { var x
= item.ProductName; } }
listOfOrders.Clear(); return Ok(); } public
Order_Details_VM Delete(int id) { if (id > 0)
{ // here by productis var selectedCustomer =
listOfOrders.Find(p => p.ProductID == id); if
(selectedCustomer != null) {
listOfOrders.Remove(selectedCustomer); return
selectedCustomer; } } return new
Order_Details_VM(); } }*
*sorry for long code but hope that get help with that please ?*
*Thanks a lot ....*
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.