[Form]
public class CostUpItem extends FormRun
{
//Template Name
const str CostUpItemTemplate = 'CostUpItemTemplate';
str importedFile;
public void exportCSV(str _filename)
{
CommaStreamIo io = CommaStreamIo::constructForWrite();
str filename = strFmt('%1.csv', _filename);
//write the header
if(_filename==CostUpItemTemplate)
{
io.writeExp(['Item No.','New Cost', 'Create Date']);
io.writeExp(['CBTGABC00001','2,150.00', '9/3/2018']);
}
//Get the stream
System.IO.Stream stream = io.getStream();
stream.Position = 0 ;
//StreamReader to read as str
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
str csvFileContent = reader.ReadToEnd();
//save file
File::SendStringAsFileToUser(csvFileContent, filename);
}
public void chkFields(container record)
{
Str itemno, newcost, createdate;
itemno = conPeek(record,1);
newcost = conPeek(record,2);
createdate = conPeek(record,3);
//check trackingno exist or not
SalesLine salesLine;
select firstonly salesLine where salesLine.ItemId == itemno
&& salesLine.DataAreaId == curExt();
if(!salesLine)
throw Error("Item no. (" + itemno + ") doesn't exist. " );
}
public void importRecords(container record)
{
CostUpItem.clear();
boolean _error = false;
BlankNoYes BlankNoYes;
Str itemno;
real cost;
date createdate;
itemno = conPeek(record,1);
cost = str2Num(conPeek(record,2));
createdate = str2Date(conPeek(record,3), 213);
if(importedFile == CostUpItemTemplate)
{
CostUpItem.ItemId = itemno;
CostUpItem.Cost = cost;
CostUpItem.CreateDate = createdate;
}
if(Common::validateRecord(CostUpItem))
CostUpItem.insert();
else
CostUpItem.clear();
}
// Import CSV file
public void loadCSVfile()
{
#File
FileUploadTemporaryStorageResult fileUpload;
boolean first = true;
boolean isInsert;
container record;
int totalOfLines;
CommaTextStreamIo localStream;
Num number;
fileUpload = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
if(fileUpload)
{
str textFile = fileUpload.getDownloadUrl();
localStream = CommaTextStreamIo::constructForRead(File::UseFileFromURL(textFile));
if (localStream.status() != IO_Status::Ok)
{
throw error(strfmt('Is not possible to open the file. Error %1',enum2str(localStream.status())));
}
localStream.inFieldDelimiter("\,");
localStream.inRecordDelimiter('\n');
localStream.inRecordDelimiter('\r\n');
record = localStream.read();
if(conlen(record) == 1)
{
throw error('Is not possible to import the file, incorrect format');
}
}
ttsbegin;
while(record)
{
if (conLen(record))
{
if (first) //header
{
first = false;
}
else
{
//check field and insert into CostUpItem
element.chkFields(record);
element.importRecords(record);
}
}
record = localStream.read();
}
ttscommit;
}
/// <summary>
///
/// </summary>
public void init()
{
super();
CostUpItem_ds.queryBuildDataSource().addSortField(fieldNum(CostUpItem,CreateDate), SortOrder::Descending);
}
[Control("Button")]
class LoadCSV
{
/// <summary>
///
/// </summary>
public void clicked()
{
importedFile = CostUpItemTemplate;
element.loadCSVfile();
CostUpItem_ds.executeQuery();
}
}
[Control("Button")]
class Update
{
/// <summary>
///
/// </summary>
public void clicked()
{
this.bulkupload();
CostUpItem_ds.executeQuery();
}
public void bulkupload()
{
DialogButton diagBut;
str strMessage = "Comfirm update today cost up item ?";
str strTitle = "Update Cost up item";
;
diagBut = Box::yesNo(
strMessage,
DialogButton::No, // Initial focus is on the No button.
strTitle);
if (diagBut == DialogButton::No)
{
//print "The No button was clicked.";
}
else
{
// print "The button that was clicked was: ", diagBut;
CostUpItem CostUpItem1;
while select CostUpItem1 where CostUpItem1.CreateDate==today()
{
SalesLine salesLine;
ttsbegin;
while select forupdate salesLine where salesLine.ItemId == CostUpItem1.ItemId
&& salesLine.SalesStatus == SalesStatus::Backorder
&& SalesLine.SalesType == SalesType::Sales
&& salesLine.DataAreaId == curExt()
{
salesLine.NewCost = CostUpItem1.Cost;
SalesLine.NewCostCreateDate = CostUpItem1.CreateDate;
SalesLine.doUpdate();
}
ttscommit;
}
Box::info("Sales Line is updated.", "Update ");
}
}
}
[Control("Button")]
class Template
{
/// <summary>
///
/// </summary>
public void clicked()
{
super();
element.exportCSV(CostUpItemTemplate);
}
}
}
留言列表