PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/beta/AR/InvoiceEdit.asp

http://github.com/khaneh/Orders
ASP | 1514 lines | 1127 code | 171 blank | 216 comment | 162 complexity | f93a5fdcfb1610c18b07624add893b81 MD5 | raw file
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="1256"%><%
  2. ' AR (6)
  3. PageTitle="اصلاح فاكتور "
  4. SubmenuItem=1
  5. 'if not Auth(6 , 1) then NotAllowdToViewThisPage()
  6. %>
  7. <!--#include file="top.asp" -->
  8. <!--#include File="../include_farsiDateHandling.asp"-->
  9. <!--#include File="../include_JS_InputMasks.asp"-->
  10. <%
  11. function ShowErrorMessage(msg)
  12. response.write "<table align='center' cellpadding='5'><tr><td bgcolor='#FFCCCC' dir='rtl' align='center'> خطا ! <br>"& msg & "<br></td></tr></table><br>"
  13. end function
  14. function Link2Trace(OrderNo)
  15. Link2Trace="<A HREF='../order/TraceOrder.asp?act=show&order="& OrderNo & "' target='_balnk'>"& OrderNo & "</A>"
  16. end function
  17. function Link2TraceQuote(QuoteNo)
  18. Link2TraceQuote = "<A HREF='../order/Inquiry.asp?act=show&quote="& QuoteNo & "' target='_balnk'>"& QuoteNo & "</A>"
  19. end function
  20. %>
  21. <style>
  22. Table { font-size: 9pt;}
  23. div.InvHead {background-color: #CC8;text-align: center;padding: 5px 0;}
  24. div.InvHead2 {background-color: #AC7;text-align: center;padding: 5px 0;}
  25. div.InvHead3 {background-color: #F0F0F0;text-align: center;padding: 5px 0;}
  26. div.InvHead4 {background-color: #F90;text-align: center;padding: 5px 0;}
  27. div.InvLine {background-color: #F0F0F0;text-align: right;}
  28. div.InvLine2 {background-color: #F0FFF0;text-align: right;}
  29. div.InvLine4 {background-color: #FFD3A8;text-align: right;}
  30. .InvRowInput { font-family:tahoma; font-size: 9pt; border: none; background-color: #F0F0F0; text-align:right;}
  31. .InvHeadInput { font-family:tahoma; font-size: 9pt; border: none; background-color: #CCCC88; text-align:center;}
  32. .InvRowInput2 { font-family:tahoma; font-size: 9pt; border: none; background-color: #F0FFF0; text-align:right; height: 22px; margin: 0;padding: 0;}
  33. .InvRowInput4 { font-family:tahoma; font-size: 9pt; border: none; background-color: #FFD3A8; direction:LTR; text-align:right;}
  34. .InvHeadInput2 { font-family:tahoma; font-size: 9pt; border: none; background-color: #AACC77; text-align:center;}
  35. .InvHeadInput3 { font-family:tahoma; font-size: 9pt; border: none; background-color: #F0F0F0; text-align:right;}
  36. .InvHeadInput4 { font-family:tahoma; font-size: 9pt; border: none; background-color: #FF9900; text-align:center;}
  37. .InvGenInput { font-family:tahoma; font-size: 9pt; border: none; }
  38. .InvGenButton { font-family:tahoma; font-size: 9pt; border: 1px solid black; }
  39. .order {background-color: #C3C300;font-weight: bold;padding: 6px 10px 6px 10px;}
  40. .quote {background-color: #AAAAEE;font-weight: bold;padding: 6px 10px 6px 10px;}
  41. #header {width: 100%;border-left: 1px solid #585;border-right: 1px solid #585;}
  42. #header div {padding: 5px 2px;float: right;border-left: 1px solid #585; text-align: center;}
  43. div.invRow {clear: both;float: none;width: 100%;}
  44. div.invRow div {padding: 5px 2px 0 2px;float: right;border-bottom: 1px solid #585;border-left: 1px solid #585;text-align: right;height: 22px;}
  45. table.invTable {border-collapse:collapse; width:100%;}
  46. #tbody{height:60px;overflow-y:auto;width:100%;background:yellow;border-top: 1px solid #585;}
  47. td[class*="td"] {padding: 1px 2px;border-left: 1px solid red;border-bottom: 1px solid red;}
  48. div.td15{width: 15px;}
  49. td.td15{width: 15px;}
  50. div.td40{width: 40px;}
  51. td.td40{width: 40px;}
  52. div.td170{width: 170px;}
  53. td.td170{width: 170px;}
  54. div.td60{width: 60px;}
  55. td.td60{width: 60px;}
  56. div.td80{width: 80px;}
  57. td.td80{width: 80px;}
  58. div.td35{width: 35px;}
  59. td.td35{width: 35px;}
  60. div.td25{width: 25px;}
  61. td.td25{width: 25px;}
  62. div.td55{width: 55px;}
  63. td.td55{width: 55px;}
  64. div.td50{width: 50px;}
  65. td.td50{width: 50px;}
  66. div.td75{width: 75px;}
  67. td.td75{width: 75px;}
  68. </style>
  69. <SCRIPT LANGUAGE="JavaScript">
  70. <!--
  71. var okToProceed=false;
  72. var currentRow=0;
  73. //-->
  74. </SCRIPT>
  75. <%
  76. if request("act")="search" then
  77. if isnumeric(request("order")) then
  78. OrderID = clng(request("order"))
  79. mySQL="SELECT InvoiceOrderRelations.Invoice FROM InvoiceOrderRelations INNER JOIN Invoices ON InvoiceOrderRelations.Invoice = Invoices.ID WHERE (InvoiceOrderRelations.[Order] = '"& OrderID & "') AND (Invoices.IsReverse = 0) AND (Invoices.Voided = 0)"
  80. Set RS1=Conn.Execute(mySQL)
  81. if RS1.eof then
  82. Conn.close
  83. response.redirect "?errmsg=" & Server.URLEncode("اين سفارش قبلا فاكتور نشده است.")
  84. else
  85. theInvoice=RS1("Invoice")
  86. Conn.close
  87. response.redirect "?act=editInvoice&invoice=" & theInvoice
  88. end if
  89. elseif isnumeric(request("invoice")) then
  90. response.redirect "?act=editInvoice&invoice=" & request("invoice")
  91. else
  92. response.redirect "?errmsg=" & Server.URLEncode("شماره سفارش قابل قبول نمي باشد.")
  93. end if
  94. '-----------------------------------------------------------------------------------------------------
  95. '---------------------------------------------------------------------------------------- Edit Invoice
  96. '-----------------------------------------------------------------------------------------------------
  97. elseif request("act")="editInvoice" then
  98. if isnumeric(request("invoice")) then
  99. InvoiceID=clng(request("invoice"))
  100. mySQL="SELECT * FROM Invoices WHERE (ID='"& InvoiceID & "')"
  101. Set RS1 = conn.Execute(mySQL)
  102. if RS1.eof then
  103. conn.close
  104. response.redirect "?errmsg=" & Server.URLEncode("فاكتور پيدا نشد.")
  105. end if
  106. else
  107. response.redirect "?errmsg=" & Server.URLEncode("شماره فاكتور قابل قبول نمي باشد.")
  108. end if
  109. customerID= RS1("Customer")
  110. creationDate= RS1("CreatedDate")
  111. IssuedDate= RS1("IssuedDate")
  112. totalPrice= cdbl(RS1("totalPrice"))
  113. totalDiscount= cdbl(RS1("totalDiscount"))
  114. totalReverse= cdbl(RS1("totalReverse"))
  115. totalVat = cdbl(RS1("totalVat"))
  116. Voided= RS1("Voided")
  117. Issued= RS1("Issued")
  118. Approved= RS1("Approved")
  119. isReverse= RS1("IsReverse")
  120. IsA= RS1("IsA")
  121. InvoiceNo= RS1("Number")
  122. mySQL="SELECT ID,AccountTitle FROM Accounts WHERE (ID='"& customerID & "')"
  123. Set RS1 = conn.Execute(mySQL)
  124. AccountNo=RS1("ID")
  125. customerName=RS1("AccountTitle")
  126. RS1.close
  127. if isReverse then
  128. 'Check for permission for EDITTING Rev. Invoice
  129. if not Auth(6 , 5) then NotAllowdToViewThisPage()
  130. itemTypeName="فاكتور برگشت"
  131. HeaderColor="#FF9900"
  132. else
  133. 'Check for permission for EDITTING Invoice
  134. if not Auth(6 , 3) then NotAllowdToViewThisPage()
  135. itemTypeName="فاكتور"
  136. HeaderColor="#C3C300"
  137. end if
  138. if Voided then
  139. Conn.close
  140. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور باطل شده است.")
  141. elseif Issued then
  142. if Auth(6 , "A") then
  143. ' Has the Priviledge to change the Invoice
  144. response.write "<BR>"
  145. if Auth(6,"N") then
  146. set rs=Conn.Execute("select * from effectiveGlrows where sys='AR' and link in (select id from arItems where type=1 and reason=1 and link=" & invoiceID & ")")
  147. if rs.eof then
  148. call showAlert ("اين فاكتور صادر شده است.<br>هرچند كه شما اجازه داريد اين فاكتور را دچار تغييرات كلي كنيد<br>مسوليت مشكلات احتمالي را مي پذيريد؟",CONST_MSG_INFORM)
  149. else
  150. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور سند حسابداري دارد، <br>شما كه مي‌خواهيد تغييرات كلي بديد، ابتدا بايد آنرا باطل كنيد.")
  151. end if
  152. else
  153. call showAlert ("اين فاكتور صادر شده است.<br>هرچند كه شما اجازه داريد كه اين فاكتور را تغيير بدهيد<br>مسوليت مشكلات احتمالي را مي پذيريد؟",CONST_MSG_INFORM)
  154. end if
  155. else
  156. Conn.close
  157. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور صادر شده است.")
  158. end if
  159. end if
  160. %>
  161. <!-- ورود اطلاعات فاكتور -->
  162. <br>
  163. <input type="hidden" Name='tmpDlgArg' value=''>
  164. <input type="hidden" Name='tmpDlgTxt' value=''>
  165. <input type="hidden" name="VatRate" id="VatRate" value="<%=session("VatRate")%>">
  166. <FORM METHOD=POST ACTION="?act=submitEdit">
  167. <table Border="0" align="center" Width="100%" Cellspacing="1" Cellpadding="0" Dir="RTL" bgcolor="#558855">
  168. <tr bgcolor='<%=HeaderColor%>'>
  169. <td colspan='2'>
  170. <TABLE width='100%'>
  171. <TR>
  172. <TD align="left" >شماره <%=itemTypeName%>:</TD>
  173. <TD align="right" width='15%'>&nbsp;<INPUT readonly class="InvGenInput" NAME="InvoiceID" value="<%=InvoiceID%>" style="direction:ltr" TYPE="text" maxlength="10" size="10"></TD>
  174. </TR>
  175. </TABLE></td>
  176. </tr>
  177. <tr bgcolor='<%=HeaderColor%>'>
  178. <td colspan="2">
  179. <TABLE Border="0" Width="100%" Cellspacing="1" Cellpadding="0" Dir="RTL">
  180. <TR>
  181. <TD><table>
  182. <tr>
  183. <td align="left">حساب:</td>
  184. <td align="right">
  185. <span id="customer"><%' after any changes in this span "./Customers.asp" must be revised%>
  186. <INPUT TYPE="hidden" NAME="customerID" value="<%=customerID%>"><span><%=CustomerName%></span>.
  187. </span></td>
  188. <td></td>
  189. </tr>
  190. </table></TD>
  191. <TD align="left"><table>
  192. <tr>
  193. <td align="left">تاريخ صدور:</td>
  194. <td dir="LTR">
  195. <INPUT <%'if not Auth(6,"N") then response.write " readonly='readonly' "%> class="InvGenInput" NAME="issueDate" TYPE="text" maxlength="10" size="10" value="<%=IssuedDate%>" onblur="acceptDate(this)"></td>
  196. <td dir="RTL"><%="؟شنبه"%></td>
  197. </tr>
  198. </table></TD>
  199. </TR></TABLE>
  200. </td>
  201. </tr>
  202. <tr bgcolor='<%=HeaderColor%>'>
  203. <TD align="right" width="50%">
  204. مربوط به سفارش/استعلام(هاي):
  205. <%
  206. mySQL="SELECT * FROM InvoiceOrderRelations inner join Orders on InvoiceOrderRelations.[order]=orders.id WHERE (Invoice='"& InvoiceID & "')"
  207. Set RS1 = conn.Execute(mySQL)
  208. if not rs1.eof then
  209. if rs1("isOrder")="True" then
  210. isOrder="order"
  211. else
  212. isOrder="quote"
  213. end if
  214. end if
  215. %>
  216. <span id="orders" class="<%=isOrder%>">
  217. <%
  218. tempWriteAnd=""
  219. while not(RS1.eof)
  220. response.write tempWriteAnd & Link2Trace(RS1("Order"))
  221. tempWriteAnd=" و "
  222. RS1.moveNext
  223. wend
  224. %> </span>&nbsp;
  225. </TD>
  226. <TD align="left"><table>
  227. <tr>
  228. <td align="left">شماره:</td>
  229. <td dir="LTR">
  230. <INPUT class="InvGenInput" NAME="InvoiceNo" value="<%=InvoiceNo%>" style="border:1px solid black;" TYPE="text" maxlength="10" size="10"></td>
  231. <td dir="RTL">
  232. <INPUT TYPE="checkbox" value="on" NAME="IsA" onClick='checkIsA();'
  233. <% if IsA then response.write " checked='checked' "
  234. if Issued and not Auth(6,"N") then response.write " disabled='disabled' "%>> الف &nbsp;
  235. </td>
  236. </tr>
  237. </table></TD>
  238. </tr>
  239. <tr bgcolor='#CCCC88'>
  240. <TD colspan="10">
  241. <div id="header">
  242. <div class="td15 InvHead">#</div>
  243. <div class="td40 InvHead2">آيتم</div>
  244. <div class="td170 InvHead2">توضيحات</div>
  245. <div class="td25 InvHead2">طول</div>
  246. <div class="td25 InvHead2">عرض</div>
  247. <div class="td25 InvHead2">تعداد</div>
  248. <div class="td25 InvHead2">فرم</div>
  249. <div class="td50 InvHead">تعداد موثر</div>
  250. <div class="td40 InvHead">في</div>
  251. <div class="td50 InvHead">قيمت</div>
  252. <div class="td50 InvHead">تخفيف</div>
  253. <div class="td40 InvHead">برگشت</div>
  254. <div class="td50 InvHead4">ماليات</div>
  255. <div class="td75 InvHead2">قابل پرداخت</div>
  256. </div>
  257. <div id="tbody">
  258. <%
  259. i=0
  260. mySQL="SELECT *,InvoiceLines.ID as lineID,isnull(invoiceItems.fee,0) as ItemFee FROM InvoiceLines LEFT OUTER JOIN invoiceItems ON InvoiceLines.item = invoiceItems.id WHERE (Invoice='"& InvoiceID & "') "
  261. Set RS1 = conn.Execute(mySQL)
  262. while not(RS1.eof)
  263. if RS1("Item") <> 39999 then
  264. i=i+1
  265. %>
  266. <div class="invRow">
  267. <div class="td15 InvLine"><%=i%></div>
  268. <div class="td40 InvLine2"><%=RS1("Item")%>
  269. <input type='hidden' name='hasVat' value='<%=text2value(RS1("hasVat"))%>'>
  270. <input type='hidden' name='lineID' value='<%=RS1("lineID")%>'>
  271. </div>
  272. <div class="td170 InvLine2" style="padding:2px 2px 3px 2px;">
  273. <INPUT class="InvRowInput2" TYPE="text" NAME="Descriptions" value="<%=RS1("Description")%>" size="30">
  274. </div>
  275. <div class="td25 InvLine2"><%=RS1("Length")%></div>
  276. <div class="td25 InvLine2"><%=RS1("Width")%></div>
  277. <div class="td25 InvLine2"><%=RS1("Qtty")%></div>
  278. <div class="td25 InvLine2"><%=RS1("Sets")%></div>
  279. <div class="td50 InvLine"><%=Separate(RS1("AppQtty"))%></div>
  280. <div class="td40 InvLine"><%=Separate(RS1("Price")/RS1("AppQtty"))%></div>
  281. <div class="td50 InvLine"><%=Separate(RS1("Price"))%></div>
  282. <div class="td50 InvLine"><%=Separate(RS1("Discount"))%></div>
  283. <div class="td40 InvLine"><%=Separate(RS1("Reverse"))%></div>
  284. <div class="td50 InvLine4"><%=Separate(RS1("Vat"))%></div>
  285. <div class="td75 InvLine2"><%=Separate(RS1("Price") - RS1("Discount") - RS1("Reverse") + RS1("Vat"))%></div>
  286. </div>
  287. <%
  288. end if
  289. RS1.moveNext
  290. wend
  291. RS1.close
  292. %>
  293. </div>
  294. </TD>
  295. </TR>
  296. <tr bgcolor='#CCCC88'>
  297. <TD colspan="10"><div>
  298. <TABLE Border="0" Cellspacing="1" Cellpadding="0" Dir="RTL" bgcolor="#CCCC88">
  299. <tr bgcolor='#CCCC88'>
  300. <td colspan='9' width='500px'>***تخفيف رند فاكتور بعد از ثبت درج خواهد شد***</td>
  301. <td dir="LTR"><INPUT readonly class="InvHeadInput3" Name="TotalPrice" value="<%=Separate(totalPrice)%>" TYPE="text" size="9"></td>
  302. <!-- S A M -->
  303. <td dir="LTR"><INPUT readonly class="InvHeadInput3" Name="TotalDiscount" value="<%=Separate(totalDiscount)%>" TYPE="text" size="7"></td>
  304. <td dir="LTR"><INPUT readonly class="InvHeadInput3" Name="TotalReverse" value="<%=Separate(totalReverse)%>" TYPE="text" size="5"></td>
  305. <td dir="LTR"><INPUT readonly class="InvHeadInput3" Name="TotalVat" value="<%=Separate(totalVat)%>" TYPE="text" size="6"></td>
  306. <td dir="LTR"><INPUT readonly class="InvHeadInput3" Name="Payable" value="<%=Separate(totalPrice - totalDiscount - totalReverse + totalVat)%>" TYPE="text" size="9"></td>
  307. </tr>
  308. <tr bgcolor='#CCCC88'>
  309. <td colspan="9"> &nbsp; </td>
  310. <td dir="LTR"><INPUT readonly class="InvHeadInput" TYPE="text" size="9"></td>
  311. <!-- S A M -->
  312. <td dir="LTR"><INPUT readonly class="InvHeadInput3" TYPE="text" Name="TPDiscount" value="<%=Pourcent(totalDiscount,totalPrice) & "%تخفيف"%>" size="7"></td>
  313. <td dir="LTR"><INPUT readonly class="InvHeadInput3" TYPE="text" Name="TPReverse" value="<%=Pourcent(totalReverse,totalPrice) & "%برگشت"%>" size="5"></td>
  314. <td dir="LTR"><INPUT readonly calss="InvHeadINput" TYPE="text" size="6" value="<%=session("VatRate")%>%ماليات"></td>
  315. <td dir="LTR"><INPUT readonly class="InvHeadInput" TYPE="text" size="9" value="رند شده"></td>
  316. </tr>
  317. </TABLE></div></TD>
  318. </TR>
  319. </table>
  320. <TABLE Border="0" Cellspacing="5" Cellpadding="0" Dir="RTL" align='left'>
  321. <tr>
  322. <td align='center'>&nbsp;<!-- <INPUT class="InvGenButton" TYPE="button" value="تاييد فاكتور" onclick="ApproveInvoice();"> --></td>
  323. <td width="40">&nbsp;</td>
  324. <td align='center'>&nbsp;<!-- <INPUT class="InvGenButton" TYPE="button" value="صدور فاكتور" onclick="IssueInvoice();"> --></td>
  325. <td width="40">&nbsp;</td>
  326. <td align='center'><INPUT class="InvGenButton" TYPE="button" value="ذخيره " onclick="submitOperations();"></td>
  327. <td align='center'><INPUT class="InvGenButton" TYPE="button" value="انصراف" onclick="window.location='AccountReport.asp?act=showInvoice&invoice=<%=InvoiceID%>';"></td>
  328. </tr>
  329. </TABLE>
  330. </FORM>
  331. <SCRIPT LANGUAGE="JavaScript">
  332. <!--
  333. //document.getElementsByName("Items")[0].focus();
  334. //-->
  335. </SCRIPT>
  336. <%
  337. elseif request("act")="submitEdit" then
  338. '******************** Checking and Preparing Input ****************
  339. errorFound=false
  340. ON ERROR RESUME NEXT
  341. InvoiceID= clng(request.form("InvoiceID"))
  342. CustomerID= clng(request.form("CustomerID"))
  343. issueDate= request.form("issueDate")
  344. ' response.write "is A: " & request.form("IsA")
  345. if request.form("IsA") = "on" then
  346. IsA=1
  347. InvoiceNo=request.form("InvoiceNo")
  348. if InvoiceNo <> "" then InvoiceNo = cdbl(InvoiceNo)
  349. else
  350. set rs = Conn.Execute("select * from invoices where id=" & invoiceID)
  351. IsA=0
  352. InvoiceNo=""
  353. if rs("issued") and not auth(6,"N") then
  354. if rs("isA") then
  355. IsA=1
  356. InvoiceNo=request.form("InvoiceNo")
  357. if InvoiceNo <> "" then InvoiceNo = cdbl(InvoiceNo)
  358. end if
  359. end if
  360. end if
  361. ' response.write "<br>is A now:" & IsA
  362. ' response.end
  363. for i=1 to request.form("selectedOrders").count
  364. theOrder = clng(request.form("selectedOrders")(i))
  365. mySQL="SELECT ID FROM Orders WHERE ID=" & theOrder
  366. Set rs=conn.Execute(mySQL)
  367. if rs.eof then
  368. errorFound=True
  369. exit for
  370. end if
  371. rs.close
  372. next
  373. for i=1 to request.form("selectedQuotes").count
  374. theQuote= clng(request.form("selectedQuotes")(i))
  375. mySQL="SELECT ID FROM Quotes WHERE ID=" & theQuote
  376. Set rs=conn.Execute(mySQL)
  377. if rs.eof then
  378. errorFound=True
  379. exit for
  380. end if
  381. rs.close
  382. next
  383. Set rs= Nothing
  384. mySQL="SELECT * FROM Invoices WHERE (ID='"& InvoiceID & "')"
  385. Set rs= conn.Execute(mySQL)
  386. if NOT rs.eof then
  387. voided= rs("Voided")
  388. issued= rs("Issued")
  389. approved= rs("Approved")
  390. isReverse= rs("IsReverse")
  391. ApprovedBy= rs("ApprovedBy")
  392. if issued and not auth(6 , "N") then isA=rs("isA")
  393. else
  394. errorFound=True
  395. end if
  396. if Err.Number<>0 then
  397. Err.clear
  398. errorFound=True
  399. end if
  400. if NOT errorFound then
  401. TotalPrice = 0
  402. TotalDiscount = 0
  403. TotalReverse = 0
  404. TotalReceivable = 0
  405. TotalVat = 0
  406. RFD = 0
  407. for i=1 to request.form("Items").count
  408. theItem = clng(text2value(request.form("Items")(i)))
  409. mySQL="SELECT ID FROM InvoiceItems WHERE ID=" & theItem
  410. Set rs=conn.Execute(mySQL)
  411. if rs.eof then
  412. errorFound=True
  413. exit for
  414. end if
  415. rs.close
  416. theDescription = left(sqlSafe(request.form("Descriptions")(i)),100)
  417. theAppQtty = cdbl(text2value(request.form("AppQttys")(i)))
  418. thePrice = clng(text2value(request.form("Prices")(i)))
  419. theDiscount = text2value(request.form("Discounts")(i))
  420. theReverse = text2value(request.form("Reverses")(i))
  421. theLength = text2value(request.form("Lengths")(i))
  422. theWidth = text2value(request.form("Widths")(i))
  423. theQtty = text2value(request.form("Qttys")(i))
  424. theSets = text2value(request.form("Sets")(i))
  425. theVat = text2value(request.form("Vat")(i))
  426. 'theHasVat = text2value(request.form("hasVat")(i))
  427. if theDiscount <>"" then theDiscount= clng(theDiscount)
  428. if theReverse <> "" then theReverse = clng(theReverse)
  429. if theLength <> "" then theLength = cdbl(theLength)
  430. if theWidth <> "" then theWidth = cdbl(theWidth)
  431. if theQtty <> "" then theQtty = clng(theQtty)
  432. if theSets <> "" then theSets = clng(theSets)
  433. TotalPrice = TotalPrice + thePrice
  434. TotalDiscount = TotalDiscount + theDiscount
  435. TotalReverse = TotalReverse + theReverse
  436. TotalReceivable = TotalReceivable + (thePrice - theDiscount - theReverse + theVat)
  437. TotalVat = TotalVat + theVat
  438. next
  439. RFD = TotalReceivable - fix(TotalReceivable / 1000) * 1000
  440. 'RFD = RFD / 1.03
  441. TotalReceivable = TotalReceivable - RFD
  442. TotalDiscount = TotalDiscount + RFD
  443. end if
  444. if Err.Number<>0 then
  445. Err.clear
  446. errorFound=True
  447. end if
  448. ON ERROR GOTO 0
  449. if errorFound then
  450. response.write "<br>"
  451. call showAlert ("خطا در ورودي",CONST_MSG_ERROR)
  452. response.end
  453. end if
  454. '^^^^---------------- Checking Input ------------^^^^
  455. if isReverse then
  456. 'Check for permission for EDITTING Rev. Invoice
  457. if not Auth(6 , 5) then NotAllowdToViewThisPage()
  458. itemType=4
  459. else
  460. 'Check for permission for EDITTING Invoice
  461. if not Auth(6 , 3) then NotAllowdToViewThisPage()
  462. itemType=1
  463. end if
  464. if voided then
  465. Conn.close
  466. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور قبلا باطل شده است.")
  467. elseif issued then
  468. if Auth(6 , "A") then
  469. ' Has the Priviledge to change the Invoice / Reverse Invoice
  470. 'mySQL="SELECT ID FROM ARItems WHERE (Type='"& itemType & "') AND (GL_Update=1) AND (Link='"& InvoiceID & "')"
  471. 'Changed by Kid ! 831124
  472. mySQL="SELECT ARItems.ID, ARItems.GL_Update, EffGLRows.GL, EffGLRows.GLDocID FROM ARItems LEFT OUTER JOIN (SELECT Link, GL, GLDocID FROM EffectiveGLRows WHERE SYS = 'AR') EffGLRows ON ARItems.ID = EffGLRows.Link WHERE (ARItems.Type = '"& itemType & "') AND (ARItems.Link = '"& InvoiceID & "')"
  473. Set RS2 = conn.Execute(mySQL)
  474. if RS2.eof then
  475. Conn.close
  476. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("خطا !! <br><br> پيدا نشد.")
  477. else
  478. if RS2("GL_Update") = False then
  479. tmpGL=RS2("GL")
  480. tmpGLDoc=RS2("GLDocID")
  481. Conn.close
  482. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("قبلا براي اين فاكتور سند حسابداري صادر شده است.<br><br>دفتر كل:"& tmpGL & " سند شماره: "& tmpGLDoc & " .")
  483. else
  484. ARItemID=RS2("ID")
  485. IssuedButEdit=true
  486. end if
  487. end if
  488. RS2.close
  489. else
  490. Conn.close
  491. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور قبلا صادر شده است.")
  492. end if
  493. elseif approved then
  494. call UnApproveInvoice ( InvoiceID , ApprovedBy )
  495. end if
  496. '******************* Editing *******************
  497. ' ****
  498. if IssuedButEdit then
  499. ' Only Updating IssuedDate, Number & no IsA
  500. ' and related Orders
  501. '---- Checking wether issueDate is valid in current open GL
  502. If Not CheckDateFormat(issueDate) Then
  503. Conn.close
  504. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("تاريخ وارد شده معتبر نيست.")
  505. end if
  506. if (issueDate < session("OpenGLStartDate")) OR (issueDate > session("OpenGLEndDate")) then
  507. Conn.close
  508. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("خطا!<br>تاريخ وارد شده معتبر نيست. <br>(در سال مالي جاري نيست)")
  509. end if
  510. '----
  511. '----- Check GL is closed
  512. if (session("IsClosed")="True") then
  513. Conn.close
  514. response.redirect "?errMsg=" & Server.URLEncode("خطا! سال مالي جاري بسته شده و شما قادر به تغيير در آن نيستيد.")
  515. end if
  516. '----
  517. mySQL="UPDATE Invoices SET IssuedDate=N'" & issueDate & "', Number='"& InvoiceNo & "', IsA='"&IsA&"',issuedDate_en=dbo.udf_date_solarToDate(cast(substring('" & issueDate & "',1,4) as int),cast(substring('" & issueDate & "',6,2) as int),cast(substring('" & issueDate & "',9,2) as int)) WHERE (ID='"& InvoiceID & "')"
  518. 'response.write mySQL
  519. 'response.end
  520. conn.Execute(mySQL)
  521. '---------------------------------------------------------------------------------------------------
  522. for i=1 to request.form("Items").count
  523. if request.form("lineID") <> "" then
  524. 'response.write text2value(request.form("lineID")(i))
  525. theID = clng(text2value(request.form("lineID")(i)))
  526. theItem = clng(text2value(request.form("Items")(i)))
  527. theDescription = left(sqlSafe(request.form("Descriptions")(i)),100)
  528. 'theAppQtty = cdbl(text2value(request.form("AppQttys")(i)))
  529. theLength = text2value(request.form("Lengths")(i))
  530. theWidth = text2value(request.form("Widths")(i))
  531. theQtty = text2value(request.form("Qttys")(i))
  532. theSets = text2value(request.form("Sets")(i))
  533. if theLength <> "" then theLength = cdbl(theLength)
  534. if theWidth <> "" then theWidth = cdbl(theWidth)
  535. if theQtty <> "" then theQtty = clng(theQtty)
  536. if theSets <> "" then theSets = clng(theSets)
  537. mySQL="UPDATE InvoiceLines SET Item='" & theItem & "', Description=N'" & theDescription & "', Length='" & theLength & "', Width='" & theWidth & "', Qtty='" & theQtty & "', Sets='" & theSets & "' WHERE ID = " & theID
  538. conn.Execute(mySQL)
  539. 'response.write "<br>" & mySQL
  540. 'response.end
  541. end if
  542. next
  543. '---------------------------------------------------------------------------------------------------
  544. if IsA then
  545. GLAccount= "91001" 'This must be changed... (Sales A)
  546. else
  547. GLAccount= "91002" 'This must be changed... (Sales B)
  548. end if
  549. ' Changed By Kid 860118 , seasing to use Sales B
  550. 'GLAccount= "91001" 'This must be changed... (Sales A)
  551. conn.Execute("UPDATE ARItems SET GL='"& OpenGL & "', EffectiveDate='" & issueDate & "', GLAccount='"& GLAccount & "' WHERE (ID='" & ARItemID & "')")
  552. '**************** Updating Invoice-Order Relations ****************
  553. 'mySQL="UPDATE Orders SET Closed=0 WHERE ID IN (SELECT [Order] FROM InvoiceOrderRelations WHERE (Invoice= '" & InvoiceID & "'))"
  554. 'Changed By Kid ! 840509
  555. 'set orders which are ONLY related to this invoice, "Open"
  556. 'that means, orders which are related to this invoice and are NOT related to any OTHER issued invoices.
  557. mySQL ="UPDATE Orders SET Closed=0 WHERE ID IN (SELECT [Order] FROM InvoiceOrderRelations WHERE (Invoice = '" & InvoiceID & "') AND ([Order] NOT IN (SELECT InvoiceOrderRelations.[ORDER] FROM Invoices INNER JOIN InvoiceOrderRelations ON Invoices.ID = InvoiceOrderRelations.Invoice WHERE (Invoices.Issued = 1) AND (Invoices.Voided = 0) AND (Invoices.isReverse = 0) AND (Invoices.ID <> '" & InvoiceID & "'))))"
  558. conn.Execute(mySQL)
  559. ' mySQL ="DELETE FROM InvoiceOrderRelations WHERE (Invoice='" & InvoiceID & "')"
  560. ' 'mySQL ="DELETE FROM InvoiceOrderRelations WHERE (Invoice = '" & InvoiceID & "') AND ([Order] NOT IN (SELECT InvoiceOrderRelations.[ORDER] FROM Invoices INNER JOIN InvoiceOrderRelations ON Invoices.ID = InvoiceOrderRelations.Invoice WHERE (Invoices.Issued = 1) AND (Invoices.Voided = 0) AND (Invoices.isReverse = 0) AND (Invoices.ID <> '" & InvoiceID & "')))"
  561. ' conn.Execute(mySQL)
  562. '
  563. ' for i=1 to request.form("selectedOrders").count
  564. ' theOrder= clng(request.form("selectedOrders")(i))
  565. ' mySQL="INSERT INTO InvoiceOrderRelations (Invoice,[Order]) VALUES ('" & InvoiceID & "', '" & theOrder & "')"
  566. ' conn.Execute(mySQL)
  567. ' next
  568. conn.Execute("UPDATE Orders SET Closed=1 WHERE ID IN (SELECT [Order] FROM InvoiceOrderRelations WHERE (Invoice='" & InvoiceID & "'))")
  569. '^^^^------------ Updating Invoice-Order Relations ------------^^^^
  570. '**************** Updating Invoice-Quote Relations ****************
  571. mySQL ="UPDATE Quotes SET Closed=0 WHERE ID IN (SELECT [Quote] FROM InvoiceQuoteRelations WHERE (Invoice = '" & InvoiceID & "') AND ([Quote] NOT IN (SELECT InvoiceQuoteRelations.[Quote] FROM Invoices INNER JOIN InvoiceQuoteRelations ON Invoices.ID = InvoiceQuoteRelations.Invoice WHERE (Invoices.Issued = 1) AND (Invoices.Voided = 0) AND (Invoices.isReverse = 0) AND (Invoices.ID <> '" & InvoiceID & "'))))"
  572. conn.Execute(mySQL)
  573. mySQL ="DELETE FROM InvoiceQuoteRelations WHERE (Invoice='" & InvoiceID & "')"
  574. conn.Execute(mySQL)
  575. for i=1 to request.form("selectedQuotes").count
  576. theQuote= clng(request.form("selectedQuotes")(i))
  577. mySQL="INSERT INTO InvoiceQuoteRelations (Invoice,[Quote]) VALUES ('" & InvoiceID & "', '" & theQuote & "')"
  578. conn.Execute(mySQL)
  579. next
  580. conn.Execute("UPDATE Quotes SET Closed=1 WHERE ID IN (SELECT [Quote] FROM InvoiceQuoteRelations WHERE (Invoice='" & InvoiceID & "'))")
  581. '^^^^------------ Updating Invoice-Quote Relations ------------^^^^
  582. mySQL = "SELECT * FROM ARItems WHERE Type=1 AND Link=" & InvoiceID
  583. set RSSS=conn.Execute(mySQL)
  584. if RSSS.eof or not Auth(6 , "N") then
  585. response.redirect "AccountReport.asp?act=showInvoice&invoice=" & InvoiceID & "&msg=" &Server.URLEncode("اين فاكنور صادر شده و شما مجاز به تغييرات كلي در آن نيستيد.")
  586. else
  587. if RSSS("GL_Update") then
  588. ' In this secssion we update all issued invoice has not GLs
  589. '---------------------------------------------------------------------------------------------------
  590. '---------------------------------------------------------------------------------------------------
  591. '---------------------------------------------------------------------------------------------------
  592. '---------------------------------------------------------------------------------------------------
  593. '---------------------------------------------------------------------------------------------------
  594. set RSinvoice=conn.Execute("SELECT * FROM Invoices WHERE ID="&InvoiceID)
  595. 'response.write TotalReceivable & "<br>"
  596. 'response.write totalVat & "<br>"
  597. 'response.write TotalPrice & "<br>"
  598. 'response.write TotalDiscount & "<br>"
  599. 'response.end
  600. if (not RSinvoice.eof) and (clng(RSinvoice("TotalReceivable"))<>TotalReceivable or Cdbl(RSinvoice("TotalVat"))<>TotalVat) then
  601. oldTotalReceivable = CLng(RSinvoice("TotalReceivable"))
  602. mySQL="UPDATE Invoices SET Customer='"& CustomerID & "', Number='"& InvoiceNo & "', TotalPrice='"& TotalPrice & "', TotalDiscount='"& TotalDiscount & "', TotalReverse='"& TotalReverse & "', TotalReceivable='"& TotalReceivable & "' , IsA="& IsA & ", TotalVat='" & totalVat & "' WHERE (ID='"& InvoiceID & "')"
  603. conn.Execute(mySQL)
  604. mySQL="DELETE FROM InvoiceLines WHERE (Invoice='"& InvoiceID & "')"
  605. conn.Execute(mySQL)
  606. '**************************** Inserting Invoice Lines ****************
  607. for i=1 to request.form("Items").count
  608. theItem = clng(text2value(request.form("Items")(i)))
  609. theDescription = left(sqlSafe(request.form("Descriptions")(i)),100)
  610. theAppQtty = cdbl(text2value(request.form("AppQttys")(i)))
  611. thePrice = clng(text2value(request.form("Prices")(i)))
  612. theDiscount = text2value(request.form("Discounts")(i))
  613. theReverse = text2value(request.form("Reverses")(i))
  614. theLength = text2value(request.form("Lengths")(i))
  615. theWidth = text2value(request.form("Widths")(i))
  616. theQtty = text2value(request.form("Qttys")(i))
  617. theSets = text2value(request.form("Sets")(i))
  618. theVat = clng(text2value(request.form("Vat")(i)))
  619. theHasVat = text2value(request.form("hasVat")(i))
  620. if theDiscount <>"" then theDiscount= clng(theDiscount)
  621. if theReverse <> "" then theReverse = clng(theReverse)
  622. if theLength <> "" then theLength = cdbl(theLength)
  623. if theWidth <> "" then theWidth = cdbl(theWidth)
  624. if theQtty <> "" then theQtty = clng(theQtty)
  625. if theSets <> "" then theSets = clng(theSets)
  626. mySQL="INSERT INTO InvoiceLines (Invoice, Item, Description, Length, Width, Qtty, Sets, AppQtty, Price, Discount, Reverse, Vat, hasVat) VALUES ('"& InvoiceID & "', '" & theItem & "', N'" & theDescription & "', '" & theLength & "', '" & theWidth & "', '" & theQtty & "', '" & theSets & "', '" & theAppQtty & "', '" & thePrice & "', '" & theDiscount & "', '" & theReverse & "', '" & theVat & "', "& theHasVat &")"
  627. conn.Execute(mySQL)
  628. next
  629. if RFD > 0 then
  630. theItem = 39999
  631. theDescription = "تخفيف رند فاكتور"
  632. theAppQtty = 0
  633. thePrice = 0
  634. theDiscount = RFD
  635. theReverse = 0
  636. theLength = 0
  637. theWidth = 0
  638. theQtty = 0
  639. theSets = 0
  640. theVat = 0
  641. mySQL="INSERT INTO InvoiceLines (Invoice, Item, Description, Length, Width, Qtty, Sets, AppQtty, Price, Discount, Reverse, Vat) VALUES ('"& InvoiceID & "', '" & theItem & "', N'" & theDescription & "', '" & theLength & "', '" & theWidth & "', '" & theQtty & "', '" & theSets & "', '" & theAppQtty & "', '" & thePrice & "', '" & theDiscount & "', '" & theReverse & "', '" & theVat & "')"
  642. conn.Execute(mySQL)
  643. end if
  644. mySQL="UPDATE ARItems SET AmountOriginal='"& TotalReceivable &"', RemainedAmount='"& TotalReceivable &"' ,Vat='"& totalVat &"', FullyApplied = 0 WHERE ID=" & RSSS("ID")
  645. Conn.Execute(mySQL)
  646. Conn.Execute("Delete from ARItemsRelations where DebitARItem = " & RSSS("ID"))
  647. Conn.Execute(mySQL)
  648. mySQL="UPDATE Accounts SET ARBalance=ARBalance - "&TotalReceivable - oldTotalReceivable&" WHERE id=" & CustomerID
  649. conn.Execute(mySQL)
  650. end if
  651. 'end if
  652. '****
  653. '^^^^--------------- Editing ---------------^^^^
  654. '--------------------------------------------------------------------------------------------------
  655. '---------------------------------------------------------------------------------------------------
  656. '---------------------------------------------------------------------------------------------------
  657. response.redirect "AccountReport.asp?act=showInvoice&invoice=" & InvoiceID & "&msg=" &Server.URLEncode("فاكتور شما دچار تغييرات كلي شد")
  658. '---------------------------------------------------------------------------------------------------
  659. '---------------------------------------------------------------------------------------------------
  660. '---------------------------------------------------------------------------------------------------
  661. '---------------------------------------------------------------------------------------------------
  662. else
  663. response.redirect "AccountReport.asp?act=showInvoice&invoice=" & InvoiceID & "&msg=" &Server.URLEncode("براي اين فاكتور قبلا سند حسابداري صادر شده، پس آنرا نمي‌توان ويرايش كرد.")
  664. end if
  665. end if
  666. conn.close
  667. response.redirect "AccountReport.asp?act=showInvoice&invoice=" & InvoiceID & "&msg=" &Server.URLEncode("اين فاكتور قبلا صادر شده بود.<br>هرچند كه شما اجازه داريد كه اين فاكتور را تغيير بدهيد<br>بهتر است كه اين كار را تكرار نكنيد.")
  668. else
  669. ' S A M
  670. 'response.write(totalDiscount)
  671. 'response.end
  672. mySQL="UPDATE Invoices SET Customer='"& CustomerID & "', Number='"& InvoiceNo & "', TotalPrice='"& TotalPrice & "', TotalDiscount='"& TotalDiscount & "', TotalReverse='"& TotalReverse & "', TotalReceivable='"& TotalReceivable & "' , IsA='"& IsA & "', TotalVat='" & totalVat & "' WHERE (ID='"& InvoiceID & "')"
  673. conn.Execute(mySQL)
  674. mySQL="DELETE FROM InvoiceLines WHERE (Invoice='"& InvoiceID & "')"
  675. conn.Execute(mySQL)
  676. '**************************** Inserting Invoice Lines ****************
  677. for i=1 to request.form("Items").count
  678. theItem = clng(text2value(request.form("Items")(i)))
  679. theDescription = left(sqlSafe(request.form("Descriptions")(i)),100)
  680. theAppQtty = cdbl(text2value(request.form("AppQttys")(i)))
  681. thePrice = clng(text2value(request.form("Prices")(i)))
  682. theDiscount = text2value(request.form("Discounts")(i))
  683. theReverse = text2value(request.form("Reverses")(i))
  684. theLength = text2value(request.form("Lengths")(i))
  685. theWidth = text2value(request.form("Widths")(i))
  686. theQtty = text2value(request.form("Qttys")(i))
  687. theSets = text2value(request.form("Sets")(i))
  688. theVat = clng(text2value(request.form("Vat")(i)))
  689. theHasVat = text2value(request.form("hasVat")(i))
  690. if theDiscount <>"" then theDiscount= clng(theDiscount)
  691. if theReverse <> "" then theReverse = clng(theReverse)
  692. if theLength <> "" then theLength = cdbl(theLength)
  693. if theWidth <> "" then theWidth = cdbl(theWidth)
  694. if theQtty <> "" then theQtty = clng(theQtty)
  695. if theSets <> "" then theSets = clng(theSets)
  696. mySQL="INSERT INTO InvoiceLines (Invoice, Item, Description, Length, Width, Qtty, Sets, AppQtty, Price, Discount, Reverse, Vat, hasVat) VALUES ('"& InvoiceID & "', '" & theItem & "', N'" & theDescription & "', '" & theLength & "', '" & theWidth & "', '" & theQtty & "', '" & theSets & "', '" & theAppQtty & "', '" & thePrice & "', '" & theDiscount & "', '" & theReverse & "', '" & theVat & "', "& theHasVat &")"
  697. conn.Execute(mySQL)
  698. next
  699. if RFD > 0 then
  700. theItem = 39999
  701. theDescription = "تخفيف رند فاكتور"
  702. theAppQtty = 0
  703. thePrice = 0
  704. theDiscount = RFD
  705. theReverse = 0
  706. theLength = 0
  707. theWidth = 0
  708. theQtty = 0
  709. theSets = 0
  710. theVat = 0
  711. mySQL="INSERT INTO InvoiceLines (Invoice, Item, Description, Length, Width, Qtty, Sets, AppQtty, Price, Discount, Reverse, Vat) VALUES ('"& InvoiceID & "', '" & theItem & "', N'" & theDescription & "', '" & theLength & "', '" & theWidth & "', '" & theQtty & "', '" & theSets & "', '" & theAppQtty & "', '" & thePrice & "', '" & theDiscount & "', '" & theReverse & "', '" & theVat & "')"
  712. conn.Execute(mySQL)
  713. end if
  714. '**************** Updating Invoice-Order Relations ****************
  715. ' mySQL="DELETE FROM InvoiceOrderRelations WHERE (Invoice='" & InvoiceID & "')"
  716. ' conn.Execute(mySQL)
  717. ' response.write "aaaa:"
  718. ' response.write request.form("selectedOrders").count
  719. ' for i=1 to request.form("selectedOrders").count
  720. ' theOrder= clng(request.form("selectedOrders")(i))
  721. ' mySQL="INSERT INTO InvoiceOrderRelations (Invoice,[Order]) VALUES ('" & InvoiceID & "', '" & theOrder & "')"
  722. ' '--------------------------SAM------------------------------
  723. ' ' response.write mySQL
  724. ' '-----------------------------------------------------------
  725. ' conn.Execute(mySQL)
  726. ' next
  727. '^^^^------------ Updating Invoice-Order Relations ------------^^^^
  728. ' '**************** Updating Invoice-Quote Relations ****************
  729. ' mySQL="DELETE FROM InvoiceQuoteRelations WHERE (Invoice='" & InvoiceID & "')"
  730. ' conn.Execute(mySQL)
  731. ' for i=1 to request.form("selectedQuotes").count
  732. ' theQuote= clng(request.form("selectedQuotes")(i))
  733. ' mySQL="INSERT INTO InvoiceQuoteRelations (Invoice,[Quote]) VALUES ('" & InvoiceID & "', '" & theQuote & "')"
  734. ' conn.Execute(mySQL)
  735. ' next
  736. '^^^^------------ Updating Invoice-Quote Relations ------------^^^^
  737. end if
  738. '****
  739. '^^^^--------------- Editing ---------------^^^^
  740. response.redirect "AccountReport.asp?act=showInvoice&invoice=" & InvoiceID
  741. '--------------------------------------------------------------------------------------------------------------------
  742. elseif request("act")="approveInvoice" then
  743. if not Auth(6 , "C") then
  744. 'Doesn't have the Priviledge to APPROVE the Invoice
  745. response.write "<br>"
  746. call showAlert ("شما مجاز به تاييد فاكتور نيستيد",CONST_MSG_ERROR)
  747. response.end
  748. end if
  749. if request("invoice")<>"" then
  750. InvoiceID=request("invoice")
  751. if not(isnumeric(request("invoice"))) then
  752. ShowErrorMessage("خطا")
  753. response.end
  754. end if
  755. mySQL="SELECT * FROM Invoices WHERE (ID='"& InvoiceID & "')"
  756. Set RS1 = conn.Execute(mySQL)
  757. if RS1.eof then
  758. ShowErrorMessage("پيدا نشد ")
  759. response.end
  760. else
  761. '-------------------------------------------------------------------------
  762. '---- CHECK pickup list
  763. '-------------------------------------------------------------------------
  764. mySQL="select distinct sales.item, sales.description, sales.appQtty,pik.itemID,pik.itemName,pik.qtty from (select invoiceLines.item,max(invoiceLines.description) as description,sum(invoiceLines.appQtty) as appQtty, InventoryInvoiceRelations.inventoryItem from InvoiceLines inner join Invoices on invoices.ID=invoiceLines.Invoice inner join InventoryInvoiceRelations on InventoryInvoiceRelations.invoiceItem=invoiceLines.Item WHERE Invoices.id=" & InvoiceID & " group by invoiceLines.item, InventoryInvoiceRelations.inventoryItem) as sales full outer join (SELECT InventoryPickuplistItems.itemID, max(InventoryPickuplistItems.ItemName) AS ItemName, sum(InventoryPickuplistItems.Qtty) AS Qtty, InventoryInvoiceRelations.invoiceItem FROM InventoryPickuplistItems INNER JOIN InventoryPickuplists ON InventoryPickuplistItems.pickupListID = InventoryPickuplists.id INNER JOIN InvoiceOrderRelations ON InventoryPickuplistItems.Order_ID = InvoiceOrderRelations.[Order] inner join invoices on InvoiceOrderRelations.invoice=invoices.id inner join InventoryInvoiceRelations on InventoryInvoiceRelations.inventoryItem=InventoryPickuplistItems.itemID WHERE NOT InventoryPickuplists.Status = N'del' and InventoryPickuplistItems.CustomerHaveInvItem=0 and Invoices.id=" & InvoiceID & " group by InventoryPickuplistItems.itemID, InventoryInvoiceRelations.invoiceItem ) as pik on pik.itemID=sales.inventoryItem or pik.invoiceItem=sales.item"
  765. set rs=Conn.Execute (mySQL)
  766. errmsg=""
  767. msg=""
  768. while not rs.eof
  769. skeepNext=0
  770. if IsNull(rs("appQtty")) then
  771. errmsg = errmsg & "براي حواله‌ي " & rs("itemName") & " شما هيچ آيتمي در فاكتور ثبت نكرديد<br>"
  772. else
  773. if IsNull(rs("qtty")) then
  774. errmsg = errmsg & "براي " & rs("description") & " شما هيچ حواله‌اي صادر نكرده‌ايد<br>"
  775. else
  776. if CDbl(rs("qtty")) <> CDbl(rs("appQtty")) then
  777. item=rs("item")
  778. appQtty=CDbl(rs("appQtty"))
  779. desc=rs("description")
  780. itemName=""
  781. sumQtty=0
  782. do
  783. skeepNext=0
  784. if item<>rs("item") then exit do
  785. sumQtty=sumQtty+cdbl(rs("qtty"))
  786. if itemName="" then
  787. itemName = rs("itemName")
  788. else
  789. itemName=itemName & ", " & rs("itemName")
  790. end if
  791. rs.moveNext
  792. skeepNext=1
  793. if rs.eof then exit do
  794. loop while item=rs("item")
  795. 'rs.MovePrevious
  796. if appQtty<>sumQtty then errmsg = errmsg & "تعداد حواله <b>" & itemName & "</b> با تعداد <b>" & desc & "</b> مغاير است.<br>"
  797. end if
  798. end if
  799. end if
  800. if skeepNext=0 then rs.moveNext
  801. wend
  802. set rs=nothing
  803. '-------------------------------------------------------------------------
  804. '---- CHECK out Service
  805. '-------------------------------------------------------------------------
  806. mySQL="select * from (SELECT PurchaseOrders.TypeID,max(PurchaseRequests.TypeName) as TypeName, sum(PurchaseOrders.Qtty) as Qtty FROM PurchaseOrders FULL OUTER JOIN PurchaseRequestOrderRelations RIGHT OUTER JOIN PurchaseRequests INNER JOIN InvoiceOrderRelations ON PurchaseRequests.Order_ID = InvoiceOrderRelations.[Order] ON PurchaseRequestOrderRelations.Req_ID = PurchaseRequests.ID ON PurchaseOrders.ID = PurchaseRequestOrderRelations.Ord_ID WHERE (InvoiceOrderRelations.Invoice =" & InvoiceID & ") and PurchaseRequests.Status<> 'del' and PurchaseOrders.Status<>'CANCEL' and PurchaseOrders.IsService=1 group by PurchaseOrders.TypeID) as outService full outer join (select InvoiceItems.RelatedInventoryItemID,max(description) as description,sum(appQtty) as appQtty from InvoiceLines inner join InvoiceItems on InvoiceItems.ID=invoiceLines.Item and InvoiceItems.Type=5 where Invoice=" & InvoiceID & " group by InvoiceItems.RelatedInventoryItemID) as sales on sales.RelatedInventoryItemID=outService.TypeID"
  807. set rs=Conn.Execute (mySQL)
  808. while not rs.eof
  809. if IsNull(rs("appQtty")) then
  810. errmsg = errmsg & "براي خدمات " & rs("typeName") & " شما هيچ آيتمي در فاكتور ثبت نكرديد<br>"
  811. else
  812. if IsNull(rs("qtty")) then
  813. errmsg = errmsg & "براي " & rs("description") & " شما هيچ درخواست خدماتي ثبت نكرده‌ايد<br>"
  814. else
  815. if CDbl(rs("qtty")) <> CDbl(rs("appQtty")) then msg = msg & "تعداد <b>" & rs("typeName") & "</b> با تعداد <b>" & rs("description") & "</b> مغاير است.<br>"
  816. end if
  817. end if
  818. rs.moveNext
  819. wend
  820. set rs=nothing
  821. if (errmsg<>"") then
  822. Conn.close
  823. response.redirect "?errMsg=" & Server.URLEncode(errmsg)
  824. end if
  825. '-------------------------------------------------------------------------
  826. '---------------------------------------------
  827. '-------------------------------------------------------------------------
  828. if RS1("Voided") = True then
  829. Conn.close
  830. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور قبلا باطل شده است.")
  831. elseif RS1("Issued") = True then
  832. Conn.close
  833. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور قبلا صادر شده است.")
  834. elseif RS1("Approved") = True then
  835. Conn.close
  836. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور قبلا تاييد شده است.")
  837. end if
  838. end if
  839. else
  840. ShowErrorMessage("خطا")
  841. response.end
  842. end if
  843. '--------------------SAM, Iqnore multi invoice in one order
  844. 'mySQL="SELECT COUNT(*) AS OrderCount FROM InvoiceOrderRelations WHERE [Order] IN (SELECT [Order] FROM InvoiceOrderRelations WHERE Invoice=" & InvoiceID &")"-----------SAM coorect this on 9 mar 2011
  845. mySQL = "SELECT COUNT(Invoice) AS OrderCount FROM (SELECT DISTINCT InvoiceOrderRelations.Invoice FROM InvoiceOrderRelations inner join Invoices on InvoiceOrderRelations.Invoice = Invoices.ID WHERE InvoiceOrderRelations.[Order] IN (SELECT [Order] FROM InvoiceOrderRelations WHERE Invoice=204133) and Invoices.Voided=0) tbl"
  846. set RS1= conn.Execute(mySQL)
  847. ' response.write rs1("OrderCount")
  848. ' response.end
  849. if RS1("OrderCount")>1 then
  850. conn.close
  851. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("به اين سفارش دو فاكتور مربوط شده! لطفا به هر سفارش فقط يك فاكتور متصل كنيد.")
  852. end if
  853. '------------------------------------------------
  854. mySQL="UPDATE Invoices SET Approved=1, ApprovedDate=N'"& shamsiToday() & "', ApprovedBy='"& session("ID") & "' WHERE (ID='"& InvoiceID & "')"
  855. conn.Execute(mySQL)
  856. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&msg=" & Server.URLEncode(msg)
  857. '-------------------------------------------------------------------------------------------------------
  858. elseif request("act")="IssueInvoice" then
  859. if not Auth(6 , "D") then
  860. 'Doesn't have the Priviledge to ISSUE the Invoice
  861. response.write "<br>"
  862. call showAlert ("شما مجاز به صدور فاكتور نيستيد",CONST_MSG_ERROR)
  863. response.end
  864. end if
  865. ON ERROR RESUME NEXT
  866. InvoiceID= clng(request("Invoice"))
  867. if Err.Number<>0 then
  868. Err.clear
  869. conn.close
  870. response.redirect "top.asp?errMsg=" & Server.URLEncode("خطا!")
  871. end if
  872. ON ERROR GOTO 0
  873. creationDate= shamsiToday()
  874. issueDate= SqlSafe(request("issueDate"))
  875. if issueDate="" then issueDate=creationDate
  876. if issueDate<>creationDate then
  877. if Auth(6 , "I") then
  878. ' can ISSUE the Invoice / Rev. Invoice on another Date
  879. '---- Checking wether issueDate is valid in current open GL
  880. If Not CheckDateFormat(issueDate) Then
  881. Conn.close
  882. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("تاريخ وارد شده معتبر نيست.")
  883. end if
  884. if (issueDate < session("OpenGLStartDate")) OR (issueDate > session("OpenGLEndDate")) then
  885. Conn.close
  886. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("خطا!<br>تاريخ وارد شده معتبر نيست. <br>(در سال مالي جاري نيست)")
  887. end if
  888. '----
  889. else
  890. 'Doesn't have the Priviledge to ISSUE the Invoice on another Date
  891. response.write "<br>"
  892. call showAlert ("شما مجاز به صدور فاكتور در اين تاريخ نيستيد",CONST_MSG_ERROR)
  893. response.end
  894. end if
  895. end if
  896. '----- Check GL is closed
  897. if (session("IsClosed")="True") then
  898. Conn.close
  899. response.redirect "?errMsg=" & Server.URLEncode("خطا! سال مالي جاري بسته شده و شما قادر به تغيير در آن نيستيد.")
  900. end if
  901. '----
  902. '---- Checking wether issueDate is valid in current open GL
  903. If Not CheckDateFormat(issueDate) Then
  904. Conn.close
  905. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("خطا!<br>تاريخ صدور معتبر نيست.")
  906. end if
  907. if (issueDate < session("OpenGLStartDate")) OR (issueDate > session("OpenGLEndDate")) then
  908. Conn.close
  909. response.redirect "AccountReport.asp?act=showInvoice&invoice=" & InvoiceID & "&errmsg=" & Server.URLEncode("خطا!<br>تاريخ صدور معتبر نيست. <br>(در سال مالي جاري نيست)")
  910. end if
  911. '----
  912. '---- CHECK pickup list
  913. mySQL="select distinct sales.item, sales.description, sales.appQtty,pik.itemID,pik.itemName,pik.qtty from (select invoiceLines.item,max(invoiceLines.description) as description,sum(invoiceLines.appQtty) as appQtty, InventoryInvoiceRelations.inventoryItem from InvoiceLines inner join Invoices on invoices.ID=invoiceLines.Invoice inner join InventoryInvoiceRelations on InventoryInvoiceRelations.invoiceItem=invoiceLines.Item WHERE Invoices.id=" & InvoiceID & " group by invoiceLines.item, InventoryInvoiceRelations.inventoryItem) as sales full outer join (SELECT InventoryPickuplistItems.itemID, max(InventoryPickuplistItems.ItemName) AS ItemName, sum(InventoryPickuplistItems.Qtty) AS Qtty, InventoryInvoiceRelations.invoiceItem FROM InventoryPickuplistItems INNER JOIN InventoryPickuplists ON InventoryPickuplistItems.pickupListID = InventoryPickuplists.id INNER JOIN InvoiceOrderRelations ON InventoryPickuplistItems.Order_ID = InvoiceOrderRelations.[Order] inner join invoices on InvoiceOrderRelations.invoice=invoices.id inner join InventoryInvoiceRelations on InventoryInvoiceRelations.inventoryItem=InventoryPickuplistItems.itemID WHERE NOT InventoryPickuplists.Status = N'del' and InventoryPickuplistItems.CustomerHaveInvItem=0 and Invoices.id=" & InvoiceID & " group by InventoryPickuplistItems.itemID, InventoryInvoiceRelations.invoiceItem ) as pik on pik.itemID=sales.inventoryItem or pik.invoiceItem=sales.item"
  914. set rs=Conn.Execute (mySQL)
  915. errMsg=""
  916. while not rs.eof
  917. if IsNull(rs("appQtty")) then
  918. errMsg = errMsg & "براي حواله‌ي " & rs("itemName") & " شما هيچ آيتمي در فاكتور ثبت نكرديد<br>"
  919. else
  920. if IsNull(rs("qtty")) then
  921. errMsg = errMsg & "براي " & rs("description") & " شما هيچ حواله‌اي صادر نكرده‌ايد<br>"
  922. else
  923. if CDbl(rs("qtty")) <> CDbl(rs("appQtty")) then
  924. 'errMsg = errMsg & "تعداد حواله <b>" & rs("itemName") & "</b> با تعداد <b>" & rs("description") & "</b> مغاير است.<br>"
  925. item=rs("item")
  926. appQtty=CDbl(rs("appQtty"))
  927. desc=rs("description")
  928. itemName=""
  929. sumQtty=0
  930. do
  931. skeepNext=0
  932. if item<>rs("item") then exit do
  933. sumQtty=sumQtty+cdbl(rs("qtty"))
  934. if itemName="" then
  935. itemName = rs("itemName")
  936. else
  937. itemName=itemName & ", " & rs("itemName")
  938. end if
  939. rs.moveNext
  940. skeepNext=1
  941. if rs.eof then exit do
  942. loop while item=rs("item")
  943. 'rs.MovePrevious
  944. if appQtty<>sumQtty then errmsg = errmsg & "تعداد حواله <b>" & itemName & "</b> با تعداد <b>" & desc & "</b> مغاير است.<br>"
  945. end if
  946. end if
  947. end if
  948. rs.moveNext
  949. wend
  950. set rs=nothing
  951. '----
  952. '---- CHECK out Service
  953. mySQL="select * from (SELECT PurchaseOrders.TypeID,max(PurchaseRequests.TypeName) as TypeName, sum(PurchaseOrders.Qtty) as Qtty FROM PurchaseOrders FULL OUTER JOIN PurchaseRequestOrderRelations RIGHT OUTER JOIN PurchaseRequests INNER JOIN InvoiceOrderRelations ON PurchaseRequests.Order_ID = InvoiceOrderRelations.[Order] ON PurchaseRequestOrderRelations.Req_ID = PurchaseRequests.ID ON PurchaseOrders.ID = PurchaseRequestOrderRelations.Ord_ID WHERE (InvoiceOrderRelations.Invoice =" & InvoiceID & ") and PurchaseRequests.Status<> 'del' and PurchaseOrders.Status<>'CANCEL' and PurchaseOrders.IsService=1 group by PurchaseOrders.TypeID) as outService full outer join (select InvoiceItems.RelatedInventoryItemID,max(description) as description,sum(appQtty) as appQtty from InvoiceLines inner join InvoiceItems on InvoiceItems.ID=invoiceLines.Item and InvoiceItems.Type=5 where Invoice=" & InvoiceID & " group by InvoiceItems.RelatedInventoryItemID) as sales on sales.RelatedInventoryItemID=outService.TypeID"
  954. set rs=Conn.Execute (mySQL)
  955. while not rs.eof
  956. if IsNull(rs("appQtty")) then
  957. errMsg = errMsg & "براي خدمات " & rs("typeName") & " شما هيچ آيتمي در فاكتور ثبت نكرديد<br>"
  958. else
  959. if IsNull(rs("qtty")) then
  960. errMsg = errMsg & "براي " & rs("description") & " شما هيچ درخواست خدماتي ثبت نكرده‌ايد<br>"
  961. else
  962. if CDbl(rs("qtty")) <> CDbl(rs("appQtty")) then msg = msg & "تعداد <b>" & rs("typeName") & "</b> با تعداد <b>" & rs("description") & "</b> مغاير است.<br>"
  963. end if
  964. end if
  965. rs.moveNext
  966. wend
  967. set rs=nothing
  968. if (errMsg<>"") then
  969. Conn.close
  970. response.redirect "?errMsg=" & Server.URLEncode(errMsg)
  971. end if
  972. '---------------------------------------------
  973. mySQL="SELECT * FROM Invoices WHERE (ID='"& InvoiceID & "')"
  974. Set RS1 = conn.Execute(mySQL)
  975. if RS1.eof then
  976. conn.close
  977. response.redirect "top.asp?errMsg=" & Server.URLEncode("خطا! فاكتور با شماره" & InvoiceID & " پيدا نشد.")
  978. else
  979. voided= RS1("Voided")
  980. issued= RS1("Issued")
  981. approved= RS1("Approved")
  982. isReverse= RS1("IsReverse")
  983. customerID= RS1("Customer")
  984. invoiceFee= RS1("TotalReceivable")
  985. IsA = RS1("IsA")
  986. Vat = RS1("TotalVat")' sam
  987. if voided then
  988. Conn.close
  989. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور قبلا باطل شده است.")
  990. elseif issued then
  991. Conn.close
  992. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور قبلا صادر شده است.")
  993. elseif not approved then
  994. Conn.close
  995. response.redirect "?act=editInvoice&invoice="& InvoiceID & "&errmsg=" & Server.URLEncode("اين فاكتور تاييد نشده است.")
  996. end if
  997. end if
  998. mySQL="UPDATE Invoices SET Issued=1, IssuedDate=N'"& issueDate & "', IssuedBy='"& session("ID") & "' WHERE (ID='"& InvoiceID & "')"
  999. conn.Execute(mySQL)
  1000. if isReverse then
  1001. isCredit=1
  1002. itemType=4
  1003. else
  1004. isCredit=0
  1005. itemType=1
  1006. '----------------------- Declaring the related orders as closed --------------
  1007. conn.Execute("UPDATE Orders SET Closed=1 WHERE ID IN (SELECT [Order] FROM InvoiceOrderRelations WHERE (Invoice='" & InvoiceID & "'))")
  1008. '----------------------- Declaring the related Quotes as closed --------------
  1009. conn.Execute("UPDATE Quotes SET Closed=1 WHERE ID IN (SELECT [Quote] FROM InvoiceQuoteRelations WHERE (Invoice='" & InvoiceID & "'))")
  1010. end if
  1011. '**************************** Creating ARItem for Invoice / Reverse Invoice ****************
  1012. '*** Type = 1 means ARItem is an Invoice
  1013. '*** Type = 4 means ARItem is a Reverse Invoice
  1014. firstGLAccount= "13003" 'This must be changed... (Business Debitors)
  1015. if IsA then
  1016. GLAccount= "91001" 'This must be changed... (Sales A)
  1017. else
  1018. GLAccount= "91002" 'This must be changed... (Sales B)
  1019. end if
  1020. '
  1021. ' Changed By Kid 860118 , seasing to use Sales B
  1022. 'GLAccount= "91001" 'This must be changed... (Sales A)
  1023. mySQL="INSERT INTO ARItems (GLAccount, GL, FirstGLAccount, Account, EffectiveDate, IsCredit, Type, Link, AmountOriginal, CreatedDate, CreatedBy, RemainedAmount, Vat) VALUES ('" &_
  1024. GLAccount & "', '"& OpenGL & "', '"& firstGLAccount & "', '"& CustomerID & "', N'"& issueDate & "', '"& isCredit & "', '"& itemType & "', '"& InvoiceID & "', '"& invoiceFee & "', N'"& creationDate & "', '"& session("ID") & "', '"& invoiceFee & "', '" & Vat & "')"
  1025. conn.Execute(mySQL)
  1026. if isReverse then
  1027. '*** ATTENTION: Increasing AR Balance ....
  1028. mySQL="UPDATE Accounts SET ARBalance = ARBalance + '"& invoiceFee & "' WHERE (ID='"& CustomerID & "')"
  1029. else
  1030. '*** ATTENTION: Decreasing AR Balance ....
  1031. mySQL="UPDATE Accounts SET ARBalance = ARBalance - '"& invoiceFee & "' WHERE (ID='"& CustomerID & "')"
  1032. end if
  1033. conn.Execute(mySQL)
  1034. conn.close
  1035. response.redirect "AccountReport.asp?act=showInvoice&invoice="& InvoiceID & "&msg=" & Server.URLEncode(msg)
  1036. '---------------------------------------------------------------------------------------------------------
  1037. elseif request("act")="voidInvoice" then
  1038. if not Auth(6 , "F") then
  1039. 'Doesn't have the Priviledge to VOID the Invoice
  1040. response.write "<br>"
  1041. call showAlert ("شما مجاز به ابطال فاكتور نيستيد",CONST_MSG_ERROR)
  1042. response.end
  1043. end if
  1044. comment=sqlSafe(request("comment"))
  1045. InvoiceID=request("invoice")
  1046. if InvoiceID="" or not(isnumeric(InvoiceID)) then
  1047. response.write "<br>"
  1048. call showAlert ("خطا در شماره فاكتور",CONST_MSG_ERROR)
  1049. response.end
  1050. end if
  1051. InvoiceID=clng(InvoiceID)
  1052. mySQL="SELECT * FROM Invoices WHERE (ID='"& InvoiceID & "')"
  1053. Set RS1 = conn.Execute(mySQL)
  1054. if RS1.eof then
  1055. ShowErrorMessage("پيدا نشد ")
  1056. response.end
  1057. else
  1058. voided= RS1("Voided")
  1059. issued= RS1("Issued")
  1060. issuedBy= RS1("IssuedBy")
  1061. isReverse= RS1("IsReverse")
  1062. customerID= RS1("Customer")
  1063. invoiceFee= RS1("TotalReceivable")
  1064. IsA = RS1("IsA")
  1065. if voided then
  1066. ShowErrorMessage("اين فاكتور قبلا در تاريخ <span dir='LTR'>"& RS1("VoidedDate") & "</span> باطل شده است.")
  1067. response.end
  1068. end if
  1069. end if
  1070. mySQL="UPDATE Invoices SET Voided=1, VoidedDate=N'"& shamsiToday() & "', VoidedBy='"& session("ID") & "' WHERE (ID='"& InvoiceID & "')"
  1071. conn.Execute(mySQL)
  1072. if isReverse then
  1073. isCredit=1
  1074. itemType=4
  1075. itemTypeName="فاكتور برگشت از فروش"
  1076. else
  1077. isCredit=0
  1078. itemType=1
  1079. itemTypeName="فاكتور"
  1080. '---------- Declaring the related orders as Open -------------------
  1081. 'mySQL="UPDATE Orders SET Closed=0 WHERE ID IN (SELECT [Order] FROM InvoiceOrderRelations WHERE (Invoice= '" & InvoiceID & "'))"
  1082. 'Changed By Kid ! 840509
  1083. 'set orders which are ONLY related to this invoice, "Open"
  1084. 'that means, orders which are related to this invoice and are NOT related to any OTHER issued invoices.
  1085. mySQL ="UPDATE Orders SET Closed=0 WHERE ID IN (SELECT [Order] FROM InvoiceOrderRelations WHERE (Invoice = '" & InvoiceID & "') AND ([Order] NOT IN (SELECT InvoiceOrderRelations.[ORDER] FROM Invoices INNER JOIN InvoiceOrderRelations ON Invoices.ID = InvoiceOrderRelations.Invoice WHERE (Invoices.Issued = 1) AND (Invoices.Voided = 0) AND (Invoices.isReverse = 0) AND (Invoices.ID <> '" & InvoiceID & "'))))"
  1086. conn.Execute(mySQL)
  1087. '---------- Declaring the related Quotes as Open -------------------
  1088. 'set Quotes which are ONLY related to this invoice, "Open"
  1089. 'that means, Quotes which are related to this invoice and are NOT related to any OTHER issued invoices.
  1090. mySQL ="UPDATE Quotes SET Closed=0 WHERE ID IN (SELECT [Quote] FROM InvoiceQuoteRelations WHERE (Invoice = '" & InvoiceID & "') AND ([Quote] NOT IN (SELECT InvoiceQuoteRelations.[Quote] FROM Invoices INNER JOIN InvoiceQuoteRelations ON Invoices.ID = InvoiceQuoteRelations.Invoice WHERE (Invoices.Issued = 1) AND (Invoices.Voided = 0) AND (Invoices.isReverse = 0) AND (Invoices.ID <> '" & InvoiceID & "'))))"
  1091. conn.Execute(mySQL)
  1092. end if
  1093. '**************************** Voiding ARItem of Invoice / Reverse Invoice ****************
  1094. '*** Type = 1 means ARItem is an Invoice
  1095. '*** Type = 4 means ARItem is a Reverse Invoice
  1096. '***
  1097. '********* Finding the ARItem of Invoice / Reverse Invoice
  1098. mySQL="SELECT ID FROM ARItems WHERE (Type = '"& itemType & "') AND (Link='"& InvoiceID & "')"
  1099. Set RS1=conn.Execute(mySQL)
  1100. voidedARItem=RS1("ID")
  1101. '********* Finding other ARItems related to this Item
  1102. if isReverse then
  1103. mySQL="SELECT ID AS RelationID, DebitARItem, Amount FROM ARItemsRelations WHERE (CreditARItem = '"& voidedARItem & "')"
  1104. Set RS1=conn.Execute(mySQL)
  1105. Do While not (RS1.eof)
  1106. '********* Adding back the amount in the relation, to the credit ARItem ...
  1107. conn.Execute("UPDATE ARItems SET RemainedAmount=RemainedAmount+ '"& RS1("Amount") & "', FullyApplied=0 WHERE (ID = '"& RS1("DebitARItem") & "')")
  1108. '********* Deleting the relation
  1109. conn.Execute("DELETE FROM ARItemsRelations WHERE ID='"& RS1("RelationID") & "'")
  1110. RS1.movenext
  1111. Loop
  1112. else
  1113. mySQL="SELECT ID AS RelationID, CreditARItem, Amount FROM ARItemsRelations WHERE (DebitARItem = '"& voidedARItem & "')"
  1114. Set RS1=conn.Execute(mySQL)
  1115. Do While not (RS1.eof)
  1116. '********* Adding back the amount in the relation, to the credit ARItem ...
  1117. conn.Execute("UPDATE ARItems SET RemainedAmount=RemainedAmount+ '"& RS1("Amount") & "', FullyApplied=0 WHERE (ID = '"& RS1("CreditARItem") & "')")
  1118. '********* Deleting the relation
  1119. conn.Execute("DELETE FROM ARItemsRelations WHERE ID='"& RS1("RelationID") & "'")
  1120. RS1.movenext
  1121. Loop
  1122. end if
  1123. '********* Voiding ARItem
  1124. conn.Execute("UPDATE ARItems SET RemainedAmount=0, FullyApplied=0, Voided=1 WHERE (ID = '"& voidedARItem & "')")
  1125. '**************************************************************
  1126. '* Affecting Account's AR Balance
  1127. '**************************************************************
  1128. if isReverse then
  1129. mySQL="UPDATE Accounts SET ARBalance = ARBalance - '"& invoiceFee & "' WHERE (ID='"& CustomerID & "')"
  1130. else
  1131. mySQL="UPDATE Accounts SET ARBalance = ARBalance + '"& invoiceFee & "' WHERE (ID='"& CustomerID & "')"
  1132. end if
  1133. conn.Execute(mySQL)
  1134. '***
  1135. '***---------------- End of Voiding ARItem of Invoice / Reverse Invoice ----------------
  1136. ' Sending a Message to Issuer ...
  1137. if trim(comment)<>"" then comment = chr(13) & chr(10) & "[" & comment & "]"
  1138. MsgTo = issuedBy
  1139. msgTitle = "Invoice Voided"
  1140. msgBody = "فاكتور فوق توسط "& session("CSRName") & " باطل شد." & comment
  1141. RelatedTable = "invoices"
  1142. relatedID = invoiceID
  1143. replyTo = 0
  1144. IsReply = 0
  1145. urgent = 1
  1146. MsgFrom = session("ID")
  1147. MsgDate = shamsiToday()
  1148. MsgTime = currentTime10()
  1149. Conn.Execute ("INSERT INTO Messages (MsgFrom, MsgTo, MsgTime, MsgDate, IsRead, MsgTitle, MsgBody, replyTo, IsReply, relatedID, RelatedTable, urgent) VALUES ( "& MsgFrom & ", "& MsgTo & ", N'"& MsgTime & "', N'"& MsgDate & "', 0, N'"& MsgTitle & "', N'"& MsgBody & "', "& replyTo & ", "& IsReply & ", "& relatedID & ", '"& RelatedTable & "', "& urgent & ")")
  1150. ' Copying the PreInvoice Data...
  1151. response.redirect "InvoiceInput.asp?act=copyInvoice&invoice="& InvoiceID & "&msg=" & Server.URLEncode(itemTypeName & " شماره "& InvoiceID & " باطل شد.")
  1152. '-----------------------------------------------------------------------------------------------
  1153. '--------------------------------------------S A M----------------------------------------------
  1154. '-----------------------------------------------------------------------------------------------
  1155. elseif request("act")="voidInvoiceOnly" then
  1156. if not Auth(6 , "M") then
  1157. 'Doesn't have the Priviledge to VOID the Invoice
  1158. response.write "<br>"
  1159. call showAlert ("شما مجاز به ابطال فاكتور نيستيد",CONST_MSG_ERROR)
  1160. response.end
  1161. end if
  1162. comment=sqlSafe(request("comment"))
  1163. InvoiceID=request("invoice")
  1164. if InvoiceID="" or not(isnumeric(InvoiceID)) then
  1165. response.write "<br>"
  1166. call showAlert ("خطا در شماره فاكتور",CONST_MSG_ERROR)
  1167. response.end
  1168. end if
  1169. InvoiceID=clng(InvoiceID)
  1170. mySQL="SELECT * FROM Invoices WHERE (ID='"& InvoiceID & "')"
  1171. Set RS1 = conn.Execute(mySQL)
  1172. if RS1.eof then
  1173. ShowErrorMessage("پيدا نشد ")
  1174. response.end
  1175. else
  1176. voided= RS1("Voided")
  1177. issued= RS1("Issued")
  1178. issuedBy= RS1("IssuedBy")
  1179. isReverse= RS1("IsReverse")
  1180. customerID= RS1("Customer")
  1181. invoiceFee= RS1("TotalReceivable")
  1182. IsA = RS1("IsA")
  1183. if voided then
  1184. ShowErrorMessage("اين فاكتور قبلا در تاريخ <span dir='LTR'>"& RS1("VoidedDate") & "</span> باطل شده است.")
  1185. response.end
  1186. end if
  1187. if isReverse then
  1188. itemType=4
  1189. else
  1190. itemType=1
  1191. end if
  1192. set myRS=conn.Execute("SELECT * FROM ARItems WHERE (Type = '"& itemType & "') AND (Link='"& InvoiceID & "') AND GL_Update=1")
  1193. if myRS.eof then
  1194. ShowErrorMessage("براي اين فاكتور قبلا سند حسابداري صادر شده است فلذا شما حق ابطال آنرا نداريد")
  1195. response.end
  1196. end if
  1197. end if
  1198. mySQL="UPDATE Invoices SET Voided=1, VoidedDate=N'"& shamsiToday() & "', VoidedBy='"& session("ID") & "' WHERE (ID='"& InvoiceID & "')"
  1199. conn.Execute(mySQL)
  1200. if isReverse then
  1201. isCredit=1
  1202. itemType=4
  1203. itemTypeName="فاكتور برگشت از فروش"
  1204. else
  1205. isCredit=0
  1206. itemType=1
  1207. itemTypeName="فاكتور"
  1208. '---------- Declaring the related orders as Open -------------------
  1209. 'mySQL="UPDATE Orders SET Closed=0 WHERE ID IN (SELECT [Order] FROM InvoiceOrderRelations WHERE (Invoice= '" & InvoiceID & "'))"
  1210. 'Changed By Kid ! 840509
  1211. 'set orders which are ONLY related to this invoice, "Open"
  1212. 'that means, orders which are related to this invoice and are NOT related to any OTHER issued invoices.
  1213. mySQL ="UPDATE Orders SET Closed=0 WHERE ID IN (SELECT [Order] FROM InvoiceOrderRelations WHERE (Invoice = '" & InvoiceID & "') AND ([Order] NOT IN (SELECT InvoiceOrderRelations.[ORDER] FROM Invoices INNER JOIN InvoiceOrderRelations ON Invoices.ID = InvoiceOrderRelations.Invoice WHERE (Invoices.Issued = 1) AND (Invoices.Voided = 0) AND (Invoices.isReverse = 0) AND (Invoices.ID <> '" & InvoiceID & "'))))"
  1214. conn.Execute(mySQL)
  1215. '---------- Declaring the related Quotes as Open -------------------
  1216. 'set Quotes which are ONLY related to this invoice, "Open"
  1217. 'that means, Quotes which are related to this invoice and are NOT related to any OTHER issued invoices.
  1218. mySQL ="UPDATE Quotes SET Closed=0 WHERE ID IN (SELECT [Quote] FROM InvoiceQuoteRelations WHERE (Invoice = '" & InvoiceID & "') AND ([Quote] NOT IN (SELECT InvoiceQuoteRelations.[Quote] FROM Invoices INNER JOIN InvoiceQuoteRelations ON Invoices.ID = InvoiceQuoteRelations.Invoice WHERE (Invoices.Issued = 1) AND (Invoices.Voided = 0) AND (Invoices.isReverse = 0) AND (Invoices.ID <> '" & InvoiceID & "'))))"
  1219. conn.Execute(mySQL)
  1220. end if
  1221. '**************************** Voiding ARItem of Invoice / Reverse Invoice ****************
  1222. '*** Type = 1 means ARItem is an Invoice
  1223. '*** Type = 4 means ARItem is a Reverse Invoice
  1224. '***
  1225. '********* Finding the ARItem of Invoice / Reverse Invoice
  1226. mySQL="SELECT ID FROM ARItems WHERE (Type = '"& itemType & "') AND (Link='"& InvoiceID & "')"
  1227. Set RS1=conn.Execute(mySQL)
  1228. voidedARItem=RS1("ID")
  1229. '********* Finding other ARItems related to this Item
  1230. if isReverse then
  1231. mySQL="SELECT ID AS RelationID, DebitARItem, Amount FROM ARItemsRelations WHERE (CreditARItem = '"& voidedARItem & "')"
  1232. Set RS1=conn.Execute(mySQL)
  1233. Do While not (RS1.eof)
  1234. '********* Adding back the amount in the relation, to the credit ARItem ...
  1235. conn.Execute("UPDATE ARItems SET RemainedAmount=RemainedAmount+ '"& RS1("Amount") & "', FullyApplied=0 WHERE (ID = '"& RS1("DebitARItem") & "')")
  1236. '********* Deleting the relation
  1237. conn.Execute("DELETE FROM ARItemsRelations WHERE ID='"& RS1("RelationID") & "'")
  1238. RS1.movenext
  1239. Loop
  1240. else
  1241. mySQL="SELECT ID AS RelationID, CreditARItem, Amount FROM ARItemsRelations WHERE (DebitARItem = '"& voidedARItem & "')"
  1242. Set RS1=conn.Execute(mySQL)
  1243. Do While not (RS1.eof)
  1244. '********* Adding back the amount in the relation, to the credit ARItem ...
  1245. conn.Execute("UPDATE ARItems SET RemainedAmount=RemainedAmount+ '"& RS1("Amount") & "', FullyApplied=0 WHERE (ID = '"& RS1("CreditARItem") & "')")
  1246. '********* Deleting the relation
  1247. conn.Execute("DELETE FROM ARItemsRelations WHERE ID='"& RS1("RelationID") & "'")
  1248. RS1.movenext
  1249. Loop
  1250. end if
  1251. '********* Voiding ARItem
  1252. conn.Execute("UPDATE ARItems SET RemainedAmount=0, FullyApplied=0, Voided=1 WHERE (ID = '"& voidedARItem & "')")
  1253. '**************************************************************
  1254. '* Affecting Account's AR Balance
  1255. '**************************************************************
  1256. if isReverse then
  1257. mySQL="UPDATE Accounts SET ARBalance = ARBalance - '"& invoiceFee & "' WHERE (ID='"& CustomerID & "')"
  1258. else
  1259. mySQL="UPDATE Accounts SET ARBalance = ARBalance + '"& invoiceFee & "' WHERE (ID='"& CustomerID & "')"
  1260. end if
  1261. conn.Execute(mySQL)
  1262. '***
  1263. '***---------------- End of Voiding ARItem of Invoice / Reverse Invoice ----------------
  1264. ' Sending a Message to Issuer ...
  1265. if trim(comment)<>"" then comment = chr(13) & chr(10) & "[" & comment & "]"
  1266. MsgTo = issuedBy
  1267. msgTitle = "Invoice Voided"
  1268. msgBody = "فاكتور فوق توسط "& session("CSRName") & " باطل شد." & comment
  1269. RelatedTable = "invoices"
  1270. relatedID = invoiceID
  1271. replyTo = 0
  1272. IsReply = 0
  1273. urgent = 1
  1274. MsgFrom = session("ID")
  1275. MsgDate = shamsiToday()
  1276. MsgTime = currentTime10()
  1277. Conn.Execute ("INSERT INTO Messages (MsgFrom, MsgTo, MsgTime, MsgDate, IsRead, MsgTitle, MsgBody, replyTo, IsReply, relatedID, RelatedTable, urgent) VALUES ( "& MsgFrom & ", "& MsgTo & ", N'"& MsgTime & "', N'"& MsgDate & "', 0, N'"& MsgTitle & "', N'"& MsgBody & "', "& replyTo & ", "& IsReply & ", "& relatedID & ", '"& RelatedTable & "', "& urgent & ")")
  1278. ' Copying the PreInvoice Data...
  1279. response.redirect "InvoiceInput.asp?act=copyInvoice&invoice="& InvoiceID & "&msg=" & Server.URLEncode(itemTypeName & " شماره "& InvoiceID & " باطل شد.")
  1280. '-----------------------------------------------------------------------------------------------
  1281. '-----------------------------------------------------------------------------------------------
  1282. '-----------------------------------------------------------------------------------------------
  1283. elseif request("act")="removePreInvoice" then
  1284. response.write "<br>"
  1285. if not Auth(6 , "G") then
  1286. 'Doesn't have the Priviledge to REMOVE the Pre-Invoice
  1287. call showAlert ("شما مجاز به حذف پيش نويس نيستيد",CONST_MSG_ERROR)
  1288. response.end
  1289. end if
  1290. InvoiceID=request("invoice")
  1291. if InvoiceID="" or not(isnumeric(InvoiceID)) then
  1292. call showAlert ("خطا در شماره پيش نويس",CONST_MSG_ERROR)
  1293. response.end
  1294. end if
  1295. InvoiceID=clng(InvoiceID)
  1296. mySQL="SELECT * FROM Invoices WHERE (ID='"& InvoiceID & "')"
  1297. Set RS1 = conn.Execute(mySQL)
  1298. if RS1.eof then
  1299. call showAlert ("پيش نويس پيدا نشد.",CONST_MSG_ERROR)
  1300. else
  1301. voided= RS1("Voided")
  1302. issued= RS1("Issued")
  1303. isReverse= RS1("IsReverse")
  1304. customerID= RS1("Customer")
  1305. IsA = RS1("IsA")
  1306. if issued then
  1307. call showAlert ("اين فاكتور صادر شده است.",CONST_MSG_ERROR)
  1308. elseif voided then
  1309. call showAlert ("اين فاكتور باطل شده است.",CONST_MSG_ERROR)
  1310. else
  1311. Conn.execute("DELETE FROM InvoiceOrderRelations Where (Invoice='" & InvoiceID & "')")
  1312. Conn.execute("DELETE FROM InvoiceQuoteRelations Where (Invoice='" & InvoiceID & "')")
  1313. 'Conn.execute("DELETE FROM InvoiceLines Where (Invoice='" & InvoiceID & "')")
  1314. 'Conn.execute("DELETE FROM Invoices Where (ID='" & InvoiceID & "')")
  1315. 'Changed By Kid 830929
  1316. 'Conn.execute("UPDATE Invoices SET Voided=1 Where (ID='" & InvoiceID & "')")
  1317. 'Changed By Kid 8400502 also adding VoidedBy & VoidedDate
  1318. Conn.execute("UPDATE Invoices SET Voided=1, VoidedDate=N'"& shamsiToday() & "', VoidedBy='"& session("ID") & "' WHERE (ID='"& InvoiceID & "')")
  1319. call showAlert ("پيش نويس حذف شد.",CONST_MSG_INFORM)
  1320. end if
  1321. end if
  1322. response.end
  1323. end if
  1324. conn.Close
  1325. %>
  1326. <!--#include file="include_JS_for_Invoices.asp" -->
  1327. <SCRIPT LANGUAGE="JavaScript">
  1328. <!--
  1329. function ApproveInvoice(){
  1330. if (confirm("آيا مطمئن هستيد كه مي خواهيد اين فاكتور را 'تاييد' كنيد؟\n\n(توجه: تغييرات ذخيره نمي شوند)\n"))
  1331. window.location="?act=approveInvoice&invoice=<%=InvoiceID%>";
  1332. }
  1333. function IssueInvoice(){
  1334. if (confirm("آيا مطمئن هستيد كه مي خواهيد اين فاكتور را 'صادر' كنيد؟\n\n(توجه: تغييرات ذخيره نمي شوند)\n"))
  1335. window.location="?act=IssueInvoice&invoice=<%=InvoiceID%>";
  1336. }
  1337. function VoidInvoice(){
  1338. if (confirm("آيا مطمئن هستيد كه مي خواهيد اين فاكتور را 'باطل' كنيد؟\n"))
  1339. window.location="?act=voidInvoice&invoice=<%=InvoiceID%>";
  1340. }
  1341. //-->
  1342. </SCRIPT>
  1343. <!--#include file="tah.asp" -->