Files
moadiran/Back/Controllers/InvoiceController.cs

452 lines
22 KiB
C#
Raw Normal View History

2024-05-13 13:02:09 +03:30
using Back.Common;
using Back.Data.Models;
using Back.Services;
using Back.Validations;
2024-05-28 15:03:08 +03:30
using Microsoft.AspNetCore.Authorization;
2024-05-13 13:02:09 +03:30
using Microsoft.AspNetCore.Http;
2024-05-29 15:51:06 +03:30
using Microsoft.AspNetCore.Http.HttpResults;
2024-05-13 13:02:09 +03:30
using Microsoft.AspNetCore.Mvc;
using Shared.DTOs;
using Shared.DTOs.Serch;
namespace Back.Controllers
{
[Route("api/[controller]")]
2024-05-28 15:03:08 +03:30
[Authorize]
2024-05-13 13:02:09 +03:30
[ApiController]
public class InvoiceController : ControllerBase
{
private readonly servInvoice _servInvoice;
private readonly servUser _servUser;
private readonly AddOrUpdateInvoiceValidation _validationInvoice;
2024-05-13 16:50:26 +03:30
private readonly servTaxPayer _servTaxPayer;
public InvoiceController(servInvoice servInvoice, servUser servUser, AddOrUpdateInvoiceValidation validationInvoice, servTaxPayer servTaxPayer)
2024-05-13 13:02:09 +03:30
{
_servInvoice = servInvoice;
_servUser = servUser;
_validationInvoice = validationInvoice;
2024-05-13 16:50:26 +03:30
_servTaxPayer = servTaxPayer;
2024-05-13 13:02:09 +03:30
}
[HttpPost("GetAll")]
2024-05-17 23:05:46 +03:30
public async Task<ActionResult<PagingDto<InvoiceGridDTO>?>> GetAll([FromBody] ItemSerchGetInvoices itemSerch)
2024-05-13 13:02:09 +03:30
{
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
var UserID = claim.Value;
var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
return Ok(await _servInvoice.GetInvoices(user.RolUsers.First().CompanyID, itemSerch));
2024-05-17 23:05:46 +03:30
}
2024-05-30 15:08:36 +03:30
[HttpGet("Get/{ID}/{loaddelete}")]
public async Task<ActionResult<InvoiceDTO?>> GetAll(int ID,bool loaddelete)
2024-05-17 23:05:46 +03:30
{
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
var UserID = claim.Value;
var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
2024-05-30 15:08:36 +03:30
var result = await _servInvoice.GetInvoice(user.RolUsers.First().CompanyID, ID,!loaddelete);
2024-05-17 23:05:46 +03:30
2024-05-30 15:08:36 +03:30
return result==null ? BadRequest() : Ok(result);
2024-05-17 23:05:46 +03:30
2024-05-13 13:02:09 +03:30
}
2024-05-13 16:50:26 +03:30
[HttpPost("Add")]
public async Task<ActionResult<int>> Add([FromBody] NUInvoiceDTO item)
2024-05-13 13:02:09 +03:30
{
2024-05-26 13:04:53 +03:30
if (string.IsNullOrEmpty(item.InvoiceDate))
item.InvoiceDate = DateTime.Now.ConvertMiladiToShamsi();
2024-05-16 23:40:32 +03:30
if (string.IsNullOrEmpty(item.InvoicIssueDate))
item.InvoicIssueDate = DateTime.Now.ConvertMiladiToShamsi();
2024-05-26 17:25:34 +03:30
//if (!ModelState.IsValid)
// return BadRequest(item);
2024-05-13 13:02:09 +03:30
//-----GetUserAndCompany
2024-05-27 18:57:25 +03:30
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
var UserID = claim.Value;
2024-05-13 13:02:09 +03:30
var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
//-----Validaton
var resultValidationmodel = await _validationInvoice.ValidateAsync(Tuple.Create(user.RolUsers.First().CompanyID, item, eActionValidation.add));
if (!resultValidationmodel.IsValid)
return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());
//if (item.BillReference.HasValue)
//{
// Invoice ReferenceInvoice = await _servInvoice.GetInvoiceByInvoiceID(item.BillReference.Value);
// if (ReferenceInvoice == null) return NotFound("صورتحساب مرجع یافت نشد");
// if (await _servCompany.ExsistCompanyByComoanyIDandUserID(UserID, ReferenceInvoice.CompanyID.Value)) return Forbid("صورتحساب مرجع برای شما در دسترس نمی باشد");
//}
2024-05-27 18:57:25 +03:30
return Ok(await _servInvoice.AddInvoice(new Invoice()
2024-05-26 13:04:53 +03:30
{
Title = item.Title,
Des = item.Des,
invoiceType = InvoiceType.Bidding,
CustomerID = item.CustomerID,
CompanyID = user.RolUsers.First().CompanyID,
InvoicIssueDate = item.InvoicIssueDate.Replace("/", ""),
InvoiceDate = item.InvoicIssueDate.Replace("/", ""),
LastChangeUserID = Convert.ToInt32(UserID),
BillReference = null,
IsDeleted = false,
2024-06-08 21:48:26 +03:30
PatternID = item.PatternID,
setm=1
2024-05-26 13:04:53 +03:30
}));
2024-05-13 13:02:09 +03:30
2024-05-13 16:50:26 +03:30
}
[HttpPut("Update")]
2024-05-27 22:01:14 +03:30
public async Task<ActionResult<bool>> Update([FromBody] NUInvoiceDTO item)
2024-05-13 16:50:26 +03:30
{
2024-05-27 22:01:14 +03:30
//if (!ModelState.IsValid)
// return BadRequest(item);
2024-05-13 16:50:26 +03:30
//-----GetUserAndCompany
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
var UserID = claim.Value;
var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
//-----Validaton
var resultValidationmodel = await _validationInvoice.ValidateAsync(Tuple.Create(user.RolUsers.First().CompanyID, item, eActionValidation.update));
if (!resultValidationmodel.IsValid)
return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());
//-----Get invoice
2024-05-26 13:04:53 +03:30
Invoice invoice = await _servInvoice.GetInvoiceByInvoiceID(user.RolUsers.First().CompanyID, item.ID.Value);
2024-05-13 16:50:26 +03:30
if (invoice == null)
return BadRequest(new List<string> { "invoice notFound..." });
if (invoice.PatternID != item.PatternID || invoice.CustomerID != item.CustomerID
|| invoice.InvoicIssueDate != item.InvoicIssueDate || invoice.InvoiceDate != item.InvoiceDate)
{
//----Check TaxPayer
if (await _servTaxPayer.ExistSuccessfulorSendorpendingInvoice(invoice))
return BadRequest(new List<string> { "این صورتحساب به سازمان ارسال شده"+'\n'+
2024-05-26 13:04:53 +03:30
"برای تغییر ،صورتحساب را ابطال/اصلاح یا برگشت بزنید"});
2024-05-27 22:01:14 +03:30
else if (invoice.invoiceType == InvoiceType.BackFrmSale)
2024-05-26 13:04:53 +03:30
{
return BadRequest(new List<string>
{ "صورتحساب در وضعیت برگشت از فروش نمی تواند ویرایش شود" });
}
2024-05-25 21:30:11 +03:30
else if (invoice.invoiceType != InvoiceType.Bidding
2024-05-26 13:04:53 +03:30
&& invoice.invoiceType != InvoiceType.Sale && invoice.invoiceType != InvoiceType.Repair)
2024-05-25 21:30:11 +03:30
{
2024-05-26 13:04:53 +03:30
return BadRequest(new List<string> { $"صورتحساب در حالت {invoice.invoiceType.GetEnumDisplayName()} نمی تواند ویرایش شود"+'\n'+
$"فقط در حالت پیش نویس ، فاکتور و اصلاح (که به سازمان ارسال نشده باشد) میتوان سند را ویرایش کرد" });
2024-05-25 21:30:11 +03:30
}
2024-05-13 16:50:26 +03:30
}
2024-05-16 23:40:32 +03:30
2024-05-13 16:50:26 +03:30
//-----change invoice
2024-05-16 23:40:32 +03:30
if (!string.IsNullOrEmpty(item.Title))
invoice.Title = item.Title;
2024-05-13 16:50:26 +03:30
invoice.PatternID = item.PatternID;
2024-05-16 23:40:32 +03:30
2024-05-26 13:04:53 +03:30
if (item.CustomerID > 0)
2024-05-16 23:40:32 +03:30
invoice.CustomerID = item.CustomerID;
if (!string.IsNullOrEmpty(item.InvoicIssueDate))
invoice.InvoicIssueDate = item.InvoicIssueDate;
if (!string.IsNullOrEmpty(item.InvoiceDate))
invoice.InvoiceDate = item.InvoiceDate;
2024-05-13 16:50:26 +03:30
invoice.Des = item.Des;
invoice.LastChangeUserID = Convert.ToInt32(UserID);
return Ok(await _servInvoice.UpdateInvoice(invoice));
}
[HttpDelete("Delete/{ID}")]
public async Task<ActionResult<bool>> Delete(int ID)
{
//-----GetUserAndCompany
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
var UserID = claim.Value;
var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
//-----Get invoice
2024-05-26 13:04:53 +03:30
Invoice invoice = await _servInvoice.GetInvoiceByInvoiceID(user.RolUsers.First().CompanyID, ID);
2024-05-13 16:50:26 +03:30
if (invoice == null)
return NotFound();
2024-05-25 21:30:11 +03:30
//----Check TaxPayer
if (await _servTaxPayer.ExistSuccessfulorSendorpendingInvoice(invoice))
2024-05-30 15:08:36 +03:30
return BadRequest(new List<string> { "این صورتحساب به سازمان ارسال شده"});
2024-05-26 13:04:53 +03:30
//else if (invoice.invoiceType != InvoiceType.Bidding
// && invoice.invoiceType != InvoiceType.Sale)
//{
// return BadRequest(new List<string> { $"صورتحساب در حالت {invoice.invoiceType.GetEnumDisplayName()} نمی تواند ویرایش شود" });
//}
2024-05-25 21:30:11 +03:30
2024-05-13 16:50:26 +03:30
invoice.LastChangeUserID = Convert.ToInt32(UserID);
//----Update and sendResult
return Ok(await _servInvoice.DeleteInvoice(invoice));
}
[HttpPost("ChangeInvoiceType/{InvoiceID}")]// ok
2024-05-30 15:08:36 +03:30
public async Task<ActionResult<InvoiceDTO>> ChangeInvoiceType(int InvoiceID, int invoiceType)
2024-05-13 16:50:26 +03:30
{
//-----GetUserAndCompany
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
var UserID = claim.Value;
var user = await _servUser.GetUserByUserID(Convert.ToInt32(UserID));
2024-05-26 13:04:53 +03:30
Invoice? Invoice = await _servInvoice.GetInvoiceByInvoiceID(user.RolUsers.First().CompanyID, InvoiceID);
2024-05-13 16:50:26 +03:30
if (Invoice == null) return NotFound();
2024-05-26 13:04:53 +03:30
2024-05-30 15:08:36 +03:30
if (Invoice.invoiceType == InvoiceType.Cancellation) return BadRequest(new List<string> { "این صورتحساب ابطال شده" });
2024-05-13 16:50:26 +03:30
var sent = await _servTaxPayer.ExistSuccessfulorSendorpendingInvoice(Invoice);
Invoice.LastChangeUserID = Convert.ToInt32(UserID);
if (Invoice.invoiceType == InvoiceType.Bidding)
{
switch (invoiceType)
{
2024-05-30 15:08:36 +03:30
case 1:
return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.Sale));
2024-05-13 16:50:26 +03:30
2024-05-30 15:08:36 +03:30
case 3:
return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.Cancellation));
//case 0:
// return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.CANCEL));
2024-05-13 16:50:26 +03:30
default:
2024-05-30 15:08:36 +03:30
return BadRequest(new List<string> { $"تغییر وضعیت از {Invoice.invoiceType.GetEnumDisplayName()} به {((InvoiceType)invoiceType).GetEnumDisplayName()} امکان پذیر نیست" });
2024-05-13 16:50:26 +03:30
}
}
if (Invoice.invoiceType == InvoiceType.Sale)
{
2024-05-30 15:08:36 +03:30
//if (sent)
//{
2024-05-13 16:50:26 +03:30
switch (invoiceType)
{
2024-05-30 15:08:36 +03:30
//case 0:
// return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.CANCEL));
case 3:
return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.Cancellation));
2024-05-13 16:50:26 +03:30
2024-05-30 15:08:36 +03:30
case 2:
await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.Repair, false);
2024-05-29 15:51:06 +03:30
var result = await _servInvoice.AddInvoice(new Invoice()
2024-05-13 16:50:26 +03:30
{
Title = Invoice.Title,
Des = Invoice.Des,
2024-05-30 15:08:36 +03:30
invoiceType = InvoiceType.Repair,
2024-05-13 16:50:26 +03:30
CustomerID = Invoice.CustomerID,
CompanyID = Invoice.CompanyID,
InvoicIssueDate = Invoice.InvoicIssueDate.Replace("/", ""),
InvoiceDate = Invoice.InvoicIssueDate.Replace("/", ""),
LastChangeUserID = Convert.ToInt32(UserID),
BillReference = Invoice.ID,
IsDeleted = false,
2024-05-30 15:08:36 +03:30
PatternID = Invoice.PatternID,
2024-06-08 21:48:26 +03:30
setm=Invoice.setm,
2024-05-30 15:08:36 +03:30
invoiceDetails=Invoice.invoiceDetails.Select(s=>new InvoiceItem
{
CODID=s.CODID,
am=s.am,
fee=s.fee,
dis=s.dis,
}).ToList()
2024-05-29 15:51:06 +03:30
}, false);
if (result > 0)
return Ok(await _servInvoice.GetInvoice(user.RolUsers.First().CompanyID,result));
break;
2024-05-30 15:08:36 +03:30
case 4:
await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.BackFrmSale, false);
2024-05-29 15:51:06 +03:30
var result1 = await _servInvoice.AddInvoice(new Invoice()
2024-05-13 16:50:26 +03:30
{
Title = Invoice.Title,
Des = Invoice.Des,
2024-05-30 15:08:36 +03:30
invoiceType = InvoiceType.BackFrmSale,
2024-05-13 16:50:26 +03:30
CustomerID = Invoice.CustomerID,
CompanyID = Invoice.CompanyID,
InvoicIssueDate = Invoice.InvoicIssueDate.Replace("/", ""),
InvoiceDate = Invoice.InvoicIssueDate.Replace("/", ""),
LastChangeUserID = Convert.ToInt32(UserID),
BillReference = Invoice.ID,
IsDeleted = false,
2024-05-30 15:08:36 +03:30
PatternID = Invoice.PatternID,
2024-06-08 21:48:26 +03:30
setm = Invoice.setm,
2024-05-30 15:08:36 +03:30
invoiceDetails = Invoice.invoiceDetails.Select(s => new InvoiceItem
{
CODID = s.CODID,
am = s.am,
fee = s.fee,
dis = s.dis,
}).ToList()
2024-05-29 15:51:06 +03:30
}, false);
if (result1 > 0)
return Ok(await _servInvoice.GetInvoice(user.RolUsers.First().CompanyID, result1));
break;
2024-05-13 16:50:26 +03:30
default:
2024-05-30 15:08:36 +03:30
return BadRequest(new List<string> { $"تغییر وضعیت از {Invoice.invoiceType.GetEnumDisplayName()} به {((InvoiceType)invoiceType).GetEnumDisplayName()} امکان پذیر نیست این صورتحساب به سامانه مودیان ارسال شده" });
2024-05-13 16:50:26 +03:30
}
2024-05-30 15:08:36 +03:30
//}
//else
//{
// switch (invoiceType)
// {
// case 0:
// return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.CANCEL));
// default:
// return BadRequest(new List<string> { $"تغییر وضعیت از {Invoice.invoiceType.GetEnumDisplayName()} به {((InvoiceType)invoiceType).GetEnumDisplayName()} امکان پذیر نیست" });
// }
//}
2024-05-13 16:50:26 +03:30
}
2024-05-30 15:08:36 +03:30
//if (Invoice.invoiceType == InvoiceType.Cancellation)
//{
// if (!sent)
// {
// switch (invoiceType)
// {
// //case InvoiceType.Sale:
// // return Ok(await _servInvoice.ChangeInvoiceType(Invoice, invoiceType));
// case 0:
// return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.CANCEL));
// default:
// return BadRequest(new List<string> { $"تغییر وضعیت از {Invoice.invoiceType.GetEnumDisplayName()} به {((InvoiceType)invoiceType).GetEnumDisplayName()} امکان پذیر نیست" });
// }
// }
// else return BadRequest(new List<string> { $"تغییر وضعیت از {Invoice.invoiceType.GetEnumDisplayName()} به {((InvoiceType)invoiceType).GetEnumDisplayName()} امکان پذیر نیست این صورتحساب به سامانه مودیان ارسال شده" });
//}
2024-05-13 16:50:26 +03:30
if (Invoice.invoiceType == InvoiceType.Repair)
{
2024-05-30 15:08:36 +03:30
//if (!sent)
//{
// switch (invoiceType)
// {
// case 0:
// return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.CANCEL));
// default:
// return BadRequest(new List<string> { $"تغییر وضعیت از {Invoice.invoiceType.GetEnumDisplayName()} به {((InvoiceType)invoiceType).GetEnumDisplayName()} امکان پذیر نیست" });
// }
//}
//else
//{
2024-05-13 16:50:26 +03:30
switch (invoiceType)
{
2024-05-30 15:08:36 +03:30
case 3:
return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.Cancellation));
2024-05-13 16:50:26 +03:30
2024-05-30 15:08:36 +03:30
case 4:
await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.BackFrmSale, false);
2024-05-29 15:51:06 +03:30
var result2 = await _servInvoice.AddInvoice(new Invoice()
2024-05-13 16:50:26 +03:30
{
Title = Invoice.Title,
Des = Invoice.Des,
2024-05-30 15:08:36 +03:30
invoiceType = InvoiceType.BackFrmSale,
2024-05-13 16:50:26 +03:30
CustomerID = Invoice.CustomerID,
CompanyID = Invoice.CompanyID,
InvoicIssueDate = Invoice.InvoicIssueDate.Replace("/", ""),
InvoiceDate = Invoice.InvoicIssueDate.Replace("/", ""),
LastChangeUserID = Convert.ToInt32(UserID),
BillReference = Invoice.ID,
IsDeleted = false,
2024-05-30 15:08:36 +03:30
PatternID = Invoice.PatternID,
2024-06-08 21:48:26 +03:30
setm = Invoice.setm,
invoiceDetails =Invoice.invoiceDetails.Select(s => new InvoiceItem
2024-05-30 15:08:36 +03:30
{
CODID = s.CODID,
am = s.am,
fee = s.fee,
dis = s.dis,
}).ToList()
2024-05-29 15:51:06 +03:30
}, false);
if (result2 > 0)
return Ok(await _servInvoice.GetInvoice(user.RolUsers.First().CompanyID, result2));
break;
2024-05-13 16:50:26 +03:30
default:
2024-05-30 15:08:36 +03:30
return BadRequest(new List<string> { $"تغییر وضعیت از {Invoice.invoiceType.GetEnumDisplayName()} به {((InvoiceType)invoiceType).GetEnumDisplayName()} امکان پذیر نیست این صورتحساب به سامانه مودیان ارسال شده" });
2024-05-13 16:50:26 +03:30
}
2024-05-30 15:08:36 +03:30
//}
2024-05-13 16:50:26 +03:30
}
if (Invoice.invoiceType == InvoiceType.BackFrmSale)
{
2024-05-30 15:08:36 +03:30
// if (!sent)
// {
2024-05-13 16:50:26 +03:30
switch (invoiceType)
{
2024-05-30 15:08:36 +03:30
//case 0:
// return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.CANCEL));
2024-05-13 16:50:26 +03:30
2024-05-30 15:08:36 +03:30
case 3:
return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.Cancellation));
2024-05-13 16:50:26 +03:30
default:
2024-05-30 15:08:36 +03:30
return BadRequest(new List<string> { $"تغییر وضعیت از {Invoice.invoiceType.GetEnumDisplayName()} به {((InvoiceType)invoiceType).GetEnumDisplayName()} امکان پذیر نیست" });
2024-05-13 16:50:26 +03:30
}
2024-05-30 15:08:36 +03:30
// }
//else
//{
// switch (invoiceType)
// {
// case 3:
// return Ok(await _servInvoice.ChangeInvoiceType(Invoice, InvoiceType.Cancellation));
// //case InvoiceType.Repair:
// // await _servInvoice.ChangeInvoiceType(Invoice, invoiceType, false);
// // return Ok(await _servInvoice.AddInvoice(new Invoice()
// // {
// // Title = Invoice.Title,
// // Des = Invoice.Des,
// // invoiceType = invoiceType,
// // CustomerID = Invoice.CustomerID,
// // CompanyID = Invoice.CompanyID,
// // InvoicIssueDate = Invoice.InvoicIssueDate.Replace("/", ""),
// // InvoiceDate = Invoice.InvoicIssueDate.Replace("/", ""),
// // LastChangeUserID = Convert.ToInt32(UserID),
// // BillReference = Invoice.ID,
// // IsDeleted = false,
// // PatternID = Invoice.PatternID
// // }, false));
// default:
// return BadRequest(new List<string> { $"تغییر وضعیت از {Invoice.invoiceType.GetEnumDisplayName()} به {((InvoiceType)invoiceType).GetEnumDisplayName()} امکان پذیر نیست این صورتحساب به سامانه مودیان ارسال شده" });
// }
//}
2024-05-13 16:50:26 +03:30
}
return NoContent();
2024-05-13 13:02:09 +03:30
}
2024-05-16 23:40:32 +03:30
[HttpGet("GetPatterns")]
public async Task<ActionResult<List<IdName<int>>>> GetPatterns()
{
return Ok(await _servTaxPayer.GetPatterns());
}
2024-06-14 22:37:22 +03:30
[HttpGet("GetReport/{InvoiceID}")]
public IActionResult GetReport(int InvoiceID)
2024-05-30 15:08:36 +03:30
{
2024-06-14 22:37:22 +03:30
var reportPath = $"Reports\\invoice.mrt";
2024-05-30 15:08:36 +03:30
var bytes = System.IO.File.ReadAllBytes(reportPath);
return new FileContentResult(bytes, "application/xml");
}
2024-05-13 13:02:09 +03:30
}
}